945cc92ac384417f830f46fdf45150dd0ff0cae9
[vfc/nfvo/catalog.git] /
1 /**
2  * Copyright 2016 ZTE Corporation.
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 package org.openo.commontosca.catalog.model.common;
17
18 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import java.io.BufferedInputStream;
23 import java.io.BufferedReader;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.InputStreamReader;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.zip.ZipEntry;
31 import java.util.zip.ZipFile;
32 import java.util.zip.ZipInputStream;
33
34 public class TemplateUtils {
35   private static final Logger logger = LoggerFactory.getLogger(TemplateUtils.class);
36   
37   /**
38    * @param zipFileName
39    * @param zipEntryName
40    * @return
41    * @throws CatalogResourceException
42    */
43   public static String readStringFromZipFile(String zipFileName, String zipEntryName) throws CatalogResourceException {
44     String[] lines = readFromZipFile(zipFileName, zipEntryName);
45     StringBuffer sb = new StringBuffer();
46     for (String line : lines) {
47       sb.append(line).append(System.lineSeparator());
48     }
49     return sb.toString();
50   }
51   
52   /**
53    * @param zipFileName
54    * @param zipEntryName
55    * @return
56    * @throws CatalogResourceException
57    */
58   @SuppressWarnings("resource")
59   public static String[] readFromZipFile(String zipFileName, String zipEntryName)
60       throws CatalogResourceException {
61     ZipInputStream zipIns = null;
62     BufferedReader zipEntryBr = null;
63     try {
64       ZipFile zipFile = new ZipFile(zipFileName);
65       
66       zipIns = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFileName)));
67       ZipEntry zipEntry;
68       while ((zipEntry = zipIns.getNextEntry()) != null) {
69         if (zipEntryName.equals(zipEntry.getName())
70             || (zipEntryName.replaceAll("/", File.separator)).equals(zipEntry.getName())) {
71           zipEntryBr = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));
72           List<String> lineList = new ArrayList<>();
73           String line;
74           while ((line = zipEntryBr.readLine()) != null) {
75             lineList.add(line);
76           }
77           
78           return lineList.toArray(new String[0]);
79         }
80       }
81     } catch (IOException e) {
82       throw new CatalogResourceException("Parse Tosca Meta Fail.", e);
83     } finally {
84       closeStreamAndReader(zipIns, zipEntryBr);
85     }
86     return new String[0];
87   }
88   
89   private static void closeStreamAndReader(ZipInputStream zin, BufferedReader br) {
90     if (br != null) {
91       try {
92         br.close();
93       } catch (IOException e1) {
94         logger.error("Buffered reader close failed !");
95       }
96     }
97     if (zin != null) {
98       try {
99         zin.closeEntry();
100       } catch (IOException e2) {
101         logger.error("Zip inputStream close failed !");
102       }
103     }
104   }
105 }