Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#
Article

Make a Zip/UnZip Software using SharpZipLib

Rate me:
Please Sign up or sign in to vote.
4.33/5 (17 votes)
14 Sep 2007CPOL 592.2K   9K   45   14
This article shows how to use SharpZipLib to make a small Zip/UnZip software easily
Screenshot - cp.jpg

Introduction

This article shows a simple way to make a Zip/UnZip software using SharpZipLib.

Background

SharpZipLib is a very nice OpenSource library. It's a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. See this link.

Using the Code

You should use FileStream class as input/output. ZipInputStream and ZipOutputStream are two important classes in the project. ZipInputStream is used to unzip the zipped data to ordinary data. ZipOutputStream is used to zip the ordinary data to zipped data. And there is another class called ZipEntry. ZipEntry is an item zipped in the *.zip file.

I use a new thread to do the zip/unzip work because the large file may lead to the UI form not responding.

This is the zip method:

C#
public static void Zip(string SrcFile, string DstFile, int BufferSize)
{
    FileStream fileStreamIn = new FileStream
		(SrcFile, FileMode.Open, FileAccess.Read);
    FileStream fileStreamOut = new FileStream
		(DstFile, FileMode.Create, FileAccess.Write);
    ZipOutputStream zipOutStream = new ZipOutputStream(fileStreamOut);
    byte[] buffer = new byte<buffersize />;
    ZipEntry entry = new ZipEntry(Path.GetFileName(SrcFile));
    zipOutStream.PutNextEntry(entry);
    int size;
    do
    {
        size = fileStreamIn.Read(buffer, 0, buffer.Length);
        zipOutStream.Write(buffer, 0, size);
    } while (size > 0);
    zipOutStream.Close();
    fileStreamOut.Close();
    fileStreamIn.Close();
}

This is the unzip method:

C#
public static void UnZip(string SrcFile, string DstFile, int BufferSize)
{
    FileStream fileStreamIn = new FileStream
		(SrcFile, FileMode.Open, FileAccess.Read);
    ZipInputStream zipInStream = new ZipInputStream(fileStreamIn);
    ZipEntry entry = zipInStream.GetNextEntry();
    FileStream fileStreamOut = new FileStream
		(DstFile + @"\" + entry.Name, FileMode.Create, FileAccess.Write);
    int size;
    byte[] buffer = new byte<buffersize />;
    do
    {
        size = zipInStream.Read(buffer, 0, buffer.Length);
        fileStreamOut.Write(buffer, 0, size);
    } while (size > 0);
    zipInStream.Close();
    fileStreamOut.Close();
    fileStreamIn.Close();
}

Points of Interest

So you can see that it's very easy to make a small Zip/UnZip software. SharpZipLib is very powerful and you can do a lot of things with it.

History

  • 14th September, 2007 -- Article uploaded

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
China China
I live in Xi'an(西安). It has 3000+ years history and is one of the birthplaces of the ancient civilization of China.
accelerate your life.

Comments and Discussions

 
QuestionThanks Vote of 5 Pin
Laiju k6-Feb-18 23:25
professionalLaiju k6-Feb-18 23:25 
Questionunzip functionality is not working. Pin
arunk52516-Apr-15 5:01
arunk52516-Apr-15 5:01 
QuestionThank you Pin
Chuck Norris25-Jun-13 8:27
Chuck Norris25-Jun-13 8:27 
GeneralFastZip class Pin
Ramon Smits28-Apr-09 3:16
Ramon Smits28-Apr-09 3:16 
Questionhow to Add File to a zip Pin
Member 195489217-Dec-08 0:14
Member 195489217-Dec-08 0:14 
GeneralVB.Net Conversion Pin
Shawn McCuan4-Jun-08 10:27
Shawn McCuan4-Jun-08 10:27 
Generalthis code is fine for a zipped folder which doesnt have any subfolders in it......but what if the zipped Pin
dedede20-Feb-08 4:45
dedede20-Feb-08 4:45 
GeneralRe: this code is fine for a zipped folder which doesnt have any subfolders in it......but what if the zipped Pin
surahman7520-Dec-09 16:24
surahman7520-Dec-09 16:24 
Generalthis code is fine for a zipped folder which doesnt have any subfolders in it......but what if the zipped Pin
dedede20-Feb-08 4:45
dedede20-Feb-08 4:45 
GeneralGreat code, thanks. Pin
Quadravex815-Nov-07 20:54
Quadravex815-Nov-07 20:54 
GeneralGood one Pin
chunchun200515-Nov-07 12:20
chunchun200515-Nov-07 12:20 
GeneralThanks for the code! Pin
Devanstator29-Sep-07 20:34
Devanstator29-Sep-07 20:34 
GeneralRe: Thanks for the code! Pin
flankerfc8-Oct-07 4:28
flankerfc8-Oct-07 4:28 
GeneralRe: Thanks for the code! Pin
archana_barani7-Feb-10 17:18
archana_barani7-Feb-10 17:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.