Zip And Unzip File In C#

Zip And Unzip File In C# Average ratng: 5,6/10 1094 votes
Active7 months ago

I'm pretty sure this is not a duplicate so bear with me for just a minute.

I am testing a program that downloads a zipped directory that contains text files. I tried writing a utility that unzips the directory in c# but the version of C# does not support System.IO.Compression.ZipFile. I would like to be able to unzip the files to read and compare. Is there a way to read the zipped files.

How can I programatically (C#) ZIP a file (in Windows) without using any third party libraries? I need a native windows call or something like that; I really dislike the idea of starting a process, but I will if I absolutely have to. A PInovke call would be much better.

Failing that, let me tell you what I'm really trying to accomplish: I need the ability to let a user download a collection of documents in a single request. Any ideas on how to accomplish this?

Cheeso
138k78 gold badges411 silver badges646 bronze badges
Esteban ArayaEsteban Araya
18k21 gold badges94 silver badges135 bronze badges

8 Answers

Are you using .NET 3.5? You could use the ZipPackage class and related classes. Its more than just zipping up a file list because it wants a MIME type for each file you add. It might do what you want.

I'm currently using these classes for a similar problem to archive several related files into a single file for download. We use a file extension to associate the download file with our desktop app. One small problem we ran into was that its not possible to just use a third-party tool like 7-zip to create the zip files because the client side code can't open it -- ZipPackage adds a hidden file describing the content type of each component file and cannot open a zip file if that content type file is missing.

adrianbanks
68.7k18 gold badges150 silver badges189 bronze badges
Brian EnsinkBrian Ensink
9,7692 gold badges43 silver badges58 bronze badges

How can I programatically (C#) ZIP a file (in Windows) without using any third party libraries?

If using the 4.5+ Framework, there is now the ZipArchive and ZipFile classes.

Yves larock rise up lyrics. You need to add references to:

  • System.IO.Compression
  • System.IO.Compression.FileSystem

For .NET Core targeting net46, you need to add dependencies for

  • System.IO.Compression
  • System.IO.Compression.ZipFile

Example project.json:

For .NET Core 2.0, just adding a simple using statement is all that is needed:

  • using System.IO.Compression;
GalacticJelloGalacticJello
9,8952 gold badges19 silver badges32 bronze badges

I was in the same situation, wanting to .NET instead of a third party library. As another poster mentioned above, simply using the ZipPackage class (introduced in .NET 3.5) is not quite enough. There is an additional file that MUST be included in the archive in order for the ZipPackage to work. If this file is added, then the resulting ZIP package can be opened directly from Windows Explorer - no problem.

All you have to do is add the [Content_Types].xml file to the root of the archive with a 'Default' node for every file extension you wish to include. Once added, I could browse the package from Windows Explorer or programmatically decompress and read its contents.

How To Unzip A Downloaded File

More information on the [Content_Types].xml file can be found here: http://msdn.microsoft.com/en-us/magazine/cc163372.aspx

Here is a sample of the [Content_Types].xml (must be named exactly) file:

And the C# for creating a ZIP file:

Note:

  • This code uses the Stream.CopyTo method in .NET 4.0
  • This will become much simpler with the introduction of the ZipArchive class in .NET 4.5: http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive(v=vs.110).aspx
JoshuaJoshua
FLICKERFLICKER
4,2723 gold badges23 silver badges50 bronze badges

For a .NET 2.0 app I used SharpZipLib. Easy to use and open source.

Tim ScarboroughTim Scarborough
1,1451 gold badge9 silver badges19 bronze badges

Based off Simon McKenzie's answer to this question, I'd suggest using a pair of methods like this:

Community
mccdyl001mccdyl001

Looks like Windows might just let you do this..

Unfortunately I don't think you're going to get around starting a separate process unless you go to a third party component.

Dave SwerskyDave Swersky
31.9k7 gold badges68 silver badges110 bronze badges

Add these 4 functions to your project:

And use them like this:

TeemoTeemo

C# Unzip File To Folder

Not the answer you're looking for? Browse other questions tagged c#compressionzipdownload or ask your own question.

Document your code

Zip And Unzip File Using C#

Every project on GitHub comes with a version-controlled wiki to give your documentation the high level of care it deserves. It’s easy to create well-maintained, Markdown or rich text documentation alongside your code.

Sign up for free See pricing for teams and enterprises

Code Reference / Zip Samples

How to use SharpZipLib to work with Zip files

These samples try to cover the range of situations you will encounter.You may need to combine parts of each sample for your application.

General usage

  • Unpack a zip using ZipInputStream (eg for Unseekable input streams)

Specific usage

Create a Zip from a HTML string (C#)

Downloaded
Clone this wiki locally
Comments are closed.