Java GzipInputStream 到 DataInputStream

Java GzipInputStream into DataInputStream

我在 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 获得无效数据。 请帮助:)

我运行下面的代码没有问题

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 文件和未压缩的文件并比较内容。它打印文件不同​​:false。如果它不适合您的文件,那么文件就不一样了。

我的最终解决方案:

    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;
}