CAB files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Virgil Stokes

    CAB files

    I would appreciate python code for creating *.cab files.

    --V. Stokes
  • Thomas Heller

    #2
    Re: CAB files

    Virgil Stokes schrieb:
    I would appreciate python code for creating *.cab files.
    >
    --V. Stokes
    Here is some code that I have still laying around. It has never been
    used in production and I do not know what you can do with the cab files
    it creates, but I have been able to create a cab and open it with winzip.

    Thomas

    <cab.py>
    from ctypes import *
    import sys, os, tempfile, glob

    BOOL = c_int
    ULONG = c_ulong
    UINT = c_uint
    USHORT = c_ushort

    class ERF(Structure):
    _fields_ = [("erfOper", c_int),
    ("erfType", c_int),
    ("fError", BOOL)]

    CB_MAX_CHUNK = 32768
    CB_MAX_DISK = 0x7ffffff
    CB_MAX_FILENAME = 256
    CB_MAX_CABINET_ NAME = 256
    CB_MAX_CAB_PATH = 256
    CB_MAX_DISK_NAM E = 256

    class CCAB(Structure) :
    _fields_ = [
    ("cb", ULONG), # size available for cabinet on this media
    ("cbFolderThres h", ULONG), # Thresshold for forcing a new Folder
    ("cbReserveCFHe ader", UINT), # Space to reserve in CFHEADER
    ("cbReserveCFFo lder", UINT), # Space to reserve in CFFOLDER
    ("cbReserveCFDa ta", UINT), # Space to reserve in CFDATA
    ("iCab", c_int), # sequential numbers for cabinets
    ("iDisk", c_int), # Disk number
    ("fFailOnIncomp ressible", c_int), # TRUE =Fail if a block is incompressible
    ("setID", USHORT), # Cabinet set ID
    ("szDisk", c_char * CB_MAX_DISK_NAM E), # current disk name
    ("szCab", c_char * CB_MAX_CABINET_ NAME), # current cabinet name
    ("szCabPath" , c_char * CB_MAX_CAB_PATH ), # path for creating cabinet
    ]

    cab = cdll.cabinet

    class HFCI(object):
    _handle = 0
    _as_parameter_ = property(lambda self: self._handle)

    def __init__(self, fnm, verbose=1):
    self.verbose = verbose
    self.erf = ERF()
    ccab = self.ccab = CCAB()
    ccab.cb = 100000000
    ccab.cbFolderTh resh = 100000
    ccab.szCab = os.path.basenam e(fnm)
    dirname = os.path.dirname (fnm)
    if not dirname:
    dirname = "."
    ccab.szCabPath = dirname + "\\"
    self._init_call backs()
    self._handle = cab.FCICreate(b yref(self.erf),
    self.pfn_filede st,
    cdll.msvcrt.mal loc,
    cdll.msvcrt.fre e,
    cdll.msvcrt._op en,
    cdll.msvcrt._re ad,
    cdll.msvcrt._wr ite,
    cdll.msvcrt._cl ose,
    cdll.msvcrt._ls eek,
    cdll.msvcrt._un link,
    self.pfn_gettem pfnm,
    byref(ccab),
    None)

    def _init_callbacks (self):
    self.pfn_gettem pfnm = CFUNCTYPE(c_int , c_void_p, c_int, c_void_p)(self. _gettempfnm)
    self.pfn_filede st = CFUNCTYPE(c_int )(self._filedes t)
    self.pfn_status = CFUNCTYPE(c_int , c_int, c_uint, c_uint, c_void_p)(self. _status)
    self.pfn_getnex tcab = CFUNCTYPE(c_int )(self._getnext cab)
    self.pfn_getope ninfo = CFUNCTYPE(c_int , c_char_p)(self. _getopeninfo)

    def _getopeninfo(se lf, fnm):
    if self.verbose:
    print "File", fnm
    return cdll.msvcrt._op en(fnm, os.O_BINARY | os.O_RDONLY)

    def _status(self, typeStatus, cb1, cb2, pv):
    return 0

    def _filedest(self) :
    return 0

    def _getnextcab(sel f):
    return 0

    def _gettempfnm(sel f, pszTempName, cbTempName, pv):
    # same as tempfile.mktemp (), but this is deprecated
    fh, fnm = tempfile.mkstem p()
    os.close(fh)
    os.remove(fnm)
    cdll.msvcrt.str cpy(pszTempName , fnm)
    return 1

    def AddFile(self, src, dst=None, compressed=0):
    if dst is None:
    dst = os.path.basenam e(src)
    cab.FCIAddFile( self,
    src,
    dst,
    0, # fExecute
    self.pfn_getnex tcab,
    self.pfn_status ,
    self.pfn_getope ninfo,
    compressed)

    def Close(self):
    if self._handle != 0:
    cab.FCIFlushCab inet(self,
    0, # fGetNextCab
    self.pfn_getnex tcab,
    self.pfn_status )
    cab.FCIDestroy( self)
    self._handle = 0

    if __name__ == "__main__":
    import os, glob

    hfci = HFCI("my-first.cab", verbose=1)

    files = glob.glob(r".\c ab\*.*")

    for fnm in files:
    hfci.AddFile(fn m)

    hfci.Close()
    <eof>

    Comment

    • =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

      #3
      Re: CAB files

      I would appreciate python code for creating *.cab files.

      The msilib module of Python 2.5 can be used to create CAB files,
      through msilib.FCICreat e(cabname, filelist), where filelist is
      a list of (systempath, cabpath).

      Regards,
      Martin

      Comment

      Working...