Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中使用LZMA SDK进行压缩/解压缩_Java_7zip_Lzma - Fatal编程技术网

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

如何在Java中使用LZMA SDK进行压缩/解压缩,java,7zip,lzma,Java,7zip,Lzma,这个网站提供了一个LZMA SDK用于压缩/解压缩文件,我想试一试,但我迷路了 有人有这方面的经验吗?还是教程?谢谢。从您发布的链接查看zip文件的java/SevenZip文件夹中的LzmaAlone.java和LzmaBench.java文件。简短回答:不要 7Zip SDK是旧的和未维护的,它只是C++库周围的JNI包装器。在现代JVM(1.7 +)上实现纯java与C++一样快,并且具有更少的DENDECIES和可移植性问题。 看看 XZ是一种基于LZMA2(LZMA的改进版本)的文件

这个网站提供了一个LZMA SDK用于压缩/解压缩文件,我想试一试,但我迷路了


有人有这方面的经验吗?还是教程?谢谢。

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

简短回答:不要

7Zip SDK是旧的和未维护的,它只是C++库周围的JNI包装器。在现代JVM(1.7 +)上实现纯java与C++一样快,并且具有更少的DENDECIES和可移植性问题。 看看

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

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

XZ文件格式设计为仅存储1个文件。因此,您需要先将源文件夹压缩/压缩到单个未压缩文件中

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

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

另类

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

您可以在github上找到它:

您可以改用library。它已经“过时”,但仍然可以正常工作

Maven依赖关系

<dependency>
    <groupId>com.github.jponge</groupId>
    <artifactId>lzma-java</artifactId>
    <version>1.2</version>
</dependency>
首先,您必须创建一个包含内容的文件才能开始压缩。您可以使用网站生成随机文本

压缩和解压缩示例

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

LzmaCompressor lzmaCompressor = new LzmaCompressor(rawFile, compressedFile);
lzmaCompressor.compress();
lzmaCompressor.decompress();

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

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

我不明白,他们不是应该做一个javadoc吗?是的,他们应该做一些文档(或javadoc),但有时你必须看一下示例代码,这就是我指导你的。没有经验,但你可能对此感兴趣:你能解释一下你的简短答案吗?这是一个相当古老的答案,我建议的库可能已经被更新/更好的东西取代了。它的要点是7Zip SDK是旧的和未维护的,它只是C++库周围的JNI包装器。在现代JVM(1.7 +)上实现纯java与C++一样快,并且具有更少的DENDECIES和便携性问题。我只有2美分。你在Sun/Oracle JVM上是正确的,它和C++一样快。不幸的是,在Android上,这种操作仍然慢5-10倍(即使在带有ART的Android 6上也是如此)。令人沮丧。在我的回答中,有更多关于此库的示例:
 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);
    }