如何使用LZMA SDK在Java中压缩/解压缩

http://www.7-zip.org/sdk.html 这个站点提供了一个用于压缩/解压缩文件的LZMA SDK,我想试一试,但我迷路了。 有人有这方面的经验吗?还是教程?谢谢。     
已邀请:
简答:不要 7zip sdk是旧的,没有维护,它只是围绕C ++库的JNI包装器。现代JVM(1.7 +)上的纯Java实现与C ++一样快,并且具有较少的依赖性和可移植性问题。 看看http://tukaani.org/xz/java.html XZ是基于LZMA2(LZMA的改进版本)的文件格式 发明XZ格式的人构建了XZ归档压缩/提取算法的纯java实现 XZ文件格式仅用于存储1个文件。因此,您需要先将源文件夹压缩/ tar到一个未压缩的文件中。 使用java库就像这样简单:
FileInputStream inFile = new FileInputStream("src.tar");
FileOutputStream outfile = new FileOutputStream("src.tar.xz");

LZMA2Options options = new LZMA2Options();

options.setPreset(7); // play with this number: 6 is default but 7 works better for mid sized archives ( > 8mb)

XZOutputStream out = new XZOutputStream(outfile, options);

byte[] buf = new byte[8192];
int size;
while ((size = inFile.read(buf)) != -1)
   out.write(buf, 0, size);

out.finish();
    
查看您发布的该链接的zip文件的Java / SevenZip文件夹中的LzmaAlone.java和LzmaBench.java文件。     
使用J7Zip。它是LZMA SDK的java端口。你在这里找到它: http://sourceforge.net/projects/p7zip/files/J7Zip/ 替代 将lzmajio.jar与LzmaInputStream和LzmaOutputStream类一起使用 你在github上找到它: http://github.com/league/lzmajio/downloads     
您可以使用此库。它“过时”但仍然可以正常工作。 Maven依赖
<dependency>
    <groupId>com.github.jponge</groupId>
    <artifactId>lzma-java</artifactId>
    <version>1.2</version>
</dependency>
实用类
import lzma.sdk.lzma.Decoder;
import lzma.streams.LzmaInputStream;
import lzma.streams.LzmaOutputStream;
import org.apache.commons.compress.utils.IOUtils;

import java.io.*;
import java.nio.file.Path;

public class LzmaCompressor
{
    private Path rawFilePath;
    private Path compressedFilePath;

    public LzmaCompressor(Path rawFilePath, Path compressedFilePath)
    {
        this.rawFilePath = rawFilePath;
        this.compressedFilePath = compressedFilePath;
    }

    public void compress() throws IOException
    {
        try (LzmaOutputStream outputStream = new LzmaOutputStream.Builder(
                new BufferedOutputStream(new FileOutputStream(compressedFilePath.toFile())))
                .useMaximalDictionarySize()
                .useMaximalFastBytes()
                .build();
             InputStream inputStream = new BufferedInputStream(new FileInputStream(rawFilePath.toFile())))
        {
            IOUtils.copy(inputStream, outputStream);
        }
    }

    public void decompress() throws IOException
    {
        try (LzmaInputStream inputStream = new LzmaInputStream(
                new BufferedInputStream(new FileInputStream(compressedFilePath.toFile())),
                new Decoder());
             OutputStream outputStream = new BufferedOutputStream(
                     new FileOutputStream(rawFilePath.toFile())))
        {
            IOUtils.copy(inputStream, outputStream);
        }
    }
}
首先,您必须创建一个包含内容的文件以开始压缩。您可以使用此网站生成随机文本。 示例压缩和解压缩
Path rawFile = Paths.get("raw.txt");
Path compressedFile = Paths.get("compressed.lzma");

LzmaCompressor lzmaCompressor = new LzmaCompressor(rawFile, compressedFile);
lzmaCompressor.compress();
lzmaCompressor.decompress();
    
以下是使用XZ Utils纯java库以LZMA2压缩算法打包和解压缩XZ压缩文件的测试示例。
import org.tukaani.xz.*;

// CompressXz
public static void main(String[] args) throws Exception {
    String from = args[0];
    String to = args[1];
    try (FileOutputStream fileStream = new FileOutputStream(to);
         XZOutputStream xzStream = new XZOutputStream(
                 fileStream, new LZMA2Options(LZMA2Options.PRESET_MAX), BasicArrayCache.getInstance())) {

        Files.copy(Paths.get(from), xzStream);
    }
}

// DecompressXz
public static void main(String[] args) throws Exception {
    String from = args[0];
    String to = args[1];
    try (FileInputStream fileStream = new FileInputStream(from);
         XZInputStream xzStream = new XZInputStream(fileStream, BasicArrayCache.getInstance())) {

        Files.copy(xzStream, Paths.get(to), StandardCopyOption.REPLACE_EXISTING);
    }
}

// DecompressXzSeekable (partial)
public static void main(String[] args) throws Exception {
    String from = args[0];
    String to = args[1];
    int offset = Integer.parseInt(args[2]);
    int size = Integer.parseInt(args[3]);
    try (SeekableInputStream fileStream = new SeekableFileInputStream(from);
         SeekableXZInputStream xzStream = new SeekableXZInputStream(fileStream, BasicArrayCache.getInstance())) {

        xzStream.seek(offset);
        byte[] buf = new byte[size];
        if (size != xzStream.read(buf)) {
            xzStream.available(); // let it throw the last exception, if any
            throw new IOException("Truncated stream");
        }
        Files.write(Paths.get(to), buf);
    }
}
    

要回复问题请先登录注册