简体   繁体   English

Java:如何将InputStream转换为GZIPInputStream?

[英]Java: How do I convert InputStream to GZIPInputStream?

I have a method like 我有一个方法

      public void put(@Nonnull final InputStream inputStream, @Nonnull final String uniqueId) throws PersistenceException {
        // a.) create gzip of inputStream
        final GZIPInputStream zipInputStream;
        try {
            zipInputStream = new GZIPInputStream(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
            throw new PersistenceException("Persistence Service could not received input stream to persist for " + uniqueId);
        }

I wan to convert the inputStream into zipInputStream , what is the way to do that? 我想将inputStream转换为zipInputStream ,这样做的方法是什么?

  • The above method is incorrect and throws Exception as "Not a Zip Format" 上面的方法不正确,并抛出异常为“不是Zip格式”

converting Java Streams to me are really confusing and I do not make them right 将Java Streams转换为我真的很混乱,我没有让它们正确

The GZIPInputStream is to be used to decompress an incoming InputStream . GZIPInputStream用于解压缩传入的InputStream To compress an incoming InputStream using GZIP, you basically need to write it to a GZIPOutputStream . 要使用GZIP压缩传入的InputStream ,您基本上需要将其写入GZIPOutputStream

You can get a new InputStream out of it if you use ByteArrayOutputStream to write gzipped content to a byte[] and ByteArrayInputStream to turn a byte[] into an InputStream . 你可以得到一个新InputStream出来的,如果你使用ByteArrayOutputStream写gzip压缩内容到byte[]ByteArrayInputStream把一个byte[]InputStream

So, basically: 所以,基本上:

public void put(@Nonnull final InputStream inputStream, @Nonnull final String uniqueId) throws PersistenceException {
    final InputStream zipInputStream;
    try {
        ByteArrayOutputStream bytesOutput = new ByteArrayOutputStream();
        GZIPOutputStream gzipOutput = new GZIPOutputStream(bytesOutput);

        try {
            byte[] buffer = new byte[10240];
            for (int length = 0; (length = inputStream.read(buffer)) != -1;) {
                gzipOutput.write(buffer, 0, length);
            }
        } finally {
            try { inputStream.close(); } catch (IOException ignore) {}
            try { gzipOutput.close(); } catch (IOException ignore) {}
        }

        zipInputStream = new ByteArrayInputStream(bytesOutput.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
        throw new PersistenceException("Persistence Service could not received input stream to persist for " + uniqueId);
    }

    // ...

You can if necessary replace the ByteArrayOutputStream / ByteArrayInputStream by a FileOuputStream / FileInputStream on a temporary file as created by File#createTempFile() , especially if those streams can contain large data which might overflow machine's available memory when used concurrently. 如果需要,可以通过FileOuputStream File#createTempFile()创建的临时文件上的FileOuputStream / FileInputStream替换ByteArrayOutputStream / ByteArrayInputStream ,特别是如果这些流可以包含大数据,这些数据可能会在并发使用时溢出计算机的可用内存。

GZIPInputStream is for reading gzip-encoding content. GZIPInputStream用于读取 gzip编码内容。

If your goal is to take a regular input stream and compress it in the GZIP format, then you need to write those bytes to a GZIPOutputStream . 如果你的目标是采用常规输入流并以GZIP格式压缩它,那么你需要将这些字节写入GZIPOutputStream

See also this answer to a related question . 另见相关问题的答案

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

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