2011-03-30 524 views

回答

4

查看您發佈的鏈接中壓縮文件的Java/SevenZip文件夾中的LzmaAlone.java和LzmaBench.java文件。

+0

我不明白,他們不應該爲此做一個javadoc嗎? – lamwaiman1988 2011-03-30 04:01:06

+2

是的,他們應該已經做了一些文檔(或javadoc),但有時你必須看看示例代碼,這是我指示你。 – 2012-01-24 19:10:03

38

簡短的回答:不

的7zip的SDK是舊的,無人維護,它只是圍繞着C++庫JNI包裝。現代JVM(1.7+)上的純Java實現與C++一樣快,並具有較少的依賴性和可移植性問題。

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

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

你能解釋你的簡短答案嗎? – 2014-10-19 21:02:48

+3

這是一個相當古老的答案,我建議的圖書館可能已經被更新/更好的東西所取代。它的要點是7zip sdk是舊的,沒有維護,它只是一個圍繞C++庫的JNI包裝。現代JVM(1.7+)上的純Java實現與C++一樣快,並具有較少的依賴性和可移植性問題。只是我2美分。 – 2014-10-20 22:17:59

+0

我希望你不介意我的編輯:P – 2014-10-20 23:19:48

2

你可以改爲使用this庫。這是「過時」,但仍然正常工作。

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

首先,你必須創建內容,開始壓縮文件。您可以使用this網站來生成隨機文本。

例壓縮和解壓

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

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