1

我有一个应用程序来下载 zip 文件并保存到 sdcard。但是我zipentry=null在读取输入流时得到,它没有进入 while 块。有人可以帮我解决这个问题吗?

         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();
           }
4

1 回答 1

0

您已经 ZipEntry zipEntry=zipInputStream.getNextEntry();在 while 循环中有一个 before 条件。将其保留并仅在 while 循环中对其进行初始化。就像是:

 ZipEntry zipEntry;     
 while ((zipEntry = zipInputStream.getNextEntry()) != null){
  //
}
于 2011-08-17T11:35:30.750 回答