• 客服QQ:114297999 合作电话:4000288880
  • 盱眙是我家,文明靠大家!小城盱眙网与您携手共建和谐盱眙、美好盱眙!
查看: 1264|回复: 0
打印 上一主题 下一主题

Java案例:压缩解压缩实现的详细介绍

[复制链接]

中级会员

Rank: 3Rank: 3

积分
468
跳转到指定楼层
楼主
发表于 2016-7-13 10:25:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
文件压缩效率取决于压缩程序使用的具体算法。有些程序能够在某些类型的文件中更好地寻找到模式,因此能更有效地压缩这些类型的文件。其他一些压缩程序在字典中又使用了字典,这使它们在压缩大文件时表现很好,但是在压缩较小的文件时效率不高。尽管这一类的所有压缩程序都基于同一个基本理念,但是它们的执行方式却各不相同。程序开发人员始终在尝试建立更好的压缩机制。
压缩原理:计算机处理的信息是以二进制数的形式表示的,因此压缩软件就是把二进制信息中相同的字符串以特殊字符标记来达到压缩的目的。下面跟着海文IT教育一起来看看java实现压缩解压缩的代码吧。
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
* GZIP压缩解压类
*/
public class MessageGZIP {
    private static Stringencode = "utf-8";//"ISO-8859-1"
    public StringgetEncode() {
        return encode;
    }
    /*
     * 设置 编码,默认编码:UTF-8
     */
    public void setEncode(Stringencode) {
        MessageGZIP.encode= encode;
    }
    /*
     * 字符串压缩为字节数组
     */
    public static byte[]compressToByte(String str){
        if (str == null ||str.length() == 0) {
            return null;
        }
        ByteArrayOutputStreamout = new ByteArrayOutputStream();
        GZIPOutputStreamgzip;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes(encode));
            gzip.close();
        } catch (IOExceptione) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }
    /*
     * 字符串压缩为字节数组
     */
    public static byte[]compressToByte(String str,String encoding){
        if (str == null ||str.length() == 0) {
            return null;
        }
        ByteArrayOutputStreamout = new ByteArrayOutputStream();
        GZIPOutputStreamgzip;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes(encoding));
            gzip.close();
        } catch (IOExceptione) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }
    /*
     * 字节数组解压缩后返回字符串
     */
    public static StringuncompressToString(byte[] b) {
        if (b == null || b.length== 0) {
            return null;
        }
        ByteArrayOutputStreamout = new ByteArrayOutputStream();
        ByteArrayInputStreamin = new ByteArrayInputStream(b);
        try {
            GZIPInputStreamgunzip = new GZIPInputStream(in);
            byte[] buffer =new byte[256];
            int n;
            while ((n =gunzip.read(buffer)) >= 0) {
                out.write(buffer,0, n);
            }
        } catch (IOExceptione) {
            e.printStackTrace();
        }
        return out.toString();
    }
    /*
     * 字节数组解压缩后返回字符串
     */
    public static StringuncompressToString(byte[] b, String encoding) {
        if (b == null || b.length== 0) {
            return null;
        }
        ByteArrayOutputStreamout = new ByteArrayOutputStream();
        ByteArrayInputStreamin = new ByteArrayInputStream(b);
        try {
            GZIPInputStreamgunzip = new GZIPInputStream(in);
            byte[] buffer =new byte[256];
            int n;
            while ((n =gunzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
            return out.toString(encoding);
        } catch (IOExceptione) {
            e.printStackTrace();
        }
        return null;
    }
}
可以到腾讯课堂或者百度传课搜索海文IT教育,观看讲解视频哦。另外7月25号我们的java课程就要开始正式进入直播阶段哦,请大家搬好板凳来看哦。
更多精彩课程尽在上海海文信息技术有限公司,http://www.oracleoaec.net/。

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖 支持支持 反对反对
回复

使用道具 举报

使用 高级模式(可批量传图、插入视频等)
您需要登录后才可以回帖 登录 | 立即注册

快速回复 返回顶部 返回列表