Microsoft .Net Framework 2.0 came with many new features which helps developers in problem solving. Among these new features you will find a namespace named as System.IO.Compression. This namespace is used to compress and decompress files, classes used for compressions and decompressions are GZipStream and DeflateStream. You can used either of the class to perform Compression and Decompression. These both classes are derived from Stream class.
In this article I will discuss only the GZipStream class. To get more understanding about this class consider an example that reads file from the system and place its compressed format in your project folder (InetPub\wwwroot\FileCompression\*.Zip).
To begin with this project, create a new website in VS2005 and add two controls HTMLFileInput (file browse) control, Button control:
<input id="browseFile" type="file" runat="server" /><br /><br />
<asp:Button ID="cmdCompressFile" runat="server" Text="Compress File" />
Protected Sub cmdCompressFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdCompressFile.Click
'A String object reads the file name (locally)
Dim FileName As String = Path.GetFileName(browseFile.Value)
'Stream object that reads file contents
Dim streamObj As Stream = browseFile.PostedFile.InputStream
'Allocate space in buffer according to the length of the file read
Dim buffer(streamObj.Length) As Byte
'Fill buffer
streamObj.Read(buffer, 0, buffer.Length)
streamObj.Close()
'File Stream object used to change the extension of a file
Dim compFile As System.IO.FileStream = File.Create(MapPath(Path.ChangeExtension(FileName, "zip")))
'GZip object that compress the file
Dim zipStreamObj As New GZipStream(compFile, CompressionMode.Compress)
'Write to the Stream object from the buffer
zipStreamObj.Write(buffer, 0, buffer.Length)
zipStreamObj.Close()
End Sub
Posted on 7/24/2006 9:55:17 AM by irshad
Posted on 7/29/2006 2:28:18 AM by IRSHAD
Posted on 12/28/2006 6:16:12 AM by Niral Patel
Posted on 1/31/2007 6:22:49 AM by Sunil Dhiman
Posted on 2/8/2007 5:50:13 PM by sabarish
Posted on 3/6/2007 2:17:59 AM by Haissam
Posted on 4/13/2007 1:48:03 AM by Ganesh
Posted on 6/29/2007 12:17:53 PM by Jeff Bazinet
Posted on 12/4/2007 12:11:35 AM by madhu
Posted on 1/21/2008 4:53:38 AM by CHetan
Posted on 3/28/2008 9:02:44 AM by navab
Posted on 5/8/2010 4:59:27 AM by abhi
Posted on 7/9/2010 12:45:28 AM by rajiv