1 packagecom.jason.zip;2
3 importjava.io.File;4 importjava.io.FileInputStream;5 importjava.io.FileOutputStream;6 importjava.io.IOException;7 importjava.io.OutputStream;8 importjava.util.ArrayList;9 importjava.util.List;10 importjava.util.zip.ZipEntry;11 importjava.util.zip.ZipOutputStream;12 /**
13 * 压缩文件夹或批量压缩文件14 * @function15 *@author小风微凉16 * @time 2018-9-14 下午2:12:4517 */
18 public classZipAction {19 private static final int BUFFER_SIZE = 2 * 1024;20 /**
21 * 压缩成ZIP22 *@paramsrcDir 压缩文件夹路径23 *@paramout 压缩文件输出流24 *@paramKeepDirStructure 是否保留原来的目录结构,true:保留目录结构;25 * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)26 *@throwsRuntimeException 压缩失败会抛出运行时异常27 */
28 public static void toZip(String srcDir, OutputStream out, booleanKeepDirStructure)29 throwsRuntimeException{30 long start =System.currentTimeMillis();31 ZipOutputStream zos = null;32 try{33 zos = newZipOutputStream(out);34 File sourceFile = newFile(srcDir);35 compress(sourceFile,zos,sourceFile.getName(),KeepDirStructure);36 long end =System.currentTimeMillis();37 System.out.println("压缩完成,耗时:" + (end - start) +" ms");38 } catch(Exception e) {39 throw new RuntimeException("zip error from ZipAction",e);40 }finally{41 if(zos != null){42 try{43 zos.close();44 } catch(IOException e) {45 e.printStackTrace();46 }47 }48 }49 }50
51 /**
52 * 压缩成ZIP 方法253 *@paramsrcFiles 需要压缩的文件列表54 *@paramout 压缩文件输出流55 *@throwsRuntimeException 压缩失败会抛出运行时异常56 */
57 public static void toZip(List srcFiles , OutputStream out)throwsRuntimeException {58 long start =System.currentTimeMillis();59 ZipOutputStream zos = null;60 try{61 zos = newZipOutputStream(out);62 for(File srcFile : srcFiles) {63 byte[] buf = new byte[BUFFER_SIZE];64 zos.putNextEntry(newZipEntry(srcFile.getName()));65 intlen;66 FileInputStream in = newFileInputStream(srcFile);67 while ((len = in.read(buf)) != -1){68 zos.write(buf, 0, len);69 }70 zos.closeEntry();71 in.close();72 }73 long end =System.currentTimeMillis();74 System.out.println("压缩完成,耗时:" + (end - start) +" ms");75 } catch(Exception e) {76 throw new RuntimeException("zip error from ZipAction",e);77 }finally{78 if(zos != null){79 try{80 zos.close();81 } catch(IOException e) {82 e.printStackTrace();83 }84 }85 }86 }87 /**
88 * 递归压缩方法89 *@paramsourceFile 源文件90 *@paramzos zip输出流91 *@paramname 压缩后的名称92 *@paramKeepDirStructure 是否保留原来的目录结构,true:保留目录结构;93 * false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)94 *@throwsException95 */
96 private static voidcompress(File sourceFile, ZipOutputStream zos, String name,97 boolean KeepDirStructure) throwsException{98 byte[] buf = new byte[BUFFER_SIZE];99 if(sourceFile.isFile()){100 //向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
101 zos.putNextEntry(newZipEntry(name));102 //copy文件到zip输出流中
103 intlen;104 FileInputStream in = newFileInputStream(sourceFile);105 while ((len = in.read(buf)) != -1){106 zos.write(buf, 0, len);107 }108 //Complete the entry
109 zos.closeEntry();110 in.close();111 } else{112 File[] listFiles =sourceFile.listFiles();113 if(listFiles == null || listFiles.length == 0){114 //需要保留原来的文件结构时,需要对空文件夹进行处理
115 if(KeepDirStructure){116 //空文件夹的处理
117 zos.putNextEntry(new ZipEntry(name + "/"));118 //没有文件,不需要文件的copy
119 zos.closeEntry();120 }121 }else{122 for(File file : listFiles) {123 //判断是否需要保留原来的文件结构
124 if(KeepDirStructure) {125 //注意:file.getName()前面需要带上父文件夹的名字加一斜杠,126 //不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
127 compress(file, zos, name + "/" +file.getName(),KeepDirStructure);128 } else{129 compress(file, zos, file.getName(),KeepDirStructure);130 }131 }132 }133 }134 }135 public static void main(String[] args) throwsException {136 /**测试压缩方法1*/
137 FileOutputStream fos1 = new FileOutputStream(new File("C:/Users/Jason/Desktop/log/innermanage.zip"));138 ZipAction.toZip("C:/Users/Jason/Desktop/log", fos1,true);139 /**测试压缩方法2*/
140 List fileList = new ArrayList<>();141 fileList.add(new File("C:/Users/Jason/Desktop/log/innermanage/TestAction - 副本.java"));142 fileList.add(new File("C:/Users/Jason/Desktop/log/innermanage/TestAction.java"));143 FileOutputStream fos2 = new FileOutputStream(new File("C:/Users/Jason/Desktop/log/test.zip"));144 ZipAction.toZip(fileList, fos2);145 }146 }