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.google.gdt.eclipse.designer.webkit.WebKitSupportWin32.java

private static void extract(ZipFile zipFile) throws Exception {
    // remove any possibly corrupted contents
    FileUtils.deleteQuietly(WEBKIT_DIR);
    WEBKIT_DIR.mkdirs();//from ww  w. ja v  a2s .c o  m
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (entry.isDirectory()) {
            new File(WEBKIT_DIR, entry.getName()).mkdirs();
            continue;
        }
        InputStream inputStream = zipFile.getInputStream(entry);
        File outputFile = new File(WEBKIT_DIR, entry.getName());
        FileOutputStream outputStream = new FileOutputStream(outputFile);
        IOUtils.copy(inputStream, outputStream);
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(outputStream);
    }
}

From source file:com.icesoft.icefaces.tutorial.component.autocomplete.AutoCompleteDictionary.java

private static void init() {
    // Raw list of xml cities.
    List cityList = null;/*  w ww  . j  av  a2s .  c  om*/

    // load the city dictionary from the compressed xml file.

    // get the path of the compressed file
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    String basePath = session.getServletContext().getRealPath("/WEB-INF/resources");
    basePath += "/city.xml.zip";

    // extract the file
    ZipEntry zipEntry;
    ZipFile zipFile;
    try {
        zipFile = new ZipFile(basePath);
        zipEntry = zipFile.getEntry("city.xml");
    } catch (Exception e) {
        log.error("Error retrieving records", e);
        return;
    }

    // get the xml stream and decode it.
    if (zipFile.size() > 0 && zipEntry != null) {
        try {
            BufferedInputStream dictionaryStream = new BufferedInputStream(zipFile.getInputStream(zipEntry));
            XMLDecoder xDecoder = new XMLDecoder(dictionaryStream);
            // get the city list.
            cityList = (List) xDecoder.readObject();
            dictionaryStream.close();
            zipFile.close();
            xDecoder.close();
        } catch (ArrayIndexOutOfBoundsException e) {
            log.error("Error getting city list, not city objects", e);
            return;
        } catch (IOException e) {
            log.error("Error getting city list", e);
            return;
        }
    }

    // Finally load the object from the xml file.
    if (cityList != null) {
        dictionary = new ArrayList(cityList.size());
        City tmpCity;
        for (int i = 0, max = cityList.size(); i < max; i++) {
            tmpCity = (City) cityList.get(i);
            if (tmpCity != null && tmpCity.getCity() != null) {
                dictionary.add(new SelectItem(tmpCity, tmpCity.getCity()));
            }
        }
        cityList.clear();
        // finally sort the list
        Collections.sort(dictionary, LABEL_COMPARATOR);
    }

}

From source file:com.dreikraft.axbo.sound.SoundPackageUtil.java

/**
 * retrieves an entry from the package file (ZIP file)
 *
 * @param packageFile the sound package file
 * @param entryName the name of the entry in the ZIP file
 * @throws com.dreikraft.infactory.sound.SoundPackageException encapsulates
 * all low level (IO) exceptions//from   ww  w . ja v  a  2  s .com
 * @return the entry data as stream
 */
public static InputStream getPackageEntryStream(File packageFile, String entryName)
        throws SoundPackageException {
    if (packageFile == null) {
        throw new SoundPackageException(new IllegalArgumentException("missing package file"));
    }

    InputStream keyIn = null;
    try {
        ZipFile packageZip = new ZipFile(packageFile);

        // get key from package
        ZipEntry keyEntry = packageZip.getEntry(LICENSE_KEY_ENTRY);
        keyIn = packageZip.getInputStream(keyEntry);
        Key key = CryptoUtil.unwrapKey(keyIn, PUBLIC_KEY_FILE);

        // read entry
        ZipEntry entry = packageZip.getEntry(entryName);
        return new ZipClosingInputStream(packageZip,
                CryptoUtil.decryptInput(packageZip.getInputStream(entry), key));
    } catch (ZipException ex) {
        throw new SoundPackageException(ex);
    } catch (IOException ex) {
        throw new SoundPackageException(ex);
    } catch (CryptoException ex) {
        throw new SoundPackageException(ex);
    } finally {
        try {
            if (keyIn != null) {
                keyIn.close();
            }
        } catch (IOException ex) {
            log.error(ex.getMessage(), ex);
        }
    }
}

From source file:org.cloudfoundry.client.lib.SampleProjects.java

private static void unpackZip(ZipFile zipFile, File unpackDir) throws IOException {
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        File destination = new File(unpackDir.getAbsolutePath() + "/" + entry.getName());
        if (entry.isDirectory()) {
            destination.mkdirs();/* w w  w.  j  a  v  a  2s.  c  om*/
        } else {
            destination.getParentFile().mkdirs();
            FileCopyUtils.copy(zipFile.getInputStream(entry), new FileOutputStream(destination));
        }
        if (entry.getTime() != -1) {
            destination.setLastModified(entry.getTime());
        }
    }
}

From source file:nz.co.fortytwo.freeboard.installer.ZipUtils.java

/**
 * Unzip a zipFile into a directory//  www.jav  a  2 s.com
 * @param targetDir
 * @param zipFile
 * @throws ZipException
 * @throws IOException
 */
public static void unzip(File targetDir, File zipFile) throws ZipException, IOException {
    ZipFile zip = new ZipFile(zipFile);

    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> z = (Enumeration<ZipEntry>) zip.entries();
    while (z.hasMoreElements()) {
        ZipEntry entry = z.nextElement();
        File f = new File(targetDir, entry.getName());
        if (f.isDirectory()) {
            if (!f.exists()) {
                f.mkdirs();
            }
        } else {
            f.getParentFile().mkdirs();
            InputStream in = zip.getInputStream(entry);
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));
            IOUtils.copy(in, out);
            in.close();
            out.flush();
            out.close();
        }

    }
    zip.close();
}

From source file:com.compomics.pladipus.core.control.util.ZipUtils.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  ww . ja v  a 2  s. co m
    }

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

    LOGGER.debug("Extracting: " + entry);

    try (BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile))) {
        IOUtils.copy(inputStream, outputStream);
        outputStream.flush();
    }
}

From source file:org.paxml.bean.UnzipTag.java

public static void unzip(File file, File dir) {
    dir.mkdirs();//w  ww  .ja v  a2 s.  c om
    ZipFile zipFile = null;

    try {
        zipFile = new ZipFile(file);

        Enumeration entries = zipFile.entries();

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

            if (entry.isDirectory()) {

                new File(dir, entry.getName()).mkdirs();
                continue;
            }

            InputStream in = null;
            OutputStream out = null;
            try {
                zipFile.getInputStream(entry);
                out = new BufferedOutputStream(new FileOutputStream(entry.getName()));
                IOUtils.copy(in, out);
            } finally {
                IOUtils.closeQuietly(in);
                IOUtils.closeQuietly(out);
            }
        }

    } catch (IOException ioe) {

        throw new PaxmlRuntimeException(
                "Cannot unzip file: " + file.getAbsolutePath() + " under dir: " + dir.getAbsolutePath(), ioe);

    } finally {

        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
                // do nothing
            }
        }
    }
}

From source file:Main.java

public static boolean upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
    ZipFile zfile = new ZipFile(zipFile);
    Enumeration<? extends ZipEntry> zList = zfile.entries();
    ZipEntry ze = null;/*from  w w w.  ja  v a 2  s.  c  o  m*/
    byte[] buf = new byte[1024];
    while (zList.hasMoreElements()) {
        ze = (ZipEntry) zList.nextElement();
        if (ze.isDirectory()) {
            continue;
        }
        Log.d(TAG, "ze.getName() = " + ze.getName());
        OutputStream os = new BufferedOutputStream(
                new FileOutputStream(getRealFileName(folderPath, ze.getName())));
        InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
        int readLen = 0;
        while ((readLen = is.read(buf, 0, 1024)) != -1) {
            os.write(buf, 0, readLen);
        }
        is.close();
        os.close();
    }
    zfile.close();
    return true;
}

From source file:android.databinding.tool.util.GenerationalClassUtil.java

private static void loadFomZipFile(File file) throws IOException {
    ZipFile zipFile = new ZipFile(file);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        for (ExtensionFilter filter : ExtensionFilter.values()) {
            if (!filter.accept(entry.getName())) {
                continue;
            }//from   w  ww  . j a v  a  2  s .  c om
            InputStream inputStream = null;
            try {
                inputStream = zipFile.getInputStream(entry);
                Serializable item = fromInputStream(inputStream);
                L.d("loaded item %s from zip file", item);
                if (item != null) {
                    //noinspection unchecked
                    sCache[filter.ordinal()].add(item);
                }
            } catch (IOException e) {
                L.e(e, "Could not merge in Bindables from %s", file.getAbsolutePath());
            } catch (ClassNotFoundException e) {
                L.e(e, "Could not read Binding properties intermediate file. %s", file.getAbsolutePath());
            } finally {
                IOUtils.closeQuietly(inputStream);
            }
        }
    }
}

From source file:com.googlecode.dex2jar.test.TestUtils.java

public static void checkZipFile(File zip) throws ZipException, Exception {
    ZipFile zipFile = new ZipFile(zip);
    for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements();) {
        ZipEntry entry = e.nextElement();
        if (entry.getName().endsWith(".class")) {
            StringWriter sw = new StringWriter();
            // PrintWriter pw = new PrintWriter(sw);
            InputStream is = zipFile.getInputStream(entry);
            try {
                verify(new ClassReader(IOUtils.toByteArray(is)));
            } finally {
                IOUtils.closeQuietly(is);
            }/*  ww w .  j a v a  2 s. c o m*/
            Assert.assertTrue(sw.toString(), sw.toString().length() == 0);
        }
    }
}