Working with Zip and ExcelXmlWriter - Using SharpZipLib
(Page 2 of 4 )
SharpZipLib’s GZip and Zip compression routines use the same basic process: create a stream for the zip type, write some bytes to it, and then close it. An example for GZip’s simplest incarnation is shown in Example 4-4.
Example 4-4. Using GZip with a single file
private void SingleGZip(string source)
{
string target = source + ".gz";
using (Stream s = new GZipOutputStream(File.Create(target)))
{
using (FileStream fs = File.OpenRead(source))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int) fs.Length);
s.Write(buffer, 0, buffer.Length);
}
}
}
Example 4-5 shows the simplicity of extracting a file from a GZip archive.
Example 4-5. Extracting a GZip file
using (Stream input = new GZipInputStream(File.OpenRead(source)))
{
using (FileStream output =
File.Create(Path.GetFileNameWithoutExtension(source)))
{
int buffSize = 2048;
byte[] outBuffer = new byte[2048];
while (true)
{
buffSize = input.Read(outBuffer, 0, buffSize);
if (buffSize > 0)
{
output.Write(outBuffer, 0, buffSize);
}
else
{
break;
}
}
}
}
Working with BZip2 compression is even easier. Static methods give quick access to compression and decompression functionality. Compressing a file is a simple matter, as shown in Example 4-6.
Example 4-6. Using BZip2 with a single file
private void BZip2Compress(string source)
{
string target = source + ".bz2";
int blockSize = 4096;
BZip2.Compress(File.OpenRead(source), File.Create(target), blockSize);
}
Decompressing likewise involves just a quick call to a static method, as you can see in Example 4-7.
Example 4-7. Extracting a BZip2 file
private void BZip2Decompress(string source) {
BZip2.Decompress(File.OpenRead(source),
File.Create(Path.GetFileNameWithoutExtension(source)));
}
Zip compression works much the same; however,ZipOutputStreamobjects can make use of theZipEntryclass.ZipEntryobjects work nicely when you want to pass in a list of files to add to an archive (say, a list of files a user has selected from a menu). Example 4-8 shows how to archive a list of files.
Example 4-8. Zipping multiple files
private void ZipMultipleCompress(string target, string[] fileNames,
int compressionLevel)
{
using (ZipOutputStream zipOutStream = new ZipOutputStream(File.Create(target)))
{
zipOutStream.SetLevel(compressionLevel);
foreach (string file in fileNames)
{
using (FileStream inStream = File.OpenRead(file))
{
byte[] buffer = new byte[inStream.Length];
inStream.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(file);
zipOutStream.PutNextEntry(entry);
zipOutStream.Write(buffer, 0, buffer.Length);
}
}
}
}
Another handy feature of theZipOutputStreamenables you to add checksums via SharpZipLib’sCrc32class. Create a newCrc32object:
Crc32 crc = new Crc32();
and then make use of it for eachZipEntryitem in your list:
// clear previous CRC, compute new one, add to entry
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
There’s also a handyFastZip class that offers quick creation and extraction of Zip files. It has a few restrictions, though:FastZipcan’t use absolute file paths, nor is it appropriate for collections of files—it’s better used for files within one directory tree.
Tar archives are fully supported in SharpZipLib as well. The usage pattern is very similar to the Zip method shown in Example 4-8. You’ll have to deal with creating output streams for the different compression types (BZip2, GZip) yourself, but this is straightforward and is well documented in the distribution’s examples.
Getting Support
SharpZipLib has an active community in forums at the library’s home page, where most issues you run into can be resolved quickly.
SharpZipLib in a Nutshell
SharpZipLib gives developers great flexibility and power in creating archives in standard, well-known formats.
Next: 4.11 Generating Excel Files from Code Using ExcelXmlWriter (Without Having Excel!) >>
More BrainDump Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the book Windows Developer Power Tools, written by James Avery and Jim Holmes (O'Reilly; ISBN: 0596527543). Check it out today at your favorite bookstore. Buy this book now.
|
|