java提取rar压缩文档

来源:互联网 发布:淘宝抢红包入口 编辑:程序博客网 时间:2024/04/20 06:18

转自:http://hi.baidu.com/wqj403/blog/item/149957345cd6a11891ef3976.html

用java怎么提取或是解压rar压缩文档?以前查了很多的资料,没有找到相关的第三方库,网上查找的资料说是解析rar只能更加rar的命令行参数来解析。因为rar压缩文档的内部结构是没有共开的。所以没有专门的解析库程序。自己也就只好用命令行了,但是在实际应用中,遇到rar加密时就遇到了问题。

所以自己又在网上很费心的找了相关资料,终于找到了一个库可以解析rar文档。

库下载地址:http://www.mucommander.com/。这是个解决多种文档的软件,是用java写的。所以能够引用来解决rar的解析。

package DOCExtract;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import DocHandler.DocTypeNameParse;
import DocHandler.IDocHandler;

import com.mucommander.file.AbstractFile;
import com.mucommander.file.FileFactory;
import com.mucommander.file.impl.rar.provider.RarFile;
import com.mucommander.file.impl.rar.provider.de.innosystec.unrar.rarfile.FileHeader;
/**
* IDocHandler 为自己写的接口类
*
*/
public class RarExtractor implements IDocHandler {
    // 每次读取的字节大小
    private int BLOCKSIZE = 1024;
    // 临时文件编号
    private int FILE_COUNT = 0;

    public int getText(InputStream inputStream, StringBuffer strBuff) {
String fileDir = "f://wang" + FILE_COUNT;
FILE_COUNT++;
String fileName = null;
try { // 将文件写入磁盘上
     writeTodev(inputStream, fileDir);
     // 从磁盘上读取文件
     File file = new File(fileDir);
     String[] subFilePath = file.list();
     fileName = fileDir + "//" + subFilePath[0];
     AbstractFile abstractFile = FileFactory.getFile(fileName);
     RarFile rarFile = new RarFile(abstractFile);
     Collection collection = rarFile.getEntries();
     IDocHandler docHandler = null;  //自己定义的解析word,ppt,pdf等其他文件的接口
     for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
   FileHeader fileHeader = (FileHeader) iterator.next();
   String subFileName = fileHeader.getFileNameString();
   // 输出rar文档里的文档名
   System.out.println("subFileName:" + subFileName);

   InputStream subinputStream = rarFile.getEntryInputStream(subFileName);
   /**
   * DocTypeNameParse为文自己写的文档类型判断类。
   */
   String fileType = DocTypeNameParse.getTypeName(subFileName);
   // 输出文档的类型
   System.out.println("fileType:" + fileType);

   docHandler = (IDocHandler) this.ExctractMap.get(fileType);
   docHandler.getText(subinputStream, strBuff);
   subinputStream.close();//这里必须关闭流,否则在遇到有文档异常时,流就会卡主
     }
     // System.out.println("strBuff:" + strBuff);
} catch (IOException e) {
     e.printStackTrace();
}
// 删除临时文件
deleteFile(fileDir);
return 0;
    }

    // 将文件写入磁盘上
    private void writeTodev(InputStream inputStream, String fileDir) {
byte[] b = new byte[BLOCKSIZE];
int readCount = 0;
try {
     File file = new File(fileDir);
     if (!file.exists())
   file.mkdirs();
     File subFile = new File(fileDir, "tempfile.rar");
     FileOutputStream os = new FileOutputStream(subFile);
     while ((readCount = inputStream.read(b)) > 0) {
   os.write(b, 0, readCount);
     }
     os.close();
} catch (IOException e) {
     e.printStackTrace();
}
    }

    // 删除临时文件
    private void deleteFile(String fileName) {
File file = new File(fileName);
File[] subFile = file.listFiles();
for (int i = 0; i < subFile.length; i++) {
     subFile[i].delete();
}
file.delete();
    }
}

 

http://www.mucommander.com/ 可下载linux操作系统下的压缩包,然后解压从中找到mucommander.jar,在导入一个java工程里将没有问题,但是当我导入已有的web工程里却发现会报:org.apache.commons.logging.impl.Log4JLogger  cannot be cast to org.apache.commons.logging.impl.Jdk14Logger。

原来是该类的com.mucommander.file.impl.s3.S3ProtocolProvider.java类中代码:

 static {
        // Turn off Jets3t logging: failed (404) HEAD request on non-existing object are logged with a SEVERE level,
        // even though this is not an error per se. We don't want those to be reported in the log, so we have no
        // choice but to disable logging entirely.
        (Jdk14Logger) LogFactory.getLog(RestS3Service.class)).getLogger().setLevel(Level.OFF);
    }

而我的项目里是采用Log4JLogger.最后采用svn把其源代码全部check out,注释掉这部分代码,把新编译的该类的class文件替换原来的重新打上jar包。

原创粉丝点击