Java Uncompress Byte Array unZip(byte[] contents)

Here you can find the source of unZip(byte[] contents)

Description

decodes the byte array representing the zip file into a string.

License

Open Source License

Parameter

Parameter Description
contents a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static String unZip(byte[] contents) throws IOException 

Method Source Code


//package com.java2s;
/*//from  w w w.j a v  a2  s  .com
 *  Copyright (C) 2010-2012 Stichting Akvo (Akvo Foundation)
 *
 *  This file is part of Akvo FLOW.
 *
 *  Akvo FLOW is free software: you can redistribute it and modify it under the terms of
 *  the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
 *  either version 3 of the License or any later version.
 *
 *  Akvo FLOW is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU Affero General Public License included below for more details.
 *
 *  The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
 */

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    /**
     * decodes the byte array representing the zip file into a string. This assumes the zip contains
     * only 1 file.
     * 
     * @param contents
     * @return
     * @throws IOException
     */
    public static String unZip(byte[] contents) throws IOException {
        return unZip(contents, null);
    }

    /**
     * unzips a single zip entry (file within a zip) and returns the content as a string.
     * 
     * @param contents
     * @param entryName
     * @return
     * @throws IOException
     */
    public static String unZip(byte[] contents, String entryName) throws IOException {
        ByteArrayInputStream zipContents = new ByteArrayInputStream(contents);
        ZipInputStream zis = new ZipInputStream(zipContents);
        ZipEntry entry;
        StringBuilder line = new StringBuilder();
        while ((entry = zis.getNextEntry()) != null) {
            if (entryName == null || entryName.equalsIgnoreCase(entry.getName())) {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                byte[] buffer = new byte[2048];
                int size;
                while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
                    out.write(buffer, 0, size);
                }
                line.append(out.toString());

                out.close();
            }
        }
        zis.closeEntry();

        return line.toString();
    }
}

Related

  1. unzip(byte[] bytes, String encoding)
  2. unzip(byte[] compressedByte)
  3. unzip(byte[] compressedData)
  4. unzip(byte[] content)
  5. unzip(byte[] content)
  6. unzip(byte[] data)
  7. unZip(byte[] data)
  8. unzip(byte[] data)
  9. unzip(byte[] datas)