我正在尝试使用commons-compress
实现LZMA压缩和解压缩。以下是我正在使用的依赖项。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.20</version>
</dependency>
以下是压缩代码:
private byte[] compress(byte[] content) throws IOException {
LZMA2Options options = new LZMA2Options();
options.setPreset(LZMA2Options.PRESET_MAX);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(content.length);
LZMAOutputStream outputStream = new LZMAOutputStream(byteArrayOutputStream, options, -1L);
outputStream.write(content);
outputStream.finish();
outputStream.close();
return byteArrayOutputStream.toByteArray();
}
以下是减压代码:
public static byte[] decompressLzmaStream(byte[] compressedBytes, int size) throws IOException {
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(compressedBytes)) {
return decompressLzmaStream(byteArrayInputStream, size);
}
}
public static byte[] decompressLzmaStream(InputStream inputStream, int size) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(size);
LZMACompressorInputStream lzmaInputStream = new LZMACompressorInputStream(inputStream);
int length;
while (-1 != (length = lzmaInputStream.read(buffer))) {
byteArrayOutputStream.write(buffer, 0, length);
}
lzmaInputStream.close();
byteArrayOutputStream.flush();
return byteArrayOutputStream.toByteArray();
}
但是当我尝试打开解压缩的文件时,它显示为malformed file
,有人可以建议我到底在做什么错吗?