当前位置: 移动技术网 > IT编程>开发语言>Java > java实现zip,gzip,7z,zlib格式的压缩打包

java实现zip,gzip,7z,zlib格式的压缩打包

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

本文主要介绍的是通过使用java的相关类可以实现对文件或文件夹的压缩。

zlib是一种数据压缩程序库,它的设计目标是处理单纯的数据(而不管数据的来源是什么)。

7z 是一种新的压缩格式,它拥有目前最高的压缩比。

gzip是一种文件压缩工具(或该压缩工具产生的压缩文件格式),它的设计目标是处理单个的文件。gzip在压缩文件中的数据时使用的就是zlib。为了保存与文件属性有关的信息,gzip需要在压缩文件(*.gz)中保存更多的头信息内容,而zlib不用考虑这一点。但gzip只适用于单个文件,所以我们在unix/linux上经常看到的压缩包后缀都是*.tar.gz或*.tgz,也就是先用tar把多个文件打包成单个文件,再用gzip压缩的结果。

zip是适用于压缩多个文件的格式(相应的工具有pkzip和winzip等),因此,zip文件还要进一步包含文件目录结构的信息,比gzip的头信息更多。但需要注意,zip格式可采用多种压缩算法,我们常见的zip文件大多不是用zlib的算法压缩的,其压缩数据的格式与gzip大不一样。

所以,你应当根据你的具体需求,选择不同的压缩技术:如果只需要压缩/解压缩数据,你可以直接用zlib实现,如果需要生成gzip格式的文件或解压其他工具的压缩结果,你就必须用gzip或zip等相关的类来处理了。

maven依赖
 

<dependency>
      <groupid>org.apache.commons</groupid>
      <artifactid>commons-compress</artifactid>
      <version>1.12</version>
    </dependency> 

zip格式

public static void zip(string input, string output, string name) throws exception {
    zipoutputstream out = new zipoutputstream(new fileoutputstream(output));
    string[] paths = input.split("\\|");
    file[] files = new file[paths.length];
    byte[] buffer = new byte[1024];
    for (int i = 0; i < paths.length; i++) {
      files[i] = new file(paths[i]);
    }
    for (int i = 0; i < files.length; i++) {
      fileinputstream fis = new fileinputstream(files[i]);
      if (files.length == 1 && name != null) {
        out.putnextentry(new zipentry(name));
      } else {
        out.putnextentry(new zipentry(files[i].getname()));
      }
      int len;
      // 读入需要下载的文件的内容,打包到zip文件
      while ((len = fis.read(buffer)) > 0) {
        out.write(buffer, 0, len);
      }
      out.closeentry();
      fis.close();
    }
    out.close();
  }

gzip打包

public static void gzip(string input, string output, string name) throws exception {
    string compress_name = null;
    if (name != null) {
      compress_name = name;
    } else {
      compress_name = new file(input).getname();
    }
    byte[] buffer = new byte[1024];
    try {
      gzipparameters gp = new gzipparameters();  //设置压缩文件里的文件名
      gp.setfilename(compress_name);
      gzipcompressoroutputstream gcos = new gzipcompressoroutputstream(new fileoutputstream(output), gp);
      fileinputstream fis = new fileinputstream(input);
      int length;
      while ((length = fis.read(buffer)) > 0) {
        gcos.write(buffer, 0, length);
      }
      fis.close();
      gcos.finish();
    } catch (ioexception ioe) {
      ioe.printstacktrace();
    }
  }

7z打包

public static void z7z(string input, string output, string name) throws exception {
    try {
      sevenzoutputfile sevenzoutput = new sevenzoutputfile(new file(output));
      sevenzarchiveentry entry = null;

      string[] paths = input.split("\\|");
      file[] files = new file[paths.length];
      for (int i = 0; i < paths.length; i++) {
        files[i] = new file(paths[i].trim());
      }
      for (int i = 0; i < files.length; i++) {
        bufferedinputstream instream = null;
        instream = new bufferedinputstream(new fileinputstream(paths[i]));
        if (name != null) {
          entry = sevenzoutput.createarchiveentry(new file(paths[i]), name);
        } else {
          entry = sevenzoutput.createarchiveentry(new file(paths[i]), new file(paths[i]).getname());
        }
        sevenzoutput.putarchiveentry(entry);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = instream.read(buffer)) > 0) {
          sevenzoutput.write(buffer, 0, len);
        }
        instream.close();
        sevenzoutput.closearchiveentry();
      }
      sevenzoutput.close();
    } catch (ioexception ioe) {
      system.out.println(ioe.tostring() + " " + input);
    }
  }

zlib打包

public static void zlib(string input, string output) throws exception {

//    deflateroutputstream dos = new deflateroutputstream(new fileoutputstream(output));
    deflateparameters dp = new deflateparameters();
    dp.setwithzlibheader(true);
    deflatecompressoroutputstream dcos = new deflatecompressoroutputstream(new fileoutputstream(output),dp);
    fileinputstream fis = new fileinputstream(input);
    int length = (int) new file(input).length();
    byte data[] = new byte[length];
    // int length;
    while ((length = fis.read(data)) > 0) {
      dcos.write(data, 0, length);
    }
    fis.close();
    dcos.finish();
    dcos.close();
  }

希望本文所述对你有所帮助,java实现zip,gzip,7z,zlib格式的压缩打包内容就给大家介绍到这里了。希望大家继续关注我们的网站!想要学习java可以继续关注本站。

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

相关文章:

验证码:
移动技术网