Example usage for java.util.zip ZipInputStream read

List of usage examples for java.util.zip ZipInputStream read

Introduction

In this page you can find the example usage for java.util.zip ZipInputStream read.

Prototype

public int read(byte[] b, int off, int len) throws IOException 

Source Link

Document

Reads from the current ZIP entry into an array of bytes.

Usage

From source file:net.grinder.util.LogCompressUtil.java

/**
 * Uncompress the given the {@link InputStream} into the given {@link OutputStream}.
 * /*  w w  w.ja  v a 2s.  c o m*/
 * @param inputStream
 *            input stream of the compressed file
 * @param outputStream
 *            file to be written
 * @param limit
 *            the limit of the output
 */
public static void unCompress(InputStream inputStream, OutputStream outputStream, long limit) {
    ZipInputStream zipInputStream = null;
    try {
        zipInputStream = new ZipInputStream(inputStream);
        byte[] buffer = new byte[COMPRESS_BUFFER_SIZE];
        int count = 0;
        long total = 0;
        checkNotNull(zipInputStream.getNextEntry(), "In zip, it should have at least one entry");
        while ((count = zipInputStream.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) {
            total += count;
            if (total >= limit) {
                break;
            }
            outputStream.write(buffer, 0, count);
        }
        outputStream.flush();
    } catch (IOException e) {
        LOGGER.error("Error occurs while uncompress");
        LOGGER.error("Details", e);
        return;
    } finally {
        IOUtils.closeQuietly(zipInputStream);
    }
}

From source file:org.kawanfw.file.api.util.client.JarReader.java

/**
 * Extract bytecode of .class as byte array
 * /* ww  w .ja  v  a2  s.  co  m*/
 * @param in
 *            the ZipInputStream
 * @param classname
 *            the class name to search for
 * @return byte array contaning the .class bytecode
 * @throws IOException
 *             if I/O errrors
 * @throws FileNotFoundException
 *             if the .class file corresponding to the classname does not
 *             exists
 */
public static byte[] extractClassFileBytecode(InputStream in, String classname)
        throws IOException, FileNotFoundException {

    if (in == null) {
        throw new IllegalArgumentException("in is null!");
    }

    if (classname == null) {
        throw new IllegalArgumentException("classname is null!");
    }

    String entryToFind = convertClassNameToEntry(classname);

    ZipInputStream zis = new ZipInputStream(in);

    byte[] byteArray = null;

    try {
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            debug("Unzipping: " + entry.getName());

            int size;
            byteArray = new byte[2048];

            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            while ((size = zis.read(byteArray, 0, byteArray.length)) != -1) {
                bos.write(byteArray, 0, size);
            }
            bos.flush();
            bos.close();

            if (entry.getName().equals(entryToFind)) {
                return bos.toByteArray();
            }

        }

        return null;

    } finally {
        zis.close();
    }

}

From source file:com.amalto.core.storage.hibernate.TypeMapping.java

private static Object _deserializeValue(Object value, FieldMetadata sourceField, FieldMetadata targetField) {
    if (targetField == null) {
        return value;
    }//from w ww.ja  va 2  s.c o m
    if (!targetField.isMany()) {
        Boolean targetZipped = targetField.<Boolean>getData(MetadataRepository.DATA_ZIPPED);
        Boolean sourceZipped = sourceField.<Boolean>getData(MetadataRepository.DATA_ZIPPED);
        if (Boolean.TRUE.equals(sourceZipped) && targetZipped == null) {
            try {
                ByteArrayInputStream bis = new ByteArrayInputStream(String.valueOf(value).getBytes("UTF-8")); //$NON-NLS-1$
                ZipInputStream zis = new ZipInputStream(new Base64InputStream(bis));
                byte[] buffer = new byte[1024];
                int read;
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                while (zis.getNextEntry() != null) {
                    while ((read = zis.read(buffer, 0, buffer.length)) > -1) {
                        bos.write(buffer, 0, read);
                    }
                }
                return new String(bos.toByteArray(), "UTF-8"); //$NON-NLS-1$
            } catch (IOException e) {
                throw new RuntimeException("Unexpected deflate exception", e); //$NON-NLS-1$
            }
        }
        String targetSQLType = sourceField.getType().getData(TypeMapping.SQL_TYPE);
        if (value != null && targetSQLType != null && SQL_TYPE_CLOB.equals(targetSQLType)) {
            try {
                Reader characterStream = ((Clob) value).getCharacterStream();
                return new String(IOUtils.toCharArray(characterStream)); // No need to close (Hibernate seems to
                                                                         // handle this).
            } catch (Exception e) {
                throw new RuntimeException("Unexpected read from clob exception", e); //$NON-NLS-1$
            }
        }
    }
    return value;
}

From source file:io.card.development.recording.Recording.java

private static Hashtable<String, byte[]> unzipFiles(InputStream recordingStream) throws IOException {
    ZipEntry zipEntry;//from   w  w  w  .j av  a  2  s .co m
    ZipInputStream zis = new ZipInputStream(recordingStream);
    Hashtable<String, byte[]> fileHash = new Hashtable<String, byte[]>();
    byte[] buffer = new byte[512];

    while ((zipEntry = zis.getNextEntry()) != null) {
        if (!zipEntry.isDirectory()) {
            int read = 0;
            ByteArrayOutputStream fileStream = new ByteArrayOutputStream();

            do {
                read = zis.read(buffer, 0, buffer.length);
                if (read != -1) {
                    fileStream.write(buffer, 0, read);
                }
            } while (read != -1);

            byte[] fileData = fileStream.toByteArray();
            fileHash.put(zipEntry.getName(), fileData);
        }
    }

    return fileHash;
}

From source file:org.opendatakit.api.forms.FormService.java

static Map<String, byte[]> processZipInputStream(ZipInputStream zipInputStream) throws IOException {
    int c;//w  w w.ja v  a  2  s . c o  m
    Map<String, byte[]> files = new HashMap<>();
    byte buffer[] = new byte[2084];
    ByteArrayOutputStream tempBAOS;
    ZipEntry zipEntry;
    while ((zipEntry = zipInputStream.getNextEntry()) != null) {
        if (!(zipEntry.isDirectory())) {
            tempBAOS = new ByteArrayOutputStream();
            while ((c = zipInputStream.read(buffer, 0, 2048)) > -1) {
                tempBAOS.write(buffer, 0, c);
            }
            files.put("tables" + BasicConsts.FORWARDSLASH + zipEntry.getName(), tempBAOS.toByteArray());
        }
    }
    return files;
}

From source file:org.clickframes.testframes.TestResultsParser.java

public static ProjectTestResults parseTestResults(File zip) throws IOException {
    // tmp directory
    File tmpDirectory = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    tmpDirectory.mkdirs();//from w  w w  . jav  a 2  s.  c o m

    ZipInputStream zis = new ZipInputStream(new FileInputStream(zip));
    ZipEntry entry;
    int BUFFER = 4096;
    while ((entry = zis.getNextEntry()) != null) {
        int count;
        byte data[] = new byte[BUFFER];
        // write the files to the disk
        File destinationFile = new File(tmpDirectory, entry.getName());
        destinationFile.getParentFile().mkdirs();
        FileOutputStream fos = new FileOutputStream(destinationFile);
        BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
        while ((count = zis.read(data, 0, BUFFER)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
    }

    File firefoxDir = new File(tmpDirectory, "firefox");

    if (!firefoxDir.exists()) {
        throw new RuntimeException("Invalid test results zip file uploaded. firefox directory does not exist.");
    }

    File testResultsXmlFile = new File(tmpDirectory, "testResults.xml");

    XMLDecoder d = new XMLDecoder(new BufferedInputStream(new FileInputStream(testResultsXmlFile)));
    ProjectTestResults projectTestResults = (ProjectTestResults) d.readObject();
    d.close();

    return projectTestResults;
}

From source file:com.eviware.soapui.integration.exporter.ProjectExporter.java

public static void unpackageAll(String archive, String path) {
    try {//from w w  w . ja v a  2  s  .co  m
        BufferedOutputStream dest = null;
        FileInputStream fis = new FileInputStream(archive);
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            int count;
            byte data[] = new byte[BUFFER];
            // write the files to the disk
            FileOutputStream fos = new FileOutputStream(path + File.separator + entry.getName());
            dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
        }
        zis.close();
    } catch (Exception e) {
        SoapUI.logError(e);
    }
}

From source file:com.ccoe.build.utils.CompressUtils.java

/**
 * Uncompress a zip to files/*ww  w  .j a v a  2  s.c o m*/
 * @param zip
 * @param unzipdir
 * @param isNeedClean
 * @return
 * @throws FileNotFoundException 
 * @throws IOException 
 */
public static List<File> unCompress(File zip, String unzipdir) throws IOException {
    ArrayList<File> unzipfiles = new ArrayList<File>();

    FileInputStream fi = new FileInputStream(zip);
    ZipInputStream zi = new ZipInputStream(new BufferedInputStream(fi));
    try {

        ZipEntry entry;
        while ((entry = zi.getNextEntry()) != null) {
            System.out.println("Extracting: " + entry);
            int count;
            byte data[] = new byte[BUFFER];
            File unzipfile = new File(unzipdir + File.separator + entry.getName());
            FileOutputStream fos = new FileOutputStream(unzipfile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

            while ((count = zi.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();

            unzipfiles.add(unzipfile);
        }

    } catch (IOException e) {
        throw e;
    } finally {
        IOUtils.closeQuietly(zi);
    }

    return unzipfiles;
}

From source file:de.suse.swamp.util.FileUtils.java

/**
 * Decompress the provided Stream to "targetPath"
 *///  w w  w . j  a  v  a  2  s  .  c  om
public static void uncompress(InputStream inputFile, String targetPath) throws Exception {
    ZipInputStream zin = new ZipInputStream(new BufferedInputStream(inputFile));
    BufferedOutputStream dest = null;
    ZipEntry entry;
    while ((entry = zin.getNextEntry()) != null) {
        int count;
        byte data[] = new byte[2048];
        // write the files to the disk
        if (entry.isDirectory()) {
            org.apache.commons.io.FileUtils.forceMkdir(new File(targetPath + "/" + entry.getName()));
        } else {
            FileOutputStream fos = new FileOutputStream(targetPath + "/" + entry.getName());
            dest = new BufferedOutputStream(fos, 2048);
            while ((count = zin.read(data, 0, 2048)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
        }
    }
}

From source file:org.ktunaxa.referral.server.FileUtil.java

public static String[] unzip(File zipFile, File targetDir) throws IOException {
    ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
    try {/* w w w  . j  a  va  2 s  . co m*/
        ZipEntry entry;
        ArrayList<String> names = new ArrayList<String>();
        while ((entry = zin.getNextEntry()) != null) {
            LOG.info("Extracting: " + entry);
            String name = entry.getName();
            names.add(name);
            int count;
            byte[] data = new byte[BUFFER];
            FileOutputStream fos = new FileOutputStream(new File(targetDir, name));
            BufferedOutputStream destination = new BufferedOutputStream(fos, BUFFER);
            try {
                while ((count = zin.read(data, 0, BUFFER)) != -1) {
                    destination.write(data, 0, count);
                }
                destination.flush();
            } finally {
                destination.close();
            }
        }
        return names.toArray(new String[names.size()]);
    } finally {
        zin.close();
    }
}