【背景】

近日在研究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. JavaScript--输出内容(document.write)

    document.write() 可用于直接向 HTML 输出流写内容.简单的说就是直接在网页中输出内容. 第一种:输出内容用“”括起,直接输出""号内的内容. <scrip ...

  2. ASP.NET MVC5 之数据迁移

    SQL 中新建数据库 DataSystem 1.web.config 数据库连接字符串: <add name="APPDataConnection" connectionSt ...

  3. Laravel5.1学习笔记13 系统架构5 Contract

    Contract 简介 为什么要用 Contract? Contract 参考 如何使用 Contract 简介 Laravel 中的 Contract 是一组定义了框架核心服务的接口.例如,Illu ...

  4. python--9、进程池

    concurrent.futures模块 进程池中的进程是固定的,若是池中有任务结束后,等待的任务进来后由空闲的进程来处理. 导入方法三连发: from 标题的模块 import 如下:Process ...

  5. [Windows Server 2008] ASP.net安装方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:安装ASP.n ...

  6. [Windows Server 2012] Filezilla安装方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:FileZ ...

  7. linux 汇编 - 函数调用

    Linux 汇编-函数调用 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !i ...

  8. Nginx 监控

    编译安装加上http_stub_status_module 模块 location /status { stub_status on; # access_log /usr/local/nginx/lo ...

  9. Please, commit your changes or stash them before you can merge

    参照 : https://blog.csdn.net/iefreer/article/details/7679631 用git pull来更新代码的时候,遇到了下面的问题: error: Your l ...

  10. 可以用作javascript异步模式的函数写法

    1. 回调函数 f1(); f2(); function f1(callback) { setTimeout(function() { // f1的任务代码 callback(); }, 1000); ...