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

17

http://www.7-zip.org/sdk.html

这个网站提供了一个LZMA SDK,用于压缩/解压文件。我想尝试一下,但是我现在不知道该怎么做。

有没有人有相关经验或者教程可以分享?谢谢。


你可能对这个感兴趣,但我没有相关经验:http://sevenzipjbind.sourceforge.net/first_steps.html - MeBigFatGuy
6个回答

38

简短回答:不用。

7zip SDK已经过时且没有维护,它只是C++库的JNI包装器。一个现代JVM(1.7+)上的纯Java实现可以和C++版本一样快,而且具有更少的依赖和可移植性问题。

看一下http://tukaani.org/xz/java.html

XZ是基于LZMA2(LZMA的改进版本)的文件格式。

发明XZ格式的人们构建了纯Java实现的XZ归档压缩/提取算法。

XZ文件格式设计用于仅存储1个文件。因此,您需要将源文件夹压缩成单个未压缩文件的zip / 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();

你能解释一下你的简短回答吗? - Anubian Noob
4
这是一份比较旧的回答,我建议的库可能已经被更新或者有更好的选择。关键在于7zip SDK已经过时而且没有维护了,它只是C++库的JNI包装器。在现代JVM(1.7+)上使用纯Java实现和C++一样快,并且具有更少的依赖性和可移植性问题。仅仅是我的个人看法。 - Stefano Fratini
4
在Sun/Oracle JVM上,你说得没错,它的速度和C++一样快。但遗憾的是,在Android上,这种操作仍然比C++慢5到10倍(即使在带有ART的Android 6上也是如此)。这真是让人沮丧。 - Quark
我在我的回答中提供了此库的更多示例:https://dev59.com/UG035IYBdhLWcg3wVuh1#49454898 - Vadzim

4
请查看您发布的链接中Java/SevenZip文件夹中的LzmaAlone.java和LzmaBench.java文件。

我不明白,难道他们不应该为此编写一个Javadoc文档吗? - lamwaiman1988
2
是的,他们应该制作一些文档(或Javadoc),但有时您必须查看示例代码,这就是我指导您的地方。 - Eve Freeman

3

2
你可以使用这个库。虽然它已经有些过时,但是仍然可以正常使用。 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();

0

这里是使用XZ Utils纯Java库进行XZ归档的测试示例,采用LZMA2压缩算法,具有出色的压缩比。

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

0

https://mvnrepository.com/artifact/org.tukaani/xz/1.8

Android 的 Kotlin 代码:

 fun initDatabase() {
    var gisFile = this.getDatabasePath("china_gis.db");
    if (!gisFile.exists()) {
        if(!gisFile.parentFile.exists()) gisFile.parentFile.mkdirs();
        var inStream = assets.open("china_gis_no_poly.db.xz")
        inStream.use { input ->
            val buf = ByteArray(1024)
            XZInputStream(input).use { input ->
                FileOutputStream(gisFile,true).use { output ->
                    var size: Int
                    while (true) {
                        size = input.read(buf);
                        if (size != -1) {
                            output.write(buf, 0, size)
                        } else {
                            break;
                        }
                    }
                    output.flush()
                }

            }
        }
    }
}

Java 代码:

 byte[] buf = new byte[8192];
    String name =  "C:\\Users\\temp22\\Downloads\\2017-007-13\\china_gis_no_poly.db.xz";
    try {

      InputStream input = new FileInputStream(name);
                FileOutputStream output=  new FileOutputStream(name+".db");
                try {
                    // Since XZInputStream does some buffering internally
                    // anyway, BufferedInputStream doesn't seem to be
                    // needed here to improve performance.
                    // in = new BufferedInputStream(in);
                    input = new XZInputStream(input);

                    int size;
                    while ((size = input.read(buf)) != -1)
                        output.write(buf, 0, size);
                    output.flush();

                } finally {
                    // Close FileInputStream (directly or indirectly
                    // via XZInputStream, it doesn't matter).
                    input.close();
                    output.close();
                }

    } catch (FileNotFoundException e) {
        System.err.println("XZDecDemo: Cannot open " + name + ": "
                           + e.getMessage());
        System.exit(1);

    } catch (EOFException e) {
        System.err.println("XZDecDemo: Unexpected end of input on "
                           + name);
        System.exit(1);

    } catch (IOException e) {
        System.err.println("XZDecDemo: Error decompressing from "
                           + name + ": " + e.getMessage());
        System.exit(1);
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接