开发者

Android decompress RAR files programmatically

开发者 https://www.devze.com 2023-03-02 16:15 出处:网络
Is there a way to decompress rar files in android 1.6 programmatically? I\'ve already tried JUNRAR but got some exceptions...

Is there a way to decompress rar files in android 1.6 programmatically?

I've already tried JUNRAR but got some exceptions...

Here's my code, after successfully opening rar file, using junrar library:

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;
     }    
   }

And DDMS perspective show this exception...?

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

ERROR/AndroidRuntime(2733): 开发者_Python百科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)

Thanks, pmc


You got a java.lang.Verify error, which are quite difficult to pinpoint. Is there a source code of that library to recompile it yourself? It may be that the library was compiled using a different version of another jar.

As workarounds:

There's a C library here: http://www.unrarlib.org/download.html that has a link to a JNI interface

Another (very easy) alternative is to use Runtime.exec() with this executable: http://forum.xda-developers.com/showthread.php?t=1015814


If you use JUNRAR library, you will meet "Out of memory" bug when you decompress a large rar file.


you can use src, change the function "doExtractFile" of archive.java add code dataIO.endpack(); unpack=null;

example

private void doExtractFile(FileHeader hd, 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);
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消