How to compress file in C#

No.of Views1474
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  17 Jun 2010 21:06:11
Tag : CSharp , Compression
in this codesnippet, i would describe how we can compress files with C#.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this code snippet help to compress files in .NET  2.0 or later version using C#.  first i have create a new  windows application using VS 2008, then  i have add a textbox and two buttons.

One button for compress and another button for decompress.finally i have added OpenDialog to select the file to compress. see below screen shot. 

Image Loading

Then add code to compress files under the Compress button click.before that you have add a reference to access Compress class in .net

using System.IO;using System.IO.Compression;

Once we added reference write code for select the source files to selection button click event.

private void btnOK_Click(object sender, EventArgs e){OpenFileDialog fdialog = new OpenFileDialog();if (fdialog.ShowDialog() == DialogResult.OK){txts.Text = fdialog.FileName;}}

Compress Button click  Event

private void btnCompress_Click(object sender, EventArgs e){try{// get information for the source fileFileInfo info = new FileInfo(txts.Text);if (info.Extension != ".gz"){byte[] data;using (FileStream sourcestream = File.OpenRead(info.FullName)){data = new byte[sourcestream.Length];// read source data to byte arraysourcestream.Read(data, 0, (int)sourcestream.Length);}using (FileStream dstream = File.Create((info.FullName + ".gz"))){using (GZipStream gzstream = new GZipStream(dstream, CompressionMode.Compress)){// write data to sys using compressgzstream.Write(data, 0, data.Length);}}}}catch (Exception ex){throw;}}

 

In Above code, just simple logic has added, first we have to read the source files and create byte array of the source data. Then i have create destination stream to from the same file with ".gz". The .GZ is extension of the new compression file.

then write data using compression stream with mode of Compression  to same location.

DeCompress Button click  Event

private void btnDCompress_Click(object sender, EventArgs e){try{FileInfo info = new FileInfo(txts.Text);if (info.Extension == ".gz"){byte[] data;using (FileStream sourcestream = File.OpenRead(info.FullName)){data = new byte[sourcestream.Length];sourcestream.Read(data, 0, (int)sourcestream.Length);}// the file name like abc.exe.gz// here i have remove last extension from the path.string destionationFileName = info.FullName.Remove(info.FullName.Length - info.Extension.Length);using (FileStream dstream = File.Create(destionationFileName)){using (GZipStream gzstream = new GZipStream(dstream, CompressionMode.Decompress)){gzstream.Write(data, 0, data.Length);}}}}catch (Exception ex){throw;}}

 

In above event,we have done just bi-direction task with mode of DeCompression.

that's all enjoy the file compression with few lines of code.

Sample Project Source

Download source files -37 kb

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
By:renukaDate Of Posted:1/20/2011 6:57:11 AM
i need coding to compress and decompress the file and folder using vb.net windows application
my form design is as same like your form design i need in vb.net windows application i need coding to compress and decompress the file and folder using vb.net windows application if possible give me code trw
By:renukaDate Of Posted:1/20/2011 6:52:42 AM
i cant compress and decompress the file ask like the sytem doing
I COMPRESSED AND DECOMPRESSED THE FILE BUT ,AFTER COMPRESSING I DOUBLE CLICKED THE ZIP FILE AND SEEN THERE THE FILE IS NOT IN THAT ORIGINAL FILE TYPE FOR EX: INPUT FILE :DOCUMENT.TXT OUTPUT FILE NY LIKE THAT. i need coding with out using external librarys(in vb.net windows application) if any one knws plz plz plz send me to my mail r iadd in this forum.i need trw
By:phongDate Of Posted:10/16/2010 6:53:29 AM
good well
i like it, if you display image before and after compress it is more much good.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top