-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompress
More file actions
82 lines (74 loc) · 1.87 KB
/
Copy pathcompress
File metadata and controls
82 lines (74 loc) · 1.87 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
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
private static void encodeTXT(String src,String des) throws IOException {
byte[] bytes = Files.readAllBytes(Paths.get(src));
Files.write(Paths.get(des), base64Encoder(gZip(new String(bytes))).getBytes());
}
private static void decodeTXT(String src,String des) throws Exception {
byte[] bytes = Files.readAllBytes(Paths.get(src));
Files.write(Paths.get(des), unGZip(base64Decoder(new String(bytes))).getBytes());
}
public static byte[] gZip(String s) {
if (s == null || "".equals(s.trim()))
return null;
try {
byte[] b = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(bos);
gzip.write(s.getBytes());
gzip.finish();
gzip.close();
b = bos.toByteArray();
bos.close();
return b;
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
/**
* 解压
*
* @param data
* @return
*/
public static String unGZip(byte[] data) {
byte[] b = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ByteArrayInputStream bis = new ByteArrayInputStream(data);
GZIPInputStream gzip = new GZIPInputStream(bis);
byte[] buf = new byte[1024];
int num = -1;
while ((num = gzip.read(buf, 0, buf.length)) != -1) {
baos.write(buf, 0, num);
}
b = baos.toByteArray();
baos.flush();
baos.close();
gzip.close();
bis.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return new String(baos.toByteArray());
}
/**
* 用BASE64编码
*/
public static String base64Encoder(byte[] b) {
if (b == null)
return null;
return new BASE64Encoder().encode(b);
}
/**
* 用BASE64解码
*/
public static byte[] base64Decoder(String s){
if (s == null)
return null;
try {
return new BASE64Decoder().decodeBuffer(s);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}