ShowUsYour<Blog>

Irregular expressions regularly

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() ) ;
  }
}
Posted: Feb 25 2005, 09:54 PM by digory | with 8 comment(s)
Filed under:

Comments

TrackBack said:

# February 24, 2005 3:13 PM

Matt Hawley said:

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], "");
if(fileName.StartsWith("\\"))
fileName = fileName.Substring(1);
fileName = fileName.Replace("\\", "/");

This will then give proper structure & allow the built-in windows zip folders to properly read these files.
# February 25, 2005 7:54 AM

binh said:

student
# March 12, 2005 4:29 AM

ravi said:

i am not able to download from the above link..

if anybody has any other link pls post that link..

# February 14, 2008 6:13 AM

murali said:

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

# February 20, 2008 10:59 PM

Moayad said:

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

# March 27, 2008 10:05 AM

atul said:

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?

# November 19, 2008 10:41 AM

Cheeso said:

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!

# January 2, 2009 11:26 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)