无法从GZIPInputStream读取小压缩字节数组

huangapple go评论95阅读模式
英文:

Unable to read small compressed byte array from GZIPInputStream

问题

我遇到了一个问题,无法解决或找到一个有用的答案。我有一段压缩字节数组的代码:

try (final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
    final GZIPOutputStream gzip = new GZIPOutputStream(byteArrayOutputStream);
    gzip.write(response.getBytes());
    gzip.close();
    response = byteArrayOutputStream.toByteArray();
} catch (IOException e) {
    log.error("压缩时出现问题", e);
}

它运行得相当顺利,生成的压缩字节数组保存在数据库中。

我有另一段代码,将从数据库中获取压缩的字节数组并进行解压:

try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(compressed)) {
    GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream, BUFFER_SIZE);
    byte[] data = new byte[BUFFER_SIZE];
    int bytesRead;

    while ((bytesRead = gzipInputStream.read(data)) != -1) {
        byteArrayOutputStream.write(data, 0, bytesRead);
    }

    gzipInputStream.close();

    return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
    log.error("解压时出现问题", e);
}

解压对于“大型”字节数组(大于32字节)非常有效,但我有一个只有20字节的压缩数组,无法解压缩它。它永远不会进入while循环,因为read始终返回-1,因此没有读取任何内容。我尝试更改缓冲区大小等等,但什么都不起作用。我做错了什么?

提前感谢!

英文:

I struggle with a problem and I can't solve it or find a useful answer to it.
I have a piece of code that compress an array of bytes :

try (final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
    final GZIPOutputStream gzip = new GZIPOutputStream(byteArrayOutputStream);
    gzip.write(response.getBytes());
    gzip.close();
    response = byteArrayOutputStream.toByteArray();
} catch (IOException e) {
    log.error("Problem in compression", e);
}

It works pretty well, and the resulting compressed array of bytes is saved in a database.

I have another piece of code that will get the compressed array of bytes in the database and that will decompress it :

try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(compressed)) {
    GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream, BUFFER_SIZE);
    byte[] data = new byte[BUFFER_SIZE];
    int bytesRead;

    while ((bytesRead = gzipInputStream.read(data)) != -1) {
        byteArrayOutputStream.write(data, 0, bytesRead);
    }

    gzipInputStream.close();

    return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
    log.error("Problem in decompression", e);
}

The decompression works very well for "big" byte array (bigger than 32 bytes), but I have, for example a compressed array of 20 bytes and I cannot decompress it.
It never enters the while because the read always return -1 and so nothing is read.
I tried to change the buffer size, etc..., but nothing works.
What am I doing wrong ?

Thank you in advance !

答案1

得分: 1

这实际上是您的代码(尽管zip输入流应该在try-with-resources块中),您可以运行:

import java.util.*;
import java.util.zip.*;
import java.io.*;

public class Unzip {
    public static void main(String[] args) throws Exception {
        String s = "H4sIAAAAAAAAA3WRQW+CQBCF7/6K0dOSUNKeiYeaYMNJo+nFxjQrTHQTYMkwhNLG/95lESSIe9jDmzffm51lqkGsasZ3IllvSs5L3jOhTOE0qS4hwwomO4TjwwzM6athNoEbiiPaoCQineaERYGx48Cf5X4cwu2w+/yr8kfayCWmgl1Yfa7Xwe57Hx4Cx7f0xvd1hFiyvJGsMjAeW6PK2JaKHcrYn1mtuqgEQYheN4jReJ65YtHgzYPmS3h5697VpT9s1KtIMdoeF17de+pt5GsbPg6KEl2g+Y22SsglZU8CWPfLbxquEEmOLiDCTfATYc5KZ4DdnIk+e0ikSSy2pE8JpmYXEGP3Vca8cI3dcP4BN4CC8VgCAAA=";
        final int BUFFER_SIZE = 32;
        byte[] compressed = Base64.getDecoder().decode(s);
        try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(compressed)) {
            GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream, BUFFER_SIZE);
            byte[] data = new byte[BUFFER_SIZE];
            int bytesRead;

            while ((bytesRead = gzipInputStream.read(data)) != -1) {
                byteArrayOutputStream.write(data, 0, bytesRead);
            }

            gzipInputStream.close();

            System.out.println(new String(byteArrayOutputStream.toByteArray()));
        } catch (IOException e) {
            e.printStackTrace();    
        }
    }
}
英文:

This is essentially your code (though the zip input stream should be in the try-with-resources block), which you can run:

import java.util.*;
import java.util.zip.*;
import java.io.*;

public class Unzip {
    public static void main(String[] args) throws Exception {
        String s = "H4sIAAAAAAAAA3WRQW+CQBCF7/6K0dOSUNKeiYeaYMNJo+nFxjQrTHQTYMkwhNLG/95lESSIe9jDmzffm51lqkGsasZ3IllvSs5L3jOhTOE0qS4hwwomO4TjwwzM6athNoEbiiPaoCQineaERYGx48Cf5X4cwu2w+/yr8kfayCWmgl1Yfa7Xwe57Hx4Cx7f0xvd1hFiyvJGsMjAeW6PK2JaKHcrYn1mtuqgEQYheN4jReJ65YtHgzYPmS3h5697VpT9s1KtIMdoeF17de+pt5GsbPg6KEl2g+Y22SsglZU8CWPfLbxquEEmOLiDCTfATYc5KZ4DdnIk+e0ikSSy2pE8JpmYXEGP3Vca8cI3dcP4BN4CC8VgCAAA=";
        final int BUFFER_SIZE = 32;
        byte[] compressed = Base64.getDecoder().decode(s);
        try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(compressed)) {
            GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream, BUFFER_SIZE);
            byte[] data = new byte[BUFFER_SIZE];
            int bytesRead;

            while ((bytesRead = gzipInputStream.read(data)) != -1) {
                byteArrayOutputStream.write(data, 0, bytesRead);
            }

            gzipInputStream.close();

            System.out.println(new String(byteArrayOutputStream.toByteArray()));
        } catch (IOException e) {
            e.printStackTrace();    
        }
    }
}

huangapple
  • 本文由 发表于 2023年8月9日 16:53:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866078-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定