java压缩zip

原生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();
    }


}

posted @ 2022-04-02 16:14  zpk-aaron  阅读(132)  评论(0编辑  收藏  举报