e3a6775194dd815ce5c124fd5b474e40a2d2313d
[usecase-ui/server.git] / server / src / main / java / org / onap / usecaseui / server / util / ZipUtil.java
1 /*
2  * Copyright (C) 2017 CTC, Inc. and others. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.usecaseui.server.util;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Enumeration;
24 import java.util.List;
25
26 import org.apache.commons.io.IOUtils;
27 import org.apache.tools.zip.ZipEntry;
28 import org.apache.tools.zip.ZipFile;
29 import org.apache.tools.zip.ZipOutputStream;
30
31 public class ZipUtil {
32
33     public static void zip(String src, String zip) throws IOException {
34         zip(new File(src), new File(zip));
35     }
36
37     public static void zip(String src, File zip) throws IOException {
38         zip(new File(src), zip);
39     }
40
41     public static void zip(File src, String zip) throws IOException {
42         zip(src, new File(zip));
43     }
44
45     public static void zip(File src, File zip) throws IOException {
46         List<ZipEntry> list = foreach(src);
47         ZipOutputStream out = new ZipOutputStream(zip);
48         for (ZipEntry en : list) {
49             File fo = new File(src.getParent(), en.getName());
50             out.putNextEntry(en);
51             FileInputStream in = new FileInputStream(fo);
52             byte[] buffer = new byte[1024*8];
53             for(int len=0;(len=in.read(buffer))!=-1;){
54                 out.write(buffer, 0, len);
55             }
56             in.close();
57             out.flush();
58         }
59         out.close();
60     }
61
62     public static void unzip(String zip,String out) throws Exception {
63         unzip(new File(zip), new File(out));
64     }
65
66     public static void unzip(String zip,File out) throws Exception {
67         unzip(new File(zip), out);
68     }
69
70     public static void unzip(File zip,String out) throws Exception {
71         unzip(zip, new File(out));
72     }
73
74     public static void unzip(File zip,File out) throws Exception {
75         ZipFile zipFile = new ZipFile(zip,"GB18030");
76         for (Enumeration<ZipEntry> entries = zipFile.getEntries(); entries.hasMoreElements();) {
77             ZipEntry entry = entries.nextElement();
78             File file = new File(out,entry.getName());
79             if (entry.isDirectory()) {
80                 file.mkdirs();
81             } else {
82                 File parent = file.getParentFile();
83                 if (!parent.exists()) {
84                     parent.mkdirs();
85                 }
86                 IOUtils.copy(zipFile.getInputStream(entry), new FileOutputStream(file));
87             }
88         }
89         zipFile.close();
90     }
91     private static List<ZipEntry> foreach(File file) {
92         return foreach(file, "");
93     }
94     private static List<ZipEntry> foreach(File file, String path) {
95         List<ZipEntry> list = new ArrayList<ZipEntry>();
96         if (file.isDirectory()) {
97             path += file.getName() + File.separator;
98             for (File fo : file.listFiles()) {
99                 list.addAll(foreach(fo, path));
100             }
101         } else if (file.isFile()) {
102             list.add(new ZipEntry(path + file.getName()));
103         }
104         return list;
105     }
106 }