This post originated from an RSS feed registered with Scala Buzz
by Zemian Deng.
Original Post: Extracting zip/jar files consitently into a folder
Feed Title: thebugslayer
Feed URL: http://www.jroller.com/thebugslayer/feed/entries/atom?cat=%2FScala+Programming
Feed Description: Notes on Scala and Sweet web framework
Most of the unzip utility gives your options of "extract here", or "extract "
when you want to extract a zip. Depending on the author of the zip file, sometimes they include
the basename in their entry path, sometime they don't. If they don't then you get the content liter
in your directory, or if they do, you might get double basename when extracted. So there is
always some clean up unless you konw the content ahead of time. Here is a utility written in Scala
that will check basename exists or not when extracting, and thus give you a consistent extraction
of the archive. You may use the script to expand jar files as well.
// extract.scala
import java.io._
import java.util.jar._
def copyStream(istream : InputStream, ostream : OutputStream) : Unit = {
var bytes = new Array[Byte](1024)
var len = -1
while({ len = istream.read(bytes, 0, 1024); len != -1 })
ostream.write(bytes, 0, len)
}
def extractJar(file : File) : Unit = {
val basename = file.getName.substring(0, file.getName.lastIndexOf("."))
val todir = new File(file.getParentFile, basename)
todir.mkdirs()
println("Extracting " + file + " to " + todir)
val jar = new JarFile(file)
val enu = jar.entries
while(enu.hasMoreElements){
val entry = enu.nextElement
val entryPath =
if(entry.getName.startsWith(basename)) entry.getName.substring(basename.length)
else entry.getName
println("Extracting to " + todir + "/" + entryPath)
if(entry.isDirectory){
new File(todir, entryPath).mkdirs
}else{
val istream = jar.getInputStream(entry)
val ostream = new FileOutputStream(new File(todir, entryPath))
copyStream(istream, ostream)
ostream.close
istream.close
}
}
}
//Extract single zip/jar files or all of them in a directory.
def accept(file : File) = List(".zip", ".jar").find{ ext => file.getName.endsWith(ext) } != None
args.foreach{ n =>
val fn = new File(n)
if(fn.isDirectory){
val files = fn.listFiles.filter{ f => accept(f) }
files.foreach{ f => extractJar(f) }
}else if(accept(fn))
extractJar(fn)
}
Note that script will take a directory of archive files or multiple files directly. Here is a example
usage.
# expand Scala package
$ scala extract.scala scala-2.7.2.final.zip
# expand all Scala source files, which were packaged in jar files.
$ scala extract.scala scala-2.7.2.final/src