Example usage for java.util.zip ZipEntry getCompressedSize

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

Introduction

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

Prototype

public long getCompressedSize() 

Source Link

Document

Returns the size of the compressed entry data.

Usage

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;//from  w  ww . ja va 2  s  .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:Main.java

public static void compressAFileToZip(File zip, File source) throws IOException {
    Log.d("source: " + source.getAbsolutePath(), ", length: " + source.length());
    Log.d("zip:", zip.getAbsolutePath());
    zip.getParentFile().mkdirs();/*from  w  ww . j  av  a 2  s . c o  m*/
    File tempFile = new File(zip.getAbsolutePath() + ".tmp");
    FileOutputStream fos = new FileOutputStream(tempFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    ZipOutputStream zos = new ZipOutputStream(bos);
    ZipEntry zipEntry = new ZipEntry(source.getName());
    zipEntry.setTime(source.lastModified());
    zos.putNextEntry(zipEntry);

    FileInputStream fis = new FileInputStream(source);
    BufferedInputStream bis = new BufferedInputStream(fis);
    byte[] bArr = new byte[4096];
    int byteRead = 0;
    while ((byteRead = bis.read(bArr)) != -1) {
        zos.write(bArr, 0, byteRead);
    }
    zos.flush();
    zos.closeEntry();
    zos.close();
    Log.d("zipEntry: " + zipEntry, "compressedSize: " + zipEntry.getCompressedSize());
    bos.flush();
    fos.flush();
    bos.close();
    fos.close();
    bis.close();
    fis.close();
    zip.delete();
    tempFile.renameTo(zip);
}

From source file:org.esa.snap.engine_utilities.util.ZipUtils.java

public static boolean isValid(final File file) {
    try (ZipFile zipfile = new ZipFile(file)) {
        try (ZipInputStream zis = new ZipInputStream(new FileInputStream(file))) {
            ZipEntry ze = zis.getNextEntry();
            if (ze == null) {
                return false;
            }/*from w  ww .  j ava2s  .  com*/
            while (ze != null) {
                // if it throws an exception fetching any of the following then we know the file is corrupted.
                try (InputStream in = zipfile.getInputStream(ze)) {
                    ze.getCrc();
                    ze.getCompressedSize();
                    ze = zis.getNextEntry();
                } catch (IOException e) {
                    return false;
                }
            }
            return true;
        } catch (IOException e) {
            return false;
        }
    } catch (IOException e) {
        return false;
    }
}

From source file:com.taobao.android.builder.tools.zip.ZipUtils.java

public static void addFileAndDirectoryToZip(File output, File srcDir, Map<String, ZipEntry> zipEntryMethodMap)
        throws Exception {
    if (output.isDirectory()) {
        throw new IOException("This is a directory!");
    }//from  ww w.  j  a v a 2s  .co m
    if (!output.getParentFile().exists()) {
        output.getParentFile().mkdirs();
    }

    if (!output.exists()) {
        output.createNewFile();
    }
    List fileList = getSubFiles(srcDir);
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(output));
    ZipEntry ze = null;
    byte[] buf = new byte[1024];
    int readLen = 0;
    for (int i = 0; i < fileList.size(); i++) {
        File f = (File) fileList.get(i);
        ze = new ZipEntry(getAbsFileName(srcDir.getPath(), f));
        ze.setSize(f.length());
        ze.setTime(f.lastModified());
        if (zipEntryMethodMap != null) {
            ZipEntry originEntry = zipEntryMethodMap.get(f.getAbsolutePath());
            if (originEntry != null) {
                ze.setCompressedSize(originEntry.getCompressedSize());
                ze.setCrc(originEntry.getCrc());
                ze.setMethod(originEntry.getMethod());
            }
        }
        zos.putNextEntry(ze);
        InputStream is = new BufferedInputStream(new FileInputStream(f));
        while ((readLen = is.read(buf, 0, 1024)) != -1) {
            zos.write(buf, 0, readLen);
        }
        is.close();
    }
    zos.close();
}

From source file:com.agilejava.maven.docbkx.ZipFileProcessorTest.java

/**
 * DOCUMENT ME!//from w  w  w  .j av a 2 s.c o  m
 *
 * @throws Exception DOCUMENT ME!
 */
public void testProcessFile() throws Exception {
    File file = new File(getDirectory(), "sample.zip");
    ZipFileProcessor processor = new ZipFileProcessor(file);
    final int[] count = new int[1];
    processor.process(new ZipEntryVisitor() {
        public void visit(ZipEntry entry, InputStream in) throws IOException {
            count[0]++;
            assertTrue(FILENAMES.contains(entry.getName()));

            if ((entry.getCompressedSize() > 0) && entry.getName().endsWith("test2.txt")) {
                assertEquals("i have a dream", IOUtils.toString(in));
                in.close();
            }
        }
    });
    assertEquals(3, count[0]);
}

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
 *//*from ww  w  .j a v a2  s .co  m*/
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");
    }
}

From source file:csns.web.controller.DownloadController.java

private long addToZip(ZipOutputStream zip, String dir, Collection<File> files) throws IOException {
    files = removeDuplicates(files);/*from ww w .  j  a  v a2 s. c o m*/

    long totalSize = 0;
    for (File file : files) {
        ZipEntry entry = new ZipEntry(dir + "/" + file.getName());
        zip.putNextEntry(entry);
        fileIO.copy(file, zip);
        zip.closeEntry();
        totalSize += entry.getCompressedSize();
    }
    return totalSize;
}

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

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

    try {//from w w w  . j av a2  s.  c  o  m
        String path = args[0].getStringValue();
        ZipFile zf = new ZipFile(path);
        log.debug(zf.getName());
        log.debug(zf.size());
        log.debug(zf.hashCode());
        ZipEntry e;
        Enumeration<? extends ZipEntry> entries = zf.entries();
        ValueSequence s = new ValueSequence();
        for (int i = 0; entries.hasMoreElements(); i++) {
            log.debug(i);
            e = entries.nextElement();
            log.debug(e.getName());
            log.debug(e.getComment());
            log.debug(e.isDirectory());
            log.debug(e.getCompressedSize());
            log.debug(e.getCrc());
            log.debug(e.getMethod());
            log.debug(e.getSize());
            log.debug(e.getTime());
            if (!e.isDirectory())
                s.add(new StringValue(e.getName()));
        }
        return s;
    } catch (Exception e) {
        throw new XPathException("error processing zip file: " + e.getLocalizedMessage(), e);
    }

}

From source file:JarResources.java

private String dumpZipEntry(ZipEntry ze) {
    StringBuffer sb = new StringBuffer();
    if (ze.isDirectory()) {
        sb.append("d ");
    } else {//from  w  w  w  .  j  ava2s .c  o  m
        sb.append("f ");
    }

    if (ze.getMethod() == ZipEntry.STORED) {
        sb.append("stored   ");
    } else {
        sb.append("defalted ");
    }

    sb.append(ze.getName());
    sb.append("\t");
    sb.append("" + ze.getSize());
    if (ze.getMethod() == ZipEntry.DEFLATED) {
        sb.append("/" + ze.getCompressedSize());
    }

    return (sb.toString());
}

From source file:ZipFileViewer.java

public Object getValueAt(int row, int col) {
    ZipEntry zipEntry = (ZipEntry) (m_zipEntries.get(row));
    switch (col) {
    case NAME:/*from   w w w.ja v  a 2 s .  c  o m*/
        return zipEntry.getName();
    case SIZE:
        if (zipEntry.isDirectory()) {
            return "";
        } else {
            return String.valueOf(zipEntry.getSize() / 1000) + " KB";
        }
    case COMP_SIZE:
        if (zipEntry.isDirectory()) {
            return "";
        } else {
            return String.valueOf(zipEntry.getCompressedSize() / 1000) + " KB";
        }
    case TYPE:
        if (zipEntry.isDirectory()) {
            return "Directory";
        } else {
            return "File";
        }
    case LAST_MODI:
        return String.valueOf(new Date(zipEntry.getTime()));
    }
    return new String();
}