| | |
| | | import java.util.zip.ZipFile; |
| | | import java.util.zip.ZipOutputStream; |
| | | |
| | | /** |
| | | * Zip帮助类 |
| | | * @author WWW |
| | | */ |
| | | @SuppressWarnings("ALL") |
| | | public class ZipHelper { |
| | | private final static int BUFFER_SIZE = 1024; |
| | | |
| | |
| | | |
| | | 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 { |
| | |
| | | dir.mkdirs(); |
| | | } |
| | | |
| | | int count; |
| | | zipFile = new ZipFile(filePath, Charset.forName("GBK")); |
| | | 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(); |
| | | fos.close(); |
| | | is.close(); |
| | | } |
| | | createDirs(zipFile, zipDir); |
| | | writeFiles(zipFile, zipDir); |
| | | |
| | | return true; |
| | | } catch (Exception ex) { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Zip压缩 |
| | | * |
| | | * @param zipFile zip源文件 |
| | | * @param sourceDir 源文件夹 |
| | | * @return 成功是/否 |
| | | */ |
| | | 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(); |
| | | } |
| | | |
| | | public static boolean zip(String zipFile, String sourceDir) { |
| | | FileOutputStream fos = null; |
| | | ZipOutputStream zos = null; |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 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) { |
| | |
| | | } else { |
| | | File[] listFiles = sourceFile.listFiles(); |
| | | if (listFiles == null || listFiles.length == 0) { |
| | | // 空文件夹的处理:没有文件,不需要文件的copy |
| | | zos.putNextEntry(new ZipEntry(name + "/")); |
| | | zos.closeEntry(); |
| | | } else { |