?1
import
?java.util.zip.ZipOutputStream;
?2
import
?java.io.IOException;
?3
import
?java.util.zip.ZipEntry;
?4
import
?java.io.FileOutputStream;
?5
import
?java.io.FileInputStream;
?6
?7
class
?CSVZip?
{
?8
?9
??
public
?
static
?
void
?Zip(String?file,String?zipfile)?
{
10
????
try
?
{
11
??????
//
創建文件輸入流對象
12
??????FileInputStream?in?
=
?
new
?FileInputStream(file);?
//
0
13
??????
//
創建文件輸出流對象
14
??????FileOutputStream?out?
=
?
new
?FileOutputStream(zipfile);?
//
1
15
??????
//
創建ZIP數據輸出流對象
16
??????ZipOutputStream?zipOut?
=
?
new
?ZipOutputStream(out);
17
??????
//
創建指向壓縮原始文件的入口
18
??????ZipEntry?entry?
=
?
new
?ZipEntry(file);?
//
0
19
??????zipOut.putNextEntry(entry);
20
??????
//
向壓縮文件中輸出數據
21
??????
int
?nNumber;
22
??????
byte
[]?buffer?
=
?
new
?
byte
[
512
];
23
??????
while
?(?(nNumber?
=
?in.read(buffer))?
!=
?
-
1
)
24
????????zipOut.write(buffer,?
0
,?nNumber);
25
??????
//
關閉創建的流對象
26
??????zipOut.close();
27
28
??????out.close();
29
??????in.close();
30
31
????}
catch
?(IOException?e)?
{
32
???????????????System.out.println(e);
33
???????????????????????????}
34
35
??}
36
}
37

?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
