Class: LibMsPack::MsCab::CabDecompressor

Inherits:
Object
  • Object
show all
Defined in:
lib/libmspack/mscab.rb

Overview

CAB decompressor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(system = nil) ⇒ CabDecompressor

Creates a new CAB decompressor.

Parameters:

  • system (MsPack::MsPackSystem, nil) (defaults to: nil)

    a custom mspack system, or nil to use the default



544
545
546
547
# File 'lib/libmspack/mscab.rb', line 544

def initialize(system = nil)
    @Decompressor = nil
    init(system)
end

Instance Attribute Details

#DecompressorObject (readonly)

Returns the value of attribute Decompressor.



540
541
542
# File 'lib/libmspack/mscab.rb', line 540

def Decompressor
  @Decompressor
end

Instance Method Details

#append(cab, nextcab) ⇒ Object

Appends one MsCabdCabinet to another, forming or extending a cabinet set.

This will attempt to append one cabinet to another such that (cab.nextcab == nextcab) && (nextcab.prevcab == cab) and any folders split between the two cabinets are merged.

The cabinets MUST be part of a cabinet set -- a cabinet set is a cabinet that spans more than one physical cabinet file on disk -- and must be appropriately matched.

It can be determined if a cabinet has further parts to load by examining the MsCabdCabinet.flags field:

  • if (flags & MSCAB_HDR_PREVCAB) is non-zero, there is a predecessor cabinet to #open and #prepend. Its MS-DOS case-insensitive filename is MsCabdCabinet.prevname
  • if (flags & MSCAB_HDR_NEXTCAB) is non-zero, there is a successor cabinet to #open and #append. Its MS-DOS case-insensitive filename is MsCabdCabinet.nextname

If the cabinets do not match, an exception will be raised. Neither cabinet has been altered, and both should be closed seperately.

Files and folders in a cabinet set are a single entity. All cabinets in a set use the same file list, which is updated as cabinets in the set are added.

Parameters:

  • cab (MsCabdCabinet)

    the cabinet which will be appended to, predecessor of nextcab

  • nextcab (MsCabdCabinet)

    the cabinet which will be appended, successor of cab

Raises:

See Also:



642
643
644
645
646
647
# File 'lib/libmspack/mscab.rb', line 642

def append(cab, nextcab)
    raise Exceptions::NotInitializedError unless @Decompressor
    error = @Decompressor.append(@Decompressor, cab, nextcab)
    raise Exceptions::LibMsPackError.new(error) unless error == LibMsPack::MSPACK_ERR_OK
    error
end

#close(cabinet) ⇒ Object

Closes a previously opened cabinet or cabinet set.

This closes a cabinet, all cabinets associated with it via the MsCabdCabinet#next, MsCabdCabinet#prevcab and MsCabdCabinet#nextcab, and all folders and files. All memory used by these entities is freed.

The cabinet is now invalid and cannot be used again. All MsCabdFolder and MsCabdFile from that cabinet or cabinet set are also now invalid, and cannot be used again.

If the cabinet given was created using #search, it MUST be the cabinet returned by #search and not one of the later cabinet pointers further along the MsCabdCabinet::next chain.

If extra cabinets have been added using #append or #prepend, these will all be freed, even if the cabinet given is not the first cabinet in the set. Do NOT #close more than one cabinet in the set.

Parameters:

Raises:

See Also:



589
590
591
592
# File 'lib/libmspack/mscab.rb', line 589

def close(cabinet)
    raise Exceptions::NotInitializedError unless @Decompressor
    @Decompressor.close(@Decompressor, cabinet)
end

#destroyObject

Destroys an existing CAB decompressor



714
715
716
717
718
# File 'lib/libmspack/mscab.rb', line 714

def destroy
    raise Exceptions::NotInitializedError unless @Decompressor
    LibMsPack.DestroyCabDecompressor(@Decompressor)
    @Decompressor = nil
end

#extract(cabfile, filename) ⇒ Object

Extracts a file from a cabinet or cabinet set.

This extracts a compressed file in a cabinet and writes it to the given filename.

The MS-DOS filename of the file, MsCabdCabinet.filename, is NOT USED by #extract. The caller must examine this MS-DOS filename, copy and change it as necessary, create directories as necessary, and provide the correct filename as a parameter, which will be passed unchanged to the decompressor's MsPackSystem#open

If the file belongs to a split folder in a multi-part cabinet set, and not enough parts of the cabinet set have been loaded and appended or prepended, an exception will be raised immediately.

Parameters:

  • cabfile (MsCabdFile)

    the file to be decompressed

  • filename (String)

    the filename of the file being written to

Raises:



679
680
681
682
683
684
# File 'lib/libmspack/mscab.rb', line 679

def extract(cabfile, filename)
    raise Exceptions::NotInitializedError unless @Decompressor
    error = @Decompressor.extract(@Decompressor, cabfile, filename)
    raise Exceptions::LibMsPackError.new(error) unless error == LibMsPack::MSPACK_ERR_OK
    error
end

#init(system = MsPack::RubyPackSystem) ⇒ Object



550
551
552
553
# File 'lib/libmspack/mscab.rb', line 550

def init(system = MsPack::RubyPackSystem)
    raise Exceptions::AlreadyInitializedError if @Decompressor
    @Decompressor = LibMsPack.CreateCabDecompressor(system)
end

#lastErrorFixnum

Returns the error code set by the most recently called method

Returns:

  • (Fixnum)

    the most recent error code

Raises:



708
709
710
711
# File 'lib/libmspack/mscab.rb', line 708

def lastError
    raise Exceptions::NotInitializedError unless @Decompressor
    @Decompressor.last_error(@Decompressor)
end

#open(filename) ⇒ MsCabdCabinet

Opens a cabinet file and reads its contents.

If the file opened is a valid cabinet file, all headers will be read and a MsCabdCabinet structure will be returned, with a full list of folders and files.

In the case of an error occuring, exception will be raised.

Parameters:

  • filename (String)

    name of the cabinet file

Returns:

Raises:

See Also:



566
567
568
569
570
571
572
# File 'lib/libmspack/mscab.rb', line 566

def open(filename)
    raise Exceptions::NotInitializedError unless @Decompressor
    cabinet = @Decompressor.open(@Decompressor, filename)
    error = lastError
    raise Exceptions::LibMsPackError.new(error) unless error == LibMsPack::MSPACK_ERR_OK
    cabinet
end

#prepend(cab, prevcab) ⇒ Object

Prepends one MsCabdCabinet to another, forming or extending a cabinet set.

This will attempt to prepend one cabinet to another, such that (cab.prevcab == prevcab) && (prevcab.nextcab == cab). In all other respects, it is identical to #append.

Parameters:

  • cab (MsCabdCabinet)

    the cabinet which will be prepended to, successor of prevcab

  • prevcab (MsCabdCabinet)

    the cabinet which will be prepended, predecessor of cab

Raises:

See Also:



661
662
663
664
665
666
# File 'lib/libmspack/mscab.rb', line 661

def prepend(cab, prevcab)
    raise Exceptions::NotInitializedError unless @Decompressor
    error = @Decompressor.prepend(@Decompressor, cab, prevcab)
    raise Exceptions::LibMsPackError.new(error) unless error == LibMsPack::MSPACK_ERR_OK
    error
end

#search(filename) ⇒ MsCabdCabinet

Searches a regular file for embedded cabinets.

This opens a normal file with the given filename and will search the entire file for embedded cabinet files

If any cabinets are found, the equivalent of #open is called on each potential cabinet file at the offset it was found. All successfully #open'ed cabinets are kept in a list.

The first cabinet found will be returned directly as the result of this method. Any further cabinets found will be chained in a list using the MsCabdCabinet#next field.

In the case of an error occuring anywhere other than the simulated #open, exception will be raised.

If no error occurs, but no cabinets can be found in the file, nil is returned.

#close should only be called on the result of #search, not on any subsequent cabinets in the MsCabdCabinet#next chain.

Parameters:

  • filename (String)

    the filename of the file to search for cabinets

Returns:

Raises:

See Also:



613
614
615
616
617
618
619
# File 'lib/libmspack/mscab.rb', line 613

def search(filename)
    raise Exceptions::NotInitializedError unless @Decompressor
    cabinet = @Decompressor.search(@Decompressor, filename)
    error = lastError
    raise Exceptions::LibMsPackError.new(error) unless error == LibMsPack::MSPACK_ERR_OK
    cabinet
end

#setParam(param, value) ⇒ Object

Sets a CAB decompression engine parameter.

The following parameters are defined:

  • MSCABD_PARAM_SEARCHBUF: How many bytes should be allocated as a buffer when using #search ? The minimum value is 4. The default value is 32768.
  • MSCABD_PARAM_FIXMSZIP: If non-zero, #extract will ignore bad checksums and recover from decompression errors in MS-ZIP compressed folders. The default value is 0 (don't recover).
  • MSCABD_PARAM_DECOMPBUF: How many bytes should be used as an input bit buffer by decompressors? The minimum value is 4. The default value is 4096.

Parameters:

  • param (Fixnum)

    the parameter to set

  • value (Fixnum)

    the value to set the parameter to

Raises:

See Also:



699
700
701
702
703
704
# File 'lib/libmspack/mscab.rb', line 699

def setParam(param, value)
    raise Exceptions::NotInitializedError unless @Decompressor
    error = @Decompressor.set_param(@Decompressor, param, value)
    raise Exceptions::LibMsPackError.new(error) unless error == LibMsPack::MSPACK_ERR_OK
    error
end