2014-09-04 67 views
0

我已經從互聯網下載了一個zip文件並保存在內部存儲器中。當我嘗試閱讀zip文件時,我不斷收到EOFException。另外,我注意到從下面代碼片段中的file.length()獲得的長度大於實際文件。Android:閱讀zipfile - EOFException

這是我的代碼片段。

File file = new File(filePath + "/" + zipFile); 
    long len = file.length(); 


    FileInputStream fis = new FileInputStream(file); 
    BufferedInputStream bis = new BufferedInputStream(fis); 
    ZipInputStream zipInputStream = new ZipInputStream(bis); 

回答

1

您可以使用此方法,

public static void unzipFile(ZipFile zipFile, File destinationPath) { 
     Enumeration files = zipFile.entries(); 
     File f = null; 
     FileOutputStream fos = null; 

     while (files.hasMoreElements()) { 
     try { 
      ZipEntry entry = (ZipEntry) files.nextElement(); 
      InputStream eis = zipFile.getInputStream(entry); 
      byte[] buffer = new byte[1024]; 
      int bytesRead = 0; 

      f = new File(destinationPath.getAbsolutePath() + File.separator + entry.getName()); 

      if (entry.isDirectory()) { 
       f.mkdirs(); 
       continue; 
      } 
      else { 
       f.getParentFile().mkdirs(); 
       f.createNewFile(); 
      } 

      fos = new FileOutputStream(f); 

      while ((bytesRead = eis.read(buffer)) != -1) { 
       fos.write(buffer, 0, bytesRead); 
      } 
     } 
     catch (IOException e) { 
      Log.e(0, DEBUG_TAG, e.getMessage()); 
      continue; 
     } 
     finally { 
      if (fos != null) { 
       try { 
        fos.close(); 
       } 
       catch (IOException e) { 
        Log.e(0, DEBUG_TAG, e.getMessage()); 
       } 
      } 
     } 
     } 
    } 
+0

我已經試過這個,但是當我做 '檔案文件=新的文件(文件路徑+ 「/」 + zipFileName);' 'ZipFile的壓縮文件= new ZipFile(file);' 它給出錯誤 - ArrayIndexOoutOfBounds – 2014-09-04 23:31:19

+0

File file = new File(filePath +「/」+「zipFileName.zip」); unzipFile(new ZipFile(file),new File(destinationPath)); – mustafasevgi 2014-09-05 22:00:32