You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by od...@apache.org on 2009/05/13 14:27:55 UTC

svn commit: r774290 - /harmony/enhanced/jdktools/trunk/modules/jdktools/src/main/java/org/apache/harmony/tools/jar/Main.java

Author: odeakin
Date: Wed May 13 12:27:55 2009
New Revision: 774290

URL: http://svn.apache.org/viewvc?rev=774290&view=rev
Log:
Apply patch for HARMONY-6208 (Partial implementation of extractJar method for Jar tool).

Modified:
    harmony/enhanced/jdktools/trunk/modules/jdktools/src/main/java/org/apache/harmony/tools/jar/Main.java

Modified: harmony/enhanced/jdktools/trunk/modules/jdktools/src/main/java/org/apache/harmony/tools/jar/Main.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/jdktools/trunk/modules/jdktools/src/main/java/org/apache/harmony/tools/jar/Main.java?rev=774290&r1=774289&r2=774290&view=diff
==============================================================================
--- harmony/enhanced/jdktools/trunk/modules/jdktools/src/main/java/org/apache/harmony/tools/jar/Main.java (original)
+++ harmony/enhanced/jdktools/trunk/modules/jdktools/src/main/java/org/apache/harmony/tools/jar/Main.java Wed May 13 12:27:55 2009
@@ -19,6 +19,7 @@
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
@@ -107,10 +108,75 @@
     /**
      * Extracts a jar file with the specified arguments
      */
-    private static void extractJar(String[] args) {
-        System.out.println("Error: Jar extraction not yet implemented");
-    }
+    private static void extractJar(String[] args) throws Exception {
+        boolean verboseFlag = false, fileFlag = false;
+
+        // Check for expected and unexpected flags
+        for (int i=1; i<args[0].length(); i++) {
+            switch (args[0].charAt(i)) {
+            case 'v':
+                verboseFlag = true;
+                break;
+            case 'f':
+                fileFlag = true;
+                break;
+            default:
+                System.out.println("Error: Illegal option for -x: '"+args[0].charAt(i)+"'");
+                return;
+            }
+        }
+
+        if (fileFlag && args.length<2) {
+            // Options specify 'f' but there is no filename present
+            System.out.println("Error: No file name specified for 'f' option");
+            return;
+        }
+    	
+    	ZipInputStream zis;
+        if (!fileFlag) {
+            // Read from stdin
+            if (verboseFlag) System.out.println("Reading input from stdin");
+            zis = new ZipInputStream(System.in);
+        } else {
+            // Read from the specified file
+            if (verboseFlag) System.out.println("Reading jar file: "+args[1]);
+            zis = new ZipInputStream(new FileInputStream(new File(args[1])));
+        }
 
+        // Read the zip entries - format and print their data
+        ZipEntry ze;
+        String workingDir = System.getProperty("user.dir");
+        
+        while ((ze = zis.getNextEntry()) != null) {
+        	if (ze.isDirectory()) {
+        		// create a new directory of the same name
+       			File newDir = new File(ze.getName());
+    			if (! newDir.exists()) {
+    				newDir.mkdirs();        				
+    			}
+    	        if (verboseFlag) {
+    	        	System.out.println("created: " + ze.getName());
+    	        }
+        	} else {
+        		// extract the file to the appropriate directory
+        		File f = new File(workingDir + File.separator + ze.getName());
+        		f.createNewFile();
+        		FileOutputStream fos = new FileOutputStream(f);
+        		int i;
+        		while ((i = zis.read()) != -1) {
+        			fos.write(i);
+        		}
+            	fos.close();
+            	if (verboseFlag) {
+            		System.out.println("inflated: " + ze.getName());
+            	}
+        	}           
+            zis.closeEntry();
+        }
+
+        zis.close();
+    }
+    	
     /**
      * Lists contents of jar file with the specified arguments
      */