2013-10-29 19 views
0

我有一個140 MB的ZIP文件,其中包含約4萬個MP3文件。我使用下面的代碼打ZIP文件中某些文件不解壓它:爲什麼ZipEntry無法在果凍豆中工作

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.2Android 4.0.3失敗。在2.2,它發現並播放MP3文件,但我認爲它在4.0.3,它一直說它無法找到ZIP文件中的條目("no such entry in the zip")。
  2. 如果我將MP3文件的數量減少到大約100個文件,則在Android 4.0.3中,它會查找並播放所選的MP3文件。

你們能幫我找出問題所在嗎?

非常感謝。

+0

此代碼在非android環境(例如:PC)上工作正常嗎?即'zip.getEntry(「myFile」+「/」+ currentWord +「.mp3」);'在PC上返回一個條目而不是null? – SrikanthLingala

+0

我不知道,因爲我沒有機會這樣測試。但請注意,此問題僅在「Android 4x」中出現,並且包含超過40k條目的zip文件。 –

回答

0

最後,我有一個解決這個問題的解決方法。我將我的zip文件分成兩個文件,每個文件包含大約20k entries。瞧,它再次像一個魅力。

我聽說過Java的問題,讀取zip文件中的條目超過64k entries。我不知道爲什麼我的文件只有大約40k條目,但它也面臨着這個問題。