java实现压缩包的解压,支持rar/zip格式

您所在的位置:网站首页 java解析java文件 java实现压缩包的解压,支持rar/zip格式

java实现压缩包的解压,支持rar/zip格式

2023-10-25 13:42| 来源: 网络整理| 查看: 265

最近需要实现压缩包的解压功能,参考了网上大佬们分享,自己进行一次整理,编写了如下一个完整的工具类,支持rar格式与zip格式压缩包的解压,有需要的朋友,可以直接用

需要引入jar包

com.github.junrar junrar 4.0.0 org.apache.poi poi 3.17

解压工具类

public class fileZipUtil { /** * zip文件解压 * @param inputFile 待解压文件夹/文件 * @param destDirPath 解压路径 */ public static void unZipFiles(String inputFile,String destDirPath) throws Exception { File srcFile = new File(inputFile);//获取当前压缩文件 // 判断源文件是否存在 if (!srcFile.exists()) { throw new Exception(srcFile.getPath() + "所指文件不存在"); } ZipFile zipFile = new ZipFile(srcFile, Charset.forName("GBK"));//创建压缩文件对象 //开始解压 Enumeration entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); // 如果是文件夹,就创建个文件夹 if (entry.isDirectory()) { String dirPath = destDirPath + "/" + entry.getName(); srcFile.mkdirs(); } else { // 如果是文件,就先创建一个文件,然后用io流把内容copy过去 File targetFile = new File(destDirPath + "/" + 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(); } } } /** * 解压RAR压缩文件到指定路径 * @param rarFile RAR压缩文件 * @param dstDir 解压到的文件夹路径 */ public static void unRarFile(String rarPath, String dstDir) throws Exception { File dstDiretory = new File(dstDir); if (!dstDiretory.exists()) { dstDiretory.mkdirs(); } try { File rarFile= new File(rarPath); Archive archive = new Archive(new FileInputStream(rarFile)); List fileHeaders = archive.getFileHeaders(); for (FileHeader fileHeader : fileHeaders) { if (fileHeader.isDirectory()) { String fileName= fileHeader.getFileNameW(); if(!existZH(fileName)){ fileName = fileHeader.getFileNameString(); } File dir = new File(dstDir + File.separator + fileName); if (!dir.exists()){ dir.mkdirs(); } } else { String fileName= fileHeader.getFileNameW().trim(); if(!existZH(fileName)){ fileName = fileHeader.getFileNameString().trim(); } File file = new File(dstDir + File.separator + fileName); try { if (!file.exists()) { if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } file.createNewFile(); } FileOutputStream os = new FileOutputStream(file); archive.extractFile(fileHeader, os); os.close(); } catch (Exception ex) { throw ex; } } } archive.close(); } catch (Exception e) { throw e; } } public static boolean existZH(String str) { String regEx = "[\\u4e00-\\u9fa5]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); while (m.find()) { return true; } return false; } //使用main方法进行测试 public static void main(String[] args) { try { String filepath = "E:\\test\\测试1.rar"; String newpath="E:\\test\\zipTest"; //获取最后一个.的位置 int lastIndexOf = filepath.lastIndexOf("."); //获取文件的后缀名 .jpg String suffix = filepath.substring(lastIndexOf); System.out.println(suffix); if(suffix.equals(".zip")){ unZipFiles(filepath,newpath); }else if(suffix.equals(".rar")){ unRarFile(filepath,newpath); } } catch (Exception e) { e.printStackTrace(); } } }


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3