使用java.util.ZipFile在同一层次中解压缩zipfile [英] unzip a zipfile in the same hierachy using java.util.ZipFile

查看:249
本文介绍了使用java.util.ZipFile在同一层次中解压缩zipfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个具有多个嵌套目录结构的zip文件,如何将其解压缩到同一个树结构中?
是否ZipFile.entries()以任何顺序提供枚举?

given a zip file with multiple nested directory structure, how do I unzip it into the same tree structure? does ZipFile.entries() provide the enumeration in any order?

推荐答案

Zip不提供每个目录结构SE。通过具有每个条目的完整路径来构建类似树的结构。 ZipFile以与添加到文件相同的方式枚举条目。

Zip doesn't offer directory structure per se. The tree alike structure is built by having full path of each entry. ZipFile enumerates the entries in the same way they have been added to the file.

注意:java.util.ZipEntry.isDirectory()只测试是否最后一个字符名称是'/',它的工作原理。

Note: java.util.ZipEntry.isDirectory() just tests if the last character of the name is '/', that's how it works.

将文件解压缩到同一目录所需的内容。 Parse然后命名如下:

What you need to extract the files into the same directory. Parse then name like that:

for(ZipEntry zipEntry : java.util.Collections.list(zipFile.entries())){//lazislav
    String name = zipEntry.getName();
    int idx = name.lastIndexOf('/');
    if (idx>=0) name=name.substring(idx)
    if (name.length()==0) continue;

    File f = new File(targetDir, name);

}

这应该或多或少(你还需要照顾重复的文件名等)

That shall do it more or less (you still need to take care of duplicate file names, etc)

这篇关于使用java.util.ZipFile在同一层次中解压缩zipfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆