I was excited that the base class library in .NET 4.5 now includes native support for working with Zip files, but in my excitement I overlooked
System.IO.Compression.FileSystem. The name of the assembly is actually misleading, because the namespace is still just
System.IO.Compression, and it only contains one type and that’s
ZipFile. This is a separate assembly from
System.IO.Compression.dll so you will need to add it as an additional reference.
ZipFile class is actually static, but it contains some four methods for quickly creating and reading zip files.
ZipFile.CreateFromDirectory(String, String) ZipFile.ExtractToDirectory(String, String) ZipFile.Open(String, ZipArchiveMode) ZipFile.OpenRead(String)
ZipFile.CreateFromDirectory(String, String) is useful for quickly archiving entire directories. I could backup my documents for instance.
I could do the reverse to restore my documents.
There are some limitations however. One is that these methods will throw exceptions if the target path already exists when creating an archive, or a target directory exists when extracting files. If an exception is thrown, it will remain partially extracted however many files and folders were previously successful.