2018-06-26 9页 doc 23KB 21阅读
is_574951
暂无简介
* Title: *
* ** Description: *
* ** Copyright ? 2008,2009 烽火科技. All right reserved *
* ** Company: 烽火科技 *
* * @author chenwei * */ public class ZipUtil { /** * 文件压缩方法 * * @param out * zip文件压缩输出流 * @param f * 压缩源文件 * @param base * 目录 * @param first * 是否是第一次操作 * @throws IOException * IO异常 */ private static void zipWork(ZipOutputStream out, File f, String base, boolean first) throws IOException { if (first) { if (f.isDirectory()) { out.putNextEntry(new org.apache.tools.zip.ZipEntry("/")); base = base + f.getName(); first = false; } else { base = f.getName(); } } if (f.isDirectory()) { File[] fl = f.listFiles(); base = base + "/"; for (int i = 0; i < fl.length; i++) { zipWork(out, fl[i], base + fl[i].getName(), first); } } else { out.putNextEntry(new org.apache.tools.zip.ZipEntry(base)); FileInputStream in = new FileInputStream(f); int b; while ((b = in.read()) != -1) { out.write(b); } in.close(); } } /** * zip文件解压 * * @param zipFile * zip文件名 * @param unZipRoot * 解压目录 * @throws IOException * IO异常 * @throws ZipException * Zip文件解压异常 */ @SuppressWarnings("unchecked") private static void unZipFileByOpache(org.apache.tools.zip.ZipFile zipFile, String unZipRoot) throws ZipException, IOException { // 获得ZIP 文件条目 java.util.Enumeration e = zipFile.getEntries(); org.apache.tools.zip.ZipEntry zipEntry; while (e.hasMoreElements()) { zipEntry = (org.apache.tools.zip.ZipEntry) e.nextElement(); InputStream fis = zipFile.getInputStream(zipEntry); if (!zipEntry.isDirectory()) { StringBuffer zipFileName = new StringBuffer(); zipFileName.append(unZipRoot); zipFileName.append("/"); zipFileName.append(zipEntry.getName()); File file = new File(zipFileName.toString()); // 获得文件父目录 File parentFile = file.getParentFile(); parentFile.mkdirs(); FileOutputStream fos = new FileOutputStream(file); byte[] b = new byte[1024]; int len; while ((len = fis.read(b, 0, b.length)) != -1) { fos.write(b, 0, len); } fos.close(); fis.close(); } } } /** * zip文件压缩方法 * * @param zipFileName * zip文件名 * @param inputFileName * 压缩文件名 * @param dncoding * 压缩编码 * @throws IOException * IO异常 */ public static void ZipFile(String zipFileName, String inputFileName, String dncoding) throws IOException { // zip输出流 org.apache.tools.zip.ZipOutputStream out = new org.apache.tools.zip.ZipOutputStream( new FileOutputStream(zipFileName)); // 设置文件压缩文件格式 out.setEncoding(dncoding); File inputFile = new File(inputFileName); zipWork(out, inputFile, "", true); out.close(); } /** * zip文件解压方法 * * @param unZipFileName * zip文件名 * @param unZipPath * 解压路径 * @param dncoding * 编码 * @throws IOException * IO异常 */ public static void unZipFile(String unZipFileName, String unZipPath, String dncoding) throws IOException { // zip文件对象 org.apache.tools.zip.ZipFile zipFile = new org.apache.tools.zip.ZipFile( unZipFileName, dncoding); // 解压文件- unZipFileByOpache(zipFile, unZipPath); } /** * 测试方法 * * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // ZipFile("d:/test/Daily_0817.zip", "d:/test/Daily_0817", "UTF-8"); // unZipFile("d:/test/Daily_0817.zip", "d:/test/", "UTF-8"); } }