Example usage for java.util.zip ZipInputStream getNextEntry

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

Introduction

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

Prototype

public ZipEntry getNextEntry() throws IOException 

Source Link

Document

Reads the next ZIP file entry and positions the stream at the beginning of the entry data.

Usage

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();//from ww  w.ja va  2 s .  c om
        } 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:Main.java

public static void unpack(InputStream in, File targetDir) throws IOException {
    ZipInputStream zin = new ZipInputStream(in);
    ZipEntry entry;/*  w  ww  . j  a v  a  2  s  .co m*/
    while ((entry = zin.getNextEntry()) != null) {
        String extractFilePath = entry.getName();
        if ((extractFilePath.startsWith("/")) || (extractFilePath.startsWith("\\"))) {
            extractFilePath = extractFilePath.substring(1);
        }
        File extractFile = new File(new StringBuilder().append(targetDir.getPath()).append(File.separator)
                .append(extractFilePath).toString());
        if (entry.isDirectory()) {
            if (!extractFile.exists())
                extractFile.mkdirs();
        } else {
            File parent = extractFile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }

            FileOutputStream os = new FileOutputStream(extractFile);
            copyFile(zin, os);
            os.flush();
            os.close();
        }
    }
}

From source file:Main.java

public static void unZip(InputStream zipFileIS, 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 ww w. j av a  2s .  c  om
    }

    //get the zip file content
    ZipInputStream zis = new ZipInputStream(zipFileIS);
    //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);

        if (ze.isDirectory()) {
            newFile.mkdirs();
        } else {

            FileOutputStream fos = new FileOutputStream(newFile);

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

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

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

From source file:com.mobicage.rogerthat.mfr.dal.Streamable.java

protected static Object fromBase64(String base64String) {
    try {/*from  w w  w .  j  a v a 2 s . co  m*/
        ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decodeBase64(base64String));

        try {
            ZipInputStream zip = new ZipInputStream(bis);
            try {
                zip.getNextEntry();
                ObjectInput in = new ObjectInputStream(zip);
                try {
                    return in.readObject();
                } finally {
                    in.close();
                }
            } finally {
                zip.close();
            }
        } finally {
            bis.close();
        }
    } catch (ClassNotFoundException e) {
        log.log((Level.SEVERE), "ClassNotFoundException while deserializing member", e);
        return null;
    } catch (IOException e) {
        log.log((Level.SEVERE), "IOException while deserializing member", e);
        return null;
    }
}

From source file:ch.ivyteam.ivy.maven.util.ClasspathJar.java

private static InputStream getInputStream(ZipInputStream zin, String entry) throws IOException {
    for (ZipEntry e; (e = zin.getNextEntry()) != null;) {
        if (e.getName().equals(entry)) {
            return zin;
        }//  w  ww  .ja v  a  2s  .  c  o m
    }
    throw new EOFException("Cannot find " + entry);
}

From source file:com.microsoft.azure.hdinsight.util.HDInsightJobViewUtils.java

public static void unzip(String zipFilePath, String destDirectory) throws IOException {
    File destDir = new File(destDirectory);
    if (!destDir.exists()) {
        destDir.mkdir();/* w  ww  .  j  a  va  2  s.com*/
    }
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    while (entry != null) {
        String filePath = destDirectory + File.separator + entry.getName();
        if (!entry.isDirectory()) {
            extractFile(zipIn, filePath);
        } else {
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

From source file:Main.java

/**
 * Decompresses a given byte array that is a compressed folder.
 * //w w w.j  av  a  2 s.  c o m
 * @param folderAsCompressedArray to decompress
 * @param unzippedLocation where the decompressed folder should be
 * @throws IOException e
 * @throws FileNotFoundException e
 */
public static void decompressFolderByteArray(byte[] folderAsCompressedArray, File unzippedLocation)
        throws IOException, FileNotFoundException {
    ZipInputStream zipFile = new ZipInputStream(new ByteArrayInputStream(folderAsCompressedArray));
    ZipEntry ze = null;
    final int minusOne = -1;
    while ((ze = zipFile.getNextEntry()) != null) {
        FileOutputStream fout = new FileOutputStream(
                new File(unzippedLocation, ze.getName()).getAbsolutePath());
        for (int c = zipFile.read(); c != minusOne; c = zipFile.read()) {
            fout.write(c);
        }
        zipFile.closeEntry();
        fout.close();
    }
    zipFile.close();
}

From source file:eu.openanalytics.rsb.AbstractITCase.java

public static void validateZipResult(final InputStream responseStream) throws IOException {
    final ZipInputStream result = new ZipInputStream(responseStream);
    ZipEntry ze = null;/*w w  w .j  a v  a  2s  . c om*/

    while ((ze = result.getNextEntry()) != null) {
        if (ze.getName().endsWith(".pdf")) {
            return;
        }
    }

    fail("No PDF file found in Zip result");
}

From source file:edu.clemson.cs.nestbed.common.util.ZipUtils.java

public static File unzip(byte[] zipData, File directory) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(zipData);
    ZipInputStream zis = new ZipInputStream(bais);
    ZipEntry entry = zis.getNextEntry();
    File root = null;//from   w  w w.j  ava  2  s .  c  om

    while (entry != null) {
        if (entry.isDirectory()) {
            File f = new File(directory, entry.getName());
            f.mkdir();

            if (root == null) {
                root = f;
            }
        } else {
            BufferedOutputStream out;
            out = new BufferedOutputStream(new FileOutputStream(new File(directory, entry.toString())),
                    BUFFER_SIZE);

            // ZipInputStream can only give us one byte at a time...
            for (int data = zis.read(); data != -1; data = zis.read()) {
                out.write(data);
            }
            out.close();
        }

        zis.closeEntry();
        entry = zis.getNextEntry();
    }
    zis.close();

    return root;
}

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();//w ww . j a  v a 2 s . c  o  m
    }

    //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();

}