- 浏览: 249856 次
- 性别:
- 来自: 苏州
文章分类
- 全部博客 (289)
- java (72)
- oracle (3)
- mysql (5)
- spring (28)
- hibernate (2)
- osgi (0)
- linux (2)
- ExtJs (1)
- jvm (0)
- mybatis (7)
- 分布式 (11)
- MINA (6)
- apache+tomcat (13)
- js+htm (7)
- android (44)
- http (1)
- hbase+hdoop (0)
- memcache (13)
- search (27)
- 部署及性能 (12)
- mongoDB (2)
- 多线程 (12)
- 安全管理验证 (9)
- struts (1)
- webservice (0)
- easyUI (1)
- spring security (16)
- pattern (6)
- 算法 (2)
最新评论
-
lzh8189146:
CommonsHttpSolrServer这个类,现在是不是没 ...
CommonsHttpSolrServer -
xiaochanzi:
我按照你的方法试了下,tomcat6可以发布,但是访问任何网页 ...
基于内嵌Tomcat的应用开发 -
phoneeye:
麻烦你,如果是抄来的文章,请给出来源。谢谢
ant 两则技巧 -
neverforget:
转载不注明出处
Spring Security3.1登陆验证 替换 usernamepasswordfilter -
liang1022:
若不使用eclipse ,如何在命令行下 运行服务端程序 ?
WebService CXF学习(入门篇2):HelloWorld
- /**
- *
- */
- package cn.edu.calis.zipfiletest;
- 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 org.apache.tools.zip.ZipEntry;
- import org.apache.tools.zip.ZipFile;
- import org.apache.tools.zip.ZipOutputStream;
- /**
- * 利于ant包来压缩文件 可对空文件夹进行压缩<br>
- *
- * 注:压缩文件的关键步骤在于对文件的读取,存放的时特别要注意文件的路径,最好的方式用相对路径,
- *
- *
- * @author Administrator
- *
- */
- public class AntZipFile4 {
- boolean flag = true; // 压缩成功与否标志
- private ZipFile zipFile;
- private ZipEntry zipEntry;
- private ZipOutputStream zipOut; // 压缩Zip
- private static int bufSize; // size of bytes
- private byte[] buf;
- private int readedBytes;
- public AntZipFile4() {
- this(1024);
- }
- public AntZipFile4(int size) {
- this.bufSize = size;
- this.buf = new byte[this.bufSize];
- }
- public boolean doZip(String srcStr, String targetStr, String targetName) {
- File zipDir = new File(srcStr);
- // 未指定压缩文件名,默认为"ZipFile"
- if (targetName == null || "".equals(targetName))
- targetName = "ZipFile";
- // 添加".zip"后缀
- if (!targetName.endsWith(".zip"))
- targetName += ".zip";
- try {
- this.zipOut = new ZipOutputStream(new FileOutputStream(new File(
- targetStr + File.separator + targetName)));
- // 压缩文件
- compressFile(zipDir, "", zipOut);
- zipOut.close();
- } catch (IOException e) {
- flag = false;
- e.printStackTrace();
- }
- return flag;
- }
- /**
- * 压缩目录(包含空目录)与文件
- *
- * @param srcFile
- * 待压缩的目录与文件
- * @param oppositePath
- * 压缩的相对路径,用于按照原文件的目录结构压缩文件
- * @param zipOut
- * ZipOutputStream压缩流
- * @throws IOException
- */
- public void compressFile(File srcFile, String oppositePath,
- ZipOutputStream zipOut) throws IOException {
- /*
- * 因为是空目录,所以要在结尾加一个"/"。 不然就会被当作是空文件。 ZipEntry的isDirectory()方法中,目录以"/"结尾.
- * org.apache.tools.zip.ZipEntry : public boolean isDirectory() { return
- * getName().endsWith("/"); }
- */
- if (srcFile.isDirectory()) {// 为目录时
- String newOppositePath = oppositePath + srcFile.getName() + "/"; // 得到相对路径
- compressFolder(srcFile, newOppositePath, zipOut);
- }
- // 如果是文件则需读;如果是空目录则无需读,直接转到zipOut.closeEntry()。
- if (srcFile.isFile()) {// 为文件时
- ZipEntry zipe = new ZipEntry(oppositePath + srcFile.getName());
- try {
- zipOut.putNextEntry(zipe);
- FileInputStream fileIn = new FileInputStream(srcFile);
- while ((this.readedBytes = fileIn.read(this.buf)) > 0) {
- zipOut.write(this.buf, 0, this.readedBytes);
- }
- fileIn.close();
- zipOut.closeEntry();
- } catch (Exception e) {
- }
- }
- }
- /**
- * 递归完成目录文件读取,由compressFile()方法调用
- *
- * @param file
- * 要读取的文件目录
- * @param oppositePath
- * 相对路径,用于按照原文件的目录结构压缩文件
- * @param zipOut
- * ZipOutputStream压缩流
- * @throws IOException
- */
- private void compressFolder(File file, String oppositePath,
- ZipOutputStream zipOut) throws IOException {
- File[] files = file.listFiles();
- if (files.length == 0) {// 如果目录为空,则单独压缩空目录。
- String fileName = files.toString();
- /*
- * 因为是空目录,所以要在结尾加一个"/"。 不然就会被当作是空文件。
- * ZipEntry的isDirectory()方法中,目录以"/"结尾. org.apache.tools.zip.ZipEntry :
- * public boolean isDirectory() { return getName().endsWith("/"); }
- * 这里oppositePath已经为相对路径,故zipOut.putNextEntry(new
- * ZipEntry(oppositePath));
- */
- if (file.isDirectory())
- fileName = fileName + "/";// 此处不能用"\\"
- zipOut.putNextEntry(new ZipEntry(oppositePath));
- // 如果是文件则需读;如果是空目录则无需读,直接转到zipOut.closeEntry()。
- if (file.isFile()) {
- FileInputStream fileIn = new FileInputStream(file);
- while ((this.readedBytes = fileIn.read(this.buf)) > 0) {
- zipOut.write(this.buf, 0, this.readedBytes);
- }
- fileIn.close();
- }
- zipOut.closeEntry();
- } else
- // 如果目录不为空,则分别处理目录和文件
- for (File subfile : files) {
- compressFile(subfile, oppositePath, zipOut);
- }
- }
- /**
- * 解压指定zip文件
- *
- * @param srcZipFile
- * 待解压的文件(包含路径)
- * @param targetStr
- * 解压存放的路径
- * @param targetName
- * 解压的存放的目录
- */
- public void upZip(String srcZipFile, String targetStr, String targetName) {
- FileOutputStream fileOut;
- File file;
- InputStream inputStream;
- try {
- this.zipFile = new ZipFile(srcZipFile);
- // 遍历每个压缩文件
- for (Enumeration entries = this.zipFile.getEntries(); entries
- .hasMoreElements();) {
- zipEntry = (ZipEntry) entries.nextElement();
- file = new File(targetStr + "/" + targetName + "/"
- + zipEntry.getName());
- if (zipEntry.isDirectory()) {// 是目录,则创建之
- file.mkdirs();
- } else {// 是文件
- // 如果指定文件的父目录不存在,则创建之
- File parent = file.getParentFile();
- if (null == parent || !parent.exists()) {
- parent.mkdirs();
- }
- // 写文件
- inputStream = zipFile.getInputStream(zipEntry);
- fileOut = new FileOutputStream(file);
- while ((this.readedBytes = inputStream.read(this.buf)) > 0) {
- fileOut.write(this.buf, 0, this.readedBytes);
- }
- fileOut.close();
- inputStream.close();
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 设置压缩或解压时缓冲区大小。
- *
- * @param bufSize
- * 缓冲区大小
- */
- public static void setBufSize(int bufSize) {
- AntZipFile4.bufSize = bufSize;
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- AntZipFile4 test = new AntZipFile4();
- test.doZip("D:/乱七八糟", "E:/乱七八糟的", "aa.zip");
- test.upZip("E:/乱七八糟的/aa.zip", "f:/", "aa");
- }
- }
上面的注释写的很说细,应该能看明白的。
下面说说学习的心得吧:
1,为什么我会选择apache提代的ant包来进行是压缩文件,其实java.util.zip包下也提供了压缩的类,但我没有先用,因为它对中文的支持不是很好,中文时会出现乱码,当然你可以修改源码后就可以了,而apache提代的ant包可以解决中文问题,也可以自己设定字符编码:
ZipOutputStream类的setEncoding("")方法
2,无论你选择何种包来压缩文件,压缩文件时,文件的路径都很重要,特别是当文件存在多级的情况下,一般你所写的文件中有递归方法,而递归方法在调用时,文件的路径需要很好的去把握。
好了,我只是在这抛砖引玉,欢迎大家叫板。
下面提供了源码
发表评论
-
eclipse 生成 ant 文件
2012-04-14 19:13 794选择你要生成Build.xml文件的项目,右键 ... -
ant 实例操作
2012-03-30 08:36 527ant 实例操作 做互联网应用的各位tx都已经 ... -
ant 两则技巧
2012-03-30 08:36 973ant 两则技巧 下面两则技巧以前没有用过,在 ... -
Ant实战篇 (二)
2012-03-30 08:36 723Ant实战篇 (二) Ant 实战篇 (二) ... -
Ant实战篇 (一)
2012-03-30 08:36 687Ant实战篇 (一) Ant 实战篇 (一) ... -
Ant简介 (标签)
2012-03-30 08:36 707Ant简介 (标签) Ant的概念 ... -
ant
2012-04-01 08:21 768ant 1 Ant是什么? Apache An ... -
我的平台功能介绍
2012-03-07 08:18 577我的平台功能介绍 分类: 项目总结 J ... -
web负载均衡小记
2012-03-05 08:25 721web负载均衡小记 分类: 计算机基础 ... -
大型高负载的网站的体系结构和web2.0时代的网站高负载
2012-02-29 10:22 765大型高负载的网站的体系结构和web2.0时代的网站 ... -
ant 学习
2012-02-12 19:43 653原文地址:http://tech.it ...
相关推荐
apache-ant-1.9.16-bin.zip
apache-ant.jar包
apache-ant-1.8.2jar 包。apache-ant所需包
apache-ant-1.6.5-bin ant编译
apache-ant-1.7.1apache-ant-1.7.1apache-ant-1.7.1apache-ant-1.7.1apache-ant-1.7.1apache-ant-1.7.1apache-ant-1.7.1apache-ant-1.7.1apache-ant-1.7.1apache-ant-1.7.1apache-ant-1.7.1apache-ant-1.7.1apache-...
apache-ant-1.7.0 apache-ant-1.7.0 apache-ant-1.7.0 apache-ant-1.7.0 apache-ant-1.7.0
apache-ant-1.10.12-bin.tar.gz
apache-ant-zip.jar 解决中文乱码解压问题
apache-ant安装包apache-ant安装包apache-ant安装包apache-ant安装包
apache-ant-1.9.2-bin.zip
apache-ant-zip.jar包及用法 用过java做压缩或解压的都知道,jdk提供的zip只能按UTF-8格式处理,所有jdk提供的zip不能支持中文文件名,可以采用Apache的zip包解决中文文件名问题。
在系统环境变量中新建一个ANT_HOME 的变量,值为D:\apache-ant-1.10.1。在path 变量中添加 %ANT_HOME%\bin; 如果没有path变量,则新建一个。 3、验证ant环境配置。开始-->运行-->输入cmd,打开dos命令窗口。...
ant.jar。里面包含apache的zip文件处理包。
有的小伙伴在离线VS2019安装包的时候会遇到一个包下载失败,就是这个apache-ant-1.9.3-bin.zip,下载以后解压,把apache-ant-1.9.3-bin.zip放到VS2019的缓存目录里就可以了。
apache-ant-1.9.2 源码,内容详实,情节丰富,欢迎下载。
Apache-ant-1.9.2
Apache-ANT完美详解 Apache-ANT完美详解 Apache-ANT完美详解
apache-ant-1.10.12-bin.tar.bz2