views:

3797

answers:

9
+8  Q: 

Zip folder in C#

What is an example (simple code) of how to zip a folder in C#?


Update:

I do not see namespace ICSharpCode. I downloaded ICSharpCode.SharpZipLib.dll but I do not know where to copy that DLL file. What do I need to do to see this namespace?

And do you have link for that MSDN example for compress folder, because I read all MSDN but I couldn't find anything.


OK, but I need next information.

Where should I copy ICSharpCode.SharpZipLib.dll to see that namespace in Visual Studio?

+13  A: 

There's nothing in the BCL to do this for you, but there are two great libraries for .NET which do support the functionality.

I've used both and can say that the two are very complete and have well-designed APIs, so it's mainly a matter of personal preference.

I'm not sure whether they explicitly support adding Folders rather than just individual files to zip files, but it should be quite easy to create something that recursively iterated over a directory and its sub-directories using the DirectoryInfo and FileInfo classes.

Noldorin
DotNetZip supports adding a Directory to a zip file, via the ZipFile.AddDirectory() methods. It recurses through the directory.
Cheeso
A: 
FileStream sourceFile = File.OpenRead(@"C:\sample.xml");
        FileStream destFile = File.Create(@"C:\sample.zip");

        GZipStream compStream = new GZipStream(destFile, CompressionMode.Compress);

        try
        {
            int theByte = sourceFile.ReadByte();
            while (theByte != -1)
            {
                compStream.WriteByte((byte)theByte);
                theByte = sourceFile.ReadByte();
            }
        }
        finally
        {
            compStream.Dispose();
        }

http://forums.asp.net/p/1086292/1743179.aspx

http://www.c-sharpcorner.com/UploadFile/neo_matrix/SharpZip04242007001341AM/SharpZip.aspx

Shoban
That's only going to "zip" a single file, and it won't be a real zip file but a gzip file with a .zip extension to it. Most utilities will open it just fine as they recognize the gzip format that they support, but other utilities will not.
Dave Van den Eynde
+2  A: 

There's an article over on MSDN that has a sample application for zipping and unzipping files and folders purely in C#. I've been using some of the classes in that successfully for a long time. The code is released under the Microsoft Permissive License, if you need to know that sort of thing.

EDIT: Thanks to Cheeso for pointing out that I'm a bit behind the times. The MSDN example I pointed to is in fact using DotNetZip and is really very fully-featured these days. Based on my experience of a previous version of this I'd happily recommend it.

SharpZipLib is also quite a mature library and is highly rated by people, and is available under the GPL license. It really depends on your zipping needs and how you view the license terms for each of them.

Rich

Xiaofu
The example code on MSDN uses DotNetZip, a free zip library that supports compression levels and encryption (including AES encryption), though the specific sample you cited does not show that. The library produces "proper" zip files.
Cheeso
Thanks for mentioning that. I'm still using the original version from a few years ago which was just a stand-alone code sample, so it looks like they've done a lot more work on it.
Xiaofu
My apologies to Cheeso, as it looks like you are the admin if not the author of the DotNetZip library! It's proved very useful to me, even in its early form from when I first encountered it. :)
Xiaofu
Edited based on Cheeso's comment.
Xiaofu
+4  A: 

From the DotNetZip help file, http://dotnetzip.codeplex.com/Release/ProjectReleases.aspx:

using (ZipFile zip = new ZipFile())
{
   zip.UseUnicode= true;  // utf-8
   zip.AddDirectory(@"MyDocuments\ProjectX");
   zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G") ; 
   zip.Save(ZipFileToCreate);
}
Simon
A: 

Just attach it in the object browser in VS

cyberzed
A: 

"Where should I copy ICSharpCode.SharpZipLib.dll to see that namespace in Visual Studio?"

You need to add the dll file as a reference in your project. Right click on References in the Solution Explorer->Add Reference->Browse and then select the dll.

Finally you'll need to add it as a using statement in whatever files you want to use it in.

AndyC
A: 

This code is used to Zip an entire folder. It's copied from this ZIP topic

   // Create a new instance.
   Zip zip = new Zip();
   // Create a new zip file.
   zip.Create("test.zip");
   // Set password
   zip.EncryptionAlgorithm = EncryptionAlgorithm.PkzipClassic;
   zip.Password = "password";
   // Add all files and subdirectories from 'c:\test' to the archive.
   zip.AddFiles(@"c:\test");
Mark Attwood
A: 

Following code uses a third-party ZIP component from Rebex:

// add content of the local directory C:\Data\  
// to the root directory in the ZIP archive
// (ZIP archive C:\archive.zip doesn't have to exist) 
Rebex.IO.Compression.ZipArchive.Add(@"C:\archive.zip", @"C:\Data\*", "");

Or if you want to add more folders without need to open and close archive multiple times:

using Rebex.IO.Compression;
...

// open the ZIP archive from an existing file 
ZipArchive zip = new ZipArchive(@"C:\archive.zip", ArchiveOpenMode.OpenOrCreate);

// add first folder
zip.Add(@"c:\first\folder\*","\first\folder");

// add second folder
zip.Add(@"c:\second\folder\*","\second\folder");

// close the archive 
zip.Close(ArchiveSaveAction.Auto);

You can download the ZIP component here.

Using a free, LGPL licensed SharpZipLib is a common alternative.

Disclaimer: I work for Rebex

Martin Vobr
A: 

There is a ZipPackage class in the System.IO.Packaging namespace which is built into .NET 3, 3.5, and 4.0.

http://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage.aspx

Here is an example how to use it. http://www.codeproject.com/KB/files/ZipUnZipTool.aspx?display=Print

dr