JDK (GZIP for data compression)

What is GZIP?

Data compression technology
Currently commonly used compression algorithms are:

GZIP, a slow algorithm with high compression ratio, the compressed data is suitable for long-term use, java.util.zip.GZIPInputStream/GZIPOutputStream in JDK is the implementation of this algorithm.
deflate, similar to GZIP, different from gzip, you can specify the compression level of the algorithm, which can balance the compression time and output file size, the optional level is 0 (no compression), and 1 (fast compression)~9 (slow compression), its implementation is java.util.zip.Deflater/Inflater.

The underlying principle of GZIP

Gzip uses the deflate algorithm for compression, so the underlying principle of gzip is also the underlying principle of deflate. For the file to be compressed, first compress a variant of the LZ77 algorithm, and then use the Huffman encoding method for the obtained result (gzip will choose to use static Huffman encoding or dynamic Huffman encoding according to the situation)

LZ77 algorithm

If there are two blocks with the same content in the file, as long as we know the position and size of the previous block, we can determine the content of the next block, and we can use information such as (the distance between the two, the length of the same content) to replace the latter a piece of content. Since the size of the (distance between the two, length of the same content) pair is smaller than the size of the replaced content, the file is compressed.

GZIP for data compression

package com.lingxu;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GzipUtil {
    
    

    /**
     * @Description: 加密
     * @author: Ajie
     * @date: 2022/5/11
     * @param str
     * @return: byte[]
     */
    public static byte[] compress(String str)  {
    
    
        ByteArrayOutputStream out =null;
        GZIPOutputStream gzip=null;
        try{
    
    
            if (str == null || str.length() == 0) {
    
    
                return null;
            }
            out = new ByteArrayOutputStream();
            gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes("ISO-8859-1"));
            gzip.finish();
            return out.toByteArray();
        }catch(Exception e){
    
    
            e.printStackTrace();
            return null;
        }finally{
    
    
            try{
    
    
                if(out!=null){
    
    
                    out.close();
                }
                if(gzip!=null){
    
    
                    gzip.close();
                }
            }catch(Exception e){
    
    
                e.printStackTrace();
            }
        }
    }

    /**
     * @Description: 解密
     * @author: Ajie
     * @date: 2022/5/11
     * @param by
     * @return: java.lang.String
     */
    public static String unCompress(byte []by) {
    
    
        ByteArrayOutputStream out=null;
        GZIPInputStream gunzip=null;
        try{
    
    
            if(by==null || by.length==0){
    
    
                return "";
            }
            out=new ByteArrayOutputStream();
            gunzip= new GZIPInputStream(new ByteArrayInputStream(by));
            byte[] buffer = new byte[1024];
            int n;
            while ((n=gunzip.read(buffer))!=-1) {
    
    
                out.write(buffer, 0, n);
            }
            out.flush();
            return new String(out.toByteArray(),"ISO-8859-1");
        }catch(Exception e){
    
    
            e.printStackTrace();
            return "";
        }finally{
    
    
            try{
    
    
                if(out!=null){
    
    
                    out.close();
                }
                if(gunzip!=null){
    
    
                    gunzip.close();
                }
            }catch(Exception e){
    
    
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) throws IOException {
    
    

        String str = "ADSaksdnkjasdhfoWEFHOwhenfouuishefiuqhwieufwieuhrfiuqwheriuhqweiurhwqeiurh";

        System.out.println(str.getBytes("utf-8").length);
        System.out.println(compress(str).length);
        System.out.println(unCompress(compress(str)));

    }
}

Guess you like

Origin blog.csdn.net/lijie0213/article/details/124721574