Doug Cutting wrote:
It would be useful to have a command-line utility (i.e., a static main(String[]) method somewhere) that lists the files and sizes contained inside a CFS file, and perhaps even an option to unpack it. Anyone care to contribute this method?

Here's a diff to add this functionality to CompoundFileReader. Comments are of course welcome, as I'm not that fantastic a Java hacker.


-garrett
Index: src/java/org/apache/lucene/index/CompoundFileReader.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-lucene/src/java/org/apache/lucene/index/CompoundFileReader.java,v
retrieving revision 1.14
diff -u -r1.14 CompoundFileReader.java
--- src/java/org/apache/lucene/index/CompoundFileReader.java    28 Sep 2004 
18:15:52 -0000      1.14
+++ src/java/org/apache/lucene/index/CompoundFileReader.java    25 Dec 2004 
04:25:11 -0000
@@ -17,12 +17,14 @@
  */
 
 import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FSDirectory;
 import org.apache.lucene.store.IndexInput;
 import org.apache.lucene.store.BufferedIndexInput;
 import org.apache.lucene.store.IndexOutput;
 import org.apache.lucene.store.Lock;
 import java.util.HashMap;
 import java.io.IOException;
+import java.io.FileOutputStream;
 
 
 /**
@@ -233,5 +235,61 @@
         }
 
 
+    }
+
+    public static void main(String [] args) {
+        String dirname = null, filename = null;
+        boolean extract = false;
+
+        for (int i = 0; i < args.length; ++i) {
+            if (args[i].equals("-extract")) {
+                extract = true;
+            } else if (dirname == null) {
+                dirname = args[i];
+            } else if (filename == null) {
+                filename = args[i];
+            }
+        }
+
+        if (dirname == null || filename == null) {
+            System.out.println("Usage: CompoundFileReader directory cfsfile");
+            System.out.println("");
+            System.out.println("Prints the filename and size of each file "
+                               + "within cfsfile.");
+            System.out.println("");
+            System.out.println("Add the -extract flag to extract files to the "
+                               + "current working directory.");
+
+            return;
+        }
+
+        try {
+            Directory dir = FSDirectory.getDirectory(dirname, false);
+
+            CompoundFileReader cfr = new CompoundFileReader(dir, filename);
+
+            String [] files = cfr.list();
+
+            for (int i = 0; i < files.length; ++i) {
+                long len = cfr.fileLength(files[i]);
+
+                System.out.println(files[i] + "\t: " + len + " bytes");
+
+                if (extract) {
+                    IndexInput ii = cfr.openInput(files[i]);
+
+                    FileOutputStream f = new FileOutputStream(files[i]);
+
+                    while (len-- != 0) {
+                        byte b = ii.readByte();
+                        f.write(b);
+                    }
+
+                    f.close();
+                }
+            }
+        } catch (IOException ioe) {
+            ioe.printStackTrace();
+        }
     }
 }

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to