2016-03-23 56 views
0

我的UnZip类不解压整个文件。这个类是从另一个活动中调用的。我的zip文件保存在手机内部存储器的主目录中。该zip文件包含文件夹和一些视频。 这个unzip有什么问题? 什么和如何从zip解压缩和解压缩文件是相同的意思?解压缩时出错

感谢您的帮助!

public class Unzip { 
    private static final String INPUT_ZIP_FILE = "sdcard/downloaded_issue.zip"; 
    private static final String OUTPUT_FOLDER = "sdcard/Atlantis/"; 

    public static void main() 
    { 
    Unzip unZip = new Unzip(); 
    unZip.unZipIt(INPUT_ZIP_FILE, OUTPUT_FOLDER); 
    } 

/** 
* Unzip it 
* @param zipFile input zip file 
* @param outputFolder zip file output folder 
*/ 
public void unZipIt(String zipFile, String outputFolder){ 
    byte[] buffer = new byte[1024]; 
    try{ 

     //create output directory is not exists 
     File folder = new File(OUTPUT_FOLDER); 
     if(!folder.exists()){ 
      folder.mkdir(); 
     } 

     //get the zip file content 
     ZipInputStream zis = 
       new ZipInputStream(new FileInputStream(zipFile)); 
     //get the zipped file list entry 
     ZipEntry ze = zis.getNextEntry(); 

     while(ze!=null){ 

      String fileName = ze.getName(); 
      File newFile = new File(outputFolder + File.separator + fileName); 

      System.out.println("file unzip : "+ newFile.getAbsoluteFile()); 

      //create all non exists folders 
      //else you will hit FileNotFoundException for compressed folder 
      new File(newFile.getParent()).mkdirs(); 

      FileOutputStream fos = new FileOutputStream(newFile); 

      int len; 
      while ((len = zis.read(buffer)) > 0) { 
       fos.write(buffer, 0, len); 
      } 

      fos.close(); 
      if (ze.isDirectory()) { 
      ze = zis.getNextEntry(); 
      } 
     } 

     zis.closeEntry(); 
     zis.close(); 

     System.out.println("Done"); 

    }catch(IOException ex){ 
     ex.printStackTrace(); 
    } 
    } 
} 
+0

的编程语言是这样吗?乍看起来像是Java或C#。 – Nyerguds

+0

这是Java。我正在使用Android Studio进行编程 – Weblu

+0

然后将其标记为。如果包含编程语言,你会得到更快的响应;人们往往会对此进行过滤。 – Nyerguds

回答

0

我认为你的'while'循环被破坏;如果下一个条目是一个目录,那么您只会获取下一个条目,而我假设您可能正在尝试跳过的目录。

无论如何,由于您为遇到的所有文件创建了文件夹,因此您可以跳过文件夹条目并写入文件条目。唯一的例外是创建空文件夹。

更换while循环通过此代码应工作:

while(ze!=null){ 
     String fileName = ze.getName(); 
     File newFile = new File(outputFolder + File.separator + fileName); 
     System.out.println("file unzip : "+ newFile.getAbsoluteFile()); 
     //create all non exists folders 
     //else you will hit FileNotFoundException for compressed folder 
     if (ze.isDirectory()) { 
      // create the folder 
      newFile.mkdirs(); 
     } 
     else { 
      // create the parent folder and write to disk 
      new File(newFile.getParent()).mkdirs(); 
      FileOutputStream fos = new FileOutputStream(newFile); 
      int len; 
      while ((len = zis.read(buffer)) > 0) { 
       fos.write(buffer, 0, len); 
      } 
      fos.close(); 
     } 
     // get the next item 
     ze = zis.getNextEntry(); 
    } 
+0

我需要提取所有文件夹。不要跳过它们。 – Weblu

+0

那么你显然需要更多的代码。 – Nyerguds

+0

不,等一下。这不是递归的,除非文件夹为空,否则文件夹条目自身没有任何价值,因为您自动为文件自动创建文件夹。你可以做的最好的是创建它们,然后跳过它们,是的。 – Nyerguds