用Java解压缩GZ文件

huangapple go评论82阅读模式
英文:

Extract GZ file with Java

问题

我正在尝试从一个 GZ 文件中提取一个 CSV 文件。

到目前为止,我尝试了以下方法来执行这个操作:

Archiver archiver = ArchiverFactory.createArchiver(null, CompressionType.GZIP);
archiver.extract(archiveFile, destFile);

或者

GzipCompressorInputStream archive = new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(archiveFile)));
OutputStream out = new FileOutputStream(destFile);
IOUtils.copy(archive, out);
out.close();
archive.close();

或者

GZIPInputStream archive = new GZIPInputStream(new FileInputStream(archiveFile));
OutputStream out = new FileOutputStream(destFile);
IOUtils.copy(archive, out);
out.close();
archive.close();

我还尝试了在 GitHub 上的 (un)compressionSnappy

在每种情况下,我都得到了如下显示的错误

java.io.IOException: Gzip-compressed data is corrupt

我使用以下控制台命令检查了 GZ 文件的有效性,命令显示一切应该都没问题:

gzip -v -t MyFileToUncompress.csv.gz
MyFileToUncompress.csv.gz: OK

GZ 文件无论是通过控制台命令、Java 本身还是在 Windows 上进行的压缩,结果都相同。

我是不是做错了什么,还是这是我的 Java 有问题(JDK 1.7 或 1.8 都产生相同的异常)?

英文:

I'm trying to extract a CSV file from a GZ file.

So far, I've tried the following ways to make this operation:

Archiver archiver = ArchiverFactory.createArchiver(null, CompressionType.GZIP);
archiver.extract(archiveFile, destFile);

Or

GzipCompressorInputStream archive = new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(archiveFile)));
OutputStream out = new FileOutputStream(destFile);
IOUtils.copy(archive, out);
out.close();
archive.close();

Or

GZIPInputStream archive= new GZIPInputStream(new FileInputStream(archiveFile));
OutputStream out = new FileOutputStream(destFile);
IOUtils.copy(archive, out);
out.close();
archive.close();

I've also given a try to Snappy which is a (un)compression lib on github.

In every case, I got the following error displayed:

java.io.IOException: Gzip-compressed data is corrupt

I've checked the GZ files validity with the following console command, which says everything should be alright.

gzip -v -t MyFileToUncompress.csv.gz
MyFileToUncompress.csv.gz: OK

The GZ files were compressed by console command or by Java itself or on a Windows. Same result so far.

Is there something I'm doing wrong or is that an issue on my Java (JDK 1.7 or 1.8 produce the same exception) ?

答案1

得分: 0

这是我用于解压缩的代码,尽管看起来似乎不会产生与第三个示例不同的结果,因为它本质上与您的第三个示例相同:

try (final OutputStream out = Files.newOutputStream(fout);
     final InputStream in = new GZIPInputStream(Files.newInputStream(fin))) {
    in.transferTo(out);
}

然而,值得检查您是否使用了最新的JDK来查看结果是否有变化,并且还要检查 gzip -d MyFileToUncompress.csv.gz 是否生成了预期的文件。

英文:

Here is code I use for gunzip, though it doesn't look as though it would produce a different outcome as it is essentially same as your third example:

try(final OutputStream out = Files.newOutputStream(fout);
    final InputStream in   = new GZIPInputStream(Files.newInputStream(fin))) {
    in.transferTo(out);
}

However it is worth checking whether your result changes using latest JDK, and also check that gzip -d MyFileToUncompress.csv.gz generates the expected file back.

huangapple
  • 本文由 发表于 2020年8月27日 15:19:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63611018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定