2 * Copyright (c) 2016, Huawei Technologies Co., Ltd.
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.
17 package org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common;
19 import java.io.BufferedInputStream;
20 import java.io.BufferedOutputStream;
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;
30 import java.util.zip.GZIPInputStream;
31 import java.util.zip.ZipEntry;
32 import java.util.zip.ZipInputStream;
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;
47 * @author quanzhong@huawei.com
48 * @version NFVO 0.5 Oct 28, 2016
50 public class UnCompressUtil {
52 private static Logger log = LoggerFactory.getLogger(UnCompressUtil.class);
54 private static Map<String, String> archiveMap = new HashMap<String, String>();
56 private static Map<String, String> compressorMap = new HashMap<String, String>();
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);
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);
86 * @param outputDirectory
91 public static boolean unCompressGzip(String zipfileName, String outputDirectory, List<String> fileNames) {
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;
106 if(name.endsWith("/")) {
107 FileUtils.mkDirs(fileName);
109 File file = getRealFileName(outputDirectory, name);
110 if(null != fileNames) {
111 fileNames.add(file.getName());
113 try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))){
115 while((b = bis.read()) != -1) {
118 log.debug("ungzip to:" + file.getCanonicalPath());
121 entry = (TarArchiveEntry)in.getNextEntry();
124 } catch(Exception e) {
125 log.error("UnCompressGZip faield:", e);
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>());
141 * @param sourceFile unzipPath
142 * @param targetPath zippath
146 public static boolean unCompressZip(String sourceFile, String targetPath,List<String> fileNames) {
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;
159 if(name.endsWith("/")) {
160 FileUtils.mkDirs(fileName);
163 byte data[] = new byte[2048];
164 File file = getRealFileName(targetPath, name);
165 fileNames.add(file.getName());
167 try(BufferedOutputStream dest = new BufferedOutputStream(new FileOutputStream(file))){
169 while((count = zis.read(data, 0, 2048)) != -1) {
170 dest.write(data, 0, count);
172 log.debug("unzip to:" + file.getCanonicalPath());}
177 } catch(Exception e) {
178 log.error("UnCompressZip faield:", e);
183 private static File getRealFileName(String zippath, String absFileName) {
184 String[] dirs = absFileName.split("/", absFileName.length());
186 File ret = new File(zippath);
188 if(dirs.length > 1) {
189 for(int i = 0; i < dirs.length - 1; i++) {
190 ret = new File(ret, dirs[i]);
198 ret = new File(ret, dirs[dirs.length - 1]);
208 * @param outputDirectory
213 public static boolean unCompressTarXZ(String zipfileName, String outputDirectory, List<String> fileNames) {
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;
228 if(name.endsWith("/")) {
229 FileUtils.mkDirs(fileName);
231 File file = getRealFileName(outputDirectory, name);
232 if(null != fileNames) {
233 fileNames.add(file.getName());
235 try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))){
237 while((b = bis.read()) != -1) {
240 log.debug("ungzip to:" + file.getCanonicalPath());}
242 entry = (TarArchiveEntry)in.getNextEntry();
245 } catch(Exception e) {
246 log.error("unCompressTarXZ faield:", e);
252 * only support .zip/.tar.gz/.tar.xz
256 * @param outputDirectory
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);
269 log.error("not supprot file type:->"+zipfileName);