1 importorg.apache.commons.io.IOUtils;2
3 importorg.apache.tools.zip.ZipEntry;4 importorg.apache.tools.zip.ZipFile;5 importorg.apache.tools.zip.ZipOutputStream;6
7 import java.io.*;8 importjava.util.ArrayList;9 importjava.util.Enumeration;10 importjava.util.List;11
12 /**
13 * @Description: java操作zip文件工具类14 * @Version: 1.015 */
16 public classZipFileUtil {17
18 private static final String ENCODE_UTF_8 = "UTF-8";19
20 /**
21 *@paramzip 压缩的目的地址 例如:D://zipTest.zip22 *@paramsrcFiles 压缩的源文件23 * @description: 压缩文件或路径24 *@version: 1.025 */
26 public static void zipFile(String zip, ListsrcFiles) {27 try{28 //判断是否为压缩后的文件后缀是否为.zip结尾
29 if (zip.endsWith(".zip") || zip.endsWith(".ZIP")) {30 ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(newFile(zip)));31 zipOut.setEncoding(ENCODE_UTF_8);//设置编码
32 for(File file : srcFiles) {33 zipFile(zip, zipOut, file, "");34 }35 zipOut.close();36 } else{37 throw new RuntimeException("target file[zip] is not .zip type file");38 }39 } catch(FileNotFoundException e) {40 throw newRuntimeException(e.getMessage());41 } catch(IOException e) {42 throw newRuntimeException(e.getMessage());43 }44 }45
46 /**
47 *@paramzip 压缩的目的地址 例如:D://zipTest.zip48 *@paramzipOut49 *@paramsrcFile 被压缩的文件50 *@parampath 在zip中的相对路径51 *@version: 1.052 */
53 private static void zipFile(String zip, ZipOutputStream zipOut, File srcFile, String path) throwsIOException {54 System.out.println(" 开始压缩文件[" + srcFile.getName() + "]");55 if (!"".equals(path) && !path.endsWith(File.separator)) {56 path +=File.separator;57 }58 if (!srcFile.exists()) {//测试此抽象路径名定义的文件或目录是否存在
59 throw new RuntimeException("压缩失败,文件或目录 " + srcFile + " 不存在!");60 } else{61 if (!srcFile.getPath().equals(zip)) {62 if(srcFile.isDirectory()) {63 //listFiles能够获取当前文件夹下的所有文件和文件夹,如果文件夹A下还有文件D,那么D也在childs里。
64 File[] files =srcFile.listFiles();65 if (files.length == 0) {66 zipOut.putNextEntry(new ZipEntry(path + srcFile.getName() +File.separator));67 zipOut.closeEntry();68 } else{69 for(File file : files) {70 zipFile(zip, zipOut, file, path +srcFile.getName());71 }72 }73 } else{74 FileInputStream in = newFileInputStream(srcFile);75 zipOut.putNextEntry(new ZipEntry(path +srcFile.getName()));76 IOUtils.copy(in, zipOut);77 in.close();78 zipOut.closeEntry();79 }80 }81 }82 }83
84 /**
85 *@paramzipPath 待解压缩的ZIP文件路径86 *@paramdescDir 目标目录87 *@returnList88 * @description: 解压缩ZIP文件,将ZIP文件里的内容解压到targetDIR目录下89 *@version: 1.090 */
91 public static ListupzipFile(String zipPath, String descDir) {92 return upzipFile(newFile(zipPath), descDir);93 }94
95 /**
96 *@paramzipFile 解压缩文件97 *@paramdescDir 压缩的目标地址,如:D:\\测试 或 /mnt/d/测试98 * @description: 对.zip文件进行解压缩99 *@version: 1.0100 */
101 @SuppressWarnings({"rawtypes", "null"})102 public static ListupzipFile(File zipFile, String descDir) {103 List list = new ArrayList<>();104 //防止文件名中有中文时出错
105 System.setProperty("sun.zip.encoding", System.getProperty("sun.jnu.encoding"));106 try{107 if (!zipFile.exists()) {108 throw new RuntimeException("解压失败,文件 " + zipFile + " 不存在!");109 }110 ZipFile zFile = new ZipFile(zipFile, "GBK");111 InputStream in = null;112 OutputStream out = null;113 for (Enumeration entries =zFile.getEntries(); entries.hasMoreElements();) {114 ZipEntry entry =(ZipEntry) entries.nextElement();115 File file = new File(descDir + File.separator +entry.getName());116 if(entry.isDirectory()) {117 file.mkdirs();118 } else{119 File parent =file.getParentFile();120 if (!parent.exists()) {121 parent.mkdirs();122 }123 in =zFile.getInputStream(entry);124 out = newFileOutputStream(file);125 IOUtils.copy(in, out);126 out.flush();127 list.add(file);128 }129 }130 zFile.close();131 in.close();132 out.close();133 } catch(IOException e) {134 throw newRuntimeException(e.getMessage());135 }136 returnlist;137 }138
139 /**
140 * @description: 对临时生成的文件夹和文件夹下的文件进行删除141 *@param: delpath142 *@version: 1.0143 */
144 public static voiddeletefile(String delpath) {145 try{146 File file = newFile(delpath);147 if (!file.isDirectory()) {//判断是不是一个目录
148 file.delete();149 } else if(file.isDirectory()) {150 String[] filelist =file.list();151 for (int i = 0; i < filelist.length; i++) {152 File delfile = new File(delpath + File.separator +filelist[i]);153 if (!delfile.isDirectory()) {154 delfile.delete();155 } else if(delfile.isDirectory()) {156 deletefile(delpath + File.separator + filelist[i]);//递归删除
157 }158 }159 file.delete();160 }161 } catch(Exception e) {162 e.printStackTrace();163 }164 }165
166
167
168 /**
169 * @description: 测试方法170 *@version: 1.0171 */
172
173 public static voidmain(String[] args) {174 String zip = "D://zipTest.zip";175 List srcFiles = new ArrayList<>();176 srcFiles.add(new File("D://testZip"));177 srcFiles.add(new File("D://999"));178 zipFile(zip, srcFiles);179 List list = upzipFile("D://zipTest.zip", "D://Solin");180 for(File file : list) {181 System.out.println(file.getName());182 }183 //deletefile(zip);
184 }185 }