【问题标题】:LZ4 Compression (C++) and Decompression (Java)LZ4 压缩 (C++) 和解压缩 (Java)
【发布时间】: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


    【解决方案1】:

    有一个factory 用于创建应该负责检查的流:

    CompressorInputStream zIn =
        new CompressorStreamFactory()
        .createCompressorInputStream(CompressorStreamFactory.LZ4_BLOCK, in);
    

    LZ4_FRAMED,具体取决于 C++ 库生成的内容。

    【讨论】:

      【解决方案2】:

      感谢大家的帮助。我使用此代码修复了它。

      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();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-05-01
        • 1970-01-01
        • 2016-03-31
        • 1970-01-01
        • 1970-01-01
        • 2018-05-16
        • 2021-02-09
        • 1970-01-01
        相关资源
        最近更新 更多