The Artima Developer Community
Sponsored Link

Scala Buzz
Extracting zip/jar files consitently into a folder

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Zemian Deng

Posts: 49
Nickname: zdeng
Registered: Jan, 2008

Zemian Deng is the creator of SweetScala web framework
Extracting zip/jar files consitently into a folder Posted: Dec 13, 2008 3:39 PM
Reply to this message Reply

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
Latest Scala Buzz Posts
Latest Scala Buzz Posts by Zemian Deng
Latest Posts From thebugslayer

Advertisement
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

Read: Extracting zip/jar files consitently into a folder

Topic: Thinking in Scala vs Erlang Previous Topic   Next Topic Topic: Java Just Died (no closures in Java 7)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use