特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过。如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:
1 package com.utility.zip; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileOutputStream; 8 import java.io.IOException; 9 import java.util.zip.ZipEntry; 10 import java.util.zip.ZipInputStream; 11 import java.util.zip.ZipOutputStream; 12 13 import com.utility.io.IOUtil; 14 15 /** 16 * 通过Java的Zip输入输出流实现压缩和解压文件 17 * 18 * @author liujiduo 19 * 20 */ 21 public final class ZipUtil { 22 23 private ZipUtil() { 24 // empty 25 } 26 27 /** 28 * 压缩文件 29 * 30 * @param filePath 31 * 待压缩的文件路径 32 * @return 压缩后的文件 33 */ 34 public static File zip(String filePath) { 35 File target = null; 36 File source = new File(filePath); 37 if (source.exists()) { 38 // 压缩文件名=源文件名.zip 39 String zipName = source.getName() + ".zip"; 40 target = new File(source.getParent(), zipName); 41 if (target.exists()) { 42 target.delete(); // 删除旧的文件 43 } 44 FileOutputStream fos = null; 45 ZipOutputStream zos = null; 46 try { 47 fos = new FileOutputStream(target); 48 zos = new ZipOutputStream(new BufferedOutputStream(fos)); 49 // 添加对应的文件Entry 50 addEntry("/", source, zos); 51 } catch (IOException e) { 52 throw new RuntimeException(e); 53 } finally { 54 IOUtil.closeQuietly(zos, fos); 55 } 56 } 57 return target; 58 } 59 60 /** 61 * 扫描添加文件Entry 62 * 63 * @param base 64 * 基路径 65 * 66 * @param source 67 * 源文件 68 * @param zos 69 * Zip文件输出流 70 * @throws IOException 71 */ 72 private static void addEntry(String base, File source, ZipOutputStream zos) 73 throws IOException { 74 // 按目录分级,形如:/aaa/bbb.txt 75 String entry = base + source.getName(); 76 if (source.isDirectory()) { 77 for (File file : source.listFiles()) { 78 // 递归列出目录下的所有文件,添加文件Entry 79 addEntry(entry + "/", file, zos); 80 } 81 } else { 82 FileInputStream fis = null; 83 BufferedInputStream bis = null; 84 try { 85 byte[] buffer = new byte[1024 * 10]; 86 fis = new FileInputStream(source); 87 bis = new BufferedInputStream(fis, buffer.length); 88 int read = 0; 89 zos.putNextEntry(new ZipEntry(entry)); 90 while ((read = bis.read(buffer, 0, buffer.length)) != -1) { 91 zos.write(buffer, 0, read); 92 } 93 zos.closeEntry(); 94 } finally { 95 IOUtil.closeQuietly(bis, fis); 96 } 97 } 98 } 99 100 /**101 * 解压文件102 * 103 * @param filePath104 * 压缩文件路径105 */106 public static void unzip(String filePath) {107 File source = new File(filePath);108 if (source.exists()) {109 ZipInputStream zis = null;110 BufferedOutputStream bos = null;111 try {112 zis = new ZipInputStream(new FileInputStream(source));113 ZipEntry entry = null;114 while ((entry = zis.getNextEntry()) != null115 && !entry.isDirectory()) {116 File target = new File(source.getParent(), entry.getName());117 if (!target.getParentFile().exists()) {118 // 创建文件父目录119 target.getParentFile().mkdirs();120 }121 // 写入文件122 bos = new BufferedOutputStream(new FileOutputStream(target));123 int read = 0;124 byte[] buffer = new byte[1024 * 10];125 while ((read = zis.read(buffer, 0, buffer.length)) != -1) {126 bos.write(buffer, 0, read);127 }128 bos.flush();129 }130 zis.closeEntry();131 } catch (IOException e) {132 throw new RuntimeException(e);133 } finally {134 IOUtil.closeQuietly(zis, bos);135 }136 }137 }138 139 public static void main(String[] args) {140 String targetPath = "E:\\Win7壁纸";141 File file = ZipUtil.zip(targetPath);142 System.out.println(file);143 ZipUtil.unzip("F:\\Win7壁纸.zip");144 }145 }
1 package com.utility.io; 2 3 import java.io.Closeable; 4 import java.io.IOException; 5 6 /** 7 * IO流工具类 8 * 9 * @author liujiduo10 * 11 */12 public class IOUtil {13 /**14 * 关闭一个或多个流对象15 * 16 * @param closeables17 * 可关闭的流对象列表18 * @throws IOException19 */20 public static void close(Closeable... closeables) throws IOException {21 if (closeables != null) {22 for (Closeable closeable : closeables) {23 if (closeable != null) {24 closeable.close();25 }26 }27 }28 }29 30 /**31 * 关闭一个或多个流对象32 * 33 * @param closeables34 * 可关闭的流对象列表35 */36 public static void closeQuietly(Closeable... closeables) {37 try {38 close(closeables);39 } catch (IOException e) {40 // do nothing41 }42 }43 44 }
参考网站