自动化立体仓库 - WMS系统
1
ZY
21 小时以前 1b5c0044b4178c7314781ac36f897b781de72064
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.zy.nc.util;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.zip.*;
 
/**
 * ѹ��������
 *
 * @author liyang
 */
public class CompressUtil {
 
    private static int buffSize = 1024;
 
    /**
     * deflaterCompress Ĭ��ѹ��
     *
     * @param source ԭ��
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String deflaterCompress(String source) throws Exception {
        String value = null;
 
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Deflater compressor = new Deflater();
 
        try {
 
            byte[] input = source.getBytes(StandardCharsets.UTF_8);
            // ����ѹ���Ǽ�
            compressor.setLevel(Deflater.DEFAULT_COMPRESSION);
            compressor.setInput(input);
            compressor.finish();
            final byte[] buf = new byte[buffSize];
 
            while (!compressor.finished()) {
                int count = compressor.deflate(buf);
                bos.write(buf, 0, count);
            }
            value = Base64Util.encryptBASE64(bos.toByteArray());
 
        } finally {
            bos.close();
            compressor.end();
        }
 
        return value;
    }
 
    /**
     * deflaterDecompress Ĭ�Ͻ�ѹ
     *
     * @param source ѹ�����ı�
     * @return
     * @throws IOException
     * @throws @throws     Exception
     */
    public static String deflaterDecompress(String source) throws Exception {
        String value = null;
 
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Inflater decompressor = new Inflater();
 
        try {
            byte[] input = Base64Util.decryptBASE64(source);
 
            decompressor.setInput(input);
            final byte[] buf = new byte[buffSize];
 
            while (!decompressor.finished()) {
                int count = decompressor.inflate(buf);
                bos.write(buf, 0, count);
            }
 
            value = new String(bos.toByteArray(), StandardCharsets.UTF_8);
        } catch (IOException | DataFormatException e) {
            throw new Exception("��ѹ�쳣 " + e.getMessage());
        } finally {
            bos.close();
            decompressor.end();
        }
        return value;
    }
 
    /**
     * gzipCompress ����gzipѹ��
     *
     * @param source ԭ��
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String gzipCompress(String source) throws Exception {
        String value = null;
 
        ByteArrayOutputStream out = null;
 
        try {
            out = new ByteArrayOutputStream();
            GZIPOutputStream gzip = new GZIPOutputStream(out);
            byte[] input = source.getBytes(StandardCharsets.UTF_8);
            gzip.write(input);
            gzip.close();
            value = Base64Util.encryptBASE64(out.toByteArray());
        } catch (IOException e) {
            throw new Exception("ѹ���쳣 " + e.getMessage());
        } finally {
            if (out != null) {
                out.close();
            }
        }
 
        return value;
    }
 
    /**
     * gzipDecompress gzip��ѹ
     *
     * @param source ѹ�����ı�
     * @return
     * @throws Exception
     */
    public static String gzipDecompress(String source) throws Exception {
        String value = null;
 
        ByteArrayOutputStream out = null;
        ByteArrayInputStream in = null;
 
        try {
            byte[] input = Base64Util.decryptBASE64(source);
            out = new ByteArrayOutputStream();
            in = new ByteArrayInputStream(input);
            GZIPInputStream ungzip = new GZIPInputStream(in);
 
            byte[] buffer = new byte[buffSize];
            int n;
            while ((n = ungzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
            ungzip.close();
            value = new String(out.toByteArray(), StandardCharsets.UTF_8);
        } catch (IOException e) {
            throw new Exception("ѹ���쳣 " + e.getMessage());
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
 
        }
        return value;
    }
}