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:io.card.development.recording.Recording.java

private static Hashtable<String, byte[]> unzipFiles(InputStream recordingStream) throws IOException {
    ZipEntry zipEntry;/*from www .j  av a  2s.  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:com.wavemaker.StudioInstallService.java

public static File unzipFile(File zipfile) {
    int BUFFER = 2048;

    String zipname = zipfile.getName();
    int extindex = zipname.lastIndexOf(".");

    try {//from   www .  ja va2  s  .  c o  m
        File zipFolder = new File(zipfile.getParentFile(), zipname.substring(0, extindex));
        if (zipFolder.exists())
            org.apache.commons.io.FileUtils.deleteDirectory(zipFolder);
        zipFolder.mkdir();

        File currentDir = zipFolder;
        //File currentDir = zipfile.getParentFile();

        BufferedOutputStream dest = null;
        FileInputStream fis = new FileInputStream(zipfile.toString());
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            System.out.println("Extracting: " + entry);
            if (entry.isDirectory()) {
                File f = new File(currentDir, entry.getName());
                if (f.exists())
                    f.delete(); // relevant if this is the top level folder
                f.mkdir();
            } else {
                int count;
                byte data[] = new byte[BUFFER];
                //needed for non-dir file ace/ace.js created by 7zip
                File destFile = new File(currentDir, entry.getName());
                // write the files to the disk
                FileOutputStream fos = new FileOutputStream(currentDir.toString() + "/" + 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();

        return currentDir;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return (File) null;

}

From source file:com.dv.util.DataViewerZipUtil.java

/**
 *
 * @param zipInput//from w ww.  j a  v a 2 s .  c  o m
 * @param dest
 * @throws IOException
 */
private static void doUnzipFile(ZipInputStream zipInput, File dest) throws IOException {
    if (!dest.exists()) {
        FileUtils.forceMkdir(dest);
    }
    for (ZipEntry entry; (entry = zipInput.getNextEntry()) != null;) {
        String entryName = entry.getName();

        File file = new File(dest, entry.getName());
        if (entryName.endsWith(File.separator)) {
            FileUtils.forceMkdir(file);
        } else {
            OutputStream out = FileUtils.openOutputStream(file);
            try {
                IOUtils.copy(zipInput, out);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                IOUtils.closeQuietly(out);
            }
        }
        zipInput.closeEntry();
    }
}

From source file:com.jsonstore.util.JSONStoreUtil.java

public static void unpack(InputStream in, File targetDir) throws IOException {
    ZipInputStream zin = new ZipInputStream(in);

    ZipEntry entry;/*from  w  w w. j  a va2 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(targetDir.getPath() + File.separator + extractFilePath);
        if (entry.isDirectory()) {
            if (!extractFile.exists()) {
                extractFile.mkdirs();
            }
            continue;
        } else {
            File parent = extractFile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
        }

        // if not directory instead of the previous check and continue
        OutputStream os = new BufferedOutputStream(new FileOutputStream(extractFile));
        copyFile(zin, os);
        os.flush();
        os.close();
    }
}

From source file:com.twinsoft.convertigo.engine.util.ZipUtils.java

/*** Added by julienda - 08/09/2012
 * Return the project name by reading the first directory into the archive (.car)
 * @param path: the archive path//from  ww w .j  a v a  2  s.c om
 * @return filename: the project name
 * @throws IOException */
public static String getProjectName(String path) throws IOException {
    Engine.logEngine.trace("PATH: " + path);

    ZipInputStream zis = new ZipInputStream(new FileInputStream(path));
    ZipEntry ze = null;
    String fileName = null;
    try {
        if ((ze = zis.getNextEntry()) != null) {
            fileName = ze.getName().replaceAll("/.*", "");
            Engine.logEngine.trace("ZipUtils.getProjectName() - fileName: " + fileName);
        }
    } finally {
        zis.close();
    }
    return fileName;
}

From source file:ZipUtil.java

public static void unZipZipFileToLocation(File zipFile, File targetDir) throws IOException {
    if (!targetDir.isDirectory()) {
        throw new Exception("Target is not a directory.");
    }/*from w  w w . j  a  v  a  2 s.  c o m*/
    FileInputStream flInStr = new FileInputStream(zipFile);
    try {
        ZipInputStream zis = new ZipInputStream(flInStr);
        try {
            ZipEntry entry = null;
            while ((entry = zis.getNextEntry()) != null) {
                String name = entry.getName();
                File newFile = new File(targetDir, name);
                if (entry.isDirectory() && !newFile.exists()) {
                    newFile.mkdirs();
                } else if (!entry.isDirectory()) {
                    if (newFile.exists()) {
                        newFile.delete();
                    }
                    File parentDir = newFile.getParentFile();
                    if (!parentDir.exists()) {
                        parentDir.mkdirs();
                    }
                    FileOutputStream stmOut = new FileOutputStream(newFile);
                    try {
                        simpleInputStreamToOutputStream(zis, stmOut);
                    } finally {
                        stmOut.close();
                    }
                }
            } //end while.
        } finally {
            zis.close();
        }
    } finally {
        flInStr.close();
    }
}

From source file:com.cip.crane.agent.utils.FileExtractUtils.java

/**
 * Unzip an input file into an output file.
 * <p>//ww w .  ja  v  a  2  s.  c o  m
 * The output file is created in the output folder, having the same name
 * as the input file, minus the '.zip' extension. 
 * 
 * @param inputFile     the input .zip file
 * @param outputDir     the output directory file. 
 * @throws IOException 
 * @throws FileNotFoundException
 *  
 * @return  The {@File} with the ungzipped content.
 */
public static void unZip(final File inputFile, final File outputDir) throws FileNotFoundException, IOException {
    LOG.debug(
            String.format("Unzipping %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));

    byte[] buf = new byte[1024];
    ZipInputStream zipinputstream = null;
    ZipEntry zipentry;
    zipinputstream = new ZipInputStream(new FileInputStream(inputFile));

    zipentry = zipinputstream.getNextEntry();
    while (zipentry != null) {
        //for each entry to be extracted
        String entryName = outputDir + "/" + zipentry.getName();
        entryName = entryName.replace('/', File.separatorChar);
        entryName = entryName.replace('\\', File.separatorChar);
        int n;
        FileOutputStream fileoutputstream;
        File newFile = new File(entryName);

        if (zipentry.isDirectory()) {
            if (!newFile.mkdirs()) {
                break;
            }
            zipentry = zipinputstream.getNextEntry();
            continue;
        }
        fileoutputstream = new FileOutputStream(entryName);

        while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
            fileoutputstream.write(buf, 0, n);
        }

        fileoutputstream.close();
        zipinputstream.closeEntry();
        zipentry = zipinputstream.getNextEntry();

    } //while
    zipinputstream.close();
}

From source file:eu.freme.eservices.epublishing.Unzipper.java

public static void unzip(ZipInputStream zis, File outputFolder) throws IOException {
    //create output directory is not exists

    if (!outputFolder.exists() && !outputFolder.mkdirs()) {
        throw new IOException("Cannot create directory " + outputFolder);
    }//from   w  w w.java  2s.  com

    //get the zipped file list entry
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {
        String fileName = ze.getName();
        File newFile = new File(outputFolder, fileName);

        logger.debug("file unzip : " + newFile.getAbsoluteFile());

        //create all non exists folders
        //else you will hit FileNotFoundException for compressed folder
        File parentDir = newFile.getParentFile();
        if (!parentDir.exists() && !parentDir.mkdirs()) {
            throw new IOException("Cannot create directory " + newFile.getParent());
        }

        if (ze.isDirectory()) {
            newFile.mkdirs();
        } else {
            try (FileOutputStream fos = new FileOutputStream(newFile)) {
                IOUtils.copyLarge(zis, fos);
            }
        }
        ze = zis.getNextEntry();
    }
    zis.closeEntry();
}

From source file:com.oprisnik.semdroid.app.parser.manifest.AXMLConverter.java

public static String getManifestString(File apk) {
    String result = null;/*from www.j  ava2 s.co m*/
    ZipInputStream zis = null;
    FileInputStream fis = null;
    try {

        fis = new FileInputStream(apk);

        zis = new ZipInputStream(fis);
        for (ZipEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) {
            if (entry.getName().equals("AndroidManifest.xml")) {
                result = getManifestString(zis);
                break;
            }
        }
    } catch (FileNotFoundException e) {
        IOUtils.closeQuietly(fis);
    } catch (IOException e) {
    } finally {
        IOUtils.closeQuietly(zis);
    }
    return result;
}

From source file:ZipFileIO.java

/**
 * Extract zip file to destination folder.
 * //from  w ww. jav a  2  s .c  om
 * @param file
 *            zip file to extract
 * @param destination
 *            destinatin folder
 */
public static void extract(File file, File destination) throws IOException {
    ZipInputStream in = null;
    OutputStream out = null;
    try {
        // Open the ZIP file
        in = new ZipInputStream(new FileInputStream(file));

        // Get the first entry
        ZipEntry entry = null;

        while ((entry = in.getNextEntry()) != null) {
            String outFilename = entry.getName();

            // Open the output file
            if (entry.isDirectory()) {
                new File(destination, outFilename).mkdirs();
            } else {
                out = new FileOutputStream(new File(destination, outFilename));

                // Transfer bytes from the ZIP file to the output file
                byte[] buf = new byte[1024];
                int len;

                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                // Close the stream
                out.close();
            }
        }
    } finally {
        // Close the stream
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}