Skip to main content
Email Password   helpLost your password?
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:

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

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralFastZip class Pin
Ramon Smits
4:16 28 Apr '09  
Generalhow to Add File to a zip Pin
Member 1954892
1:14 17 Dec '08  
GeneralVB.Net Conversion Pin
Shawn McCuan
11:27 4 Jun '08  
Generalthis code is fine for a zipped folder which doesnt have any subfolders in it......but what if the zipped Pin
dedede
5:45 20 Feb '08  
Generalthis code is fine for a zipped folder which doesnt have any subfolders in it......but what if the zipped Pin
dedede
5:45 20 Feb '08  
GeneralGreat code, thanks. Pin
Quadravex8
21:54 15 Nov '07  
GeneralGood one Pin
chunchun2005
13:20 15 Nov '07  
GeneralThanks for the code! Pin
Devanstator
21:34 29 Sep '07  
GeneralRe: Thanks for the code! Pin
flankerfc
5:28 8 Oct '07  


Last Updated 14 Sep 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009