2014-12-08 90 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