如何gzip一个InputStream并返回一个InputStream?

时间:2016-08-12 18:45:47

标签: java inputstream outputstream gzipinputstream gzipoutputstream

我从HTTP请求的响应开始:

InputStream responseInputStream = response.getEntityInputStream()

我需要gzip这个响应,所以我可以将它上传到s3并保存压缩:

this.s3.putObject(new PutObjectRequest(bucketName, key, gzippedResponseInputStream, meta));

我知道我可以从byte[]中获取responseInputStream数组,然后将它们gzip为新的InputStream。但是,对于大量数据而言,这可能效率很低。

我知道在SO上提出了类似的问题,但我没有找到任何似乎可以解决从InputStream开始并使用gzip压缩InputStream完成的特定需求。

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

public final class Example {
    public static void main(String[] args) throws IOException, InterruptedException {
        final PipedInputStream inputStream = new PipedInputStream();
        final PipedOutputStream outputStream = new PipedOutputStream(inputStream);

        Thread compressorThread = new Thread() {
            @Override
            public void run() {
                try (FileInputStream dataSource = new FileInputStream(args[0])) {
                    try (GZIPOutputStream sink = new GZIPOutputStream(outputStream)) {
                        final byte[] buffer = new byte[8 * 1024];
                        for (int bytesRead = dataSource.read(buffer); bytesRead >= 0; bytesRead = dataSource.read(buffer)) {
                            sink.write(buffer, 0, bytesRead);
                        }
                    }
                } catch (IOException ex) {
                    //TODO handle exception -> maybe use callable + executor
                }
            }
        };
        compressorThread.start();

        try (FileOutputStream destination = new FileOutputStream(args[1])) {
            final byte[] buffer = new byte[8 * 1024];
            for (int bytesRead = inputStream.read(buffer); bytesRead >= 0; bytesRead = inputStream.read(buffer)) {
                destination.write(buffer, 0, bytesRead);
            }
        }

        compressorThread.join();
    }

}

你是对的,我之前的例子是错的。您可以使用管道流。这里的问题是你不能使用来自同一线程的输入和输出流。也不要忘记写作帖子上的join()。您可以通过提供两个参数来测试我的示例:

  • args [0] - >源文件
  • args [1] - >写入压缩内容的目的地
PS:@ 11thdimension使用他的管道流解决方案快了几分钟,所以如果你觉得这很有帮助请接受他的回答

答案 1 :(得分:3)

我认为您正在寻找PipedInputStream

以下是如何做到的。

public InputStrema getGZipStream() {
    final PipedOutputStream pos = new PipedOutputStream();
    PipedInputStream pis = new PipedInputStream();

    try (final InputStream responseInputStream = response.getEntityInputStream();
    ){
        pis.connect(pos);

        Thread thread = new Thread() {
            public void run () {
                startWriting(pos, responseInputStream);
            }
        };
        thread.start();
    } catch(Exception e) {
        e.printStackTrace();
    }

    return pis;
}

public void startWriting(OutputStream out, InputStream in) {
    try (GZIPOutputStream gOut = GZIPOutputStream(out);) {
        byte[] buffer = new byte[10240];
        int len = -1;
        while ((len = in.read(buffer)) != -1) {
            gOut.write(buffer, 0, len);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            out.close();
        } catch( Exception e) {
            e.printStackTrace();
        }
    }
}

我还没有测试过此代码,如果有效,请告诉我。