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?
How to read from zipped xml files in Scala code?
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))) }
Thanks a lot. This one really helped the most. I pasted an implicit method code for converting the Java Enumeration to Scala list. collection.JavaConverters._ and asScala() helped reduce the code complexity. A lot of really helpful examples for both XML and ZIP file reading in scala. Thanks a ton. –
Parthenogenesis
You can use the Java package java.util.zip: http://download.oracle.com/javase/6/docs/api/java/util/zip/package-summary.html
Which does have gzip (though not tar.gz) support, as the OP's tag requests. –
Rectangular
I see no 'tar' here, just 'gzip'. :) A GZIPInputStream should be just what the doctor ordered. Or, if it's actually a PKZIP file, something else in the same package will work (with an extra helping of accidental complexity) –
Jamilla
hmmmm...any quick sample code to look at? Am jut being a little lazy, thats it. –
Parthenogenesis
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.
© 2022 - 2024 — McMap. All rights reserved.