使用 GZIPOutputStream 将 InputStream 压缩成 ByteArray
Compress InputStream with GZIPOutputStream into ByteArray
我有一个将 InputStream 作为参数的方法,我正在尝试使用 GZIPOutputStream 压缩输入流,然后 return 压缩字节数组。我的代码如下所示:
public static byte[] compress(final InputStream inputStream) throws IOException {
final int byteArrayOutputStreamSize = Math.max(32, inputStream.available());
try(final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(byteArrayOutputStreamSize);
final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream)) {
IOUtils.copy(inputStream, gzipOutputStream);
return byteArrayOutputStream.toByteArray();
}
}
但不知何故它似乎不起作用。我正在尝试使用以下代码对此执行单元测试:
@Test
public void testCompress() throws Exception {
final String uncompressedBytes = "some text here".getBytes();
final InputStream inputStream = ByteSource.wrap(uncompressedBytes).openStream();
final byte[] compressedBytes = CompressionHelper.compress(inputStream);
Assert.assertNotNull(compressedBytes);
final byte[] decompressedBytes = decompress(compressedBytes);
Assert.assertEquals(uncompressedBytes, decompressedBytes);
}
public static byte[] decompress(byte[] contentBytes) throws Exception {
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(contentBytes);
InputStream inputStream = new GZIPInputStream(byteArrayInputStream)) {
IOUtils.copy(inputStream, out);
return out.toByteArray();
}
}
但是我遇到了这个异常
java.io.EOFException: Unexpected end of ZLIB input stream
我做错了什么?
在将输入流复制到输出流后刷新并关闭输出流修复了问题:
IOUtils.copy(inputStream, gzipOutputStream);
gzipOutputStream.flush();
gzipOutputStream.close();
我有一个将 InputStream 作为参数的方法,我正在尝试使用 GZIPOutputStream 压缩输入流,然后 return 压缩字节数组。我的代码如下所示:
public static byte[] compress(final InputStream inputStream) throws IOException {
final int byteArrayOutputStreamSize = Math.max(32, inputStream.available());
try(final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(byteArrayOutputStreamSize);
final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream)) {
IOUtils.copy(inputStream, gzipOutputStream);
return byteArrayOutputStream.toByteArray();
}
}
但不知何故它似乎不起作用。我正在尝试使用以下代码对此执行单元测试:
@Test
public void testCompress() throws Exception {
final String uncompressedBytes = "some text here".getBytes();
final InputStream inputStream = ByteSource.wrap(uncompressedBytes).openStream();
final byte[] compressedBytes = CompressionHelper.compress(inputStream);
Assert.assertNotNull(compressedBytes);
final byte[] decompressedBytes = decompress(compressedBytes);
Assert.assertEquals(uncompressedBytes, decompressedBytes);
}
public static byte[] decompress(byte[] contentBytes) throws Exception {
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(contentBytes);
InputStream inputStream = new GZIPInputStream(byteArrayInputStream)) {
IOUtils.copy(inputStream, out);
return out.toByteArray();
}
}
但是我遇到了这个异常
java.io.EOFException: Unexpected end of ZLIB input stream
我做错了什么?
在将输入流复制到输出流后刷新并关闭输出流修复了问题:
IOUtils.copy(inputStream, gzipOutputStream);
gzipOutputStream.flush();
gzipOutputStream.close();