Java implementation of Zip,gzip,7z,zlib format compression packaging _java

Source: Internet
Author: User

This article mainly describes the use of Java-related classes can be implemented to the file or folder compression.

Zlib is a data compression library that is designed to handle simple data (regardless of the source of the data).

7z is a new compression format that has the current highest compression ratio.

Gzip is a file compression tool (or the compressed file format produced by the compression tool) that is designed to work with individual files. Gzip uses zlib to compress data in a file. In order to save information about file attributes, Gzip needs to save more header content in a compressed file (*.gz), and zlib do not have to consider this. But Gzip only applies to a single file, so we often see on the unix/linux of the compression packet suffix is *.tar.gz or *.tgz, that is, the first to use tar to package multiple files into a single file, and then use gzip compression results.

Zip is a format for compressing multiple files (the corresponding tools have PKZIP and WinZip, etc.), so the zip file further contains information about the file directory structure, more than Gzip header information. However, note that the ZIP format can use a variety of compression algorithms, most of our common ZIP files are not compressed by the zlib algorithm, its compressed data format and gzip very different.

Therefore, you should, depending on your specific needs, choose a different compression technology: If you only need to compress/decompress data, you can directly use zlib implementation, if you need to generate GZIP format files or decompression other tools of the compression results, you have to use gzip or zip and other related classes to deal with.

Maven Dependencies

<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 ZIPOUTPU Tstream (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.putnex Tentry (New ZipEntry (Files[i].getname ()));
      }
      int Len;
      Read the contents of the file that needs to be downloaded, and package to a 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 compressed 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 ();
    }
  }

7z packing

public static void z7z (string input, string output, String name) throws Exception {try {sevenzoutputfile seve
      Nzoutput = 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 packing

public static void Zlib (string input, string output) throws Exception {

//    Deflateroutputstream dos = new Deflater OutputStream (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 ();
  }

I hope this article is helpful to you, Java implementation of the ZIP,GZIP,7Z,ZLIB format of the compressed packaging content is introduced here. I hope you will continue to pay attention to our website! Want to learn Java can continue to focus on this site.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.