2016-03-23 12 views
0

My UnZipクラスでファイル全体が解凍されません。このクラスは別のアクティビティから呼び出されます。私のzipファイルは、電話の内部ストレージのメインディレクトリに保存されます。 zipファイルにはフォルダとビデオがあります。 この解凍で何が問題になっていますか? 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