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.mc.printer.model.utils.ZipHelper.java

public static void unZip(String sourceZip, String outDirName) throws IOException {
    log.info("unzip source:" + sourceZip);
    log.info("unzip to :" + outDirName);
    ZipFile zfile = new ZipFile(sourceZip);
    System.out.println(zfile.getName());
    Enumeration zList = zfile.entries();
    ZipEntry ze = null;//from   w  ww .jav a 2  s .  c  o m
    byte[] buf = new byte[1024];
    while (zList.hasMoreElements()) {
        //ZipFileZipEntry
        ze = (ZipEntry) zList.nextElement();
        if (ze.isDirectory()) {
            continue;
        }
        //ZipEntry?InputStreamOutputStream
        File fil = getRealFileName(outDirName, ze.getName());

        OutputStream os = new BufferedOutputStream(new FileOutputStream(fil));
        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();
        log.debug("Extracted: " + ze.getName());
    }
    zfile.close();

}

From source file:com.openAtlas.bundleInfo.maker.PackageLite.java

public static PackageLite parse(String apkPath) {
    ZipFile file = null;

    StringBuilder xmlSb = new StringBuilder(100);
    try {/*from  w ww  .jav a  2 s  . co  m*/
        File apkFile = new File(apkPath);
        file = new ZipFile(apkFile, ZipFile.OPEN_READ);
        ZipEntry entry = file.getEntry(DEFAULT_XML);

        AXmlResourceParser parser = new AXmlResourceParser();
        parser.open(file.getInputStream(entry));
        PackageLite packageLite = new PackageLite();
        packageLite.apkMD5 = FileUtils.getMD5(apkPath);
        packageLite.size = apkFile.length();
        packageLite.checkNativeLibs(file);
        packageLite.parse(parser);
        //System.err.println(packageLite.getBundleInfo().toString());
        file.close();
        return packageLite;
        //parser.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.acdd.ext.bundleInfo.maker.PackageLite.java

public static PackageLite parse(String apkPath) {
    ZipFile file = null;

    StringBuilder xmlSb = new StringBuilder(100);
    try {//from  ww  w . j  a v  a  2  s .  c  o  m
        File apkFile = new File(apkPath);
        file = new ZipFile(apkFile, ZipFile.OPEN_READ);
        ZipEntry entry = file.getEntry(DEFAULT_XML);

        AXmlResourceParser parser = new AXmlResourceParser();
        parser.open(file.getInputStream(entry));
        PackageLite packageLite = new PackageLite();
        packageLite.apkMD5 = ACDDFileUtils.getMD5(apkPath);
        packageLite.size = apkFile.length();
        packageLite.checkNativeLibs(file);
        packageLite.parse(parser);
        //System.err.println(packageLite.getBundleInfo().toString());
        file.close();
        return packageLite;
        //parser.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:net.sourceforge.floggy.maven.ZipUtils.java

/**
 * DOCUMENT ME!/*w  ww  .ja  va  2  s.  c o  m*/
*
* @param file DOCUMENT ME!
* @param directory DOCUMENT ME!
*
* @throws IOException DOCUMENT ME!
*/
public static void unzip(File file, File directory) throws IOException {
    ZipFile zipFile = new ZipFile(file);
    Enumeration entries = zipFile.entries();

    if (!directory.exists() && !directory.mkdirs()) {
        throw new IOException("Unable to create the " + directory + " directory!");
    }

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

        if (entry.isDirectory()) {
            temp = new File(directory, entry.getName());

            if (!temp.exists() && !temp.mkdirs()) {
                throw new IOException("Unable to create the " + temp + " directory!");
            }
        } else {
            temp = new File(directory, entry.getName());
            IOUtils.copy(zipFile.getInputStream(entry), new FileOutputStream(temp));
        }
    }

    zipFile.close();
}

From source file:com.simiacryptus.mindseye.lang.Layer.java

/**
 * From zip nn key./*w ww .  j ava  2 s .  com*/
 *
 * @param zipfile the zipfile
 * @return the nn key
 */
@Nonnull
static Layer fromZip(@Nonnull final ZipFile zipfile) {
    Enumeration<? extends ZipEntry> entries = zipfile.entries();
    @Nullable
    JsonObject json = null;
    @Nonnull
    HashMap<CharSequence, byte[]> resources = new HashMap<>();
    while (entries.hasMoreElements()) {
        ZipEntry zipEntry = entries.nextElement();
        CharSequence name = zipEntry.getName();
        try {
            InputStream inputStream = zipfile.getInputStream(zipEntry);
            if (name.equals("model.json")) {
                json = new GsonBuilder().create().fromJson(new InputStreamReader(inputStream),
                        JsonObject.class);
            } else {
                resources.put(name, IOUtils.readFully(inputStream, (int) zipEntry.getSize()));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return fromJson(json, resources);
}

From source file:de.rub.syssec.saaf.analysis.steps.extract.ApkUnzipper.java

/**
 * Extracts the given archive into the given destination directory
 * //from  w  ww .  j a v  a 2  s  . c  o  m
 * @param archive
 *            - the file to extract
 * @param dest
 *            - the destination directory
 * @throws Exception
 */
public static void extractArchive(File archive, File destDir) throws IOException {

    if (!destDir.exists()) {
        destDir.mkdir();
    }

    ZipFile zipFile = new ZipFile(archive);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    byte[] buffer = new byte[16384];
    int len;
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        String entryFileName = entry.getName();

        File dir = buildDirectoryHierarchyFor(entryFileName, destDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        if (!entry.isDirectory()) {

            File file = new File(destDir, entryFileName);

            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));

            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

            while ((len = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, len);
            }

            bos.flush();
            bos.close();
            bis.close();
        }
    }
    zipFile.close();
}

From source file:com.vmware.admiral.closures.util.ClosureUtils.java

private static void buildTarData(URL dirURL, String folderNameFilter, OutputStream outputStream)
        throws IOException {
    final JarURLConnection jarConnection = (JarURLConnection) dirURL.openConnection();
    final ZipFile jar = jarConnection.getJarFile();
    final Enumeration<? extends ZipEntry> entries = jar.entries();

    try (TarArchiveOutputStream tarArchiveOutputStream = buildTarStream(outputStream)) {
        while (entries.hasMoreElements()) {
            final ZipEntry entry = entries.nextElement();
            final String name = entry.getName();
            if (!name.startsWith(folderNameFilter)) {
                // entry in wrong subdir -- don't copy
                continue;
            }//from ww  w .  j  a  v a2s  .c  o  m
            TarArchiveEntry tarEntry = new TarArchiveEntry(entry.getName().replaceAll(folderNameFilter, ""));
            try (InputStream is = jar.getInputStream(entry)) {
                putTarEntry(tarArchiveOutputStream, tarEntry, is, entry.getSize());
            }
        }

        tarArchiveOutputStream.flush();
        tarArchiveOutputStream.close();
    }
}

From source file:dk.netarkivet.common.utils.ZipUtils.java

/** Unzip a zipFile into a directory.  This will create subdirectories
 * as needed./*from ww  w .  ja va2  s .c om*/
 *
 * @param zipFile The file to unzip
 * @param toDir The directory to create the files under.  This directory
 * will be created if necessary.  Files in it will be overwritten if the
 * filenames match.
 */
public static void unzip(File zipFile, File toDir) {
    ArgumentNotValid.checkNotNull(zipFile, "File zipFile");
    ArgumentNotValid.checkNotNull(toDir, "File toDir");
    ArgumentNotValid.checkTrue(toDir.getAbsoluteFile().getParentFile().canWrite(),
            "can't write to '" + toDir + "'");
    ArgumentNotValid.checkTrue(zipFile.canRead(), "can't read '" + zipFile + "'");
    InputStream inputStream = null;
    ZipFile unzipper = null;
    try {
        try {
            unzipper = new ZipFile(zipFile);
            Enumeration<? extends ZipEntry> entries = unzipper.entries();
            while (entries.hasMoreElements()) {
                ZipEntry ze = entries.nextElement();
                File target = new File(toDir, ze.getName());
                // Ensure that its dir exists
                FileUtils.createDir(target.getCanonicalFile().getParentFile());
                if (ze.isDirectory()) {
                    target.mkdir();
                } else {
                    inputStream = unzipper.getInputStream(ze);
                    FileUtils.writeStreamToFile(inputStream, target);
                    inputStream.close();
                }
            }
        } finally {
            if (unzipper != null) {
                unzipper.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        }
    } catch (IOException e) {
        throw new IOFailure("Failed to unzip '" + zipFile + "'", e);
    }
}

From source file:com.stevpet.sonar.plugins.dotnet.mscover.codecoverage.command.ZipUtils.java

/**
 * Extracts the specified folder from the specified archive, into the supplied output directory.
 * // ww w . ja v  a2  s.  c o m
 * @param archivePath
 *          the archive Path
 * @param folderToExtract
 *          the folder to extract
 * @param outputDirectory
 *          the output directory
 * @return the extracted folder path
 * @throws IOException
 *           if a problem occurs while extracting
 */
public static File extractArchiveFolderIntoDirectory(String archivePath, String folderToExtract,
        String outputDirectory) throws IOException {
    File destinationFolder = new File(outputDirectory);
    destinationFolder.mkdirs();

    ZipFile zip = null;
    try {
        zip = new ZipFile(new File(archivePath));
        Enumeration<?> zipFileEntries = zip.entries();
        // Process each entry
        while (zipFileEntries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();
            if (currentEntry.startsWith(folderToExtract)) {
                File destFile = new File(destinationFolder, currentEntry);
                destFile.getParentFile().mkdirs();
                if (!entry.isDirectory()) {
                    BufferedInputStream is = null;
                    BufferedOutputStream dest = null;
                    try {
                        is = new BufferedInputStream(zip.getInputStream(entry));
                        int currentByte;
                        // establish buffer for writing file
                        byte data[] = new byte[BUFFER_SIZE];

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

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

    return new File(destinationFolder, folderToExtract);
}

From source file:org.eclipse.thym.core.internal.util.FileUtils.java

private static void createFileFromZipFile(File file, ZipFile zipFile, ZipEntry zipEntry) throws IOException {
    if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) {
        throw new IOException("Can not create parent directory for " + file.toString());
    }//  w  w w  .  j a v  a  2s  . co m
    file.createNewFile();
    FileOutputStream fout = null;
    FileChannel out = null;
    InputStream in = null;
    try {
        fout = new FileOutputStream(file);
        out = fout.getChannel();
        in = zipFile.getInputStream(zipEntry);
        out.transferFrom(Channels.newChannel(in), 0, Integer.MAX_VALUE);
    } finally {
        if (out != null)
            out.close();
        if (in != null)
            in.close();
        if (fout != null)
            fout.close();
    }
}