管道基础大数据平台系统开发-【后端】-Server
13693261870
2022-10-20 3249d41536899bbacf88af45f290db3efdbdc790
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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) {
        try {
            int count;
            ZipFile 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();
                is.close();
            }
 
            return true;
        } catch (Exception ex) {
            log.error(ex.getMessage() + ex.getStackTrace() + "\n");
            return false;
        }
    }
 
    /**
     * Zip压缩
     *
     * @param zipFile   zip源文件
     * @param sourceDir 源文件夹
     * @return 成功是/否
     */
    public static boolean zip(String zipFile, String sourceDir) {
        ZipOutputStream zos = null;
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
            zos = new ZipOutputStream(fileOutputStream);
 
            File sourceFile = new File(sourceDir);
            compress(sourceFile, zos, sourceFile.getName());
 
            return true;
        } catch (Exception ex) {
            log.error(ex.getMessage() + ex.getStackTrace() + "\n");
            return false;
        } finally {
            try {
                if (zos != null) {
                    zos.close();
                }
            } catch (Exception e) {
                log.error(e.getMessage() + e.getStackTrace() + "\n");
            }
        }
    }
 
    /**
     * 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());
                }
            }
        }
    }
}