gpt4 book ai didi

java - Android查看zip文件而不解压

转载 作者:行者123 更新时间:2023-12-01 09:44:35 25 4
gpt4 key购买 nike

我正在尝试获取 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com