如何使用LZMA SDK在Java中压缩/解压缩 [英] How to use LZMA SDK to compress/decompress in Java

查看:1149
本文介绍了如何使用LZMA SDK在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆