【问题标题】:Java GzipInputStream into DataInputStreamJava GzipInputStream 转换为 DataInputStream
【发布时间】: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


【解决方案1】:

我运行以下代码没有问题

public static void main(String[] args) throws IOException {
    byte[] originalBytesArray = Files.readAllBytes(new File("OrdLog.BR-1.17.2016-09-12.bin").toPath());
    byte[] bytesArray = Files.readAllBytes(new File("OrdLog.BR-1.17.2016-09-12.bin.gz").toPath());
    DataInputStream reader = null;
    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");
    }
    byte[] uncompressedBytesArray = new byte[originalBytesArray.length];
    reader.readFully(uncompressedBytesArray);
    reader.close();
    boolean filesDiffer = false;
    for (int i = 0; i < uncompressedBytesArray.length; i++) {
        if (originalBytesArray[i] != uncompressedBytesArray[i]) {
            filesDiffer = true;
        }
    }
    System.out.println("Files differ: " + filesDiffer);
}

它读取 gzip 文件和未压缩文件并比较内容。它打印文件不同​​:假。如果它不适合您的文件,那么文件就不一样了。

【讨论】:

  • 我的问题是我使用 .readByte() 方法,如果我使用未压缩的源,它似乎会读取不同的数据。你能测试一下这个方法并与原始文件进行比较吗?
  • 我运行了你的测试:gzip 文件不同:true。 7zip 解压缩文件没有问题,并说它是一个 gzip 存档。而且我没有例外。
  • 我本来打算要文件的 :-) 感谢您提供它。读取压缩文件时出错。我将其更改为使用 readFully 以使代码更容易。没啥区别
  • 你能具体测试一下 .readByte() 吗?当我尝试你的方法时)
  • 我最好把它当作流来使用,而不是作为以后解析的数组。
【解决方案2】:

我的最终解决方案:

    try {
        byte[] gzipBytes = new byte[getUncompressedFileSize()];
        new DataInputStream(new GZIPInputStream(new FileInputStream(baseFile))).readFully(gzipBytes);
        reader = new DataInputStream(new ByteArrayInputStream(gzipBytes));
    } catch (ZipException notZip) {
        byte[] bytesArray = Files.readAllBytes(baseFile.toPath());
        reader = new DataInputStream(new ByteArrayInputStream(bytesArray));
    }

private int getUncompressedFileSize() throws IOException {
    //last 4 bytes of file is size of original file if it is less than 2GB
    RandomAccessFile raf = new RandomAccessFile(baseFile, "r");
    raf.seek(raf.length() - 4);
    int b4 = raf.read();
    int b3 = raf.read();
    int b2 = raf.read();
    int b1 = raf.read();
    int val = (b1 << 24) | (b2 << 16) + (b3 << 8) + b4;
    raf.close();
    return val;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-23
    • 2018-03-07
    • 2011-04-07
    • 2012-06-18
    • 1970-01-01
    相关资源
    最近更新 更多