#103 sys::Zip API

brian Sat 19 Aug 2006

I checked in the sys::Zip API which is designed to provide Fan's coverage of java.util.zip - something needed for the pure Fan compiler. I collapsed everything into one class in the spirit of Fan's philosophy. Plus I always get confused trying to figure out how to write with java.io.ZipFile (you have to use ZipOutputStream), having just one Zip class should be make discovery much easier. I reuse sys::File where Java uses ZipEntry (I was already doing this to some extent for reading files out of a Pod). It's a simple concise API:

final class Zip
{
  static Zip open(File file)
  static Zip read(InStream out)
  static Zip write(OutStream out)

  File file()
  Uri:File contents()
  File readNext()
  OutStream writeNext(Uri path, Time modifyTime := Time.now)
  Bool finish()
  Bool close()
  override Str toStr()
}

Zip may be used in three modes:

Zip.open() is used to read a random access file and provides access to the entire contents with the ability to read select entries:

zip := Zip.open(File.make(`test.zip`))
txt := zip.contents[`/notice.txt`].readAllStr
zip.close

Zip.read() is used to read a zip file from an input stream. Each entry is pulled off the stream using readNext():

zip := Zip.read(File.make(`test.zip`).in)
File entry
while ((entry = zip.readNext()) != null)
{
  data := entry.readAllBuf
  echo("$entry size=$data.size")
}
zip.close

Zip.write() is used to write a zip file to an output stream. Each entry must is written to the stream using writeNext():

zip := Zip.write(File.make(`test.zip`).out)
out := zip.writeNext(`/path/hello.txt`)
out.writeLine("hello zip")
out.close
zip.close

Login or Signup to reply.