业务需求:通过Java程序,不使用命令解压zip和rar压缩文件(7z可以解压rar)

读取异常处理

/*** 系统异常类* @author sgq*/
public class ReadZipException extends Exception {private static final long serialVersionUID = 1L;public ReadZipException(String message) {super(message);}public ReadZipException(String message, Throwable ex) {super(message, ex);}public ReadZipException(Throwable ex) {super(ex);}
}

所需依赖

<!-- https://mvnrepository.com/artifact/ant/ant --> ===》可以获取最新依赖<!--zip--><dependency><groupId>ant</groupId><artifactId>ant</artifactId><version>1.6.5</version></dependency><!-- https://mvnrepository.com/artifact/com.github.junrar/junrar --> ===》可以获取最新依赖<!-- 解压rar --><!--<dependency><groupId>com.github.junrar</groupId><artifactId>junrar</artifactId><version>4.0.0</version></dependency>--><dependency><groupId>com.github.axet</groupId><artifactId>java-unrar</artifactId><version>1.7.0-8</version></dependency><dependency><groupId>com.github.junrar</groupId><artifactId>junrar</artifactId><version>0.7</version></dependency><!--7z rar使用7z依赖--><dependency><groupId>net.sf.sevenzipjbinding</groupId><artifactId>sevenzipjbinding</artifactId><version>16.02-2.01</version></dependency><--rar必须引入,否则会出现空指针--><dependency><groupId>net.sf.sevenzipjbinding</groupId><artifactId>sevenzipjbinding-all-platforms</artifactId><version>16.02-2.01</version></dependency><--io流--><dependency><artifactId>commons-io</artifactId><groupId>commons-io</groupId><version>2.0.1</version></dependency>

解压zip

    /*** zip解压* @throws RuntimeException 解压失败会抛出运行时异常* path 压缩文件路径* destDirPath 文件存放路径*/public static void unZipFiles(String path, String destDirPath) throws Exception {File srcFile = new File(path);long start = System.currentTimeMillis();// 判断源文件是否存在if (!srcFile.exists()) {throw new RuntimeException(srcFile.getPath() + "所指文件不存在");}//获取压缩包文件名--解压后的目标文件夹//destDirPath = destDirPath+name;// 开始解压ZipFile zipFile = null;File targetFile = null;try {zipFile = new ZipFile(srcFile, Charset.forName("GBK"));Enumeration<?> entries = zipFile.entries();while (entries.hasMoreElements()) {ZipEntry entry = (ZipEntry) entries.nextElement();System.out.println("解压" + entry.getName());// 如果是文件夹,就创建个文件夹if (entry.isDirectory()) {String dirPath = destDirPath + File.separator + entry.getName();File dir = new File(dirPath);dir.mkdirs();} else {// 如果是文件,就先创建一个文件,然后用io流把内容copy过去targetFile = new File(destDirPath + File.separator + entry.getName());// 保证这个文件的父文件夹必须要存在if (!targetFile.getParentFile().exists()) {targetFile.getParentFile().mkdirs();}targetFile.createNewFile();// 将压缩文件内容写入到这个文件中InputStream is = zipFile.getInputStream(entry);FileOutputStream fos = new FileOutputStream(targetFile);int len;byte[] buf = new byte[1024];while ((len = is.read(buf)) != -1) {fos.write(buf, 0, len);}// 关流顺序,先打开的后关闭fos.close();is.close();}}long end = System.currentTimeMillis();System.out.println("解压完成,耗时:" + (end - start) + " ms");} catch (Exception e) {throw new RuntimeException("unzip error from ZipUtils", e);} finally {if (zipFile != null) {try {zipFile.close();} catch (IOException e) {e.printStackTrace();}}}}

解压

API文档:点击(点击类可以查看文档)
参考文章:点击

    /*** 解压rar格式* @param filePath  压缩文件路径* @param destDirPath 保存路径* @throws Exception*/public static void unrar(String filePath, String destDirPath) throws Exception {File file =new File(filePath);if (!file.exists()) {throw new RuntimeException(file.getPath() + "所指文件不存在");}// 可以指定解压的位置//int pos = file.getName().lastIndexOf(".");//从后查询后缀//解压位置// String descDir = destDirPath + File.separator+ file.getName().substring(0, pos)+File.separator; //截取到后缀,作为文件夹//开始解压rarReadZipFileUtils.unRarFile(file.getAbsolutePath(), destDirPath);}

调用下面方法:缺点只可以为rar压缩文件,zip压缩文件无法打开

    //解压rar所用到的方法public static void unRarFile(String srcRarPath, String dstDirectoryPath) {RandomAccessFile randomAccessFile = null;IInArchive inArchive = null;File file = new File(dstDirectoryPath);if (!file.exists()) {file.mkdirs();}try {randomAccessFile = new RandomAccessFile(new File(srcRarPath), "r");inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile));ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();//创建文件大小为0的数据for (int idx = 0; idx < inArchive.getNumberOfItems(); idx++) {String  property = (String)inArchive.getProperty(idx, PropID.PATH);//文件名称boolean b1 = property.toLowerCase().endsWith(".rar");if (!b1) { //先创建非压缩包内容File tarFile = new File(file + File.separator + property);if (!tarFile.isDirectory()){if (!tarFile.getParentFile().exists()) { //判断父级tarFile.getParentFile().mkdirs();}if (!tarFile.exists()) {if (!(boolean) ((property.contains(".")))) { //判断最后一级为文件夹tarFile.mkdir();}else {tarFile.createNewFile();}}}else {tarFile.mkdir();}}}for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {File tarFile = new File(file + File.separator + item.getPath());//防止包含空文件夹item.extractSlow(new ISequentialOutStream() { //开始解压public int write(byte[] data) throws SevenZipException {FileOutputStream fos = null;try {if (!tarFile.getParentFile().exists()) { //判断父级tarFile.getParentFile().mkdirs();}tarFile.createNewFile();fos = new FileOutputStream(tarFile.getAbsolutePath());fos.write(data);fos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return data.length;}});}} catch (Exception e) {e.printStackTrace();} finally {if (inArchive != null) {try {inArchive.close();} catch (SevenZipException e) {e.printStackTrace();}}if (randomAccessFile != null) {try {randomAccessFile.close();} catch (IOException e) {e.printStackTrace();}}}}

(工具包)之zip与rar解压相关推荐

  1. zip、rar解压文件

    1.添加zip.rar依赖 <!-- 解压rar --> <dependency><groupId>com.github.junrar</groupId> ...

  2. rpm, tar, gz, bz, bz2, rar, zip, lha, deb, 解压

    .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) --------------- .gz 解压 ...

  3. 7z001怎么解压在安卓手机上面_安卓zip文件压缩RAR解压手机下载-安卓zip文件压缩RAR解压v1.0最新版下载...

    安卓zip文件压缩RAR解压是一款非常好用的手机压缩解压缩神器,在安卓zip文件压缩RAR解压上我们可以看到很多的实用的功能,软件可以帮助我们更好的处理我们手机中的文件,感兴趣的朋友赶紧下载安卓zip ...

  4. linux,rpm, tar, gz, bz, bz2, rar, zip, lha, deb, 解压

    linux下解压命令大全 .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) --------- ...

  5. 压缩包下载后php文件怎么打开,用户下载的压缩包rar格式或zip文件如何解压 解压后就可以安装或运行里面的文件了...

    狸窝网盘中分享有很多解决方案中使用到的软件资源,下载到电脑后是一个软件的文件压缩包,有的用户下载后不知道如何解压或说解压不了,怎么办?由于狸窝所面向用户比较大众化,为方便不同年龄层次和新手的使用,这里 ...

  6. c# rar解压大小_C#解压缩Zip,Rar等压缩文件(详细说明)

    其实这个东西网上已经有很多了 给出了一大把  当然我也是在网上找到得 只不过 说明不够详细 经过测试 给出详细的备注: 解压的给的很详细  压缩的基本也一样 只不过参数信息不一样罢了: 利用winra ...

  7. cordova 安卓文件多选_安卓zip文件压缩RAR解压软件下载-安卓zip文件压缩RAR解压下载v3.0.4安卓版...

    安卓zip文件压缩RAR解压是一款非常好用的手机压缩解压缩神器,在安卓zip文件压缩RAR解压上我们可以看到很多的实用的功能,软件可以帮助我们更好的处理我们手机中的文件,感兴趣的朋友赶紧下载安卓zip ...

  8. linux rar解压_linux命令

    文件搜索 打包和压缩文件 Linux下的多用户多任务操作系统 查看文件内容 挂载一个文件系统 磁盘空间 用户和群组 文件的权限 文件的特殊属性 RPM 包 YUM 软件包升级器 DEB 包 安装软件 ...

  9. linux rar命令没找到,Linux没有rar解压命令

    使用微软系统的同学都知道rar解压缩软件,它是一个商业的收费软件,那在免费开源的linux系统怎么解压后缀为rar的文件呢,我相信很多同学都是先把rar的文件下载到电脑本地,然后再用7-zip或者wi ...

最新文章

  1. vscode 新建php模板,使用VSCode快速创建vue文件模版的方法介绍
  2. ODAC(V9.5.15) 学习笔记(十七)主从模式
  3. 带宽测量:带宽测量工具下载
  4. react实战课程_在使用React一年后,我学到的最重要的课程
  5. 【转载】DNN6开源CMS
  6. 图解java多线程设计模式 pdf_图解Java设计模式之状态模式
  7. 第十二章_网络搭建及训练
  8. c语言 队列方法的编写
  9. 计算机主机包括哪两大部分,电脑硬件分为哪几个部分
  10. python 解析pys文件,并将其写入txt和excel文件
  11. 俩人同样写代码,我拿七千他咋五万八...
  12. win10 linux分区大小调整大小,如何在windows系统中调整分区大小(包括windows10/8/7)...
  13. flt 转word 图片不全(已解决)
  14. icc校色文件使用教程_Windows7色彩管理显示器ICC设置方法
  15. Deepin java开发环境的搭建jdk8,tomcat9,maven3.3.9,nodejs,vue3+
  16. C++ 检索 IP地址
  17. vmware虚机安装vmtool
  18. 【算法】震惊!!!史上最详细的卡特兰数浅谈!!!
  19. Jenkins整合dingding json pusher,发送自定义消息到钉钉群
  20. 如何将ASCII码值转换为字符

热门文章

  1. 【Python爬虫项目】全民K歌
  2. 使用anaconda安装pytorch——看这一篇就行了
  3. 抖音斜体加粗昵称生成网站html源码
  4. 兼容火狐浏览器的select下拉框样式
  5. iOS 启动图标尺寸大全
  6. html600像素宽如何设置,如何设置图片打印尺寸,长与宽指定大小(CM)?
  7. MATLAB关于polt3d函数的问题求解
  8. JS、HTML人民币转大写,金额转大写
  9. python3爬取视频代码_Python爬虫视频以及使用python3爬取的实例
  10. mysql整段注释_mysql备注(注释)代码怎么打?