mysileng
導航
BlogJava
首頁
新隨筆
聯(lián)系
聚合
管理
<
2009年5月
>
日
一
二
三
四
五
六
26
27
28
29
30
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
1
2
3
4
5
6
留言簿
(1)
給我留言
查看公開留言
查看私人留言
隨筆分類
HTML學習(3)
(rss)
隨筆檔案
2009年7月 (3)
2009年5月 (2)
文章檔案
2009年5月 (1)
閱讀排行榜
1.?第一課時 HTML入門與基本標記(1221)
2.?第三課時 HTML表單和框架(555)
3.?第二課時 HTML超連接和表格(487)
4.?文件夾導入與導出(214)
5.?學習java(142)
評論排行榜
1.?第三課時 HTML表單和框架(0)
2.?第二課時 HTML超連接和表格(0)
3.?第一課時 HTML入門與基本標記(0)
4.?文件夾導入與導出(0)
5.?學習java(0)
常用鏈接
我的隨筆
我的評論
我的參與
統(tǒng)計
隨筆 - 5
文章 - 1
評論 - 0
引用 - 0
最新評論
文件夾導入與導出
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
posted on 2009-05-24 00:21
鑫龍
閱讀(214)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Powered by:
BlogJava
Copyright © 鑫龍
主站蜘蛛池模板:
龙泉市
|
柏乡县
|
辽阳县
|
彭州市
|
四川省
|
宕昌县
|
阿拉善左旗
|
宜兰市
|
遂昌县
|
新泰市
|
济源市
|
佛坪县
|
伊春市
|
锡林郭勒盟
|
宁阳县
|
彭泽县
|
乐清市
|
阿拉善右旗
|
平舆县
|
集贤县
|
屏东县
|
长顺县
|
永安市
|
任丘市
|
沙湾县
|
镇平县
|
迁安市
|
遵义市
|
河间市
|
宁明县
|
怀来县
|
博兴县
|
定日县
|
沂水县
|
徐闻县
|
金昌市
|
鸡泽县
|
山西省
|
容城县
|
浮山县
|
长岛县
|