Example usage for java.util.zip ZipEntry getMethod

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

Introduction

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

Prototype

public int getMethod() 

Source Link

Document

Returns the compression method of the entry.

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 w w.j  ava  2s. c om
}

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.DEFLATED);
    }//w ww  . j  a  v a2  s  .  com
}

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 a2 s.c  om*/
            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 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 {/*  ww w. j a v a  2  s . com*/
            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) {

    try {//  www  .  j a v a2 s . co m
        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:com.taobao.android.builder.tools.sign.LocalSignHelper.java

private static Predicate<String> getNoCompressPredicate(String srcPath) throws IOException {
    Set<String> noCompressEntries = new HashSet<>();
    ZipFile zipFile = new ZipFile(srcPath);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry zipEntry = entries.nextElement();
        if (zipEntry.getMethod() == 0) {
            if (zipEntry.getName().endsWith(".so")) {
                nativeLibrariesPackagingMode = NativeLibrariesPackagingMode.UNCOMPRESSED_AND_ALIGNED;
            }/*from  w ww.  j a v  a 2  s .c  om*/
            noCompressEntries.add(zipEntry.getName());
        }
    }
    return s -> noCompressEntries.contains(s);
}

From source file:com.taobao.android.builder.tools.sign.LocalSignHelper.java

private static Predicate<String> getNoCompressPredicate(File inputFile) throws IOException {
    List<String> paths = new ArrayList<>();
    ZipFile zFile = new ZipFile(inputFile);
    Enumeration<? extends ZipEntry> enumeration = zFile.entries();
    while (enumeration.hasMoreElements()) {
        ZipEntry zipEntry = enumeration.nextElement();
        if (zipEntry.getMethod() == 0) {
            paths.add(zipEntry.getName());
        }//  w w w .  j a  va2 s.c  o  m
    }
    return s -> paths.contains(s);

}

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

public static void rezip(File output, File srcDir, Map<String, ZipEntry> zipEntryMethodMap) throws Exception {
    if (output.isDirectory()) {
        throw new IOException("This is a directory!");
    }//from  w ww  . ja v  a 2 s .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(ze.getName());
            if (originEntry != null) {
                if (originEntry.getMethod() == STORED) {
                    ze.setCompressedSize(f.length());
                    InputStream in = new BufferedInputStream(new FileInputStream(f));
                    try {
                        CRC32 crc = new CRC32();
                        int c;
                        while ((c = in.read()) != -1) {
                            crc.update(c);
                        }
                        ze.setCrc(crc.getValue());
                    } finally {
                        in.close();
                    }
                }
                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.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   w ww.  j av  a  2  s  .  c om*/
    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.diffplug.gradle.ZipMisc.java

/**
 * Modifies only the specified entries in a zip file. 
 *
 * @param input       a source from a zip file
 * @param output      an output to a zip file
 * @param toModify      a map from path to an input stream for the entries you'd like to change
 * @param toOmit      a set of entries you'd like to leave out of the zip
 * @throws IOException/*from   w w w.jav a  2  s.  c om*/
 */
public static void modify(ByteSource input, ByteSink output, Map<String, Function<byte[], byte[]>> toModify,
        Predicate<String> toOmit) throws IOException {
    try (ZipInputStream zipInput = new ZipInputStream(input.openBufferedStream());
            ZipOutputStream zipOutput = new ZipOutputStream(output.openBufferedStream())) {
        while (true) {
            // read the next entry
            ZipEntry entry = zipInput.getNextEntry();
            if (entry == null) {
                break;
            }

            Function<byte[], byte[]> replacement = toModify.get(entry.getName());
            if (replacement != null) {
                byte[] clean = ByteStreams.toByteArray(zipInput);
                byte[] modified = replacement.apply(clean);
                // if it's the entry being modified, enter the modified stuff
                try (InputStream replacementStream = new ByteArrayInputStream(modified)) {
                    ZipEntry newEntry = new ZipEntry(entry.getName());
                    newEntry.setComment(entry.getComment());
                    newEntry.setExtra(entry.getExtra());
                    newEntry.setMethod(entry.getMethod());
                    newEntry.setTime(entry.getTime());

                    zipOutput.putNextEntry(newEntry);
                    copy(replacementStream, zipOutput);
                }
            } else if (!toOmit.test(entry.getName())) {
                // if it isn't being modified, just copy the file stream straight-up
                ZipEntry newEntry = new ZipEntry(entry);
                newEntry.setCompressedSize(-1);
                zipOutput.putNextEntry(newEntry);
                copy(zipInput, zipOutput);
            }

            // close the entries
            zipInput.closeEntry();
            zipOutput.closeEntry();
        }
    }
}