-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCompressDemo.java
More file actions
39 lines (30 loc) · 1.17 KB
/
CompressDemo.java
File metadata and controls
39 lines (30 loc) · 1.17 KB
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
package compress;
import java.util.Random;
import priv.dengjl.compress.util.CompressUtil;
public class CompressDemo {
public static final String allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static String generateString(int length) {
StringBuffer sb = new StringBuffer();
Random random = new Random();
for (int i = 0; i < length; i++) {
sb.append(allChar.charAt(random.nextInt(allChar.length())));
}
return sb.toString();
}
public static void main(String[] args) {
try {
String data = generateString(1024 * 10);
System.out.println("压缩前数据内容:" + data);
byte[] dataBytes = data.getBytes();
System.out.println("压缩前数据大小:" + dataBytes.length);
byte[] resultBytes = CompressUtil.GZIP.compress(dataBytes);
System.out.println("压缩后数据大小:" + resultBytes.length);
byte[] uncompressBytes = CompressUtil.GZIP.uncompress(resultBytes);
System.out.println("解压后数据大小:" + uncompressBytes.length);
String result = new String(uncompressBytes);
System.out.println("解压后数据内容:" + result);
} catch (Exception e) {
e.printStackTrace();
}
}
}