Wednesday, April 18, 2007

How to Zip/Unzip files using C#.NET

How to Zip/Unzip files using C#.NET
Following example works only with Visual Studio .NET 2005 Framework 2.0
Namespace
using System.IO.Compression;
using System.IO;
Example data to run ZipFile function:
sourceFile = @"D:\Ritesh\standards.pdf"
destinationFile = @"C:\backup\standards.zip"
Private bool ZipFile(string sourceFile, string destinationFile)
{
using (FileStream oldFile = File.OpenRead(sourceFile)
using (FileStream newFile = File.Create(destinationFile)
using (GZipStream compression = new GZipStream(newFile, CompressionMode.Compress))
{
byte[] buffer = new byte[1024];
int numberOfBytesRead = oldFile.Read(buffer, 0, buffer.Length);
while (numberOfBytesRead > 0)
{
compression.Write(buffer, 0, numberOfBytesRead);
numberOfBytesRead = oldFile.Read(buffer, 0, buffer.Length);
}
compression.Close();
}
}
Example data to run UnZipFile function :
sourceFile = @"C:\backup\standards.zip
destinationFile = @"C:\backup\standards.pdf"
Private bool UnZipFile(string sourceFile, string destinationFile)
{
using(FileStream compressFile = File.Open(sourceFile,FileMode.Open))
using (FileStream uncompressedFile = File.Create(destinationFile)
using (GZipStream compression = new GZipStream(compressFile,
CompressionMode.Decompress))
{
int data = compression.ReadByte();
while(data != -1)
{
uncompressedFile.WriteByte((byte) data);
data = compression.ReadByte();
}
compression.Close();
}
}
Another way to Zip/Unzip files using Shell32.DLL in C#.NET
First you need to download Shell32.dll, you can download this DLL from following location.
Namespace
using Shell32;
using System.IO;
Example data to run ZipFile function:
sourceFolder = @"D:\New Folder\"
destinationFile = @"C:\backup\test.zip"
Private bool ZipFile(string sourceFolder, string destinationFile)
{
byte[] emptyzip = new byte[]{80,75,5,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
FileStream fs = File.Create(@"C:\backup\test.zip");
fs.Write(emptyzip, 0, emptyzip.Length);
fs.Flush();
fs.Close();
fs = null;
//Copy a folder and its contents into the newly created zip file
Shell32.ShellClass sc = new Shell32.ShellClass();
Shell32.Folder SrcFlder = sc.NameSpace(sourceFolder)
Shell32.Folder DestFlder = sc.NameSpace(destinationFile)
Shell32.FolderItems items = SrcFlder.Items();
DestFlder.CopyHere(items, 20);
}
Example data to run UnZipFile function :
inputFileName = @"C:\backup\standards.zip
destinationPath = @"C:\backup\"
private string UnzipFile(string inputFileName, string destinationPath)
{
Shell shell = new ShellClass();
Folder sourceFolder = shell.NameSpace(inputFileName);
Folder destinationFolder = shell.NameSpace(destinationPath);
string outputFileName = sourceFolder.Items().Item(0).Name;
destinationFolder.CopyHere(sourceFolder.Items(),"");
return outputFileName;
}

Ritesh Kumar Kesharwani

7 comments:

vaishu said...

i want to create a tool to push a .zip folder to remote machine using "psexec" and then unzip that folder to a directory and then execute it.
Could you please help me with this?
Thanks in advance.

Lovebloom said...
This comment has been removed by the author.
Lovebloom said...

thanks bhaiya

Mukul Keshari

Alex said...

Obviously in the world there tool which better than winzip,and to my mind this is next application-zip file check,it is better works with zip files and it resources wider as far as I can see than winzip,also it has free status as how as I remember,software permits to repair corrupted archives with *.zip extension,solve errors on example-CRC error and Cannot open file: it does not appear to be a valid archive,program for check repair zip file and check integrity of zip file uses several different algorithms for crc check fix zip file and data recovery, that is why, the process will take some time, according to CPU performance and file size,program will work under all supported versions of this operating system,will keep initial structure of your archive as well as original names of files and directories,check repair zip file can check why zip file is corrupted and work with password protected archives, but you should know this password.

yogesh said...

I have tried unzipping a .zip file but didn't work for me as u have used GZIPStream Uncompress.

The below link helped me out

UnZip files in .Net C#

Anonymous said...

If I want to Give a password to this zip file how to do that, can anyone help me out in this, Thanks in Advance

Anonymous said...

Hello, I like your way of doing unzip with Shell32. I followed it however, I am getting " The system cannot find the file specified" error at the shell.NameSpace(sourceZipFile). I have the Shell32.dll reference. I also have the Interop.shell32.dll in my both bin/debug and obj/debug. Is there way that you can shed some lights on this? I know many people on the internet made what you suggested work. some how it failed on me, my colleague got the same problem too. I think it is the environment that cause the problem, is there any place that I need to check from the environment point of view? I am using VS2005.