22

如果我通过 ZipFile 类打开一个大 zip 文件 (250MB) 并尝试读取条目。这在模拟器和真实硬件中的 2.x 上运行良好。如果我在我的平板电脑(Asus Transformer 运行 4.0.3)或模拟器(3.2)上使用确切的一些代码,我无法读取任何条目。ZipFile 类的 size() 函数始终返回零,并且 ZipFile 不返回任何 zip 条目。即使是我平板电脑上 ROM 附带的 zip 应用程序也无法读取任何条目。zip 文件未损坏。我检查了它。

从 ZipFile 读取的代码适用于所有具有较小 zip 文件的版本。2.x 和 3.x/4.x 之间发生了什么变化?

我的测试文件是来自 HighVoltage Sid Collection 的 C64Music.zip。它包含超过 40.000 个文件,大小约为 250MB。

我不知道在哪里看。

4

3 回答 3

2

这是 android ZipFile 实现的一个已知问题:

http://code.google.com/p/android/issues/detail?id=23207

基本上 zip 文件只支持最多 65k 个条目。有一个名为 Zip64 的 zip 文件格式的扩展版本,它支持更多的条目。不幸的是,Android 上的 ZipFile 无法读取 Zip64。您可能会发现 C64Music.zip 文件是 Zip64 格式

一种解决方法是使用 Apache Commons Compress 库而不是本机实现。他们的 ZipFile 版本支持 Zip64: http ://commons.apache.org/compress/apidocs/org/apache/commons/compress/archivers/zip/ZipFile.html

于 2012-08-15T06:58:13.927 回答
0

在 3.x/4.x 中进行了很多更改以防止滥用 UI 线程。因此,您的应用程序可能会崩溃,因为您没有将昂贵的磁盘 I/O 操作卸载到单独的Thread.

于 2012-07-17T14:32:43.593 回答
0
    public class Compress { 

  private static final int BUFFER = 2048;  
  private String[] _files; 
  private String _zipFile;  
  public Compress(String[] files, String zipFile) { 
    _files = files; 
    _zipFile = zipFile; 
  }  
  public void zip() { 
    try  { 
      BufferedInputStream origin = null; 
      FileOutputStream dest = new FileOutputStream(_zipFile);  
      ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); 
      byte data[] = new byte[BUFFER]; 
      for(int i=0; i < _files.length; i++) { 
        Log.v("Compress", "Adding: " + _files[i]); 
        FileInputStream fi = new FileInputStream(_files[i]); 
        origin = new BufferedInputStream(fi, BUFFER); 
        ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1)); 
        out.putNextEntry(entry); 
        int count; 
        while ((count = origin.read(data, 0, BUFFER)) != -1) { 
          out.write(data, 0, count); 
        } 
        origin.close(); 
      } 

      out.close(); 
    } catch(Exception e) { 
      e.printStackTrace(); 
    } 

  } 

}

Call Compress like given below where you want to zip a file  :----

String zipFilePath = "fileName.zip";
File zipFile = new File(zipFilePath);
String[] files = new String[] {"/sdcard/fileName"};
if(!zipFile.exists()){
    new Compress(files, zipFilePath).zip();
   }
于 2012-07-16T10:40:09.157 回答