zj
2024-06-04 64eea4421c603de5f01b6519a271283a0d181b3d
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
131
package db.util;
 
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.ZipInputStream;
import java.util.zip.ZipOutputStream;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
public class ZipUtils {
    /**    
     * logger
     */
    private  static final Logger logger = LoggerFactory.getLogger(ZipUtils.class); 
 
    /**    
     * <p>Description: 压缩文件     </p>
     * 
     * @param zname   生成文件名
     * @param source  待压缩文件
     */
    public static void createZip(String zname, String source) {
        logger.info("start compressing backup files...");
        try {
            File filezip = new File(zname);
            if (!filezip.exists()) {
                filezip.createNewFile();
            }
          
            ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(zname));
            compressFile(zout, "", new File(source));
            zout.close();
            logger.info("compress backup files finished");
        } catch (Exception e) {
            logger.error("compress backup files error", e);
        }
    }
 
    /**    
     * <p>Description: 压缩文件             </p>
     * 
     * @param zout     ZipOutputStream
     * @param dir      相对路径
     * @param source   源文件
     * @throws IOException ioException
     */
    private static void compressFile(ZipOutputStream zout, String dir, File source) throws IOException {
        if (logger.isDebugEnabled()) {
            logger.debug("do compressing file:" + source);
        }
        if (source.isDirectory()) {
            File[] fs = source.listFiles();
            dir = dir + (dir.length() == 0 ? "" : "/") + source.getName();
            for (File f : fs) {
                compressFile(zout, dir, f);
            }
        }
        else {
            dir = dir.length() == 0 ? "" : dir + "/" + source.getName();
            ZipEntry entry = new ZipEntry(dir);
 
            // ZipEntry entry = new ZipEntry(source.getName());
            zout.putNextEntry(entry);
            FileInputStream fin = new FileInputStream(source);
            int size = 0;
            byte[] buf = new byte[10240];
            while ((size = fin.read(buf, 0, 10240)) != -1) {
                zout.write(buf, 0, size);
            }
            fin.close();
        }
    }
 
    /**    
     * <p>Description: 解压文件             </p>
     * 
     * @param source    源文件
     * @param destdir   目的目录
     */
    public static void unZip(String source, String destdir) {
        try {
            logger.info("start decompressing backup files...");
            FileInputStream fin = new FileInputStream(source);
            ZipInputStream zin = new ZipInputStream(fin);
 
            ZipEntry entry = null;
            while ((entry = zin.getNextEntry()) != null) {
 
                File filename = new File(destdir + entry.getName());
                if (logger.isDebugEnabled()) {
                    // System.out.println("正在解压" + entry.getName());
                    logger.debug("do decompressing file:" + entry.getName());
                }
 
                File path = new File(filename.getParentFile().getPath()); // 确认目标目录存在、解压文件夹存在
                if (entry.isDirectory()) {
                    if (!filename.exists()) {
                        filename.mkdirs();
                    }
                    zin.closeEntry();
                    continue;
                }
                if (!path.exists()) {
                    path.mkdirs();
                }
 
                FileOutputStream fout = new FileOutputStream(filename); // 得到目标文件流
 
                int size = 0;
                byte[] buf = new byte[256];
                while ((size = zin.read(buf)) != -1) { // 每个entry都有一个文件末尾的标识
                    fout.write(buf, 0, size);
                }
                zin.closeEntry();
                fout.close();
            }
 
            fin.close();
            zin.close();
            // System.out.println("解压完成!");
            logger.info("decompress backup files finished");
        } catch (Exception e) {
            logger.error("decompress backup files error", e);
        }
    }
 
}