Example usage for java.util.zip ZipFile getInputStream

List of usage examples for java.util.zip ZipFile getInputStream

Introduction

In this page you can find the example usage for java.util.zip ZipFile getInputStream.

Prototype

public InputStream getInputStream(ZipEntry entry) throws IOException 

Source Link

Document

Returns an input stream for reading the contents of the specified zip file entry.

Usage

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

public static void doUnzipFile(ZipFile zipFile, File dest) throws IOException {
    if (!dest.exists()) {
        FileUtils.forceMkdir(dest);//from   www. ja  v  a  2 s . c o  m
    }
    Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        File file = new File(dest, entry.getName());
        if (entry.getName().endsWith(File.separator)) {
            FileUtils.forceMkdir(file);
        } else {
            OutputStream out = FileUtils.openOutputStream(file);
            InputStream in = zipFile.getInputStream(entry);
            try {
                IOUtils.copy(in, out);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                IOUtils.closeQuietly(out);
            }
        }
    }
    zipFile.close();
}

From source file:com.serotonin.m2m2.Main.java

private static Properties getProperties(ZipFile zip) throws IOException, Main.ModulePropertiesException {
    ZipEntry propFile = zip.getEntry("module.signed");
    if (propFile != null) {
        return getProperties(zip.getInputStream(propFile), true);
    }/*  w  ww.  java  2s  .c o m*/
    propFile = zip.getEntry("module.properties");
    if (propFile == null) {
        throw new RuntimeException("module.properties not found in module zip file");
    }
    return getProperties(zip.getInputStream(propFile), false);
}

From source file:org.eclipse.vjet.testframework.artifactmanager.project.ProjectArtifactManager.java

public static String unzipProject(File zipFile, File destDir, String projectName) throws IOException {
    if (!TestUtils.isDirectoryWritable(destDir, true)) {
        return "unable to create writable directory: " + destDir;
    }//from   www.ja va2s  .  com
    if (!TestUtils.isFileReadable(zipFile)) {
        return "no readable file: " + zipFile;
    }

    ZipFile zip = null;
    String projectEntryName = "projects" + "/" + projectName;
    try {
        zip = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> entries = zip.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (!entry.isDirectory() && entry.getName().startsWith(projectEntryName)) {
                File destFile = new File(destDir, entry.getName());
                InputStream src = null;
                try {
                    src = zip.getInputStream(entry);
                    String result = TestUtils.write(src, destFile);
                    if (null != result) {
                        return result;
                    }
                } finally {
                    if (null != src) {
                        src.close();
                    }
                }
            }
        }
    } finally {
        if (null != zip) {
            zip.close();
        }
    }
    return null;
}

From source file:de.xwic.appkit.core.util.ZipUtil.java

/**
 * Unzips the files from the zipped file into the destination folder.
 * //from  ww  w.  ja  v a 2s. com
 * @param zippedFile
 *            the files array
 * @param destinationFolder
 *            the folder in which the zip archive will be unzipped
 * @return the file array which consists into the files which were zipped in
 *         the zippedFile
 * @throws IOException
 */
public static File[] unzip(File zippedFile, String destinationFolder) throws IOException {

    ZipFile zipFile = null;
    List<File> files = new ArrayList<File>();

    try {

        zipFile = new ZipFile(zippedFile);
        Enumeration<?> entries = zipFile.entries();

        while (entries.hasMoreElements()) {

            ZipEntry entry = (ZipEntry) entries.nextElement();

            if (!entry.isDirectory()) {

                String filePath = destinationFolder + System.getProperty("file.separator") + entry.getName();
                FileOutputStream stream = new FileOutputStream(filePath);

                InputStream is = zipFile.getInputStream(entry);

                log.info("Unzipping " + entry.getName());

                int n = 0;
                while ((n = is.read(BUFFER)) > 0) {
                    stream.write(BUFFER, 0, n);
                }

                is.close();
                stream.close();

                files.add(new File(filePath));

            }

        }

        zipFile.close();
    } catch (IOException e) {

        log.error("Error: " + e.getMessage(), e);
        throw e;

    } finally {

        try {

            if (null != zipFile) {
                zipFile.close();
            }

        } catch (IOException e) {
            log.error("Error: " + e.getMessage(), e);
            throw e;
        }

    }

    File[] array = files.toArray(new File[files.size()]);
    return array;
}

From source file:com.twosigma.beakerx.kernel.magic.command.ClasspathAddMvnDepsCellMagicCommandTest.java

private static void unzipRepo() {
    try {/*from  w  w w. j a  va 2  s  .co  m*/
        ZipFile zipFile = new ZipFile(BUILD_PATH + "/testMvnCache.zip");
        Enumeration<?> enu = zipFile.entries();
        while (enu.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) enu.nextElement();
            String name = BUILD_PATH + "/" + zipEntry.getName();
            File file = new File(name);
            if (name.endsWith("/")) {
                file.mkdirs();
                continue;
            }

            File parent = file.getParentFile();
            if (parent != null) {
                parent.mkdirs();
            }

            InputStream is = zipFile.getInputStream(zipEntry);
            FileOutputStream fos = new FileOutputStream(file);
            byte[] bytes = new byte[1024];
            int length;
            while ((length = is.read(bytes)) >= 0) {
                fos.write(bytes, 0, length);
            }
            is.close();
            fos.close();

        }
        zipFile.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:au.org.ands.vocabs.toolkit.utils.ToolkitFileUtils.java

/** Compress the files in the backup folder for a project.
 * @param projectId The project ID//from ww w .  j a v  a 2  s. c  o  m
 * @throws IOException Any exception when reading/writing data.
 */
public static void compressBackupFolder(final String projectId) throws IOException {
    String backupPath = getBackupPath(projectId);
    if (!Files.isDirectory(Paths.get(backupPath))) {
        // No such directory, so nothing to do.
        return;
    }
    String projectSlug = makeSlug(projectId);
    // The name of the ZIP file that does/will contain all
    // backups for this project.
    Path zipFilePath = Paths.get(backupPath).resolve(projectSlug + ".zip");
    // A temporary ZIP file. Any existing content in the zipFilePath
    // will be copied into this, followed by any other files in
    // the directory that have not yet been added.
    Path tempZipFilePath = Paths.get(backupPath).resolve("temp" + ".zip");

    File tempZipFile = tempZipFilePath.toFile();
    if (!tempZipFile.exists()) {
        tempZipFile.createNewFile();
    }

    ZipOutputStream tempZipOut = new ZipOutputStream(new FileOutputStream(tempZipFile));

    File existingZipFile = zipFilePath.toFile();
    if (existingZipFile.exists()) {
        ZipFile zipIn = new ZipFile(existingZipFile);

        Enumeration<? extends ZipEntry> entries = zipIn.entries();
        while (entries.hasMoreElements()) {
            ZipEntry e = entries.nextElement();
            logger.debug("compressBackupFolder copying: " + e.getName());
            tempZipOut.putNextEntry(e);
            if (!e.isDirectory()) {
                copy(zipIn.getInputStream(e), tempZipOut);
            }
            tempZipOut.closeEntry();
        }
        zipIn.close();
    }

    File dir = new File(backupPath);
    File[] files = dir.listFiles();

    for (File source : files) {
        if (!source.getName().toLowerCase().endsWith(".zip")) {
            logger.debug("compressBackupFolder compressing and " + "deleting file: " + source.toString());
            if (zipFile(tempZipOut, source)) {
                source.delete();
            }
        }
    }

    tempZipOut.flush();
    tempZipOut.close();
    tempZipFile.renameTo(existingZipFile);
}

From source file:Main.java

public static void unzipEPub(String inputZip, String destinationDirectory) throws IOException {
    int BUFFER = 2048;
    List zipFiles = new ArrayList();
    File sourceZipFile = new File(inputZip);
    File unzipDestinationDirectory = new File(destinationDirectory);
    unzipDestinationDirectory.mkdir();//from  w  w w . j  a v  a  2  s . c  o m

    ZipFile zipFile;
    zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
    Enumeration zipFileEntries = zipFile.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {

        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
        String currentEntry = entry.getName();
        File destFile = new File(unzipDestinationDirectory, currentEntry);

        if (currentEntry.endsWith(".zip")) {
            zipFiles.add(destFile.getAbsolutePath());
        }

        File destinationParent = destFile.getParentFile();
        destinationParent.mkdirs();

        if (!entry.isDirectory()) {
            BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));
            int currentByte;
            // buffer for writing file
            byte data[] = new byte[BUFFER];

            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

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

        }

    }
    zipFile.close();

    for (Iterator iter = zipFiles.iterator(); iter.hasNext();) {
        String zipName = (String) iter.next();
        unzipEPub(zipName,
                destinationDirectory + File.separatorChar + zipName.substring(0, zipName.lastIndexOf(".zip")));
    }
}

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

/**
 * Create file list from Zip file.//w w  w  .j  a  v a 2  s .  c om
 * @param originalZipFile source Zip file
 * @param targetFileList target file list file
 */
public static void createFileList(File originalZipFile, File targetFileList) throws IOException {
    ZipFile zip = new ZipFile(originalZipFile);
    try {
        FileOutputStream output = new FileOutputStream(targetFileList);
        try {
            FileList.Writer writer = FileList.createWriter(output, true);
            Enumeration<? extends ZipEntry> entries = zip.entries();
            while (entries.hasMoreElements()) {
                ZipEntry next = entries.nextElement();
                InputStream input = zip.getInputStream(next);
                try {
                    OutputStream target = writer.openNext(FileList.content(next.getName().replace('\\', '/')));
                    try {
                        IOUtils.pipingAndClose(input, target);
                    } finally {
                        target.close();
                    }
                } finally {
                    input.close();
                }
            }
            writer.close();
        } finally {
            output.close();
        }
    } finally {
        zip.close();
    }
}

From source file:org.n52.geoar.newdata.PluginLoader.java

/**
 * Extracts and parses the geoar-plugin.xml plugin-descriptor to create and
 * fill a {@link PluginInfo} instance./*from   w  w w. j av  a2 s  .  c o m*/
 * 
 * @param pluginFile
 * @return
 */
private static PluginInfo readPluginInfoFromPlugin(File pluginFile) {
    try {
        ZipFile zipFile = new ZipFile(pluginFile);
        ZipEntry pluginDescriptorEntry = zipFile.getEntry("geoar-plugin.xml");
        if (pluginDescriptorEntry == null) {
            return null;
        }

        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(zipFile.getInputStream(pluginDescriptorEntry));
        // Find name
        String name = null;
        NodeList nodeList = document.getElementsByTagName("name");
        if (nodeList != null && nodeList.getLength() >= 1) {
            name = nodeList.item(0).getTextContent();
        } else {
            LOG.warn("Plugin Descriptor for " + pluginFile.getName() + " does not specify a name");
        }

        // Find publisher
        String publisher = null;
        nodeList = document.getElementsByTagName("publisher");
        if (nodeList != null && nodeList.getLength() >= 1) {
            publisher = nodeList.item(0).getTextContent();
        } else {
            LOG.warn("Plugin Descriptor for " + pluginFile.getName() + " does not specify a publisher");
        }

        // Find description
        String description = null;
        nodeList = document.getElementsByTagName("description");
        if (nodeList != null && nodeList.getLength() >= 1) {
            description = nodeList.item(0).getTextContent();
        } else {
            LOG.warn("Plugin Descriptor for " + pluginFile.getName() + " does not specify a description");
        }

        // Find identifier
        String identifier = null;
        nodeList = document.getElementsByTagName("identifier");
        if (nodeList != null && nodeList.getLength() >= 1) {
            identifier = nodeList.item(0).getTextContent();
        } else {
            LOG.warn("Plugin Descriptor for " + pluginFile.getName() + " does not specify an identifier");
        }

        // Find version
        Long version = null;
        nodeList = document.getElementsByTagName("version");
        if (nodeList != null && nodeList.getLength() >= 1) {
            String versionString = "-" + nodeList.item(0).getTextContent();

            Matcher matcher = pluginVersionPattern.matcher(versionString);
            if (matcher.find() && matcher.group(1) != null) {
                try {
                    version = parseVersionNumber(matcher.group(1));
                } catch (NumberFormatException e) {
                    LOG.error("Plugin filename version invalid: " + matcher.group(1));
                }
            }
        } else {
            LOG.warn("Plugin Descriptor for " + pluginFile.getName() + " does not specify a version");
        }

        if (identifier == null) {
            identifier = name;
        }

        return new PluginInfo(pluginFile, name, description, version, identifier, publisher);
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (ZipException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:azkaban.utils.Utils.java

public static void unzip(ZipFile source, File dest) throws IOException {
    Enumeration<?> entries = source.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        File newFile = new File(dest, entry.getName());
        if (entry.isDirectory()) {
            newFile.mkdirs();//from w  w w .  j  a va 2  s .  c  om
        } else {
            newFile.getParentFile().mkdirs();
            InputStream src = source.getInputStream(entry);
            try {
                OutputStream output = new BufferedOutputStream(new FileOutputStream(newFile));
                try {
                    IOUtils.copy(src, output);
                } finally {
                    output.close();
                }
            } finally {
                src.close();
            }
        }
    }
}