管道基础大数据平台系统开发-【后端】-Server
1
sws
2022-11-26 ab849f796bdc17236a95ea5fe5c166fb8f24a75c
src/main/java/com/lf/server/helper/ZipHelper.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,145 @@
package com.lf.server.helper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
/**
 * Zip帮助类
 * @author WWW
 */
public class ZipHelper {
    private final static int BUFFER_SIZE = 1024;
    private final static byte[] BUFFER = new byte[1024];
    private final static Log log = LogFactory.getLog(ZipHelper.class);
    /**
     * Zip解压
     *
     * @param filePath zip文件
     * @param zipDir   è§£åŽ‹è·¯å¾„
     * @return æˆåŠŸæ˜¯/否
     */
    public static boolean unzip(String filePath, String zipDir) {
        ZipFile zipFile = null;
        try {
            int count;
            zipFile = new ZipFile(filePath);
            Enumeration e = zipFile.entries();
            while (e.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) e.nextElement();
                if (entry.isDirectory()) {
                    File f = new File(zipDir + File.separator + entry.getName());
                    if (!f.exists()) {
                        f.mkdirs();
                    }
                    continue;
                }
                BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));
                FileOutputStream fos = new FileOutputStream(zipDir + File.separator + entry.getName());
                BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE);
                while ((count = is.read(BUFFER, 0, BUFFER_SIZE)) != -1) {
                    dest.write(BUFFER, 0, count);
                }
                dest.flush();
                dest.close();
                fos.close();
                is.close();
            }
            return true;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return false;
        } finally {
            try {
                if (null != zipFile) {
                    zipFile.close();
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    }
    /**
     * Zip压缩
     *
     * @param zipFile   zip源文件
     * @param sourceDir æºæ–‡ä»¶å¤¹
     * @return æˆåŠŸæ˜¯/否
     */
    public static boolean zip(String zipFile, String sourceDir) {
        FileOutputStream fos = null;
        ZipOutputStream zos = null;
        try {
            fos = new FileOutputStream(zipFile);
            zos = new ZipOutputStream(fos);
            File sourceFile = new File(sourceDir);
            compress(sourceFile, zos, sourceFile.getName());
            return true;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return false;
        } finally {
            try {
                if (null != zos) {
                    zos.close();
                }
                if (null != fos) {
                    fos.close();
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    }
    /**
     * Zip压缩
     *
     * @param sourceFile zip源文件
     * @param zos        zip输出流
     * @param name       zip文件名称
     * @throws Exception
     */
    public static void compress(File sourceFile, ZipOutputStream zos, String name) throws Exception {
        if (sourceFile.isFile()) {
            // å‘zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
            zos.putNextEntry(new ZipEntry(name));
            // copy文件到zip输出流中
            int len;
            FileInputStream in = new FileInputStream(sourceFile);
            while ((len = in.read(BUFFER)) != -1) {
                zos.write(BUFFER, 0, len);
            }
            zos.closeEntry();
            in.close();
        } else {
            File[] listFiles = sourceFile.listFiles();
            if (listFiles == null || listFiles.length == 0) {
                // ç©ºæ–‡ä»¶å¤¹çš„处理:没有文件,不需要文件的copy
                zos.putNextEntry(new ZipEntry(name + "/"));
                zos.closeEntry();
            } else {
                for (File file : listFiles) {
                    compress(file, zos, name + "/" + file.getName());
                }
            }
        }
    }
}