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:Main.java

public static void main(String args[]) throws Exception {
    ZipOutputStream fout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(args[0])));

    for (int n = 1; n < args.length; n++) {

        BufferedInputStream fin = new BufferedInputStream(new FileInputStream(args[n]));
        ZipEntry ze = new ZipEntry(rmPath(args[n]));

        fout.putNextEntry(ze);/*from  w w  w.  j  ava  2  s  . c  om*/

        int i;
        do {
            i = fin.read();
            if (i != -1)
                fout.write(i);
        } while (i != -1);

        fout.closeEntry();
        fin.close();

        System.out.println("Compressing " + args[n]);
        System.out.println(
                " Original Size: " + ze.getSize() + " Compressed Size: " + ze.getCompressedSize() + "\n");
    }
    fout.close();
}

From source file:ws.salient.aws.Application.java

public static void unpackRepository(String resourceName) {
    try {//w  ww.j  ava  2s  .c om
        try (ZipInputStream stream = new ZipInputStream(Application.class.getResourceAsStream(resourceName))) {
            String outdir = MavenSettings.getSettings().getLocalRepository();

            System.out.println(outdir);
            ZipEntry entry;
            while ((entry = stream.getNextEntry()) != null) {

                if (entry.getSize() == 0) {
                    File outpath = new File(outdir + "/" + entry.getName());
                    outpath.mkdirs();
                } else {
                    File outpath = new File(outdir + "/" + entry.getName());
                    outpath.createNewFile();
                    try (FileOutputStream output = new FileOutputStream(outpath)) {
                        IOUtils.copy(stream, output);
                    }
                }
            }
        }
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.dustindoloff.s3websitedeploy.Main.java

private static boolean upload(final AmazonS3 s3Client, final String bucket, final ZipFile zipFile) {
    boolean failed = false;
    final ObjectMetadata data = new ObjectMetadata();
    final Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        final ZipEntry entry = entries.nextElement();
        data.setContentLength(entry.getSize());
        try {/*from w w w .j a  v  a  2s . co m*/
            s3Client.putObject(bucket, entry.getName(), zipFile.getInputStream(entry), data);
        } catch (final AmazonClientException | IOException e) {
            failed = true;
        }
    }
    return !failed;
}

From source file:Main.java

public static void readZipFile(String file) throws Exception {
    ZipFile zf = new ZipFile(file);
    InputStream in = new BufferedInputStream(new FileInputStream(file));
    ZipInputStream zin = new ZipInputStream(in);
    ZipEntry ze;
    while ((ze = zin.getNextEntry()) != null) {
        if (ze.isDirectory()) {
        } else {/* ww w  .j ava  2 s .  c  o m*/
            System.err.println("file - " + ze.getName() + " : " + ze.getSize() + " bytes");
            long size = ze.getSize();
            if (size > 0) {
                BufferedReader br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
                br.close();
            }
            System.out.println();
        }
    }
    zin.closeEntry();
}

From source file:org.commonjava.indy.folo.FoloUtils.java

/**
 * Read records from input stream and execute consumer function.
 * @param inputStream/*  w  ww  .j  a v  a 2s. c  o m*/
 * @param consumer
 * @return count of records read from the stream
 */
public static int readZipInputStreamAnd(InputStream inputStream, Consumer<TrackedContent> consumer)
        throws IOException, ClassNotFoundException {
    int count = 0;
    try (ZipInputStream stream = new ZipInputStream(inputStream)) {
        ZipEntry entry;
        while ((entry = stream.getNextEntry()) != null) {
            logger.trace("Read entry: %s, len: %d", entry.getName(), entry.getSize());
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int len;
            byte[] buffer = new byte[1024];
            while ((len = stream.read(buffer)) > 0) {
                bos.write(buffer, 0, len);
            }
            bos.close();

            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
            TrackedContent record = (TrackedContent) ois.readObject();
            consumer.accept(record);
            count++;
        }
    }
    return count;
}

From source file:nl.knaw.dans.common.lang.file.UnzipUtil.java

private static List<File> extract(final File zipFile, final String destPath, final UnzipListener unzipListener)
        throws FileNotFoundException, ZipException, IOException {
    final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));

    // get total uncompressed size of zip file
    final ZipFile zf = new ZipFile(zipFile);
    final Enumeration<? extends ZipEntry> e = zf.entries();
    long totSize = 0;
    int totFiles = 0;
    while (e.hasMoreElements()) {
        final ZipEntry ze = (ZipEntry) e.nextElement();
        totSize += ze.getSize();
        totFiles++;//from w  ww. j  a v  a 2s . c o  m
    }
    zf.close();

    return extract(zis, destPath, totFiles, unzipListener, totSize);
}

From source file:Main.java

public static byte[] readZipEntry(File zfile, ZipEntry entry) throws ZipException, IOException {
    Log.d("file3: ", zfile.toString());
    Log.d("zipEntry3: ", entry.toString());
    ZipFile zipFile = new ZipFile(zfile);
    if (entry != null && !entry.isDirectory()) {
        byte[] barr = new byte[(int) entry.getSize()];
        int read = 0;
        int len = 0;
        InputStream is = zipFile.getInputStream(entry);
        BufferedInputStream bis = new BufferedInputStream(is);
        int length = barr.length;
        while ((len = bis.read(barr, read, length - read)) != -1) {
            read += len;/*from   w w  w. j av  a  2  s  .  c o m*/
        }
        bis.close();
        is.close();
        zipFile.close();
        return barr;
    } else {
        zipFile.close();
        return new byte[0];
    }
}

From source file:org.fuin.kickstart4j.Utils.java

/**
 * Unzips a file into a local directory. WARNING: Only relative path entries
 * are allowed inside the archive! <code>IOException</code>s are mapped into
 * a <code>RuntimeException</code> .
 * //from   w  w  w.ja v  a 2  s .c  o m
 * @param listener
 *            Listener to inform - Can be <code>null</code> if no progress
 *            information is needed.
 * @param zipFile
 *            Source ZIP file - Cannot be <code>null</code> and must be a
 *            valid ZIP file.
 * @param zipFileNo
 *            Number of the zip file.
 * @param destDir
 *            Destination directory - Cannot be <code>null</code> and must
 *            exist.
 * @param cancelable
 *            Signals if the unzip should be canceled - Can be
 *            <code>null</code> if no cancel option is required.
 */
public static void unzip(final FileCopyProgressListener listener, final File zipFile, final int zipFileNo,
        final File destDir, final Cancelable cancelable) {
    try {
        Utils4J.unzip(zipFile, destDir, new Utils4J.UnzipInputStreamWrapper() {
            public InputStream wrapInputStream(final InputStream in, final ZipEntry entry,
                    final File destFile) {
                if (entry.getSize() > Integer.MAX_VALUE) {
                    throw new IllegalArgumentException(
                            "Cannot handle files larger than " + Integer.MAX_VALUE + " bytes!");
                }
                if (listener != null) {
                    listener.updateFile(zipFile.toString(), destFile.toString(), zipFileNo,
                            (int) entry.getSize());
                }
                if (LOG.isInfoEnabled()) {
                    LOG.info("UNZIP " + zipFile + " => " + destFile);
                }
                return new FileCopyProgressInputStream(listener, in, (int) entry.getSize());
            }
        }, cancelable);
    } catch (final IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.mycontroller.standalone.backup.Restore.java

private static void extractZipFile(String zipFileName, String destination)
        throws FileNotFoundException, IOException {
    ZipFile zipFile = new ZipFile(zipFileName);
    Enumeration<?> enu = zipFile.entries();
    //create destination if not exists
    FileUtils.forceMkdir(FileUtils.getFile(destination));
    while (enu.hasMoreElements()) {
        ZipEntry zipEntry = (ZipEntry) enu.nextElement();
        String name = zipEntry.getName();
        long size = zipEntry.getSize();
        long compressedSize = zipEntry.getCompressedSize();
        _logger.debug("name:{} | size:{} | compressed size:{}", name, size, compressedSize);
        File file = FileUtils.getFile(destination + File.separator + name);
        //Create destination if it's not available
        if (name.endsWith(File.separator)) {
            file.mkdirs();/*ww  w .j a v a 2s  .c om*/
            continue;
        }

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

        InputStream is = zipFile.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();
    }
    zipFile.close();
}

From source file:org.mycontroller.standalone.BackupRestore.java

private static void extractZipFile(String zipFileName, String destination)
        throws FileNotFoundException, IOException {
    ZipFile zipFile = new ZipFile(zipFileName);
    Enumeration<?> enu = zipFile.entries();
    //create destination if not exists
    FileUtils.forceMkdir(FileUtils.getFile(destination));
    while (enu.hasMoreElements()) {
        ZipEntry zipEntry = (ZipEntry) enu.nextElement();
        String name = zipEntry.getName();
        long size = zipEntry.getSize();
        long compressedSize = zipEntry.getCompressedSize();
        _logger.debug("name:{} | size:{} | compressed size:{}", name, size, compressedSize);
        File file = FileUtils.getFile(destination + "/" + name);
        //Create destination if it's not available
        if (name.endsWith("/")) {
            file.mkdirs();/*from  w w  w . j  a  v  a 2 s . c  om*/
            continue;
        }

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

        InputStream is = zipFile.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();
    }
    zipFile.close();
}