Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Zip Up files

Tags:

c#

.net

zip

Whats the best way to zip up files using C#? Ideally I want to be able to seperate files into a single archive.

like image 761
AJM Avatar asked Nov 03 '09 11:11

AJM


3 Answers

You can use DotNetZip to archieve this. It´s free to use in any application.

Here´s some sample code:

   try
   {
     // for easy disposal
     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);
   }
like image 140
Jehof Avatar answered Oct 04 '22 17:10

Jehof


This is now built into the framework if you have version 4.5+

Otherwise, use Ionic.

Namespace is System.IO.Packaging.ZIPPackage.

See http://visualstudiomagazine.com/articles/2012/05/21/net-framework-gets-zip.aspx for a story.

like image 20
FastAl Avatar answered Oct 04 '22 16:10

FastAl


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...

like image 41
Jon Skeet Avatar answered Oct 04 '22 17:10

Jon Skeet