- 浏览: 174308 次
- 性别:
- 来自: 西安
文章分类
最新评论
-
zhongyue1q:
您好,我想继续开发DBExplorer系统,您说说一下您的QQ ...
推荐一款使用Extjs做GUI的数据库浏览工具--DBExplorer -
huangyabin001:
你好现在遇到一个问题,我现在的工作是修改系统bug,现在有个b ...
Android2.1消息应用(Messaging)源码学习笔记之二 -
sunjunliangsunjun:
Android2.1消息应用(Messaging)源码学习笔记之二 -
java-admin:
1:bitbucket
免费支持private repo,最多 ...
Code host选择 -
jialeadmin:
期待下文字数补足
[翻译]Groovy入门指南——01初学者教程
使用Java操作zip文件
Java提供了操作zip文件的API,具体来说,它们位于:java.util.zip 包中,以下的两个工具类分别用于创建zip文件、展开(解压缩)zip文件。
创建zip文件的助手类:
/** * @author INC062805 * */ public class ZipHelper { // 静态创建起 public static ZipHelper create(File input, File output) { // 检查参数 if (input == null || !input.exists()) { throw new IllegalArgumentException("input is NULL or not exist!"); } if (output == null) { throw new IllegalArgumentException("output can not be NULL!"); } return new ZipHelper(input, output); } // 隐藏构建器 ZipHelper(File input, File output) { this.input = input; this.output = output; } // 开始执行压缩 public boolean start() { OutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(output)); ZipOutputStream zipOut = new ZipOutputStream(out); zipOut.setLevel(ZipEntry.STORED); zip(zipOut, input, input); zipOut.flush(); zipOut.finish(); zipOut.close(); return true; } catch (Exception e) { // e.printStackTrace(); return false; } finally { IOUtils.quietCloseOutputStream(out); } } // 执行压缩,input:输入文件(可以是目录),rootDir:起始目录 private void zip(ZipOutputStream out, File input, File rootDir) throws IOException { if (input == null || !input.exists()) { return; } if (input.isDirectory()) { zipDirectory(out, input, rootDir); } else { zipFile(out, input, rootDir); } } // 向ZipOutputStream中添加目录,dir:输入目录,rootDir:起始目录 private void zipDirectory(ZipOutputStream out, File dir, File rootDir) throws IOException { // 检查input目录,是否是系统顶级目标,例如:c:\ or / if (rootDir.getParentFile() == null) { // SKIP ROOT DIR } if (!onlyFile) { String name = getEntryNameString(dir, rootDir) + '/'; out.putNextEntry(new ZipEntry(name)); // System.out.println("zip.dir:" + entry.getName()); } File[] files = dir.listFiles(); for (File f : files) { zip(out, f, rootDir); } } // 向ZipOutputStream中 添加文件 private void zipFile(ZipOutputStream out, File file, File rootDir) throws IOException { // ZipEntry entry = new ZipEntry(getEntryNameString(file, rootDir)); out.putNextEntry(entry); InputStream in = null; try { in = new FileInputStream(file); IOUtils.copy(in, out); out.closeEntry(); // System.out.println("zip.file:" + entry.getName()); } finally { IOUtils.quietCloseInputStream(in); } } // 通过输入文件(名称),取得在Zip Entry中应该有的名称 private String getEntryNameString(final File input, final File rootDir) { if (onlyFile) { return input.getName(); } // 检查是否是root if (input.equals(rootDir)) { return input.getName(); } // 确认回溯前的起始位置 final File parent = rootDir.getParentFile(); File file = input; // 回溯 直到找到 root StringBuilder ret = new StringBuilder(); do { if (ret.length() > 0) { ret.insert(0, '/'); } ret.insert(0, file.getName()); file = file.getParentFile(); } while (file != null && !file.equals(parent)); return ret.toString(); } // 是否更新已存在的输出文件 // private boolean update = false; // 是否仅仅打包文件,忽略目录结构 private boolean onlyFile = false; // 输入输出 private final File input; private final File output; public boolean isOnlyFile() { return onlyFile; } public void setOnlyFile(boolean onlyFile) { this.onlyFile = onlyFile; } }
展开zip文件的助手类:
/** * @author INC062805 * * 展开zip文件的助手类 */ public class UnZipHelper { // public static UnZipHelper create(File srcZipFile, File destDir) { return new UnZipHelper(srcZipFile, destDir); } // private File srcZipFile, destDir; private CharSequence error = null; // 隐藏构建器 private UnZipHelper(File srcZipFile, File destDir) { this.srcZipFile = srcZipFile; this.destDir = destDir; } // 取得错误信息 public CharSequence getErrorInfo() { return error; } // 展开src 指定的zip文件到 目标位置dest public boolean start() { return start(true); } // 展开src 指定的zip文件到 目标位置dest,并自动创建顶级目录 public boolean start(boolean autoCreateTopDir) { // 自动创建顶级目录 --- 与zip文件同名 if (autoCreateTopDir) { String name = srcZipFile.getName(); // 析除扩展名 int pos = name.lastIndexOf('.'); if (pos > 0) { name = name.substring(0, pos); } destDir = new File(destDir, name); destDir.mkdirs(); } try { ZipFile zf = new ZipFile(srcZipFile); Enumeration<? extends ZipEntry> e = zf.entries(); boolean bool = false; while (e.hasMoreElements()) { ZipEntry ze = e.nextElement(); // 处理目录 if (ze.isDirectory()) { bool = expandDirectory(ze, destDir); continue; } // 处理文件 bool = expandFile(zf, ze, destDir); } return bool; } catch (Exception e) { error = "open zipfile Error:(" + e.getLocalizedMessage() + ")"; return false; } } // 解压缩目录元素 到目标位置 private boolean expandDirectory(ZipEntry ze, File destDir) { File dir = new File(destDir, ze.getName()); boolean bool = dir.exists() ? true : dir.mkdirs(); // System.out.println("Expand.Dir:" + dir.getAbsolutePath()); if (!bool) { error = "create Dest Directory Error,:-("; } return bool; } // 解压缩文件元素到目标目录下 private boolean expandFile(ZipFile zf, ZipEntry ze, File destDir) { // 定位到目标目录 String name = ze.getName(); File dir = locateDestDirectory(name, destDir); // 确认目标文件位置 int pos = name.lastIndexOf('/'); if (pos > 0) { name = name.substring(pos + 1); } // 从zip 输入流中创建 目标文件 return makeFile(new File(dir, name), zf, ze); } // 定位目标实体的所在目录 private File locateDestDirectory(String path, File destDir) { File dir = destDir; int pos = path.indexOf('/'); while (pos > 0) { dir = new File(dir, path.substring(0, pos)); path = path.substring(pos + 1); pos = path.indexOf('/'); } return dir; } // private boolean makeFile(File file, ZipFile zf, ZipEntry ze) { OutputStream out = null; InputStream in = null; try { // boolean bool = file.exists() ? true : file.createNewFile(); // System.out.println("create New File:" + bool); if (!bool) { error = "createNewFile.Error,:-("; return false; } // out = new BufferedOutputStream(new FileOutputStream(file)); in = zf.getInputStream(ze); // long len = IOUtils.copy(in, out); // System.out.println("make New File length:" + len); return true; } catch (Exception e) { error = "makeFile.Error:(" + e.getLocalizedMessage() + ")"; return false; } finally { IOUtils.quietCloseOutputStream(out); IOUtils.quietCloseInputStream(in); } } }
参考资料:
http://www.cnblogs.com/CUCmehp/archive/2008/10/28/1320872.html
http://hi.baidu.com/yezongbo/blog/item/1b7960fd6aae661308244d14.html
除了Java平台本身提供的API之外,我还发现一个开源的项目,Truezip (http://truezip.java.net/)有机会再研究吧。
附件是两个助手类文件,可下载使用(将.png后缀删除即可):
ZipHelper.java 、UnZipHelper.java
发表评论
-
Quartz学习笔记
2011-11-15 16:02 0Quartz学习笔记 Quartz是Java平台上,优 ... -
从Eclipse向IDEA10迁移简记
2011-10-16 21:29 0从Eclipse向IDEA10迁移简记 ... -
读书笔记——《Head_First_设计模式》
2011-04-28 16:34 0读书笔记——《Head_First ... -
Java断言——assert
2011-04-26 15:58 3465Java断言——assert Java从1.4 ... -
Java中的Classloader
2011-08-21 18:57 1041Java中的Classloader简介 Java程序并 ... -
Java内存及GC的基础知识备忘
2009-04-01 11:20 13801、堆与栈 在Java中除 ... -
commons-loggin 与 slf4j的桥接器
2008-11-12 15:32 1776选择使用SLF4j + Logback后,像Spring等还未 ... -
请帮我找到这个丢失的字符,又是字符集的问题~?
2007-12-01 11:48 1010我上一个帖子 询问 在dom4j解析xml时无法正确识别字符 ... -
dom4j解析xml文档时的字符引用(数字实体)问题及初步分析。
2007-11-30 14:45 2221xml中的字符引用 也叫 ... -
请教 XML解析时实体引用的问题
2007-11-28 15:11 2289请教 XML解析时实体引用的问题 我的XML文件有如下的内容片 ... -
CXF在Spring2.5中出现错误,及解决方法。
2007-11-24 14:36 1432当在Spring2.5中启用基于标注配置Bean时(即开启以下 ... -
CXF (不依赖Spring)怎么在tomcat下部署Service??
2007-11-24 14:28 3026CXF说实现了JAX-WS2.0规范,那么在不借助于Sprin ... -
实录Seam示例程序Booking学习过程02
2007-09-28 10:58 2093实录Seam示例程序Booking学习过程02 上 ... -
实录Seam示例程序Booking学习过程
2007-09-26 15:57 3518实录Seam示例Booking学习 ... -
在Tomcat中使用EmbeddedJBoss
2007-09-24 11:35 3079新发布的Seam2.0 使用了 ...
相关推荐
java 操作Zip文件(压缩、解压、加密) zip4j-1.3.2.jar ant-1.10.6.jar
Java操作zip压缩格式的开源项目,功能强大而且使用方便,能完全满足Java操作Zip压缩文件,官方网址为:http://www.lingala.net/zip4j/ 可以下载到jar包、源码和示例,好像没有提供API文档。 不过需要使用代理访问...
winszipaes是Java语言下的ZIP文件操作接口,支持密码,但源代码不支持中文,该jar包对源码作了一点修改,使其支持中文,修改信息可以参照我的博客http://blog.csdn.net/zhangyihui1986/article/details/7724229或者...
Java调用Zip类批量压缩多个文件,此前有一个是压缩单个文件,也可参考,相关代码中可找到此源码。 public class ZipDemo extends JFrame{ JFileChooser fileChooser; //文件选择器 JList fileList; //待...
java读取共享文件使用jcifs-1.1.11.jar 的SmbFile 类使用用户名密码共享目录操作共享文件
Java操作zip压缩格式的开源项目,功能强大而且使用方便,能完全满足Java操作Zip压缩文件,官方网址为:http://www.lingala.net/zip4j/ 可以下载到jar包、源码和示例,好像没有提供API文档。 不过需要使用代理访问...
Java实现Zip压缩文件操作的工具类 文章介绍:https://blog.csdn.net/rongbo91/article/details/117747042 (可作为Jar依赖包直接使用) 1、项目使用前,请进入rdc-bom目录下,执行mvn clean install命令 2、可...
包含了使用的jar包,以及一个Java类,实现了使用Java对多目录多层级的文件进行打包,以及对ZIP包进行解压缩的操作。
用Java压缩解压ZIP文件,将利用java.util.zip 包中提供的类来实现压缩和解压zip 格式文件的功能。当然,本例在功能上完全没有Winzip 等成熟的压缩软件那么强,也不能做的很强,本例仅仅是演示如何来使用java.util....
java将文件压缩成ZIP格式的工具包API,操作简捷。 内附源码、文档、用例。
java控制指定路径下的文件夹压缩成zip格式。 java将zip压缩包解压成为文件夹。直接使用io流进行操作,不需借助其他jar包
JAVA 文件常用流操作 包括文件的创建,重命名,删除等等。字节流,字符流,缓存流,数据流,打印流,内存流等等
java,Blob字段操作,将图片或者文件保存到数据库中.zip
winzipaes是Java操作ZIP文件的开源项目,支持密码,但不支持中文文件名。此文件中包含修改后的源码、文档、jar包、依赖包及示例程序等文件。可以压缩文件名含有中文的文件,修改之处可以参看博客...
Java解压缩文件,并以ZIP格式压缩文件,主要是使用java.util.zip 包中的类来实现解压、压缩文件功能,如果你对这个类并不太熟悉,你正好可以参考一下这个类是如何用的。
winzipaes是Java操作ZIP文件的开源项目,支持密码,但不支持中文文件名,此文件中是修改后的源码,可以压缩文件名含有中文的文件,修改之处可以参看博客http://xjlsgcjdtc.iteye.com/blog/1439514或者...
这几天做一个操作docx的文档的功能,Apache的POI功能有限,就参考了相关资料写的一个Java压缩和解压zip文件的工具类。全部使用的是Java自带的类库,中文的文件名会有乱码的问题。
java操作压缩文件和解压文件实例代码(经测试),把jar文件导入到项目里。源代码根据注释修改下路径就能用了
jsp页面上传zip压缩包,用ajax传给后端进行解压,创建路径解压存储在本地,然后程序调用解压后的文件进行操作,然后再删除临时文件夹。用于批量导入信息和照片,然后ajax回调导入结果返回前端显示给操作者。
一、 流的分类 • 按数据流动方向 – 输入流:只能从中读取字节数据,而不能向其写出数据 – 输出流:只能向其写入字节数据,而不能从中读取数据 • 按照流所处理的数据类型 – 字节流:用于处理字节数据。...