Example usage for java.util.zip ZipEntry getSize

List of usage examples for java.util.zip ZipEntry getSize

Introduction

In this page you can find the example usage for java.util.zip ZipEntry getSize.

Prototype

public long getSize() 

Source Link

Document

Returns the uncompressed size of the entry data.

Usage

From source file:es.juntadeandalucia.panelGestion.negocio.utiles.Utils.java

public static byte[] getShapeDataFromZip(byte[] localSourceData) {
    byte[] data = null;
    InputStream in = new ByteArrayInputStream(localSourceData);
    ZipInputStream zis = new ZipInputStream(in);
    ZipEntry ze;
    try {/*from   ww w. j a v  a2s.c o m*/
        while ((ze = zis.getNextEntry()) != null) {
            if (!ze.isDirectory()) {
                String fileName = ze.getName().toLowerCase();
                if (fileName.endsWith(".shp")) {
                    long entrySize = ze.getSize();
                    if (entrySize != -1) {
                        data = IOUtils.toByteArray(zis, entrySize);
                        break;
                    }
                }
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            zis.closeEntry();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            zis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return data;
}

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

/**
 * From zip nn key./*from  w w w .  j  a  v a 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:org.openstreetmap.josm.tools.ImageProvider.java

private static ImageResource getIfAvailableZip(String full_name, File archive, ImageType type) {
    ZipFile zipFile = null;/*  ww  w.  j  a va 2  s  .c  o m*/
    try {
        zipFile = new ZipFile(archive);
        ZipEntry entry = zipFile.getEntry(full_name);
        if (entry != null) {
            int size = (int) entry.getSize();
            int offs = 0;
            byte[] buf = new byte[size];
            InputStream is = null;
            try {
                is = zipFile.getInputStream(entry);
                switch (type) {
                case SVG:
                    URI uri = getSvgUniverse().loadSVG(is, full_name);
                    SVGDiagram svg = getSvgUniverse().getDiagram(uri);
                    return svg == null ? null : new ImageResource(svg);
                case OTHER:
                    while (size > 0) {
                        int l = is.read(buf, offs, size);
                        offs += l;
                        size -= l;
                    }
                    BufferedImage img = null;
                    try {
                        img = ImageIO.read(new ByteArrayInputStream(buf));
                    } catch (IOException e) {
                    }
                    return img == null ? null : new ImageResource(img);
                default:
                    throw new AssertionError();
                }
            } finally {
                if (is != null) {
                    is.close();
                }
            }
        }
    } catch (Exception e) {
        System.err.println(tr("Warning: failed to handle zip file ''{0}''. Exception was: {1}",
                archive.getName(), e.toString()));
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException ex) {
            }
        }
    }
    return null;
}

From source file:org.sourcepit.common.utils.zip.EntrySizeBasedZipPartitioner.java

private TreeMap<ZipEntry, Integer> getZipEntryNames(ZipInputStreamFactory streamFactory)
        throws ZipException, IOException {
    final TreeMap<ZipEntry, Integer> entryToIndex = new TreeMap<ZipEntry, Integer>(new Comparator<ZipEntry>() {
        public int compare(ZipEntry ze1, ZipEntry ze2) {
            long s1 = ze1.getSize();
            long s2 = ze2.getSize();
            int r = (int) -(s1 - s2);
            return r == 0 ? -1 : r;
        }/*from w  ww .  jav  a2s.  co  m*/
    });

    final ZipInputStream zipStream = streamFactory.newZipInputStream();
    try {
        int idx = 0;
        ZipEntry zipEntry = zipStream.getNextEntry();
        while (zipEntry != null) {
            entryToIndex.put(zipEntry, Integer.valueOf(idx));
            zipEntry = zipStream.getNextEntry();
            idx++;
        }
    } finally {
        IOUtils.closeQuietly(zipStream);
    }

    return entryToIndex;
}

From source file:JarResources.java

public JarResources(String jarFileName) throws Exception {
    this.jarFileName = jarFileName;
    ZipFile zf = new ZipFile(jarFileName);
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        ZipEntry ze = (ZipEntry) e.nextElement();

        htSizes.put(ze.getName(), new Integer((int) ze.getSize()));
    }/*from ww w .j  av  a  2 s  .  c  om*/
    zf.close();

    // extract resources and put them into the hashtable.
    FileInputStream fis = new FileInputStream(jarFileName);
    BufferedInputStream bis = new BufferedInputStream(fis);
    ZipInputStream zis = new ZipInputStream(bis);
    ZipEntry ze = null;
    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            continue;
        }

        int size = (int) ze.getSize();
        // -1 means unknown size.
        if (size == -1) {
            size = ((Integer) htSizes.get(ze.getName())).intValue();
        }

        byte[] b = new byte[(int) size];
        int rb = 0;
        int chunk = 0;
        while (((int) size - rb) > 0) {
            chunk = zis.read(b, rb, (int) size - rb);
            if (chunk == -1) {
                break;
            }
            rb += chunk;
        }

        htJarContents.put(ze.getName(), b);
    }
}

From source file:org.atombeat.xquery.functions.util.GetZipEntrySize.java

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {/*from ww w  . j  av a 2  s.c  om*/
        ZipFile zf = new ZipFile(args[0].getStringValue());
        ZipEntry ze = zf.getEntry(args[1].getStringValue());
        return new IntegerValue(ze.getSize());
    } catch (Exception e) {
        throw new XPathException("error processing zip file: " + e.getLocalizedMessage(), e);
    }

}

From source file:com.marklogic.mapreduce.ZipEntryInputStream.java

public boolean hasNext() {
    try {/*  w  ww.j  a  v a  2s . co m*/
        ZipEntry entry;
        while ((entry = zipIn.getNextEntry()) != null) {
            if (entry.getSize() > 0) {
                entryName = entry.getName();
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Zip entry name: " + entryName);
                }
                return true;
            }
        }
        return false;
    } catch (IOException e) {
        LOG.error("Error getting next zip entry from " + fileName, e);
        return false;
    }
}

From source file:org.geowebcache.rest.filter.ZipFilterUpdate.java

public void runUpdate(RequestFilter filter, TileLayer tl) throws RestletException {

    try {// w  w w .  j  av  a 2  s.c om
        ZipInputStream zis = new ZipInputStream(is);

        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            log.info("Reading " + ze.getName() + " (" + ze.getSize() + " bytes ) for " + filter.getName());

            if (ze.isDirectory()) {
                throw new RestletException("Zip file cannot contain directories.",
                        Status.CLIENT_ERROR_BAD_REQUEST);
            }

            String[] parsedName = parseName(ze.getName());

            byte[] data = ServletUtils.readStream(zis, 16 * 1024, 1500, false);

            try {
                filter.update(data, tl, parsedName[0], Integer.parseInt(parsedName[1]));

            } catch (GeoWebCacheException e) {
                throw new RestletException("Error updating " + filter.getName() + ": " + e.getMessage(),
                        Status.SERVER_ERROR_INTERNAL);
            }

            ze = zis.getNextEntry();
        }

    } catch (IOException ioe) {
        throw new RestletException("IOException while reading zip, " + ioe.getMessage(),
                Status.CLIENT_ERROR_BAD_REQUEST);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            // Ok at this point
        }

    }
}

From source file:org.owasp.webgoat.lessons.ZipBomb.java

private long unzippedSize(File uploadedFile) throws ZipException, IOException {
    ZipFile zf = new ZipFile(uploadedFile);

    long total = 0;
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) e.nextElement();

        total += entry.getSize();

    }/* w w w.  j a va2 s  . c o m*/
    return total;
}

From source file:org.ambraproject.article.service.XslIngestArchiveProcessor.java

/**
 * Generate a description for a single zip-entry.
 *
 * @param ze  the zip entry to describe.
 * @param buf the buffer to place the description into
 *///  www . j a  v a 2 s .com
private static void entry2xml(ZipEntry ze, StringBuilder buf) {
    buf.append("<ZipEntry name=\"").append(attrEscape(ze.getName())).append("\"");

    if (ze.isDirectory())
        buf.append(" isDirectory=\"true\"");
    if (ze.getCrc() >= 0)
        buf.append(" crc=\"").append(ze.getCrc()).append("\"");
    if (ze.getSize() >= 0)
        buf.append(" size=\"").append(ze.getSize()).append("\"");
    if (ze.getCompressedSize() >= 0)
        buf.append(" compressedSize=\"").append(ze.getCompressedSize()).append("\"");
    if (ze.getTime() >= 0)
        buf.append(" time=\"").append(ze.getTime()).append("\"");

    if (ze.getComment() != null || ze.getExtra() != null) {
        buf.append(">\n");

        if (ze.getComment() != null)
            buf.append("<Comment>").append(xmlEscape(ze.getComment())).append("</Comment>\n");
        if (ze.getExtra() != null)
            buf.append("<Extra>").append(base64Encode(ze.getExtra())).append("</Extra>\n");

        buf.append("</ZipEntry>\n");
    } else {
        buf.append("/>\n");
    }
}