Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Create Zip File Using Dotnet

Posted by Viral Sarvaiya on July 11, 2009

in the dotnet framework there are two method for compressing data

1) GZip

2)Deflate

here i am using GZip Stream (System.IO.Compression).

Steps

1) Create Simple Web Application (asp.net with C#).

2) put below control in to page.

2.1) Button (id=”btnCompress”)

3) put below code in to click event of button.

<code>//create one folder in the project (txt)
//get the folder physical path
string filepath = Server.MapPath("txt");
//create file StreamWriter sw = new StreamWriter(filepath + @"\1.txt");
sw.WriteLine("we compress this file after few min.");
sw.Close();
//open 1.txt file
FileStream objFile = File.OpenRead(filepath + @"\" + "1.txt");
//create zip file
FileStream objZipFile = File.Create(filepath + @"\zip.gz");
//this is compress stream
System.IO.Compression.GZipStream objGZip = new System.IO.Compression.GZipStream(objZipFile, CompressionMode.Compress);
//Read Bytes From Destination File
int bytes = objFile.ReadByte();
while (bytes != -1)
{
 //write byte in Compress File
 objGZip.WriteByte((byte)bytes);
 bytes = objFile.ReadByte();
}
objGZip.Close(); objZipFile.Close();</code>

Thanks.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: