示例代码:
String zipFilePath = filePath+".zip"; //压缩文件 在原文件名的基础上,追加 。zip ZipUtil.compress(filePath,zipFilePath); //压缩文件
/** * @Description: * 压缩和解压工具 */ public class ZipUtil { public static void compress(String srcFilePath, String destFilePath) { File srcFile = new File(srcFilePath); if (!srcFile.exists()) { throw new RuntimeException(srcFilePath + "不存在"); } if(!srcFile.isFile()){ throw new RuntimeException(srcFilePath + "不是文件"); } File zipFile = new File(srcFilePath+".zip"); FileOutputStream fos = null; CheckedOutputStream cos = null; ZipOutputStream zos = null; try { fos = new FileOutputStream(zipFile); cos = new CheckedOutputStream(fos, new CRC32()); zos = new ZipOutputStream(cos); // zos.setEncoding("GBK"); //设置字符集 zos.setMethod(ZipOutputStream.DEFLATED); // 启用压缩 zos.setLevel(Deflater.BEST_COMPRESSION); // 设置压缩级别为最强压缩 String baseDir = ""; compressFile(srcFile, zos, baseDir); zos.close(); } catch (Exception e) { e.printStackTrace(); } finally{ IOUtils.closeQuietly(fos); IOUtils.closeQuietly(cos); IOUtils.closeQuietly(zos); } } /** * 压缩文件 * */ /** * 压缩文件 * @param file 待压缩文件 * @param zos 压缩流 * @param baseDir 文件标记 */ private static void compressFile(File file, ZipOutputStream zos, String baseDir) { BufferedInputStream bis = null; FileInputStream fis = null; try { fis = new FileInputStream(file); bis = new BufferedInputStream(fis); ZipEntry entry = new ZipEntry(baseDir + file.getName()); zos.putNextEntry(entry); int count; byte[] buf = new byte[1024]; while ((count = bis.read(buf)) != -1) { zos.write(buf, 0, count); } zos.flush(); // 注,在使用缓冲流写压缩文件时,一个条件完后一定要刷新,不然可能有的内容就会存入到后面条目中去了 zos.closeEntry(); // 关闭当前ZIP条目并定位流以写入下一个条目 } catch (Exception e) { e.printStackTrace(); } finally{ IOUtils.closeQuietly(fis); IOUtils.closeQuietly(bis); } } /** * @Description: * 解压文件 * @param zipPath 被压缩文件,请使用绝对路径 * @param //targetPath 解压路径,解压后的文件将会放入此目录中,请使用绝对路径 * 默认为压缩文件的路径的父目录为解压路径 * @param //encoding 解压编码 */ public static void decompress(String zipPath) throws FileNotFoundException, ZipException, IOException { // 获取解缩文件 File file = new File(zipPath); if (!file.isFile()) { throw new FileNotFoundException("要解压的文件不存在"); } String targetPath = file.getParent(); // ZipFile zipFile = new ZipFile(file, "GBK"); // 实例化ZipFile对象 GBK // Enumeration<ZipEntry> files = zipFile.getEntries(); // 获取ZipFile中的条目 ZipFile zipFile = new ZipFile(file, Charset.forName("GBK")); // 实例化ZipFile对象 GBK Enumeration<? extends ZipEntry> files = zipFile.entries(); // 获取ZipFile中的条目 ZipEntry entry = null; // 迭代中的每一个条目 File outFile = null; // 解压后的文件 BufferedInputStream bin = null; // 读取压缩文件的输入流 BufferedOutputStream bout = null; // 写入解压后文件的输出流 while (files.hasMoreElements()) { entry = files.nextElement(); // 获取解压条目 // 实例化解压后文件对象 outFile = new File(targetPath + File.separator + entry.getName()); // 如果条目为目录,则跳向下一个 if (entry.getName().endsWith(File.separator)) { outFile.mkdirs(); continue; } if (!outFile.getParentFile().exists()) { // 创建目录 outFile.getParentFile().mkdirs(); } outFile.createNewFile(); // 创建新文件 if (!outFile.canWrite()) { // 如果不可写,则跳向下一个条目 continue; } try { // 获取读取条目的输入流 bin = new BufferedInputStream(zipFile.getInputStream(entry)); // 获取解压后文件的输出流 bout = new BufferedOutputStream(new FileOutputStream(outFile)); // 读取条目,并写入解压后文件 byte[] buffer = new byte[1024]; int readCount = -1; while ((readCount = bin.read(buffer)) != -1) { bout.write(buffer, 0, readCount); } } finally { IOUtils.closeQuietly(bin); IOUtils.closeQuietly(bout); } } zipFile.close(); //这个需要关闭不然删除不了 } /** * @Description: * 解压文件 // * @param zipPath 被压缩文件,请使用绝对路径 // * @param targetPath 解压路径,解压后的文件将会放入此目录中,请使用绝对路径 * 默认为压缩文件的路径的父目录为解压路径 // * @param encoding 解压编码 */ // public static void main(String[] args) throws Exception{ // String file = "C:\\FakePath\\412901197810083510_20180909_p2p_m.xml"; // ZipUtil.compress(file,file+".zip"); // ZipUtil.decompress(file+".zip"); // } }