¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.lf.server.helper; |
| | | |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | |
| | | import java.io.*; |
| | | import java.util.Enumeration; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipFile; |
| | | import java.util.zip.ZipOutputStream; |
| | | |
| | | /** |
| | | * Zip帮å©ç±» |
| | | * @author WWW |
| | | */ |
| | | public class ZipHelper { |
| | | private final static int BUFFER_SIZE = 1024; |
| | | |
| | | private final static byte[] BUFFER = new byte[1024]; |
| | | |
| | | private final static Log log = LogFactory.getLog(ZipHelper.class); |
| | | |
| | | /** |
| | | * è§£å缩 |
| | | * |
| | | * @param filePath zipæä»¶ |
| | | * @param zipDir è§£åè·¯å¾ |
| | | * @return æåæ¯/å¦ |
| | | */ |
| | | public static boolean unzip(String filePath, String zipDir) { |
| | | try { |
| | | int count; |
| | | ZipFile zipfile = new ZipFile(filePath); |
| | | |
| | | Enumeration e = zipfile.entries(); |
| | | while (e.hasMoreElements()) { |
| | | ZipEntry entry = (ZipEntry) e.nextElement(); |
| | | if (entry.isDirectory()) { |
| | | File f = new File(zipDir + File.separator + entry.getName()); |
| | | if (!f.exists()) { |
| | | f.mkdirs(); |
| | | } |
| | | continue; |
| | | } |
| | | |
| | | BufferedInputStream is = new BufferedInputStream(zipfile.getInputStream(entry)); |
| | | FileOutputStream fos = new FileOutputStream(zipDir + File.separator + entry.getName()); |
| | | BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE); |
| | | |
| | | while ((count = is.read(BUFFER, 0, BUFFER_SIZE)) != -1) { |
| | | dest.write(BUFFER, 0, count); |
| | | } |
| | | dest.flush(); |
| | | dest.close(); |
| | | is.close(); |
| | | } |
| | | |
| | | return true; |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage() + ex.getStackTrace() + "\n"); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Zipå缩 |
| | | * |
| | | * @param zipFile Zipæºæä»¶ |
| | | * @param sourceDir æºæä»¶å¤¹ |
| | | * @return æåæ¯/å¦ |
| | | */ |
| | | public static boolean zip(String zipFile, String sourceDir) { |
| | | ZipOutputStream zos = null; |
| | | try { |
| | | FileOutputStream fileOutputStream = new FileOutputStream(zipFile); |
| | | zos = new ZipOutputStream(fileOutputStream); |
| | | |
| | | File sourceFile = new File(sourceDir); |
| | | compress(sourceFile, zos, sourceFile.getName()); |
| | | |
| | | return true; |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage() + ex.getStackTrace() + "\n"); |
| | | return false; |
| | | } finally { |
| | | try { |
| | | if (zos != null) { |
| | | zos.close(); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage() + e.getStackTrace() + "\n"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Zipå缩 |
| | | * |
| | | * @param sourceFile Zipæºæä»¶ |
| | | * @param zos Zipè¾åºæµ |
| | | * @param name Zipæä»¶åç§° |
| | | * @throws Exception |
| | | */ |
| | | public static void compress(File sourceFile, ZipOutputStream zos, String name) throws Exception { |
| | | if (sourceFile.isFile()) { |
| | | // åzipè¾åºæµä¸æ·»å ä¸ä¸ªzipå®ä½ï¼æé å¨ä¸name为zipå®ä½çæä»¶çåå |
| | | zos.putNextEntry(new ZipEntry(name)); |
| | | |
| | | // copyæä»¶å°zipè¾åºæµä¸ |
| | | int len; |
| | | FileInputStream in = new FileInputStream(sourceFile); |
| | | while ((len = in.read(BUFFER)) != -1) { |
| | | zos.write(BUFFER, 0, len); |
| | | } |
| | | zos.closeEntry(); |
| | | in.close(); |
| | | } else { |
| | | File[] listFiles = sourceFile.listFiles(); |
| | | if (listFiles == null || listFiles.length == 0) { |
| | | // 空æä»¶å¤¹çå¤çï¼æ²¡ææä»¶ï¼ä¸éè¦æä»¶çcopy |
| | | zos.putNextEntry(new ZipEntry(name + "/")); |
| | | zos.closeEntry(); |
| | | } else { |
| | | for (File file : listFiles) { |
| | | compress(file, zos, name + "/" + file.getName()); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |