2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.tosca;
22 import io.vavr.control.Try;
23 import java.util.function.Function;
24 import java.util.zip.ZipEntry;
25 import java.util.zip.ZipOutputStream;
28 * ZipWriter abstracts the Zip file writing logic.
30 public interface ZipWriter {
33 * Builds a ZipWriter that outputs the data on a {@link java.util.zip.ZipOutputStream}
35 * @param zos the target {@link java.util.zip.ZipOutputStream}
37 static ZipWriter live(ZipOutputStream zos) {
38 return (entryName, payload) -> Try.of(() -> {
39 zos.putNextEntry(new ZipEntry(entryName));
41 // We can return null as a Void is expected;
47 * Writes an entry provided with its name and its payload
49 * @param entryName The entry's name to use in the zip file
50 * @param payload The payload to write for this entry
52 Try<Void> write(String entryName, byte[] payload);
55 * Writes an entry provided with its name and its payload
57 * @param entryName The entry's name to use in the zip file
58 * @param payload The payload to write for this entry
60 default Try<Void> write(String entryName, String payload) {
61 return write(entryName, payload.getBytes());
65 * Alias for {@link org.openecomp.sdc.be.tosca.ZipWriter}
67 * @param entryName The entry's name to use in the zip file
69 default Function<String, Try<Void>> writeString(String entryName) {
70 return payload -> write(entryName, payload.getBytes());
73 default Function<byte[], Try<Void>> write(String entryName) {
74 return payload -> write(entryName, payload);