如何使用 LZMA SDK 在 Java 中压缩/解压 [英] How to use LZMA SDK to compress/decompress in Java
问题描述
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.
推荐答案
简短回答:不要
7zip sdk 很旧而且没有维护,它只是围绕 C++ 库的 JNI 包装器.现代 JVM (1.7+) 上的纯 Java 实现与 C++ 实现一样快,并且依赖性和可移植性问题更少.
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.
看看 http://tukaani.org/xz/java.html
XZ 是基于 LZMA2(LZMA 的改进版)的文件格式
XZ is a file format based on LZMA2 (an improved version of LZMA)
发明 XZ 格式的人构建了 XZ 存档压缩/提取算法的纯 Java 实现
The guys that invented the XZ format build a pure java implementation of the XZ archive compression / extraction algorithms
XZ 文件格式旨在仅存储 1 个文件.因此,您需要先将源文件夹压缩/tar 到一个未压缩的文件中.
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.
使用 java 库就这么简单:
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();
这篇关于如何使用 LZMA SDK 在 Java 中压缩/解压的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!