Zip and Unzip files using GZipStream and DeflateStream classes


The .NET framework provides us a way to compress and decompress data using the classes under the System.IO.Compression. There are two main classes that perform the job, namely the GZipStream class and the DeflateStream class. Both use the same algorithm to compress and decompress data but there are some differences. In fact, GZipStream method supports more formats specifications that DeflateStream method something that renders the compressed files according to the GZipStream more used in a multiplatform context that uses GZip method to compress and decompress files. In the other hand, the Deflate stream method is recommended if we are using compressed and decompressed files in a single platform context for example the same machine is used to compress, decompress and use the handled files at the same time because DeflateStream method, unlike the GZipStream, doesn't support additional information, flags and headers that facilitate the operation across different platforms. Hence, it will be considered are optimal solution for a mono platform.

In this article, I propose this solution that helps to compress and decompress files. First this is a class that uses GZipStream class

using System;

using System.IO;

using System.IO.Compression;

 

namespace appWin

{

/// <summary>

/// This enumeration enables user to precise

/// to select mode zip or unzip

/// </summary>

public enum Action{Zip,UnZip };

/// <summary>

/// This class performs files compression and decompression

/// </summary>

public class GZip

{

/// <summary>

/// This is a private field that represents

/// the full source file path

/// </summary>

private string _SourceFileName ="";

/// <summary>

/// This is a private field that represents

/// the full destination file path

/// </summary>

private string _DestinationFileName ="";

/// <summary>

/// This byte array is used to stock both

/// The input file contents and out put file

/// contents as bytes

/// </summary>

private byte[] oBuffer;

/// <summary>

/// This is the class responsible of

/// zipping and unzipping files

/// </summary>

private GZipStream oZipper;

/// <summary>

/// This is a default constructor

/// </summary>

public GZip() { }

/// <summary>

/// This is an overloaded constructor

/// </summary>

/// <param name="SourceFileName">This represents the

/// full source file name of the one going to be zipped

/// </param>

/// <param name="DestinationFileName">This represents the

/// full source file name of the one going to be unziped

/// </param>

/// <param name="action">Choose between zip or unzip mode</param>

public GZip(string SourceFileName, string DestinationFileName,Action action)

{

oZipper = null;

this.SourceFileName = SourceFileName;

this.DestinationFileName = DestinationFileName;

/* The user only precizes the zip mode

or the desired action in order to be performed

* instead of using the method directly that is

marked as protected, take a look below */

if (action == Action.Zip)

{ this.CompressFile(); }

if (action == Action.UnZip)

{ this.DecompressFile(); }

 

}

/// <summary>

/// This is the source file full path property

/// </summary>

public string SourceFileName

{

get

{

return _SourceFileName;

}

set

{

_SourceFileName = value;

}

}

/// <summary>

/// This is the destination full path property

/// </summary>

public string DestinationFileName

{

get

{

return _DestinationFileName;

}

set

{

_DestinationFileName = value;

 

}

}

/// <summary>

/// This is the method responsible for compression, it is marked

/// as protected because we use it is called at the constructor

/// level when a compression mode is chosen instead of using it directly

/// </summary>

protected void CompressFile()

{

 

if (File.Exists(SourceFileName))

{     

using (FileStream inputFile = File.Open(SourceFileName, FileMode.Open), outputFile = File.Create(DestinationFileName))

{

using (oZipper = new GZipStream(outputFile, CompressionMode.Compress))

{

    oBuffer = new byte[inputFile.Length];

    int counter = 0;

    while ((counter = inputFile.Read(oBuffer, 0, oBuffer.Length)) != 0)

    {

        oZipper.Write(oBuffer, 0, counter);

    }

}

oBuffer = null;

}

}

//TO DO here notify user that the task is performed

}

/// <summary>

/// This is the method responsible for compression, it is marked

/// as protected because we use it is called at the constructor

/// level when a decompression mode is chosen instead of using it directly

/// </summary>

protected void DecompressFile()

{

 

if (File.Exists(SourceFileName))

{

using (FileStream inputFile = File.Open(SourceFileName, FileMode.Open), outputFile = File.Create(DestinationFileName))

{

using (oZipper = new GZipStream(inputFile, CompressionMode.Decompress))

{

oBuffer = new byte[inputFile.Length];

int counter;

while ((counter = oZipper.Read(oBuffer, 0, oBuffer.Length)) != 0)

{

    outputFile.Write(oBuffer, 0, counter);

}

}

oBuffer = null;

}

}

MessageBox.Show("Decompression done");

}

}

//TO DO here notify user that the task is performed

 

}

Now, in order to consume the class service here you is the code

private void button1_Click(object sender, EventArgs e)

{

  GZip oZip = new GZip(@"C:\Test.txt", @"C:\Test1.txt", Action.Zip);

  oZip = new GZip(@"C:\Test1.txt", @"C:\Test2.txt", Action.UnZip);

}

This is another class but that uses DeflateStream class:

using System;

using System.IO;

using System.IO.Compression;

 

namespace appWin

{

/// <summary>

/// This enumeration enables user to precise

/// to select mode zip or unzip

/// </summary>

public enum Action{Zip,UnZip };

/// <summary>

/// This class performs files compression and decompression

/// </summary>

public class Deflat

{

/// <summary>

/// This is a private field that represents

/// the full source file path

/// </summary>

private string _SourceFileName ="";

/// <summary>

/// This is a private field that represents

/// the full destination file path

/// </summary>

private string _DestinationFileName ="";

/// <summary>

/// This byte array is used to stock both

/// The input file contents and out put file

/// contents as bytes

/// </summary>

private byte[] oBuffer;

/// <summary>

/// This is the class responsible of

/// zipping and unzipping files

/// </summary>

private DeflateStream oZipper;

/// <summary>

/// This is a default constructor

/// </summary>

public Deflat() { }

/// <summary>

/// This is an overloaded constructor

/// </summary>

/// <param name="SourceFileName">This represents the

/// full source file name of the one going to be zipped

/// </param>

/// <param name="DestinationFileName">This represents the

/// full source file name of the one going to be unziped

/// </param>

/// <param name="action">Choose between zip or unzip mode</param>

public Deflat(string SourceFileName, string DestinationFileName,Action action)

{

oZipper = null;

this.SourceFileName = SourceFileName;

this.DestinationFileName = DestinationFileName;

/* The user only precizes the zip mode

or the desired action in order to be performed

* instead of using the method directly that is

marked as protected, take a look below */

if (action == Action.Zip)

{ this.CompressFile(); }

if (action == Action.UnZip)

{ this.DecompressFile(); }

 

}

/// <summary>

/// This is the source file full path property

/// </summary>

public string SourceFileName

{

get

{

return _SourceFileName;

}

set

{

_SourceFileName = value;

}

}

/// <summary>

/// This is the destination full path property

/// </summary>

public string DestinationFileName

{

get

{

return _DestinationFileName;

}

set

{

_DestinationFileName = value;

 

}

}

/// <summary>

/// This is the method responsible for compression, it is marked

/// as protected because we use it is called at the constructor

/// level when a compression mode is chosen instead of using it directly

/// </summary>

protected void CompressFile()

{

 

if (File.Exists(SourceFileName))

{     

using (FileStream inputFile = File.Open(SourceFileName, FileMode.Open), outputFile = File.Create(DestinationFileName))

{

using (oZipper = new DeflateStream(outputFile, CompressionMode.Compress))

{

    oBuffer = new byte[inputFile.Length];

    int counter = 0;

    while ((counter = inputFile.Read(oBuffer, 0, oBuffer.Length)) != 0)

    {

        oZipper.Write(oBuffer, 0, counter);

    }

}

oBuffer = null;

}

}

MessageBox.Show("Compression done");

}

/// <summary>

/// This is the method responsible for compression, it is marked

/// as protected because we use it is called at the constructor

/// level when a decompression mode is chosen instead of using it directly

/// </summary>

protected void DecompressFile()

{

 

if (File.Exists(SourceFileName))

{

using (FileStream inputFile = File.Open(SourceFileName, FileMode.Open), outputFile = File.Create(DestinationFileName))

{

using (oZipper = new DeflateStream(inputFile, CompressionMode.Decompress))

{

oBuffer = new byte[inputFile.Length];

int counter;

while ((counter = oZipper.Read(oBuffer, 0, oBuffer.Length)) != 0)

{

    outputFile.Write(oBuffer, 0, counter);

}

}

oBuffer = null;

}

}

// TO DO here notify the user that the action is performed

}

}

}

Now, in order to consume the class service here you is the code

private void button1_Click(object sender, EventArgs e)

{

  GZip oZip = new GZip(@"C:\Test.txt", @"C:\Test1.txt", Action.Zip);

  oZip = new GZip(@"C:\Test1.txt", @"C:\Test2.txt", Action.UnZip);

}

I have marked the methods compress and decompress as protected in order do not to be used directly, instead in provide the choice for the developer via the enumeration Action to choose witch mode will be used and then the both methods are triggered from the constructor. This is more simple and elegant from my point of view.

Finally, and in order to provide more flexibility and more possibilities for developers to extend the both classes you can extract an interface as below:

using System;

namespace appWin

{

    interface IZipUnZip

    {

        /// <summary>

        /// This is the full path of the source path

        /// </summary>

        string DestinationFileName { get; set; }

        /// <summary>

        /// This is the full path of the destination path

        /// </summary>

        string SourceFileName { get; set; }

    }

}

Then make the both classes implement this interface

/// <summary>

/// This class performs files compression and decompression

/// </summary>

public class Deflat : appWin.IZipUnZip

{

.

.

.

/// <summary>

/// This class performs files compression and decompression

/// </summary>

public class Deflat : appWin.IZipUnZip

{

.

.

.

That's it

Good Dotneting!!!

Up Next
    Ebook Download
    View all
    Learn
    View all