简体   繁体   English

Java GzipInputStream转换为DataInputStream

[英]Java GzipInputStream into DataInputStream

I have a problem with GZip in Java. 我在Java中遇到GZip问题。 Currently i work with files that are gzipped. 目前,我正在处理压缩文件。 One file in one gzip archive. 一个gzip存档中的一个文件。 And if i decompress them manually and then parse them everything works. 如果我手动解压缩它们,然后解析它们,则一切正常。 But i want to automate this with Java and GZipInputStream but it doesn't work. 但是我想用Java和GZipInputStream自动化它,但是不起作用。 I need to have DataInputStream at the end. 我需要在末尾有DataInputStream。 My code is: 我的代码是:

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

I also tried new GZIPInputStream(new FileInputStream(baseFile)); 我还尝试了new GZIPInputStream(new FileInputStream(baseFile)); The result is the same. 结果是一样的。 Due to output i see that Gzip stream creates without exception but later i get invalid data from DataInputStream. 由于输出,我看到Gzip流毫无例外地创建,但是后来我从DataInputStream获取了无效数据。 Please help :) 请帮忙 :)

I ran the following code without problems 我运行以下代码没有问题

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

It reads the gzip file and the uncompressed file and compares the content. 它读取gzip文件和未压缩的文件并比较内容。 It prints Files differ: false. 它打印文件不同​​:false。 If it doesn't for your files than the files are not the same. 如果不是您的文件,则文件不相同。

My final solution: 我的最终解决方案:

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

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

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