[英]LZ4 Compression (C++) and Decompression (Java)
我的目標是在 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);
}
任何幫助都會很有用。 提前致謝。
有一個創建流的工廠應該負責檢查:
CompressorInputStream zIn =
new CompressorStreamFactory()
.createCompressorInputStream(CompressorStreamFactory.LZ4_BLOCK, in);
或LZ4_FRAMED
取決於 C++ 庫生成的內容。
感謝大家的幫助。 我使用此代碼修復了它。
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.