Java by API/java.util.zip/ZipEntry
Содержание
new ZipEntry(String name)
<source lang="java">
import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main {
public static void main(String[] argv) throws Exception { String[] filenames = new String[] { "filename1", "filename2" }; byte[] buf = new byte[1024]; String outFilename = "outfile.zip"; ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename)); for (int i = 0; i < filenames.length; i++) { FileInputStream in = new FileInputStream(filenames[i]); out.putNextEntry(new ZipEntry(filenames[i])); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } out.close(); }
}
</source>
ZipEntry.DEFLATED
<source lang="java">
import java.util.Date; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class Main {
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 { 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) + "%"); } } }
}
</source>
ZipEntry: getComment()
<source lang="java">
import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class Main {
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(); long crc = ze.getCrc(); System.out.println("Its CRC is " + crc); String comment = ze.getComment(); if (comment != null && !comment.equals("")) { System.out.println(comment); } if (ze.isDirectory()) { System.out.println(name + " is a directory"); } } }
}
</source>
ZipEntry: getCompressedSize()
<source lang="java">
import java.io.IOException; import java.util.Date; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class Main {
public static void main(String[] args) { try { 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(); System.out.println(name); System.out.println(lastModified); System.out.println(uncompressedSize); System.out.println(compressedSize); } } catch (IOException ex) { System.err.println(ex); } }
}
</source>
ZipEntry: getCrc()
<source lang="java">
import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class Main {
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(); long crc = ze.getCrc(); System.out.println("Its CRC is " + crc); String comment = ze.getComment(); if (comment != null && !comment.equals("")) { System.out.println(comment); } if (ze.isDirectory()) { System.out.println(name + " is a directory"); } } }
}
</source>
ZipEntry: getName()
<source lang="java">
import java.io.FileInputStream; import java.util.Date; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Main {
public static void main(String[] args) throws Exception { FileInputStream fis = new FileInputStream(args[0]); ZipInputStream zis = new ZipInputStream(fis); ZipEntry ze; while ((ze = zis.getNextEntry()) != null) System.out.println(ze.getName() + " [" + ze.getSize() + "] [" + new Date(ze.getTime()).toString() + "]"); }
}
</source>
ZipEntry: getSize()
<source lang="java">
import java.io.FileInputStream; import java.util.Date; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Main {
public static void main(String[] args) throws Exception { FileInputStream fis = new FileInputStream(args[0]); ZipInputStream zis = new ZipInputStream(fis); ZipEntry ze; while ((ze = zis.getNextEntry()) != null) System.out.println(ze.getName() + " [" + ze.getSize() + "] [" + new Date(ze.getTime()).toString() + "]"); }
}
</source>
ZipEntry: getTime()
<source lang="java">
import java.io.FileInputStream; import java.util.Date; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Main {
public static void main(String[] args) throws Exception { FileInputStream fis = new FileInputStream(args[0]); ZipInputStream zis = new ZipInputStream(fis); ZipEntry ze; while ((ze = zis.getNextEntry()) != null) System.out.println(ze.getName() + " [" + ze.getSize() + "] [" + new Date(ze.getTime()).toString() + "]"); }
}
</source>
ZipEntry: isDirectory()
<source lang="java">
import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class Main {
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(); long crc = ze.getCrc(); System.out.println("Its CRC is " + crc); String comment = ze.getComment(); if (comment != null && !comment.equals("")) { System.out.println(comment); } if (ze.isDirectory()) { System.out.println(name + " is a directory"); } } }
}
</source>
ZipEntry.STORED
<source lang="java">
import java.util.Date; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class Main {
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 { 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) + "%"); } } }
}
</source>