I have seen many tutorials on how to compress a single file in c#. But I need to be able to create a normal *.zip file out of more than just one file. Is there anything in .NET that can do this? What would you suggest (baring in mind I'm under strict rules and cannot use other libraries)
Thank you
Just an update on this for anyone else who stumbles across this question.
Starting in .NET 4.5 you are able to compress a directory using
System.IO.Compression
into a zip file. You have to addSystem.IO.Compression.FileSystem
as a reference as it is not referenced by default. Then you can write:The only potential problem is that this assembly is not available for Windows Store Apps.
Here are a few resources you might consider: Creating Zip archives in .NET (without an external library like SharpZipLib)
Zip Your Streams with System.IO.Packaging
My recommendation and preference would be to use system.io.packacking. This keeps your dependencies down (just the framework). Jgalloway’s post (the first reference) provides a good example of adding two files to a zip file. Yes, it is more verbose, but you can easily create a façade (to a degree his AddFileToZip does that).
HTH
My 2 cents:
So Zip files could be created directly from files/dirs.
You should look into Zip Packages
They are a more structured version of normal ZIP archives, requiring some meta stuff in the root. So other ZIP tools can open a package, but the Sysytem.IO.Packaging API can not open all ZIP files.
You can now use the ZipArchive class (System.IO.Compression.ZipArchive), available from .NET 4.5
Example: Generating a zip of PDF files
Here's some code I wrote after using the above posts. Thanks for all your help.
This code accepts a list of file paths and creates a zip file out of them.