package com.cxd.bank.utils; import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; /** * @time 21:21 * @修改记录 <pre> * 版本 修改人 修改时间 修改内容描述 * -------------------------------------------------- * <p> * -------------------------------------------------- * </pre> */ public class ZipUtils { /** * @param targetFile 目标文件 * @param lacation 压缩文件存放url * @param newFileName 压缩文件名称 * @param description 文件描述 */ public static void zipFile(File targetFile, String lacation, String newFileName, String description){ InputStream input = null; // 定义文件的输入流 ZipOutputStream zipOut = null; // 声明压缩流对象 try { // 定义压缩文件名称 File zipFile = new File(lacation + File.separator +newFileName + ".zip"); zipFile.createNewFile(); input = new FileInputStream(targetFile); zipOut = null; zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); // 设置ZipEntry对象 zipOut.putNextEntry(new ZipEntry(targetFile.getName())); // 设置注释 zipOut.setComment(description) ; byte[] buffer = new byte[4096] ; while((input.read(buffer)) > 0){ // 读取内容 zipOut.write(buffer) ; // 压缩输出 } } catch (IOException e) { e.printStackTrace(); }finally { try { input.close() ; // 关闭输入流 zipOut.close() ; // 关闭输出流 } catch (IOException e) { e.printStackTrace(); } } } /** * 压缩文件夹 * @param targetFiles * @param location * @param newFileName * @param description */ public static void zipFiles(File targetFiles , String location, String newFileName, String description) { File zipFile = new File(location + File.separator + newFileName +".zip") ; // 定义压缩文件名称 InputStream input = null ; // 定义文件输入流 ZipOutputStream zipOut = null ; // 声明压缩流对象 try { zipOut = new ZipOutputStream(new FileOutputStream(zipFile)) ; zipOut.setComment(description) ; // 设置注释 byte[] buffer = new byte[4096] ; if(targetFiles.isDirectory()){ // 判断是否是文件夹 File[] lists = targetFiles.listFiles() ; // 列出全部文件 for(int i=0;i<lists.length;i++){ input = new FileInputStream(lists[i]) ; // 定义文件的输入流 zipOut.putNextEntry(new ZipEntry(lists[i].getName())) ; // 设置ZipEntry对象 while((input.read(buffer)) > 0){ // 读取内容 zipOut.write(buffer) ; // 压缩输出 } input.close() ; // 关闭输入流 } } } catch (IOException e) { e.printStackTrace(); } finally { try { input.close(); zipOut.close() ; // 关闭输出流 } catch (IOException e) { e.printStackTrace(); } } } /** * 解压zip文件 * @param targetZip * @param location */ public static void unZipFiles(File targetZip, String location) { File outFile = null ; // 输出文件的时候要有文件夹的操作 ZipInputStream zipInput = null ; // 定义压缩输入流 OutputStream out = null ; // 定义输出流,用于输出每一个实体内容 InputStream input = null ; // 定义输入流,读取每一个ZipEntry ZipEntry entry = null ; // 每一个压缩实体 try { ZipFile zipFile = new ZipFile(targetZip) ; // 实例化ZipFile对象 zipInput = new ZipInputStream(new FileInputStream(targetZip)) ; // 实例化ZIpInputStream while((entry = zipInput.getNextEntry()) != null){ // 得到一个压缩实体 System.out.println("解压缩" + entry.getName() + "文件。") ; // 定义输出的文件路径 outFile = new File(location + File.separator + entry.getName()) ; // 如果输出文件夹不存在 if(!outFile.getParentFile().exists()){ // 创建文件夹 ,如果这里的有多个文件夹不存在,请使用mkdirs(), // 如果只是单纯的一个文件夹,使用mkdir()就好了 outFile.getParentFile().mkdir() ; } if(!outFile.exists()){ // 判断输出文件是否存在 outFile.createNewFile() ; // 创建文件 } input = zipFile.getInputStream(entry) ; // 得到每一个实体的输入流 out = new FileOutputStream(outFile) ; // 实例化文件输出流 byte[] buffer = new byte[4096]; while(input.read(buffer) > 0){ out.write(buffer) ; } input.close() ; // 关闭输入流 out.close() ; // 关闭输出流 } } catch (IOException e) { e.printStackTrace(); } finally { try { input.close() ; out.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { // 测试 File file = new File("./name.zip"); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } //ZipUtils.zipFile(file, ".", "feng", "压缩文件测试"); ZipUtils.unZipFiles(file, "./name"); //ZipUtils.zipFiles(file, ".", "name", "压缩文件夹"); } }