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

发布于 2024-10-27 20:06:25 字数 169 浏览 6 评论 0原文

http://www.7-zip.org/sdk.html 这个网站提供了一个用于压缩/解压缩文件的 LZMA SDK,我想尝试一下,但我迷失了。

有人有这方面的经验吗?或者教程?谢谢。

http://www.7-zip.org/sdk.html
This site provide a LZMA SDK for compress/decompress files, I would like to give it a shot but I am lost.

Anyone got experience on this? Or a tutorial? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

请叫√我孤独 2024-11-03 20:06:25

简短的回答:不要

7zip sdk 很旧且无人维护,它只是 C++ 库的 JNI 包装器。现代 JVM (1.7+) 上的纯 Java 实现与 C++ 实现一样快,并且依赖性和可移植性问题更少。

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

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

发明 XZ 格式的人构建了 XZ 存档压缩/提取算法的纯 java 实现

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

Short answer: don't

The 7zip sdk is old and unmaintained and it's just a JNI wrapper around the C++ library. A pure Java implementation on a modern JVM (1.7+) is as fast as a C++ one and has less dependecies and portability issues.

Have a look at http://tukaani.org/xz/java.html

XZ is a file format based on LZMA2 (an improved version of LZMA)

The guys that invented the XZ format build a pure java implementation of the XZ archive compression / extraction algorithms

The XZ file format is designed to store 1 file only. Thus you need to zip/tar the source folder(s) into a single uncompressed file first.

Using the java library is as easy as this:

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();
萌辣 2024-11-03 20:06:25

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

Check out the LzmaAlone.java and LzmaBench.java files in the Java/SevenZip folder of the zip file from that link you posted.

笑看君怀她人 2024-11-03 20:06:25

使用 J7Zip。它是 LZMA SDK 的 java 端口。您可以在这里找到它:

http://sourceforge.net/projects/p7zip/files/J7Zip/< /a>

替代方案

将 lzmajio.jar 与 LzmaInputStream 和 LzmaOutputStream 类一起使用,

您可以在 github 上找到它:

http://github .com/league/lzmajio/downloads

Use J7Zip. Its a java port of the LZMA SDK. You find it here:

http://sourceforge.net/projects/p7zip/files/J7Zip/

alternative

Use the lzmajio.jar with LzmaInputStream and LzmaOutputStream classes

you find it on github:

http://github.com/league/lzmajio/downloads

街道布景 2024-11-03 20:06:25

您可以使用这个库代替。它已经“过时”但仍然工作正常。

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

You can use this library instead. It is "outdated" but still works fine.

Maven dependency

<dependency>
    <groupId>com.github.jponge</groupId>
    <artifactId>lzma-java</artifactId>
    <version>1.2</version>
</dependency>

Utility class

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

Firstly you have to create a file with content to start compressing. You can use this website to generate random text.

Example compression and decompression

Path rawFile = Paths.get("raw.txt");
Path compressedFile = Paths.get("compressed.lzma");

LzmaCompressor lzmaCompressor = new LzmaCompressor(rawFile, compressedFile);
lzmaCompressor.compress();
lzmaCompressor.decompress();
伪装你 2024-11-03 20:06:25

以下是使用 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);
    }
}

Here are tested examples of using XZ Utils pure java library to pack and unpack XZ archives with LZMA2 compression algorithm with great ratio.

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);
    }
}
北方的巷 2024-11-03 20:06:25

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

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

Kotlin code for Android:

 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 code:

 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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文