apache commons使用7zip压缩

md1*_*980 8 compression apache zip 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());

    }
}
Run Code Online (Sandbox Code Playgroud)

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

线程"main"中的异常java.lang.NoClassDefFoundError:org.tukaani.xz.FilterOptions at java.lang.J9VMInternals.verifyImpl(Native Method)at 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的一部分.有什么想法吗?

Eri*_*man 20

正如Apache Commons 的" 已知限制"页面所述:

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

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

<dependency>
  <groupId>org.tukaani</groupId>
  <artifactId>xz</artifactId>
  <version>1.5</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

  • PSA:当前版本的 Apache Commons Compression (1.14) 需要版本 1.6“xz”。 (2认同)