2012-02-20 8 views
1

ジップファイルを解凍します。私はバイトとしてzipデータを持っています。私はzipファイルを解凍するために以下のコードを使用しました。しかし、私はzipファイルのファイルから名前を読むことができます。しかし、出力データが破損しています。私はZipInputStreamの全データを読んでいると思います。どのように各ファイルのデータを読むことができますか? Plzを私に 感謝を...助けるバイトのジッパーデータを解凍

ByteArrayInputStream binput = new ByteArrayInputStream(zipeddatainbyte); 
    ZipInputStream mzipstream = new ZipInputStream(binput); 
    File f2 = new File("/sdcard/Unziped Data/"); 
    f2.mkdirs(); 
    ZipEntry entry; 
    FileOutputStream out; 
    byte[] bout=null; 
    try { 
     while ((entry = mzipstream.getNextEntry()) != null) { 
      System.out.println("entry: " + entry.getName() + ", " 
        + entry.getSize()); 
      bout=new byte[(int) entry.getSize()]; 
      mzipstream.read(bout); 
      out = new FileOutputStream("/sdcard/Unziped Data/"+entry.getName()); 
      out.write(bout); 
      out.close(); 



     } 
    } catch (IOException e) { 

     e.printStackTrace(); 
    } 
+0

この答えはあなたのための役に立つかもしれない http://stackoverflow.com/questions/5028421/android-unzip-a-folder –

答えて

2

あなたはentry.getSizeを()信頼が、それぞれのZipEntryのためのストリームの最後まで読み、実際に読まれているものをコントロールしようとはなりません。これは、ZipInputStreamsだけでなく、Java InputStreamにも適用されます。このスニペットはhereから抽出されます。

ZipEntry entrada; 
while (null != (entrada=zis.getNextEntry())){ 
    System.out.println(entrada.getName()); 

    FileOutputStream fos = new FileOutputStream(entrada.getName()); 
    int leido; 
    byte [] buffer = new byte[1024]; 
    while (0<(leido=zis.read(buffer))){ 
     fos.write(buffer,0,leido); 
    } 
    fos.close(); 
    zis.closeEntry(); 
} 
+0

ありがとうございました。..それはwoks ... :) :) –