Home > Computer Programming > Unzipping Files from Zip Archive !

Unzipping Files from Zip Archive !

Here comes the code for Unzipping files.

public void UnzipFile(string source, string destdir)
{
// This method unzips all files in "source" directory
// and unzips them in the destdir folder
if( (source.Substring( source.Length – 4 , 4)).Equals(".zip") == false)
return;
ZipInputStream MyZipInputStream; // for reading zip stream
FileStream MyFileStream; // for writing the contents of each file
MyZipInputStream = new ZipInputStream(new FileStream( source , FileMode.Open, FileAccess.Read));
ZipEntry MyZipEntry = MyZipInputStream.GetNextEntry();
while( MyZipEntry != null ) // till there are entries in archive
{
MyFileStream = new FileStream( destdir + "\\" + MyZipEntry.Name, FileMode.OpenOrCreate, FileAccess.Write);
int count;
Byte [] buffer = new Byte[4096];
count = MyZipInputStream.Read(buffer, 0, 4096);
while (count > 0) // this loop copies the contents of archived file in orignal text file
{
MyFileStream.Write(buffer, 0, count);
count = MyZipInputStream.Read(buffer, 0, 4096);
}
MyFileStream.Close();
MyZipEntry = MyZipInputStream.GetNextEntry();
}
MyZipInputStream.Close();
}

Regards,
I, Haroon

Advertisements
Categories: Computer Programming
  1. No comments yet.
  1. January 19, 2007 at 3:57 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: