Java实现对zip和rar文件的解压缩
通过java实现对zip和rar文件的解压缩
1 package com.svse.test; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.OutputStream; 8 import java.util.Enumeration; 9 10 import org.apache.tools.zip.ZipEntry; 11 import org.apache.tools.zip.ZipFile; 12 13 import de.innosystec.unrar.Archive; 14 import de.innosystec.unrar.rarfile.FileHeader; 15 /** 16 * zip和rar解压缩工具类 17 * @author lenovo 18 * 19 */ 20 public class ZipAndRarTools { 21 22 /** 23 * 解压rar 24 * @param sourceRarPath 需要解压的rar文件全路径 25 * @param destDirPath 需要解压到的文件目录 26 * @throws Exception 27 */ 28 public static void unrar(String sourceRarPath, String destDirPath) throws Exception { 29 File sourceRar=new File(sourceRarPath); 30 File destDir=new File(destDirPath); 31 Archive archive = null; 32 FileOutputStream fos = null; 33 System.out.println("Starting 开始解压..."); 34 try { 35 archive = new Archive(sourceRar); 36 FileHeader fh = archive.nextFileHeader(); 37 int count = 0; 38 File destFileName = null; 39 while (fh != null) { 40 System.out.println((++count) + ") " + fh.getFileNameString()); 41 String compressFileName = fh.getFileNameString().trim(); 42 destFileName = new File(destDir.getAbsolutePath() + "/" + compressFileName); 43 if (fh.isDirectory()) { 44 if (!destFileName.exists()) { 45 destFileName.mkdirs(); 46 } 47 fh = archive.nextFileHeader(); 48 continue; 49 } 50 if (!destFileName.getParentFile().exists()) { 51 destFileName.getParentFile().mkdirs(); 52 } 53 54 55 fos = new FileOutputStream(destFileName); 56 archive.extractFile(fh, fos); 57 fos.close(); 58 fos = null; 59 fh = archive.nextFileHeader(); 60 } 61 62 archive.close(); 63 archive = null; 64 System.out.println("Finished 解压完成!"); 65 } catch (Exception e) { 66 throw e; 67 } finally { 68 if (fos != null) { 69 try { 70 fos.close(); 71 fos = null; 72 } catch (Exception e) { 73 } 74 } 75 if (archive != null) { 76 try { 77 archive.close(); 78 archive = null; 79 } catch (Exception e) { 80 } 81 } 82 } 83 } 84 85 86 /** 87 * 解压Zip文件 88 * @param zipFileName 需要解压缩的文件位置 89 * @param descFileName 将文件解压到某个路径 90 * @throws IOException 91 */ 92 public static void unZip(String zipFileName,String descFileName) throws IOException{ 93 System.out.println("文件解压开始..."); 94 String descFileNames=descFileName; 95 if(!descFileNames.endsWith(File.separator)){ 96 descFileNames=descFileNames+File.separator; 97 } 98 try { 99 ZipFile zipFile=new ZipFile(zipFileName); 100 ZipEntry entry=null; 101 String entryName=null; 102 String descFileDir=null; 103 byte[] buf=new byte[4096]; 104 int readByte=0; 105 @SuppressWarnings("rawtypes") 106 Enumeration enums=zipFile.getEntries(); 107 while(enums.hasMoreElements()){ 108 entry =(ZipEntry) enums.nextElement(); 109 entryName=entry.getName(); 110 descFileDir=descFileNames+entryName; 111 if(entry.isDirectory()){ 112 new File(descFileDir).mkdir(); 113 continue; 114 }else{ 115 new File(descFileDir).getParentFile().mkdir(); 116 } 117 File file=new File(descFileDir); 118 OutputStream os=new FileOutputStream(file); 119 InputStream is=zipFile.getInputStream(entry); 120 while((readByte=is.read(buf))!=-1){ 121 os.write(buf, 0, readByte); 122 } 123 os.close(); 124 is.close(); 125 } 126 zipFile.close(); 127 System.out.println("文件解压成功!"); 128 } catch (Exception e) { 129 System.out.println("文件解压失败!"); 130 e.printStackTrace(); 131 } 132 133 } 134 135 public static void main(String[] args) throws Exception { 136 //ZipAndRarTools.unrar(newFile("D:\\存放资料的压缩包\\员工材料.rar"),newFile("D:\\存放资料的非压缩包\\")); 137 138 ZipAndRarTools.unZip("D:\\rarTest\\jar包和配置文件资源.zip", "D:\\rarTest"); 139 ZipAndRarTools.unrar("D:\\rarTest\\rar压缩包.rar", "D:\\rarTest"); 140 141 } 142 143 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!