【问题标题】:Download zip file and save it in sdcard下载 zip 文件并将其保存在 sdcard 中
【发布时间】:2011-08-17 11:00:02
【问题描述】:

我有一个下载 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();
           }

【问题讨论】:

    标签: android zipinputstream


    【解决方案1】:

    在 while 循环中,您已经有一个 ZipEntry zipEntry=zipInputStream.getNextEntry(); 之前的条件。将其保留并仅在 while 循环中对其进行初始化。比如:

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

    【讨论】:

    • 不,只是为了检查我之前初始化的Zipentry的值,通过它我知道该值为空
    猜你喜欢
    • 1970-01-01
    • 2019-09-05
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    相关资源
    最近更新 更多