1、实现说明:

在下面引入的包中可以看出:

1)压缩、解压zip是用到的apache 的zip包,需要在引入jar包(jar-ant.rar);

2)压缩、解压rar需要用到的是junrar,这里需要引入第三方jar包,我用到的是junrar-0.7.jar

下面的两个方法是实现解压文件的实例,压缩文件的实现其实也很简单,大家可以查查实现方式,在此我还是不关心压缩文件(相对来说用的少)


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;/*** * @author liuBf * 类说明:解压文件公用类 **/
public class UnZipOrRarUtils {/*** 这里用到了synchronized ,也就是防止出现并发问题 ***/public static synchronized void untar(String tarFileName, String extPlace)throws Exception {unRarFile(tarFileName, extPlace);}public static synchronized void unzip(String zipFileName, String extPlace)throws Exception {unZipFiles(zipFileName, extPlace);}/*** 解压zip格式的压缩文件到指定位置* * @param zipFileName*            压缩文件* @param extPlace*            解压目录* @throws Exception*/@SuppressWarnings("unchecked")public static boolean unZipFiles(String zipFileName, String extPlace)throws Exception {System.setProperty("sun.zip.encoding",System.getProperty("sun.jnu.encoding"));try {(new File(extPlace)).mkdirs();File f = new File(zipFileName);ZipFile zipFile = new ZipFile(zipFileName, "GBK"); // 处理中文文件名乱码的问题if ((!f.exists()) && (f.length() <= 0)) {throw new Exception("要解压的文件不存在!");}String strPath, gbkPath, strtemp;File tempFile = new File(extPlace);strPath = tempFile.getAbsolutePath();Enumeration<?> e = zipFile.getEntries();while (e.hasMoreElements()) {ZipEntry zipEnt = (ZipEntry) e.nextElement();gbkPath = zipEnt.getName();if (zipEnt.isDirectory()) {strtemp = strPath + File.separator + gbkPath;File dir = new File(strtemp);dir.mkdirs();continue;} else { // 读写文件InputStream is = zipFile.getInputStream(zipEnt);BufferedInputStream bis = new BufferedInputStream(is);gbkPath = zipEnt.getName();strtemp = strPath + File.separator + gbkPath;// 建目录String strsubdir = gbkPath;for (int i = 0; i < strsubdir.length(); i++) {if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {String temp = strPath + File.separator+ strsubdir.substring(0, i);File subdir = new File(temp);if (!subdir.exists())subdir.mkdir();}}FileOutputStream fos = new FileOutputStream(strtemp);BufferedOutputStream bos = new BufferedOutputStream(fos);int c;while ((c = bis.read()) != -1) {bos.write((byte) c);}bos.close();fos.close();}}return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 根据原始rar路径,解压到指定文件夹下.* * @param srcRarPath*            原始rar路径* @param dstDirectoryPath*            解压到的文件夹*/public static void unRarFile(String srcRarPath, String dstDirectoryPath) {if (!srcRarPath.toLowerCase().endsWith(".rar")) {System.out.println("非rar文件!");return;}File dstDiretory = new File(dstDirectoryPath);if (!dstDiretory.exists()) {// 目标目录不存在时,创建该文件夹dstDiretory.mkdirs();}Archive a = null;try {a = new Archive(new File(srcRarPath));if (a != null) {// a.getMainHeader().print(); // 打印文件信息.FileHeader fh = a.nextFileHeader();while (fh != null) {// 防止文件名中文乱码问题的处理String fileName = fh.getFileNameW().isEmpty() ? fh.getFileNameString() : fh.getFileNameW();if (fh.isDirectory()) { // 文件夹File fol = new File(dstDirectoryPath + File.separator+ fileName);fol.mkdirs();} else { // 文件File out = new File(dstDirectoryPath + File.separator+ fileName.trim());try {if (!out.exists()) {if (!out.getParentFile().exists()) {// 相对路径可能多级,可能需要创建父目录.out.getParentFile().mkdirs();}out.createNewFile();}FileOutputStream os = new FileOutputStream(out);a.extractFile(fh, os);os.close();} catch (Exception ex) {ex.printStackTrace();}}fh = a.nextFileHeader();}a.close();}} catch (Exception e) {e.printStackTrace();}}
}

java实现解压(zip和rar文件)实例-解决中文乱码问题相关推荐

  1. php 判断是rar或是zip文件,PHP解压zip和rar文件的方法介绍

    PHP解压zip和rar文件的方法介绍?实例代码举例php语言如何解压文件.php开发经常会遇到解压zip和rar文件,以下方法供大家参考,判断文件是zip类型的还是rar类型,调用相关组件执行解压缩 ...

  2. Linux 下载安装 rar 并解压rar压缩包(Linux下如何解压.zip和.rar文件)

    Linux下如何解压.zip和.rar文件,对于Window下的常见压缩文件.zip和.rar,Linux也有相应的方法来解压它们: 1)对于zip linux下提供了zip和unzip程序,zip是 ...

  3. 使用Python解压zip、rar文件

    解压 zip 文件 基本解压操作 import zipfile''' 基本格式:zipfile.ZipFile(filename[,mode[,compression[,allowZip64]]]) ...

  4. Ubuntu 14 如何解压 .zip、.rar 文件

    .zip 和 .rar 是Windows下常用的压缩文件,在Ubuntu中如何解压. [解压.zip文件] Ubuntu中貌似已经安装了unzip软件,解压命令如下: unzip ./FileName ...

  5. python 解压文件 已存在_python解压zip和rar文件问题以及解决方法

    写此文的目的是记录自己的学习过程,一些经过,遇到问题以及解决问题的方法,存放在这里,方便自己以后复习以及自己的一些总结,如果此文对你有帮助,请随时联系我微信:wangzhixiong1533 ◆ ◆ ...

  6. java如何解压rar文件怎么打开_如何在java中实现对zip和rar文件的解压

    如何在java中实现对zip和rar文件的解压 关注:101  答案:1  mip版 解决时间 2021-01-26 10:50 提问者芣①樣哋羙莮 2021-01-25 22:44 如何在java中 ...

  7. java解压zip与rar

    这两天,因为项目需要,涉及到文件夹的上传,想了很久,在网上也找了一些资料,都没有什么很好的办法,都是用插件之类的解决,后面就想到了上传压缩 文件,说到压缩文件,自然就会遇到,文件的解压缩问题,大家都知 ...

  8. linux 压缩7z文件夹,Linux 下压缩与解压.zip和.rar及.7z文件

    Linux 下压缩与解压.zip和.rar及.7z文件对于Window下的常见压缩文件.zip和.rar,Linux也有相应的方法来解压它们: 1)对于.zip linux下提供了zip和unzip程 ...

  9. java读取.properties文件及解决中文乱码问题

    java读取.properties文件及解决中文乱码问题 参考文章: (1)java读取.properties文件及解决中文乱码问题 (2)https://www.cnblogs.com/helloq ...

最新文章

  1. springDataJpa实现普通模糊查询
  2. springboot源码 红色J_通达信副图指标源码之,出手就赢
  3. html去除边角,WEB开发向HTML5及CSS3迈进(1)——圆框边角的处理
  4. Python--练习及面试题
  5. 2011.03.16中国食盐价格不上涨,日本不缺少食盐。
  6. The 2016 ACM-ICPC Asia China-Final Contest Promblem D
  7. Android 内容提供器---简介
  8. 苹果电脑如何修改环境变量
  9. MySQL的基本语法(授权与备份)
  10. Beego 使用笔记
  11. 实现Modbus Rtu 服务端 通过RS485转4G方式 实现远程采集传输串口设备数据
  12. PR视频转场预设 10个快节奏极限运动空间扭曲效果PR转场过渡预设
  13. Taro 3 正式版发布!京东推出开放式跨端跨框架方案,这些React、Nerv、Vue、jQuey都能支持上了!...
  14. 腾讯云 8核 16G 18M 的轻量应用服务器评测
  15. 合成大西瓜?不如写个可以一起聊天吃瓜放烟花的聊天室
  16. 浅入浅出数据分析之Hello NumPy系列(四)
  17. selenium.common.exceptions.SessionNotCreatedException浏览器版本不匹配报错
  18. 【算法】一文详解贪心法
  19. python实践意义_在Python学习中过程比结果更有意义
  20. Listview 的自绘

热门文章

  1. php侧边栏导航效果,原生js实现电商侧边导航效果
  2. VRoid Studio 3d卡通建模
  3. 穿越雷区(Python)
  4. 万邦淘宝代购系统;海外代购系统;代购程序,代购系统源码PHP前端源码
  5. 为什么计算机有信息记忆功能,存储器是计算机实现记忆功能的一个重要组成部分...
  6. js回调执行顺序,同步任务与异步任务,宏任务与微任务
  7. 如何在word中插入比较好看的代码
  8. python xlwings包添加或修改excel的页眉
  9. CSS3 变形 过渡 动画
  10. Nginx配置使用详解