2011-08-17 16 views
1

私はzipファイルをダウンロードしてsdcardに保存するアプリケーションを持っています。しかし、私は入力ストリームを読んでいる間にzipentry=nullを取得しますが、whileブロックには入りません。この問題を解決するのに誰も助けてくれませんか?zipファイルをダウンロードしてsdcardに保存します

  try { 
      // fileInputStream = new InputStream(input); 
      ZipInputStream zipInputStream 
      = new ZipInputStream(new BufferedInputStream(input)); 
      ZipEntry zipEntry=zipInputStream.getNextEntry(); 

      Toast.makeText(getApplicationContext(), "I have entered try block zipentry"+zipEntry,Toast.LENGTH_LONG).show(); 
      System.out.println("Zipentry is"+zipEntry); 

      while ((zipEntry = zipInputStream.getNextEntry()) != null){ 
       Toast.makeText(getApplicationContext(), "Entered into while block",Toast.LENGTH_LONG).show(); 

      String zipEntryName = zipEntry.getName(); 
      File file = new File(to + zipEntryName); 

      if (file.exists()){ 

      } else { 
       if(zipEntry.isDirectory()){ 
       file.mkdirs(); 
       }else{ 
       byte buffer[] = new byte[BUFFER_SIZE]; 
       FileOutputStream fileOutputStream = new FileOutputStream(file); 
       bufferedOutputStream 
       = new BufferedOutputStream(fileOutputStream, BUFFER_SIZE); 
       int count; 

       while ((count 
       = zipInputStream.read(buffer, 0, BUFFER_SIZE)) != -1) { 
       bufferedOutputStream.write(buffer, 0, count); 
       } 

       bufferedOutputStream.flush(); 
       bufferedOutputStream.close(); 
       } 

      } 
      zipInputStream.close(); 
      } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
       Toast.makeText(getApplicationContext(), "File not found",Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
      }catch (IOException e) { 
      // TODO Auto-generated catch block 
       Toast.makeText(getApplicationContext(), "Input outout error",Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
      } 

答えて

0

whileループには、条件の前にすでに1つのZipEntry zipEntry=zipInputStream.getNextEntry();があります。それを残して、whileループでのみ初期化します。何かのように:

ZipEntry zipEntry;  
while ((zipEntry = zipInputStream.getNextEntry()) != null){ 
    // 
} 
+0

いいえ、ちょうどZipentryの値をチェックする前に私は値が空であることを知るようになった – Ammu