会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
爱睇资讯
IT资讯,最新最前沿。我们的口号:能解决实际问题的软件才是好软件!
业界新闻均来自
華爾街日報 中文網
博客园
首页
新随笔
联系
订阅
管理
Java实现压缩一个目录下所有图片文件
import
java.io.
*
;
import
java.util.
*
;
import
java.util.zip.
*
;
/** */
/**
* 打包照片文件
*/
public
class
PhotoZip
{
public
static
void
main(String[] args)
{
if
(args.length
<
2
)
{
System.out.println(
"
Usage: java PhotoZip photodir target.zip
"
);
}
else
{
try
{
byte
[] buf
=
new
byte
[
1024
];
//
缓冲区
File dir
=
new
File(args[
0
]);
//
照片目录
ZipOutputStream zos
=
new
ZipOutputStream(
new
FileOutputStream(args[
1
]));
//
压缩包流
String[] files
=
dir.list();
//
列表照片目录下文件
String fPath
=
dir.getCanonicalPath();
if
(
!
fPath.endsWith(File.separator))
{
fPath
=
fPath
+
File.separator;
}
for
(
int
idx
=
0
; idx
<
files.length; idx
++
)
{
if
(files[idx].length()
>=
12
)
{
try
{
zos.putNextEntry(
new
ZipEntry(files[idx].substring(
0
,
8
)
+
"
.jpg
"
));
//
统一文件名为8位+扩展名.jpg
BufferedInputStream bis
=
new
BufferedInputStream(
new
FileInputStream(fPath
+
files[idx]));
while
(bis.read(buf,
0
,
1024
)
!=
-
1
)
{
zos.write(buf);
}
bis.close();
zos.closeEntry();
}
catch
(ZipException zexp)
{
zexp.printStackTrace();
}
}
}
zos.close();
}
catch
(Exception exp)
{
exp.printStackTrace();
}
}
}
}
;
posted @
2006-02-11 10:01
Rookie.Zhang
阅读(
970
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告