Tuesday, December 2, 2008

Great Utility

SharpZipLib is a great little library that helps you programmatically compress/decompress zip archives. If your needs aren't too exotic (ie. you need to programmatically zip/unzip a series of files/folders) this could very well be your ticket.

You've probably heard of it before (it's been around for quite a while), some sample usage might look like this (compresses a file).

This is pretty normal usage. It adds a zip entry to an archive and creates said archive. What might confuse you though is the error you'll get if you try to to unpack the archive using a legacy unzip tool (ie. the stock Windows XP decompression tool).

The stock Windows XP extraction tool won't be able to unpack the archive and will complain about the archive being "invalid or corrupted" if you try to extract the archive contents.

Zip64 Extensions

What's happening here is that the utility is enabling Zip64 extensions for the archive, and some older utilities can't read Zip64 Extensions. You could simply turn off Zip64 Extensions, but this will cause problems when you start adding files larger than 4GB to your archive.

A better solution is to make a mild tweak to the way we add files to the archive. By specifying the size of the file we're adding, the ZipOutputStream can decide whether or not to use the Zip64 extensions. If we don't need them then they'll be turned off automatically. The mild tweak below fixes the error from the above code:

...ZipEntry entry = new ZipEntry(Path.GetFileName(sourceFilePath));entry.DateTime = DateTime.Now;/* By specifying a size, SharpZipLib will turn on/off UseZip64 based on the file sizes. If Zip64 is ON* some legacy zip utilities (ie. Windows XP) who can't read Zip64 will be unable to unpack the archive.* If Zip64 is OFF, zip archives will be unable to support files larger than 4GB. */entry.Size = new FileInfo(sourceFilePath).Length;zipStream.PutNextEntry(entry);...

Hope that helps someone. That error drove me crazy for a while and I didn't find much via Google.

17 comments:

Anonymous
said...

The timing of this post was great. I had a web app using the SharpZipLib code and had no issues using my current Winzip or 7-zip.

Users with Winzip 8.0, Freezip, and the XP Compressed Folders were encountering problems. What I noticed was if I opened the old zip file with XP Compressed Folders, it would display both the packed size and original size as 4Gb which certainly coincides with the Zip64 comment.

Thank you.This is also the workaround for the error if you add an empty file (0 bytes) via SharpZipLib 0.85.5 to an ZIP archive. WinZip 10.0 consider this files as invalid: "Error: invalid compressed data to inflate".

I was searching for a work-around, and saw this post. It looks like this is a fix to help people avoid FUTURE problems--could you possibly help me with a fix for something at hand?

I'm trying to unzip a password protected archive containing two video files, 486mb and 286mb. When I go to extract files/open archive, and put in password, I receive this message: "Error: invalid compressed data to inflate."

About Me

Tyler Holmes is a Solutions Architect working in Portland, Oregon. He lives mostly in the MS tech stack and is currently treading the waters of Communication/Collaboration and Business Intelligence with off the shelf/open source technologies.