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