我试图获取zip文件的内容而不提取它。我正在使用ZipFile来获取条目。但我观察到它是以zip /文件格式提供zip文件夹中的所有文件,如system / app.apk,而不是将系统作为目录(如file.listFiles()给出的方式)。如何以目录结构格式获取文件?
Zip结构:
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
}
答案 0 :(得分:0)
尝试以下方法(见下文)检索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;
}