2014-12-08 97 views
1

我無法解壓縮大文件(50 MB)。我試過ZipInputStreamZipFileAndroid:打開大型壓縮文件

我收到以下異常,當我使用ZipFile

java.util.zip.ZipException: EOCD not found; not a Zip archive?

當我使用ZipInputStream我得到的跟隨誤差:

there is no zip entry(zin.getNextEntry())

ZipInputStream代碼:

public static void unzip(File _zipFile, File directory) throws IOException { 
    try { 
     FileInputStream fin = new FileInputStream(_zipFile); 
     ZipInputStream zin = new ZipInputStream(fin); 
     ZipEntry ze = null; 
     while ((ze = zin.getNextEntry()) != null) { 
      // not getting here 
     } 
     zin.close(); 
    } 
    catch (Exception e) { 
    } 
} 

ZipFile代碼:

public static void unzipa(File zipfile, File directory) throws IOException { 
    try { 
     ZipFile zfile = new ZipFile(zipfile); // getting exception here 
     Enumeration<? extends ZipEntry> entries = zfile.entries(); 
     while (entries.hasMoreElements()) { 
      ZipEntry entry = entries.nextElement(); 

     } 
     zfile.close(); 
    } 
    catch (Exception ex) { 
     ErroHandling.HandleError(ex); 
    } 

} 
+0

可能重複卡爾很慢。我如何優化性能?](http://stackoverflow.com/questions/3975847/extrakting-zip-to-sd-card-is-very-slow-how-can-i-optimize-performance) – TheIT 2015-02-28 18:55:20

回答

1

如果初始化ZipFileZipExceptionIOException被拋出,那麼你應該測試ZIP的完整性。您可能還想確保您具有讀/寫訪問權限。如果您在Android的內部存儲(sdcard)上解壓縮此文件,則需要在AndroidManifest中聲明以下權限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

代碼看起來還好我,但這裏是一個解決方案,我熟了真正的快速和有效的ZIP測試文件大於100 MB:[Extrakting郵編爲SD的

public static boolean unzip(final File zipFile, final File destinationDir) { 
    ZipFile zip = null; 
    try { 
     zip = new ZipFile(zipFile); 
     final Enumeration<? extends ZipEntry> zipFileEntries = zip.entries(); 
     while (zipFileEntries.hasMoreElements()) { 
      final ZipEntry entry = zipFileEntries.nextElement(); 
      final String entryName = entry.getName(); 
      final File destFile = new File(destinationDir, entryName); 
      final File destinationParent = destFile.getParentFile(); 
      if (destinationParent != null && !destinationParent.exists()) { 
       destinationParent.mkdirs(); 
      } 
      if (!entry.isDirectory()) { 
       final BufferedInputStream is = new BufferedInputStream(
         zip.getInputStream(entry)); 
       int currentByte; 
       final byte data[] = new byte[2048]; 
       final FileOutputStream fos = new FileOutputStream(destFile); 
       final BufferedOutputStream dest = new BufferedOutputStream(fos, 2048); 
       while ((currentByte = is.read(data, 0, 2048)) != -1) { 
        dest.write(data, 0, currentByte); 
       } 
       dest.flush(); 
       dest.close(); 
       is.close(); 
      } 
     } 
    } catch (final Exception e) { 
     return false; 
    } finally { 
     if (zip != null) { 
      try { 
       zip.close(); 
      } catch (final IOException ignored) { 
      } 
     } 
    } 
    return true; 
} 
+0

我越來越例外行zip = new ZipFile(zipFile); java.util.zip.ZipException:未找到EOCD;不是一個Zip存檔? 我可以在桌面上打開zip文件。我可以打開,如果我減小文件大小爲10 MB(從zip中刪除一些文件)。 – user2330792 2014-12-08 09:43:06