我知道这个问题被问了很多次,但是我有一个类型为PDF的deflate字符串,这时我运行以下代码时出现此错误...
Exception in thread "main" java.util.zip.ZipException: invalid code lengths set
at java.util.zip.InflaterInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at dummy.ZlibCompression.shovelInToOut(ZlibCompression.java:66)
at dummy.ZlibCompression.decompressFile(ZlibCompression.java:53)
at dummy.ZlibCompression.main(ZlibCompression.java:78)
这是我的代码......
public static void compressFile(File raw, File compressed)
throws IOException
{
InputStream in = new FileInputStream(raw);
OutputStream out =
new DeflaterOutputStream(new FileOutputStream(compressed));
shovelInToOut(in, out);
in.close();
out.close();
}
/**
* Decompresses a zlib compressed file.
*/
public static void decompressFile(InputStream compressed, File raw)
throws IOException
{
System.out.println(compressed.available());
InputStream in =
new InflaterInputStream(compressed,
new Inflater(true));
System.out.println(in.available());
OutputStream out = new FileOutputStream(raw);
shovelInToOut(in, out);
in.close();
out.close();
}
/**
* Shovels all data from an input stream to an output stream.
*/
private static void shovelInToOut(InputStream in, OutputStream out)
throws IOException
{
byte[] buffer = new byte[in.available()];
int len;
while((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
}
/**
* Main method to test it all.
*/
public static void main(String[] args) throws IOException, DataFormatException {
// File compressed = new File("book1out.dfl");
// compressFile(new File("book1"), compressed); //StandardCharsets.UTF_8
decompressFile(new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8)), new File("F:\\decompressed.pdf"));
}
当我执行sysout时,我看到System.out.println(compressed.available());尺寸133844 和System.out.println(in.available());是1(我不知道为什么)
这里是deflate字符串Temp file link