【问题标题】:Getting an exception when trying to un-zip the 7-Zip Archive (in binary mode)尝试解压缩 7-Zip 存档时出现异常(二进制模式)
【发布时间】:2015-11-27 06:27:11
【问题描述】:

我正在 Eclipse 中运行 Java 应用程序。在这里,我正在尝试使用以下代码解压缩 7-Zip 存档文件:

SevenZFile sevenZFile = new SevenZFile(new File(localPath));
        SevenZArchiveEntry entry = sevenZFile.getNextEntry();
        while(entry!=null){
            tempExtractFilePath = extractFilePath + entry.getName();
            FileOutputStream out = new FileOutputStream(tempExtractFilePath);
            if(!patchOutputList.contains(tempExtractFilePath)) {
                patchOutputList.add(tempExtractFilePath);
            }
            byte[] content = new byte[(int) entry.getSize()];
            sevenZFile.read(content, 0, content.length);
            out.write(content);
            out.close();
            entry = sevenZFile.getNextEntry();
        }
sevenZFile.close();

在调试模式下(运行 Eclipse)执行上述一组行时,我能够解压缩 7-zip 文件(没有问题)。

但是当尝试以二进制模式执行相同操作时(在将 Java 应用程序导出到 .JAR 文件后),我遇到了以下异常跟踪:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/compress/archivers/sevenz/SevenZFile
    at com.ami.veb.core.patch.PatchInfo.unZipPatchFile(PatchInfo.java:1047)
    at com.ami.patch.patchinspector.PatchInspector.applyPatchesLocally(Patch
Inspector.java:536)
        at com.ami.patch.patchinspector.PatchInspector.main(PatchInspector.java:
198)
Caused by: java.lang.ClassNotFoundException:   org.apache.commons.compress.archive
rs.sevenz.SevenZFile
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
        ... 3 more

我还在 Java 应用程序的 .classpath 中添加了以下 2 行

<classpathentry exported="true" kind="lib" path="lib/commons-compress-1.9.jar"/>
<classpathentry exported="true" kind="lib" path="lib/xz-1.5.jar"/>

但它仍然没有解决问题。

任何帮助将不胜感激!

【问题讨论】:

    标签: java eclipse-rcp classpath 7zip eclipse-classpath


    【解决方案1】:

    不用自己写

    尝试导出 Runnable Jar File / 选项“将所需库打包到生成的 JAR”

    它会导出所有内容(以及其他依赖项)

    然后看到:Difference between extracting and packaging libraries into a jar file

    有帮助吗?

    【讨论】:

      猜你喜欢
      • 2015-06-16
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      • 1970-01-01
      相关资源
      最近更新 更多