作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试获取 zip 文件的内容而不解压它。我正在使用 ZipFile 来获取条目。但我观察到它是以 system/app.apk 等文件格式提供 zip 文件夹中的所有文件,而不是将 system 作为目录提供(如 file.listFiles() 给出的方式)。如何获取目录结构格式的文件?
邮政编码结构:
ZipFolder.zip - system (folder) -> app.apk(file)
- meta (folder) -> manifest(folder) -> new.apk (file)
代码:
ZipFile zipFile = new ZipFile(mPath);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while(entries.hasMoreElements()) {
// below code returns system/app.apk and meta/manifest/new.apk
// instead of system,meta folders
ZipEntry entry = entries.nextElement();
String fileName = entry.getName();
boolean isDirectory = entry.isDirectory(); //returns false
}
最佳答案
尝试以下方法(见下文)来检索 zip 文件的文件列表。
请注意:
List<String>
中对于特定的目录名称。root
关键。public HashMap<String, List<String>> retrieveListing(File zipFile) {
HashMap<String, List<String>> contents = new HashMap<>();
try {
FileInputStream fin = new FileInputStream(zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
if(ze.isDirectory()) {
String directory = ze.getName();
if (!contents.containsKey(directory)) {
contents.put(directory, new ArrayList<String>());
}
} else {
String file = ze.getName();
int pos = file.lastIndexOf("/");
if (pos != -1) {
String directory = file.substring(0, pos+1);
String fileName = file.substring(pos+1);
if (!contents.containsKey(directory)) {
contents.put(directory, new ArrayList<String>());
List<String> fileNames = contents.get(directory);
fileNames.add(fileName);
} else {
List<String> fileNames = contents.get(directory);
fileNames.add(fileName);
}
} else {
if (!contents.containsKey("root")) {
contents.put("root", new ArrayList<String>());
}
List<String> fileNames = contents.get("root");
fileNames.add(file);
}
}
zin.closeEntry();
}
zin.close();
} catch(Exception e) {
e.printStackTrace();
}
return contents;
}
关于java - Android查看zip文件而不解压,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38170766/
这个问题在这里已经有了答案: Why does += of a list within a Python tuple raise TypeError but modify the list anyw
我是一名优秀的程序员,十分优秀!