[英]LZ4 Compression (C++) and Decompression (Java)
My objective is to compress a file using LZ4 in C++ and to decompress it in Java.我的目标是在 C++ 中使用 LZ4 压缩文件并在 Java 中解压缩它。
My text file (A.txt):我的文本文件(A.txt):
Hi, Hello everyone.
Thanks.
The file after c++ compression (A.txt.lz4): c++压缩后的文件(A.txt.lz4):
"M@Pw €Hi, Hello everyone.
Thanks.
Then I decompressed it in Java (B.txt):然后我在Java(B.txt)中解压:
i, Hello everyone.
Thanks.
The problem is I'm not getting the first character of every file.问题是我没有得到每个文件的第一个字符。 I can't figure out where I'm going wrong.
我不知道我哪里错了。
My java code:我的 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);
}
Any help would be useful.任何帮助都会很有用。 Thanks in advance.
提前致谢。
There is a factory for creating streams that should take care of the check:有一个创建流的工厂应该负责检查:
CompressorInputStream zIn =
new CompressorStreamFactory()
.createCompressorInputStream(CompressorStreamFactory.LZ4_BLOCK, in);
or LZ4_FRAMED
depending on what the C++ library generates.或
LZ4_FRAMED
取决于 C++ 库生成的内容。
Thanks everyone for the help.感谢大家的帮助。 I fixed it using this code.
我使用此代码修复了它。
public static void uncompressLz4File(String str1, String str2) {
File f1 = new File(str1);
File f2 = new File(str2);
try (FileInputStream fin = new FileInputStream(f1);
BufferedInputStream in = new BufferedInputStream(fin);
OutputStream out = Files.newOutputStream(Paths.get(f2.getAbsolutePath()));
FramedLZ4CompressorInputStream zIn = new FramedLZ4CompressorInputStream(in)) {
int n;
byte[] b = new byte[1024];
while ((n = zIn.read(b)) > 0) {
out.write(b, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.