How to read from zipped xml files in Scala code?

2019-03-19 07:45发布

问题:

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?

回答1:

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))) }


回答2:

You can use the Java package java.util.zip: http://download.oracle.com/javase/6/docs/api/java/util/zip/package-summary.html



回答3:

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.