Gets the uncompressed size of this GZIPInputStream?

后端 未结 8 598
余生分开走
余生分开走 2021-01-04 13:51

I have a GZIPInputStream that I constructed from another ByteArrayInputStream. I want to know the original (uncompressed) length for the gzip data.

相关标签:
8条回答
  • 2021-01-04 14:49

    Get the FileChannel from the underlying FileInputStream instead. It tells you both file size and current position of the compressed file. Example:

    @Override
    public void produce(final DataConsumer consumer, final boolean skipData) throws IOException {
        try (FileInputStream fis = new FileInputStream(tarFile)) {
            FileChannel channel = fis.getChannel();
            final Eta<Long> eta = new Eta<>(channel.size());
            try (InputStream is = tarFile.getName().toLowerCase().endsWith("gz")
                ? new GZIPInputStream(fis) : fis) {
                try (TarArchiveInputStream tais = (TarArchiveInputStream) new ArchiveStreamFactory()
                    .createArchiveInputStream("tar", new BufferedInputStream(is))) {
    
                    TarArchiveEntry tae;
                    boolean done = false;
                    while (!done && (tae = tais.getNextTarEntry()) != null) {
                        if (tae.getName().startsWith("docs/") && tae.getName().endsWith(".html")) {
                            String data = null;
                            if (!skipData) {
                                data = new String(tais.readNBytes((int) tae.getSize()), StandardCharsets.UTF_8);
                            }
                            done = !consumer.consume(data);
                        }
    
                        String progress = eta.toStringPeriodical(channel.position());
                        if (progress != null) {
                            System.out.println(progress);
                        }
                    }
                    System.out.println("tar bytes read: " + tais.getBytesRead());
                } catch (ArchiveException ex) {
                    throw new IOException(ex);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-04 14:51

    There is no reliable way to get the length other than decompressing the whole thing. See Uncompressed file size using zlib's gzip file access function .

    0 讨论(0)
提交回复
热议问题