Java实现rar解压
jar包下载地址
https://mvnrepository.com/artifact/com.github.junrar/junrar
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<FileHeader> 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+ "%" ); } } }); |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 解决跨域问题的这6种方案,真香!
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· Windows 提权-UAC 绕过
2013-11-23 DB2与Sybase/Oracle/Informix的比较
2013-11-23 Spring3.1.2与Hibernate4.1.8整合
2013-11-23 c3p0、dbcp、proxool、BoneCP比较