views:

1144

answers:

6

I'm trying to create a Zip file from .Net that can be read from Java code.

I've used SharpZipLib to create the Zip file but also if the file generated is valid according to the CheckZip function of the #ZipLib library and can be successfully uncompressed via WinZip or WinRar I always get an error when trying to uncompress it using the Java.Utils.Zip class in Java.

Problem seems to be in the wrong header written by SharpZipLib, I've also posted a question on the SharpDevelop forum but with no results (see http://community.sharpdevelop.net/forums/t/8272.aspx for info) but with no result.

Has someone a code sample of compressing a Zip file with .Net and de-compressing it with the Java.Utils.Zip class?

Regards Massimo

+2  A: 

Can't help with SharpZipLib, but you can try to create zip file using ZipPackage class System.IO.Packaging without using 3rd part libraries (requires .NET 3+).

aku
Forgot to say: current solution works in the framework 2.0, but good suggestion about .Net 3.x when we'll port it
massimogentilini
+5  A: 

I have used DotNetZip library and it seems to work properly. Typical code:

using (ZipFile zipFile = new ZipFile())
{
  zipFile.AddDirectory(sourceFolderPath);
  zipFile.Save(archiveFolderName);
}
Panos
Thank you, I've quickly evaluated the suggested component and, from the CodePlex site, it seems a good solution. I'll check the code and let you know
massimogentilini
don't forget to surround the new ZipFile() in a using clause! ZipFile is IDisposable.
Cheeso
+1  A: 

To judge whether it's really a conformant ZIP file, see PKZIP's .ZIP File Format Specification.

For what it's worth I have had no trouble using SharpZipLib to create ZIPs on a Windows Mobile device and open them with WinZip or Windows XP's built-in Compressed Folders feature, and also no trouble producing ZIPs on the desktop with SharpZipLib and processing them with my own ZIP extraction utility (basically a wrapper around zlib) on the mobile device.

Mike Dimmick
+2  A: 

You don't wanna use the ZipPackage class in .NET - it isn't quite a standard zip model. Well it is, but it presumes a particular structure in the file, with a manifest with a well-known name, and so on. ZipPackage seems to have been optimized for Office docs and XPS docs.

A third-party library, like http://www.codeplex.com/DotNetZip, is probably a better bet if you are doing general-purpose ZIP files and want good interoperability.

DotNetZip builds files that are very interoperable with just about everything, including Java's java.utils.zip. But be careful using features that Java does not support, like ZIP64 or Unicode. ZIP64 is useful only for very large archives, which Java does not support well at this time, I think. Java supports Unicode in a particular way, so if you produce a Unicode-based ZIP file with DotNetZip, you just have to follow a few rules and it will work fine.

Cheeso
A: 

I had a similar problem with unzipping SharpZipLib-zipped files on Linux. I think I solved it (well I works on Linux and Mac now, I tested it), check out my blog post: http://igorbrejc.net/development/c/sharpziplib-making-it-work-for-linuxmac

Igor Brejc
A: 

I had the same problem creating zips with SharpZipLib (latest version) and extracting with java.utils.zip.

Here is what fixed the problem for me. I had to force the exclusion of the zip64 usage:

ZipOutputStream s = new ZipOutputStream(File.Create(someZipFileName))

s.UseZip64 = UseZip64.Off;

This solution worked for me, however I up-voted it before I had a chance to test it, now I can't upvote it again.
Dave