简体   繁体   English

用Java解压缩ubuntu lz4文件

[英]Decompress compressed ubuntu lz4 file in Java

I Build Image from Dockerfile: 我从Dockerfile构建映像:

FROM ubuntu
RUN apt-get update

Then save the image to my local computer and I got this file for example: 然后将图像保存到本地计算机,例如,我得到了这个文件:

archive.ubuntu.com_ubuntu_dists_bionic_restricted_binary-amd64_Packages.lz4

I'm trying to decompress ubuntu bionic lz4 file in Java lz4-java: 我正在尝试在Java lz4-java中解压缩ubuntu仿生lz4文件:

 LZ4Factory factory = LZ4Factory.fastestInstance();
 byte[] encoded = Files.readAllBytes(Paths.get("<Path to file>"));
 final int compressedLength = data.length;
            LZ4Compressor compressor = factory.fastCompressor();
            byte[] restored = new byte[compressedLength];
            LZ4SafeDecompressor decompressor2 = factory.safeDecompressor();
            decompressor2.decompress(data, 0, compressedLength, restored, 0);

I use this dependency: 我使用这种依赖关系:

    <dependency>
        <groupId>org.lz4</groupId>
        <artifactId>lz4-java</artifactId>
        <version>1.5.1</version>
    </dependency>

But I still get this exception: 但是我仍然得到这个异常:

Exception in thread "main" net.jpountz.lz4.LZ4Exception: Error decoding offset 4 of input buffer
    at net.jpountz.lz4.LZ4JNISafeDecompressor.decompress(LZ4JNISafeDecompressor.java:38)
    at net.jpountz.lz4.LZ4SafeDecompressor.decompress(LZ4SafeDecompressor.java:74)
    at org.whitesource.fs.Main.main(Main.java:89)

假设您的档案使用正式的LZ4帧格式,则您可能更喜欢支持LZ4帧格式的LZ4 Java版本

I wrote this method to decompress/uncompress lz4 file and write the string to a new output file 我编写了此方法来解压缩/解压缩lz4文件并将字符串写入新的输出文件

import org.apache.commons.compress.compressors.lz4.FramedLZ4CompressorInputStream;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;

.... ....

public File uncompressLz4File(File lz4File) {
            File lz4output = new File(lz4File.getParent() + Constants.SYS_FILE_SEPARATOR + "lz4Output.txt");
            try (InputStream fin = Files.newInputStream(lz4File.toPath());
                 BufferedInputStream in = new BufferedInputStream(fin);
                 OutputStream out = Files.newOutputStream(Paths.get(lz4output.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, 0, n);
                }
                return lz4output;
            } catch (Exception e) {
                logger.warn("Failed to uncompress lz4 packages file '{}'. Exception message: '{}'", lz4File.getPath(), e.getMessage());
                logger.debug("Exception: '{}'", e);
                return null;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM