JDK自带的zip AIP在java.util.zip包下面,主要有以下几个类:
java.util.zip.ZipEntry
java.util.zip.ZipInputStreamjava.util.zip.ZipOutputStream
本文编写的zip工具类有以下功能:打包(单个文件、目录)、解包、查看zip包文件
工具类代码如下
1 package org.net5ijy.util.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.ArrayList; 10 import java.util.List; 11 import java.util.zip.ZipEntry; 12 import java.util.zip.ZipInputStream; 13 import java.util.zip.ZipOutputStream; 14 15 /** 16 * ZIP工具类 17 * 18 */ 19 public class ZipUtil { 20 21 /** 22 * 解包时的读写大小:10MB 23 */ 24 private static final int SIZE = 1024 * 1024 * 10; 25 26 /** 27 * 打包 28 * 29 * @param src 30 * - 需要打包的文件或目录,不可以是根目录,为根目录时抛出IOException 31 * @param zipFile 32 * - 打包后的.zip文件 33 * @throws IOException 34 */ 35 public static void zip(File src, File zipFile) throws IOException { 36 37 // 不可以压缩根目录 38 if (src.getParent() == null) { 39 throw new IOException("不可以压缩根目录"); 40 } 41 // 获取基目录 42 String base = src.getParentFile().getAbsolutePath(); 43 44 ZipOutputStream out = null; 45 BufferedOutputStream bos = null; 46 47 try { 48 49 out = new ZipOutputStream(new FileOutputStream(zipFile)); 50 51 // 创建缓冲输出流 52 bos = new BufferedOutputStream(out); 53 54 // 调用函数 55 zip(out, bos, src, base); 56 57 } catch (IOException e) { 58 e.printStackTrace(); 59 throw e; 60 } finally { 61 // 关闭流 62 if (bos != null) { 63 bos.close(); 64 } 65 if (out != null) { 66 out.close(); 67 } 68 } 69 } 70 71 private static void zip(ZipOutputStream out, BufferedOutputStream bos, 72 File sourceFile, String base) throws IOException { 73 74 // 如果路径为目录(文件夹) 75 if (sourceFile.isDirectory()) { 76 out.putNextEntry(new ZipEntry(sourceFile.getAbsolutePath().replace( 77 base + File.separator, "") 78 + "/")); 79 // 取出文件夹中的文件(或子文件夹) 80 File[] flist = sourceFile.listFiles(); 81 if (flist.length > 0) { 82 for (int i = 0; i < flist.length; i++) { 83 zip(out, bos, flist[i], base); 84 } 85 } 86 } else { // 如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中 87 out.putNextEntry(new ZipEntry(sourceFile.getAbsolutePath().replace( 88 base + File.separator, ""))); 89 BufferedInputStream bis = null; 90 try { 91 // 文件输入流 92 bis = new BufferedInputStream(new FileInputStream(sourceFile)); 93 int b = bis.read(); 94 // 将源文件写入到zip文件中 95 while (b > -1) { 96 bos.write(b); 97 b = bis.read(); 98 } 99 bos.flush();100 } catch (Exception e) {101 throw e;102 } finally {103 if (bis != null) {104 bis.close();105 }106 }107 }108 }109 110 /**111 * 解包112 * 113 * @param zipFile114 * - 需要解包的.zip文件115 * @param targetFolder116 * - 目标目录117 * @throws IOException118 */119 public static void unzip(File zipFile, File targetFolder)120 throws IOException {121 122 // 如果源文件是null 或者 不是一个文件123 if (zipFile == null || !zipFile.isFile()124 || !zipFile.getName().endsWith(".zip")) {125 throw new IOException("请选择正确的.zip文件");126 }127 128 // 如果目标目录为null 或者 不是一个目录129 if (targetFolder == null || !targetFolder.isDirectory()) {130 throw new IOException("请选择正确的解压目录");131 }132 133 // 定义zip文件输入流134 ZipInputStream zin = null;135 136 try {137 138 // 从zipFile创建对应的ZipInputStream输入流139 zin = new ZipInputStream(new FileInputStream(zipFile));140 141 // 获取下一个条目142 for (ZipEntry entry = zin.getNextEntry(); entry != null; entry = zin143 .getNextEntry()) {144 145 // 获取文件或者目录名146 String name = entry.getName();147 // 目录148 if (name.endsWith("/")) {149 File dir = new File(targetFolder, name);150 dir.mkdirs();151 } else { // 文件152 unzip(targetFolder, zin, name);153 }154 }155 } catch (IOException e) {156 e.printStackTrace();157 throw e;158 } finally {159 if (zin != null) {160 zin.close();161 }162 }163 }164 165 private static void unzip(File targetFolder, ZipInputStream zin, String name)166 throws IOException {167 168 // 定义输出流169 FileOutputStream out = null;170 171 try {172 173 // 创建输出流174 out = new FileOutputStream(new File(targetFolder.getAbsolutePath()175 + File.separator + name));176 177 byte[] buf = new byte[SIZE];178 int len = zin.read(buf);179 180 while (len > -1) {181 out.write(buf, 0, len);182 len = zin.read(buf);183 }184 out.flush();185 186 } catch (Exception e) {187 throw e;188 } finally {189 if (out != null) {190 out.close();191 }192 }193 }194 195 /**196 * 获取.zip文件中的条目集合197 * 198 * @param zipFile199 * - 待查看的.zip文件200 * @return 条目的集合201 * @throws IOException202 */203 public static ListzipEntryList(File zipFile) throws IOException {204 205 // 如果源文件是null 或者 不是一个文件206 if (zipFile == null || !zipFile.isFile()) {207 throw new IOException("请选择正确的.zip文件");208 }209 210 // 定义zip文件输入流211 ZipInputStream zin = null;212 213 List l = new ArrayList ();214 215 try {216 // 从zipFile创建对应的ZipInputStream输入流217 zin = new ZipInputStream(new FileInputStream(zipFile));218 // 获取下一个条目219 ZipEntry entry = zin.getNextEntry();220 221 while (entry != null) {222 l.add(entry);223 entry = zin.getNextEntry();224 }225 } catch (IOException e) {226 e.printStackTrace();227 throw e;228 } finally {229 if (zin != null) {230 zin.close();231 }232 }233 return l;234 }235 236 /**237 * 获取.zip文件中的条目名称集合238 * 239 * @param zipFile240 * - 待查看的.zip文件241 * @return 条目名称集合242 * @throws IOException243 */244 public static List zipEntryNameList(File zipFile)245 throws IOException {246 List l = new ArrayList ();247 List entryList = zipEntryList(zipFile);248 for (ZipEntry zipEntry : entryList) {249 l.add(zipEntry.getName());250 }251 return l;252 }253 }
测试代码
1 import static org.net5ijy.util.zip.ZipUtil.*; 2 3 public class ZipUtilTest { 4 5 public static void main(String[] args) throws IOException { 6 7 zip(new File( 8 "D:\\workspace\\SpringCloud\\zip-demo\\src\\org\\net5ijy\\util\\zip\\ZipUtil.java"), 9 new File("d:\\zip01.zip"));10 11 zip(new File("D:\\workspace\\SpringCloud\\spring-data-demo2\\"),12 new File("d:\\zip02.zip"));13 14 unzip(new File("d:\\zip02.zip"), new File("D:\\a\\"));15 16 ListnameList = zipEntryNameList(new File("d:\\zip02.zip"));17 for (String name : nameList) {18 System.out.println(name);19 }20 }21 }