当前位置: 移动技术网 > IT编程>开发语言>Java > Java压缩解压zip技术_动力节点Java学院整理

Java压缩解压zip技术_动力节点Java学院整理

2019年07月22日  | 移动技术网IT编程  | 我要评论

java解压缩zip - 多个文件(包括文件夹),对多个文件和文件夹进行压缩,对复杂的文件目录进行解压。压缩方法使用的是可变参数,可以压缩1到多个文件..可以写数组的方式或者一个个写到参数列表里面...

 zipfiles(zip,"abc",new file("d:/english"),new file("d:/发放数据.xls")); 

测试文件目录结构: 

http://www.lhsxpumps.com/_images4/10qianwan/20190722/b_0_201907221018008315.png

测试的压缩内容:english文件夹和同级的两个excel文件

 file[] files = new file[]{new file("d:/english"),new file("d:/发放数据.xls"),new file("d:/中文名称.xls")}; 

下面是压缩的代码:

 /** 
  * 压缩文件-由于out要在递归调用外,所以封装一个方法用来 
  * 调用zipfiles(zipoutputstream out,string path,file... srcfiles) 
  * @param zip 
  * @param path 
  * @param srcfiles 
  * @throws ioexception 
  * 
  */ 
  public static void zipfiles(file zip,string path,file... srcfiles) throws ioexception{ 
   zipoutputstream out = new zipoutputstream(new fileoutputstream(zip)); 
   ziptest.zipfiles(out,path,srcfiles); 
   out.close(); 
   system.out.println("*****************压缩完毕*******************"); 
  } 
  /** 
  * 压缩文件-file 
  * @param zipfile zip文件 
  * @param srcfiles 被压缩源文件 
  * 
  */ 
  public static void zipfiles(zipoutputstream out,string path,file... srcfiles){ 
   path = path.replaceall("\\*", "/"); 
   if(!path.endswith("/")){ 
    path+="/"; 
   } 
   byte[] buf = new byte[1024]; 
   try { 
    for(int i=0;i<srcfiles.length;i++){ 
     if(srcfiles[i].isdirectory()){ 
      file[] files = srcfiles[i].listfiles(); 
      string srcpath = srcfiles[i].getname(); 
      srcpath = srcpath.replaceall("\\*", "/"); 
      if(!srcpath.endswith("/")){ 
       srcpath+="/"; 
      } 
      out.putnextentry(new zipentry(path+srcpath)); 
     zipfiles(out,path+srcpath,files); 
     } 
     else{ 
      fileinputstream in = new fileinputstream(srcfiles[i]); 
      system.out.println(path + srcfiles[i].getname()); 
      out.putnextentry(new zipentry(path + srcfiles[i].getname())); 
      int len; 
      while((len=in.read(buf))>0){ 
       out.write(buf,0,len); 
      } 
      out.closeentry(); 
      in.close(); 
     } 
   } 
   } catch (exception e) { 
    e.printstacktrace(); 
   } 
 } 

在压缩的时候,针对文件夹进行判断,然后递归压缩文件。

然后是解压:

/** 
  * 解压到指定目录 
  * @param zippath 
  * @param descdir 
  * 
  */ 
  public static void unzipfiles(string zippath,string descdir)throws ioexception{ 
   unzipfiles(new file(zippath), descdir); 
  } 
  /** 
  * 解压文件到指定目录 
  * @param zipfile 
  * @param descdir 
  * 
  */ 
  @suppresswarnings("rawtypes") 
  public static void unzipfiles(file zipfile,string descdir)throws ioexception{ 
   file pathfile = new file(descdir); 
   if(!pathfile.exists()){ 
    pathfile.mkdirs(); 
   } 
  zipfile zip = new zipfile(zipfile); 
   for(enumeration entries = zip.getentries();entries.hasmoreelements();){ 
    zipentry entry = (zipentry)entries.nextelement(); 
    string zipentryname = entry.getname(); 
    inputstream in = zip.getinputstream(entry); 
    string outpath = (descdir+zipentryname).replaceall("\\*", "/");; 
    //判断路径是否存在,不存在则创建文件路径 
    file file = new file(outpath.substring(0, outpath.lastindexof('/'))); 
    if(!file.exists()){ 
     file.mkdirs(); 
    } 
    //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压 
    if(new file(outpath).isdirectory()){ 
     continue; 
    } 
    //输出文件路径信息 
    system.out.println(outpath); 
    
    outputstream out = new fileoutputstream(outpath); 
    byte[] buf1 = new byte[1024]; 
    int len; 
    while((len=in.read(buf1))>0){ 
     out.write(buf1,0,len); 
    } 
    in.close(); 
    out.close(); 
    } 
   system.out.println("******************解压完毕********************"); 
  } 

解压的时候,针对文件夹判断创建不存在的文件夹,对文件夹只创建,不进行解压..因为解压是针对文件的,不是文件夹,文件夹需要自己创建。

测试方法:

 public static void main(string[] args) throws ioexception { 
   /** 
   * 压缩文件 
   */ 
   file[] files = new file[]{new file("d:/english"),new file("d:/发放数据.xls"),new file("d:/中文名称.xls")}; 
   file zip = new file("d:/压缩.zip"); 
   zipfiles(zip,"abc",files); 
   
   /** 
   * 解压文件 
   */ 
   file zipfile = new file("d:/压缩.zip"); 
   string path = "d:/zipfile/"; 
   unzipfiles(zipfile, path); 
  } 

测试方法并没有对异常做任何处理,这是不对的,请不要模仿。

输出结果:

 abc/english/templete.xls 
 abc/english/中文/bjpowernode/isea/533/abc/templete.xls 
 abc/english/中文/bjpowernode/isea/533/abc/zipfile2/templete.xls 
 abc/english/中文/bjpowernode/isea/533/abc/zipfile2/zipfile/abc/templete.xls 
 abc/english/中文/bjpowernode/isea/533/abc/zipfile2/zipfile/abc/zipfile2/templete.xls 
 abc/english/中文/bjpowernode/isea/533/abc/zipfile2/zipfile/abc/zipfile2/领卡清单.xls 
 abc/english/中文/bjpowernode/isea/533/abc/zipfile2/领卡清单.xls 
 abc/english/中文/bjpowernode/isea/templete.xls 
 abc/english/中文/bjpowernode/isea/领卡清单.xls 
 abc/english/中文/bjpowernode/templete.xls 
 abc/english/领卡清单.xls 
 abc/发放数据.xls 
 abc/中文名称.xls 
 *****************压缩完毕******************* 
 d:/zipfile/abc/中文名称.xls 
 d:/zipfile/abc/发放数据.xls 
 d:/zipfile/abc/english/领卡清单.xls 
 d:/zipfile/abc/english/中文/bjpowernode/templete.xls 
 d:/zipfile/abc/english/中文/bjpowernode/isea/领卡清单.xls 
 d:/zipfile/abc/english/中文/bjpowernode/isea/templete.xls 
 d:/zipfile/abc/english/中文/bjpowernode/isea/533/abc/templete.xls 
 d:/zipfile/abc/english/templete.xls 
 d:/zipfile/abc/english/中文/bjpowernode/isea/533/abc/zipfile2/templete.xls 
 d:/zipfile/abc/english/中文/bjpowernode/isea/533/abc/zipfile2/zipfile/abc/templete.xls 
 d:/zipfile/abc/english/中文/bjpowernode/isea/533/abc/zipfile2/zipfile/abc/zipfile2/templete.xls 
 d:/zipfile/abc/english/中文/bjpowernode/isea/533/abc/zipfile2/zipfile/abc/zipfile2/领卡清单.xls 
 d:/zipfile/abc/english/中文/bjpowernode/isea/533/abc/zipfile2/领卡清单.xls 
 ******************解压完毕******************** 

http://www.lhsxpumps.com/_images4/10qianwan/20190722/b_0_201907221018018256.jpg

以上所述是小编给大家介绍的java压缩解压zip技术,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网