您好,欢迎来到画鸵萌宠网。
搜索
您的当前位置:首页java文件压缩实用文档

java文件压缩实用文档

来源:画鸵萌宠网
Java代码实现文件的压缩,压缩完成后删除源文件 private void compress(File f, String baseDir, ZipOutputStream zos) { if (!f.exists()) { return; }

File[] fs = f.listFiles(); BufferedInputStream bis = null; byte[] buff = new byte[1024 * 10]; FileInputStream fis = null; try {

for (int i = 0; i < fs.length; i++) { String fName = fs[i].getName(); if (fs[i].isFile()) {

ZipEntry zipEntry = new ZipEntry(baseDir + fName);

zos.putNextEntry(zipEntry); fis = new FileInputStream(fs[i]);

bis = new BufferedInputStream(fis, 1024 * 10);

int read = 0;

while ((read = bis.read(buff, 0, 1024 * 10)) != -1) {

zos.write(buff, 0, read); }

fis.close(); fs[i].delete();

} else if (fs[i].isDirectory()) {

compress(fs[i], baseDir + fName + \"/\", zos); fs[i].delete(); } }

f.delete();

} catch (Exception e) { e.printStackTrace(); } finally { try {

if (null != bis)

bis.close(); if (null != fis)

fis.close(); } catch (Exception e) { e.printStackTrace(); }

} }

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo8.com 版权所有 湘ICP备2023022238号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务