Java实现GZIP压缩与解压缩

Gzip压缩

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static byte[] compress(String str, String charset) {
if (str == null || str.length() == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip;
try {
gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes(charset));
gzip.close();
} catch (Exception e) {
e.printStackTrace();
}
return out.toByteArray();
}

Gzip解压缩

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static byte[] uncompress(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
try {
GZIPInputStream unGzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = unGzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
}
return out.toByteArray();
}

工具代码集合

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
public class GzipUtils {

public static byte[] compress(String str, String charset) {
if (str == null || str.length() == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip;
try {
gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes(charset));
gzip.close();
} catch (Exception e) {
e.printStackTrace();
}
return out.toByteArray();
}

public static byte[] compress(String str) throws IOException {
return compress(str, StandardCharsets.UTF_8.name());
}

public static byte[] uncompress(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
try {
GZIPInputStream unGzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = unGzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
}
return out.toByteArray();
}

public static byte[] uncompress(String str, String charset) throws IOException {
return uncompress(str.getBytes(charset));
}

public static byte[] uncompress(String str) throws IOException {
return uncompress(str.getBytes(StandardCharsets.UTF_8.name()));
}

public static String uncompressToString(byte[] bytes, String charset) {
if (bytes == null || bytes.length == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
try {
GZIPInputStream unGzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = unGzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
return out.toString(charset);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

public static String uncompressToString(byte[] bytes) {
return uncompressToString(bytes, StandardCharsets.UTF_8.name());
}

public static String uncompressToString(String str, String inputCharset, String outputCharset) throws IOException {
return new String(uncompress(str, inputCharset), outputCharset);
}

public static String uncompressToString(String str) throws IOException {
return new String(uncompress(str, StandardCharsets.ISO_8859_1.name()), StandardCharsets.UTF_8.name());
}
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2021 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :