2 * Copyright 2016 ZTE Corporation.
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.
16 package org.openo.commontosca.catalog.model.common;
18 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import java.io.BufferedInputStream;
23 import java.io.BufferedReader;
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;
34 public class TemplateUtils {
35 private static final Logger logger = LoggerFactory.getLogger(TemplateUtils.class);
41 * @throws CatalogResourceException
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());
56 * @throws CatalogResourceException
58 @SuppressWarnings("resource")
59 public static String[] readFromZipFile(String zipFileName, String zipEntryName)
60 throws CatalogResourceException {
61 ZipInputStream zipIns = null;
62 BufferedReader zipEntryBr = null;
64 ZipFile zipFile = new ZipFile(zipFileName);
66 zipIns = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFileName)));
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<>();
74 while ((line = zipEntryBr.readLine()) != null) {
78 return lineList.toArray(new String[0]);
81 } catch (IOException e) {
82 throw new CatalogResourceException("Parse Tosca Meta Fail.", e);
84 closeStreamAndReader(zipIns, zipEntryBr);
89 private static void closeStreamAndReader(ZipInputStream zin, BufferedReader br) {
93 } catch (IOException e1) {
94 logger.error("Buffered reader close failed !");
100 } catch (IOException e2) {
101 logger.error("Zip inputStream close failed !");