Search - Articles - Dev Forums - Favorites - Member Login
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009


Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Build the right apps the right way with powerful development tools.
Visual Studio 2010. Learn more...
How to compress file using .Net Framework 2.0

Author: Faraz
Download Source Code : 281_FileCompression.zip

In this article I will try to explain you the technique of compressing file using .Net framework 2.0. Here I will explain this new feature with the help of an example created in ASP.Net 2.0.

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

On cmdCompressFile_Click event add the following code:

 

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

Notice that the GZipStream constructor requires the two parameters, the stream that is to be compress and CompressionMode enumeration, which tells the class to compress or decompress file.

Article Comments
Compressing the files and folders using vb.net

Posted on 7/24/2006 9:55:17 AM by irshad

How to create a Remote Desktop application in VB.Net

Posted on 7/29/2006 2:28:18 AM by IRSHAD

pls give me solution that work with vb.net for compressing a file

Posted on 12/28/2006 6:16:12 AM by Niral Patel

Thanks
this artcle helps me very much.

Posted on 1/31/2007 6:22:49 AM by Sunil Dhiman

Hai thanks in advance,i have tried to use this code in .net2.0 it has not worked when i try to add the namespace System.IO.Compression is not exist in the application how to add and how to use my .net1.1 version

Posted on 2/8/2007 5:50:13 PM by sabarish

In asp.net 1.1 you can't use this library, the author of this article mentioned that its specific for ASP.NET 2.0. for more information about zipping files in asp.net 1.1 check the below link

http://dotnetslackers.com/community/blogs/haissam/archive/2007/02/19/Zipping-Files-in-Asp.net-1.1.aspx

Posted on 3/6/2007 2:17:59 AM by Haissam

The zipped file does not open. It geives the error as file is corrupted. any ideas y?

Posted on 4/13/2007 1:48:03 AM by Ganesh

Hello,

For anyone interested in using GZipStream to do compression of folders and multiple files, check out this article:

http://www.vwd-cms.com/forum/forums.aspx?topic=18

I hope that you find this article and the code samples helpful.

Jeff Bazinet

Posted on 6/29/2007 12:17:53 PM by Jeff Bazinet

hey i need a code to compress folder that contains many subfolder but subfolder must not be compressed only the main folder has to be compressed ....plz can any one help me regarding this............. thanks

Posted on 12/4/2007 12:11:35 AM by madhu

hey,
I got compression of file. its Gr8.
I also want code for decompress file.
can u send me reply on my mail ID?
please....

Posted on 1/21/2008 4:53:38 AM by CHetan

Our serach ends here..

Really helpful for us...

Thanks a lot.

Posted on 3/28/2008 9:02:44 AM by navab

it is national waste

Posted on 5/8/2010 4:59:27 AM by abhi

this is not a proper source code for file compression

Posted on 7/9/2010 12:45:28 AM by rajiv

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Upload File using FTP in ASP.Net 2.0

Disclaimer - Privacy
© 2002-2010 DevASP.net