views:

5543

answers:

5

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

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?

+26  A: 

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.

Brian Ensink
This has worked well in the past for me.
David
Oh SO, how I love you! Thanks Brian; yuo just saved us a lot of headaches and some $$$.
Esteban Araya
Note that this doesn't always work in reverse. Some Zip files will not rehydrate using the ZipPackage class. Files made with ZipPackage will so you should be good.
Craig
Glad I could help.
Brian Ensink
+3  A: 

Compress Zip files with Windows Shell API and C#

Dana Holt
You may not want to do this with the shell API because it shows a progress box while running. There's a flag to turn off the progress box but it does not always work.
Cheeso
@Cheeso - The article addresses that issue by moving the shell API code to a separate exe, then using .NET to run it specifying the process have a window style of hidden.
Dana Holt
Wow... Every time i see this article linked to, i cringe. If you're worried about adding a dependency to a third-party library or .exe, then adding one to what is essentially an add-in for the Windows Shell should send you running for the hills...
Shog9
@Shog9 - I agree it isn't something I would do, but it does meet the question's requirements. :)
Dana Holt
Heh... that it does. Fair enough.
Shog9
Esteban Araya
A: 

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 Swersky
A: 

You can use the GZipStream class to compress streams. There are lots of examples around on how to do what you want to do.

JP Alioto
Doesn't that use GZIP, not ZIP?
grawity
GZipStream does not read or write zip files. The doc is quite clear on this.
Cheeso
Wrapping a compressed stream with a file stream and writing it out to a file is trivial.
JP Alioto
Wrapping a GZipStream with a FileStream will produce a GZIP file, not a ZIP file.
Cheeso
A: 

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

Tim
I can't use 3rd party libs.
Esteban Araya
Shoud have read a little more closely. Doh! SharpZipLib is still a nice library for what it's worth.
Tim