apacheant进行zip解压缩操作示例分享
需要导入ant.jar包,apache网站(http://ant.apache.org/bindownload.cgi)下载即可。
importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.util.zip.ZipOutputStream;
importorg.apache.tools.ant.Project;
importorg.apache.tools.ant.taskdefs.Expand;
importorg.apache.tools.zip.ZipEntry;
importcom.xyq.io.util.CloseIoUtil;
publicclassZipUtil{
privatestaticfinalStringENCODE="UTF-8";
publicstaticvoidzip(StringinputFilePath,StringzipFileName){
FileinputFile=newFile(inputFilePath);
if(!inputFile.exists())
thrownewRuntimeException("原始文件不存在!!!");
FilebasetarZipFile=newFile(zipFileName).getParentFile();
if(!basetarZipFile.exists()&&!basetarZipFile.mkdirs())
thrownewRuntimeException("目标文件无法创建!!!");
BufferedOutputStreambos=null;
FileOutputStreamout=null;
ZipOutputStreamzOut=null;
try{
//创建文件输出对象out,提示:注意中文支持
out=newFileOutputStream(newString(zipFileName.getBytes(ENCODE)));
bos=newBufferedOutputStream(out);
//?⑽募??出ZIP输出流接起来
zOut=newZipOutputStream(bos);
zip(zOut,inputFile,inputFile.getName());
CloseIoUtil.closeAll(zOut,bos,out);
}catch(Exceptione){
e.printStackTrace();
}
}
privatestaticvoidzip(ZipOutputStreamzOut,Filefile,Stringbase){
try{
//如果文件句柄是目录
if(file.isDirectory()){
//获取目录下的文件
File[]listFiles=file.listFiles();
//建立ZIP条目
zOut.putNextEntry(newZipEntry(base+"/"));
base=(base.length()==0?"":base+"/");
if(listFiles!=null&&listFiles.length>0)
//遍历目录下文件
for(Filef:listFiles)
//递归进入本方法
zip(zOut,f,base+f.getName());
}
//如果文件句柄是文件
else{
if(base==""){
base=file.getName();
}
//填入文件句柄
zOut.putNextEntry(newZipEntry(base));
//开始压缩
//从文件入流读,写入ZIP出流
writeFile(zOut,file);
}
}catch(Exceptione){
e.printStackTrace();
}
}
privatestaticvoidwriteFile(ZipOutputStreamzOut,Filefile)
throwsIOException{
FileInputStreamin=null;
BufferedInputStreambis=null;
in=newFileInputStream(file);
bis=newBufferedInputStream(in);
intlen=0;
byte[]buff=newbyte[2048];
while((len=bis.read(buff))!=-1)
zOut.write(buff,0,len);
zOut.flush();
CloseIoUtil.closeAll(bis,in);
}
/****
*解压
*
*@paramzipPath
* zip文件路径
*@paramdestinationPath
* 解压的目的地点
*@paramecode
* 文件名的编码字符集
*/
publicstaticvoidunZip(StringzipPath,StringdestinationPath){
FilezipFile=newFile(zipPath);
if(!zipFile.exists())
thrownewRuntimeException("zipfile"+zipPath
+"doesnotexist.");
Projectproj=newProject();
Expandexpand=newExpand();
expand.setProject(proj);
expand.setTaskType("unzip");
expand.setTaskName("unzip");
expand.setSrc(zipFile);
expand.setDest(newFile(destinationPath));
expand.setEncoding(ENCODE);
expand.execute();
System.out.println("unzipdone!!!");
}
publicstaticvoidmain(String[]args){
Stringdir=newString("F:\\我的备份\\文档\\MyEclipse+9.0正式版破解与激活(亲测可用)");
dir=newString("F:/111.JPG");
zip(dir,"f:/BZBXB/zipant.zip");
unZip("f:/BZBXB/zipant.zip","f:/XX/xx/");
}
}
相关文章
- 使用ant提供的zip进行压缩和解压缩,支持中文详解编程语言
- Java操作zip压缩和解压缩文件工具类详解编程语言
- Linux安装Zip:快速且易操作(linux怎么安装zip)
- 文件Linux解压实战:文件ZIP的解压技巧(linux的解压zip)
- Linux文件压缩成Zip格式(linux压缩成zip)
- 安装Zip版MySQL: 走上正确的路(安装zip版mysql)
- Linux安装Zip包的简单步骤(linux安装zip包)
- Linux压缩工具:Zip打包操作(zip打包linux)
- Linux 下载解压 ZIP 文件的方法(zip解压linux)
- MySQL5.6的稳定升级:zip压缩包来袭(mysql5.6zip)
- Linux解压Zip:一步到位(linux 解压zip)
- 探索Linux查看ZIP文件内容的方法(linux查看zip内容)
- Linux解压zip文件快速简易指令:让你轻轻松松搞定!(linux解压zip文件命令)
- MySQL的ZIP配置简单易学(mysql zip 配置)
- MySQL使用zip压缩包进行运行(mysql zip 运行)
- 用Mysql轻松压缩和解压Zip网址(mysql zip网址)
- 轻松搭建MySQL Zip版的环境配置教程(mysql zip版配置)
- MySQL ZIP 版简单实用的数据库安装方式(mysql zip 版)
- MySQL怎样使用Zip文件(mysql zip怎么用)
- .net自带的库生成zip文件的方法
- php实现zip压缩文件解压缩代码分享(简单易懂)
- IIS、Apache、Nginx下用IE下载apk文件变成zip的解决办法
- ThinkPHP实现将本地文件打包成zip下载