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.