• java压缩zip zpk


    原生java压缩文件为zip

    package com.icc.utils;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    /**
     * <p>
     * <p>
     * 2021/12/9 11:31
     *
     * @author ZhangPengKang
     * @version 1.0
     */
    @SuppressWarnings("unused")
    public class UZipUtil {
    
    
        /**
         * 完成的结果文件--输出的压缩文件
         */
        File targetFile;
    
        public UZipUtil(File target) {
            targetFile = target;
            if (targetFile.exists()) {
                boolean delete = targetFile.delete();
            }
        }
    
        /**
         * 压缩文件
         */
        private void zipFiles(File srcFile) {
    
            ZipOutputStream out = null;
            try {
                out = new ZipOutputStream(new FileOutputStream(targetFile));
    
                if(srcFile.isFile()){
                    zipFile(srcFile, out, "");
                } else{
                    File[] list = srcFile.listFiles();
                    if (list != null) {
                        for (File file : list) {
                            compress(file, out, "");
                        }
                    }
                }
                System.out.println("压缩完毕");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (out != null)
                        out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * 压缩文件夹里的文件
         */
        private void compress(File file, ZipOutputStream out, String basedir) {
            /* 判断是目录还是文件 */
            if (file.isDirectory()) {
                this.zipDirectory(file, out, basedir);
            } else {
                this.zipFile(file, out, basedir);
            }
        }
    
        /**
         * 压缩单个文件
         */
        private void zipFile(File srcfile, ZipOutputStream out, String basedir) {
            if (!srcfile.exists())
                return;
    
            byte[] buf = new byte[1024];
            FileInputStream in = null;
    
            try {
                int len;
                in = new FileInputStream(srcfile);
                out.putNextEntry(new ZipEntry(basedir + srcfile.getName()));
    
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (out != null)
                        out.closeEntry();
                    if (in != null)
                        in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * 压缩文件夹
         */
        private void zipDirectory(File dir, ZipOutputStream out, String basedir) {
            if (!dir.exists())
                return;
            File[] files = dir.listFiles();
            if (files != null) {
                for (File file : files) {
                    /* 递归 */
                    compress(file, out, basedir + dir.getName() + "/");
                }
            }
        }
    
    
        public static String doCompression(String src, String tagDir) {
            File td = new File(tagDir);
            if (!td.exists()) {
                boolean mkdirs = td.mkdirs();
            }
            File srcFile = new File(src);
            File tagFile = new File(tagDir + File.separator + srcFile.getName() + ".zip");
            new UZipUtil(tagFile).zipFiles(srcFile);
            return tagFile.getPath();
        }
    
        public static String doCompression(String src, String tagDir, String fileName) {
            return doCompression(src, tagDir, fileName, "zip");
        }
    
        public static String doCompression(String src, String tagDir, String fileName, String fileType) {
            File td = new File(tagDir);
            if (!td.exists()) {
                boolean mkdirs = td.mkdirs();
            }
            File srcFile = new File(src);
            File tagFile = new File(tagDir + File.separator + fileName + "." + fileType);
            new UZipUtil(tagFile).zipFiles(srcFile);
            return tagFile.getPath();
        }
    
    
    }
    
    
  • 相关阅读:
    MybatisProperties注册IOC容器和初始化
    Springboot源码之application.yaml读取过程
    DataSource的注册容器和初始化
    修改ha_config配置文件
    读书笔记--Python基础教程 001
    Python实现购物车小程序
    Python3实现三级菜单
    实现用户登录并且输入错误三次后锁定该用户
    day1-python 的基础部分
    翻译:《实用的Python编程》06_02_Customizing_iteration
  • 原文地址:https://www.cnblogs.com/zpKang/p/16092671.html
Copyright © 2020-2023  润新知