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.meltmedia.cadmium.core.util.WarUtils.java

/**
 * Adds vHost and context-root mappings to a jboss-web.xml file contained withing a zip/war file.
 * @param inZip The zip file that the original jboss-web.xml file is in.
 * @param outZip The zip output stream to write the updated jboss-web.xml file to.
 * @param jbossWeb The zip element that represents the jboss-web.xml file.
 * @param domain The domain to add a vHost for.
 * @param context The context to add a context-root for.
 * @throws Exception//from  w  w  w.  jav  a 2 s  .co  m
 */
public static void updateDomain(ZipFile inZip, ZipOutputStream outZip, ZipEntry jbossWeb, String domain,
        String context) throws Exception {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(inZip.getInputStream(jbossWeb));

    Element rootNode = null;
    NodeList nodes = doc.getElementsByTagName("jboss-web");
    if (nodes.getLength() == 1) {
        rootNode = (Element) nodes.item(0);
    }

    if (domain != null && domain.length() > 0) {
        Element vHost = doc.createElement("virtual-host");
        removeNodesByTagName(rootNode, "virtual-host");
        vHost.appendChild(doc.createTextNode(domain));
        rootNode.appendChild(vHost);
    }

    if (context != null && context.length() > 0) {
        Element cRoot = doc.createElement("context-root");
        removeNodesByTagName(rootNode, "context-root");
        cRoot.appendChild(doc.createTextNode(context));
        rootNode.appendChild(cRoot);
    }

    storeXmlDocument(outZip, jbossWeb, doc);
}

From source file:Main.java

public static void unzip(String strZipFile) {

    try {/*from w  w  w. j  a va  2 s  .  c  om*/
        /*
         * STEP 1 : Create directory with the name of the zip file
         * 
         * For e.g. if we are going to extract c:/demo.zip create c:/demo directory where we can extract all the zip entries
         */
        File fSourceZip = new File(strZipFile);
        String zipPath = strZipFile.substring(0, strZipFile.length() - 4);
        File temp = new File(zipPath);
        temp.mkdir();
        System.out.println(zipPath + " created");

        /*
         * STEP 2 : Extract entries while creating required sub-directories
         */
        ZipFile zipFile = new ZipFile(fSourceZip);
        Enumeration<? extends ZipEntry> e = zipFile.entries();

        while (e.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            File destinationFilePath = new File(zipPath, entry.getName());

            // create directories if required.
            destinationFilePath.getParentFile().mkdirs();

            // if the entry is directory, leave it. Otherwise extract it.
            if (entry.isDirectory()) {
                continue;
            } else {
                // System.out.println("Extracting " + destinationFilePath);

                /*
                 * Get the InputStream for current entry of the zip file using
                 * 
                 * InputStream getInputStream(Entry entry) method.
                 */
                BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

                int b;
                byte buffer[] = new byte[1024];

                /*
                 * read the current entry from the zip file, extract it and write the extracted file.
                 */
                FileOutputStream fos = new FileOutputStream(destinationFilePath);
                BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);

                while ((b = bis.read(buffer, 0, 1024)) != -1) {
                    bos.write(buffer, 0, b);
                }

                // flush the output stream and close it.
                bos.flush();
                bos.close();

                // close the input stream.
                bis.close();
            }

        }
        zipFile.close();
    } catch (IOException ioe) {
        System.out.println("IOError :" + ioe);
    }

}

From source file:com.gisgraphy.domain.geoloc.importer.ImporterHelper.java

/**
 * unzip a file in the same directory as the zipped file
 * //w  w w  .  ja  v a2 s.com
 * @param file
 *            The file to unzip
 */
public static void unzipFile(File file) {
    logger.info("will Extracting file: " + file.getName());
    Enumeration<? extends ZipEntry> entries;
    ZipFile zipFile;

    try {
        zipFile = new ZipFile(file);

        entries = zipFile.entries();

        while (entries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) entries.nextElement();

            if (entry.isDirectory()) {
                // Assume directories are stored parents first then
                // children.
                (new File(entry.getName())).mkdir();
                continue;
            }

            logger.info("Extracting file: " + entry.getName() + " to " + file.getParent() + File.separator
                    + entry.getName());
            copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(
                    new FileOutputStream(file.getParent() + File.separator + entry.getName())));
        }

        zipFile.close();
    } catch (IOException e) {
        logger.error("can not unzip " + file.getName() + " : " + e.getMessage());
        throw new ImporterException(e);
    }
}

From source file:org.apache.maven.doxia.siterenderer.DefaultSiteRenderer.java

private static void copyFileFromZip(ZipFile file, ZipEntry entry, File destFile) throws IOException {
    FileOutputStream fos = new FileOutputStream(destFile);

    try {//  w w  w.ja va2  s. c o  m
        IOUtil.copy(file.getInputStream(entry), fos);
    } finally {
        IOUtil.close(fos);
    }
}

From source file:org.jboss.pvt.harness.validators.NOSignatureCheckValidator.java

@Override
protected boolean validate(File jarFile, Map<String, String> params) throws Exception {
    ZipFile zipFile = new ZipFile(jarFile);
    StringWriter sw = new StringWriter();
    IOUtils.copy(new InputStreamReader(zipFile.getInputStream(zipFile.getEntry("META-INF/MANIFEST.MF"))), sw);
    return !sw.getBuffer().toString().contains("Digest:");
}

From source file:kr.ac.kaist.swrc.jhannanum.share.JSONZipReader.java

/**
 * read JSON file from JAR file./*from   w w w  .j a  v  a 2 s . c o  m*/
 * @return Length of JSON Keys
 * @throws JSONException, IOException
 */

protected int read() throws JSONException, IOException {
    ZipFile zip = new ZipFile(zipFilePath);
    ZipEntry entry = zip.getEntry(jsonFile);
    InputStream in = zip.getInputStream(entry);
    this.json = read(in);
    zip.close();
    return json.length();
}

From source file:com.aurel.track.admin.server.dbbackup.DatabaseBackupBL.java

public static Properties getBackupInfo(File backupFile) throws DatabaseBackupBLException {
    Properties prop = new Properties();
    ZipFile zipFile = null;

    try {/*from   w  ww.j  av  a2  s  . c o  m*/
        zipFile = new ZipFile(backupFile, ZipFile.OPEN_READ);
    } catch (IOException e) {
        throw new DatabaseBackupBLException(e.getMessage(), e);
    }

    ZipEntry zipEntryInfo = zipFile.getEntry(DataReader.FILE_NAME_INFO);
    if (zipEntryInfo == null) {
        try {
            zipFile.close();
        } catch (IOException e) {
            throw new DatabaseBackupBLException(e.getMessage(), e);
        }
        throw new DatabaseBackupBLException("Invalid backup. No file info");
    }

    try {
        prop.load(zipFile.getInputStream(zipEntryInfo));
    } catch (IOException e) {
        throw new DatabaseBackupBLException(e.getMessage(), e);
    }

    try {
        zipFile.close();
    } catch (IOException e) {
        throw new DatabaseBackupBLException(e.getMessage(), e);
    }
    return prop;
}

From source file:com.blazemeter.bamboo.plugin.ServiceManager.java

public static void unzip(String srcZipFileName, String destDirectoryName, BuildLogger logger) {
    try {//from ww  w .java  2 s .  c  o m
        BufferedInputStream bufIS = null;
        // create the destination directory structure (if needed)
        File destDirectory = new File(destDirectoryName);
        destDirectory.mkdirs();

        // open archive for reading
        File file = new File(srcZipFileName);
        ZipFile zipFile = new ZipFile(file, ZipFile.OPEN_READ);

        //for every zip archive entry do
        Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries();
        while (zipFileEntries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            logger.addBuildLogEntry("\tExtracting jtl report: " + entry);

            //create destination file
            File destFile = new File(destDirectory, entry.getName());

            //create parent directories if needed
            File parentDestFile = destFile.getParentFile();
            parentDestFile.mkdirs();

            bufIS = new BufferedInputStream(zipFile.getInputStream(entry));
            int currentByte;

            // buffer for writing file
            byte data[] = new byte[BUFFER_SIZE];

            // write the current file to disk
            FileOutputStream fOS = new FileOutputStream(destFile);
            BufferedOutputStream bufOS = new BufferedOutputStream(fOS, BUFFER_SIZE);

            while ((currentByte = bufIS.read(data, 0, BUFFER_SIZE)) != -1) {
                bufOS.write(data, 0, currentByte);
            }

            // close BufferedOutputStream
            bufOS.flush();
            bufOS.close();
        }
        bufIS.close();
    } catch (Exception e) {
        logger.addErrorLogEntry("Failed to unzip report: check that it is downloaded");
    }
}

From source file:marytts.util.io.FileUtils.java

private static void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException {

    if (entry.isDirectory()) {
        createDir(new File(outputDir, entry.getName()));
        return;//from   w  w  w.j  av a 2 s.co m
    }

    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        createDir(outputFile.getParentFile());
    }

    BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));

    try {
        IOUtils.copy(inputStream, outputStream);
    } finally {
        outputStream.close();
        inputStream.close();
    }
}

From source file:org.guvnor.m2repo.backend.server.GuvnorM2Repository.java

private static String loadPomFromJar(final File file) {
    InputStream is = null;//from ww  w.  ja va2s . c o m
    InputStreamReader isr = null;
    try {
        ZipFile zip = new ZipFile(file);

        for (Enumeration e = zip.entries(); e.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) e.nextElement();

            if (entry.getName().startsWith("META-INF/maven") && entry.getName().endsWith("pom.xml")) {
                is = zip.getInputStream(entry);
                isr = new InputStreamReader(is, "UTF-8");
                StringBuilder sb = new StringBuilder();
                for (int c = isr.read(); c != -1; c = isr.read()) {
                    sb.append((char) c);
                }
                return sb.toString();
            }
        }
    } catch (ZipException e) {
        log.error(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage());
    } finally {
        if (isr != null) {
            try {
                isr.close();
            } catch (IOException e) {
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }

    return null;
}