为什么ZipEntry无法在Jelly Bean中工作

时间:2013-10-29 10:39:12

标签: java android zip

我有一个140 MB的ZIP文件,包含大约4万个MP3文件。我使用以下代码在ZIP文件中播放某个文件而不解压缩它:

String fullPath = Environment.getExternalStorageDirectory().getPath() + "_audio_.mp3";
String path = Environment.getExternalStorageDirectory().getPath() + "mySoundFolder";

try {           
ZipFile zip = new ZipFile(path + "myFile.zip");                     
Enumeration zipEntries = zip.entries();                          
ZipEntry entry = zip.getEntry("myFile" + "/" + currentWord + ".mp3"); 

if (entry != null) {
    Log.i(MAIN_TAG, "entry found: " + entry.getName());   
    InputStream in = zip.getInputStream(entry);  

        File f = new File(fullPath);
        FileOutputStream out = new FileOutputStream(f);     
        IOUtils.copy(in, out);
        byte buffer[] = new byte[4096];
        int read;
        while ((read = in.read(buffer)) != -1)
        { 
        out.write(buffer, 0, read);     
        }                       
            if (f.exists())                         
                {
                    Log.i(MAIN_TAG,"Audio file found!");   
                    final MediaPlayer mp = new MediaPlayer();
                    mp.setDataSource(fullPath); 
                    mp.prepare();
                    mp.setOnBufferingUpdateListener(null);
                    mp.setLooping(false);                   
                    mp.setOnPreparedListener(new OnPreparedListener()
                    { public void onPrepared(MediaPlayer arg0) 
                        { 
                            mp.start();                         
                        Log.i(MAIN_TAG,"Pronunciation finished!");
                        }});                                            

                        }
                    else
                    {
                        Log.i(MAIN_TAG,"File doesn't exist!!"); 
                    }                       
                } 
                else {
                    // no such entry in the zip
                    Log.i(MAIN_TAG, "no such entry in the zip");                
                 }              
            } 
            catch (IOException e) {

                e.printStackTrace();
             Log.i(MAIN_TAG,"IOException reading zip file");                
            }           
        }   

此代码有两个奇怪的事情:

  1. 它在Android 2.2中完美无缺,但在Android 4.0.3失败。在2.2中,它会按照我的预期找到并播放MP3文件,但在4.0.3中,它一直说它无法在ZIP文件中找到该条目("no such entry in the zip")。
  2. 如果我将MP3文件的数量减少到大约100个文件,那么在Android 4.0.3中,它会找到并播放所选的MP3文件。
  3. 你能帮助我找出问题所在吗?

    提前多多感谢。

1 个答案:

答案 0 :(得分:0)

最后,我有解决此问题的方法。我将我的zip文件拆分为两个文件,每个文件包含大约20k entries。瞧,它再次像魅力一样。

我听说过Java读取zip文件中超过64k entries的条目的问题。我不知道为什么我的文件只有大约40k条目,但它也面临着问题。

相关问题