Commits

Miki Tebeka committed 855f8c0

works with many files

Comments (0)

Files changed (3)

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

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

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");
 

src/test/resources/2.zip

Binary file added.