Kamesh Rao Yeduvakula
Kamesh Rao Yeduvakula

Reputation: 1205

How to read from zipped xml files in Scala code?

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

Answers (3)

Terje Sten Bjerkseth
Terje Sten Bjerkseth

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

Aaron Novstrup
Aaron Novstrup

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

Bernhard
Bernhard

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

Related Questions