2014-10-29 71 views
0

我正在unziping一個基於官方文檔和一些例子的文件。我目前的實現將該文件解壓縮到zip文件所在的同一目錄中。我想解壓到設備中的特定目錄。我怎樣才能做到這一點? ZipInputStream允許這個功能,還是我必須解壓縮,然後將文件移動到所需的文件夾?如何解壓縮文件在Android中執行特定的文件夾?

這是我的代碼:

public static boolean unpackZip(String path, String zipname) { 
    InputStream is; 
    ZipInputStream zis; 
    try { 
     String filename; 
     is = new FileInputStream(path + zipname); 
     zis = new ZipInputStream(new BufferedInputStream(is)); 
     ZipEntry ze; 
     byte[] buffer = new byte[1024]; 
     int count; 

     while ((ze = zis.getNextEntry()) != null) { 
      filename = ze.getName(); 

      if (ze.isDirectory()) { 
       File fmd = new File(path + filename); 
       fmd.mkdirs(); 
       continue; 
      } 

      FileOutputStream fout = new FileOutputStream(path + filename); 

      while ((count = zis.read(buffer)) != -1) { 
       fout.write(buffer, 0, count); 
      } 

      fout.close(); 
      zis.closeEntry(); 
     } 

     zis.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } 

    return true; 
} 

回答

0

當您打開該文件作爲File fmd = new File(path + filename);FileOutputStream fout = new FileOutputStream(path + filename);,你可以簡單地追加目標目錄path + filename僅僅是文件路徑。我不確定你在這種情況下傳遞的路徑是什麼,我假定爲zip文件的路徑,如果你想提取其他地方你需要傳遞另一個變量與目標。例如:

public static boolean unpackZip(String path, String zipname, String outputPath) { 
    InputStream is; 
    ZipInputStream zis; 
    try { 
     String filename; 
     is = new FileInputStream(path + zipname); 
     zis = new ZipInputStream(new BufferedInputStream(is)); 
     ZipEntry ze; 
     byte[] buffer = new byte[1024]; 
     int count; 

     while ((ze = zis.getNextEntry()) != null) { 
      filename = ze.getName(); 

      if (ze.isDirectory()) { 
       File fmd = new File(outputPath + filename); 
       fmd.mkdirs(); 
       continue; 
      } 

      FileOutputStream fout = new FileOutputStream(outputPath + filename); 

      while ((count = zis.read(buffer)) != -1) { 
       fout.write(buffer, 0, count); 
      } 

      fout.close(); 
      zis.closeEntry(); 
     } 

     zis.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } 

    return true; 
} 
0

指定目標目錄作爲參數。除此之外,你的代碼似乎沒問題。

public static boolean unpackZip(String path, String zipname, String targetDirectory) { 
    InputStream is; 
    ZipInputStream zis; 
    try { 
     String filename; 
     is = new FileInputStream(path + zipname); 
     zis = new ZipInputStream(new BufferedInputStream(is)); 
     ZipEntry ze; 
     byte[] buffer = new byte[1024]; 
     int count; 

     while ((ze = zis.getNextEntry()) != null) { 
      filename = ze.getName(); 

      if (ze.isDirectory()) { 
       File fmd = new File(targetDirectory + filename); 
       fmd.mkdirs(); 
       continue; 
      } 

      FileOutputStream fout = new FileOutputStream(targetDirectory + filename); 

      while ((count = zis.read(buffer)) != -1) { 
       fout.write(buffer, 0, count); 
      } 

      fout.close(); 
      zis.closeEntry(); 
     } 

     zis.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } 

    return true; 
} 
1

您需要通過拆分字符串創建的文件夾:

   if (filename.contains("/")){ 
       String[] folders = filename.split("/"); 
       for (String item : folders) 
       { 
        File fmd = new File(path + item); 
        if (!item.contains(".") && !fmd.exists()){ 
         fmd.mkdirs(); 
         Log.d("created folder", item); 
        } 
       } 
      } 

全碼:

public static boolean unpackZip(String path, String zipname) { 
InputStream is; 
ZipInputStream zis; 
try { 
    String filename; 
    is = new FileInputStream(path + zipname); 
    zis = new ZipInputStream(new BufferedInputStream(is)); 
    ZipEntry ze; 
    byte[] buffer = new byte[1024]; 
    int count; 

    while ((ze = zis.getNextEntry()) != null) { 
     filename = ze.getName(); 

     if (ze.isDirectory()) { 
      File fmd = new File(path + filename); 
      fmd.mkdirs(); 
      continue; 
     } 
      //ADD THIS// 
      if (filename.contains("/")){ 
       String[] folders = filename.split("/"); 
       for (String item : folders) 
       { 
        File fmd = new File(path + item); 
        if (!item.contains(".") && !fmd.exists()){ 
         fmd.mkdirs(); 
         Log.d("created folder", item); 
        } 
       } 
      } 

     FileOutputStream fout = new FileOutputStream(path + filename); 

     while ((count = zis.read(buffer)) != -1) { 
      fout.write(buffer, 0, count); 
     } 

     fout.close(); 
     zis.closeEntry(); 
    } 

    zis.close(); 
} catch (IOException e) { 
    e.printStackTrace(); 
    return false; 
} 

return true; 

}

相關問題