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

16 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

chengja said...

He alreadywow goldhas more receiving yardswow goldin one postseason thanwow goldany player in history.

chengja said...

He makes every defensive back look too slow,toodofus kamaskamas dofusacheter dofusbuy kamassmall or too weak toacheter kamascover him.On Fitzgerald's first touchdown.

chengja said...

On his second touchdown,Fitzgeraldworld of warcraft goldcheap wow goldwow oradjusted beautifully to Kurt Warner's throw while it was in the air.While Fitzgerald was making one of his patented leaping catches,Eagles safety Quintin Demps got twistedwow power levelingworld of warcraft goldwow gold kaufenwow gold cheaplike a Philadelphia pretzel.Demps ended upwow levelwow geldwow gold kaufenfalling down,allowingwow gold cheapwow powerlevelingFitzgerald to waltz into the end zone for a 62-yard touchdown.

chengja said...

The Eagles had not givenwow goldwow goldup more than 20 points in a gamewow goldwow goldsince Thanksgiving.

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#

vfdvgf said...

If you are eq2 plat looking buy ffxi gil as well as FFXI Gil WOW Power final fantasy gil Leveling lotro gold and World Of eve isk When you aoc gold need lineage 2 adena someone FFXI Gil to Maple Story mesos listen,world of warcraft gold I'll be 2moon dil there.lord of the rings gold When world of warcraft power leveling you world of warcraft power leveling need Guild Wars Gold a hug,final fantasy gil I'll be age of conan gold there.

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

Alexis said...

Yesterday when I renamed my zip file something happened and it was damamged.But fortunately in couple hours I found in google-cyclic redundancy repair zip.And it helped me very qucikly.It has free status as far as I know and can too works faster and recovers more documents, than other services.

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.

Anonymous said...

Hello. And Bye. great site great yes yess.
[url=http://free-sex-porn-xxx-clips.tumblr.com] sex[/url]
sex
http://free-sex-porn-xxx-clips.tumblr.com

Alex said...

I know a lot of tools,which works with other type of files. But yesterday I had a problem with my zip files and I couldn't keep in mind no one of it. Fortunately I called a friend-programmer up and he recommended this application - winzip fixer. It solved my situation for minutes and free of charge as far as I remember. I hazard the remark that the tool helped my sister too and she thanked me.

King Bayern Munich said...

This is a great article, supplies the useful information for me, thanks in this
soft sofa
Modern-stone-sculpture
billiards
Long span shelving
baby stroller
oil paintings
outdoor ceramic tiles
casual shoes
oil paintings