How to read from zipped xml files in Scala code?

自闭症网瘾萝莉.ら 提交于 2019-12-03 17:43:11

问题


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.



来源:https://stackoverflow.com/questions/5153544/how-to-read-from-zipped-xml-files-in-scala-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!