I have a bunch of ZIP files that are in desperate need of some hierarchical reorganization and extraction. What I can do, currently, is create the directory structure and move the zip files to the proper location. The mystic cheese that I am missing is the part that extracts the files from the ZIP archive.
I have seen the MSDN articles on the ZipArchive
class and understand them reasonable well. I have also seen the VBScript ways to extract. This is not a complex class so extracting stuff should be pretty simple. In fact, it works "mostly". I have included my current code below for reference.
using (ZipPackage package = (ZipPackage)Package.Open(@"..\..\test.zip", FileMode.Open, FileAccess.Read)) { PackagePartCollection packageParts = package.GetParts(); foreach (PackageRelationship relation in packageParts) { //Do Stuff but never gets here since packageParts is empty. } }
The problem seems to be somewhere in the GetParts
(or GetAnything for that matter). It seems that the package, while open, is empty. Digging deeper the debugger shows that the private member _zipArchive shows that it actually has parts. Parts with the right names and everything. Why won't the GetParts
function retrieve them? I'ver tried casting the open to a ZipArchive and that didn't help. Grrr.
To unzip a single file or folder, open the zipped folder, then drag the file or folder from the zipped folder to a new location. To unzip all the contents of the zipped folder, press and hold (or right-click) the folder, select Extract All, and then follow the instructions.
If you are manipulating ZIP files, you may want to look into a 3rd-party library to help you.
For example, DotNetZip, which has been recently updated. The current version is now v1.8. Here's an example to create a zip:
using (ZipFile zip = new ZipFile()) { zip.AddFile("c:\\photos\\personal\\7440-N49th.png"); zip.AddFile("c:\\Desktop\\2005_Annual_Report.pdf"); zip.AddFile("ReadMe.txt"); zip.Save("Archive.zip"); }
Here's an example to update an existing zip; you don't need to extract the files to do it:
using (ZipFile zip = ZipFile.Read("ExistingArchive.zip")) { // 1. remove an entry, given the name zip.RemoveEntry("README.txt"); // 2. Update an existing entry, with content from the filesystem zip.UpdateItem("Portfolio.doc"); // 3. modify the filename of an existing entry // (rename it and move it to a sub directory) ZipEntry e = zip["Table1.jpg"]; e.FileName = "images/Figure1.jpg"; // 4. insert or modify the comment on the zip archive zip.Comment = "This zip archive was updated " + System.DateTime.ToString("G"); // 5. finally, save the modified archive zip.Save(); }
here's an example that extracts entries:
using (ZipFile zip = ZipFile.Read("ExistingZipFile.zip")) { foreach (ZipEntry e in zip) { e.Extract(TargetDirectory, true); // true => overwrite existing files } }
DotNetZip supports multi-byte chars in filenames, Zip encryption, AES encryption, streams, Unicode, self-extracting archives. Also does ZIP64, for file lengths greater than 0xFFFFFFFF, or for archives with more than 65535 entries.
free. open source
get it at codeplex or direct download from windows.net - CodePlex has been discontinued and archived
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With