com.formkiq.core.util.Zips.java Source code

Java tutorial

Introduction

Here is the source code for com.formkiq.core.util.Zips.java

Source

/*
 * Copyright (C) 2016 FormKiQ Inc.
 *
 * 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.
 */
package com.formkiq.core.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.IOUtils;

/**
 * Helper method for dealing with Zip Files.
 *
 */
public final class Zips {

    /**
     * Extract Zip file to Map.
     * @param bytes byte[]
     * @return {@link Map}
     * @throws IOException IOException
     */
    public static Map<String, byte[]> extractZipToMap(final byte[] bytes) throws IOException {

        Map<String, byte[]> map = new HashMap<>();
        ByteArrayInputStream is = new ByteArrayInputStream(bytes);
        ZipInputStream zipStream = new ZipInputStream(is);

        try {

            ZipEntry entry = null;

            while ((entry = zipStream.getNextEntry()) != null) {

                String filename = entry.getName();
                byte[] data = IOUtils.toByteArray(zipStream);
                map.put(filename, data);
            }

        } finally {

            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(zipStream);
        }

        return map;
    }

    /**
     * Find bytes in zip by name.
     * @param bytes byte[]
     * @param name {@link String}
     * @return {@link Optional}
     * @throws IOException IOException
     */
    public static Optional<byte[]> findInZip(final byte[] bytes, final String name) throws IOException {

        Map<String, byte[]> map = Zips.extractZipToMap(bytes);
        if (map.containsKey(name)) {
            return Optional.of(map.get(name));
        }

        return Optional.empty();
    }

    /**
     * Is Zip file valid.
     * @param file {@link File}
     * @return boolean
     * @throws IOException IOException
     */
    public static boolean isValid(final File file) throws IOException {

        ZipFile zipfile = new ZipFile(file);
        IOUtils.closeQuietly(zipfile);
        return true;
    }

    /**
     * Create Zip file.
     * @param map {@link Map}
     * @return byte[] zip file
     * @throws IOException IOException
     */
    public static byte[] zipFile(final Map<String, byte[]> map) throws IOException {

        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(bs);

        for (Map.Entry<String, byte[]> e : map.entrySet()) {

            String name = e.getKey();
            byte[] data = e.getValue();

            ZipEntry ze = new ZipEntry(name);
            zip.putNextEntry(ze);

            zip.write(data, 0, data.length);
            zip.closeEntry();
        }

        zip.finish();

        byte[] bytes = bs.toByteArray();

        IOUtils.closeQuietly(bs);
        IOUtils.closeQuietly(zip);

        return bytes;
    }

    /**
     * Zips data.
     * @param name {@link String}
     * @param data {@link String}
     * @return byte[]
     * @throws IOException IOException
     */
    public static byte[] zipFile(final String name, final String data) throws IOException {
        Map<String, byte[]> map = new HashMap<>();
        map.put(name, Strings.getBytes(data));
        return zipFile(map);
    }

    /**
     * private constructor.
     */
    private Zips() {
    }
}