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:org.fao.geonet.api.mapservers.GeoFile.java

/**
 * Returns the names of the vector layers (Shapefiles) in the geographic file.
 *
 * @param onlyOneFileAllowed Return exception if more than one shapefile found
 * @return a collection of layer names//from  w  ww  .  j  av a  2s  .c o  m
 * @throws IllegalArgumentException If more than on shapefile is found and onlyOneFileAllowed is
 *                                  true or if Shapefile name is not equal to zip file base
 *                                  name
 */
public Collection<String> getVectorLayers(final boolean onlyOneFileAllowed) throws IOException {
    final LinkedList<String> layers = new LinkedList<String>();
    if (zipFile != null) {
        for (Path path : zipFile.getRootDirectories()) {
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    String fileName = file.getFileName().toString();
                    if (fileIsShp(fileName)) {
                        String base = getBase(fileName);

                        if (onlyOneFileAllowed) {
                            if (layers.size() > 1)
                                throw new IllegalArgumentException("Only one shapefile per zip is allowed. "
                                        + layers.size() + " shapefiles found.");

                            if (base.equals(getBase(fileName))) {
                                layers.add(base);
                            } else
                                throw new IllegalArgumentException("Shapefile name (" + base
                                        + ") is not equal to ZIP file name (" + file.getFileName() + ").");
                        } else {
                            layers.add(base);
                        }
                    }
                    if (fileIsSld(fileName)) {
                        _containsSld = true;
                        _sldBody = fileName;
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        }
        if (_containsSld) {
            ZipFile zf = new ZipFile(new File(this.file.toString()));
            InputStream is = zf.getInputStream(zf.getEntry(_sldBody));
            BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
            String line;
            _sldBody = "";
            while ((line = br.readLine()) != null) {
                _sldBody += line;
            }
            br.close();
            is.close();
            zf.close();
        }
    }

    return layers;
}

From source file:com.servoy.extension.install.CopyZipEntryImporter.java

protected void handleZipEntry(File outputFile, ZipFile zipFile, ZipEntry entry) throws IOException {
    copyFile(outputFile, new BufferedInputStream(zipFile.getInputStream(entry)), false);
}

From source file:com.ephesoft.dcma.util.FileUtils.java

/**
 * To get Input Stream from Zip.//from  w w w . ja va  2s .com
 * 
 * @param zipName {@link String}
 * @param fileName {@link String}
 * @return {@link InputStream}
 * @throws FileNotFoundException in case of error
 * @throws IOException in case of error
 */
public static InputStream getInputStreamFromZip(final String zipName, final String fileName)
        throws FileNotFoundException, IOException {
    ZipFile zipFile = new ZipFile(zipName + ZIP_FILE_EXT);
    // InputStream inputStream = zipFile.getInputStream(zipFile.getEntry(fileName));
    return zipFile.getInputStream(zipFile.getEntry(fileName));
}

From source file:no.uio.medicine.virsurveillance.parsers.CSVsGBDdata.java

public void zip() throws IOException {
    ZipFile zipFile = new ZipFile("C:/test.zip");

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

    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        InputStream stream = zipFile.getInputStream(entry);
    }/*from w  w  w  . j  av  a 2 s .  co  m*/

}

From source file:com.blackducksoftware.integration.fortify.BlackDuckIssueParser.java

@Override
public Scan parseAnalysisInformation(ZipFile zipFile, ZipEntry zipEntry) throws IOException {
    InputStream inputStream = zipFile.getInputStream(zipEntry);
    String entryName = zipEntry.getName();
    return parseAnalysisInformation(inputStream, entryName);
}

From source file:com.github.promeg.configchecker.Main.java

File openDexFile(ZipFile zipFile, ZipEntry entry) throws IOException {
    // We know it's a zip; see if there's anything useful inside.  A
    // failure here results in some type of IOException (of which
    // ZipException is a subclass).
    InputStream zis = zipFile.getInputStream(entry);

    // Create a temp file to hold the DEX data, open it, and delete it
    // to ensure it doesn't hang around if we fail.
    File dexFile = File.createTempFile("dexdeps", ".dex");

    // Copy all data from input stream to output file.
    IOUtils.copy(zis, new FileOutputStream(dexFile));

    return dexFile;
}

From source file:org.apache.rave.provider.w3c.service.impl.WookieWidgetService.java

private Widget returnURLFromConfig(File wgtFile) {
    Widget widget = null;/*  w  w  w. j  av  a  2 s. c  o m*/
    try {
        final ZipFile zipFile = new ZipFile(wgtFile);
        ZipEntry entry = zipFile.getEntry("config.xml");
        InputStream input = zipFile.getInputStream(entry);
        BufferedReader br = new BufferedReader(new InputStreamReader(input, "UTF-8"));
        try {
            String line = null;
            while ((line = br.readLine()) != null) {
                if (line.contains("id=")) {
                    String val = line.substring(line.indexOf("id=") + 4,
                            line.indexOf("\"", line.indexOf("id=") + 5));
                    widget = new W3CWidget();
                    widget.setUrl(val);
                    return widget;
                }
            }
        } finally {
            input.close();
        }
    } catch (Exception e) {
        logger.error(e.getMessage());
    }
    return widget;
}

From source file:com.l2jfree.gameserver.script.ScriptPackage.java

/**
 * @param scriptFiles The scriptFiles to set.
 *///from  w ww  .j a  v a 2s.  co  m
private void addFiles(ZipFile pack) {
    for (Enumeration<? extends ZipEntry> e = pack.entries(); e.hasMoreElements();) {
        ZipEntry entry = e.nextElement();
        if (entry.getName().endsWith(".xml")) {
            try {
                ScriptDocument newScript = new ScriptDocument(entry.getName(), pack.getInputStream(entry));
                _scriptFiles.add(newScript);
            } catch (IOException e1) {
                _log.error(e1.getMessage(), e1);
            }
        } else if (!entry.isDirectory()) {
            // ignore it
        }
    }
}

From source file:io.github.bonigarcia.wdm.Downloader.java

public static final File extract(File compressedFile, String export) throws IOException {
    log.trace("Compressed file {}", compressedFile);

    File file = null;//w  ww  . ja v  a  2s.  c  om
    if (compressedFile.getName().toLowerCase().endsWith("tar.bz2")) {
        file = unBZip2(compressedFile);
    } else if (compressedFile.getName().toLowerCase().endsWith("tar.gz")) {
        file = unTarGz(compressedFile);
    } else if (compressedFile.getName().toLowerCase().endsWith("gz")) {
        file = unGzip(compressedFile);
    } else {
        ZipFile zipFolder = new ZipFile(compressedFile);
        Enumeration<?> enu = zipFolder.entries();

        while (enu.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) enu.nextElement();

            String name = zipEntry.getName();
            long size = zipEntry.getSize();
            long compressedSize = zipEntry.getCompressedSize();
            log.trace("Unzipping {} (size: {} KB, compressed size: {} KB)", name, size, compressedSize);

            file = new File(compressedFile.getParentFile() + File.separator + name);
            if (!file.exists() || WdmConfig.getBoolean("wdm.override")) {
                if (name.endsWith("/")) {
                    file.mkdirs();
                    continue;
                }

                File parent = file.getParentFile();
                if (parent != null) {
                    parent.mkdirs();
                }

                InputStream is = zipFolder.getInputStream(zipEntry);
                FileOutputStream fos = new FileOutputStream(file);
                byte[] bytes = new byte[1024];
                int length;
                while ((length = is.read(bytes)) >= 0) {
                    fos.write(bytes, 0, length);
                }
                is.close();
                fos.close();
                file.setExecutable(true);
            } else {
                log.debug(file + " already exists");
            }

        }
        zipFolder.close();
    }

    file = checkPhantom(compressedFile, export);

    log.trace("Resulting binary file {}", file.getAbsoluteFile());
    return file.getAbsoluteFile();
}

From source file:com.gelakinetic.mtgfam.helpers.ZipUtils.java

/**
 * Unzip a file directly into getFilesDir()
 *
 * @param zipFile The zip file to unzip/* w w  w .ja v  a  2  s. c  om*/
 * @param context The application context, for getting files and the like
 * @throws IOException Thrown if something goes wrong with unzipping and writing
 */
private static void unZipIt(ZipFile zipFile, Context context) throws IOException {
    Enumeration<? extends ZipEntry> entries;

    entries = zipFile.entries();

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

        if (entry.isDirectory()) {
            /* Assume directories are stored parents first then children.
             * This is not robust, just for demonstration purposes. */
            if (!(new File(entry.getName())).mkdir()) {
                return;
            }
            continue;
        }
        String[] path = entry.getName().split("/");
        String pathCat = "";
        if (path.length > 1) {
            for (int i = 0; i < path.length - 1; i++) {
                pathCat += path[i] + "/";
                File tmp = new File(context.getFilesDir(), pathCat);
                if (!tmp.exists()) {
                    if (!tmp.mkdir()) {
                        return;
                    }
                }
            }
        }

        InputStream in = zipFile.getInputStream(entry);
        OutputStream out;
        if (entry.getName().contains("_preferences.xml")) {
            String sharedPrefsDir = context.getFilesDir().getPath();
            sharedPrefsDir = sharedPrefsDir.substring(0, sharedPrefsDir.lastIndexOf("/")) + "/shared_prefs/";

            out = new BufferedOutputStream(new FileOutputStream(new File(sharedPrefsDir, entry.getName())));
        } else {
            out = new BufferedOutputStream(
                    new FileOutputStream(new File(context.getFilesDir(), entry.getName())));
        }
        byte[] buffer = new byte[1024];
        int len;
        while ((len = in.read(buffer)) >= 0) {
            out.write(buffer, 0, len);
        }

        in.close();
        out.close();
    }

    zipFile.close();
}