简体   繁体   English

帮助从.Net创建Zip文件并从Java读取它们

[英]Help in creating Zip files from .Net and reading them from Java

I'm trying to create a Zip file from .Net that can be read from Java code. 我正在尝试从.Net创建一个可以从Java代码中读取的Zip文件。

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. 我已经使用SharpZipLib来创建Zip文件,但是如果根据#ZipLib库的CheckZip函数生成的文件是有效的并且可以通过WinZip或WinRar成功解压缩我尝试使用Java解压缩时总是会出错Java中的.Utils.Zip类。

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. 问题似乎是在SharpZipLib编写的错误标题中,我还在SharpDevelop论坛上发布了一个问题,但没有结果(请参阅http://community.sharpdevelop.net/forums/t/8272.aspx获取信息)但没有结果。

Has someone a code sample of compressing a Zip file with .Net and de-compressing it with the Java.Utils.Zip class? 有人使用.Net压缩Zip文件并使用Java.Utils.Zip类对其进行解压缩的代码示例吗?

Regards Massimo 关心马西莫

I have used DotNetZip library and it seems to work properly. 我使用过DotNetZip库 ,它似乎运行正常。 Typical code: 典型代码:

using (ZipFile zipFile = new ZipFile())
{
  zipFile.AddDirectory(sourceFolderPath);
  zipFile.Save(archiveFolderName);
}

I had the same problem creating zips with SharpZipLib (latest version) and extracting with java.utils.zip. 我在使用SharpZipLib(最新版本)创建拉链并使用java.utils.zip解压缩时遇到了同样的问题。

Here is what fixed the problem for me. 以下是解决这个问题的方法。 I had to force the exclusion of the zip64 usage: 我不得不强制排除zip64的用法:

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

s.UseZip64 = UseZip64.Off;

You don't wanna use the ZipPackage class in .NET - it isn't quite a standard zip model. 你不想在.NET中使用ZipPackage类 - 它不是一个标准的zip模型。 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. ZipPackage似乎已针对Office文档和XPS文档进行了优化。

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. 如果您正在使用通用ZIP文件并希望获得良好的互操作性,那么第三方库(如http://www.codeplex.com/DotNetZip )可能是更好的选择。

DotNetZip builds files that are very interoperable with just about everything, including Java's java.utils.zip. DotNetZip构建了几乎所有东西都可以互操作的文件,包括Java的java.utils.zip。 But be careful using features that Java does not support, like ZIP64 or Unicode. 但要小心使用Java不支持的功能,如ZIP64或Unicode。 ZIP64 is useful only for very large archives, which Java does not support well at this time, I think. ZIP64仅适用于非常大的档案,我认为Java目前不支持这种档案。 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. Java以特定方式支持Unicode,因此如果您使用DotNetZip生成基于Unicode的ZIP文件,您只需遵循一些规则即可正常工作。

无法使用SharpZipLib,但您可以尝试使用ZipPackageSystem.IO.Packaging创建zip文件,而无需使用第三方库(需要.NET 3+)。

I had a similar problem with unzipping SharpZipLib-zipped files on Linux. 我在Linux上解压缩SharpZipLib压缩文件时遇到了类似的问题。 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 我想我解决了它(我现在在Linux和Mac上工作,我测试过它),查看我的博客文章: http//igorbrejc.net/development/c/sharpziplib-making-it-work-for-linuxmac

To judge whether it's really a conformant ZIP file, see PKZIP's .ZIP File Format Specification . 要判断它是否真的是一个符合标准的ZIP文件,请参阅PKZIP的.ZIP文件格式规范

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. 值得一提的是,使用SharpZipLib在Windows Mobile设备上创建ZIP并使用WinZip或Windows XP的内置压缩文件夹功能打开它们也毫不费力,使用SharpZipLib在桌面上生成ZIP并使用我的处理方式也没有问题。在移动设备上拥有自己的ZIP提取实用程序(基本上是zlib的包装器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM