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.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.zip.GZIPInputStream;
30 import java.util.zip.ZipEntry;
31 import java.util.zip.ZipInputStream;
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;
46 * @author quanzhong@huawei.com
47 * @version NFVO 0.5 Oct 28, 2016
49 public class UnCompressUtil {
51 private static Logger log = LoggerFactory.getLogger(UnCompressUtil.class);
53 private static Map<String, String> archiveMap = new HashMap<>();
55 private static Map<String, String> compressorMap = new HashMap<>();
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);
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);
85 * @param outputDirectory
90 public static boolean unCompressGzip(String zipfileName, String outputDirectory, List<String> fileNames) {
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;
105 if(name.endsWith("/")) {
106 FileUtils.mkDirs(fileName);
108 File file = getRealFileName(outputDirectory, name);
109 if(null != fileNames) {
110 fileNames.add(file.getName());
112 try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))){
114 while((b = bis.read()) != -1) {
117 log.debug("ungzip to:" + file.getCanonicalPath());
120 entry = (TarArchiveEntry)in.getNextEntry();
123 } catch(Exception e) {
124 log.error("UnCompressGZip faield:", e);
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>());
140 * @param sourceFile unzipPath
141 * @param targetPath zippath
145 public static boolean unCompressZip(String sourceFile, String targetPath,List<String> fileNames) {
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;
158 if(name.endsWith("/")) {
159 FileUtils.mkDirs(fileName);
162 byte[] data = new byte[2048];
163 File file = getRealFileName(targetPath, name);
164 fileNames.add(file.getName());
166 try(BufferedOutputStream dest = new BufferedOutputStream(new FileOutputStream(file))){
168 while((count = zis.read(data, 0, 2048)) != -1) {
169 dest.write(data, 0, count);
171 log.debug("unzip to:" + file.getCanonicalPath());}
176 } catch(Exception e) {
177 log.error("UnCompressZip faield:", e);
182 private static File getRealFileName(String zippath, String absFileName) {
183 String[] dirs = absFileName.split("/", absFileName.length());
185 File ret = new File(zippath);
187 if(dirs.length > 1) {
188 for(int i = 0; i < dirs.length - 1; i++) {
189 ret = new File(ret, dirs[i]);
197 ret = new File(ret, dirs[dirs.length - 1]);
207 * @param outputDirectory
212 public static boolean unCompressTarXZ(String zipfileName, String outputDirectory, List<String> fileNames) {
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;
227 if(name.endsWith("/")) {
228 FileUtils.mkDirs(fileName);
230 File file = getRealFileName(outputDirectory, name);
231 if(null != fileNames) {
232 fileNames.add(file.getName());
234 try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))){
236 while((b = bis.read()) != -1) {
239 log.debug("ungzip to:" + file.getCanonicalPath());}
241 entry = (TarArchiveEntry)in.getNextEntry();
244 } catch(Exception e) {
245 log.error("unCompressTarXZ faield:", e);
251 * only support .zip/.tar.gz/.tar.xz
255 * @param outputDirectory
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);
268 log.error("not supprot file type:->"+zipfileName);