Example usage for java.util.zip ZipInputStream available

List of usage examples for java.util.zip ZipInputStream available

Introduction

In this page you can find the example usage for java.util.zip ZipInputStream available.

Prototype

public int available() throws IOException 

Source Link

Document

Returns 0 after EOF has reached for the current entry data, otherwise always return 1.

Usage

From source file:org.jetbrains.kotlin.jvm.compiler.longTest.ResolveDescriptorsFromExternalLibraries.java

private static boolean testLibraryFile(@Nullable File jar, @NotNull String libDescription, int classesPerChunk)
        throws IOException {
    System.out.println("Testing library " + libDescription + "...");
    if (jar != null) {
        System.out.println("Using file " + jar);
    } else {/*from   w  w w  . ja v a 2  s.c om*/
        jar = findRtJar();
        System.out.println("Using rt.jar: " + jar);
    }

    long start = System.currentTimeMillis();

    FileInputStream is = new FileInputStream(jar);
    boolean hasErrors = false;
    try {
        ZipInputStream zip = new ZipInputStream(is);

        while (zip.available() > 0) {
            hasErrors |= parseLibraryFileChunk(jar, libDescription, zip, classesPerChunk);
        }
    } finally {
        try {
            is.close();
        } catch (Throwable e) {
        }
    }

    System.out.println("Testing library " + libDescription + " done in "
            + millisecondsToSecondsString(System.currentTimeMillis() - start) + "s "
            + (hasErrors ? "with" : "without") + " errors");

    return hasErrors;
}

From source file:org.springframework.roo.addon.roobot.eclipse.client.AddOnRooBotEclipseOperationsImpl.java

private boolean populateBundleCache(boolean startupTime) {
    boolean success = false;
    InputStream is = null;//from   w  w w .  j  a  va  2  s. com
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        String url = props.getProperty("roobot.url", ROOBOT_XML_URL);
        if (url == null) {
            log.warning("Bundle properties could not be loaded");
            return false;
        }
        if (url.startsWith("http://")) {
            // Handle it as HTTP
            URL httpUrl = new URL(url);
            String failureMessage = urlInputStreamService.getUrlCannotBeOpenedMessage(httpUrl);
            if (failureMessage != null) {
                if (!startupTime) {
                    // This wasn't just an eager startup time attempt, so let's display the error reason
                    // (for startup time, we just fail quietly)
                    log.warning(failureMessage);
                }
                return false;
            }
            // It appears we can acquire the URL, so let's do it
            is = urlInputStreamService.openConnection(httpUrl);
        } else {
            // Fallback to normal protocol handler (likely in local development testing etc
            is = new URL(url).openStream();
        }
        if (is == null) {
            log.warning("Could not connect to Roo Addon bundle repository index");
            return false;
        }

        ZipInputStream zip = new ZipInputStream(is);
        zip.getNextEntry();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[8192];
        int length = -1;
        while (zip.available() > 0) {
            length = zip.read(buffer, 0, 8192);
            if (length > 0) {
                baos.write(buffer, 0, length);
            }
        }

        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        Document roobotXml = db.parse(bais);

        if (roobotXml != null) {
            bundleCache.clear();
            for (Element bundleElement : XmlUtils.findElements("/roobot/bundles/bundle",
                    roobotXml.getDocumentElement())) {
                String bsn = bundleElement.getAttribute("bsn");
                List<Comment> comments = new LinkedList<Comment>();
                for (Element commentElement : XmlUtils.findElements("comments/comment", bundleElement)) {
                    comments.add(new Comment(Rating.fromInt(new Integer(commentElement.getAttribute("rating"))),
                            commentElement.getAttribute("comment"),
                            dateFormat.parse(commentElement.getAttribute("date"))));
                }
                Bundle bundle = new Bundle(bundleElement.getAttribute("bsn"),
                        new Float(bundleElement.getAttribute("uaa-ranking")).floatValue(), comments);

                for (Element versionElement : XmlUtils.findElements("versions/version", bundleElement)) {
                    if (bsn != null && bsn.length() > 0 && versionElement != null) {
                        String signedBy = "";
                        String pgpKey = versionElement.getAttribute("pgp-key-id");
                        if (pgpKey != null && pgpKey.length() > 0) {
                            Element pgpSigned = XmlUtils.findFirstElement(
                                    "/roobot/pgp-keys/pgp-key[@id='" + pgpKey + "']/pgp-key-description",
                                    roobotXml.getDocumentElement());
                            if (pgpSigned != null) {
                                signedBy = pgpSigned.getAttribute("text");
                            }
                        }

                        Map<String, String> commands = new HashMap<String, String>();
                        for (Element shell : XmlUtils.findElements("shell-commands/shell-command",
                                versionElement)) {
                            commands.put(shell.getAttribute("command"), shell.getAttribute("help"));
                        }

                        StringBuilder versionBuilder = new StringBuilder();
                        versionBuilder.append(versionElement.getAttribute("major")).append(".")
                                .append(versionElement.getAttribute("minor"));
                        String versionMicro = versionElement.getAttribute("micro");
                        if (versionMicro != null && versionMicro.length() > 0) {
                            versionBuilder.append(".").append(versionMicro);
                        }
                        String versionQualifier = versionElement.getAttribute("qualifier");
                        if (versionQualifier != null && versionQualifier.length() > 0) {
                            versionBuilder.append(".").append(versionQualifier);
                        }

                        String rooVersion = versionElement.getAttribute("roo-version");
                        if (rooVersion.equals("*") || rooVersion.length() == 0) {
                            rooVersion = getVersionForCompatibility();
                        } else {
                            String[] split = rooVersion.split("\\.");
                            if (split.length > 2) {
                                //only interested in major.minor
                                rooVersion = split[0] + "." + split[1];
                            }
                        }

                        BundleVersion version = new BundleVersion(versionElement.getAttribute("url"),
                                versionElement.getAttribute("obr-url"), versionBuilder.toString(),
                                versionElement.getAttribute("name"),
                                new Long(versionElement.getAttribute("size")).longValue(),
                                versionElement.getAttribute("description"), pgpKey, signedBy, rooVersion,
                                commands);
                        // For security reasons we ONLY accept httppgp:// add-on versions
                        if (!version.getUri().startsWith("httppgp://")) {
                            continue;
                        }
                        bundle.addVersion(version);
                    }
                    bundleCache.put(bsn, bundle);
                }
            }
            success = true;
        }
        zip.close();
        baos.close();
        bais.close();
    } catch (Throwable ignore) {
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException ignored) {
        }
    }
    if (success && startupTime) {
        //printAddonStats();
    }
    return success;
}

From source file:org.springframework.roo.addon.roobot.eclipse.client.AddOnRooBotEclipseOperationsImpl.java

private boolean verifyRepository(String repoUrl) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    Document doc = null;/*from  ww  w  .j  av a  2  s.  com*/
    try {
        URL obrUrl = null;
        obrUrl = new URL(repoUrl);
        DocumentBuilder db = dbf.newDocumentBuilder();
        if (obrUrl.toExternalForm().endsWith(".zip")) {
            ZipInputStream zip = new ZipInputStream(obrUrl.openStream());
            zip.getNextEntry();

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[8192];
            int length = -1;
            while (zip.available() > 0) {
                length = zip.read(buffer, 0, 8192);
                if (length > 0) {
                    baos.write(buffer, 0, length);
                }
            }
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            doc = db.parse(bais);
        } else {
            doc = db.parse(obrUrl.openStream());
        }
        Validate.notNull(doc, "RooBot was unable to parse the repository document of this add-on");
        for (Element resource : XmlUtils.findElements("resource", doc.getDocumentElement())) {
            if (resource.hasAttribute("uri")) {
                if (!resource.getAttribute("uri").startsWith("httppgp")) {
                    log.warning("Sorry, the resource " + resource.getAttribute("uri")
                            + " does not follow HTTPPGP conventions mangraded by Spring Roo so the OBR file at "
                            + repoUrl + " is unacceptable at this time");
                    return false;
                }
            }
        }
        doc = null;
    } catch (Exception e) {
        throw new IllegalStateException("RooBot was unable to parse the repository document of this add-on", e);
    }
    return true;
}

From source file:org.wso2.carbon.server.util.Utils.java

/**
 * @param zipInputStream zipInputStream//w w w . ja  v  a  2s.co  m
 * @return return zipetry map
 * @throws IOException IOException
 */
private static List<ZipEntry> populateList(ZipInputStream zipInputStream) throws IOException {
    List<ZipEntry> listEntry = new ArrayList<ZipEntry>();
    while (zipInputStream.available() == 1) {
        ZipEntry entry = zipInputStream.getNextEntry();
        if (entry == null) {
            break;
        }
        listEntry.add(entry);
    }
    return listEntry;
}

From source file:reconcile.hbase.mapreduce.ZipInputFormat.java

private static byte[] read(String entry, ZipInputStream zis, int numBytes) {
    byte[] data = new byte[numBytes];
    int n = 0;/* w  w w.  j  ava  2  s  . c o  m*/
    try {
        while (zis.available() == 1 && (n < numBytes)) {
            data[n] = (byte) zis.read();
            ++n;
        }
    } catch (IOException e) {
        LOG.error("failure reading zip entry(" + entry + ")");
        e.printStackTrace();
        return null;
    }
    LOG.info("Read bytes(" + n + ") from entry (" + entry + ")");
    LOG.debug("Read value(" + Bytes.toString(data) + ") from entry (" + entry + ")");

    return data;
}

From source file:reconcile.hbase.mapreduce.ZipInputFormat.java

private static byte[] read(String entry, ZipInputStream zis) {
    ArrayList<byte[]> dataArray = new ArrayList<byte[]>();
    byte[] current = null;

    int i = 0;//from ww w  .j a  va  2  s . com
    int n = 0;
    try {
        while (zis.available() == 1) {
            if (n % bufSize == 0) {
                current = new byte[bufSize];
                dataArray.add(current);
                i = 0;
            }
            current[i] = (byte) zis.read();
            ++n;
            ++i;
        }
    } catch (IOException e) {
        LOG.error("failure reading zip entry(" + entry + ")");
        e.printStackTrace();
        return null;
    }
    --n;

    // Copy multiple buffers into single large buffer
    byte[] data = new byte[n];
    i = 0;
    for (byte[] buffer : dataArray) {
        int copyLength = bufSize;
        if ((i + copyLength) > n) {
            copyLength = n - i;
        }
        for (int j = 0; j < copyLength; ++j) {
            data[i] = buffer[j];
            ++i;
        }
    }

    LOG.info("Read bytes(" + n + ") from entry (" + entry + ")");
    LOG.debug("Read value(" + Bytes.toString(data) + ") from entry (" + entry + ")");

    return data;
}

From source file:util.CreationZipTool.java

/**
 * Returns the zip entries as Media beans.
 *
 * @param zipBytes//from w  w  w  .  ja v  a 2s  . com
 * @return List of Media beans
 */
public List getEntries(byte[] zipBytes) throws SomeZipException {

    ByteArrayInputStream bis = null;
    ZipInputStream zis = null;
    ZipEntry zipEntry = null;
    List entries = new ArrayList();
    byte[] b = null;

    try {
        bis = new ByteArrayInputStream(zipBytes);
        zis = new ZipInputStream(bis);
        boolean notDone = true;
        zipEntry = zis.getNextEntry();
        while (zipEntry != null) {
            if (zipEntry.isDirectory()) {
                zipEntry = zis.getNextEntry();
                continue;
            }
            b = new byte[MAX_MEDIA_SIZE];
            int offset = 0;
            int rb = 0;
            while ((zis.available() != 0) && ((rb = zis.read(b, offset, CHUNK)) != -1)) {
                offset += rb;
            }
            if ((zis.available() == 0) || (rb == -1)) {
                String entryName = zipEntry.getName();
                String suffix = getSuffix(entryName);
                //if (validImage.isValidSuffix(suffix)) {
                Media media = new Media();
                logger.info("Found New Image " + offset + " bytes");
                media.setData(new String(b, 0, offset).getBytes());
                media.setSize(offset);
                logger.info("ZipEntry name = " + entryName);
                media.setName(fileName(entryName));
                entries.add(media);
                //} else {
                //logger.warn("Bad image file in zip, ignoring " + entryName);
                //}
                zis.closeEntry();
                zipEntry = zis.getNextEntry();
            }
        }
        zis.close();
    } catch (Exception e) {
        throw new SomeZipException("Error processing zip bytes", e);
    }
    return entries;
}

From source file:util.TraversalZipTool.java

/**
 * Returns the zip entries as Media beans.
 *
 * @param zipBytes/*from w  w  w  .  java2  s. c  om*/
 * @return List of Media beans
 */
public List getEntries(byte[] zipBytes) throws SomeZipException {

    ByteArrayInputStream bis = null;
    ZipInputStream zis = null;
    ZipEntry zipEntry = null;
    List entries = new ArrayList();
    byte[] b = null;

    try {
        bis = new ByteArrayInputStream(zipBytes);
        zis = new ZipInputStream(bis);
        boolean notDone = true;
        zipEntry = zis.getNextEntry();
        while (zipEntry != null) {
            if (zipEntry.isDirectory()) {
                zipEntry = zis.getNextEntry();
                continue;
            }
            b = new byte[MAX_MEDIA_SIZE];
            int offset = 0;
            int rb = 0;
            while ((zis.available() != 0) && ((rb = zis.read(b, offset, CHUNK)) != -1)) {
                offset += rb;
            }
            if ((zis.available() == 0) || (rb == -1)) {
                String entryName = zipEntry.getName();
                String suffix = getSuffix(entryName);
                if (validImage.isValidSuffix(suffix)) {
                    Media media = new Media();
                    //logger.info("Found New Image " + offset + " bytes");
                    media.setData(new String(b, 0, offset).getBytes());
                    media.setSize(offset);
                    //logger.info("ZipEntry name = " + entryName);
                    media.setName(fileName(entryName));
                    entries.add(media);
                } else {
                    logger.warn("Bad image file in zip, ignoring " + entryName);
                }
                zis.closeEntry();
                zipEntry = zis.getNextEntry();
            }
        }
        zis.close();
    } catch (Exception e) {
        throw new SomeZipException("Error processing zip bytes", e);
    }
    return entries;
}