文件压缩效率取决于压缩程序使用的具体算法。有些程序能够在某些类型的文件中更好地寻找到模式,因此能更有效地压缩这些类型的文件。其他一些压缩程序在字典中又使用了字典,这使它们在压缩大文件时表现很好,但是在压缩较小的文件时效率不高。尽管这一类的所有压缩程序都基于同一个基本理念,但是它们的执行方式却各不相同。程序开发人员始终在尝试建立更好的压缩机制。

压缩原理:计算机处理的信息是以二进制数的形式表示的,因此压缩软件就是把二进制信息中相同的字符串以特殊字符标记来达到压缩的目的。下面跟着海文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/。