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:org.bonitasoft.platform.setup.PlatformSetupTestUtils.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.
    outputFile.getParentFile().mkdirs();
    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[1024];
        while ((bytesRead = zipInputstream.read(buffer)) > -1) {
            fileOutputStream.write(buffer, 0, bytesRead);
        }//  w  ww .j a v  a 2 s. c  o m
        fileOutputStream.flush();
    } catch (final IOException ioe) {
        // In case of error, the file is deleted
        outputFile.delete();
        throw ioe;
    }
}

From source file:Main.java

private static void fileUnZip(ZipInputStream zis, File file) throws FileNotFoundException, IOException {
    java.util.zip.ZipEntry zip = null;
    while ((zip = zis.getNextEntry()) != null) {
        String name = zip.getName();
        File f = new File(file.getAbsolutePath() + File.separator + name);
        if (zip.isDirectory()) {
            f.mkdirs();/*w ww. ja  v a  2 s  . co m*/
        } else {
            f.getParentFile().mkdirs();
            f.createNewFile();
            BufferedOutputStream bos = null;
            try {
                bos = new BufferedOutputStream(new FileOutputStream(f));
                byte b[] = new byte[2048];
                int aa = 0;
                while ((aa = zis.read(b)) != -1) {
                    bos.write(b, 0, aa);
                }
                bos.flush();
            } finally {
                bos.close();
            }
            bos.close();
        }
    }

}

From source file:gabi.FileUploadServlet.java

private static void unzip(String zipFilePath, String destDir) {
    File dir = new File(destDir);
    // create output directory if it doesn't exist
    if (!dir.exists())
        dir.mkdirs();/*from   www  .j  av  a2  s.com*/
    FileInputStream fis;
    //buffer for read and write data to file
    byte[] buffer = new byte[1024];
    try {
        fis = new FileInputStream(zipFilePath);
        ZipInputStream zis = new ZipInputStream(fis);
        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(destDir + File.separator + fileName);
            System.out.println("Unzipping to " + newFile.getAbsolutePath());
            //create directories for sub directories in zip
            new File(newFile.getParent()).mkdirs();
            FileOutputStream fos = new FileOutputStream(newFile);
            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            //close this ZipEntry
            zis.closeEntry();
            ze = zis.getNextEntry();
        }
        //close last ZipEntry
        zis.closeEntry();
        zis.close();
        fis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void unZip(String zipFile, String outputFolder) throws IOException {

    byte[] buffer = new byte[1024];

    //create output directory is not exists
    File folder = new File(outputFolder);
    if (!folder.exists()) {
        folder.mkdir();/*from w  w w. ja va  2s  .  c  om*/
    }

    //get the zip file content
    ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
    //get the zipped file list entry
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {

        String fileName = ze.getName();
        File newFile = new File(outputFolder + File.separator + fileName);

        //create all non exists folders
        //else you will hit FileNotFoundException for compressed folder
        if (ze.isDirectory())
            newFile.mkdirs();
        else {
            newFile.getParentFile().mkdirs();

            FileOutputStream fos = new FileOutputStream(newFile);

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

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

    zis.closeEntry();
    zis.close();

}

From source file:pl.psnc.synat.wrdz.common.utility.ZipUtility.java

/**
 * Unpacks specified input stream into destination directory.
 * //from  w  ww  . ja v  a2s.  c  o  m
 * @param src
 *            zip source file
 * @param dest
 *            destination folder
 * @throws IOException
 *             in case when something goes wrong in method
 */
public static void unzip(InputStream src, File dest) throws IOException {
    if (!dest.exists()) {
        throw new FileNotFoundException("Destination folder not found!");
    }
    byte[] buffer = new byte[1024];
    ZipInputStream zipInput = new ZipInputStream(src);
    ZipEntry entry = null;
    while ((entry = zipInput.getNextEntry()) != null) {
        if (!entry.isDirectory()) {
            File file = new File(dest, entry.getName());
            File parentFile = file.getParentFile();
            if (parentFile != null) {
                parentFile.mkdirs();
            }
            BufferedOutputStream bos = null;
            try {
                bos = new BufferedOutputStream(new FileOutputStream(file), 1024);
                int len;
                while ((len = zipInput.read(buffer)) != -1) {
                    bos.write(buffer, 0, len);
                }
            } finally {
                bos.flush();
                bos.close();
            }
        }
        zipInput.closeEntry();
    }
}

From source file:Main.java

public static void unzip(File zipFile, File targetDirectory) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
    try {//www.  ja v a  2s . c  om
        ZipEntry ze;
        int count;
        byte[] buffer = new byte[8192];
        while ((ze = zis.getNextEntry()) != null) {
            File file = new File(targetDirectory, ze.getName());
            File dir = ze.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs())
                throw new FileNotFoundException("Failed to ensure directory: " + dir.getAbsolutePath());
            if (ze.isDirectory())
                continue;
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }
        }
    } finally {
        zis.close();
    }
}

From source file:org.springframework.xdocreport.core.utils.TemplatesUtils.java

/**
 * Get content as string from a file/*w w w.  j av  a 2s  . c o m*/
 * 
 * @param is
 *            odt file as input stream
 * 
 * @return content as string or null
 */
public static String getODTContentAsString(InputStream is) {
    try {
        ZipInputStream zipInputStream = new ZipInputStream(is);
        ZipEntry entry;
        entry = zipInputStream.getNextEntry();
        String tmpContent = "content.txt";
        File tmpContentFile = TemplatesUtils.createFileTemp(tmpContent);
        while (entry != null) {

            if (entry.getName().equals(ODTConstants.CONTENT_XML_ENTRY)) {
                // Reads content
                byte[] buf = new byte[1024];
                int len;
                OutputStream out = new FileOutputStream(tmpContentFile);
                while ((len = zipInputStream.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.flush();
                break;
            }
            entry = zipInputStream.getNextEntry();
        }
        Reader reader = new InputStreamReader(new FileInputStream(tmpContentFile), EncodingConstants.UTF_8);
        StringWriter writer = new StringWriter();
        IOUtils.copy(reader, writer);

        return writer.toString();
    } catch (IOException e) {
        return null;
    }
}

From source file:gov.va.chir.tagline.dao.FileDao.java

public static TagLineModel loadTagLineModel(final File file) throws Exception {
    final TagLineModel model = new TagLineModel();

    // Unzip each file to temp
    final File temp = new File(System.getProperty("java.io.tmpdir"));

    byte[] buffer = new byte[BUFFER_SIZE];

    final ZipInputStream zis = new ZipInputStream(new FileInputStream(file));

    ZipEntry entry = zis.getNextEntry();

    while (entry != null) {
        final String name = entry.getName();

        File tempFile = new File(temp, name);

        // Write out file
        final FileOutputStream fos = new FileOutputStream(tempFile);
        int len;//from  w  ww .  ja  v  a  2  s . co m
        while ((len = zis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
        }

        fos.close();

        // Determine which file was written
        if (name.equalsIgnoreCase(FILENAME_FEATURES)) {
            model.setFeatures(loadFeatures(tempFile));
        } else if (name.equalsIgnoreCase(FILENAME_HEADER)) {
            model.setHeader(loadHeader(tempFile));
        } else if (name.equalsIgnoreCase(FILENAME_MODEL)) {
            model.setModel(loadModel(tempFile));
        } else {
            throw new IllegalStateException(String.format("Unknown file in TagLine model file (%s)", name));
        }

        // Delete temp file
        tempFile.delete();

        // Get next entry
        zis.closeEntry();
        entry = zis.getNextEntry();
    }

    zis.close();

    return model;
}

From source file:Main.java

public static void unzip(InputStream fin, String targetPath, Context context) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fin));
    try {/*w  w  w  .  j  av a2 s  .com*/
        ZipEntry zipEntry;
        int count;
        byte[] buffer = new byte[8192];
        while ((zipEntry = zis.getNextEntry()) != null) {
            File file = new File(targetPath, zipEntry.getName());
            File dir = zipEntry.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs()) {
                throw new FileNotFoundException("Failed to get directory: " + dir.getAbsolutePath());
            }
            if (zipEntry.isDirectory()) {
                continue;
            }
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }
            Log.d("TEST", "Unzipped " + file.getAbsolutePath());
        }
    } finally {
        zis.close();
    }
}

From source file:org.n52.oxf.util.IOHelper.java

public static void decompressAll(File zipFile, File targetDirectory) throws IOException, ZipException {

    if (!targetDirectory.isDirectory())
        throw new IOException("2nd Parameter targetDirectory is not a valid diectory!");

    byte[] buf = new byte[4096];
    ZipInputStream in = new ZipInputStream(new FileInputStream(zipFile));
    while (true) {
        // Nchsten Eintrag lesen
        ZipEntry entry = in.getNextEntry();
        if (entry == null) {
            break;
        }/*from w  ww.  ja  v a 2  s  .co m*/

        // Ausgabedatei erzeugen
        FileOutputStream out = new FileOutputStream(
                targetDirectory.getAbsolutePath() + "/" + new File(entry.getName()).getName());
        // Lese-Vorgang
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.close();

        // Eintrag schlieen
        in.closeEntry();
    }
    in.close();
}