在数据传输的过程中如果能够对数据进行压缩处理后,然后再与数据库进行传输处理能节省大量的数据,而且能加快传输的速度!效果还是挺明显的:
测试结果:
- [{"name":"张123三","age":"12","sex":"男","info":".....
- 压缩前:2507
- 压缩后:170
- 解压缩后:2507
- 解压缩后数据:[{"name":"张123三","age":"12","sex":"男","info":"....
压缩代码:
- <pre name="code" class="java">package com.xiaoyi.utils.compress;
- 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 = 0;
- public static final byte FLAG_GBK_STRING_COMPRESSED_BYTEARRAY = 1;
- public static final byte FLAG_UTF8_STRING_COMPRESSED_BYTEARRAY = 2;
- public static final byte FLAG_NO_UPDATE_INFO = 3;
- /**
- * 数据压缩
- *
- * @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;
- }
- }
压缩代码:
- ArrayList<Jjjz> jjjzs = null;
- Gson gson = new Gson();
- String jsonStr = gson.toJson(jjjzs, jjjzs.getClass());
- byte[] resultOriginalByte = jsonStr.getBytes();
- Compress.byteCompress(resultOriginalByte)//压缩后的数据
解压代码:
- byte[] resultByte = Compress.byteDecompress(compressedByte);
- result = new String(resultByte, "utf-8");