Commits

Miki Tebeka  committed 25863d4 Merge

Merge with multi

  • Participants
  • Parent commits 0d62fc3, 855f8c0

Comments (0)

Files changed (4)

 syntax: glob
 
+.classpath
+.project
 target
 README.html

File src/main/java/com/mikitebeka/mapred/ZipLineIterator.java

 public class ZipLineIterator implements Iterator<String> {
 
     private ZipInputStream zin = null;
-    private String name;
+    ZipEntry entry = null;
+    private String nextLine = null;
+    private BufferedReader reader = null;
     private int pos = 0;
-    private long size;
-    private String nextLine;
-    private BufferedReader reader;
 
     public ZipLineIterator(InputStream in) throws IOException {
         zin = new ZipInputStream(in);
-        ZipEntry entry = zin.getNextEntry();
+
+        nextEntry();
+    }
+
+    private void nextEntry() throws IOException {
+        nextLine = null;
+
+        if (entry != null) {
+            zin.closeEntry();
+        }
+
+        entry = zin.getNextEntry();
         if (entry == null) {
             return;
         }
 
-        size = entry.getSize();
-        name = entry.getName();
         reader = new BufferedReader(new InputStreamReader(zin));
-
         nextLine = reader.readLine();
     }
 
     @Override
     public String next() {
         String line = nextLine;
+        pos += (line == null) ? 0 : line.length();
+
         try {
             nextLine = reader.readLine();
+            if (nextLine == null) {
+                nextEntry();
+            }
         } catch (IOException e) {
             nextLine = null;
         }
      * @return the name
      */
     public String getName() {
-        return name;
+        return (entry == null) ? null : entry.getName();
     }
 
     public void close() throws IOException {
     }
 
     public float getProgress() {
-        if (size == 0) {
-            return 1.0f;
-        }
-
-        return (float) (((double) pos) / size);
+        // FIXME: Find a good way to get total zip size from ZipInputStream
+        return 0.5f;
     }
 
     /**

File src/test/java/com/mikitebeka/mapred/ZipLineReaderTest.java

         return new ZipLineIterator(in);
     }
 
-    public void TestSmallFile() throws IOException {
-        ZipLineIterator it = getZip("3.zip");
+    private ArrayList<String> readLines(ZipLineIterator it) {
         ArrayList<String> lines = new ArrayList<String>();
         while (it.hasNext()) {
             lines.add(it.next());
         }
 
+        return lines;
+    }
+
+    public void TestSmallFile() throws IOException {
+        ZipLineIterator it = getZip("3.zip");
+        ArrayList<String> lines = readLines(it);
+
         @SuppressWarnings("serial")
         ArrayList<String> expected = new ArrayList<String>() {
             {
         Assert.assertTrue(lines.equals(expected));
     }
 
+    public void TestMultiFiles() throws IOException {
+        ZipLineIterator it = getZip("2.zip");
+        ArrayList<String> lines = readLines(it);
+
+        @SuppressWarnings("serial")
+        ArrayList<String> expected = new ArrayList<String>() {
+            {
+                add("1");
+                add("2");
+                add("3");
+                add("a");
+                add("b");
+                add("c");
+            }
+        };
+
+        Assert.assertTrue(lines.equals(expected));
+    }
+
     public void TestBigFile() throws IOException {
         ZipLineIterator it = getZip("big.zip");
 

File src/test/resources/2.zip

Binary file added.