| | |
| | | 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) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 创建目录 |
| | | */ |
| | | 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源文件 |