JDK(GZIP实现数据压缩)

GZIP是什么?

数据压缩技术
目前常用的压缩算法有:

GZIP,一个压缩比搞的慢速算法,压缩后的数据适合长期使用,JDK中的java.util.zip.GZIPInputStream/GZIPOutputStream是这个算法的实现。
deflate,和GZIP类似,与gzip不同的是,可以指定算法的压缩级别,这样可以在压缩时间和输出文件大小上进行平衡,可选级别有0(不压缩),以及1(快速压缩)~9(慢速压缩),它的实现是java.util.zip.Deflater/Inflater。

GZIP底层原理

gzip使用deflate算法进行压缩,所以gzip底层原理也即是deflate的底层原理。对要压缩的文件,首先进行LZ77算法的一个变种进行压缩,对得到的结果再使用Huffman编码的方法(gzip会根据情况选择使用静态Huffman编码或者动态Huffman编码)

LZ77算法

如果文件中有两块内容相同,那么只要知道前一块的位置和大小,我们就可以确定后一块的内容,我们可以用(两者之间的距离,相同内容的长度)这样的信息来替换后一块内容。由于(两者之间的距离,相同内容的长度)这一对信息的大小小于被替换内容的大小,所以文件得到了压缩。

GZIP 实现数据压缩

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

    }
}

猜你喜欢

转载自blog.csdn.net/lijie0213/article/details/124721574