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:fridgegameinstaller.installation.java

public static void unzip(ZipFile zipFile, File jiniHomeParentDir) {

    Enumeration files = zipFile.entries();
    File f = null;/*from w  ww.java  2 s  . com*/
    FileOutputStream fos = null;

    while (files.hasMoreElements()) {
        try {
            ZipEntry entry = (ZipEntry) files.nextElement();
            InputStream eis = zipFile.getInputStream(entry);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;

            f = new File(jiniHomeParentDir.getAbsolutePath() + File.separator + entry.getName());

            if (entry.isDirectory()) {
                f.mkdirs();
                continue;
            } else {
                f.getParentFile().mkdirs();
                f.createNewFile();
            }

            fos = new FileOutputStream(f);

            while ((bytesRead = eis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
            continue;
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    }
}

From source file:gdt.data.entity.facet.ExtensionHandler.java

public static InputStream getResourceStream(String jar$, String resource$) {
    try {/*  w  w w.j av a 2 s.c o  m*/
        //      System.out.println("ExtensionHandler:loadIcon:jar="+jar$);
        ZipFile zf = new ZipFile(jar$);
        Enumeration<? extends ZipEntry> entries = zf.entries();
        ZipEntry ze;
        String[] sa;
        while (entries.hasMoreElements()) {
            try {
                ze = entries.nextElement();
                sa = ze.getName().split("/");
                //     System.out.println("ExtensionHandler:loadIcon:zip entry="+sa[sa.length-1]);
                if (resource$.equals(sa[sa.length - 1])) {
                    InputStream is = zf.getInputStream(ze);
                    if (is != null)
                        return is;

                }
            } catch (Exception e) {

            }
        }
        return null;
    } catch (Exception e) {
        Logger.getLogger(ExtensionHandler.class.getName()).severe(e.toString());
        return null;
    }

}

From source file:com.meltmedia.cadmium.core.util.WarUtils.java

/**
 * This will update the web.xml contained within a zip/war file to add a shiro security filter.
 * @param inZip The zip file to pull the original web.xml file from.
 * @param outZip The zip output stream to write the updated web.xml file to.
 * @param e The zip entry the points to the web.xml file in the zip files.
 * @throws Exception//w  ww . jav  a  2  s  .c o  m
 */
private static void addSecurity(ZipFile inZip, ZipOutputStream outZip, ZipEntry e) throws Exception {

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(inZip.getInputStream(e));

    Element root = doc.getDocumentElement();

    addListener(doc, root);

    addContextParam(doc, root);

    addEnvContextParam(doc, root);

    addFilter(doc, root);

    addFilterMapping(doc, root);

    storeXmlDocument(outZip, e, doc);
}

From source file:gdt.data.entity.facet.ExtensionHandler.java

public static InputStream getResourceStream(Entigrator entigrator, String extension$, String resource$) {
    try {/*from  w w  w  .j  a  v  a 2  s  .  c  o  m*/

        System.out.println(
                "ExtensionHandler:getResourceStream:extension=" + extension$ + " resource=" + resource$);
        Sack extension = entigrator.getEntityAtKey(extension$);
        String lib$ = extension.getElementItemAt("field", "lib");
        String jar$ = entigrator.getEntihome() + "/" + extension$ + "/" + lib$;
        //      System.out.println("ExtensionHandler:loadIcon:jar="+jar$);
        ZipFile zf = new ZipFile(jar$);
        Enumeration<? extends ZipEntry> entries = zf.entries();
        ZipEntry ze;
        String[] sa;
        while (entries.hasMoreElements()) {
            try {
                ze = entries.nextElement();
                sa = ze.getName().split("/");
                //     System.out.println("ExtensionHandler:loadIcon:zip entry="+sa[sa.length-1]);
                if (resource$.equals(sa[sa.length - 1])) {
                    InputStream is = zf.getInputStream(ze);
                    if (is != null)
                        return is;

                }
            } catch (Exception e) {

            }
        }
        return null;
    } catch (Exception e) {
        Logger.getLogger(ExtensionHandler.class.getName()).severe(e.toString());
        return null;
    }

}

From source file:gdt.data.entity.facet.ExtensionHandler.java

public static String loadIcon(Entigrator entigrator, String extension$, String resource$) {
    try {/*from  w w  w  .java  2s .co m*/

        //System.out.println("ExtensionHandler:loadIcon:extension="+extension$+" handler="+handlerClass$);
        Sack extension = entigrator.getEntityAtKey(extension$);
        String lib$ = extension.getElementItemAt("field", "lib");
        String jar$ = entigrator.getEntihome() + "/" + extension$ + "/" + lib$;
        //      System.out.println("ExtensionHandler:loadIcon:jar="+jar$);
        ZipFile zf = new ZipFile(jar$);
        Enumeration<? extends ZipEntry> entries = zf.entries();
        ZipEntry ze;
        String[] sa;
        while (entries.hasMoreElements()) {
            try {
                ze = entries.nextElement();
                sa = ze.getName().split("/");
                //         System.out.println("ExtensionHandler:loadIcon:zip entry="+sa[sa.length-1]);
                if (resource$.equals(sa[sa.length - 1])) {
                    InputStream is = zf.getInputStream(ze);
                    //            System.out.println("ExtensionHandler:loadIcon:input stream="+is.toString());
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    byte[] b = new byte[1024];
                    int bytesRead = 0;
                    while ((bytesRead = is.read(b)) != -1) {
                        bos.write(b, 0, bytesRead);
                    }
                    byte[] ba = bos.toByteArray();
                    is.close();
                    return Base64.encodeBase64String(ba);
                }
            } catch (Exception e) {

            }
        }
        return null;
    } catch (Exception e) {
        Logger.getLogger(ExtensionHandler.class.getName()).severe(e.toString());
        return null;
    }

}

From source file:Main.java

public static void unzip(URL _url, URL _dest, boolean _remove_top) {
    int BUFFER_SZ = 2048;
    File file = new File(_url.getPath());

    String dest = _dest.getPath();
    new File(dest).mkdir();

    try {/*from  w ww.j  a va2  s  . c om*/
        ZipFile zip = new ZipFile(file);
        Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();

        // Process each entry
        while (zipFileEntries.hasMoreElements()) {
            // grab a zip file entry
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();

            if (_remove_top)
                currentEntry = currentEntry.substring(currentEntry.indexOf('/'), currentEntry.length());

            File destFile = new File(dest, currentEntry);
            //destFile = new File(newPath, destFile.getName());
            File destinationParent = destFile.getParentFile();

            // create the parent directory structure if needed
            destinationParent.mkdirs();

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

                // write the current file to disk
                FileOutputStream fos = new FileOutputStream(destFile);
                BufferedOutputStream dst = new BufferedOutputStream(fos, BUFFER_SZ);

                // read and write until last byte is encountered
                while ((currentByte = is.read(data, 0, BUFFER_SZ)) != -1) {
                    dst.write(data, 0, currentByte);
                }
                dst.flush();
                dst.close();
                is.close();
            }
            /*
            if (currentEntry.endsWith(".zip"))
            {
            // found a zip file, try to open
            extractFolder(destFile.getAbsolutePath());
            }*/
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.agnitas.util.ZipUtilities.java

/**
 * Readou of a Zip file//from   w  ww.j  ava 2  s.  c o m
 * @param zipFile
 * @return all file entries
 * @throws IOException
 */
public static Map<String, byte[]> readExistingZipFile(File zipFile) throws IOException {
    Map<String, byte[]> returnMap = new HashMap<String, byte[]>();

    ZipFile sourceZipFile = null;
    BufferedInputStream bufferedInputStream = null;
    ByteArrayOutputStream byteArrayOutputStream = null;

    try {
        sourceZipFile = new ZipFile(zipFile);

        // readout of all entries
        Enumeration<? extends ZipEntry> srcEntries = sourceZipFile.entries();
        while (srcEntries.hasMoreElements()) {
            ZipEntry sourceZipFileEntry = srcEntries.nextElement();

            bufferedInputStream = new BufferedInputStream(sourceZipFile.getInputStream(sourceZipFileEntry));
            byteArrayOutputStream = new ByteArrayOutputStream();

            byte[] bufferArray = new byte[1024];
            int byteBufferFillLength = bufferedInputStream.read(bufferArray);
            while (byteBufferFillLength > -1) {
                byteArrayOutputStream.write(bufferArray, 0, byteBufferFillLength);
                byteBufferFillLength = bufferedInputStream.read(bufferArray);
            }

            returnMap.put(sourceZipFileEntry.getName(), byteArrayOutputStream.toByteArray());

            byteArrayOutputStream.close();
            byteArrayOutputStream = null;

            bufferedInputStream.close();
            bufferedInputStream = null;
        }

        sourceZipFile.close();

        return returnMap;
    } finally {
        if (bufferedInputStream != null) {
            try {
                bufferedInputStream.close();
            } catch (Exception e) {
            }
            bufferedInputStream = null;
        }

        if (byteArrayOutputStream != null) {
            try {
                byteArrayOutputStream.close();
            } catch (Exception e) {
            }
            byteArrayOutputStream = null;
        }

        if (sourceZipFile != null) {
            try {
                sourceZipFile.close();
            } catch (Exception e) {
            }
            sourceZipFile = null;
        }
    }
}

From source file:com.pieframework.runtime.utils.Zipper.java

public static void unzip(String zipFile, String toDir) throws ZipException, IOException {

    int BUFFER = 2048;
    File file = new File(zipFile);

    ZipFile zip = new ZipFile(file);
    String newPath = toDir;// www. j a  va2  s .c  o  m

    File toDirectory = new File(newPath);
    if (!toDirectory.exists()) {
        toDirectory.mkdir();
    }
    Enumeration zipFileEntries = zip.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {
        // grab a zip file entry
        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();

        String currentEntry = entry.getName();
        File destFile = new File(newPath, FilenameUtils.separatorsToSystem(currentEntry));
        //System.out.println(currentEntry);
        File destinationParent = destFile.getParentFile();

        // create the parent directory structure if needed
        destinationParent.mkdirs();
        if (!entry.isDirectory()) {
            BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
            int currentByte;
            // establish buffer for writing file
            byte data[] = new byte[BUFFER];

            // write the current file to disk
            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

            // read and write until last byte is encountered
            while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
            dest.flush();
            dest.close();
            is.close();
        }
    }
    zip.close();

}

From source file:moskitt4me.repositoryClient.core.util.RepositoryClientUtil.java

public static void extractZipFile(String folder, String zipName) throws Exception {

    ZipFile zipFile = new ZipFile(new File(folder + "/" + zipName));

    Enumeration entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        Object obj = entries.nextElement();
        if (obj instanceof ZipEntry) {
            ZipEntry entry = (ZipEntry) obj;

            InputStream eis = zipFile.getInputStream(entry);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;

            File f = new File(folder + "/" + entry.getName());

            if (entry.isDirectory()) {
                f.mkdirs();//w  w w .  j a v a 2 s. com
                eis.close();
                continue;
            } else {
                f.getParentFile().mkdirs();
                f.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(f);

            while ((bytesRead = eis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }

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

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

    zipFile.close();
}

From source file:org.apache.synapse.libraries.util.LibDeployerUtils.java

private static void extract(String sourcePath, String destPath) throws IOException {
    Enumeration entries;//from  www .java2  s  .c o m
    ZipFile zipFile;

    zipFile = new ZipFile(sourcePath);
    entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        // we don't need to copy the META-INF dir
        if (entry.getName().startsWith("META-INF/")) {
            continue;
        }
        // if the entry is a directory, create a new dir
        if (entry.isDirectory()) {
            createDir(destPath + entry.getName());
            continue;
        }
        // if the entry is a file, write the file
        copyInputStream(zipFile.getInputStream(entry),
                new BufferedOutputStream(new FileOutputStream(destPath + entry.getName())));
    }
    zipFile.close();
}