开发者

Download zip file and save it in sdcard

开发者 https://www.devze.com 2023-03-28 12:20 出处:网络
I have an application to download zip file and save to sdcard. But I get zipentry=null while reading inputstream, it doesn\'t enter the while block. Could anyone help me in solving this problem, pleas

I have an application to download zip file and save to sdcard. But I get zipentry=null while reading inputstream, it doesn't enter the while block. Could anyone help me in solving this problem, please?

         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 开发者_JAVA百科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();
           }


You already have one ZipEntry zipEntry=zipInputStream.getNextEntry(); before condition in the while loop. Leave it out and initialize it only in the while loop. Something like:

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

精彩评论

暂无评论...
验证码 换一张
取 消