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

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

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.html
匿名

发表评论

匿名网友

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

确定