【发布时间】:2016-09-15 07:43:57
【问题描述】:
我对 Java 中的 GZip 有疑问。目前我使用 gzip 压缩的文件。一个 gzip 存档中的一个文件。如果我手动解压缩它们然后解析它们一切正常。但我想用 Java 和 GZipInputStream 自动化它,但它不起作用。 最后我需要有 DataInputStream 。我的代码是:
byte[] bytesArray = Files.readAllBytes(baseFile.toPath());
try {
reader = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(bytesArray)));
System.out.println("gzip");
} catch (ZipException notZip) {
reader = new DataInputStream(new ByteArrayInputStream(bytesArray));
System.out.println("no gzip");
}
我也试过 new GZIPInputStream(new FileInputStream(baseFile)); 结果是一样的。由于输出,我看到 Gzip 流毫无例外地创建,但后来我从 DataInputStream 获得无效数据。 请帮忙:)
【问题讨论】:
-
无效数据比如什么?什么时候有效数据应该是什么?怎么写的?
-
抱歉 :) 如果我使用原始文件或 gzip 压缩版本,reader.readByte() 会提供不同的结果。
标签: java gzip datainputstream gzipinputstream