java压缩文件夹zip

代码
   
     
import java.io. * ;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileZip {
public static void main(String[] args)
throws Exception {
File f
= new File( " d:/taobao " );
ZipOutputStream out
= new ZipOutputStream( new FileOutputStream(
" d:/test.zip " ));
zip(out, f,
null );
System.out.println(
" zip done " );
out.close();
}

private static void zip(ZipOutputStream out, File f, String base)
throws Exception {
System.out.println(
" zipping " + f.getAbsolutePath());
if (f.isDirectory()) {
File[] fc
= f.listFiles();
if (base != null )
out.putNextEntry(
new ZipEntry(base + " / " ));
base
= base == null ? "" : base + " / " ;
for ( int i = 0 ; i < fc.length; i ++ ) {
zip(out, fc[i], base
+ fc[i].getName());
}
}
else {
out.putNextEntry(
new ZipEntry(base));
FileInputStream in
= new FileInputStream(f);
int b;
while ((b = in.read()) != - 1 )
out.write(b);
in.close();
}
}
}

 

你可能感兴趣的:(java压缩)