2016-11-28 14 views
1

私のアプリケーションはzipをダウンロードしなければならず、アプリケーションフォルダに解凍する必要があります。問題は、zipにはファイルがなくフォルダがあり、各フォルダには異なるファイルがあるということです。私は同じ構造を保つだろうが、どのようにそれを行うのか分からない。私はファイルのzipでそれを行うが、フォルダのzipでは成功しません。 どのようにそれを知っている誰かがいますか? 多くのありがとうございます。アンドロイドのzipからフォルダを抽出する

+0

https://github.com/commonsguy/cwac-security/#usage-ziputils – CommonsWare

答えて

3

ZIPアーカイブ内の各ディレクトリエントリのディレクトリを作成する必要があります。

/** 
* Unzip a ZIP file, keeping the directory structure. 
* 
* @param zipFile 
*  A valid ZIP file. 
* @param destinationDir 
*  The destination directory. It will be created if it doesn't exist. 
* @return {@code true} if the ZIP file was successfully decompressed. 
*/ 
public static boolean unzip(File zipFile, File destinationDir) { 
    ZipFile zip = null; 
    try { 
    destinationDir.mkdirs(); 
    zip = new ZipFile(zipFile); 
    Enumeration<? extends ZipEntry> zipFileEntries = zip.entries(); 
    while (zipFileEntries.hasMoreElements()) { 
     ZipEntry entry = zipFileEntries.nextElement(); 
     String entryName = entry.getName(); 
     File destFile = new File(destinationDir, entryName); 
     File destinationParent = destFile.getParentFile(); 
     if (destinationParent != null && !destinationParent.exists()) { 
     destinationParent.mkdirs(); 
     } 
     if (!entry.isDirectory()) { 
     BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry)); 
     int currentByte; 
     byte data[] = new byte[DEFUALT_BUFFER]; 
     FileOutputStream fos = new FileOutputStream(destFile); 
     BufferedOutputStream dest = new BufferedOutputStream(fos, DEFUALT_BUFFER); 
     while ((currentByte = is.read(data, 0, DEFUALT_BUFFER)) != EOF) { 
      dest.write(data, 0, currentByte); 
     } 
     dest.flush(); 
     dest.close(); 
     is.close(); 
     } 
    } 
    } catch (Exception e) { 
    return false; 
    } finally { 
    if (zip != null) { 
     try { 
     zip.close(); 
     } catch (IOException ignored) { 
     } 
    } 
    } 
    return true; 
} 
+0

良い仕事:ここでは、ディレクトリ構造を維持します、私が書いた方法と使用です。私を救う。 – Abhishek

関連する問題