Code to easily create zip files on the fly

The other day I posted about a free .NET library for creating zip files on the fly:

     http://weblogs.asp.net/dneimke/archive/2005/02/23/378781.aspx

Here's a little method that I wrote which wraps the functionality of the ICSharpCode library to easily package one or more files into a zip file.

 

private static void WriteZipFile( string[] filesToZip, string writeToFilePath ) {

  try {
    if ( EnsureDirectory(writeToFilePath) ) {
      
      Crc32 crc = new Crc32();
      ZipOutputStream s = new ZipOutputStream(File.Create(writeToFilePath));
      s.SetLevel(9); // 0 - store only to 9 - means best compression

      for( int i=0; i<filesToZip.Length; i++ ) {
      
        // Must use a relative path here so that files show up in the Windows Zip File Viewer
        // .. hence the use of Path.GetFileName(...)
        ZipEntry entry = new ZipEntry(Path.GetFileName(filesToZip[i]));
        entry.DateTime = DateTime.Now;

        // Read in the 
        using(FileStream fs = File.OpenRead(filesToZip[i])) {
  
          byte[] buffer = new byte[fs.Length];
          fs.Read(buffer, 0, buffer.Length);

          // set Size and the crc, because the information
          // about the size and crc should be stored in the header
          // if it is not set it is automatically written in the footer.
          // (in this case size == crc == -1 in the header)
          // Some ZIP programs have problems with zip files that don't store
          // the size and crc in the header.
          entry.Size = fs.Length;
          fs.Close();
      
          crc.Reset();
          crc.Update(buffer);
          entry.Crc  = crc.Value;
          s.PutNextEntry(entry);
          s.Write(buffer, 0, buffer.Length);
        }
      }
  
      s.Finish();
      s.Close();
    }
  } 
  catch( Exception ex ) {
    HttpContext.Current.Trace.Warn( ex.ToString() ) ;
  }
}

8 Comments

  • I've used this assembly in Unleash It, and the only thing I can suggest if you want to have folder structure with files, that you need to do the following instead of just setting the new ZipEntry to the name of the file:



    string fileName = fci.Path.Replace(filesToZip[i], &quot;&quot;);

    if(fileName.StartsWith(&quot;\\&quot;))

    fileName = fileName.Substring(1);

    fileName = fileName.Replace(&quot;\\&quot;, &quot;/&quot;);



    This will then give proper structure &amp; allow the built-in windows zip folders to properly read these files.

  • i am not able to download from the above link..
    if anybody has any other link pls post that link..

  • hi i can't extract icsharpcode.com libraray.it says it corrupted. can any body send me or let me know where i can download the library

  • dear All

    can any body tell me i tried to save zip file but if the files name in arabic it save the file defferent that the oroginal

  • I am using this library to zip the file. But it is being zipped by using zip64 encryption. I want it to be encrypted by zipArchive. Can I change encryption method?

  • DotNetZip is so much simpler to use. You don't have to worry about CRC's in the zip header, and you don't need to read in your own files. With DotNetZip, all this is taken care of for you. You have clear control over ZIP64 and ZIP (Weak) encryption. You get unicode support too.

    http:///www.codeplex.com/DotNetZip

    And yes, DotNetZip is still free!

  • Thanks alot for this piece of code, helped alot,
    never realized that
    entry.Size = fs.Length;
    would cause problem.. i did not include that in my code and was not able to access the zipped files from the server...
    hope it helps someone else too...

    thanks again

  • Dude code is OWESOME............

Comments have been disabled for this content.