栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JDK(GZIP实现数据压缩)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 {

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

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

    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/880243.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号