Whats the best way to zip up files using C#? Ideally I want to be able to seperate files into a single archive.
+1
A:
Have you looked at SharpZipLib?
I believe you can build zip files with classes in the System.IO.Packaging
namespace - but every time I've tried to look into it, I've found it rather confusing...
Jon Skeet
2009-11-03 11:50:32
+2
A:
Take a look at this library: http://www.icsharpcode.net/OpenSource/SharpZipLib/
It is pretty comprehensive, it deals with many formats, is open-source, and you can use in closed-source commercial applications.
It is very simple to use:
byte[] data1 = new byte[...];
byte[] data2 = new byte[...];
/*...*/
var path = @"c:\test.zip";
var zip = new ZipOutputStream(new FileStream(path, FileMode.Create))
{
IsStreamOwner = true
}
zip.PutNextEntry("File1.txt");
zip.Write(data1, 0, data1.Length);
zip.PutNextEntry("File2.txt");
zip.Write(data2, 0, data2.Length);
zip.Close();
zip.Dispose();
Bruno Reis
2009-11-03 11:51:13
+3
A:
You can use DotNetZip to archieve this. It´s free to use in any application.
Here´s some sample code:
try
{
using (ZipFile zip = new ZipFile()
{
// add this map file into the "images" directory in the zip archive
zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images");
// add the report into a different directory in the archive
zip.AddFile("c:\\Reports\\2008-Regional-Sales-Report.pdf", "files");
zip.AddFile("ReadMe.txt");
zip.Save("MyZipFile.zip");
}
}
catch (System.Exception ex1)
{
System.Console.Error.WriteLine("exception: " + ex1);
}
Jehof
2009-11-03 11:51:20
ps: I just added a multi-threaded deflate to DotNetZip. Because deflate is compute-intensive, using multiple threads makes it significantly faster on large files, when running on multi-core machines. In my measurements, it can cut 45% of the time from zipping a large archive, as compared to DotNetZip without the multi-thread support. And the programming model is no different - it's the same code on the outside. It's only the internals that have changed. You need v1.9.0.29 to get this speedup.
Cheeso
2009-11-04 17:20:30
+2
A:
There are a few librarys around - the most popular of which are DotNetZip and SharpZipLib.
Dan Diplo
2009-11-03 11:52:52