eda6b78ef8804b591313a6d43e902559508a163e
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / main / java / org / openo / nfvo / 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.openo.nfvo.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     public static Map<String, String> archiveMap = new HashMap<String, String>();
55
56     public 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         FileInputStream fis = null;
93         ArchiveInputStream in = null;
94         BufferedInputStream bis = null;
95         try {
96             fis = new FileInputStream(zipfileName);
97             GZIPInputStream gis = new GZIPInputStream(new BufferedInputStream(fis));
98             in = new ArchiveStreamFactory().createArchiveInputStream("tar", gis);
99             bis = new BufferedInputStream(in);
100             TarArchiveEntry entry = (TarArchiveEntry)in.getNextEntry();
101             while(entry != null) {
102                 String name = entry.getName();
103                 String[] names = name.split("/");
104                 String fileName = outputDirectory;
105                 for(int i = 0; i < names.length; i++) {
106                     String str = names[i];
107                     fileName = fileName + File.separator + str;
108                 }
109                 if(name.endsWith("/")) {
110                     FileUtils.mkDirs(fileName);
111                 } else {
112                     File file = getRealFileName(outputDirectory, name);
113                     if(null != fileNames) {
114                         fileNames.add(file.getName());
115                     }
116                     BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
117                     int b = -1;
118                     while((b = bis.read()) != -1) {
119                         bos.write(b);
120                     }
121                     log.debug("ungzip to:" + file.getCanonicalPath());
122                     bos.flush();
123                     bos.close();
124                 }
125                 entry = (TarArchiveEntry)in.getNextEntry();
126             }
127             return true;
128         } catch(Exception e) {
129             log.error("UnCompressGZip faield:", e);
130             return false;
131         } finally {
132             try {
133                 if(null != bis) {
134                     bis.close();
135                 }
136             } catch(IOException e) {
137                 log.error("UnCompressGZip faield:", e);
138             }
139         }
140     }
141
142     public static void unCompressAllZip(String sourcePath, String targetPath) throws FileNotFoundException {
143         for(File file : FileUtils.listFiles(new File(sourcePath))) {
144             unCompressZip(file.getAbsolutePath(),
145                     targetPath + File.separator + file.getName().substring(0, file.getName().lastIndexOf(".")),new ArrayList<String>());
146         }
147     }
148
149     /**
150      * zip
151      * <br/>
152      * 
153      * @param sourceFile unzipPath
154      * @param targetPath zippath
155      * @param fileNames
156      * @since NFVO 0.5
157      */
158     public static boolean unCompressZip(String sourceFile, String targetPath,List<String> fileNames) {
159         try {
160             BufferedOutputStream dest = null;
161             FileInputStream fis = new FileInputStream(sourceFile);
162             ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
163             ZipEntry entry = null;
164             while((entry = zis.getNextEntry()) != null) {
165                 String name = entry.getName();
166                 String[] names = name.split("/");
167                 String fileName = targetPath;
168                 for(int i = 0; i < names.length; i++) {
169                     String str = names[i];
170                     fileName = fileName + File.separator + str;
171                 }
172                 if(name.endsWith("/")) {
173                     FileUtils.mkDirs(fileName);
174                 } else {
175                     int count;
176                     byte data[] = new byte[2048];
177                     File file = getRealFileName(targetPath, name);
178                     fileNames.add(file.getName());
179                     dest = new BufferedOutputStream(new FileOutputStream(file));
180
181                     while((count = zis.read(data, 0, 2048)) != -1) {
182                         dest.write(data, 0, count);
183                     }
184                     log.debug("unzip to:" + file.getCanonicalPath());
185                     dest.flush();
186                     dest.close();
187                 }
188             }
189             zis.close();
190             return true;
191         } catch(Exception e) {
192             log.error("UnCompressZip faield:", e);
193         }
194         return false;
195     }
196
197     private static File getRealFileName(String zippath, String absFileName) {
198         String[] dirs = absFileName.split("/", absFileName.length());
199
200         File ret = new File(zippath);
201
202         if(dirs.length > 1) {
203             for(int i = 0; i < dirs.length - 1; i++) {
204                 ret = new File(ret, dirs[i]);
205             }
206         }
207
208         if(!ret.exists()) {
209             ret.mkdirs();
210         }
211
212         ret = new File(ret, dirs[dirs.length - 1]);
213
214         return ret;
215     }
216
217     /**
218      * tar.xz
219      * <br/>
220      * 
221      * @param zipfileName
222      * @param outputDirectory
223      * @param fileNames
224      * @return
225      * @since NFVO 0.5
226      */
227     public static boolean unCompressTarXZ(String zipfileName, String outputDirectory, List<String> fileNames) {
228         ArchiveInputStream in = null;
229         BufferedInputStream bis = null;
230         try {
231             XZCompressorInputStream xzis =
232                     new XZCompressorInputStream(new BufferedInputStream(new FileInputStream(zipfileName)));
233             in = new ArchiveStreamFactory().createArchiveInputStream("tar", xzis);
234             bis = new BufferedInputStream(in);
235             TarArchiveEntry entry = (TarArchiveEntry)in.getNextEntry();
236             while(entry != null) {
237                 String name = entry.getName();
238                 String[] names = name.split("/");
239                 String fileName = outputDirectory;
240                 for(int i = 0; i < names.length; i++) {
241                     String str = names[i];
242                     fileName = fileName + File.separator + str;
243                 }
244                 if(name.endsWith("/")) {
245                     FileUtils.mkDirs(fileName);
246                 } else {
247                     File file = getRealFileName(outputDirectory, name);
248                     if(null != fileNames) {
249                         fileNames.add(file.getName());
250                     }
251                     BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
252                     int b = -1;
253                     while((b = bis.read()) != -1) {
254                         bos.write(b);
255                     }
256                     log.debug("ungzip to:" + file.getCanonicalPath());
257                     bos.flush();
258                     bos.close();
259                 }
260                 entry = (TarArchiveEntry)in.getNextEntry();
261             }
262             return true;
263         } catch(Exception e) {
264             log.error("unCompressTarXZ faield:", e);
265         } finally {
266             try {
267                 if(null != bis) {
268                     bis.close();
269                 }
270             } catch(IOException e) {
271                 log.error("unCompressTarXZ faield:", e);
272             }
273         }
274         return false;
275     }
276     
277     /**
278      * only support .zip/.tar.gz/.tar.xz
279      * <br/>
280      * 
281      * @param zipfileName
282      * @param outputDirectory
283      * @param fileNames
284      * @return
285      * @since  NFVO 0.5
286      */
287     public static boolean unCompress(String zipfileName, String outputDirectory, List<String> fileNames) {
288         if(zipfileName.endsWith(".zip")){
289             return unCompressZip(zipfileName,outputDirectory,fileNames);
290         }else if(zipfileName.endsWith(".tar.gz")){
291             return unCompressGzip(zipfileName,outputDirectory,fileNames);
292         }else if(zipfileName.endsWith(".tar.xz")){
293             return unCompressTarXZ(zipfileName,outputDirectory,fileNames);
294         }else{
295             log.error("not supprot file type:->"+zipfileName);
296             return false;
297         }
298     }
299
300         
301 }