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 有点生疏,所以它可能会有所改进,但你明白了)