i need to zip a folder with all of the content inside help of GZipStream, or framework dlls .
-
Tuesday, February 28, 2012 11:54 AM
i need to zip a folder with all of the content inside help of GZipStream, or framework dlls only .
please give me only .net Framework Dlls names or some sample Code...
please help me.
All Replies
-
Tuesday, February 28, 2012 12:29 PM
I think the following article can fulfill your requirement
protected void btnZip_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); // Builder to save report string ZipFileName = String.Format(@"C:\ZippedFolders\({0}).MyZip.zip", DateTime.Now.ToString("yyyyMMdd")); // Zip Destiny File Name string theDirectory = @"C:\FolderToZip"; // Folder to zip try { sb.Append(String.Format("Directory To Zip: {0}.<br/>", theDirectory)); sb.Append(String.Format("Zip file: {0}.<br/>", ZipFileName)); string[] allFiles = Directory.GetFiles(theDirectory, "*.*", SearchOption.AllDirectories); // Get all files from // the folder to zip if (System.IO.File.Exists(ZipFileName)) // Small piece of code // to delete zip file if it already exists { System.IO.File.Delete(ZipFileName); sb.Append(String.Format ("Deleted old Zip file: {0}.<br/>", ZipFileName)); } // J# code to zip FileOutputStream fos = new FileOutputStream(ZipFileName); // J# output // stream (Zip File) ZipOutputStream zos = new ZipOutputStream(fos); // J# output zip zos.setLevel(9); // Set the level of compression. // It may be a value between 0 and 9 /* Add each file from folder to zip, to zip file. This way, the tree of the folder to zip will be reflected on the zip file */ for (int i = 0; i < allFiles.Length; i++ ) { string sourceFile = allFiles[i]; FileInputStream fis = new FileInputStream(sourceFile); // J# input //stream to fill zip file /* Add the entry to the zip file (The Replace will remove the full path Ex.: file C:\FolderToZip\Files\Tmp\myFile.xml, will be written as Files\Tmp\myFile.xml on the zip file If this code was not written, it would generate the whole tree since the beginning of the FolderToZip This way the zip file begins directly at the contents of C:\FolderToZip */ ZipEntry ze = new ZipEntry(sourceFile.Replace(theDirectory + @"\", "")); zos.putNextEntry(ze); sbyte[] buffer = new sbyte[1024]; int len; while ((len = fis.read(buffer)) >= 0) { zos.write(buffer, 0, len); // Write buffer to Zip File } fis.close(); // Close input Stream } // Close outputs zos.closeEntry(); zos.close(); fos.close(); sb.Append(String.Format("Folder {0} Zipped successfully to File {1}.<br/>", theDirectory, ZipFileName)); } catch (Exception eX) { sb.Append(String.Format("Error zipping folder {0}. Details: {1}. Stack Trace: {2}.<br/>", theDirectory, eX.Message, eX.StackTrace)); } lbReport.Text = sb.ToString(); // Show result/report }
Regards, Nighting Liu
-
Tuesday, February 28, 2012 12:35 PM
Hi Akash,
first you have to understand, that GZipStream is not creating ZIP files at all. It just compresses a stream of data. (A Zip-File is more because a ZIP file also is a container for multiple files and directories). So I fear you cannot create a ZIP file just with GZipStream.
You could use ZipPackage to create a ZIP file. Just have a look at http://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage.aspx for more details.
Maybe you want to think about the requirement to not use any 3rd party libraries. There are some nice libraries e.g. the SharpZipLib: http://www.icsharpcode.net/opensource/sharpziplib/
With kind regards,
Konrad
- Proposed As Answer by Link.fr Tuesday, February 28, 2012 3:06 PM
- Marked As Answer by Bob ShenMicrosoft Contingent Staff, Moderator Friday, March 09, 2012 7:19 AM

