jiangping
2024-05-22 a7fb50c91e4932aa175be266fbdd42b2a70eba66
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package cn.emay.sdk.util;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
 
/**
 * GZIP 压缩工具
 * @author Frank
 *
 */
public class GZIPUtils {
 
    public static void main(String[] args) throws IOException {
        String sst = "hahahah";
        System.out.println(sst);
        System.out.println(System.currentTimeMillis());
        System.out.println("size:" + sst.length());
        byte[] bytes = sst.getBytes();
        System.out.println("length:" + bytes.length);
        System.out.println(System.currentTimeMillis());
        byte[] end = compress(bytes);
        System.out.println(System.currentTimeMillis());
        System.out.println("length:" + end.length);
        System.out.println(System.currentTimeMillis());
        byte[] start = decompress(end);
        System.out.println(System.currentTimeMillis());
        System.out.println("length:" + start.length);
        System.out.println(new String(start));
    }
 
    /**
     * 数据压缩传输
     * 
     * @param is
     * @param os
     * @throws Exception
     */
    public static void compressTransfe(byte[] bytes, OutputStream out) throws IOException {
        GZIPOutputStream gos = null;
        try {
            gos = new GZIPOutputStream(out);
            gos.write(bytes);
            gos.finish();
            gos.flush();
        } finally{
            if(gos != null){
                gos.close();
            }
        }
    }
    
    /**
     * 数据压缩
     * 
     * @param is
     * @param os
     * @throws Exception
     */
    public static byte[] compress(byte[] bytes) throws IOException {
        ByteArrayOutputStream out = null;
        GZIPOutputStream gos = null;
        try {
            out = new ByteArrayOutputStream();
            gos = new GZIPOutputStream(out);
            gos.write(bytes);
            gos.finish();
            gos.flush();
        } finally{
            if(gos != null){
                gos.close();
            }
            if(out != null){
                out.close();
            }
        }
        return out.toByteArray();
    }
    
    /**
     * 数据解压
     * 
     * @param in
     * @return
     * @throws IOException
     */
    public static byte[] decompress(byte[] bytes) throws IOException {
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        GZIPInputStream gin = new GZIPInputStream(in);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int count;
        byte data[] = new byte[1024];
        while ((count = gin.read(data, 0, 1024)) != -1) {
            out.write(data, 0, count);
        }
        out.flush();
        out.close();
        gin.close();
        return out.toByteArray();
    }
 
}