【问题标题】:Android view zip file without extractingAndroid查看zip文件而不解压
【发布时间】:2016-07-03 14:08:37
【问题描述】:

我正在尝试获取一个 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
      }

【问题讨论】:

    标签: java android zipfile


    【解决方案1】:

    尝试以下方法(见下文)来检索 zip 文件的文件列表。

    请注意:

    • 目录名称用作键。
    • 文件名存储在特定目录名称的List&lt;String&gt; 中。
    • 如果文件未存储在目录中,我们将添加到默认的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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多