| | |
| | | import org.apache.commons.logging.LogFactory; |
| | | |
| | | import java.io.*; |
| | | import java.nio.charset.Charset; |
| | | import java.util.Enumeration; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipFile; |
| | |
| | | private final static Log log = LogFactory.getLog(ZipHelper.class); |
| | | |
| | | /** |
| | | * 解压缩 |
| | | * Zip解压 |
| | | * |
| | | * @param filePath zip文件 |
| | | * @param zipDir 解压路径 |
| | | * @return 成功是/否 |
| | | */ |
| | | public static boolean unzip(String filePath, String zipDir) { |
| | | ZipFile zipFile = null; |
| | | 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(); |
| | | File dir = new File(zipDir); |
| | | if (!dir.exists() || !dir.isDirectory()) { |
| | | dir.mkdirs(); |
| | | } |
| | | |
| | | zipFile = new ZipFile(filePath, Charset.forName("GBK")); |
| | | createDirs(zipFile, zipDir); |
| | | writeFiles(zipFile, zipDir); |
| | | |
| | | return true; |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage() + ex.getStackTrace() + "\n"); |
| | | log.error(ex.getMessage(), ex); |
| | | return false; |
| | | } finally { |
| | | try { |
| | | if (null != zipFile) { |
| | | zipFile.close(); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 创建目录 |
| | | */ |
| | | private static void createDirs(ZipFile zipFile, String zipDir) { |
| | | 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(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 写文件 |
| | | */ |
| | | private static void writeFiles(ZipFile zipFile, String zipDir) throws IOException { |
| | | Enumeration<?> e = zipFile.entries(); |
| | | while (e.hasMoreElements()) { |
| | | ZipEntry entry = (ZipEntry) e.nextElement(); |
| | | if (!entry.isDirectory()) { |
| | | writeFile(zipFile, entry, zipDir); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 写文件 |
| | | */ |
| | | private static void writeFile(ZipFile zipFile, ZipEntry entry, String zipDir) throws IOException { |
| | | int count; |
| | | |
| | | 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(); |
| | | fos.close(); |
| | | is.close(); |
| | | } |
| | | |
| | | /** |
| | | * Zip压缩 |
| | | * |
| | | * @param zipFile Zip源文件 |
| | | * @param zipFile zip源文件 |
| | | * @param sourceDir 源文件夹 |
| | | * @return 成功是/否 |
| | | */ |
| | | public static boolean zip(String zipFile, String sourceDir) { |
| | | FileOutputStream fos = null; |
| | | ZipOutputStream zos = null; |
| | | try { |
| | | FileOutputStream fileOutputStream = new FileOutputStream(zipFile); |
| | | zos = new ZipOutputStream(fileOutputStream); |
| | | fos = new FileOutputStream(zipFile); |
| | | zos = new ZipOutputStream(fos); |
| | | |
| | | File sourceFile = new File(sourceDir); |
| | | compress(sourceFile, zos, sourceFile.getName()); |
| | | |
| | | return true; |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage() + ex.getStackTrace() + "\n"); |
| | | log.error(ex.getMessage(), ex); |
| | | return false; |
| | | } finally { |
| | | try { |
| | | if (zos != null) { |
| | | if (null != zos) { |
| | | zos.close(); |
| | | } |
| | | if (null != fos) { |
| | | fos.close(); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage() + e.getStackTrace() + "\n"); |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | } |
| | | } |
| | |
| | | /** |
| | | * Zip压缩 |
| | | * |
| | | * @param sourceFile Zip源文件 |
| | | * @param zos Zip输出流 |
| | | * @param name Zip文件名称 |
| | | * @param sourceFile zip源文件 |
| | | * @param zos zip输出流 |
| | | * @param name zip文件名称 |
| | | * @throws Exception |
| | | */ |
| | | public static void compress(File sourceFile, ZipOutputStream zos, String name) throws Exception { |