JAR Archives: Jar Lister, Java example Java code example - Click here to copy ->>>
Can't find what you're looking for? Try our search:
|
|
Really working examples categorized by API, package, class. You can compile and run our examples right away!
Not from source code for Java projects - only working examples! Copy, compile and run!
|
------------------
/* Title: Hardcore Java
* Title: Java I/O
* Second Edition: May 2006
* ISBN 10: 0-596-52750-0
| Code: |
import java.util.*;
import java.util.zip.*;
import java.util.jar.*;
import java.io.*;
public class JarLister {
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();
long crc = je.getCrc();
int method = je.getMethod();
String comment = je.getComment();
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) + "%");
}
System.out.println("Its CRC is " + crc);
if (comment != null && !comment.equals("")) {
System.out.println(comment);
}
if (je.isDirectory()) {
System.out.println(name + " is a directory");
}
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();
}
}
}
|
|
|
References.
The list of classes which were used on this page you can find below. The
links to Java API contain official SUN documentation about all used classes.
[ Go Back ]
|