package com.xxx.web.util; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import org.apache.tools.zip.ZipOutputStream; public class ZipUtil { private static int bufferSize = 2048; /** * 压缩 * @param srcFileOrDir 原文件或文件夹 * @param destZipFilePath 输出到的目标路径,默认zip文件为原文件或文件夹名称 */ public static void zip(String srcFileOrDir, String destZipFilePath){ File file = new File(srcFileOrDir); if(!file.exists()){ return; } String destZipFileName = file.getName(); zip(srcFileOrDir, destZipFilePath, destZipFileName); } /** * 压缩 * @param srcFileOrDir 原文件或文件夹 * @param destZipFilePath 输出到的目标路径 * @param destZipFileName 生成的zip文件名称 */ public static void zip(String srcFileOrDir, String destZipFilePath, String destZipFileName){ File file = new File(srcFileOrDir); if(!file.exists()){ return; } if(!destZipFilePath.endsWith(File.separator)){ destZipFilePath += File.separator; } File destZipFileParentDir = new File(destZipFilePath); if(!destZipFileParentDir.exists()){ destZipFileParentDir.mkdirs(); } if(!destZipFileName.endsWith(".zip")&&!destZipFileName.endsWith(".ZIP")){ destZipFileName += ".zip"; } if(file.isFile()){ zipFile(srcFileOrDir, destZipFilePath, destZipFileName); }else if(file.isDirectory()){ zipDir(srcFileOrDir, destZipFilePath, destZipFileName); } } private static void zipFile(String srcFileName, String destZipFilePath, String destZipFileName){ File srcFile = new File(srcFileName); ZipOutputStream zipOutputStream = null; try { zipOutputStream = new ZipOutputStream(new FileOutputStream(destZipFilePath + destZipFileName)); zipOutputStream.setEncoding(System.getProperty("sun.jnu.encoding")); String fileName = srcFile.getName(); ZipEntry entry = new ZipEntry(fileName); BufferedInputStream bis = null; try { zipOutputStream.putNextEntry(entry); bis = new BufferedInputStream(new FileInputStream(srcFile)); byte[] buf = new byte[bufferSize]; int len; while ((len = bis.read(buf)) >= 0) { zipOutputStream.flush(); zipOutputStream.write(buf, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { try { if(zipOutputStream!=null){ zipOutputStream.closeEntry(); } if(bis!=null){ bis.close(); } } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { if(zipOutputStream!=null){ zipOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } private static void zipDir(String srcDir, String destZipFilePath, String destZipFileName){ if(!srcDir.endsWith(File.separator)){ srcDir += File.separator; } File srcFile = new File(srcDir); File[] files = srcFile.listFiles(); ZipOutputStream zipOutputStream = null; try { zipOutputStream = new ZipOutputStream(new FileOutputStream(destZipFilePath + destZipFileName)); zipOutputStream.setEncoding(System.getProperty("sun.jnu.encoding")); if(files!=null&&files.length>0){ for(File f :files){ compressFiles(f, f.getParent() ,zipOutputStream); } } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { if(zipOutputStream!=null){ zipOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } private static void compressFiles(File file, String basePath, ZipOutputStream stream){ if(file==null||stream==null){ return; } if(file.isDirectory()){ ZipEntry entry = new ZipEntry(getEntryPath(file,basePath) + File.separator); try { stream.putNextEntry(entry); } catch (IOException e) { e.printStackTrace(); } File[] files = file.listFiles(); if(files!=null&&files.length>0){ for(File f :files){ compressFiles(f, basePath, stream); } } }else{ String fileName = getEntryPath(file, basePath); ZipEntry entry = new ZipEntry(fileName); BufferedInputStream bis = null; try { stream.putNextEntry(entry); bis = new BufferedInputStream(new FileInputStream(file)); byte[] buf = new byte[bufferSize]; int len; while ((len = bis.read(buf)) >= 0) { stream.flush(); stream.write(buf, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { try { if(stream!=null){ stream.closeEntry(); } if(bis!=null){ bis.close(); } } catch (IOException e) { e.printStackTrace(); } } } } private static String getEntryPath(File file, String basePath){ String path = file.getPath().substring(basePath.length() + 1); return path; } /** * 解压 * @param srcZipFile 压缩文件 * @param destDir 目标路径 * @return */ public static boolean unzip(String srcZipFile, String destDir) { boolean rs = true; ZipFile zipFile = null; Enumeration<ZipEntry> entries = null; try { zipFile = new ZipFile(srcZipFile, System.getProperty("sun.jnu.encoding")); if(zipFile!=null){ entries = zipFile.getEntries(); } } catch (IOException e) { e.printStackTrace(); } if(zipFile==null||entries == null){ return true; } ZipEntry zipEntry = null; while (entries.hasMoreElements()) { zipEntry = entries.nextElement(); if (zipEntry.isDirectory()) { String name = zipEntry.getName(); try { name = name.substring(0, name.length() - 1); mkDirs(destDir + File.separator + name); } catch (Exception e) { e.printStackTrace(); } } else { String name = zipEntry.getName(); File file = new File(destDir + File.separator + name); try { mkDirs(file.getParent()); } catch (Exception e) { e.printStackTrace(); } try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } InputStream in = null; FileOutputStream out = null; try { in = zipFile.getInputStream(zipEntry); out = new FileOutputStream(file); int c; byte[] by = new byte[1024]; while ((c = in.read(by)) != -1) { out.flush(); out.write(by, 0, c); } } catch (Exception e) { e.printStackTrace(); } finally { if(out!=null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } if(in!=null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } } try { zipFile.close(); } catch (IOException e) { e.printStackTrace(); } return rs; } private static void mkDirs(String dir){ if (dir == null || dir.equals("")){ return; } File file = new File(dir); if (!file.exists()){ file.mkdirs(); } } }
java实现zip压缩和解压工具
猜你喜欢
转载自www.cnblogs.com/jinzhiming/p/10302984.html
今日推荐
周排行