【背景】

近日在研究web邮件下载功能,下载的邮件能够导入foxmail邮件client。可是批量下载邮件还需将邮件打成一个压缩包。

从网上搜索通过java实现文件压缩、解压缩有非常多现成的样例。

【參考代码】(须要下载apache ant.jar包)

import java.io.File;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet; public class Zipper {
public final static String encoding = "GBK"; // 压缩
public static void zip(String srcPathname, String zipFilepath)
throws BuildException, RuntimeException {
File file = new File(srcPathname);
if (!file.exists())
throw new RuntimeException("source file or directory "
+ srcPathname + " does not exist."); Project proj = new Project();
FileSet fileSet = new FileSet();
fileSet.setProject(proj);
// 推断是文件夹还是文件
if (file.isDirectory()) {
fileSet.setDir(file);
// ant中include/exclude规则在此都能够使用
// 比方:
// fileSet.setExcludes("**/*.txt");
// fileSet.setIncludes("**/*.xls");
} else {
fileSet.setFile(file);
} Zip zip = new Zip();
zip.setProject(proj);
zip.setDestFile(new File(zipFilepath));
zip.addFileset(fileSet);
zip.setEncoding(encoding);
zip.execute();
} // 解压缩
public static void unzip(String zipFilepath, String destDir)
throws BuildException, RuntimeException {
if (!new File(zipFilepath).exists())
throw new RuntimeException("zip file " + zipFilepath
+ " does not exist."); Project proj = new Project();
Expand expand = new Expand();
expand.setProject(proj);
expand.setTaskType("unzip");
expand.setTaskName("unzip");
expand.setEncoding(encoding); expand.setSrc(new File(zipFilepath));
expand.setDest(new File(destDir));
expand.execute();
}
}

java代理使用 apache ant实现文件压缩/解压缩的更多相关文章

  1. 使用 apache ant 轻松实现文件压缩/解压缩(转)

    原文地址:http://blog.csdn.net/irvine007/article/details/6779492 maven配置ant包: <dependency> <grou ...

  2. Java实现对zip和rar文件的解压缩

    通过java实现对zip和rar文件的解压缩

  3. ubuntu下文件压缩/解压缩

    ubuntu下文件压缩/解压缩 http://blog.csdn.net/luo86106/article/details/6946255 .gz 解压1:gunzip FileName.gz 解压2 ...

  4. 前端部署ant+yuicompressor文件压缩+获取版本+SSH公布(部分代码)

    文件压缩: <apply executable="java" parallel="false" failonerror="true" ...

  5. AntZipUtils【基于Ant的Zip压缩解压缩工具类】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 Android 压缩解压zip文件一般分为两种方式: 基于JDK的Zip压缩工具类 该版本存在问题:压缩时如果目录或文件名含有中文, ...

  6. ubuntu下文件压缩/解压缩命令总结

    .gz 解压1:gunzip FileName.gz 解压2:gzip -d FileName.gz 压缩:gzip FileName .tar.gz 解压:tar zxvf FileName.tar ...

  7. Shell命令-文件压缩解压缩之tar、unzip

    文件及内容处理 - tar.unip 1.tar:打包压缩命令 tar命令的功能说明 tar 命令常用语用于备份文件,tar 是用来建立,还原备份文件的工具程序,它可以加入,解开备份文件内的文件 ta ...

  8. Shell命令-文件压缩解压缩之gzip、zip

    文件及内容处理 - gzip.zip 1.gzip:gzip压缩工具 gzip命令的功能说明 gzip 命令用于压缩文件.gzip 是个使用广泛的压缩程序,文件经它压缩过后,其名称后面会多出 .gz ...

  9. Android zip文件压缩解压缩

    DirTraversal.java <P style="TEXT-ALIGN: left; PADDING-BOTTOM: 0px; WIDOWS: 2; TEXT-TRANSFORM ...

随机推荐

  1. 将电脑浏览器User-Agent识别改成手机浏览器UA几种简单方法

    第一种方法:修改浏览器的快捷方式 右击桌面上的Chrome浏览器图标,在弹出的右键菜单中选择“复制”,复制一个图标副本到桌面.右击该副本,选择“属性”,打开相应的对话框,在“目标”文本框的字符后面添加 ...

  2. Android项目实战_手机安全卫士手机防盗界面

    #安全卫士手机防盗# ###1.Activity的任务栈 1.类似一个木桶,每层只能放一个木块,我们放入木块和取出木块的时候只能从最上面开始操作 ###2.Android中的坐标系![](http:/ ...

  3. Android集成二维码扫描功能

    文章转载自  https://github.com/yipianfengye/android-zxingLibrary 在具体介绍该扫描库之前我们先看一下其具体的使用方式,看看是不是几行代码就可以集成 ...

  4. JS——null

    变量被赋值为null,目的往往是为了销毁这个对象: var n1 = 1; n1 = null;

  5. dotnetnuke 添加用户属性 Profile

    if (DotNetNuke.Entities.Profile.ProfileController.GetPropertyDefinitionByName(this.PortalId, "Q ...

  6. C# textbox 获得焦点

    this.ActiveControl = txt_core;

  7. mvc EF 出现异常, 能提示出那个字段出现问题

    } catch (DbEntityValidationException ex) { message = "添加异常"; desc = " {" + ex.Me ...

  8. vi 命令学习(一)

    行内移动] w word 向后移动一个单词 b back ·向前移动一个单词 行首 ^ 行首,第一个不是空白字符的位置 $ 行尾 [行数移动] gg go 文件顶部 G go 文件末尾 数字gg go ...

  9. 关于WEB开发下面DIV层被OCX控件拦住问题

    控件分为有窗口控件与无窗口控件,无窗口控件很好办,如flash控件,可以通过添加wmode属性来解决挡住DIV层这个问题,添加的代码如下: 解决无窗口控件挡住DIV: 1 <param  nam ...

  10. python str操作

    1. str.format():使用“{}”占位符格式化字符串(占位符中的索引号形式和键值对形式可以混合使用). 1 >>> string = 'python{}, django{} ...