I was catching up on some MSDN Magazines that have been piled up and collecting dust for a few months, and I found a nice little article titled What’s New in the .NET 4.5 Base Class Library. The biggest news is the simplified asynchronous programming. This is huge, but I’ve had enough of it shoved down my throat since first hearing about it at PDC in 2010. Now, that’s not to say that I’m not excited about it; it’s just old news for “what’s new” to me.
I kept reading, though, and came across a section about new support for zip archives. I don’t do a lot with zip files, but it does come up from time to time. In the past, I’ve always been surprised that this wasn’t something natively supported in .NET. I’ve had to use open-source solutions like SharpZipLib (GPL) and DotNetZip (Ms-PL), but I always felt like I shouldn’t need a 3rd party library. It looks as though Microsoft agreed with that sentiment.
It seemed pretty cool and easy enough to use, so I wanted to check it out immediately. Here are some quick examples of how to take advantage of some of this new functionality in .NET 4.5. I created a WPF application that allows you to do each of the functions listed below. Note that the code samples reference some additional methods that aren’t included here. You can view the complete source on GitHub.
Extract an entire archive
private void OnExtractArchive(object sender, RoutedEventArgs e) { var archive = PromptForOpenFile( string.Empty, ".zip", "Zip archives (.zip)|*.zip"); if (string.IsNullOrEmpty(archive)) return; var destination = PromptForDirectory(); ZipFile.ExtractToDirectory(archive, destination); }
Extract a single file
private void OnExtractFile(object sender, RoutedEventArgs e) { var archive = PromptForOpenFile( string.Empty, ".zip", "Zip archives (.zip)|*.zip"); if (string.IsNullOrEmpty(archive)) return; using (ZipArchive zipArchive = ZipFile.Open(archive, ZipArchiveMode.Read)) { var itemToExtract = PromptForArchiveEntry(zipArchive); if (itemToExtract == null) return; var target = PromptForSaveFile( itemToExtract.FullName, string.Empty, "All files (.*)|*.*"); using (var fs = new FileStream(target, FileMode.Create)) { using (var contents = itemToExtract.Open()) { contents.CopyToAsync(fs); } } } }
Create an archive from a directory
private void OnCreateArchive(object sender, RoutedEventArgs e) { var dir = PromptForDirectory(); var target = PromptForSaveFile( "Archive.zip", ".zip", "Zip archives (.zip)|*.zip"); ZipFile.CreateFromDirectory(dir, target); }
Add a single file to an archive
private void OnAddFileToArchive(object sender, RoutedEventArgs e) { var archive = PromptForOpenFile( string.Empty, ".zip", "Zip archives (.zip)|*.zip"); if (string.IsNullOrEmpty(archive)) return; var file = PromptForOpenFile( string.Empty, ".*", "All files (.*)|*.*"); if (string.IsNullOrEmpty(archive)) return; using (ZipArchive zipArchive = ZipFile.Open(archive, ZipArchiveMode.Update)) { var name = Path.GetFileName(file); zipArchive.CreateEntryFromFile(file, name); } }