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
- 14th September, 2007 -- Article uploaded
| You must Sign In to use this message board. |
|
|
 |
|
|
 |
|
 |
Hi,
Is it possible to add a file to a zip file ? And how ?
Thanks very much for your great code.
Emmanuel
|
| Sign In·View Thread·PermaLink | 1.50/5 |
|
|
|
 |
|
 |
Here is a VB.Net version of the above code:
Public Sub zip(ByVal srcFile As String, ByVal dstFile As String, ByVal bufferSize As Integer) Dim fileStreamIn As New FileStream(srcFile, FileMode.Open, FileAccess.Read) Dim fileStreamOut As New FileStream(dstFile, FileMode.Create, FileAccess.Write) Dim zipOutStream As New ZipOutputStream(fileStreamOut) Dim buffer(bufferSize) As Byte Dim entry As New ZipEntry(Path.GetFileName(srcFile)) zipOutStream.PutNextEntry(entry) Dim size As Integer
Do size = fileStreamIn.Read(buffer, 0, buffer.Length) zipOutStream.Write(buffer, 0, size) Loop While size > 0
zipOutStream.Close() fileStreamOut.Close() fileStreamIn.Close() End Sub
Public Sub unZip(ByVal srcFile As String, ByVal dstFile As String, ByVal bufferSize As Integer) Dim fileStreamIn As FileStream = New FileStream(srcFile, FileMode.Open, FileAccess.Read) Dim zipInStream As ZipInputStream = New ZipInputStream(fileStreamIn) Dim entry As ZipEntry = zipInStream.GetNextEntry Dim fileStreamOut As FileStream = _ New FileStream(dstFile + "\" + entry.Name, FileMode.Create, FileAccess.Write) Dim buffer(bufferSize) As Byte Dim size As Integer
Do size = zipInStream.Read(buffer, 0, buffer.Length) fileStreamOut.Write(buffer, 0, size) Loop While size > 0
zipInStream.Close() fileStreamOut.Close() fileStreamIn.Close() End Sub
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
hi, this code is fine for a zipped folder which doesnt have any subfolders in it......but what if the zipped folder contains a sub folder and then the zip folder needs to be unzipped. In such a case, the above code does not work.
|
| Sign In·View Thread·PermaLink | 2.67/5 |
|
|
|
 |
|
 |
hi, this code is fine for a zipped folder which doesnt have any subfolders in it......but what if the zipped folder contains a sub folder and then the zip folder needs to be unzipped. In such a case, the above code does not work. any work arounds?
|
| Sign In·View Thread·PermaLink | 2.67/5 |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
It's a little hard to find documentation on ShaprZipLib, so thanks! Here's a modified version which will allows you to extract more than one file:
<code> public void UnZip(string SrcFile, string DstDir) { FileStream fileStreamIn = new FileStream(SrcFile, FileMode.Open, FileAccess.Read); ZipInputStream zipInStream = new ZipInputStream(fileStreamIn);
ZipEntry entry; int size; byte[] buffer = new byte[BUFFERSIZE]; while ((entry = zipInStream.GetNextEntry()) != null) { FileStream fileStreamOut = new FileStream(DstDir + @"\" + entry.Name, FileMode.Create, FileAccess.Write); while((size = zipInStream.Read(buffer, 0, buffer.Length)) > 0) { fileStreamOut.Write(buffer, 0, size); } fileStreamOut.Close(); } zipInStream.Close(); fileStreamIn.Close(); } </code>
Devan
|
| Sign In·View Thread·PermaLink | 1.67/5 |
|
|
|
 |
|
|
 |
|
|