【问题标题】:Unexpected Java GZIPOutputStream results意外的 Java GZIPOutputStream 结果
【发布时间】:2019-03-12 14:07:07
【问题描述】:

我们有以下 Java 方法来使用 GZIPOutputStream 压缩文件

 private void archive(Path originalFile) {
    Path tempFile = originalFile.resolveSibling(originalFile.toFile().getName() + TEMPORARY_FILE_EXTENSION);
    Path gzippedFile = originalFile.resolveSibling(originalFile.toFile().getName() + ARCHIVED_FILE_EXTENSION);
    try {
        try (FileInputStream input = new FileInputStream(originalFile.toFile());
            BufferedOutputStream output = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(tempFile.toFile())))) {
            IOUtils.copy(input,output);
            output.flush();
        }
        Files.move(tempFile, gzippedFile, StandardCopyOption.REPLACE_EXISTING);
        Files.delete(originalFile);
        LOGGER.info("Archived file {} to {}", originalFile, gzippedFile);
    } catch (IOException e) {
        LOGGER.error("Could not archive file {}: " + e.getMessage(), originalFile, e);
    }
    try {
        Files.deleteIfExists(tempFile);
    } catch (IOException e) {
        LOGGER.error("Could not delete temporary file {}: " + e.getMessage(), tempFile, e);
    }
}

问题是如果我们手动解压文件:

gzip -d file_name

生成的解压文件与原始文件不匹配。 文件大小和总行数减少。例如从 33MB 到 32MB 丢失 800K 行。

问题可能与我们正在压缩的文件的编码 (EBCDIC) 有关吗? https://en.wikipedia.org/wiki/EBCDIC

【问题讨论】:

  • @JonSkeet 如果我错了,请纠正我,但似乎 OutputStream 已经是 try-with-resources 语句的一部分。
  • 他完美地关闭了流。您的问题似乎在另一层上。我试过你的代码,它表现完美。卷上有足够的空间吗?
  • @VGR:你说得对。我错过了牙套的位置。 (显然滚动没有帮助。)将删除我的第一条评论以减少混乱。
  • @SirFartALot 创建了一些单元测试,实际上我无法重现所描述的问题。错误发生时我会尝试检查是否存在空间问题。

标签: java gzip gzipoutputstream


【解决方案1】:

经过多次测试,我们无法重现该问题,这一定与压缩过程中没有足够的空间有关。 @SirFartALot 感谢您指出这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多