2016-07-03 8 views
0

Zipファイルの内容を抽出せずに取得しようとしています。私はZipFileを使ってエントリを取得しています。しかし、私が観察したことは、zipフォルダ内のすべてのファイルをsystem/app.apkのようなファイル形式で提供することです(file.listFiles()のように)。どのようにディレクトリ構造形式でファイルを取得するのですか?Androidビューの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

は、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; 
} 
関連する問題