23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

}<br />

public static void main(String[] args) {<br />

for (int i = 0; i < args.length; i++) {<br />

try {<br />

JarFile jf = new JarFile(args[i]);<br />

}<br />

Enumeration e = jf.entries();<br />

while (e.hasMoreElements()) {<br />

JarEntry je = (JarEntry) e.nextElement();<br />

String name = je.getName();<br />

Date lastModified = new Date(je.getTime());<br />

long uncompressedSize = je.getSize();<br />

long compressedSize = je.getCompressedSize();<br />

long crc = je.getCrc();<br />

int method = je.getMethod();<br />

String comment = je.getComment();<br />

}<br />

<strong>Java</strong> I/O<br />

if (method == ZipEntry.STORED) {<br />

System.out.println(name + " was stored at " + lastModified);<br />

System.out.println("with a size of " + uncompressedSize<br />

+ " bytes");<br />

}<br />

else if (method == ZipEntry.DEFLATED) {<br />

System.out.println(name + " was deflated at " + lastModified);<br />

System.out.println("from " + uncompressedSize + " bytes to "<br />

+ compressedSize + " bytes, a savings of "<br />

+ (100.0 - 100.0*compressedSize/uncompressedSize) + "%");<br />

}<br />

else {<br />

System.out.println(name<br />

+ " was compressed using an unrecognized method at "<br />

+ lastModified);<br />

System.out.println("from " + uncompressedSize + " bytes to "<br />

+ compressedSize + " bytes, a savings of "<br />

+ (100.0 - 100.0*compressedSize/uncompressedSize) + "%");<br />

}<br />

System.out.println("Its CRC is " + crc);<br />

if (comment != null && !comment.equals("")) {<br />

System.out.println(comment);<br />

}<br />

if (je.isDirectory()) {<br />

System.out.println(name + " is a directory");<br />

}<br />

Attributes a = je.getAttributes();<br />

if (a != null) {<br />

Object[] nameValuePairs = a.entrySet().toArray();<br />

for (int j = 0; j < nameValuePairs.length; j++) {<br />

System.out.println(nameValuePairs[j]);<br />

}<br />

}<br />

System.out.println();<br />

}<br />

catch (<strong>IO</strong>Exception e) {<br />

System.err.println(e);<br />

e.printStackTrace();<br />

}<br />

184

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!