JS 与 trick 代码的运用及解析全攻略
859
2022-10-24
压缩文件
package com.java.charpter15; 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 MyZip { private void zip(String zipFileName,File inputFile) throws IOException { ZipOutputStream out=new ZipOutputStream(new FileOutputStream(inputFile)); zip(out, inputFile,""); System.out.println("压缩中..."); out.close(); } private void zip(ZipOutputStream out,File f,String base) throws IOException { if (f.isDirectory()) { File[] fl=f.listFiles(); out.putNextEntry(new ZipEntry(base+"/")); base=base.length()==0?"":base+"/"; for (int i = 0; i < fl.length; i++) { zip(out, f, base+fl[i]); } } else { out.putNextEntry(new ZipEntry(base)); //创建FileInputStream对象 FileInputStream in=new FileInputStream(f); int b; System.out.println(base); while ((b=in.read())!=-1) { out.write(b); } in.close(); } } public static void main(String[] args) { MyZip book=new MyZip(); try { book.zip("E:/hello.zip", new File("D:\Data")); System.out.println("压缩完成"); } catch (Exception e) { e.printStackTrace(); } } }
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~