2013-07-08 56 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();