.NET Framework 4.5 Gets Built-In ZIP Support
Join the DZone community and get the full member experience.Join For Free
"One of the missing feature of .NET framework was a support for Zip file manipulation such as reading the zip archive, adding files, extracting files, etc. and we were using some third party libraries such as excellent DotNetZip. In .NET 4.5, we have an extensive support for manipulating .zip files.
First thing that you should do is to add System.IO.Compression assembly as reference to your project. You may also want to reference System.IO.Compression.FileSystem assembly to access three extension methods (from the ZipFileExtensions class) for the ZipArchive class: CreateEntryFromFile, CreateEntryFromFile, and ExtractToDirectory. These extension methods enable you to compress and decompress the contents of the entry to a file.
The System.IO.Compression namespace contains classes that provide basic compression and decompression services for streams
"Provides static methods for creating, extracting, and opening zip archives.
The ZipFile class provides convenient static methods for working with zip archives. To use these methods, you must reference the System.IO.Compression.FileSystem assembly in your project. The System.IO.Compression.FileSystem assembly is not available for Metro style apps. Therefore, the ZipFile class and ZipFileExtensions class (which is also in the System.IO.Compression.FileSystem assembly) are not available in Metro style apps. In Metro style apps, you work with compressed files by using the methods in the ZipArchive, ZipArchiveEntry, DeflateStream, and GZipStream classes.
Represents a package of compressed files in the zip archive format.
The ZipArchive class enables you to work with a package of compressed files. The package contains an entry for each compressed file. You can:
Retrieve an entry by using the GetEntry method.
Retrieve the entire collection of entries by using the Entries property.
Create a new entry in the package by calling the overloaded CreateEntry method.
When you create a new entry, the file is compressed and added to the zip package. The CreateEntry method enables you to specify a directory hierarchy when adding the entry. You include the relative path of the new entry within the zip package. For example, creating a new entry with a relative path of AddedFolder\NewFile.txt creates a compressed text file in a directory named AddedFolder.
If you reference the System.IO.Compression.FileSystem assembly in your project, you can access three extension methods (from the ZipFileExtensions class) for the ZipArchive class: CreateEntryFromFile, CreateEntryFromFile, and ExtractToDirectory. These extension methods enable you to compress and decompress the contents of the entry to a file. The System.IO.Compression.FileSystem assembly is not available for Metro style apps. In Metro style apps, you can compress and decompress the contents of a zip archive by using the DeflateStream or GZipStream class.
In any case, it's good seeing this making it into the Framework. I do wonder that there's no encrypt/decrypt support. Aren't some encryption types part of the Zip format? (I know some some vendors add their own, but aren't there some that are more standard?). Or am I being stupid?
Still, it's good to at least get this level of zip support baked into the framework. Got to love incremental improvement.
Opinions expressed by DZone contributors are their own.