【问题标题】:Java Unzip - no Exception but result is emptyJava Unzip - 没有异常但结果为空
【发布时间】:2014-09-11 22:16:15
【问题描述】:

我正在使用来自网络的以下代码

File dir = new File(dest.getAbsolutePath(), archiveName);
    // create output directory if it doesn't exist
    if (!dir.exists()) {
        dir.mkdirs();
    }
    System.err.println(pArchivePath);
ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(pArchivePath);
        Enumeration<?> enu = zipFile.entries();
        while (enu.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) enu.nextElement();

            String name = zipEntry.getName();
            long size = zipEntry.getSize();
            long compressedSize = zipEntry.getCompressedSize();
            System.out.printf("name: %-20s | size: %6d | compressed size: %6d\n", 
                    name, size, compressedSize);

            File file = new File(name);
            if (name.endsWith("/")) {
                System.err.println("make dir " + name);
                file.mkdirs();
                continue;
            }

            File parent = file.getParentFile();
            if (parent != null) {
                parent.mkdirs();
            }

            InputStream is = zipFile.getInputStream(zipEntry);
            FileOutputStream fos = new FileOutputStream(file);
            byte[] bytes = new byte[1024];
            int length;
            while ((length = is.read(bytes)) >= 0) {
                fos.write(bytes, 0, length);
            }
            is.close();
            fos.close();

        }
        zipFile.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

解压缩档案。它包含 7zip 或 Winrar 作为 .zip 存档,但重命名为 .qtz(但我不认为这会导致问题..) 因此,如果我运行代码来解压缩我的存档,一切正常:我在 sysout/err 上获得列出所有文件的输出,也没有发生异常,但是如果我查看目标目录......它是空的 - 只有根文件夹存在.

我也用过

  Runtime.getRuntime().exec(String.format("unzip %s -d %s", pArchivePath, dest.getPath()));

但我不能再使用它了,因为新进程已启动,并且在 java 代码中的解压缩过程之后,我将继续处理存档。

那么问题是.. 为什么这种和平的代码不起作用?有很多类似的例子,但没有一个对我有用。

br,菲利普

编辑:以下解决了我的问题

File file = new File(dir.getParent(), name);

所以我没有为这个文件设置正确的父路径。

【问题讨论】:

    标签: java stream unzip zip


    【解决方案1】:

    您的代码中的以下片段:

           File parent = file.getParentFile();
            if (parent != null) {
                parent.mkdirs();
            }
    

    这会在哪里创建父目录?因为我尝试了您的代码,它不是在目标目录中创建,而是在我的 Eclipse 项目目录中创建。查看您的代码,目标目录没有使用,对吧?

    代码实际上提取了 zip 文件的内容,但不是我预期的。

    【讨论】:

    • 是的,这是一个很好的提示。问题是“父”采用了“文件”的父路径。所以我刚刚用 new File(dir.getParent(), name);现在它对我有用。
    • 酷。您能否将问题标记为已回答?
    【解决方案2】:

    我在想,因为我看不到你在做这样的事情:

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipPath+ "Zip.zip"));
    out.putNextEntry(new ZipEntry("Results.csv"));
    

    我仍在努力,但我认为问题在于这会使文件在 zip 中

    你也应该使用 ZipOutputStream 来写;喜欢

    out.write(bytes, 0, length);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      • 2020-06-29
      相关资源
      最近更新 更多