【问题标题】:java.io.FileNotFoundException - zip4jjava.io.FileNotFoundException - zip4j
【发布时间】:2019-04-03 16:01:52
【问题描述】:

我尝试创建一个简单的 Java 程序,该程序尝试使用 zip4j 解压缩 zip 文件。但是每次我得到一个java.io.FileNotFoundException。我不知道为什么,因为路径存在并且文件可以为 java 写。 zip 文件也没有损坏。

我使用lates zip4j 库运行最新的jdk。一切都正确实施。我试图解压缩一个只包含一个 jar 的 zip 并且它有效。但是包含更多文件的 zip 失败了。

我是 Java 新手,也是使用 zip4j 的新手。

String enviorment = new File(".").getCanonicalPath();
unzip(enviorment + "/../WITCLASS/cache/dokumentation.zip", enviorment + "/../WITCLASS/cache/");

.zip 文件包括一些 .html 和 .js 文件

private static void unzip(String zipFilePath, String destDir) {
        String source = zipFilePath;
        String destination = destDir;
        String password = "";

        try {
            ZipFile zipFile = new ZipFile(source);
            if (zipFile.isEncrypted()) {
                zipFile.setPassword(password);
            }
            zipFile.extractAll(destination);
        } catch (ZipException e) {
            e.printStackTrace();
            System.out.println(e);
        }
    }

预期的输出是:所有文件都成功解压。

但我只有:

net.lingala.zip4j.exception.ZipException: net.lingala.zip4j.exception.ZipException: net.lingala.zip4j.exception.ZipException: java.io.FileNotFoundException: /home/user/Downloads/ENGINE/../WITCLASS/cache/dokumentation/example/elements.html (Not a directory)
    at net.lingala.zip4j.unzip.Unzip.initExtractFile(Unzip.java:163)
    at net.lingala.zip4j.unzip.Unzip.initExtractAll(Unzip.java:83)
    at net.lingala.zip4j.unzip.Unzip.extractAll(Unzip.java:73)
    at net.lingala.zip4j.core.ZipFile.extractAll(ZipFile.java:488)
    at net.lingala.zip4j.core.ZipFile.extractAll(ZipFile.java:451)
    at NavigationBar.unzip(NavigationBar.java:329)
    at NavigationBar.aptget(NavigationBar.java:205)
    at NavigationBar$ScriptBridge.execute(NavigationBar.java:163)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at com.sun.webkit.Utilities.lambda$fwkInvokeWithContext$0(Utilities.java:94)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.webkit.Utilities.fwkInvokeWithContext(Utilities.java:94)
    at com.sun.webkit.WebPage.twkProcessMouseEvent(Native Method)
    at com.sun.webkit.WebPage.dispatchMouseEvent(WebPage.java:807)
    at javafx.scene.web.WebView.processMouseEvent(WebView.java:1045)
    at javafx.scene.web.WebView.lambda$registerEventHandlers$3(WebView.java:1168)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$5(GtkApplication.java:139)
    at java.lang.Thread.run(Thread.java:748)
Caused by: net.lingala.zip4j.exception.ZipException: net.lingala.zip4j.exception.ZipException: java.io.FileNotFoundException: /home/user/Downloads/ENGINE/../WITCLASS/cache/dokumentation/example/elements.html (Not a directory)
    at net.lingala.zip4j.unzip.UnzipEngine.unzipFile(UnzipEngine.java:98)
    at net.lingala.zip4j.unzip.Unzip.initExtractFile(Unzip.java:160)
    ... 53 more
Caused by: net.lingala.zip4j.exception.ZipException: java.io.FileNotFoundException: /home/user/Downloads/ENGINE/../WITCLASS/cache/dokumentation/example/elements.html (Not a directory)
    at net.lingala.zip4j.unzip.UnzipEngine.getOutputStream(UnzipEngine.java:435)
    at net.lingala.zip4j.unzip.UnzipEngine.unzipFile(UnzipEngine.java:79)
    ... 54 more
Caused by: java.io.FileNotFoundException: /home/user/Downloads/ENGINE/../WITCLASS/cache/dokumentation/example/elements.html (Not a directory)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(FileOutputStream.java:270)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
    at net.lingala.zip4j.unzip.UnzipEngine.getOutputStream(UnzipEngine.java:432)
    ... 55 more

我了解如何解压缩的来源: What is a good Java library to zip/unzip files?

【问题讨论】:

  • 消息不清楚/home/user/Downloads/ENGINE/../WITCLASS/cache/dokumentation/example/elements.html(不是目录)
  • 您没有在此处指定作为参数传递的内容,但看起来您指定了 HTML 文件作为第二个参数,而不是指定目录。
  • @Jens 这是我收到的唯一消息。我尝试指定,但这是我唯一知道的事情
  • @RealSkeptic 我现在添加了传递的参数。它是一个 .zip 文件,其中包含一些 .html 文件、文件夹和 .js 文件。我只传递 zip 的路径。

标签: java zip unzip zip4j


【解决方案1】:

我找到了一个简单的解决方法来解压文件@What is a good Java library to zip/unzip files?

unzip("path/to/SORUCE.zip", "path/to/destinationfolder");

public void unzip(String source, String dest) throws IOException {
    java.util.zip.ZipFile zipFile = new ZipFile(source);
    try {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            File entryDestination = new File(dest, entry.getName());
            if (entry.isDirectory()) {
                entryDestination.mkdirs();
            } else {
                entryDestination.getParentFile().mkdirs();
                InputStream in = zipFile.getInputStream(entry);
                OutputStream out = new FileOutputStream(entryDestination);
                IOUtils.copy(in, out);
                IOUtils.closeQuietly(in);
                out.close();
            }
        }
    } finally {
        zipFile.close();
    }
}

【讨论】:

  • 什么是压缩/解压缩文件的好 Java 库? 库的建议是题外话
  • @Jens 没错。我只想帮助这篇文章,如果还没有解决方案,可以向用户展示一种解决方法。这就是为什么我没有将此帖子设置为解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-21
  • 2014-08-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多