【发布时间】:2020-08-07 13:51:44
【问题描述】:
我的目标是在 C++ 中使用 LZ4 压缩文件并在 Java 中解压缩。
我的文本文件(A.txt):
Hi, Hello everyone.
Thanks.
c++压缩后的文件(A.txt.lz4):
"M@Pw €Hi, Hello everyone.
Thanks.
然后我用Java解压(B.txt):
i, Hello everyone.
Thanks.
问题是我没有得到每个文件的第一个字符。我不知道哪里出错了。
我的java代码:
public static void uncompressLz4File(String str1, String str2) {
File f1 = new File(str1);
File f2 = new File(str2);
try (InputStream fin = Files.newInputStream(f1.toPath());
BufferedInputStream in = new BufferedInputStream(fin);
OutputStream out = Files.newOutputStream(Paths.get(f2.getAbsolutePath()));
FramedLZ4CompressorInputStream zIn = new FramedLZ4CompressorInputStream(in))
{
int n;
zIn.getCompressedCount();
byte[] b = new byte[1];
int uncompressedLength = zIn.read(b, 0, 1) == -1 ? -1 : b[0] & 255;
b[0] = (byte) uncompressedLength;
final byte[] buffer = new byte[uncompressedLength];
while (-1 != (n = zIn.read(buffer)))
{
out.write(buffer);
}
}
catch (Exception e)
{
}
}
public static void main(String args[]) throws IOException
{
String str1 = "C:\\Users\\aravinth\\Desktop\\A.txt.lz4";
String str2 = "C:\\Users\\aravinth\\Desktop\\B.txt";
uncompressLz4File(str1, str2);
}
任何帮助都会很有用。提前致谢。
【问题讨论】:
标签: java compression lz4