压缩工具类,提供压缩文件、解压文件的方法。
源码如下:(点击下载 - ZipUtils.java 、FolderUtils.java、ant-1.7.0.jar、commons-io-2.4.jar、commons-lang-2.6.jar)
1 import java.io.BufferedInputStream; 2 import java.io.BufferedOutputStream; 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.util.Enumeration; 9 import org.apache.commons.io.FilenameUtils; 10 import org.apache.commons.io.IOUtils; 11 import org.apache.commons.lang.ArrayUtils; 12 import org.apache.commons.lang.StringUtils; 13 import org.apache.tools.zip.ZipEntry; 14 import org.apache.tools.zip.ZipFile; 15 import org.apache.tools.zip.ZipOutputStream; 16 17 /** 18 * 压缩工具类 19 * 20 */ 21 public class ZipUtils { 22 23 private static final String DEFAULT_CHARSET = "UTF-8"; 24 25 /** 26 * 压缩文件夹 27 * 28 * @param zipFileName 29 * 打包后文件的名称,含路径 30 * @param sourceFolder 31 * 需要打包的文件夹或者文件的路径 32 * @param zipPathName 33 * 打包目的文件夹名,为空则表示直接打包到根 34 */ 35 public static void zip(String zipFileName, String sourceFolder, String zipPathName) throws Exception { 36 ZipOutputStream out = null; 37 try { 38 File zipFile = new File(zipFileName); 39 40 FolderUtils.mkdirs(zipFile.getParent()); 41 out = new ZipOutputStream(zipFile); 42 out.setEncoding(DEFAULT_CHARSET); 43 if (StringUtils.isNotBlank(zipPathName)) { 44 zipPathName = FilenameUtils.normalizeNoEndSeparator(zipPathName, true) + "/"; 45 } else { 46 zipPathName = ""; 47 } 48 zip(out, sourceFolder, zipPathName); 49 } catch (IOException e) { 50 e.printStackTrace(); 51 throw new Exception(e); 52 } finally { 53 IOUtils.closeQuietly(out); 54 } 55 } 56 57 /** 58 * 压缩文件夹 59 * 60 * @param zipFile 61 * a {@link java.lang.String} object. 62 * @param source 63 * a {@link java.lang.String} object. 64 */ 65 public static void zip(String zipFile, String source) throws Exception { 66 File file = new File(source); 67 zip(zipFile, source, file.isFile() ? StringUtils.EMPTY : file.getName()); 68 } 69 70 /** 71 * 压缩文件夹 72 * 73 * @param zipFile 74 * a {@link java.io.File} object. 75 * @param source 76 * a {@link java.io.File} object. 77 */ 78 public static void zip(File zipFile, File source) throws Exception { 79 zip(zipFile.getAbsolutePath(), source.getAbsolutePath()); 80 } 81 82 private static void zip(ZipOutputStream zos, String file, String pathName) throws IOException { 83 File file2zip = new File(file); 84 if (file2zip.isFile()) { 85 zos.putNextEntry(new ZipEntry(pathName + file2zip.getName())); 86 IOUtils.copy(new FileInputStream(file2zip.getAbsolutePath()), zos); 87 zos.flush(); 88 zos.closeEntry(); 89 } else { 90 File[] files = file2zip.listFiles(); 91 if (ArrayUtils.isNotEmpty(files)) { 92 for (File f : files) { 93 if (f.isDirectory()) { 94 zip(zos, FilenameUtils.normalizeNoEndSeparator(f.getAbsolutePath(), true), 95 FilenameUtils.normalizeNoEndSeparator(pathName + f.getName(), true) + "/"); 96 } else { 97 zos.putNextEntry(new ZipEntry(pathName + f.getName())); 98 IOUtils.copy(new FileInputStream(f.getAbsolutePath()), zos); 99 zos.flush(); 100 zos.closeEntry(); 101 } 102 } 103 } 104 } 105 } 106 107 /** 108 * 解压 109 * 110 * @param fromZipFile 111 * zip文件路径 112 * @param unzipPath 113 * 解压路径 114 */ 115 @SuppressWarnings("unchecked") 116 public static final void unzip(String fromZipFile, String unzipPath) throws Exception { 117 118 FileOutputStream fos = null; 119 InputStream is = null; 120 String path1 = StringUtils.EMPTY; 121 String tempPath = StringUtils.EMPTY; 122 123 if (!new File(unzipPath).exists()) { 124 new File(unzipPath).mkdir(); 125 } 126 ZipFile zipFile = null; 127 try { 128 zipFile = new ZipFile(fromZipFile, DEFAULT_CHARSET); 129 } catch (IOException e1) { 130 e1.printStackTrace(); 131 throw new Exception(e1); 132 } 133 File temp = new File(unzipPath); 134 String strPath = temp.getAbsolutePath(); 135 Enumeration<ZipEntry> enu = zipFile.getEntries(); 136 ZipEntry zipEntry = null; 137 while (enu.hasMoreElements()) { 138 zipEntry = (ZipEntry) enu.nextElement(); 139 path1 = zipEntry.getName(); 140 if (zipEntry.isDirectory()) { 141 tempPath = FilenameUtils.normalizeNoEndSeparator(strPath + File.separator + path1, true); 142 File dir = new File(tempPath); 143 dir.mkdirs(); 144 continue; 145 } else { 146 147 BufferedInputStream bis = null; 148 BufferedOutputStream bos = null; 149 try { 150 is = zipFile.getInputStream(zipEntry); 151 bis = new BufferedInputStream(is); 152 path1 = zipEntry.getName(); 153 tempPath = FilenameUtils.normalizeNoEndSeparator(strPath + File.separator + path1, true); 154 FolderUtils.mkdirs(new File(tempPath).getParent()); 155 fos = new FileOutputStream(tempPath); 156 bos = new BufferedOutputStream(fos); 157 158 IOUtils.copy(bis, bos); 159 } catch (IOException e) { 160 e.printStackTrace(); 161 throw new Exception(e); 162 } finally { 163 IOUtils.closeQuietly(bis); 164 IOUtils.closeQuietly(bos); 165 IOUtils.closeQuietly(is); 166 IOUtils.closeQuietly(fos); 167 } 168 } 169 } 170 } 171 }