?1
package
?zhenyu.tools.file;
?2
?3
import
?java.io.
*
;
?4
?5
import
?java.util.
*
;
?6
import
?java.util.zip.
*
;
?7
?8
?9
public
?
class
?Unzip?
{
10
????
public
?Unzip()?
{
11
????}
12
13
????
public
?
static
?
void
?main(String[]?args)?
{
14
????????Unzip?unzip?
=
?
new
?Unzip();
15
????????unzip.UnzipFile(
"
D:\\tmp.zip
"
,?
"
e:\\tmp
"
);
16
????}
17
18
????
public
?
static
?
final
?
void
?copyInputStream(InputStream?in,?OutputStream?out)
19
????????
throws
?IOException?
{
20
????????
byte
[]?buffer?
=
?
new
?
byte
[
1024
];
21
????????
int
?len;
22
23
????????
while
?((len?
=
?in.read(buffer))?
>=
?
0
)
24
????????????out.write(buffer,?
0
,?len);
25
26
????????in.close();
27
????????out.close();
28
????}
29
30
????
private
?
void
?UnzipFile(String?from,?String?to)?
{
31
????????Enumeration?entries;
32
????????ZipFile?zipFile;
33
34
????????
if
?(from?
==
?
null
)?
{
35
????????????System.err.println(
"
Usage:?Unzip?zipfile
"
);
36
37
????????????
return
;
38
????????}
39
40
????????
try
?
{
41
????????????zipFile?
=
?
new
?ZipFile(from);
42
????????????entries?
=
?zipFile.entries();
43
44
????????????
while
?(entries.hasMoreElements())?
{
45
????????????????ZipEntry?entry?
=
?(ZipEntry)?entries.nextElement();
46
????????????????System.out.println(entry.getName());
47
48
????????????????
if
?(entry.getName().lastIndexOf(
"
\\
"
)?
!=
?
-
1
)?
{
49
50
????????????????????System.out.println(
"
Extracting?directory:?
"
?
+
?to?
+
51
????????????????????????File.separator?
+
?entry.getName());
52
53
????????????????????String?txt;
54
????????????????????StringTokenizer?st?
=
?
new
?StringTokenizer(entry.getName(),
55
????????????????????????????
"
\\
"
);
56
????????????????????
for
?(
int
?i?
=
?
0
;?st.hasMoreTokens()
&&
i
<
st.countTokens();?i
++
)?
{
57
????????????????????????txt?
=
?st.nextToken().trim();
58
????????????????????????
if
?(to.endsWith(
"
\\
"
))?
{
59
????????????????????????????File?myFilePath?
=
?
new
?File(to?
+
?txt);
60
61
????????????????????????????
if
?(
!
myFilePath.exists())?
{
62
????????????????????????????????myFilePath.mkdir();
63
????????????????????????????}
64
????????????????????????}
?
else
?
{
65
????????????????????????????File?myFilePath?
=
?
new
?File(to?
+
?File.separator?
+
66
????????????????????????????????????txt);
67
68
????????????????????????????
if
?(
!
myFilePath.exists())?
{
69
????????????????????????????????myFilePath.mkdir();
70
????????????????????????????}
71
????????????????????????}
72
????????????????????}
73
????????????????}
74
75
????????????????System.out.println(
"
Extracting?file:?
"
?
+
?to?
+
?File.separator?
+
76
????????????????????entry.getName());
77
????????????????System.out.println(
"
--------------------------------------
"
);
78
79
????????????????????????????????copyInputStream(zipFile.getInputStream(entry),
80
????????????????????????????????????
new
?BufferedOutputStream(
81
????????????????????????????????????????
new
?FileOutputStream(to?
+
?File.separator?
+
?entry.getName())));
82
????????????}
83
84
????????????zipFile.close();
85
????????}
?
catch
?(IOException?ioe)?
{
86
????????????System.err.println(
"
Unhandled?exception:
"
);
87
????????????ioe.printStackTrace();
88
89
????????????
return
;
90
????????}
91
????}
92
}
93
壓縮包文件結(jié)構(gòu):

?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

83

84

85



86

87

88

89

90

91

92

93

|-tem
?????? |-folderA
?????????????????? |-filea
????????????????? ?|-fileb
?????????????????? |-filec
?????? |-filed
解壓后文件結(jié)構(gòu):
|-tem
?????? |-folderA
?????????????????? |-filea
????????????????? ?|-fileb
?????????????????? |-filec
?????? |-filed