zl程序教程

您现在的位置是:首页 >  后端

当前栏目

apacheant进行zip解压缩操作示例分享

zip 操作 示例 进行 分享 解压缩
2023-06-13 09:15:17 时间

需要导入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/");
 }
}