5,427,303 members and growing! (20,449 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Make a Zip/UnZip software using SharpZipLib

By flankerfc

This article shows how to use SharpZipLib to make a small Zip/UnZip software easily
C# 2.0, C#Windows, .NET, .NET 2.0, WinXPVS2005, Visual Studio, Dev

Posted: 14 Sep 2007
Updated: 14 Sep 2007
Views: 10,987
Bookmarked: 16 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 2.77 Rating: 3.27 out of 5
2 votes, 28.6%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
4 votes, 57.1%
4
1 vote, 14.3%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
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. http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx

Using the code

You should use FileStream class as input/output. ZipInputStream and ZipOutputStream is two important class in the project. ZipInputStream is used to unzip the zipped data to ordinary data. And 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.

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

This is the method of zip:

    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;
        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 method of unzip:

    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;
        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 it's very easy to make a small Zip/UnZip software.SharpZipLib is very powerful and you can do a lot of things.

History

Sept. 14, 2007 -- upload

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

flankerfc


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.
Occupation: Software Developer
Location: China China

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralVB.Net ConversionmemberShawn McCuan11:27 4 Jun '08  
Generalthis code is fine for a zipped folder which doesnt have any subfolders in it......but what if the zippedmemberdedede5:45 20 Feb '08  
Generalthis code is fine for a zipped folder which doesnt have any subfolders in it......but what if the zippedmemberdedede5:45 20 Feb '08  
GeneralGreat code, thanks.memberQuadravex821:54 15 Nov '07  
GeneralGood onememberchunchun200513:20 15 Nov '07  
GeneralThanks for the code!memberDevanstator21:34 29 Sep '07  
GeneralRe: Thanks for the code!memberflankerfc5:28 8 Oct '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Sep 2007
Editor:
Copyright 2007 by flankerfc
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project