Changeset 732

Show
Ignore:
Timestamp:
06/04/06 12:27:05 (2 years ago)
Author:
mfenniak
Message:

begin adding .NET compression library support as a replacement for zlib when not available

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pypdf/trunk/pyPdf/filters.py

    r727 r732  
    3535__author_email__ = "mfenniak@pobox.com" 
    3636 
    37 import zlib 
    3837from generic import NameObject 
     38 
     39try: 
     40    import zlib 
     41    def decompress(data): 
     42        return zlib.decompress(data) 
     43    def compress(data): 
     44        return zlib.compress(data) 
     45except ImportError: 
     46    # Unable to import zlib.  If running in IronPython, try using 
     47    # System.IO.Compression instead. 
     48    from System import IO, Text 
     49    _encoding = Text.ASCIIEncoding() 
     50    def _string_to_bytearr(string): 
     51        return _encoding.GetBytes(string) 
     52    def _bytearr_to_string(bytes): 
     53        return _encoding.GetString(bytes) 
     54    def _read_bytes(gz, byteArray): 
     55        pass 
     56    def decompress(data): 
     57        bytes = _string_to_bytearr(data) 
     58        ms = IO.MemoryStream() 
     59        ms.Write(bytes, 0, bytes.Length) 
     60        ms.Position = 0  # fseek 0 
     61        gz = IO.Compression.GZipStream(ms, IO.Compression.CompressionMode.Decompress) 
     62        bytes = _read_bytes(gz) 
     63        retval = _bytearr_to_string(bytes) 
     64        gz.Close() 
     65        return retval 
     66    # implementation not complete or tested, but checked in just for fun. 
     67     
     68 
    3969 
    4070class FlateDecode(object): 
    4171    def decode(data, decodeParms): 
    42         data = zlib.decompress(data) 
     72        data = decompress(data) 
    4373        predictor = 1 
    4474        if decodeParms: 
     
    77107 
    78108    def encode(data): 
    79         return zlib.compress(data) 
     109        return compress(data) 
    80110    encode = staticmethod(encode) 
    81111