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:it.tidalwave.northernwind.importer.infoglue.ExportConverter.java

protected void addLibraries(final @Nonnull String zippedLibraryPath, final @Nonnull ZonedDateTime dateTime)
        throws IOException {
    final @Cleanup ZipFile zipFile = new ZipFile(zippedLibraryPath);
    final Enumeration enumeration = zipFile.entries();

    while (enumeration.hasMoreElements()) {
        final ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();

        if (!zipEntry.isDirectory()) {
            //                System.out.println("Unzipping: " + zipEntry.getName());
            final @Cleanup InputStream is = new BufferedInputStream(zipFile.getInputStream(zipEntry));
            ResourceManager.addCommand(new AddResourceCommand(dateTime, zipEntry.getName(),
                    IOUtils.toByteArray(is), "Extracted from library"));
        }/*from ww w  . ja v a2s. c o  m*/
    }
}

From source file:m3.classe.M3ClassLoader.java

private DataInputStream getStreamFromJar(String name, File path) throws MAKException {
    if (!this.m_zipProblems.contains(path)) {
        try {/*from ww w . j  a va2 s.  c o  m*/
            DataInputStream dis = null;
            ZipFile file = new ZipFile(path);
            ZipEntry entry = file.getEntry(name.replace('.', '/') + '.' + "class");
            if (entry == null) {
                entry = file.getEntry(name.replace('.', '/') + '.' + "properties");
            }
            if (entry != null)
                ;
            return new DataInputStream(file.getInputStream(entry));
        } catch (IOException e) {
            Ressource.logger.warn("Failed to get " + name + " from jar " + path
                    + " - MAK will try again using another method", e);
            this.m_zipProblems.add(path);
        }
    }
    return getStreamFromJar2(name, path);
}

From source file:de.onyxbits.raccoon.appmgr.ExtractWorker.java

private void parseResourceTable() throws IOException {
    ZipFile zf = new ZipFile(source);
    ZipEntry entry = Utils.getEntry(zf, AndroidConstants.RESOURCE_FILE);
    if (entry == null) {
        // if no resource entry has been found, we assume it is not needed by this
        // APK//w w w.j av  a  2s .  c o  m
        this.resourceTable = new ResourceTable();
        return;
    }

    this.resourceTable = new ResourceTable();

    InputStream in = zf.getInputStream(entry);
    ByteBuffer buffer = ByteBuffer.wrap(Utils.toByteArray(in));
    ResourceTableParser resourceTableParser = new ResourceTableParser(buffer);
    resourceTableParser.parse();
    this.resourceTable = resourceTableParser.getResourceTable();
}

From source file:org.openmrs.module.clinicalsummary.io.UploadSummariesTask.java

/**
 * In order to correctly perform the encryption - decryption process, user must store their init vector table. This init vector will be given to the
 * user as a small file and it is required to perform the decryption process.
 *
 * @throws Exception//  ww  w .  j a  v a 2 s.c  om
 */
protected void processInitVector() throws Exception {
    String encryptedFilename = StringUtils.join(Arrays.asList(filename, TaskConstants.FILE_TYPE_ENCRYPTED),
            ".");
    ZipFile encryptedFile = new ZipFile(new File(TaskUtils.getEncryptedOutputPath(), encryptedFilename));

    Enumeration<? extends ZipEntry> entries = encryptedFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry zipEntry = entries.nextElement();
        if (zipEntry.getName().endsWith(TaskConstants.FILE_TYPE_SECRET)) {
            InputStream inputStream = encryptedFile.getInputStream(zipEntry);
            initVector = FileCopyUtils.copyToByteArray(inputStream);
            if (initVector.length != IV_SIZE) {
                throw new Exception("Secret file is corrupted or invalid secret file are being used.");
            }
        }
    }
}

From source file:org.eclipse.koneki.ldt.core.internal.buildpath.LuaExecutionEnvironmentManager.java

public static LuaExecutionEnvironment getExecutionEnvironmentFromCompressedFile(final String filePath)
        throws CoreException {
    /*//from   w  w w .j  a  v a 2  s.c o  m
     * Extract manifest file
     */
    ZipFile zipFile = null;
    String manifestString = null;
    try {
        zipFile = new ZipFile(filePath);
        final Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
        while (zipEntries.hasMoreElements()) {
            final ZipEntry zipEntry = zipEntries.nextElement();
            if ((!zipEntry.getName().contains("/")) //$NON-NLS-1$
                    && zipEntry.getName().endsWith(LuaExecutionEnvironmentConstants.MANIFEST_EXTENSION)) {

                // check there are only one manifest.
                if (manifestString != null) {
                    throwException(
                            MessageFormat.format("Invalid Execution Environment : more than one \"{0}\" file.", //$NON-NLS-1$
                                    LuaExecutionEnvironmentConstants.MANIFEST_EXTENSION),
                            null, IStatus.ERROR);
                }

                // read manifest
                final InputStream input = zipFile.getInputStream(zipEntry);
                manifestString = IOUtils.toString(input);
            }
        }
    } catch (IOException e) {
        throwException(MessageFormat.format("Unable to extract manifest from zip file {0}", filePath), e, //$NON-NLS-1$
                IStatus.ERROR);
    } finally {
        if (zipFile != null)
            try {
                zipFile.close();
            } catch (IOException e) {
                Activator.logWarning(MessageFormat.format("Unable to close zip file {0}", filePath), e); //$NON-NLS-1$
            }
    }

    // if no manifest extract
    if (manifestString == null) {
        throwException(MessageFormat.format("No manifest \"{0}\" file found", //$NON-NLS-1$
                LuaExecutionEnvironmentConstants.MANIFEST_EXTENSION), null, IStatus.ERROR);
    }

    return getLuaExecutionEnvironmentFromManifest(manifestString, null);
}

From source file:it.readbeyond.minstrel.librarian.FormatHandlerEPUB.java

private String getOPFPath(File f) {
    String ret = null;//www  . j  a va2  s . c o  m
    try {
        ZipFile zipFile = new ZipFile(f, ZipFile.OPEN_READ);
        ZipEntry containerEntry = zipFile.getEntry(EPUB_CONTAINER_PATH);
        if (containerEntry != null) {
            InputStream is = zipFile.getInputStream(containerEntry);
            parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
            parser.setInput(is, null);
            parser.nextTag();
            ret = parseContainer();
            is.close();
        }
        zipFile.close();
    } catch (Exception e) {
        // nop
    }
    return ret;
}

From source file:com.magnet.tools.tests.ScenarioUtils.java

/**
 * Utility method to unzip a file/*  ww  w  .  ja  v a2 s  .com*/
 *
 * @param file      file to unzip
 * @param outputDir destination directory
 * @throws IOException if an io exception occurs
 */
public static void unzip(File file, File outputDir) throws IOException {
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(file);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            File entryDestination = new File(outputDir, entry.getName());
            if (entry.isDirectory()) {
                if (!entryDestination.mkdirs()) {
                    throw new IllegalStateException("Cannot create directory " + entryDestination);
                }
                continue;
            }
            if (entryDestination.getParentFile().mkdirs()) {
                throw new IllegalStateException("Cannot create directory " + entryDestination.getParentFile());
            }
            InputStream in = zipFile.getInputStream(entry);
            OutputStream out = new FileOutputStream(entryDestination);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
    } finally {
        if (null != zipFile) {
            try {
                zipFile.close();
            } catch (Exception e) {
                /* do nothing */
            }
        }
    }
}

From source file:com.google.appinventor.buildserver.util.AARLibrary.java

/**
 * Extracts the package name from the Android Archive without needing to unzip it to a location
 * in the file system/*from  ww  w.  j  a  v a  2s.  c  o  m*/
 *
 * @param zip the input stream reading from the Android Archive.
 * @return the package name declared in the archive's AndroidManifest.xml.
 * @throws IOException if reading the input stream fails.
 */
private String extractPackageName(ZipFile zip) throws IOException {
    ZipEntry entry = zip.getEntry("AndroidManifest.xml");
    if (entry == null) {
        throw new IllegalArgumentException(zip.getName() + " does not contain AndroidManifest.xml");
    }
    try {
        ZipEntryWrapper wrapper = new ZipEntryWrapper(zip.getInputStream(entry));
        // the following call will automatically close the input stream opened above
        return AndroidManifest.getPackage(wrapper);
    } catch (StreamException | XPathExpressionException e) {
        throw new IOException("Exception processing AndroidManifest.xml", e);
    }
}

From source file:dk.deck.resolver.util.ZipUtils.java

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

    if (entry.isDirectory()) {
        createDir(new File(outputDir, entry.getName()));
        return;/*w ww . ja va  2s  . c om*/
    }

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

    // log.debug("Extracting: " + entry);
    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:com.l2jfree.gameserver.util.JarClassLoader.java

private byte[] loadClassData(String name) throws IOException {
    byte[] classData = null;
    for (String jarFile : _jars) {
        ZipFile zipFile = null;
        DataInputStream zipStream = null;

        try {//from w  w w  .ja  va2  s .co  m
            File file = new File(jarFile);
            zipFile = new ZipFile(file);
            String fileName = name.replace('.', '/') + ".class";
            ZipEntry entry = zipFile.getEntry(fileName);
            if (entry == null)
                continue;
            classData = new byte[(int) entry.getSize()];
            zipStream = new DataInputStream(zipFile.getInputStream(entry));
            zipStream.readFully(classData, 0, (int) entry.getSize());
            break;
        } catch (IOException e) {
            _log.warn(jarFile + ":", e);
            continue;
        } finally {
            try {
                if (zipFile != null)
                    zipFile.close();
            } catch (Exception e) {
                _log.warn("", e);
            }
            try {
                if (zipStream != null)
                    zipStream.close();
            } catch (Exception e) {
                _log.warn("", e);
            }
        }
    }
    if (classData == null)
        throw new IOException("class not found in " + _jars);
    return classData;
}