.NET Zone is brought to you in partnership with:

I’ve been a Windows developer since 3.0 and caught the Visual Basic wave early with v1. I’ve released a “production” application in every version of VB since then (except VB for DOS). Focusing on enterprise, line-of-business development I’ve built Call Center Applications, Mortgage finance systems, Customer Relationship Management tools and more recently I’ve been in the Litigation Support/Electronic Data Discovery/Electronically Stored Information space. Greg is a DZone MVB and is not an employee of DZone and has posted 476 posts at DZone. You can read more from them at their website. View Full User Profile

.NET Framework 4.5 Gets Built-In ZIP Support

05.20.2012
| 4836 views |
  • submit to reddit

TugberkUgurlu.com - .NET 4.5 to Support Zip File Manipulation Out of the Box

"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.

..."

System.IO.Compression Namespace

The System.IO.Compression namespace contains classes that provide basic compression and decompression services for streams

image

ZipFile Class

"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.

..."

ZipArchive Class

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.

Published at DZone with permission of Greg Duncan, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)