Here you can find the source of addToZip(byte[] zip, String file, String fileName)
Parameter | Description |
---|---|
zip | the zip file which will have the content added |
file | the string to add to the zip |
fileName | the zip file name |
public static byte[] addToZip(byte[] zip, String file, String fileName) throws IOException
//package com.java2s; /*-/*w ww . ja v a 2 s. co m*/ * #%L * thinkbig-feed-manager-controller * %% * Copyright (C) 2017 ThinkBig Analytics * %% * 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. * #L% */ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; public class Main { /** * Adds an entry to a zip file * * @param zip the zip file which will have the content added * @param file the string to add to the zip * @param fileName the zip file name * @return the zip file with the newly added content */ public static byte[] addToZip(byte[] zip, String file, String fileName) throws IOException { InputStream zipInputStream = new ByteArrayInputStream(zip); ZipInputStream zis = new ZipInputStream(zipInputStream); byte[] buffer = new byte[1024]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (ZipOutputStream zos = new ZipOutputStream(baos)) { ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { ByteArrayOutputStream out = new ByteArrayOutputStream(); int len = 0; while ((len = zis.read(buffer)) > 0) { out.write(buffer, 0, len); } out.close(); zos.putNextEntry(entry); zos.write(out.toByteArray()); zos.closeEntry(); } zis.closeEntry(); zis.close(); entry = new ZipEntry(fileName); zos.putNextEntry(entry); zos.write(file.getBytes()); zos.closeEntry(); } return baos.toByteArray(); } }