In this tutorial we’ll look at the System.IO.Compression Namespace offered in the .Net Framework for compressing & decompressing files. To create a zip archive with this Namespace is fairly straightforward and easy. We will demonstrate how to create a zip file and how to decompress a zip archive. One drawback of this method, when using the GZipStream class you are compressing a stream, not a file, so there’s no sensible way of handling multiple files because GZip contains no file header information (That’s where libraries like SharpZipLib comes into play, that's a dicsussion for a future tutorial).
For zipping a single file we will use the GZipStream class along with the FileStream class. For this method you will provide 2 parameters:
- The file to be zipped
- The name of the resulting zip file
We will open the file to be read, and then create a new stream for the resulting file. We will then create an instance of the GZipStream class and write the contents of the input file to it. This is how this is accomplished with native .Net:
/// <summary>
/// method for compressing a single file into a zip file
/// </summary>
/// <param name="file">the file we're compressing</param>
/// <param name="outputFile">the output zip file</param>
/// <returns></returns>
/// public bool
public bool CompressFile(string file, string outputFile)
{
try
{
//open the file to be compressed
using (var inFile = File.OpenRead(file))
{
//create a new stream from the resulting zip file to be created
using (var outFile = File.Create(outputFile))
{
//now create a GZipStream object for writing the input file to
using (var compress = new GZipStream(outFile, CompressionMode.Compress, false))
{
//buffer array to hold the input file in
byte[] buffer = new byte[inFile.Length];
//read the file into the FileStream
int read = inFile.Read(buffer, 0, buffer.Length);
//now loop (as long as we have data) and
//write to the GZipStream
while (read > 0)
{
compress.Write(buffer, 0, read);
read = inFile.Read(buffer, 0, buffer.Length);
}
}
}
}
return true;
}
catch (IOException ex)
{
MessageBox.Show(string.Format("Error compressing file: {0}", ex.Message));
return false;
}
}
Decompressing with the GZipStream is a lot like what we did when compressing a file. Your method will expect 2 parameters:
- The source file (your zip file)
- The name of the output file
From there we will open two file streams, one for the file being decompressed, and one for the output file. We will then open a new GZipStream object with the CompressionMode of Decompress so we can decompress the underlying stream we're working with. Then as long as we have data in our stream we write it to the output stream, then when it’s completed you will have a file. This is how our decompress method looks:
/// <summary>
/// method for decompressing a zip file
/// </summary>
/// <param name="source">the zip file we're decompressing</param>
/// <param name="destFile">the destination</param>
/// <returns></returns>
public bool Decompress(string source, string destFile)
{
try
{
//open a FileStream from the file we're decompressing
using (var inStream = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read))
{
//create a FileStream for the resulting output file
using (var outStream = new FileStream(destFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
//Open a GZipStream from the source zip file
using (var zipStream = new GZipStream(inStream, CompressionMode.Decompress, true))
{
//byte arrya the size of our input file
byte[] buffer = new byte[inStream.Length];
while (true)
{
int count = zipStream.Read(buffer, 0, buffer.Length);
if (count != 0)
outStream.Write(buffer, 0, count);
if (count != buffer.Length)
// have reached the end
break;
}
}
}
}
return true;
}
catch (Exception ex)
{
// handle or display the error
MessageBox.Show(string.Format("Error decompressing file {0}: {1}", source, ex.Message));
return false;
}
}
And there you have it, compressing & decompressing files with the System.IO.Compression Namespace in the .Net Framework. In the very near future we’ll take a look at using the SharpZipLib for creating a zip from an entire directory, decompressing a file and adding new files (or an entire directory) to an existing archive file. Thanks for reading



MultiQuote



|