Reputation: 1205
How do I access XML data files directly from a zipped file in my Scala program? Are there any direct ways to programmatically unzip and read contents in my Scala code?
Upvotes: 9
Views: 7834
Reputation: 683
Here are a couple of ways of doing it in 2.8.1:
cat > root.xml << EOF
<ROOT>
<id>123</id>
</ROOT>
EOF
zip root root.xml
and then in the REPL:
val rootzip = new java.util.zip.ZipFile("root.zip")
import collection.JavaConverters._
val entries = rootzip.entries.asScala
entries foreach { e =>
val x = scala.xml.XML.load(rootzip.getInputStream(e))
println(x)
}
or something like:
val rootzip = new java.util.zip.ZipFile("root.zip")
import scala.collection.JavaConversions._
rootzip.entries.
filter (_.getName.endsWith(".xml")).
foreach { e => println(scala.xml.XML.load(rootzip.getInputStream(e))) }
Upvotes: 17
Reputation: 21017
I personally prefer TrueZip. It allows you to treat archive files as a virtual file system, providing the same interface as standard Java file I/O.
Upvotes: 4
Reputation: 8821
You can use the Java package java.util.zip: http://download.oracle.com/javase/6/docs/api/java/util/zip/package-summary.html
Upvotes: 5