压缩数据封装方法源代码

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class Compress {

	private static final int BUFFER_LENGTH = 400;

	// 压缩字节最小长度,小于这个长度的字节数组不适合压缩,压缩完会更大
	public static final int BYTE_MIN_LENGTH = 50;

	// 字节数组是否压缩标志位
	public static final byte FLAG_GBK_STRING_UNCOMPRESSED_BYTEARRAY = 1;
	//表示服务器传递的数据是GBK编码的字符串经过压缩后的字节数组
	public static final byte FLAG_GBK_STRING_COMPRESSED_BYTEARRAY = 2;
	public static final byte FLAG_UTF8_STRING_COMPRESSED_BYTEARRAY = 3;
	public static final byte FLAG_NO_UPDATE_INFO = 4;

	/**
	 * 数据压缩
		 * @param is
	 * @param os
	 * @throws Exception
	 */
	public static void compress(InputStream is, OutputStream os)
			throws Exception {

		GZIPOutputStream gos = new GZIPOutputStream(os);

		int count;
		byte data[] = new byte[BUFFER_LENGTH];
		while ((count = is.read(data, 0, BUFFER_LENGTH)) != -1) {
			gos.write(data, 0, count);
		}

		gos.finish();

		gos.flush();
		gos.close();
	}

	/**
	 * 数据解压缩
		 * @param is
	 * @param os
	 * @throws Exception
	 */
	public static void decompress(InputStream is, OutputStream os)
			throws Exception {

		GZIPInputStream gis = new GZIPInputStream(is);

		int count;
		byte data[] = new byte[BUFFER_LENGTH];
		while ((count = gis.read(data, 0, BUFFER_LENGTH)) != -1) {
			os.write(data, 0, count);
		}

		gis.close();
	}

	/**
	 * 数据压缩
		 * @param data
	 * @return
	 * @throws Exception
	 */
	public static byte[] byteCompress(byte[] data) throws Exception {
		ByteArrayInputStream bais = new ByteArrayInputStream(data);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		// 压缩
		compress(bais, baos);

		byte[] output = baos.toByteArray();

		baos.flush();
		baos.close();

		bais.close();

		return output;
	}

	/**
	 * 数据解压缩
		 * @param data
	 * @return
	 * @throws Exception
	 */
	public static byte[] byteDecompress(byte[] data) throws Exception {
		ByteArrayInputStream bais = new ByteArrayInputStream(data);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		// 解压缩

		decompress(bais, baos);

		data = baos.toByteArray();

		baos.flush();
		baos.close();

		bais.close();

		return data;
	}
	
 // 压缩String型数据
 // 服务器端压缩数据用
 public static String stringCompress(String data) throws Exception {
 byte[] resultOriginalByte = data.getBytes();
 // 组织最后返回数据的缓冲字节数组
 ByteArrayOutputStream resultBuffer = new ByteArrayOutputStream();

 // 如果要返回的结果字节数组小于50位,不将压缩
 if (resultOriginalByte.length < Compress.BYTE_MIN_LENGTH) {
 byte flagByte = Compress.FLAG_GBK_STRING_UNCOMPRESSED_BYTEARRAY;
 resultBuffer.write(flagByte);
 resultBuffer.write(resultOriginalByte);
 } else {
 try {
 byte flagByte = Compress.FLAG_GBK_STRING_COMPRESSED_BYTEARRAY;
 resultBuffer.write(flagByte);
 resultBuffer.write(Compress.byteCompress(resultOriginalByte));
 } catch (Exception e) {
 e.printStackTrace();
 }
 }

 resultBuffer.flush();
 resultBuffer.close();
 String res = resultBuffer.toString("ISO-8859-1");
 return res;
 }

 // 解压缩String型数据
 // 客户端解压缩数据用
 public static String stringDecompress(String data) throws Exception {
 byte[] receivedByte = data.getBytes("ISO-8859-1");
 String rev = null;
 // 判断接收到的字节数组是否是压缩过的
 if (receivedByte[0] == Compress.FLAG_GBK_STRING_UNCOMPRESSED_BYTEARRAY) {
 rev = new String(receivedByte, 1, receivedByte.length - 1, "gb2312");
 } else if (receivedByte[0] == Compress.FLAG_GBK_STRING_COMPRESSED_BYTEARRAY) {

 byte[] compressedByte = new byte[receivedByte.length - 1];

 for (int i = 0; i < compressedByte.length; i++) {
 compressedByte[i] = receivedByte[i + 1];
 }
 byte[] resultByte = Compress.byteDecompress(compressedByte);
 rev = new String(resultByte, "gb2312");
 } else{
 rev = "0";
 }
 return rev;
 }
}

你可能感兴趣的:(android,压缩)