简体   繁体   English

意外的Java GZIPOutputStream结果

[英]Unexpected Java GZIPOutputStream results

We have the following Java method to compress files using GZIPOutputStream 我们有以下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);
    }
}

The problem is that if we manually decompress back the file: 问题是,如果我们手动解压缩该文件:

gzip -d file_name

The resulting decompressed file does not match the original file. 生成的解压缩文件与原始文件不匹配。 The file size and the total number of lines are decreased. 文件大小和总行数减少。 For example from 33MB to 32MB with a loss of 800K lines. 例如,从33MB到32MB,损失80万行。

Could the issue be related with the encoding ( EBCDIC ) of the files we are compressing? 问题可能与我们正在压缩的文件的编码( EBCDIC )有关吗? https://en.wikipedia.org/wiki/EBCDIC https://en.wikipedia.org/wiki/EBCDIC

After several Tests we have not been able to reproduce the issue, it must have been related with not having enough space on the volume during the compression. 经过几次测试后,我们无法重现该问题,它一定与压缩期间卷上没有足够的空间有关。 @SirFartALot thanks for pointing that out. @SirFartALot感谢您指出这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM