当前位置: 移动技术网 > IT编程>开发语言>Java > java 解压与压缩文件夹的实例详解

java 解压与压缩文件夹的实例详解

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

 java 解压与压缩文件夹的实例详解

注意:jdk7支持设置编码设置编码格式 zipfile,zipinputstream,zipoutputstream都增加了编码格式,如果是jdk1.6需要其他的包辅助

下面为自带jdk压缩文件夹代码:

public void dozip(string srcfile, string zipfile) throws ioexception {
  string temp = "";
  file src = new file(srcfile);
  file zipfile=new file(zipfile);
  //判断要压缩的文件存不存在
  if (!src.exists()) {
    system.err.println("要压缩的文件不存在!");
    system.exit(1);
  }
  //如果说压缩路径不存在,则创建
  if (!zipfile.getparentfile().exists()) {
    zipfile.getparentfile().mkdirs();
//   system.out.println("创建ok");
  }
  // 封装压缩的路径
  bufferedoutputstream bos=new bufferedoutputstream(new fileoutputstream(zipfile));
  //这里可以加入校验
//checkedoutputstream cos = new checkedoutputstream(bos,new crc32());  
  //还可以设置压缩格式,默认utf-8
  charset charset = charset.forname("gbk"); 
  zipoutputstream zos = new zipoutputstream(bos,charset);
  zip(src, zos, temp);
  //关闭流
  zos.flush();
  zos.close();
  system.out.println("压缩完成!");
  system.out.println("压缩文件的位置是:" + zipfile);
// system.out.println("检验和:"+cos.getchecksum().getvalue());
  }

 private void zip(file file, zipoutputstream zos, string temp)
    throws ioexception {
  // 如果不加"/"将会作为文件处理,空文件夹不需要读写操作
  if (file.isdirectory()) {
    string str = temp + file.getname() + "/";
    zos.putnextentry(new zipentry(str));
    file[] files = file.listfiles();
    for (file file2 : files) {
    zip(file2, zos, str);
    }
  } else {
    // system.out.println("当前文件的父路径:"+temp);
    zipfile(file, zos, temp);
  }
  }

  private void zipfile(file srcfile, zipoutputstream zos, string temp)
    throws ioexception {
  // 默认的等级压缩-1
  // zos.setlevel(xxx);
  // 封装待压缩文件
  bufferedinputstream bis = new bufferedinputstream(new fileinputstream(
    srcfile));
  zos.putnextentry(new zipentry(temp + srcfile.getname()));

  byte buf[] = new byte[1024];
  int len;
  while ((len = bis.read(buf)) != -1) {
    zos.write(buf, 0, len);
  }
  //按标准需要关闭当前条目,不写也行
  zos.closeentry();
  bis.close();
  }

下面为解压:

这里先说一下好压的解压规则:

1.如果解压到与压缩文件同名的文件夹,则直接解压

如果自定义了其他文件夹xxx,则先创建xxx,再放入解压后的文件夹

2.好压压缩的时候,是采用gbk格式的,所以在解压的时候,为了统一,采用gbk解压另外再说一下winrar,因为rar压缩是申请了专利(商业软件),所以rar压缩算法是不公开的,但是解压算法是有的,其压缩默认也是gbk格式的;
经过测试,发现,不管压缩的时候采用utf-8还是gbk,解压的时候用gbk都可以正确解压!(具体原因还不清楚)

本java程序是直接解压到文件夹的,默认解压到与压缩文件同路径

如果解压编码有问题,则报错:java.lang.illegalargumentexception: malformed

如果压缩文件有密码:则报错:java.util.zip.zipexception: encrypted zip entry not supporte

//方法1:
public void unzip(string zipfile) throws ioexception {
  //检查是否是zip文件,并判断文件是否存在
  checkfilename(zipfile);
  long starttime = system.currenttimemillis();
  file zfile=new file(zipfile);
  //获取待解压文件的父路径
  string parent=zfile.getparent()+"/";
  fileinputstream fis=new fileinputstream(zfile);
  charset charset = charset.forname("gbk");//默认utf-8
// checkedinputstream cis = new checkedinputstream(fis,new crc32());
  zipinputstream zis = new zipinputstream(fis,charset);// 输入源zip路径
  zipentry entry=null;
  bufferedoutputstream bos=null;
  while ((entry=zis.getnextentry())!=null) {
    if (entry.isdirectory()) {
    file filepath=new file(parent+entry.getname());
    //如果目录不存在,则创建
    if (!filepath.exists()) {
      filepath.mkdirs();
    }
    }else{
    fileoutputstream fos=new fileoutputstream(parent+entry.getname());
    bos=new bufferedoutputstream(fos);
    byte buf[] = new byte[1024];
    int len;
    while ((len = zis.read(buf)) != -1) {
      bos.write(buf, 0, len);
    }
    zis.closeentry();
    //关闭的时候会刷新
    bos.close();
    }
  }
  zis.close();
  long endtime = system.currenttimemillis();
  system.out.println("解压完成!所需时间为:"+(endtime-starttime)+"ms");
// system.out.println("校验和:"+cis.getchecksum().getvalue());
  }

  private void checkfilename(string name) {
  //文件是否存在
  if (!new file(name).exists()) {
    system.err.println("要解压的文件不存在!");
    system.exit(1);
  }
  // 判断是否是zip文件
  int index = name.lastindexof(".");
  string str=name.substring(index+1);
  if (!"zip".equalsignorecase(str)) {
    system.err.println("不是zip文件,无法解压!");
    system.exit(1);
  } 
    }

方法2:

利用zipfile解压,方法跟zipinputstream类似,都是连续取到entry,然后再用entry判断,听说zipfile内建了缓冲流,所以对于同一个文件解压多次效率比zipinputstream高些

 public void dozip(string zipfile) throws ioexception {
  checkfilename(zipfile);
  long starttime = system.currenttimemillis();
  // 获取待解压文件的父路径
  file zfile = new file(zipfile);
  string parent = zfile.getparent() + "/";
  // 设置,默认是utf-8
  charset charset = charset.forname("gbk");
  zipfile zip = new zipfile(zipfile, charset);
  zipentry entry = null;
  //封装解压后的路径
  bufferedoutputstream bos=null;
  //封装待解压文件路径
  bufferedinputstream bis=null;
  enumeration<zipentry> enums = (enumeration<zipentry>) zip.entries();
  while (enums.hasmoreelements()) {
    entry = enums.nextelement();
    if (entry.isdirectory()) {
    file filepath = new file(parent + entry.getname());
    // 如果目录不存在,则创建
    if (!filepath.exists()) {
      filepath.mkdirs();
    }
    }else{
    bos=new bufferedoutputstream(new fileoutputstream(parent + entry.getname()));
    //获取条目流
bis =new bufferedinputstream(zip.getinputstream(entry));
    byte buf[] = new byte[1024];
    int len;
    while ((len = bis.read(buf)) != -1) {
      bos.write(buf, 0, len);
    }

    bos.close();
    }
  }
  bis.close();
  zip.close();
  system.out.println("解压后的路径是:"+parent);
  long endtime = system.currenttimemillis();
  system.out.println("解压成功,所需时间为:"+(endtime-starttime)+"ms");
  }

  private void checkfilename(string name) {
  // 文件是否存在
  if (!new file(name).exists()) {
    system.err.println("要解压的文件不存在!");
    system.exit(1);
  }
  // 判断是否是zip文件
  int index = name.lastindexof(".");
  string str = name.substring(index + 1);
  if (!"zip".equalsignorecase(str)) {
    system.err.println("不是zip文件,无法解压!");
    system.exit(1);
  }
  }

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

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

相关文章:

验证码:
移动技术网