SmartZip Archive File Library – Creating and Using Archive Files
(Page 1 of 4 )
Have you ever wanted to work with zip or other archive file formats in the .NET Framework? There is a free library that allows you to do just that. Keep reading to find out more.
Introduction
The .NET Framework has no built-in way to work with Zip or other archive file formats. However, this is often a useful option to have. You may want to send compressed files over the Internet, or create a feature on a program to automatically archive certain groups of files (such as logs) for better organization and space management.
Luckily, there is a free library which does this for us. Created by Mike Krueger, it is called the SmartZip library. It is open source and governed by the GPL. This library supports regular Zip format, as well as Gzip, Tar, and Bzip2. The code in this document will deal specifically with the regular Zip format; however, the class structures and methods for working with the other file formats are similar.
Creating a Zip File
In order to create a zip file, there are several steps you must perform. They are as follows:
- Create the ZipOutputStream object in the code.
- Set the compression ratio on the ZipOutputStream object. Now, for each file you want to add to the ZipOutputStream you must do the following:
- Open the file to add in a file stream.
- Create a byte buffer to store the file in memory
- Create a ZipEntry object to hold metadata for the file
- Use the write method to put the file into the output stream.
- After all the files you want to add have been added, you must call the finish and close methods to complete the file creation.
Overall, these steps are relatively simple, and don’t contain any major surprises. The only area where care is required surrounds keeping the directory structure intact within a zip file, and much of this care is handled by a method provided by the ZipEntry class, called “CleanName.” I will now describe this process in detail, and give some code snippits to illustrate each step.
First of all, we must create the ZipOutputStream object and set its compression ratio:
ZipOutputStream outStream=new ZipOutputStream(File.Create(“temp.zip”);
outStream.SetLevel(9);
These statements create an output stream object that is attached to the file “temp.zip” in the local working directory. This also sets the compression ratio, in this case to the highest compression. The compression level ranges from 0, which performs no compression and basically just combines several files into one, up to 9, which performs the most compression available in the Zip file standard.
After creating the ZipOutputStream object, you must begin adding files to the stream so they can be written. The following code does this for a single file:
FileStream fs = new FileStream(filename);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer,0,buffer.Length);
This code opens the file and puts it into a byte buffer. The write method for adding files to the output stream only accepts a byte buffer, so this is necessary. Now, we must create the ZipEnty class to hold the metadata about this particular file entry in the ZipOutputStream and write the byte buffer to the stream.
String entryName = ZipEntry.CleanName(filename);
ZipEntry entry = new ZipEntry(entryName);
outStream.PutNextEntry(entry);
outStream.Write(buffer,0,buffer.Length);
In this code block, we first create a string to hold the “clean” name of the file being added. This cleaned name is an important step because it strips away the parts of the .NET filename representation that don’t work with the Zip standard. For example, it cleans off the windows volume labels (such as “C:\”) or windows file share names (such as “\\server\dir”). You can add files to the Zip file and preserve their directory structure by using the complete relative filename from the current working directory.
This process above is then repeated for each file you wish to add to the Zip archive. It is also possible to build a function to walk through each directory in directory structure and add each one of those files to a zip archive and maintain that directory structure.
outStream.finish();
outStream.close();
The above lines of code are the last required after having finished everything you want to do with the zip file. The “finish” method is important because it writes out the final information for closing the zip file. If this method doesn’t get called, anything else that tries to access your zip file won’t be able to, as the file won’t be formed correctly. The close method closes the stream connection to the file and releases it for use by other processes.
Next: The ZipEntry Class >>
More .NET Articles
More By Michael Swanson