C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Zip and Unzip files using C#


Posted Date: 16 Nov 2008    Resource Type: Code Snippets    Category: C# Syntax
Author: MASNSNMember Level: Gold    
Rating: 1 out of 5Points: 10



using System;

using System.IO;

using System.IO.Compression;



namespace appWin

{

///

/// This enumeration enables user to precise

/// to select mode zip or unzip

///


public enum Action{Zip,UnZip };

///

/// This class performs files compression and decompression

///


public class GZip

{

///

/// This is a private field that represents

/// the full source file path

///


private string _SourceFileName ="";

///

/// This is a private field that represents

/// the full destination file path

///


private string _DestinationFileName ="";

///

/// This byte array is used to stock both

/// The input file contents and out put file

/// contents as bytes

///


private byte[] oBuffer;

///

/// This is the class responsible of

/// zipping and unzipping files

///


private GZipStream oZipper;

///

/// This is a default constructor

///


public GZip() { }

///

/// This is an overloaded constructor

///


/// This represents the

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

///

/// This represents the

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

///

/// Choose between zip or unzip mode

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(); }



}

///

/// This is the source file full path property

///


public string SourceFileName

{

get

{

return _SourceFileName;

}

set

{

_SourceFileName = value;

}

}

///

/// This is the destination full path property

///


public string DestinationFileName

{

get

{

return _DestinationFileName;

}

set

{

_DestinationFileName = value;



}

}

///

/// 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

///


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

}

///

/// 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

///


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

{

///

/// This enumeration enables user to precise

/// to select mode zip or unzip

///


public enum Action{Zip,UnZip };

///

/// This class performs files compression and decompression

///


public class Deflat

{

///

/// This is a private field that represents

/// the full source file path

///


private string _SourceFileName ="";

///

/// This is a private field that represents

/// the full destination file path

///


private string _DestinationFileName ="";

///

/// This byte array is used to stock both

/// The input file contents and out put file

/// contents as bytes

///


private byte[] oBuffer;

///

/// This is the class responsible of

/// zipping and unzipping files

///


private DeflateStream oZipper;

///

/// This is a default constructor

///


public Deflat() { }

///

/// This is an overloaded constructor

///


/// This represents the

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

///

/// This represents the

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

///

/// Choose between zip or unzip mode

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(); }



}

///

/// This is the source file full path property

///


public string SourceFileName

{

get

{

return _SourceFileName;

}

set

{

_SourceFileName = value;

}

}

///

/// This is the destination full path property

///


public string DestinationFileName

{

get

{

return _DestinationFileName;

}

set

{

_DestinationFileName = value;



}

}

///

/// 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

///


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");

}

///

/// 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

///


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);

}



Responses

Author: KR    15 Dec 2008Member Level: Gold   Points : 1
Hi,

Nice coding..
Very useful for developers..

Thanks,

Regards
Vijay



Author: Deepika Haridas    16 Dec 2008Member Level: Diamond   Points : 0
Great one..
Thanks for posting it..
Very Useful.




Author: MASNSN    16 Dec 2008Member Level: Gold   Points : 1
Not at all you're welcome, you can read more of my articles, please visit

http://www.c-sharpcorner.com/Authors/AuthorDetails.aspx?AuthorID=yougerthen


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Zip Unzip file C#  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Encrypt decrypt data within your configuration file
Previous Resource: Implement a progress bar within Silverlight 2 context
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use