Java实现对zip和rar文件的解压缩
通过java实现对zip和rar文件的解压缩
1 package com.svse.test; 2 import java.io.File; 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.OutputStream; 7 import java.util.Enumeration; 8 import org.apache.tools.zip.ZipEntry; 9 import org.apache.tools.zip.ZipFile; 10 import de.innosystec.unrar.Archive; 11 import de.innosystec.unrar.rarfile.FileHeader; 12 /** 13 * zip和rar解压缩工具类 14 * @author lenovo 15 * 16 */ 17 public class ZipAndRarTools { 18 /** 19 * 解压rar 20 * @param sourceRarPath 需要解压的rar文件全路径 21 * @param destDirPath 需要解压到的文件目录 22 * @throws Exception 23 */ 24 public static void unrar(String sourceRarPath, String destDirPath) throws Exception { 25 File sourceRar=new File(sourceRarPath); 26 File destDir=new File(destDirPath); 27 Archive archive = null; 28 FileOutputStream fos = null; 29 System.out.println("Starting 开始解压..."); 30 try { 31 archive = new Archive(sourceRar); 32 FileHeader fh = archive.nextFileHeader(); 33 int count = 0; 34 File destFileName = null; 35 while (fh != null) { 36 System.out.println((++count) + ") " + fh.getFileNameString()); 37 String compressFileName = fh.getFileNameString().trim(); 38 destFileName = new File(destDir.getAbsolutePath() + "/" + compressFileName); 39 if (fh.isDirectory()) { 40 if (!destFileName.exists()) { 41 destFileName.mkdirs(); 42 } 43 fh = archive.nextFileHeader(); 44 continue; 45 } 46 if (!destFileName.getParentFile().exists()) { 47 destFileName.getParentFile().mkdirs(); 48 } 49 50 fos = new FileOutputStream(destFileName); 51 archive.extractFile(fh, fos); 52 fos.close(); 53 fos = null; 54 fh = archive.nextFileHeader(); 55 } 56 57 archive.close(); 58 archive = null; 59 System.out.println("Finished 解压完成!"); 60 } catch (Exception e) { 61 throw e; 62 } finally { 63 if (fos != null) { 64 try { 65 fos.close(); 66 fos = null; 67 } catch (Exception e) { 68 } 69 } 70 if (archive != null) { 71 try { 72 archive.close(); 73 archive = null; 74 } catch (Exception e) { 75 } 76 } 77 } 78 } 79 80 81 /** 82 * 解压Zip文件 83 * @param zipFileName 需要解压缩的文件位置 84 * @param descFileName 将文件解压到某个路径 85 * @throws IOException 86 */ 87 public static void unZip(String zipFileName,String descFileName) throws IOException{ 88 System.out.println("文件解压开始..."); 89 String descFileNames=descFileName; 90 if(!descFileNames.endsWith(File.separator)){ 91 descFileNames=descFileNames+File.separator; 92 } 93 try { 94 ZipFile zipFile=new ZipFile(zipFileName); 95 ZipEntry entry=null; 96 String entryName=null; 97 String descFileDir=null; 98 byte[] buf=new byte[4096]; 99 int readByte=0; 100 @SuppressWarnings("rawtypes") 101 Enumeration enums=zipFile.getEntries(); 102 while(enums.hasMoreElements()){ 103 entry =(ZipEntry) enums.nextElement(); 104 entryName=entry.getName(); 105 descFileDir=descFileNames+entryName; 106 if(entry.isDirectory()){ 107 new File(descFileDir).mkdir(); 108 continue; 109 }else{ 110 new File(descFileDir).getParentFile().mkdir(); 111 } 112 File file=new File(descFileDir); 113 OutputStream os=new FileOutputStream(file); 114 InputStream is=zipFile.getInputStream(entry); 115 while((readByte=is.read(buf))!=-1){ 116 os.write(buf, 0, readByte); 117 } 118 os.close(); 119 is.close(); 120 } 121 zipFile.close(); 122 System.out.println("文件解压成功!"); 123 } catch (Exception e) { 124 System.out.println("文件解压失败!"); 125 e.printStackTrace(); 126 } 127 128 } 129 130 public static void main(String[] args) throws Exception { 131 //ZipAndRarTools.unrar(newFile("D:\\存放资料的压缩包\\员工材料.rar"),newFile("D:\\存放资料的非压缩包\\")); 132 133 ZipAndRarTools.unZip("D:\\rarTest\\jar包和配置文件资源.zip", "D:\\rarTest"); 134 ZipAndRarTools.unrar("D:\\rarTest\\rar压缩包.rar", "D:\\rarTest"); 135 136 } 137 }
坎坷困难会让你不断的强大起来 -- 前提是你别怂