cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package cn.ksource.core.util;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
 
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
 
public class ZipUtil {
    /** 
     * 压缩目录 
     *  
     * @param zos zip包输出流 
     * @param dirPath 被压缩目录的路径 
     * @param basePath 压缩进zip包里面相对于zip根目录的路径 
     * @throws Exception 
     */  
    private static void zipDirectory(ZipOutputStream zos, String dirPath, String basePath) throws IOException {  
        File dir = new File(dirPath);  
        if (dir.exists()) {  
            File files[] = dir.listFiles();  
      
            if (files.length > 0) // 不为空目录情况  
            {  
                for (int i = 0; i < files.length; i++) {  
      
                    if (files[i].isDirectory()) {  
                        zipDirectory(zos, files[i].getPath(), basePath + files[i].getName() // .substring(files[i].getName().lastIndexOf(File.separator)  
                                                                                            // + 1)  
                                                              + File.separator);  
                    } else {  
                        zipFile(zos, files[i].getPath(), basePath);  
                    }  
      
                }  
            } else // 把空目录加入ZIP条目  
            {  
                if (File.separatorChar != '/') { // 属性于ZipEntry的缺陷,不支持windows的路径分隔符"\"  
                    basePath = basePath.replace("\\", "/");  
                }  
                ZipEntry ze = new ZipEntry(basePath); // 使用指定名称创建新的 ZIP 条目  
                zos.putNextEntry(ze); // 加入ZIP条目操作!  
            }  
        }  
    }  
      
    /** 
     * 压缩文件 
     *  
     * @param zos zip包输出流 
     * @param filePath 被压缩文件的路径 
     * @param basePath 压缩进zip包里面相对于zip根目录的路径 
     * @throws IOException 
     * @throws Exception 
     */  
    private static void zipFile(ZipOutputStream zos, String filePath, String basePath) throws IOException {  
        File file = new File(filePath);  
      
        if (file.exists()) {  
      
            FileInputStream fis = null;  
      
            try {  
                fis = new FileInputStream(filePath);  
                if (File.separatorChar != '/') { // 属性于ZipEntry的缺陷,不支持windows的路径分隔符"\"  
                    basePath = basePath.replace("\\", "/");  
                }  
                ZipEntry ze = new ZipEntry(basePath + file.getName()); // zip条目要有相对于ZIP文件根目录的路径  
                zos.putNextEntry(ze); // 先进行ZIP条目加入操作再进行读取与写到输出流  
                byte[] buffer = new byte[8192];  
                int count = 0;  
                while ((count = fis.read(buffer)) > 0) {  
                    zos.write(buffer, 0, count);  
                }  
            } finally {  
                if (fis != null) {  
                    fis.close();  
                }  
            }  
      
        }  
    }  
      
    private static void compress(OutputStream os, String[] paths) throws IOException {  
        ZipOutputStream zos = null;  
        try {  
            zos = new ZipOutputStream(os); // /  
      
            for (int i = 0; i < paths.length; i++) // 遍历每个可生成File对象的路径  
            {  
                if (paths[i].equals(""))  
                    continue;  
                java.io.File file = new java.io.File(paths[i]);  
                if (file.exists()) {  
      
                    if (file.isDirectory()) // 目录情形  
                    {  
                        zipDirectory(zos, file.getPath(), file.getName() + File.separator);  
                    } else // 文件情形  
                    {  
                        zipFile(zos, file.getPath(), ""); // 程序刚进入时创建的ZIP条目在根目录下,所以第三个参数为""  
                    }  
                }  
            }  
        } finally {  
            if (zos != null) {  
                zos.close();  
            }  
        }  
      
    }  
      
    /** 
     * 将路径列表所指向的文件或目录压缩到指定位置 
     *  
     * @param zipFilename 指定压缩后的zip文件的路径与名称 
     * @param paths 指定要压缩的包含文件或目录的路径列表 
     * @throws Exception 
     */  
      
    public static void compress(String zipFilename, String[] paths) throws IOException {  
        compress(new FileOutputStream(zipFilename), paths);  
      
    }  
      
    /** 
     * 将指定的zip压缩文件解压到指定路径 (解压到当前文件夹)
     *  
     * @param unzipPath 指定解压后的路径 
     * @param zipFilePath 指定要解压的zip文件 
     * @throws IOException 
     */  
    public static void decompress(String unzipPath, String zipFilePath) throws IOException {  
        FileOutputStream fileOut = null;  
        File file;  
        File unzip = new File(unzipPath);  
        InputStream inputStream = null;  
        byte[] buffer = new byte[8192];  
        int count = 0;  
        ZipFile zipFile = null;  
        try {  
      
            zipFile = new ZipFile(zipFilePath);  
      
            for (Enumeration entries = zipFile.getEntries(); entries.hasMoreElements();) {  
                ZipEntry entry = (ZipEntry) entries.nextElement();
                System.out.println("unzip.getPath()================"+unzip.getPath());
                System.out.println("entry.getName()================"+entry.getName());
                file = new File(unzip.getPath() + File.separator + entry.getName());  
                if (entry.isDirectory()) {  
                    file.mkdirs();  
                } else {  
                    // 如果指定文件的目录不存在,则创建之.  
                    File parent = file.getParentFile();  
                    if (!parent.exists()) {  
                        parent.mkdirs();  
                    }  
                    try {  
                        inputStream = zipFile.getInputStream(entry);  
      
                        fileOut = new FileOutputStream(file);  
      
                        while ((count = inputStream.read(buffer)) > 0) {  
                            fileOut.write(buffer, 0, count);  
                        }  
                    } finally {  
                        if (fileOut != null) {  
                            fileOut.close();  
                        }  
                        if (inputStream != null) {  
                            inputStream.close();  
                        }  
                    }  
                }  
            }  
        } finally {  
            if (zipFile != null) {  
                zipFile.close();  
            }  
        }  
      
    }
    
    
    
    /**
     * 创建目录
     * 
     * @param path
     *            目录绝对路径名
     */
    static void createDir(String path) {
     File dir = new File(path);
     if (dir.exists() == false)
      dir.mkdir();
    }
 
    /**
     * 取得文件名,不包含后缀名
     * 
     * @param name
     *            完整文件名
     * @return 文件名(不包含后缀名)
     */
    static String getSuffixName(String name) {
     return name.substring(0, name.lastIndexOf("."));
    }
    
    
    /**
     * 解压zip文件(解压到文件夹名路径)
     * 
     * @param zipFilePath
     *            zip文件绝对路径
     * @param unzipDirectory
     *            解压到的确
     * @throws Exception
     */
    public static String unzip(String zipFilePath, String unzipDirectory,String homePage)
      throws Exception {
        
     String page = new String();
     
     // 创建文件对象
     File file = new File(zipFilePath);
     // 创建zip文件对象
     ZipFile zipFile = new ZipFile(file);
     // 创建本zip文件解压目录
     File unzipFile = new File(unzipDirectory + "/"
       + getSuffixName(file.getName()));
     if (unzipFile.exists())
      unzipFile.delete();
     unzipFile.mkdir();
     // 得到zip文件条目枚举对象
     Enumeration zipEnum = zipFile.getEntries();
     //Enumeration zipEnum = (Enumeration) zipFile.getEntry("112");
     // 定义输入输出流对象
     InputStream input = null;
     OutputStream output = null;
     // 定义对象
     ZipEntry entry = null;
     // 循环读取条目
     while (zipEnum.hasMoreElements()) {
      // 得到当前条目
      entry = (ZipEntry) zipEnum.nextElement();
      String entryName = new String(entry.getName());
      if(entryName.contains(homePage)) {
          page = entryName;
      }
      // 用/分隔条目名称
      String names[] = entryName.split("\\/");
      int length = names.length;
      String path = unzipFile.getAbsolutePath();
      for (int v = 0; v < length; v++) {
       if (v < length - 1) { // 最后一个目录之前的目录
        path += "/" + names[v] + "/";
        createDir(path);
       } else { // 最后一个
        if (entryName.endsWith("/")) // 为目录,则创建文件夹
         createDir(unzipFile.getAbsolutePath() + "/" + entryName);
        else { // 为文件,则输出到文件
         input = zipFile.getInputStream(entry);
         output = new FileOutputStream(new File(unzipFile
           .getAbsolutePath()
           + "/" + entryName));
         byte[] buffer = new byte[1024 * 8];
         int readLen = 0;
         while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1)
          output.write(buffer, 0, readLen);
         // 关闭流
         input.close();
         output.flush();
         output.close();
        }
       }
      }
     }
     return page;
    }
    
    
    public static void main(String[] args) {
        try {
            System.out.println(unzip("D:\\abc\\abc.zip","D:\\abc","index.html"));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}