2014-10-29 90 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; 

}