文件夾的壓縮與解壓
1
package homeWork.first;
2
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileNotFoundException;
6
import java.io.FileOutputStream;
7
import java.io.IOException;
8
import java.util.zip.ZipEntry;
9
import java.util.zip.ZipInputStream;
10
import java.util.zip.ZipOutputStream;
11
12
/**
13
* 壓縮與解壓類
14
*
15
* @author mysileng
16
*
17
*/
18
public class ZipDemo {
19
20
/**
21
* 壓縮單個文件
22
*
23
* @param fileIn
24
* @param fileout
25
*/
26
public void singleFileZip(File fileIn, File fileout) {
27
FileInputStream in = null;
28
FileOutputStream out = null;
29
ZipOutputStream zip = null;
30
try {
31
in = new FileInputStream(fileIn);
32
out = new FileOutputStream(fileout);
33
zip = new ZipOutputStream(out);
34
byte[] b = new byte[1024];
35
36
ZipEntry entry = new ZipEntry(fileIn.getName());
37
zip.putNextEntry(entry);
38
while (in.read(b) != -1) {
39
zip.write(b);
40
}
41
} catch (FileNotFoundException e) {
42
e.printStackTrace();
43
} catch (IOException e) {
44
e.printStackTrace();
45
} finally {
46
try {
47
zip.close();
48
out.close();
49
in.close();
50
} catch (IOException e) {
51
e.printStackTrace();
52
}
53
54
}
55
56
}
57
58
/**
59
* 壓縮文件夾
60
*
61
* @param befor文件夾壓縮之前的路徑
62
* @param after文件夾壓縮之后的路徑
63
*/
64
public void mirsZip(String befor, String after) {
65
File fileIn = new File(befor);
66
File fileOut = new File(after);
67
68
FileOutputStream out = null;
69
ZipOutputStream zip = null;
70
String base = "";
71
try {
72
out = new FileOutputStream(fileOut);
73
zip = new ZipOutputStream(out);
74
zip(zip, fileIn, base);
75
} catch (FileNotFoundException e) {
76
e.printStackTrace();
77
} finally {
78
try {
79
zip.close();
80
out.close();
81
} catch (IOException e) {
82
e.printStackTrace();
83
}
84
85
}
86
}
87
88
private void zip(ZipOutputStream zip, File fileIn, String base) {
89
if (fileIn.isDirectory()) {
90
File[] list = fileIn.listFiles();
91
base = base + fileIn.getName() + "/";// 在壓縮文件里建立文件夾
92
ZipEntry entry = new ZipEntry(base);
93
try {
94
zip.putNextEntry(entry);
95
} catch (IOException e) {
96
e.printStackTrace();
97
}
98
for (int i = 0; i < list.length; i++) {
99
zip(zip, list[i], base);
100
}
101
} else {
102
FileInputStream in = null;
103
try {
104
in = new FileInputStream(fileIn);
105
byte[] b = new byte[1024];
106
ZipEntry entry = new ZipEntry(base + fileIn.getName());
107
zip.putNextEntry(entry);
108
while (in.read(b) != -1) {
109
zip.write(b);
110
}
111
} catch (FileNotFoundException e) {
112
e.printStackTrace();
113
} catch (IOException e) {
114
e.printStackTrace();
115
}
116
}
117
118
}
119
120
/**
121
* 解壓
122
*
123
* @param befor
124
* @param after
125
*/
126
public void getZip(String befor, String after) {
127
File fileIn = new File(befor);
128
FileInputStream in = null;
129
FileOutputStream out = null;
130
ZipInputStream zip = null;
131
132
try {
133
in = new FileInputStream(fileIn);
134
zip = new ZipInputStream(in);
135
ZipEntry entry = null;
136
137
while ((entry = zip.getNextEntry()) != null) {
138
if (entry.isDirectory()) {// 判斷是文件夾還是文件
139
new File(after + entry.getName()).mkdirs();// 如果是文件夾就建立
140
} else {
141
out = new FileOutputStream(// 如果是文件就讀出來
142
new File(after + entry.getName()));
143
byte[] b = new byte[1024];
144
while (zip.read(b) != -1) {
145
out.write(b);
146
}
147
out.close();
148
}
149
}
150
} catch (FileNotFoundException e) {
151
e.printStackTrace();
152
} catch (IOException e) {
153
e.printStackTrace();
154
} finally {
155
try {
156
zip.close();
157
in.close();
158
} catch (IOException e) {
159
e.printStackTrace();
160
}
161
}
162
163
}
164
}
165

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

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165
