2016-03-08 18 views
0

喜我嘗試提取zip文件 和zip文件包含follwoing內容ZIP條目顯示目錄中的文件

zipfile.zip:

  • zip文件/ text1.txt
  • 壓縮文件/ text2.txt
  • zip文件/分貝/模式/的text.txt
  • zip文件/僞影/ tex.txt

,這是,我的代碼

File file = new File("/home/solomon/originar.zip"); 
     try { 
      InputStream ios = new FileInputStream(file); 
      ZipInputStream zis = new ZipInputStream(ios); 
      File outputfolder = new File(file.getParent()+File.separator+"EXTRACT"); 
      if(!outputfolder.exists()){ 
       outputfolder.mkdirs(); 
      } 
      ZipEntry ZENTRY; 
      FileOutputStream fos=null; 
      while((ZENTRY=zis.getNextEntry())!=null){ 
       File file1 = new File(outputfolder.getAbsolutePath()+File.separator+ZENTRY.getName()); 
       new File(file1.getParent()).mkdirs(); 
       System.out.println("filename"+file1.isDirectory()); 
       if(!file1.isDirectory()){ 
        fos = new FileOutputStream(file1); 
        int len; 
        while((len=zis.read())>-1){ 
         fos.write(len); 
        } 
       } 




      } 
      fos.close(); 
      zis.close(); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 



    } 

file1.txtfile2.txt已經成功地提取,但是當我讀到text.txt這裏面db/schema文件夾失敗,因爲db被顯示爲文件,而不是文件夾。如何解決這個問題?

回答

0

請參閱內嵌批註

// remove the EXTRACT string from the output folder path.I think there is no extract directory 

File outputfolder = new File(file.getParent()+File.separator+"EXTRACT"); 
. 
. 
. 
    while((ZENTRY=zis.getNextEntry())!=null){ 
    // remove the outputfolder.getAbsolutePath() 
    File file1 = new File(outputfolder+File.separator+ZENTRY.getName()); 
      new File(file1.getParent()).mkdirs(); 
      System.out.println("filename::"+ file1.getName()+"::"+file1.isDirectory()); 
      if(!file1.isDirectory()){ 
       fos = new FileOutputStream(file1); 
       int len; 
       while((len=zis.read())>-1){ 

        fos.write(len); 
       } 
       System.out.println("file name on read::"+ file1); 
      }