java解压/读取rar文件 |
您所在的位置:网站首页 › jar解压到指定目录的文件夹里 › java解压/读取rar文件 |
转自在此感谢作者:https://blog.csdn.net/lbf5210/article/details/51206642
com.github.junrar junrar 3.0.0 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() 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(); } } |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |