博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java实现文件及目录压缩
阅读量:4703 次
发布时间:2019-06-10

本文共 4039 字,大约阅读时间需要 13 分钟。

package org.alfresco.repo.bom.util;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipOutputStream;/** * Compressor Util * @author HJ * */public class CompressorUtil {        private static final String source = "F:/test"; // wait compressor source path     private static final String zipSource = "F:/chiang.zip"; // after compressor zip file path    private static long startTime;// compressor start system time    private static long endTime;// compressor end system time        public void compressor() throws Exception{        startTime = System.currentTimeMillis();//record start compressor system time ,         boolean flag = false;// flag :true->compressor success        String baseDir = "";//defalut relative Dir , "" is gen Dir                File s = new File(source);        File zs = new File(zipSource);//create zip file        if (zs.exists()) {
// if this dir exists this zip file zs.delete(); // delete this zip file , } ZipOutputStream zos = null; try { zos = new ZipOutputStream(new FileOutputStream(zs)); zos.setEncoding("GBK"); // solve Chinese garbled startCompressor(baseDir, zos, s); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if (zos!=null) zos.close(); endTime = System.currentTimeMillis(); System.out.println("compressor success,use time:"+(endTime-startTime)+"ms"); } } public void startCompressor(String baseDir,ZipOutputStream zos,File source) throws Exception{ if (source.isFile()) {
// is file toCompressedFile(baseDir, zos, source); } if (source.isDirectory()) { //is dir File[] sources = source.listFiles(); // get dir all files ( file or dir) for(File f:sources){ if (f.isFile()) {
// is file toCompressedFile(baseDir, zos, f); } if (f.isDirectory()) {
//is dir // if is dir , update baseDir value . String newBaseDir = baseDir + f.getName() + "/"; createCompressedDir(baseDir, zos, f);//create dir and entry startCompressor(newBaseDir, zos, f); // Re } } } } /** * add entry to zip file by stream way * @param baseDir * @param zos * @param f * @throws Exception */ public void toCompressedFile(String baseDir,ZipOutputStream zos,File f) throws Exception{ InputStream input = null; ZipEntry z = new ZipEntry(baseDir+f.getName()); try { zos.putNextEntry(z); // add entry to zip file input = new FileInputStream(f); int data = 0; while ((data=input.read())!=-1) { zos.write(data); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(input!=null) input.close(); zos.closeEntry(); } } /** * create compressed file dir and ZipEntry * @param baseDir * @param zos zip file's ZipOutputStream * @param f */ public void createCompressedDir(String baseDir,ZipOutputStream zos,File f){ ZipEntry z = new ZipEntry(baseDir+f.getName()+"/"); try { zos.putNextEntry(z); zos.closeEntry(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //test main method public static void main(String[] args) throws Exception{ CompressorUtil cu = new CompressorUtil(); cu.compressor(); }}

 

转载于:https://www.cnblogs.com/chiangfai/p/5828191.html

你可能感兴趣的文章
java编程基础(三)流程控制语句
查看>>
让数据库跑的更快的7个MySQL优化建议
查看>>
jquery 取id模糊查询
查看>>
解决在vue中,自用mask模态框出来后,下层的元素依旧可以滑动的问题
查看>>
修改node节点名称
查看>>
PAT(B) 1014 福尔摩斯的约会(Java)
查看>>
PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树
查看>>
项目开发总结报告(GB8567——88)
查看>>
SSH加固
查看>>
端口扫描base
查看>>
iOS IM开发的一些开源、框架和教程等资料
查看>>
FansUnion:共同写博客计划终究还是“流产”了
查看>>
python 二维字典
查看>>
Arrays类学习笔记
查看>>
实验吧之【天下武功唯快不破】
查看>>
2019-3-25多线程的同步与互斥(互斥锁、条件变量、读写锁、自旋锁、信号量)...
查看>>
win7-64 mysql的安装
查看>>
dcm4chee 修改默认(0002,0013) ImplementationVersionName
查看>>
maven3在eclipse3.4.2中创建java web项目
查看>>
发布时间 sql语句
查看>>