2013-07-08 57 views
8

拉鍊我知道如何創建zip文件:如何創建與LZMA壓縮

import java.io.*; 
import java.util.zip.*; 
public class ZipCreateExample{ 
    public static void main(String[] args) throws Exception 
     // input file 
     FileInputStream in = new FileInputStream("F:/sometxt.txt"); 

     // out put file 
     ZipOutputStream out = new ZipOutputStream(new FileOutputStream("F:/tmp.zip")); 

     // name the file inside the zip file 
     out.putNextEntry(new ZipEntry("zippedjava.txt")); 

     // buffer size 
     byte[] b = new byte[1024]; 
     int count; 

     while ((count = in.read(b)) > 0) { 
      System.out.println(); 
      out.write(b, 0, count); 
     } 
     out.close(); 
     in.close(); 
    } 
} 

但我不知道如何使用LZMA壓縮。

我發現這個項目:https://github.com/jponge/lzma-java它創建壓縮文件,但我不知道我應該如何結合它與我現有的解決方案。

+0

無論是Java的郵編util的,也不是每一個ZipEntry的共享,壓縮支持LZMA壓縮。大概需要一兩天的時間來擴展Commons-Compress,通過使用上面的LZMA代碼來支持它並覆蓋存儲檢查| DEFLATE。事實上,如果Commons-Compress可以使用更可擴展的方法,ZipArchiveEntries使用所需的壓縮方法(例如ZipArchiveEntryLZMA)進行擴展,那將會很不錯。事實上,ZipArchiveOutputStream中的檢查太多,無法很快完成。 –

回答

0

有一個在你提到的網站的例子:適應您的需求

final File sourceFile = new File("F:/sometxt.txt"); 
final File compressed = File.createTempFile("lzma-java", "compressed"); 

final LzmaOutputStream compressedOut = new LzmaOutputStream.Builder(
     new BufferedOutputStream(new FileOutputStream(compressed))) 
     .useMaximalDictionarySize() 
     .useEndMarkerMode(true) 
     .useBT4MatchFinder() 
     .build(); 

final InputStream sourceIn = new BufferedInputStream(new FileInputStream(sourceFile)); 

IOUtils.copy(sourceIn, compressedOut); 
sourceIn.close(); 
compressedOut.close(); 

(我不知道,如果它的工作原理,它是圖書館的只是使用與您的代碼片段)

+0

正如我在我的問題所說,這只是創建壓縮文件沒有壓縮存檔 – hudi

+0

是不是一樣?...輸出只是輸入字節的壓縮流,然後寫入文件。 – matcauthon

+0

nope它沒有,因爲存檔可能包含大量的壓縮文件 – hudi

2

最新版本的Apache Commons Compress(1.6版在2013年10月23日發佈)支持LZMA壓縮。

看看http://commons.apache.org/proper/commons-compress/examples.html,特別是關於.7z壓縮/解壓的。

比如你要存儲來自HTTP響應HTML頁面,並要壓縮它說:

SevenZOutputFile sevenZOutput = new SevenZOutputFile(new File("outFile.7z")); 

File entryFile = new File(System.getProperty("java.io.tmpdir") + File.separator + "web.html"); 
SevenZArchiveEntry entry = sevenZOutput.createArchiveEntry(entryFile, "web.html"); 

sevenZOutput.putArchiveEntry(entry); 
sevenZOutput.write(rawHtml.getBytes()); 
sevenZOutput.closeArchiveEntry(); 
sevenZOutput.close();