From 806a46b2f785dd6b0a453ae1a6f74f4091e2d656 Mon Sep 17 00:00:00 2001 From: zm330 Date: Fri, 28 Feb 2020 17:01:34 +0800 Subject: Add zip parser Issue-ID: SO-2368 Signed-off-by: zm330 Change-Id: I6174746badecec59a16573e56ac2924dbadc6747 --- .../main/java/org/onap/so/asdc/util/ZipParser.java | 67 ++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 asdc-controller/src/main/java/org/onap/so/asdc/util/ZipParser.java (limited to 'asdc-controller/src/main') diff --git a/asdc-controller/src/main/java/org/onap/so/asdc/util/ZipParser.java b/asdc-controller/src/main/java/org/onap/so/asdc/util/ZipParser.java new file mode 100644 index 0000000000..c2f04dca7b --- /dev/null +++ b/asdc-controller/src/main/java/org/onap/so/asdc/util/ZipParser.java @@ -0,0 +1,67 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (c) 2019, CMCC Technologies Co., Ltd. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.asdc.util; + +import java.io.*; +import java.nio.charset.Charset; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import java.util.zip.ZipInputStream; + +public class ZipParser { + + private static volatile ZipParser instance; + + public static ZipParser getInstance() { + if (instance == null) { + synchronized (ZipParser.class) { + if (instance == null) { + instance = new ZipParser(); + } + } + } + return instance; + } + + public String parseJsonForZip(String path) throws IOException { + ZipFile zf = new ZipFile(path); + InputStream in = new BufferedInputStream(new FileInputStream(path)); + Charset cs = Charset.forName("utf-8"); + ZipInputStream zin = new ZipInputStream(in, cs); + ZipEntry ze; + String artifactContent = null; + while ((ze = zin.getNextEntry()) != null) { + if (ze.toString().endsWith("json")) { + StringBuilder jsonStr = new StringBuilder(); + BufferedReader br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze))); + String line; + while ((line = br.readLine()) != null) { + jsonStr.append(line); + } + br.close(); + artifactContent = jsonStr.toString().replace("\"", "\\\"").replaceAll("\\s", ""); + } + } + zin.closeEntry(); + return artifactContent; + } + +} -- cgit 1.2.3-korg