308adb27a585aa5a9947771e7a8a5ba50f51616a
[vfc/nfvo/driver/vnfm/gvnfm.git] /
1 /*
2  * Copyright (c) 2016, Huawei Technologies Co., Ltd.
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
17 package org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common;
18
19 import java.io.BufferedInputStream;
20 import java.io.BufferedOutputStream;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.zip.GZIPInputStream;
31 import java.util.zip.ZipEntry;
32 import java.util.zip.ZipInputStream;
33
34 import org.apache.commons.compress.archivers.ArchiveInputStream;
35 import org.apache.commons.compress.archivers.ArchiveStreamFactory;
36 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
37 import org.apache.commons.compress.compressors.CompressorStreamFactory;
38 import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * <br/>
44  * <p>
45  * </p>
46  * 
47  * @author quanzhong@huawei.com
48  * @version NFVO 0.5 Oct 28, 2016
49  */
50 public class UnCompressUtil {
51
52     private static Logger log = LoggerFactory.getLogger(UnCompressUtil.class);
53
54     private static Map<String, String> archiveMap = new HashMap<String, String>();
55
56     private static Map<String, String> compressorMap = new HashMap<String, String>();
57
58     static {
59         // archive type
60         archiveMap.put(ArchiveStreamFactory.AR, ArchiveStreamFactory.AR);
61         archiveMap.put(ArchiveStreamFactory.ARJ, ArchiveStreamFactory.ARJ);
62         archiveMap.put(ArchiveStreamFactory.CPIO, ArchiveStreamFactory.CPIO);
63         archiveMap.put(ArchiveStreamFactory.DUMP, ArchiveStreamFactory.DUMP);
64         archiveMap.put(ArchiveStreamFactory.JAR, ArchiveStreamFactory.JAR);
65         archiveMap.put(ArchiveStreamFactory.SEVEN_Z, ArchiveStreamFactory.SEVEN_Z);
66         archiveMap.put(ArchiveStreamFactory.TAR, ArchiveStreamFactory.TAR);
67         archiveMap.put(ArchiveStreamFactory.ZIP, ArchiveStreamFactory.ZIP);
68
69         // compressor type
70         compressorMap.put(CompressorStreamFactory.BZIP2, CompressorStreamFactory.BZIP2);
71         compressorMap.put(CompressorStreamFactory.DEFLATE, CompressorStreamFactory.DEFLATE);
72         compressorMap.put(CompressorStreamFactory.GZIP, CompressorStreamFactory.GZIP);
73         compressorMap.put(CompressorStreamFactory.LZMA, CompressorStreamFactory.LZMA);
74         compressorMap.put(CompressorStreamFactory.PACK200, CompressorStreamFactory.PACK200);
75         compressorMap.put(CompressorStreamFactory.SNAPPY_FRAMED, CompressorStreamFactory.SNAPPY_FRAMED);
76         compressorMap.put(CompressorStreamFactory.SNAPPY_RAW, CompressorStreamFactory.SNAPPY_RAW);
77         compressorMap.put(CompressorStreamFactory.XZ, CompressorStreamFactory.XZ);
78         compressorMap.put(CompressorStreamFactory.Z, CompressorStreamFactory.Z);
79     }
80
81     /**
82      * tar.gz
83      * <br/>
84      * 
85      * @param zipfileName
86      * @param outputDirectory
87      * @param fileNames
88      * @return
89      * @since NFVO 0.5
90      */
91     public static boolean unCompressGzip(String zipfileName, String outputDirectory, List<String> fileNames) {
92         try (
93             FileInputStream fis = new FileInputStream(zipfileName);
94             GZIPInputStream gis = new GZIPInputStream(new BufferedInputStream(fis));
95             ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("tar", gis);
96             BufferedInputStream bis = new BufferedInputStream(in)){
97             TarArchiveEntry entry = (TarArchiveEntry)in.getNextEntry();
98             while(entry != null) {
99                 String name = entry.getName();
100                 String[] names = name.split("/");
101                 String fileName = outputDirectory;
102                 for(int i = 0; i < names.length; i++) {
103                     String str = names[i];
104                     fileName = fileName + File.separator + str;
105                 }
106                 if(name.endsWith("/")) {
107                     FileUtils.mkDirs(fileName);
108                 } else {
109                         File file = getRealFileName(outputDirectory, name);
110                         if(null != fileNames) {
111                                 fileNames.add(file.getName());
112                         }
113                         try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))){
114                                 int b = -1;
115                                 while((b = bis.read()) != -1) {
116                                         bos.write(b);
117                                 }
118                                 log.debug("ungzip to:" + file.getCanonicalPath());
119                         }
120                 }
121                 entry = (TarArchiveEntry)in.getNextEntry();
122             }
123             return true;
124         } catch(Exception e) {
125             log.error("UnCompressGZip faield:", e);
126             return false;
127         } 
128     }
129
130     public static void unCompressAllZip(String sourcePath, String targetPath) throws FileNotFoundException {
131         for(File file : FileUtils.listFiles(new File(sourcePath))) {
132             unCompressZip(file.getAbsolutePath(),
133                     targetPath + File.separator + file.getName().substring(0, file.getName().lastIndexOf(".")),new ArrayList<String>());
134         }
135     }
136
137     /**
138      * zip
139      * <br/>
140      * 
141      * @param sourceFile unzipPath
142      * @param targetPath zippath
143      * @param fileNames
144      * @since NFVO 0.5
145      */
146     public static boolean unCompressZip(String sourceFile, String targetPath,List<String> fileNames) {
147         try (
148             FileInputStream fis = new FileInputStream(sourceFile);
149             ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))){
150             ZipEntry entry = null;
151             while((entry = zis.getNextEntry()) != null) {
152                 String name = entry.getName();
153                 String[] names = name.split("/");
154                 String fileName = targetPath;
155                 for(int i = 0; i < names.length; i++) {
156                     String str = names[i];
157                     fileName = fileName + File.separator + str;
158                 }
159                 if(name.endsWith("/")) {
160                     FileUtils.mkDirs(fileName);
161                 } else {
162                     int count;
163                     byte data[] = new byte[2048];
164                     File file = getRealFileName(targetPath, name);
165                     fileNames.add(file.getName());
166
167                     try(BufferedOutputStream dest = new BufferedOutputStream(new FileOutputStream(file))){
168
169                     while((count = zis.read(data, 0, 2048)) != -1) {
170                         dest.write(data, 0, count);
171                     }
172                     log.debug("unzip to:" + file.getCanonicalPath());}
173                 }
174             }
175             zis.close();
176             return true;
177         } catch(Exception e) {
178             log.error("UnCompressZip faield:", e);
179         }
180         return false;
181     }
182
183     private static File getRealFileName(String zippath, String absFileName) {
184         String[] dirs = absFileName.split("/", absFileName.length());
185
186         File ret = new File(zippath);
187
188         if(dirs.length > 1) {
189             for(int i = 0; i < dirs.length - 1; i++) {
190                 ret = new File(ret, dirs[i]);
191             }
192         }
193
194         if(!ret.exists()) {
195             ret.mkdirs();
196         }
197
198         ret = new File(ret, dirs[dirs.length - 1]);
199
200         return ret;
201     }
202
203     /**
204      * tar.xz
205      * <br/>
206      * 
207      * @param zipfileName
208      * @param outputDirectory
209      * @param fileNames
210      * @return
211      * @since NFVO 0.5
212      */
213     public static boolean unCompressTarXZ(String zipfileName, String outputDirectory, List<String> fileNames) {
214         try( 
215             XZCompressorInputStream xzis =
216                     new XZCompressorInputStream(new BufferedInputStream(new FileInputStream(zipfileName)));
217             ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("tar", xzis);
218             BufferedInputStream bis = new BufferedInputStream(in)){
219             TarArchiveEntry entry = (TarArchiveEntry)in.getNextEntry();
220             while(entry != null) {
221                 String name = entry.getName();
222                 String[] names = name.split("/");
223                 String fileName = outputDirectory;
224                 for(int i = 0; i < names.length; i++) {
225                     String str = names[i];
226                     fileName = fileName + File.separator + str;
227                 }
228                 if(name.endsWith("/")) {
229                     FileUtils.mkDirs(fileName);
230                 } else {
231                     File file = getRealFileName(outputDirectory, name);
232                     if(null != fileNames) {
233                         fileNames.add(file.getName());
234                     }
235                     try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))){
236                     int b = -1;
237                     while((b = bis.read()) != -1) {
238                         bos.write(b);
239                     }
240                     log.debug("ungzip to:" + file.getCanonicalPath());}
241                 }
242                 entry = (TarArchiveEntry)in.getNextEntry();
243             }
244             return true;
245         } catch(Exception e) {
246             log.error("unCompressTarXZ faield:", e);
247         } 
248         return false;
249     }
250     
251     /**
252      * only support .zip/.tar.gz/.tar.xz
253      * <br/>
254      * 
255      * @param zipfileName
256      * @param outputDirectory
257      * @param fileNames
258      * @return
259      * @since  NFVO 0.5
260      */
261     public static boolean unCompress(String zipfileName, String outputDirectory, List<String> fileNames) {
262         if(zipfileName.endsWith(".zip")){
263             return unCompressZip(zipfileName,outputDirectory,fileNames);
264         }else if(zipfileName.endsWith(".tar.gz")){
265             return unCompressGzip(zipfileName,outputDirectory,fileNames);
266         }else if(zipfileName.endsWith(".tar.xz")){
267             return unCompressTarXZ(zipfileName,outputDirectory,fileNames);
268         }else{
269             log.error("not supprot file type:->"+zipfileName);
270             return false;
271         }
272     }
273
274         
275 }