【问题标题】:Issue with Zipped Streams from .Net and reading them from Java来自 .Net 的压缩流问题并从 Java 中读取它们
【发布时间】:2009-11-09 16:08:24
【问题描述】:

我正在尝试从 .Net 压缩一个可以从 Java 代码读取的流。 所以作为输入,我有一个字节数组,我想压缩它,我希望有一个二进制数组。

我已经用 SharpZipLib 和 DotNetZip 对压缩字节数组进行了测试, 但不幸的是,我在尝试使用 Java 中的 java.util.zip.Deflater 类解压缩它时总是出错。

有人有使用 .Net 压缩字符串或字节数组并使用 java.util.zip.Deflater 类对其进行解压缩的代码示例吗?

【问题讨论】:

  • 与其提供新代码,不如给我们展示一下不起作用的代码?
  • @Stan,请发布您的代码。

标签: java .net compression zip sharpziplib


【解决方案1】:

您不需要触摸DeflaterDeflater 处理解压缩 zip 文件中的各个条目。

ZipInputStream 是一个奇怪的类。如果您确实需要随机访问实际文件,还有ZipFile(出于多种原因,我不推荐它)。

【讨论】:

  • 其实我只需要压缩一个String。我不是在处理 ZIP 文件。斯坦
【解决方案2】:

Inflater 不读取 zip 流。它读取 ZLIB(或 DEFLATE)流。 ZIP 格式包含带有附加元数据的纯 DEFLATE 流。 Inflater 不处理该元数据。


如果您在 Java 端进行充气,则需要 Inflater。
在 .NET 方面,您可以使用 DotNetZip 中的 Ionic.Zlib.ZlibStream 类进行压缩 - 换句话说,生成 Java Inflater 可以读取的内容。

我刚刚测试过这个;此代码有效。 Java 端解压缩 .NET 端压缩的内容。

.NET 端:

byte[] compressed = Ionic.Zlib.ZlibStream .CompressString(originalText);
File.WriteAllBytes("ToInflate.bin", compressed);

Java 端:

public void Run()
    throws java.io.FileNotFoundException,
           java.io.IOException,
           java.util.zip.DataFormatException,
           java.io.UnsupportedEncodingException,
            java.security.NoSuchAlgorithmException
{
    String filename = "ToInflate.bin";
    File file = new File(filename);
    InputStream is = new FileInputStream(file);

    // Get the size of the file
    int length = (int)file.length();

    byte[] deflated = new byte[length];

    // Read in the bytes
    int offset = 0;
    int numRead = 0;
    while (offset < deflated.length
           && (numRead=is.read(deflated, offset, deflated.length-offset)) >= 0) {
        offset += numRead;
    }


    // Decompress the bytes
    Inflater decompressor = new Inflater();
    decompressor.setInput(deflated, 0, length);
    byte[] result = new byte[100];
    int totalRead= 0; 
    while ((numRead = decompressor.inflate(result)) > 0)
        totalRead += numRead;
    decompressor.end();

    System.out.println("Inflate: total size of inflated data: " + totalRead + "\n");

    result = new byte[totalRead];
    decompressor = new Inflater();        
    decompressor.setInput(deflated, 0, length);
    int resultLength = decompressor.inflate(result);
    decompressor.end();

    // Decode the bytes into a String
    String outputString = new String(result, 0, resultLength, "UTF-8");

    System.out.println("Inflate: inflated string: "  + outputString + "\n");
}

(我对 Java 有点生疏,所以它可能会有所改进,但你明白了)

【讨论】:

    【解决方案3】:

    这是 Sun 在 ZipStreams 上的页面: http://java.sun.com/developer/technicalArticles/Programming/compression/

    另一个处理 ZipStreams 的库是 POI。它更专注于使用 MS OFfic XML 格式文档,但它可能对如何处理流有一些不同的见解。 http://poi.apache.org/apidocs/org/apache/poi/openxml4j/opc/internal/marshallers/ZipPartMarshaller.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-23
      • 1970-01-01
      • 2020-12-01
      相关资源
      最近更新 更多