Skip to content

Commit 7661c00

Browse files
committed
新增:GZIP简单使用
1 parent 33af73a commit 7661c00

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.javacore.io.zip;
2+
3+
import java.io.BufferedOutputStream;
4+
import java.io.BufferedReader;
5+
import java.io.FileInputStream;
6+
import java.io.FileNotFoundException;
7+
import java.io.FileOutputStream;
8+
import java.io.FileReader;
9+
import java.io.IOException;
10+
import java.io.InputStreamReader;
11+
import java.util.zip.GZIPInputStream;
12+
import java.util.zip.GZIPOutputStream;
13+
14+
/*
15+
* Copyright [2015] [Jeff Lee]
16+
*
17+
* Licensed under the Apache License, Version 2.0 (the "License");
18+
* you may not use this file except in compliance with the License.
19+
* You may obtain a copy of the License at
20+
*
21+
* http://www.apache.org/licenses/LICENSE-2.0
22+
*
23+
* Unless required by applicable law or agreed to in writing, software
24+
* distributed under the License is distributed on an "AS IS" BASIS,
25+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26+
* See the License for the specific language governing permissions and
27+
* limitations under the License.
28+
*/
29+
30+
/**
31+
* @author Jeff Lee
32+
* @since 2015-10-17 13:59:25
33+
* GZIP简单使用
34+
*/
35+
public class GZIPcompress {
36+
public static void main(String[] args) throws IOException {
37+
// 用Reader读文件
38+
BufferedReader in = new BufferedReader(new InputStreamReader(
39+
new FileInputStream("data.gz" ), "UTF-8" ));
40+
// 使用缓冲输出流,输出压缩流文件
41+
BufferedOutputStream out = new BufferedOutputStream(
42+
new GZIPOutputStream(new FileOutputStream("data.gz")));
43+
System.out.println("Writing File 压缩");
44+
int c;
45+
while ((c = in.read()) > 0)
46+
out.write(String.valueOf((char)c).getBytes("UTF-8"));
47+
in.close();
48+
out.close();
49+
50+
System.out.println("Reading File 解压");
51+
// 用输入解压流读取文件
52+
BufferedReader in2 = new BufferedReader(
53+
new InputStreamReader(
54+
new GZIPInputStream(new FileInputStream("data.gz")),"UTF-8"));// encoding question
55+
String s;
56+
while ((s=in2.readLine()) != null)
57+
System.out.println(s);
58+
in2.close();
59+
}
60+
}

0 commit comments

Comments
 (0)