2011-05-01 95 views
3

有沒有一種方法來解壓縮android1.6編程的rar文件?Android以編程方式解壓縮RAR文件

我已經嘗試過JUNRAR但有一些例外......

這裏是我的代碼,後成功打開RAR文件,使用junrar庫:

FileHeader fh=null; 
    while(true) 
    { 
    fh=rar.nextFileHeader(); 
    if(fh==null) return false;  
    if(fh.isEncrypted()) continue;  
    //check file 

    if(!fh.isDirectory() && fh.getFileNameString().toLowerCase().endsWith(".jpg")) 
    { 
    try 
    { 
     File f=new File(tmppath+covername);  //name of the destination file 

     OutputStream stream = new FileOutputStream(f);    
     rar.extractFile(fh, stream);   //call junrar  

     stream.close(); 
     return true; 
    } 
    catch (FileNotFoundException e1) 
    { 
     // TODO Auto-generated catch block 
     return false; 
    } 
    catch (RarException e) 
    { 
     // TODO Auto-generated catch block 
     return false; 
    } 
    catch (IOException e) 
    { 
     // TODO Auto-generated catch block 
     return false; 
    }  
    } 

而且DDMS透視圖顯示此異常.. 。?

ERROR/AndroidRuntime(2733): Uncaught handler: thread Thread-9 exiting due to uncaught exception 

ERROR/AndroidRuntime(2733): java.lang.VerifyError: de.innosystec.unrar.unpack.ppm.SubAllocator 

ERROR/AndroidRuntime(2733): at de.innosystec.unrar.unpack.ppm.ModelPPM.<init>(ModelPPM.java:73) 

ERROR/AndroidRuntime(2733): at de.innosystec.unrar.unpack.Unpack.<init>(Unpack.java:43) 

ERROR/AndroidRuntime(2733): at de.innosystec.unrar.Archive.doExtractFile(Archive.java:456) 

ERROR/AndroidRuntime(2733): at de.innosystec.unrar.Archive.extractFile(Archive.java:440) 

ERROR/AndroidRuntime(2733): at com.pmc.myRar.unrarCover(myRar.java:164) 

ERROR/AndroidRuntime(2733): at com.pmc.myDataBase.addRar(myDataBase.java:541) 

ERROR/AndroidRuntime(2733): at com.pmc.libraryActivity.addtoDB(libraryActivity.java:306) 

ERROR/AndroidRuntime(2733): at com.pmc.libraryActivity$2.run(libraryActivity.java:240) 

ERROR/AndroidRuntime(2733): at java.lang.Thread.run(Thread.java:1060) 

感謝, PMC

+0

使用'ADB logcat',DDMS,或在Eclipse中DDMS角度來考察logcat的,並期待在與相關的堆棧跟蹤你的例外。 Android中沒有內置RAR文件,因此您需要爲它找到一些第三方JAR。 – CommonsWare 2011-05-01 16:24:03

+0

感謝您的評論。我編輯了我的原始文章以包含代碼和異常。 – pmc 2011-05-01 19:39:54

回答

4

你有java.lang.Verify錯誤,這是相當難以查明。有沒有該庫的源代碼來自己重新編譯它?這可能是庫使用另一個版本的另一個jar編譯的。

至於解決方法:

有一個C庫的位置:有一個鏈接到JNI接口http://www.unrarlib.org/download.html

另一個(很容易),另一種方法是使用Runtime.exec()這個可執行文件:http://forum.xda-developers.com/showthread.php?t=1015814

+0

謝謝!我會嘗試你的建議。 – pmc 2011-05-01 20:48:10

0

如果你使用JUNRAR庫,當你解壓一個大的rar文件時,你會遇到「內存不足」的錯誤。

0

您可以使用src,更改archive.java的函數「doExtractFile」 添加代碼 dataIO.endpack(); unpack = null;

例如

私人無效doExtractFile(FileHeader裏高清,OutputStream的OS)

 throws RarException, IOException { 
    dataIO.init(os); 
    dataIO.init(hd); 
    dataIO.setUnpFileCRC(this.isOldFormat() ? 0 : 0xffFFffFF); 
    if (unpack == null) { 
     unpack = new Unpack(dataIO); 
    } 
    if (!hd.isSolid()) { 
     unpack.init(null); 
    } 
    unpack.setDestSize(hd.getFullUnpackSize()); 
    try { 
     unpack.doUnpack(hd.getUnpVersion(), hd.isSolid()); 
     // Verify file CRC 
     hd = dataIO.getSubHeader(); 
     long actualCRC = hd.isSplitAfter() ? ~dataIO.getPackedCRC() 
       : ~dataIO.getUnpFileCRC(); 
     int expectedCRC = hd.getFileCRC(); 
     if (actualCRC != expectedCRC) { 
      throw new RarException(RarExceptionType.crcError); 
      // System.out.println(hd.isEncrypted()); 
     } 

     dataIO.endpack();//add yzd 
     unpack=null;// add yzd 

    } catch (Exception e) { 
     unpack.cleanUp(); 
     if (e instanceof RarException) { 
      // throw new RarException((RarException)e); 
      throw (RarException) e; 
     } else { 
      throw new RarException(e); 
     } 
    } 
} 
相關問題