提示:java.util.zipoutputstream
java api压缩为zip文件
代码:
public static final int buffer_size = 1024;
public static void main(string[] args) throws ioexception {
string src = "d:\\chat";
string des = "d:\\chat01.zip";
zipoutputstream zos = null;
try{
zos = new zipoutputstream(new fileoutputstream(des));
file srcfile = new file(src);
string base = srcfile.getname();
filezip(srcfile,zos,base);
}catch (exception e) {
// todo: handle exception
e.printstacktrace();
}finally{
if(zos!=null){
zos.close();
}
}
system.out.println("文件压缩成功啦" + src);
}
private static void filezip(file srcfile, zipoutputstream zos, string base)
throws exception{
// todo auto-generated method stub
if(!srcfile.exists()){
system.out.println("文件不存在" + srcfile.getpath());
}
if(srcfile.isfile()){
zos.putnextentry(new zipentry(base));
fileinputstream fis = new fileinputstream(srcfile);
byte[] buf = new byte[buffer_size];
int n=0;
while((n=fis.read(buf, 0, buf.length))!=-1){
zos.write(buf, 0, n);
}
fis.close();
}else{
if(srcfile.isdirectory()) {
base = base + file.separator;
file[] subfiles = srcfile.listfiles();
for (file subfile : subfiles) {
filezip(subfile, zos, base + subfile.getname());
}
}
}
}
}
如对本文有疑问, 点击进行留言回复!!
before社区电量是什么意思 Before社区电量获得方法
RecycleView入门详解(教你全面掌握RecycleView用法)
动态权限请求框架RxPermissions(几行代码搞定权限)
URL路径@PathVariable出现点号“.“时值遭截断问题
网友评论