Create Zip File Using Dotnet
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.
//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();
Thanks.
3 Comments »
Leave a comment
-
Archives
- June 2009 (2)
- December 2008 (1)
- October 2008 (2)
- September 2008 (8)
- August 2008 (6)
- July 2008 (8)
- June 2008 (3)
- May 2008 (8)
- April 2008 (11)
- March 2008 (3)
-
Categories
-
RSS
Entries RSS
Comments RSS
Hi,
I tried this code. I included the using directive “using System.IO.Compression;”. but the StreamWriter is not coming in the intellisense list. Though i compiled, it gives the obvious error “The type or namespace name ‘StreamWriter’ could not be found (are you missing a using directive or an assembly reference?) “. Pls help.
hi saravanan,
add below name space.
//add namespace
using System.IO;
using System.IO.Compression;
I find this link was working fine… Gzip C#