2012-03-30 61 views
21

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

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

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

我没有线索在哪里看。

+0

您是否尝试阅读运行3.x/4.x的Asus或模拟器上的其他zip文件?这个zip文件只有问题吗? – 2012-05-24 09:11:40

+0

您是否吞咽任何异常?如果没有,getName()是否返回zipfile的名称? (简单的测试,但可能会出现令人惊讶的事情)。 如果您使用或不使用OPEN_READ标志打开zip,您会得到不同的结果吗? – 2012-07-03 16:41:44

+0

我不确定,你可以尝试用'JarFile'吗?... – 2012-07-10 13:52:04

回答

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(); 
    } 
0

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

2

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

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

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

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