服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java建立zip压缩文件


  import java.io.*;

import java.util.zip.*;

class zip {

public static void main(string args[]) throws ioexception {

byte b[] = new byte[512];

zipoutputstream zout = new zipoutputstream(system.out);

for(int i = 0; i < args.length; i ++) {

inputstream in = new fileinputstream(args[i]);

zipentry e = new zipentry(args[i].replace(file.separatorchar,'/'));

zout.putnextentry(e);

int len=0;

while((len=in.read(b)) != -1) {

zout.write(b,0,len);

}

zout.closeentry();

print(e);

}

zout.close();

}



public static void print(zipentry e){

printstream err = system.err;

err.print("added " + e.getname());

if (e.getmethod() == zipentry.deflated) {

long size = e.getsize();

if (size > 0) {

long csize = e.getcompressedsize();

long ratio = ((size-csize)*100) / size;

err.println(" (deflated " + ratio + "%)");

}

else {

err.println(" (deflated 0%)");

}

}

else {

err.println(" (stored 0%)");

}

}

}

扫描关注微信公众号