2 * Copyright (C) 2017 CTC, Inc. and others. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.usecaseui.server.util;
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;
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;
31 public class ZipUtil {
33 public static void zip(String src, String zip) throws IOException {
34 zip(new File(src), new File(zip));
37 public static void zip(String src, File zip) throws IOException {
38 zip(new File(src), zip);
41 public static void zip(File src, String zip) throws IOException {
42 zip(src, new File(zip));
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());
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);
62 public static void unzip(String zip,String out) throws Exception {
63 unzip(new File(zip), new File(out));
66 public static void unzip(String zip,File out) throws Exception {
67 unzip(new File(zip), out);
70 public static void unzip(File zip,String out) throws Exception {
71 unzip(zip, new File(out));
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()) {
82 File parent = file.getParentFile();
83 if (!parent.exists()) {
86 IOUtils.copy(zipFile.getInputStream(entry), new FileOutputStream(file));
91 private static List<ZipEntry> foreach(File file) {
92 return foreach(file, "");
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));
101 } else if (file.isFile()) {
102 list.add(new ZipEntry(path + file.getName()));