Example usage for java.util.zip ZipEntry STORED

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

Introduction

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

Prototype

int STORED

To view the source code for java.util.zip ZipEntry STORED.

Click Source Link

Document

Compression method for uncompressed entries.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    String zipname = "data.zip";
    ZipFile zipFile = new ZipFile(zipname);
    Enumeration enumeration = zipFile.entries();
    while (enumeration.hasMoreElements()) {
        ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
        System.out.println(zipEntry.getMethod() == ZipEntry.STORED);
    }/*from  w ww .  j a v  a2s .  co m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ZipFile zf = new ZipFile("your.zip");
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        ZipEntry ze = (ZipEntry) e.nextElement();
        String name = ze.getName();

        Date lastModified = new Date(ze.getTime());
        long uncompressedSize = ze.getSize();
        long compressedSize = ze.getCompressedSize();

        int method = ze.getMethod();

        if (method == ZipEntry.STORED) {
            System.out.println(name + " was stored at " + lastModified);
            System.out.println("with a size of  " + uncompressedSize + " bytes");
        } else if (method == ZipEntry.DEFLATED) {
            System.out.println(name + " was deflated at " + lastModified);
            System.out.println("from  " + uncompressedSize + " bytes to " + compressedSize
                    + " bytes, a savings of " + (100.0 - 100.0 * compressedSize / uncompressedSize) + "%");
        } else {/*from www .  java 2s . c o m*/
            System.out.println(name + " was compressed at " + lastModified);
            System.out.println("from  " + uncompressedSize + " bytes to " + compressedSize
                    + " bytes, a savings of " + (100.0 - 100.0 * compressedSize / uncompressedSize) + "%");
        }
    }
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {

    JarFile jf = new JarFile(args[0]);
    Enumeration e = jf.entries();
    while (e.hasMoreElements()) {
        JarEntry je = (JarEntry) e.nextElement();
        String name = je.getName();
        Date lastModified = new Date(je.getTime());
        long uncompressedSize = je.getSize();
        long compressedSize = je.getCompressedSize();

        int method = je.getMethod();

        if (method == ZipEntry.STORED) {
            System.out.println(name + " was stored at " + lastModified);
            System.out.println("with a size of  " + uncompressedSize + " bytes");
        } else if (method == ZipEntry.DEFLATED) {
            System.out.println(name + " was deflated at " + lastModified);
            System.out.println("from  " + uncompressedSize + " bytes to " + compressedSize
                    + " bytes, a savings of " + (100.0 - 100.0 * compressedSize / uncompressedSize) + "%");
        } else {//from   w  w  w.j  ava2s  .co m
            System.out.println(name + " was compressed using an unrecognized method at " + lastModified);
            System.out.println("from  " + uncompressedSize + " bytes to " + compressedSize
                    + " bytes, a savings of " + (100.0 - 100.0 * compressedSize / uncompressedSize) + "%");
        }
    }
}

From source file:MainClass.java

public static void main(String[] args) {

    try {//from  w  w  w.j  a  va  2  s . com
        ZipFile zf = new ZipFile("your.zip");
        Enumeration e = zf.entries();
        while (e.hasMoreElements()) {
            ZipEntry ze = (ZipEntry) e.nextElement();
            String name = ze.getName();

            Date lastModified = new Date(ze.getTime());
            long uncompressedSize = ze.getSize();
            long compressedSize = ze.getCompressedSize();

            int method = ze.getMethod();

            if (method == ZipEntry.STORED) {
                System.out.println(name + " was stored at " + lastModified);
                System.out.println("with a size of  " + uncompressedSize + " bytes");
            } else if (method == ZipEntry.DEFLATED) {
                System.out.println(name + " was deflated at " + lastModified);
                System.out.println("from  " + uncompressedSize + " bytes to " + compressedSize
                        + " bytes, a savings of " + (100.0 - 100.0 * compressedSize / uncompressedSize) + "%");
            } else {
                System.out.println(name + " was compressed using an unrecognized method at " + lastModified);
                System.out.println("from  " + uncompressedSize + " bytes to " + compressedSize
                        + " bytes, a savings of " + (100.0 - 100.0 * compressedSize / uncompressedSize) + "%");
            }
        }
    } catch (IOException ex) {
        System.err.println(ex);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ZipFile zf = new ZipFile("a.zip");
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        ZipEntry ze = (ZipEntry) e.nextElement();
        String name = ze.getName();

        long uncompressedSize = ze.getSize();
        long compressedSize = ze.getCompressedSize();
        long crc = ze.getCrc();
        int method = ze.getMethod();
        String comment = ze.getComment();

        System.out.println(name + " was stored at " + new Date(ze.getTime()));
        if (method == ZipEntry.STORED) {
            System.out.println("with a size of  " + uncompressedSize + " bytes");
        } else if (method == ZipEntry.DEFLATED) {
            System.out.println("from " + uncompressedSize + " bytes to " + compressedSize);
        } else {//from   w w  w .j av a 2  s. c  o m
            System.out.println("from " + uncompressedSize + " bytes to " + compressedSize);
        }
        System.out.println("Its CRC is " + crc);
        if (comment != null && !comment.equals("")) {
            System.out.println(comment);
        }
        if (ze.isDirectory()) {
            System.out.println(name + " is a directory");
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    JarFile jf = new JarFile("a.jar");
    Enumeration e = jf.entries();
    while (e.hasMoreElements()) {
        JarEntry je = (JarEntry) e.nextElement();
        System.out.println(je.getName());
        long uncompressedSize = je.getSize();
        long compressedSize = je.getCompressedSize();
        long crc = je.getCrc();
        int method = je.getMethod();
        String comment = je.getComment();
        System.out.println(new Date(je.getTime()));
        System.out.println("from  " + uncompressedSize + " bytes to " + compressedSize);
        if (method == ZipEntry.STORED) {
            System.out.println("ZipEntry.STORED");
        } else if (method == ZipEntry.DEFLATED) {
            System.out.println(ZipEntry.DEFLATED);
        }//ww w .ja  va 2 s  .  c  o m
        System.out.println("Its CRC is " + crc);
        System.out.println(comment);
        System.out.println(je.isDirectory());

        Attributes a = je.getAttributes();
        if (a != null) {
            Object[] nameValuePairs = a.entrySet().toArray();
            for (int j = 0; j < nameValuePairs.length; j++) {
                System.out.println(nameValuePairs[j]);
            }
        }
        System.out.println();
    }
}

From source file:com.smash.revolance.ui.model.helper.ArchiveHelper.java

public static File buildArchive(File archive, File... files) throws FileNotFoundException {
    FileOutputStream fos = new FileOutputStream(archive);
    ZipOutputStream zos = new ZipOutputStream(fos);
    int bytesRead;
    byte[] buffer = new byte[1024];
    CRC32 crc = new CRC32();

    for (File file : files) {
        if (!file.exists()) {
            System.err.println("Skipping: " + file);
            continue;
        }/*from   w  w  w. j  a v a2  s  .co  m*/
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            crc.reset();
            while ((bytesRead = bis.read(buffer)) != -1) {
                crc.update(buffer, 0, bytesRead);
            }

            bis.close();

            // Reset to beginning of input stream
            bis = new BufferedInputStream(new FileInputStream(file));
            String entryPath = FileHelper.getRelativePath(archive.getParentFile(), file);

            ZipEntry entry = new ZipEntry(entryPath);
            entry.setMethod(ZipEntry.STORED);
            entry.setCompressedSize(file.length());
            entry.setSize(file.length());
            entry.setCrc(crc.getValue());
            zos.putNextEntry(entry);
            while ((bytesRead = bis.read(buffer)) != -1) {
                zos.write(buffer, 0, bytesRead);
            }
        } catch (FileNotFoundException e) {

        } catch (IOException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        } finally {
            IOUtils.closeQuietly(bis);
        }
    }
    IOUtils.closeQuietly(zos);
    return archive;
}

From source file:brut.directory.ZipUtils.java

private static void processFolder(final File folder, final ZipOutputStream zipOutputStream,
        final int prefixLength) throws BrutException, IOException {
    for (final File file : folder.listFiles()) {
        if (file.isFile()) {
            final String cleanedPath = BrutIO.sanitizeUnknownFile(folder,
                    file.getPath().substring(prefixLength));
            final ZipEntry zipEntry = new ZipEntry(BrutIO.normalizePath(cleanedPath));

            // aapt binary by default takes in parameters via -0 arsc to list extensions that shouldn't be
            // compressed. We will replicate that behavior
            final String extension = FilenameUtils.getExtension(file.getAbsolutePath());
            if (mDoNotCompress != null
                    && (mDoNotCompress.contains(extension) || mDoNotCompress.contains(zipEntry.getName()))) {
                zipEntry.setMethod(ZipEntry.STORED);
                zipEntry.setSize(file.length());
                BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(file));
                CRC32 crc = BrutIO.calculateCrc(unknownFile);
                zipEntry.setCrc(crc.getValue());
                unknownFile.close();//w  w w .jav a  2 s.  c o m
            } else {
                zipEntry.setMethod(ZipEntry.DEFLATED);
            }

            zipOutputStream.putNextEntry(zipEntry);
            try (FileInputStream inputStream = new FileInputStream(file)) {
                IOUtils.copy(inputStream, zipOutputStream);
            }
            zipOutputStream.closeEntry();
        } else if (file.isDirectory()) {
            processFolder(file, zipOutputStream, prefixLength);
        }
    }
}

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 a va 2 s .  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:net.sf.jabref.exporter.OpenDocumentSpreadsheetCreator.java

private static void storeOpenDocumentSpreadsheetFile(File file, InputStream source) throws Exception {

    try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) {

        //addResourceFile("mimetype", "/resource/ods/mimetype", out);
        ZipEntry ze = new ZipEntry("mimetype");
        String mime = "application/vnd.oasis.opendocument.spreadsheet";
        ze.setMethod(ZipEntry.STORED);
        ze.setSize(mime.length());// w w  w  .j ava 2 s .  c  o m
        CRC32 crc = new CRC32();
        crc.update(mime.getBytes());
        ze.setCrc(crc.getValue());
        out.putNextEntry(ze);
        for (int i = 0; i < mime.length(); i++) {
            out.write(mime.charAt(i));
        }
        out.closeEntry();

        ZipEntry zipEntry = new ZipEntry("content.xml");
        //zipEntry.setMethod(ZipEntry.DEFLATED);
        out.putNextEntry(zipEntry);
        int c;
        while ((c = source.read()) >= 0) {
            out.write(c);
        }
        out.closeEntry();

        // Add manifest (required for OOo 2.0) and "meta.xml": These are in the
        // resource/ods directory, and are copied verbatim into the zip file.
        OpenDocumentSpreadsheetCreator.addResourceFile("meta.xml", "/resource/ods/meta.xml", out);

        OpenDocumentSpreadsheetCreator.addResourceFile("META-INF/manifest.xml", "/resource/ods/manifest.xml",
                out);
    }
}