【问题标题】:Unable to extract folders from zip file using ZipEntry in windows无法在 Windows 中使用 ZipEntry 从 zip 文件中提取文件夹
【发布时间】:2012-06-22 12:03:05
【问题描述】:
java.util.zip.ZipInputStream zis = new java.util.zip.ZipInputStream(new BufferedInputStream(is));

    java.util.zip.ZipEntry entry;
    new File(outdir+ File.separator+"changelog").delete();
    new File(outdir+ File.separator+"media").delete();
    try {
        while ((entry = zis.getNextEntry()) != null) {

            File of = new File(outdir + File.separator + entry.getName());

            if (entry.isDirectory()) {
                of.mkdirs();
                continue;
            } else {
                File xx = new File(of.getParent());
                if (!xx.exists()) {
                    Stack<String> todo = new Stack<String>();
                    do {
                        todo.push(xx.getAbsolutePath());
                        xx = new File(xx.getParent());
                    } while (!xx.exists());
                    while (todo.size() > 0) {
                        xx = new File(todo.pop());
                        if (!xx.exists()) {
                            xx.mkdirs();
                        }
                    }
                }
            }

            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(of), buffer.length);

            cpio(new BufferedInputStream(zis), bos, "unzip:" + entry.getName());

            bos.flush();
            bos.close();
        }
    } catch (IllegalArgumentException e) {
        // problem with chars in entry name likely
    }catch(Exception e){
        System.out.println(e+"Srikanth");
    }
    zis.close();

}

entry.isDirectory() 总是返回 false,因此它创建的是文件而不是目录。有什么问题?

【问题讨论】:

    标签: java zip extract


    【解决方案1】:

    ZipEntry 来自 ZipInputStream 表示文件末尾的空目录,带有 \ ,目录中的元素带有 /

    所以 entry.isDirectory() 不能使用空目录。

    ZipFile 中的 ZipEntry 工作正常。我认为 ZipInputStreamZipEntry 行为之间存在差异。

    【讨论】:

    【解决方案2】:

    isDirectory 根本不适用于使用 Windows 标准选项“发送到/压缩文件”压缩的文件

    zip 的格式与使用 7zip 或 Winzip 等工具生成的格式不同。 (很高兴有一个标准的存档压缩:D)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多