因为项目要用到压缩、解压缩zip格式压缩包,只好自己封装一个。
废话少说,直接上代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
package com.mxzhe.demo.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.apache.log4j.Logger; /** * @ClassName: ZipCompressor * @CreateTime Apr 28, 2013 1:12:16 PM * @author : Mayi * @Description: 压缩文件的通用工具类-采用java.util.zip实现,较复杂。 * */ public class ZipCompressor { private Logger logger = Logger.getLogger(ZipCompressor.class); static final int BUFFER = 8192; private File zipFile; /** * 压缩文件构造函数 * @param pathName 压缩的文件存放目录 */ public ZipCompressor(String pathName) { zipFile = new File(pathName); } /** 一次性压缩多个文件,文件存放至一个文件夹中 */ public static void ZipMultiFile(String filepath, String zippath) { try { File file = new File(filepath);// 要被压缩的文件夹 File zipFile = new File(zippath); InputStream input = null; ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); if (file.isDirectory()) { File[] files = file.listFiles(); for (int i = 0; i < files.length; ++i) { input = new FileInputStream(files[i]); zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName())); int temp = 0; while ((temp = input.read()) != -1) { zipOut.write(temp); } input.close(); } } zipOut.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 执行压缩操作 * @param srcPathName 被压缩的文件/文件夹 */ public void compressExe(String srcPathName) { File file = new File(srcPathName); if (!file.exists()){ throw new RuntimeException(srcPathName + "不存在!"); } try { FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream out = new ZipOutputStream(fos); String basedir = ""; compressByType(file, out, basedir); out.close(); } catch (Exception e) { e.printStackTrace(); logger.error("执行压缩操作时发生异常:"+e); throw new RuntimeException(e); } } /** * 判断是目录还是文件,根据类型(文件/文件夹)执行不同的压缩方法 * @param file * @param out * @param basedir */ private void compressByType(File file, ZipOutputStream out, String basedir) { System.out.println(file.getPath()+file.getName()); /* 判断是目录还是文件 */ if (file.isDirectory()) { logger.info("压缩:" + basedir + file.getName()); this.compressDirectory(file, out, basedir); } else { logger.info("压缩:" + basedir + file.getName()); this.compressFile(file, out, basedir); } } /** * 压缩一个目录 * @param dir * @param out * @param basedir */ private void compressDirectory(File dir, ZipOutputStream out, String basedir) { if (!dir.exists()){ return; } File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { /* 递归 */ compressByType(files[i], out, basedir + dir.getName() + "/"); } } /** * 压缩一个文件 * @param file * @param out * @param basedir */ private void compressFile(File file, ZipOutputStream out, String basedir) { if (!file.exists()) { return; } try { InputStream input = new FileInputStream(file); out.putNextEntry(new ZipEntry(file.getPath())); int temp = 0; while((temp = input.read()) != -1){ out.write(temp); } input.close(); //out.close(); } catch (Exception e) { throw new RuntimeException(e); } } public static void main(String[] args){ ZipCompressor zc = new ZipCompressor("D:\\FILE_FTP\\药监码文件\\药监文件\\2015\\09\\07\\20150907111245.zip"); zc.compressExe("D:\\FILE_FTP\\药监码文件\\药监文件\\2015\\09\\07\\20150907111245\\"); System.out.println("zip压缩完成"); } } |