java rar_Java实现rar解压

这个Java代码示例展示了如何使用UnRarUtils类来解压RAR文件,并在解压过程中调用回调方法更新进度。同时,它还实现了计算解压后单个文件的MD5值。整个过程包括了文件的读取、写入、异常处理以及MD5的计算逻辑。
摘要由CSDN通过智能技术生成

jar包下载地址

UnRarUtils.java

import com.github.junrar.Archive;

import com.github.junrar.UnrarCallback;

import com.github.junrar.exception.RarException;

import com.github.junrar.rarfile.FileHeader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.math.BigInteger;

import java.security.MessageDigest;

import java.util.List;

public class UnRarUtils {

/**

* @param rarFileName rar file name

* @param outFilePath output file path

* @param callback callback

* @author shijian

* @throws Exception

*/

public static void unrar(String rarFileName, String outFilePath, UnrarCallback callback) throws Exception {

Archive archive = new Archive(new File(rarFileName), callback);

if(archive == null){

throw new FileNotFoundException(rarFileName + " NOT FOUND!");

}

if(archive.isEncrypted()){

throw new Exception(rarFileName + " IS ENCRYPTED!");

}

List files = archive.getFileHeaders();

for (FileHeader fh : files) {

if(fh.isEncrypted()){

throw new Exception(rarFileName + " IS ENCRYPTED!");

}

String fileName = fh.getFileNameString();

if(fileName != null && fileName.trim().length() > 0){

String saveFileName = outFilePath+ File.separator+fileName;

File saveFile = new File(saveFileName);

File parent = saveFile.getParentFile();

if(!parent.exists()){

parent.mkdirs();

}

if(!saveFile.exists()){

saveFile.createNewFile();

}

FileOutputStream fos = new FileOutputStream(saveFile);

try {

archive.extractFile(fh, fos);

} catch (RarException e) {

throw e;

}finally{

try{

fos.flush();

fos.close();

}catch (Exception e){

}

}

}

}

}

/**

* 获取单个文件的MD5值!

* @param file

* @return

*/

public static String getFileMD5(File file) {

if (!file.isFile()) {

return null;

}

MessageDigest digest = null;

FileInputStream in = null;

byte buffer[] = new byte[1024];

int len;

try {

digest = MessageDigest.getInstance("MD5");

in = new FileInputStream(file);

while ((len = in.read(buffer, 0, 1024)) != -1) {

digest.update(buffer, 0, len);

}

in.close();

} catch (Exception e) {

e.printStackTrace();

return null;

}

BigInteger bigInt = new BigInteger(1, digest.digest());

return bigInt.toString(16);

}

}

使用

UnRarUtils.unrar(rarFile.getAbsolutePath(), sdDir.getAbsolutePath(), new UnrarCallback() {

int currentProgress = -1;

@Override

public boolean isNextVolumeReady(Volume volume) {

return true;

}

@Override

public void volumeProgressChanged(long l, long l1) {

int progress = (int)((double)l/l1*100);

if(currentProgress != progress){

currentProgress = progress;

LogUtils.addLog(context,TAG,"Unrar "+rarFile.getName()+" rate: "+progress+"%");

}

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值