java文件压缩 apache开源包_通过apache实现文件&文件夹目录的打包压缩工具类ZipUtils...

通过apache开源包ant-1.9.4.jar(下载)的工具包中zip提供ZipOutputStream、ZipEntry类来实现对文件或文件夹目录的压缩打包,代码工具类ZipUtils如下package xwood.tools;@b@@b@import java.io.BufferedInputStream;@b@import java.io.BufferedOutputStream;@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.FileOutputStream;@b@import java.io.InputStream;@b@import java.io.OutputStream;@b@@b@import org.apache.tools.zip.ZipEntry;@b@import org.apache.tools.zip.ZipOutputStream;@b@@b@public class ZipUtils {@b@    /**@b@     * 压缩文件@b@     * @param sourceDir@b@     *        原文件目录@b@     * @param zipFile@b@     *        压缩后的文件名称@b@     * @throws Exception@b@     */@b@    public static void doZip(String sourceDir, File zipFile) throws Exception{@b@        OutputStream os= new FileOutputStream(zipFile);@b@        BufferedOutputStream bos = new BufferedOutputStream(os);@b@        ZipOutputStream zos = new ZipOutputStream(bos);@b@        File file = new File(sourceDir);@b@        String basePath = null;@b@        if (file.isDirectory()) {@b@            basePath = file.getPath();@b@        } else {@b@            basePath = file.getParent();@b@        }@b@        zipFile(file, basePath, zos);@b@        zos.closeEntry();@b@        zos.close();@b@    }@b@@b@    /**@b@     * 压缩文件@b@     * @param source@b@     * @param basePath@b@     * @param zos@b@     * @throws Exception@b@     */@b@    private static void zipFile(File source, String basePath,ZipOutputStream zos) throws Exception {@b@        File[] files = new File[0];@b@        if (source.isDirectory()) {@b@            files = source.listFiles();@b@        } else {@b@            files = new File[1];@b@            files[0] = source;@b@        }@b@        String pathName;@b@        byte[] buf = new byte[1024];@b@        int length = 0;@b@        for (File file : files) {@b@            if (file.isDirectory()) {@b@                pathName = file.getPath().substring(basePath.length() + 1)+ "/";@b@                zos.putNextEntry(new ZipEntry(pathName));@b@                zipFile(file, basePath, zos);@b@            } else {@b@                pathName = file.getPath().substring(basePath.length() + 1);@b@                InputStream is = new FileInputStream(file);@b@                BufferedInputStream bis = new BufferedInputStream(is);@b@                zos.putNextEntry(new ZipEntry(pathName));@b@                while ((length = bis.read(buf)) > 0) {@b@                    zos.write(buf, 0, length);@b@                }@b@                is.close();@b@            }@b@        }@b@    }@b@    @b@    public static void main(String[] args) throws Exception{@b@        ZipUtils.doZip("C:/NJ/bak/js",new File("C:/NJ/bak/js_bak.zip"));@b@    }@b@@b@}

运行结果如下图所示

174b4bb58b116c392e4d3f285f70cf2d.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值