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[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:abfab3d.shapejs.Project.java

private static void extractZip(String zipFile, String outputFolder, Map<String, String> sceneFiles,
        List<String> resources) {
    byte[] buffer = new byte[1024];

    try {/* w  ww. j av  a  2s .  com*/
        //create output directory is not exists
        File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }

        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            // Ignore directories
            if (ze.isDirectory())
                continue;

            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);
            System.out.println("file unzip : " + newFile.getAbsoluteFile());

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            // Save path to the script and parameters files
            if (fileName.endsWith(".json")) {
                sceneFiles.put("paramFile", newFile.getAbsolutePath());
            } else if (fileName.endsWith(".js")) {
                sceneFiles.put("scriptFile", newFile.getAbsolutePath());
            } else {
                resources.add(newFile.getAbsolutePath());
            }

            fos.close();
            ze = zis.getNextEntry();
        }

        zis.closeEntry();
        zis.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:org.geoserver.data.util.IOUtils.java

public static void decompress(InputStream input, File destDir) throws IOException {
    ZipInputStream zin = new ZipInputStream(input);
    ZipEntry entry = null;//from   ww w  . j ava2 s  . c  o m

    byte[] buffer = new byte[1024];
    while ((entry = zin.getNextEntry()) != null) {
        File f = new File(destDir, entry.getName());
        if (entry.isDirectory()) {
            f.mkdirs();
            continue;
        }

        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));

        int n = -1;
        while ((n = zin.read(buffer)) != -1) {
            out.write(buffer, 0, n);
        }

        out.flush();
        out.close();
    }
}

From source file:com.asakusafw.bulkloader.testutil.UnitTestUtil.java

public static boolean assertZipFile(File[] expectedFile, File zipFile) throws IOException {

    ZipInputStream zipIs = new ZipInputStream(new FileInputStream(zipFile));
    ZipEntry zipEntry = null;/*from   w w w  .  ja  v a  2s  .  c om*/
    int i = 0;
    while ((zipEntry = zipIs.getNextEntry()) != null) {
        if (zipEntry.isDirectory()) {
            continue;
        }
        i++;
        File tempFile = new File("target/asakusa-thundergate/tempdata" + String.valueOf(i));
        FileOutputStream fos = new FileOutputStream(tempFile);
        byte[] b = new byte[1024];
        while (true) {
            int read = zipIs.read(b);
            if (read == -1) {
                break;
            }
            fos.write(b, 0, read);
        }

        // ?
        if (!assertFile(expectedFile[i - 1], tempFile)) {
            tempFile.delete();
            return false;
        }

        // temp
        tempFile.delete();
    }
    return true;
}

From source file:com.sldeditor.test.unit.tool.vector.VectorToolTest.java

/**
 * Extracts a zip entry (file entry)./*from  w  w  w . ja v  a2s  . c  o m*/
 *
 * @param zipIn the zip in
 * @param filePath the file path
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
    byte[] bytesIn = new byte[4096];
    int read = 0;
    while ((read = zipIn.read(bytesIn)) != -1) {
        bos.write(bytesIn, 0, read);
    }
    bos.close();
}

From source file:org.opentestsystem.authoring.testitembank.service.impl.ApipZipOutputFileBuilderService.java

private static final void unzipToLocalDirectory(final String parentDir, final GridFSDBFile zipFile)
        throws IOException {
    ZipInputStream zipInputStream = null;
    try {//  ww w. j  a  v  a  2  s. c  o  m
        zipInputStream = new ZipInputStream(zipFile.getInputStream());

        ZipEntry nextEntry = null;
        while ((nextEntry = zipInputStream.getNextEntry()) != null) {
            if (!nextEntry.isDirectory()) {
                // create local file
                final File f = new File(parentDir + "/" + nextEntry.getName());
                f.getParentFile().mkdirs();
                f.createNewFile();

                int len = 0;
                final byte[] buff = new byte[1024];

                // write entry to file
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(f);
                    while ((len = zipInputStream.read(buff)) > 0) {
                        fos.write(buff, 0, len);
                    }
                } catch (final FileNotFoundException e) {
                    LOGGER.error("unexcepted FileNotFoundException: ", e);
                } finally {
                    closeAndFlushQuietly(fos);
                    closeEntryQuiety(zipInputStream);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(zipInputStream);
    }
}

From source file:com.bukanir.android.utils.Utils.java

public static String unzipSubtitle(String zip, String path) {
    InputStream is;/*from  w w w  .  ja va2  s. c  o m*/
    ZipInputStream zis;
    try {
        String filename = null;
        is = new FileInputStream(zip);
        zis = new ZipInputStream(new BufferedInputStream(is));
        ZipEntry ze;
        byte[] buffer = new byte[1024];
        int count;

        while ((ze = zis.getNextEntry()) != null) {
            filename = ze.getName();

            if (ze.isDirectory()) {
                File fmd = new File(path + "/" + filename);
                fmd.mkdirs();
                continue;
            }

            if (filename.endsWith(".srt") || filename.endsWith(".sub")) {
                FileOutputStream fout = new FileOutputStream(path + "/" + filename);
                while ((count = zis.read(buffer)) != -1) {
                    fout.write(buffer, 0, count);
                }
                fout.close();
                zis.closeEntry();
                break;
            }
            zis.closeEntry();
        }
        zis.close();

        File z = new File(zip);
        z.delete();

        return path + "/" + filename;

    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:org.talend.dataprofiler.core.ui.imex.model.ZipFileExportWriter.java

/**
 * DOC bZhou Comment method "addFilesToExistingZip".
 * //w ww.  ja  v  a  2 s. c  o  m
 * @param zipFile
 * @param files
 * @throws IOException
 */
public static void addFilesToExistingZip(File zipFile, Map<File, String> fileMap) throws IOException {
    // get a temp file
    File tempFile = File.createTempFile(zipFile.getName(), null);
    // delete it, otherwise you cannot rename your existing zip to it.
    tempFile.delete();
    // MOD klliu bug TDQ-1691 2011-09-09
    FileUtils.copyFile(zipFile, tempFile);
    // renameTo works on windows, don't work on linux
    // boolean renameOk = zipFile.renameTo(tempFile);
    // if (!renameOk) {
    //            throw new RuntimeException("could not rename the file " + zipFile.getAbsolutePath() + " to "//$NON-NLS-1$ //$NON-NLS-2$
    // + tempFile.getAbsolutePath());
    // }
    // ~
    byte[] buf = new byte[4096];

    ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry entry = zin.getNextEntry();
    while (entry != null) {
        String name = entry.getName();

        boolean notInFiles = true;
        for (File f : fileMap.keySet()) {
            if (f.getName().equals(new Path(name).lastSegment())) {
                notInFiles = false;
                break;
            }
        }
        if (notInFiles) {
            // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry(name));
            // Transfer bytes from the ZIP file to the output file
            int len;
            while ((len = zin.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
        entry = zin.getNextEntry();
    }
    // Close the streams
    zin.close();
    // Compress the files
    for (File f : fileMap.keySet()) {
        InputStream in = new FileInputStream(f);
        // Add ZIP entry to output stream.
        out.putNextEntry(new ZipEntry(fileMap.get(f)));
        // Transfer bytes from the file to the ZIP file
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        // Complete the entry
        out.closeEntry();
        in.close();
    }
    // Complete the ZIP file
    out.close();
    tempFile.delete();
}

From source file:org.jenkinsci.plugins.fortifycloudscan.util.ArchiveUtil.java

public static void unzip(File directory, File zipFile) throws FileNotFoundException, IOException {
    if (!directory.exists()) {
        directory.mkdirs();//from w  ww . j a  va  2  s  . c  om
    }
    byte[] buffer = new byte[2048];

    FileInputStream fInput = null;
    ZipInputStream zipInput = null;
    try {
        fInput = new FileInputStream(zipFile);
        zipInput = new ZipInputStream(fInput);
        ZipEntry entry = zipInput.getNextEntry();
        while (entry != null) {
            String entryName = entry.getName();
            File file = new File(directory.getAbsolutePath() + File.separator + entryName);
            if (entry.isDirectory()) {
                File newDir = new File(file.getAbsolutePath());
                if (!newDir.exists()) {
                    newDir.mkdirs();
                }
            } else {
                if (!file.getParentFile().isDirectory() && !file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                FileOutputStream fOutput = new FileOutputStream(file);
                int count;
                while ((count = zipInput.read(buffer)) > 0) {
                    fOutput.write(buffer, 0, count);
                }
                fOutput.close();
            }
            zipInput.closeEntry();
            entry = zipInput.getNextEntry();
        }
        zipInput.closeEntry();
        zipInput.close();
        fInput.close();
    } catch (FileNotFoundException e) {
        throw new FileNotFoundException(e.getMessage());
    } catch (IOException e) {
        throw new IOException(e);
    } finally {
        try {
            zipInput.closeEntry();
        } catch (IOException e) {
        }
        IOUtils.closeQuietly(zipInput);
        IOUtils.closeQuietly(fInput);
    }
}

From source file:org.bonitasoft.engine.io.IOUtil.java

private static void writeZipInputToFile(final ZipInputStream zipInputstream, final File outputFile)
        throws FileNotFoundException, IOException {
    // The input is a file. An FileOutputStream is created to write the content of the new file.
    mkdirs(outputFile.getParentFile());/* ww  w .ja v a  2  s  .  c  o  m*/
    try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile)) {
        // The contents of the new file, that is read from the ZipInputStream using a buffer (byte []), is written.
        int bytesRead;
        final byte[] buffer = new byte[BUFFER_SIZE];
        while ((bytesRead = zipInputstream.read(buffer)) > -1) {
            fileOutputStream.write(buffer, 0, bytesRead);
        }
        fileOutputStream.flush();
    } catch (final IOException ioe) {
        // In case of error, the file is deleted
        outputFile.delete();
        throw ioe;
    }
}

From source file:Main.java

public static void unZipFolder(InputStream input, String outPathString) throws Exception {
    java.util.zip.ZipInputStream inZip = new java.util.zip.ZipInputStream(input);
    java.util.zip.ZipEntry zipEntry = null;
    String szName = "";

    while ((zipEntry = inZip.getNextEntry()) != null) {
        szName = zipEntry.getName();// w  w w .  ja va  2s  .c o  m

        if (zipEntry.isDirectory()) {

            // get the folder name of the widget
            szName = szName.substring(0, szName.length() - 1);
            java.io.File folder = new java.io.File(outPathString + java.io.File.separator + szName);
            folder.mkdirs();

        } else {

            java.io.File file = new java.io.File(outPathString + java.io.File.separator + szName);
            file.createNewFile();
            // get the output stream of the file
            java.io.FileOutputStream out = new java.io.FileOutputStream(file);
            int len;
            byte[] buffer = new byte[1024];
            // read (len) bytes into buffer
            while ((len = inZip.read(buffer)) != -1) {
                // write (len) byte from buffer at the position 0
                out.write(buffer, 0, len);
                out.flush();
            }
            out.close();
        }
    } //end of while

    inZip.close();
}