Java Zip Files addToZip(byte[] zip, String file, String fileName)

Here you can find the source of addToZip(byte[] zip, String file, String fileName)

Description

Adds an entry to a zip file

License

Apache License

Parameter

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

Return

the zip file with the newly added content

Declaration

public static byte[] addToZip(byte[] zip, String file, String fileName) throws IOException 

Method Source Code


//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();
    }
}

Related

  1. addFileToZip(ZipOutputStream out, InputStream in, String entry)
  2. addFileToZip(ZipOutputStream zipOutputStream, File file, String basePath)
  3. addFileToZip(ZipOutputStream zipOutputStream, String path, byte[] bytes)
  4. addFileToZip(ZipOutputStream zos, File file, File rootDir)
  5. addFileToZipOutputStream(File file, ZipOutputStream zipOs)
  6. addToZip(File directoryToZip, File file, ZipOutputStream zos)
  7. addToZip(File f, int truncate, ZipOutputStream os, byte[] buff)
  8. addToZip(File file, ZipOutputStream out, String folder)
  9. addToZip(File input, String entryName, ZipOutputStream zos, boolean withHidden, Set excludeEntries)