转自在此感谢作者:https://blog.csdn.net/lbf5210/article/details/51206642

<!-- 解压rar -->
<dependency>
   <groupId>com.github.junrar</groupId>
   <artifactId>junrar</artifactId>
   <version>3.0.0</version>
</dependency>

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

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();
        }
    }
}

————————————————
版权声明:本文为CSDN博主「卡奇派对」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lbf5210/article/details/51206642

public static void main(String[] args) {String rarPath = "C:\\Users\\DELL\\Desktop\\sim\\1.rar";File rarFile = new File(rarPath);try {UnZipAnRar.unRar(rarFile, "C:\\Users\\DELL\\Desktop\\");} catch (Exception e) {e.printStackTrace();}
}

读取rar文件名称

import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;import java.io.*;
import java.util.Enumeration;public class UnZipAnRar {public static void unRar(File rarFile, String outDir) throws Exception {File outFileDir = new File(outDir);if (!outFileDir.exists()) {boolean isMakDir = outFileDir.mkdirs();if (isMakDir) {System.out.println("创建压缩目录成功");}}Archive archive = new Archive(new FileInputStream(rarFile));FileHeader fileHeader = archive.nextFileHeader();while (fileHeader != null) {String fileName = fileHeader.getFileNameW().isEmpty() ? fileHeader.getFileNameString() : fileHeader.getFileNameW();System.out.println(fileName);if (fileHeader.isDirectory()) {fileHeader = archive.nextFileHeader();continue;}fileHeader = archive.nextFileHeader();}archive.close();}
}

java解压/读取rar文件相关推荐

  1. java util zip.zipexc,JAVA解压zip压缩文件的实例

    今天在弄一个东西,需要在PL/SQL中解压zip的压缩包,刚开始的时候是想着直接在PLSQL中调用java,在java里面调用unzip的shell命令来解析压缩文件,但是比较悲剧,一直老是失败,在尝 ...

  2. ubuntu解压/压缩rar文件

    一般通过默认安装的ubuntu是不能解压rar文件的,只有在安装了rar解压工具之后,才可以解压.其实在ubuntu下安装rar解压工具是非常简单的,只需要两个步骤就可以迅速搞定. ubuntu 下r ...

  3. Java解压rar5压缩文件

    使用代码解压压缩文件,并指定解压后路径 导入依赖 <dependency><groupId>com.github.axet</groupId><artifac ...

  4. java解压在线tgz文件

    比如我们要解压这个url下 http://域名:端口号/文件夹/datai-cli-0.0.2.tgz,datai-cli-0.0.2.tgz的tgz文件里的某个文件,然后在上传,废话不多说了,上代码 ...

  5. java 解压zip文件

    public class UnZipUtils {public static synchronized boolean unzip(String zipFileName, String extPlac ...

  6. Java解压压缩加密文件zip

    前言 ❤Java学习路线个人总结-博客 ❤欢迎点赞

  7. Java解压上传zip或rar文件,并解压遍历文件中的html的路径

    1.本文只提供了一个功能的代码 public String addFreeMarker() throws Exception {HttpSession session = request.getSes ...

  8. java上传zip文件并解压读取

    最近遇到一个这样的需求:传一个压缩包给后台,后台保存后解压读取里面的文件,现学现做.在这里做个记录 文件上传 文件上传有很多方法,这里推荐一个自己感觉挺好用的一种,代码奉上: @PostMapping ...

  9. Java解压RAR文件的几种方式

    第一种: public class fileZipUtil {/*** zip文件解压* @param inputFile 待解压文件夹/文件* @param destDirPath 解压路径*/pu ...

最新文章

  1. 计算机书籍-医学图像数据可视化分析与处理
  2. oracle10g应用,2017企业级Oracle10g数据库管理与应用
  3. volatile的用法
  4. 7.13 T2 Shit 题(shit)
  5. RPC框架——简单高效hessian的使用方式
  6. RHCSA笔记整理(1)
  7. 运行 命令 linux,Linux基本命令运行
  8. SOT-143封装 ESD二极管
  9. 电脑摄像头一维条形码matlab识别
  10. 求$N^N$的首位数字
  11. Exadata的独门武器--卸载(Offloading)
  12. AIDE手机编程初级教程(零基础向) 引入篇
  13. matlab导出prn文件怎么打开,prn文件介绍及打印方法
  14. 一个简单的圆形图片实现
  15. 苹果手机刷机显示无法联系软件更新服务器,为什么苹果手机无法自动更新软件怎么办...
  16. maven私服上传jar包
  17. 2022-2028全球与中国体育在线直播视频流市场现状及未来发展趋势
  18. 1024,只有程序员才会翘首以盼的日子!
  19. CTF MISC图片隐写简单题学习思路总结(持续更新)
  20. 加速度传感器的原理和应用-手机翻转、失重检测、运动检测、位置识别

热门文章

  1. 试戴耳钉会感染艾滋病吗?
  2. 斯坦福教授告诉你:什么是元学习「 CS330 笔记 (三) 」
  3. 当前比较流行的页面布局方式
  4. 微信小程录制视频上传服务器,微信小程序-从相册获取图片,视频使用相机拍照,录像上传+服务器nodejs版接收-微信小程序视频上传功能-微信小程序视频上传...
  5. java微信头像失效问题,将微信头像上传至七牛云
  6. 2016年11月30日 angularJS input=file 绑定change事件
  7. Bootstrap抽样和Monte Carlo思想
  8. 图解设计模式- Mediator 模式
  9. USB HID Class 的具体应用
  10. MATLAB中:冒号用法