forked from molgenis/molgenis-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
776 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package org.molgenis.io; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.Reader; | ||
import java.nio.charset.Charset; | ||
import java.util.Enumeration; | ||
import java.util.Iterator; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipFile; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import org.molgenis.io.csv.CsvReader; | ||
|
||
import com.google.common.base.Function; | ||
import com.google.common.collect.Iterators; | ||
|
||
public class ZipTableReader implements TableReader | ||
{ | ||
private final ZipFile zipFile; | ||
private final Map<String, ZipEntry> tableNameMap; | ||
|
||
public ZipTableReader(ZipFile zipFile) throws IOException | ||
{ | ||
if (zipFile == null) throw new IllegalArgumentException("zip file is null"); | ||
this.zipFile = zipFile; | ||
|
||
// init table name map | ||
tableNameMap = new LinkedHashMap<String, ZipEntry>(); | ||
for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements();) | ||
{ | ||
ZipEntry zipEntry = e.nextElement(); | ||
|
||
// remove extension from filename | ||
int pos = zipEntry.getName().lastIndexOf('.'); | ||
String tableName = pos != -1 ? zipEntry.getName().substring(0, pos) : zipEntry.getName(); | ||
|
||
tableNameMap.put(tableName, zipEntry); | ||
} | ||
} | ||
|
||
@Override | ||
public Iterator<TupleReader> iterator() | ||
{ | ||
return Iterators.transform(tableNameMap.values().iterator(), new Function<ZipEntry, TupleReader>() | ||
{ | ||
@Override | ||
@Nullable | ||
public TupleReader apply(@Nullable | ||
ZipEntry zipEntry) | ||
{ | ||
try | ||
{ | ||
return toTupleReader(zipEntry); | ||
} | ||
catch (IOException e) | ||
{ | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException | ||
{ | ||
// noop | ||
} | ||
|
||
@Override | ||
public TupleReader getTupleReader(String tableName) throws IOException | ||
{ | ||
ZipEntry zipEntry = tableNameMap.get(tableName); | ||
return zipEntry != null ? toTupleReader(zipEntry) : null; | ||
} | ||
|
||
@Override | ||
public Iterable<String> getTableNames() throws IOException | ||
{ | ||
return tableNameMap.keySet(); | ||
} | ||
|
||
private TupleReader toTupleReader(ZipEntry zipEntry) throws IOException | ||
{ | ||
String name = zipEntry.getName(); | ||
if (name.endsWith(".csv") || name.endsWith(".txt")) | ||
{ | ||
Reader reader = new InputStreamReader(zipFile.getInputStream(zipEntry), Charset.forName("UTF-8")); | ||
return new CsvReader(reader); | ||
} | ||
else if (name.endsWith(".tsv")) | ||
{ | ||
Reader reader = new InputStreamReader(zipFile.getInputStream(zipEntry), Charset.forName("UTF-8")); | ||
return new CsvReader(reader, '\t'); | ||
} | ||
else | ||
{ | ||
throw new IOException("unknown file type: " + name); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.