Repository navigation
Expand file tree
/
Copy pathGzipCompress.java
More file actions
64 lines (54 loc) · 1.85 KB
/
Copy pathGzipCompress.java
File metadata and controls
64 lines (54 loc) · 1.85 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
package com.example.compress.gzip;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import static javafx.scene.input.KeyCode.G;
/**
* Created by guolei on 16-8-9.
* ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
* | 没有神兽,风骚依旧! |
* | QQ:1120832563 |
* ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
*/
public class GzipCompress {
public static void main(String[] args){
compress();
uncompress();
}
/**
* 压缩
*/
private static void compress() {
try {
//创建GZIP 文件 .gz
File file = new File("./demo.gz");
if (file.exists()){
file.delete();
}
file.createNewFile();
byte[] b_1 = new String("测试gzip压缩").getBytes("utf-8");
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(new FileOutputStream(file));
gzipOutputStream.write(b_1);
gzipOutputStream.flush();
gzipOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void uncompress() {
File file = new File("./demo.gz");
try {
GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(file));
byte[] b = new byte[1024];
while (gzipInputStream.read(b) != -1){
System.err.println("读取到的内容");
System.err.println(new String(b).trim());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}