当前位置: 首页 > 知识库问答 >
问题:

Java GzipInputStream转换为DataInputStream

公良征
2023-03-14

我在JavaGZip有一个问题。目前我处理的文件是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");
    }

我还尝试了新的GZIPInputStream(新的FileInputStream(baseFile));结果是一样的。由于输出,我看到Gzip流无一例外地创建,但后来我从DataInputStream获得了无效数据。请帮助:)

共有2个答案

黎玺
2023-03-14

我的最终解决方案:

    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;
}
皇甫展
2023-03-14

我毫无问题地运行了以下代码

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。如果它不适用于您的文件,则文件不相同。

 类似资料:
  • 问题内容: 我怎样才能投来? 问题答案: 您必须创建一个新的。

  • 问题内容: 我仍然是Java的初学者,所以我对线程和并发的了解还很少。但是,由于java.util.Timer和TimerTask存在问题,我希望能够将ScheduledThreadPoolExecutor用作计时器。我对线程的创建非常感兴趣,并且知道我将在几周内学习它们。但是,如果可以的话,有人可以给我一个基本的示例,说明如何使用util.timer将当前的迷你测试程序转换为ScheduledT

  • 更新:我已经尝试将getCalcMean()和calcMean;我理解这个错误的含义,但我不知道如何修复它,因为最终结果需要是double,我认为java可以计算double和int并得到double。我做错了什么? 我正在计算键入999后的总数平均值,但它一直显示为0,我不知道为什么。 有人能告诉我如何让我的getCalcMean()方法将平均值显示为numTotal/count吗? ---班级

  • 我想把下面的代码转换成Java8s。

  • 最近,我浏览了一些网站,将中缀转换成前缀符号,最后我被卷了起来。 我已经给出了我所做的步骤。。 例:-(1(2*3))(5*6)(7/8) 方法1:-(无需任何算法的手动转换):- 方法2:- 根据现场情况http://scanftree.com/Data_Structure/infix-to-prefix 所以,在这里我完全被绞死了。 请任何人提供以下方面的信息:- 关于我在以上2种方法中哪里出

  • 问题内容: 如何将String 列表转换为数组?以下代码返回错误。 错误: 问题答案: 你要 有关文档,请参见此处,请注意,您也可以以填充已传递数组的方式调用此方法,而不仅仅是使用它来计算要返回的类型。另请注意,也许当您打印阵列时,您更喜欢 因为那将打印实际的元素。