The deflate compression implementation method in Java _java

Source: Internet
Author: User
Tags base64 deflater uncompress

In the transmission of files, in order to make large files more convenient and rapid transmission, the general use of compression to file compression and then transmission, The Deflater and Inflater classes in the Java.util.zip package in Java provide the user with the compression of the deflate algorithm, the following is a self written compression and decompression implementation, and an example of compressed file content, which involves a specific way to view the JDK API understanding.

/** * * @param inputbyte * Uncompressed byte array * @return uncompressed byte array * @throws IOException/public static
    Byte[] Uncompress (byte[] inputbyte) throws IOException {int len = 0;
    Inflater infl = new Inflater ();
    Infl.setinput (Inputbyte);
    Bytearrayoutputstream BOS = new Bytearrayoutputstream ();
    byte[] Outbyte = new byte[1024];
        try {while (!infl.finished ()) {//uncompress and output the uncompressed content to the byte output stream bos len = infl.inflate (outbyte);
        if (len = = 0) {break;
      } bos.write (Outbyte, 0, Len);
    } infl.end ();
    catch (Exception e) {//} finally {Bos.close ();
  return Bos.tobytearray ();
   }/** * compression. * * @param inputbyte * @return Compressed byte array * @throws IOException/public static byte[] Comp
    Ress (byte[] inputbyte) throws IOException {int len = 0;
    Deflater DEFL = new Deflater ();
    Defl.setinput (Inputbyte);
    Defl.finish (); BytearrAyoutputstream BOS = new Bytearrayoutputstream ();
    byte[] Outputbyte = new byte[1024];
        try {while (!defl.finished ()) {//Compress and output the compressed content to the byte output stream bos len = defl.deflate (outputbyte);
      Bos.write (outputbyte, 0, Len);
    } defl.end ();
    finally {bos.close ();
  return Bos.tobytearray (); public static void Main (string[] args) {try {FileInputStream FIS = new FileInputStream ("d:\\testdeflate.t
      XT ");
      int len = fis.available ();
      Byte[] B = new Byte[len];
      Fis.read (b);
      byte[] bd = compress (b);
      In order to compress the content can transmit on the network, generally uses the Base64 code String ENCODESTR = base64.encodebase64string (BD);
      byte[] bi = uncompress (base64.decodebase64 (ENCODESTR));
      FileOutputStream fos = new FileOutputStream ("D:\\testinflate.txt");
      Fos.write (BI);
      Fos.flush ();
      Fos.close ();
    Fis.close (); catch (Exception e) {//}}

Above this Java deflate compression implementation method is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.