Home > Computer Programming > Zipping and Unzipping files Programmetically.

Zipping and Unzipping files Programmetically.

Following is the C# source code for Zipping a file using SharpZip

public void ZipFile(string source, string dest)
{
// this method zips the file form source directory to destinaton with same name
try
{
ZipEntry entry = new ZipEntry(Path.GetFileName(source));
FileInfo fi = new FileInfo(source);
entry.ExternalFileAttributes = (int)fi.Attributes;
entry.Size = fi.Length;
FileStream input = File.OpenRead(source);
ZipOutputStream output = new ZipOutputStream(File.Create(dest));
output.PutNextEntry(entry);
Byte [] buffer= new Byte[8192];
int len;
do
{
len = input.Read(buffer, 0 , buffer.Length);
if(len > 0)
output.Write(buffer, 0, len);
}while(len!=0);
output.Close();
input.Close();
}
catch
{}
}

I will soon upload the unzip code.

Haroon

Advertisements
Categories: Computer Programming
  1. No comments yet.
  1. No trackbacks yet.

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: