java.io.FileNotFoundException - zip4j - PullRequest
       106

java.io.FileNotFoundException - zip4j

0 голосов
/ 03 апреля 2019

Я попытался создать простую Java-программу, которая пытается разархивировать zip-файл с помощью zip4j. Но каждый раз я получаю исключение java.io.FileNotFoundException. Я не знаю почему, потому что путь существует, и файл может писать для Java. Также zip-файл не поврежден.

Я запускаю последнюю версию jdk с библиотекой lates zip4j. Все правильно реализовано. Я попытался разархивировать почтовый индекс, который включает только флягу, и это работало. Но почтовый индекс с большим количеством файлов не удался.

Я новичок в 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

Источник, где я узнаю, как распаковать: Что такое хорошая библиотека Java для архивирования / распаковки файлов?

1 Ответ

1 голос
/ 03 апреля 2019

Я нашел простой способ распаковать файлы @ Что такое хорошая библиотека Java для архивирования / распаковки файлов?

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();
    }
}
...