import java.util.zip.*; // for Zip classes import java.util.*; // for Date import java.io.*; // for File, IOException public class ZipInfo { protected long totalBytes = 0; // uncompressed bytes in ZIP protected long zipFileLength; //------------------------------------------------------------------- // static main delegates all work to object so we're not stuck in a // "static universe" //------------------------------------------------------------------- public static void main (String[] args) { try { new ZipInfo().go(args); } catch (IOException ioError) { System.out.println("An I/O error occurred: " + ioError); } } //------------------------------------------------------------------- // ZipInfo main entry point. //------------------------------------------------------------------- protected void go(String[] args) throws IOException { File file; String tentativeFilename, zipFilename = null; ZipFile zip = null; ZipEntry entry; if ( args.length != 1 ) { System.out.println( "Please specify a JAR or ZIP filename (with or without extension)."); System.exit(10); } // grab sole command line argument tentativeFilename = args[0]; // turn it into definitive filename zipFilename = completeFilename(tentativeFilename); file = new File(zipFilename); zipFileLength = file.length(); // open ZIP-compatible file try { zip = new ZipFile(file); } catch (ZipException zipError) { System.out.print("ERROR: Problem with zip file: "); System.out.println(zipError); System.exit(10); } System.out.println(""); System.out.println("File : "+ zipFilename); System.out.println(""); // dump neat report on all entries System.out.println("Filename\t\tSize\tModification Date"); System.out.println("--------\t\t----\t-----------------"); Enumeration zipEntries = zip.entries(); while ( zipEntries.hasMoreElements() ) { entry = (ZipEntry) zipEntries.nextElement(); dumpInfo(entry); } System.out.println(""); // summarize compression efficiency float ratio = 100 - ((float)zipFileLength / totalBytes * 100); System.out.println("Total bytes ..... : " + totalBytes); System.out.println("Compressed to ... : " + zipFileLength); System.out.println("Compression ratio : " + ratio + " %"); } //------------------------------------------------------------------- // Dump summary information for a ZIP entry. //------------------------------------------------------------------- protected void dumpInfo(ZipEntry entry) { String entryName = entry.getName(); String padding; long size = entry.getSize(); // pad the entry name out with spaces to align columns neatly. padding = columnPadding(3*8, entryName); System.out.print(entryName + padding + size + "\t"); System.out.print( new Date(entry.getTime()) ); System.out.println(""); String comment = entry.getComment(); if ( comment != null ) { System.out.println("\tcomment : " + comment); } totalBytes += size; // keep track of total uncompressed bytes } //------------------------------------------------------------------- // Pad a text column out to a certain width //------------------------------------------------------------------- private static String spaces = " "; public static String columnPadding(int columnWidth, String string) { int numberOfPaddingSpaces; numberOfPaddingSpaces = columnWidth - string.length(); // if no padding required, or string is wider than column, return // a single space so that columns dont "stick" to each other. if ( numberOfPaddingSpaces < 1 ) { numberOfPaddingSpaces = 1; } return spaces.substring(0, numberOfPaddingSpaces); } //------------------------------------------------------------------- // Determine filename //------------------------------------------------------------------- protected String completeFilename(String tentativeFilename) { String zipFilename = null; boolean zipExtension = tentativeFilename.endsWith(".zip"); boolean jarExtension = tentativeFilename.endsWith(".jar"); boolean noExtensionAtAll = (! zipExtension) && (! jarExtension); if ( noExtensionAtAll ) { if ( fileExists(tentativeFilename + ".zip")) { zipFilename = tentativeFilename + ".zip"; } else if ( fileExists(tentativeFilename + ".jar")) { zipFilename = tentativeFilename + ".jar"; } } else { if ( fileExists(tentativeFilename) ) { zipFilename = tentativeFilename; } } if ( zipFilename == null ) { System.out.print("Sorry, could not find file '"); if ( noExtensionAtAll ) { System.out.println(tentativeFilename + "[.jar][.zip]'"); } else { System.out.println(tentativeFilename + "'"); } System.exit(10); } return zipFilename; } //------------------------------------------------------------------- // See if a given file exists //------------------------------------------------------------------- public static boolean fileExists (String filename) { File file = new File(filename); if ( !file.exists() ) { return false; } return ! file.isDirectory(); // dont let directories trip us up } } // End of Class ZipInfo