chen888的gravatar头像
chen888 2017-08-04 11:35:05

java压缩和解压指定文件夹下的文件实例

需求:

   现在有这样一个需求,有一个chen文件夹,下面有A/B两个文件夹,里面有分别有a1.jpg/a2.jpg和b1.jpg/b2.jpg四个图片,现在我想取出a1.jpg和b1.jpg,然后还按照这个结构打成一个zip包.

Java主要代码:

/**
     * 压缩文件或路径
     * 
     * @param zip
     *            压缩的目的地址
     * @param srcFiles
     *            压缩的源文件
     */
    public static void zipFile(String zip, List<File> srcFiles) {
        try {
            if (zip.endsWith(".zip") || zip.endsWith(".ZIP")) {
                ZipOutputStream _zipOut = new ZipOutputStream(
                        new FileOutputStream(new File(zip)));
                for (File _f : srcFiles) {
                    //选择指定压缩文件
                    if(_f.getName().equals("a1.jpg") || _f.getName().equals("b1.jpg"))
                        handlerFile(zip, _zipOut, _f, "");
                }
                _zipOut.close();
            } else {
                System.out.println("target file[" + zip+ "] is not .zip type file");
            }
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
    }

    /**
     * 
     * @param zip
     *            压缩的目的地址
     * @param zipOut
     * @param srcFile
     *            被压缩的文件信息
     * @param path
     *            在zip中的相对路径
     * @throws IOException
     */
    private static void handlerFile(String zip, ZipOutputStream zipOut,
            File srcFile, String path) throws IOException {
        System.out.println(" begin to compression file[" + srcFile.getName()
                + "]");
        if (!"".equals(path) && !path.endsWith(File.separator)) {
            path += File.separator;
        }
        if (!srcFile.getPath().equals(zip)) {
            if (srcFile.isDirectory()) {
                File[] _files = srcFile.listFiles();
                if (_files.length == 0) {
                    zipOut.putNextEntry(new ZipEntry(path + srcFile.getName()
                            + File.separator));
                    zipOut.closeEntry();
                } else {
                    for (File _f : _files) {
                        handlerFile(zip, zipOut, _f, path + srcFile.getName());
                    }
                }
            } else {
                InputStream _in = new FileInputStream(srcFile);
                zipOut.putNextEntry(new ZipEntry(path + srcFile.getName()));
                int len = 0;
                byte[] _byte = new byte[1024];
                while ((len = _in.read(_byte)) > 0) {
                    zipOut.write(_byte, 0, len);
                }
                _in.close();
                zipOut.closeEntry();
            }
        }
    }

 

压缩目录或文件夹:看具体项目代码,主要是自己记录一下这种需求的代码,方便以后自己遇到这种需求,不要重新写,大家有好的方法可以交流呢!!

实现效果图:

 

java压缩和解压指定文件夹下的文件实例


打赏

文件名:com.zip.zip,文件大小:19.817K 下载
最代码最近下载分享源代码列表最近下载
zzzyyy1  LV2 2月26日
cindyd  LV1 2021年11月1日
dybtom  LV10 2019年6月28日
武静123  LV1 2019年5月28日
18170035580  LV14 2018年11月3日
FunnyKing  LV19 2017年8月7日
最代码官方  LV167 2017年8月4日
最代码最近浏览分享源代码列表最近浏览
zzzyyy1  LV2 2月26日
一只小扒菜啊 2023年10月30日
暂无贡献等级
EFWAGGFAWGR 2023年10月19日
暂无贡献等级
zhangjilu  LV18 2023年2月18日
沙漏哦  LV9 2022年9月14日
yymmdm  LV6 2022年8月10日
whfuai  LV14 2022年7月27日
wanglinddad  LV53 2022年4月22日
李海洋  LV12 2021年12月1日
cindyd  LV1 2021年11月1日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友