java implements the compression and packaging of zip gzip 7z and zlib formats

  • 2020-05-12 02:32:50
  • OfStack

This article focuses on the compression of files or folders by using java's related classes.

zlib is a data compression library designed to process pure data (regardless of its source).

7z is a new compression format with the highest compression ratio available today.

gzip is a file compression tool (or the compressed file format produced by the compression tool) designed to process a single file. gzip USES zlib when compressing data in a file. To save information about file properties, gzip needs to save more header content in a compressed file (*.gz), while zlib does not consider this 1 point. However, gzip only applies to a single file, so the common compression package suffixes we see on UNIX/Linux are *.tar.gz or *.tgz, which is the result of packaging multiple files into a single file with tar and then compressing them with gzip.

zip is a format for compressing multiple files (PkZip, WinZip, etc.), so the zip file contains more information about the file directory structure than the gzip header. However, it should be noted that the zip format can be compressed by a variety of algorithms. Most of our common zip files are not compressed by the zlib algorithm, and their compressed data format is quite different from that of gzip.

Therefore, you should choose different compression techniques according to your specific requirements: if you only need to compress/extract data, you can use zlib directly; if you need to generate files in gzip format or extract compression results from other tools, you must use gzip or zip or other relevant classes.

maven rely on


<dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-compress</artifactId>
      <version>1.12</version>
    </dependency> 

zip format


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;
      //  Read in the contents of the files you need to download and package them into zip file 
      while ((len = fis.read(buffer)) > 0) {
        out.write(buffer, 0, len);
      }
      out.closeEntry();
      fis.close();
    }
    out.close();
  }

gzip packaging


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();  // Set the file name in the zip file 
      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();
    }
  }

7 z packaging


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 packaging


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();
  }

Hope this article described some help to you, implement zip java, gzip, 7 z, zlib format of compressed package content is to introduce you to here. We hope you continue to pay attention to our website! Stay tuned to learn java.


Related articles: