apache commons使用7zip压缩

时间:2015-01-30 04:10:04

标签: apache zip compression 7zip apache-commons-compress

我正在尝试使用以下代码,我从apache commons压缩示例网页创建一个zip文件,使用sevenZ类希望压缩比普通java zip更快。这就是我的代码看起来像

public static void main(String[] args) {
    // TODO Auto-generated method stub
    try{
    BufferedInputStream instream = new BufferedInputStream(new FileInputStream("c:/temp/test.txt"));

    SevenZOutputFile sevenZOutput = new SevenZOutputFile(new File("c:/temp/7ztest.zip"));
    SevenZArchiveEntry entry = sevenZOutput.createArchiveEntry(new File("c:/temp/test.txt"),"blah.txt");
    sevenZOutput.putArchiveEntry(entry);
    byte[] buffer = new byte[1024];
    int len;
    while ((len = instream.read(buffer)) > 0) {sevenZOutput.write(buffer, 0, len);}

    sevenZOutput.closeArchiveEntry();
    sevenZOutput.close();
    instream.close();
    }catch(IOException ioe) {
        System.out.println(ioe.toString());

    }
}

我得到这个看起来如此无关的错误

  

线程“main”中的异常java.lang.NoClassDefFoundError:org.tukaani.xz.FilterOptions       at java.lang.J9VMInternals.verifyImpl(Native Method)       在java.lang.J9VMInternals.verify(J9VMInternals.java:93)

我已经导入了apache包

  

import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;   import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;

但不确定org.tukaani.xz.FilterOptions是什么,它看起来不像是apace commons compress的一部分。有什么想法吗?

1 个答案:

答案 0 :(得分:16)

正如Apache Commons的" Known Limitations"页面所述:

"格式需要其他可选的XZ for Java库。"

对于其他格式,此依赖关系是可选的,但您需要7zip。

<dependency>
  <groupId>org.tukaani</groupId>
  <artifactId>xz</artifactId>
  <version>1.5</version>
</dependency>