package zip;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class ZipFile { public void zip(String filepath,String zippath) throws IOException { File fs = new File(filepath); ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(zippath)); zo.setComment("多文件压缩"); recursionZip(fs,zo,""); zo.close(); } private void recursionZip(File fs,ZipOutputStream zo,String baseDir) throws IOException { if(fs.isDirectory()) { File[] ff = fs.listFiles(); if(ff.length==0) { zo.putNextEntry(new ZipEntry(baseDir + fs.getName()+"/")); zo.closeEntry(); } for(File f : ff) { recursionZip(f,zo,baseDir + fs.getName() + File.separator); } }else { byte[] b = new byte[1024]; zo.putNextEntry(new ZipEntry(baseDir + fs.getName())); FileInputStream fi = new FileInputStream(fs); int len = 0; while((len = fi.read(b))!=-1) { zo.write(b,0,len); } fi.close(); zo.closeEntry(); } } public static void main(String[] args) { String filepath = "d:"+ File.separator + "hello"; String zippath = "d:" + File.separator + "hello.zip"; ZipFile zf = new ZipFile(); try { zf.zip(filepath,zippath); }catch(Exception e) { e.printStackTrace(); } }}
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦