Lhafile

Description

Lhafile is python C extension to extract lha file(.lzh).
It does not need external program for extract.
It has only extract function, not archive function.
Its interface is likely zipfile module is included in regular python environment.

(14/08/2016)
This has not been maintenance. But I keep to put my code on this server
Sorry for inconvenience.

install

$ easy_install http://svn.neotitans.net/lhafile/
$ pip install http://svn.neotitans.net/lhafile/

Binaries for Windows

lhafile-0.1.win32-py2.6.exe
lhafile-0.1.win-amd64-py2.6.exe
lhafile-0.1.win32-py2.7.exe
lhafile-0.1.win-amd64-py2.7.exe

Source codes

lhafile-0.1.tar.gz
lhafile-0.1.zip

Example

You can use this module likely zipfile module.

import lhafile

# Create Lhafile instance from filename
f = lhafile.Lhafile('foo.lzh')

# Print each file informaion in archive file.
for info in f.infolist():
   print info.filename

# Extract data from archive
f.read('bar.txt')
import os
import sys

def extract_all(lzhname):
    """Extract files under current directory"""
    print "Extract", lzhname, "..."
    # make directory to extract
    root, ext = os.path.splitext(lzhname)
    try:
        os.makedirs(root)
    except OSError, e:
        print e
        return
    # open lzh file and get file names in it.
    lha = lhafile.Lhafile(lzhname)
    files = [info.filename for info in lha.infolist()]
    # extract all files
    for filename in files:
        ufilename = unicode(filename, "cp932")
        dirname = os.path.dirname(ufilename)
        basename = os.path.basename(ufilename)
        try:
            os.makedirs(os.path.join(root, dirname))
        except OSError, e:
            pass
        if basename:
            print " extract", filename
            open(os.path.join(root, dirname, basename), "wb").write(lha.read(filename))

if __name__ == '__main__':
    extract_all(sys.argv[1])