Reading and Writing LHA Files

readLha(String fileName, { handler(entry) } ) or
(URL url, { handler( entry) } ) or
(File file, { handler( entry) } ) or
(InputStream inputStream, { handler( entry) } )

When handler is not specified, the entries of the LHA file are printed.

> readLha("pnuts.lha")
       0 Fri May 05 18:30:30 JST 2000 pnuts/
       0 Fri May 05 18:30:30 JST 2000 pnuts/awt/
    2869 Tue May 30 16:08:32 JST 2000 pnuts/awt/DialogOutputStream.class
    4980 Fri May 05 18:30:30 JST 2000 pnuts/awt/Layout.class
....

When handler is specified, the function is called with a object that has the following properties.

Property Name Type
input InputStream
header.path String
header.lastModified java.util.Date
header.originalSize long
header.compressedSize long

For instance, the following function writes the content of a file in a LHA file to the specified OutputStream.

function extract(lhafile, name, out){
  readLha(lhafile, function (e) if (e.header.path == name) read(e.input, out))
}
readLhaEntries(String fileName ) or
(URL url ) or
(File file ) or
(InputStream inputStream )

readLhaEntries() returns a generater which generates references to the archived files.

Property Name Type
input InputStream
header LhaHeader
header.path String
header.lastModified java.util.Date
header.origialSize long
header.compressedSize long
e.g.
for (e : readLhaEntries(zfile)){
   println(e.header.path)
}
extractLha(
( String fileName | File file | InputStream input | URL url ),
( String fileName | File file | OutputStream output | Writer writer ),
{, ( String entryName | matchFunction(String entryName) ) } )

extractLha() extracts the specified entries of a LHA file in a number of ways.

First parameter specifies the LHA file to be read.

If the 2nd argument is a directory, the entries of the LHA file are stored in the directory. If the 2nd argument is a file, the LHA entries are stored in the file.

e.g.
extractLha("foo.lha", ".")

If entryName is specified as the third parameter, LHA headers that matches the entryName are extracted. If matchFunction is specified, LHA entries that the function returns true are extracted.

If only two parameters are specified, all entries of the LHA file are extracted.

e.g.
extractLha("rt.lha", "script.lha", function (n) n.startsWith("javax/script/"))

If either output or writer is specified, a LHA entry is written to the stream.

e.g.
extractLha("foo.lha", getContext().getWriter(), "pnuts/lang/pnuts.properties")
writeLha(String file, ( String[] | String ) fileList) or
(File file, ( String[] | String ) fileList ) or
(OutputStream outputStream, ( String[] | String ) fileList )

writeLha() creates a LHA file from the all files in fileList, and saves them in the file or writes to the specified outputStream.

writeLha("foo.lha", ["foo.txt", "bar.class"])

writeLha("classes.lha", "classes")
writeLhaEntries(String fileName, entries) or
(OutputStream outputStream, entries) or
(File file, entries)

writeLhaEntries() creates a LHA file, specifying a Generater that generates references to the archived files. The file references should have the following properties.

Property Name Type
input InputStream (any objects that open() can handle), null if no data is present
header.path String
header.lastModified java.util.Data
e.g.
function fileEntries(dir){
    for (i : dir.files()){
      f = File(i)
      yield {"header"=>{"path"=>f.name, "lastModified"=>date(f.lastModified())},
             "input"=>isDirectory(f) ? null : f}
    }
}
writeLhaEntries(filename, fileEntries("src"))