package com.imooc.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; /** * * @version 1.0 * @since 2015-9-14 * 文件解压缩工具类 * */ public final class ZipUtil { /** * 缓冲大小 */ private static int BUFFERSIZE = 2 << 10; /** * 压缩 * @param paths * @param fileName */ public static void zip(String[] paths, String fileName) { ZipOutputStream zos = null; try { zos = new ZipOutputStream(new FileOutputStream(fileName)); for(String filePath : paths) { //递归压缩文件 File file = new File(filePath); String relativePath = file.getName(); if(file.isDirectory()) { relativePath += File.separator; } zipFile(file, relativePath, zos); } } catch (IOException e) { e.printStackTrace(); } finally { try { if(zos != null) { zos.close(); } } catch (IOException e) { e.printStackTrace(); } } } public static void zipFile(File file, String relativePath, ZipOutputStream zos) { InputStream is = null; try { if(!file.isDirectory()) { ZipEntry zp = new ZipEntry(relativePath); zos.putNextEntry(zp); is = new FileInputStream(file); byte[] buffer = new byte[BUFFERSIZE]; int length = 0; while ((length = is.read(buffer)) >= 0) { zos.write(buffer, 0, length); } zos.flush(); zos.closeEntry(); } else { for(File f: file.listFiles()) { zipFile(f, relativePath + f.getName() + File.separator, zos); } } } catch (IOException e) { e.printStackTrace(); } finally { try { if(is != null) { is.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 解压缩 * @param fileName * @param path */ public static void unzip(String fileName, String path) { FileOutputStream fos = null; InputStream is = null; try { ZipFile zf = new ZipFile(new File(fileName)); Enumeration en = zf.entries(); while (en.hasMoreElements()) { ZipEntry zn = (ZipEntry) en.nextElement(); if (!zn.isDirectory()) { is = zf.getInputStream(zn); File f = new File(path + zn.getName()); File file = f.getParentFile(); file.mkdirs(); fos = new FileOutputStream(path + zn.getName()); int len = 0; byte bufer[] = new byte[BUFFERSIZE]; while (-1 != (len = is.read(bufer))) { fos.write(bufer, 0, len); } fos.close(); } } } catch (ZipException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if(null != is) { is.close(); } if(null != fos) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * @param args */ public static void main(String[] args) { zip(new String[] {"D:/tmp/20150418/logs/file/2015-08-28/debug-log.log","D:/tmp/20150418/logs/file/2015-08-28/error-log.log"}, "D:/tmp/20150418/logs/file/2015-08-28/test.zip"); unzip("D:/tmp/20150418/logs/file/2015-08-28/test.zip", "D:/test/"); } }
相关推荐
java中使用ZipEntry对文件目录下的所有文件进行压缩,已解决中文乱码问题,亲测可用,请放心下载。
NKBIN BIN文件解压缩工具,可以方便的解压缩出BIN的文件。
对zip文件进行解压缩工具类,以及jar包
java中使用ZipEntry对文件目录下的所有文件进行压缩,已解决中文乱码问题,亲测可用,请放心下载。
文件压缩与解压缩zip工具类
java解压文件工具类、放入工程直接可用、案例、web开发、unziputil
php在线解压缩工具 php在线解压缩工具php在线解压缩工具
java开发文件解压缩工具箱,可解压ZIP,RAR解决中文乱码
主要介绍了Java压缩文件工具类ZipUtil使用方法代码示例,具有一定借鉴价值,需要的朋友可以参考下。
简单的一个工具类,支持zip、rar、tar等的压缩解压缩,亲自测过可用
解压缩加密工具类SSSS
主要介绍了Java实现的zip压缩及解压缩工具类,结合实例形式分析了java对文件的进行zip压缩及解压缩的具体操作技巧,需要的朋友可以参考下
根据公司的需求做的一个ISCAB解压缩工具!简单易用
zlib压缩解压缩工具,包含源代码,拖拽文件直接压缩解压缩
新中大备份文件解压缩工具
新中大备份数据格式解压缩工具,方便对新中大的数据进行处理
Java工具类ZIP解压缩Java工具类ZIP解压缩Java工具类ZIP解压缩
免费解压缩工具_轻量级别_无广告不收费_bandzip_pro_free_超好用的文件解压缩工具
基于对话框的Rinex O文件压缩解压缩工具
将文件打包成压缩文件,以及对压缩包的解压,方便好用。