Changeset 733

Show
Ignore:
Timestamp:
06/05/06 09:00:36 (2 years ago)
Author:
mfenniak
Message:

Add working and tested .NET framework based code for compression and decompression.

Files:

Legend:

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

    r732 r733  
    4444        return zlib.compress(data) 
    4545except 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) 
     46    # Unable to import zlib.  Attempt to use the System.IO.Compression 
     47    # library from the .NET framework. (IronPython only) 
     48    import System 
     49    from System import IO, Collections, Array 
     50    def _string_to_bytearr(buf): 
     51        retval = Array.CreateInstance(System.Byte, len(buf)) 
     52        for i in range(len(buf)): 
     53            retval[i] = ord(buf[i]) 
     54        return retval 
    5255    def _bytearr_to_string(bytes): 
    53         return _encoding.GetString(bytes) 
    54     def _read_bytes(gz, byteArray): 
    55         pass 
     56        retval = "" 
     57        for i in range(bytes.Length): 
     58            retval += chr(bytes[i]) 
     59        return retval 
     60    def _read_bytes(stream): 
     61        ms = IO.MemoryStream() 
     62        buf = Array.CreateInstance(System.Byte, 2048) 
     63        while True: 
     64            bytes = stream.Read(buf, 0, buf.Length) 
     65            if bytes == 0: 
     66                break 
     67            else: 
     68                ms.Write(buf, 0, bytes) 
     69        retval = ms.ToArray() 
     70        ms.Close() 
     71        return retval 
    5672    def decompress(data): 
    5773        bytes = _string_to_bytearr(data) 
     
    5975        ms.Write(bytes, 0, bytes.Length) 
    6076        ms.Position = 0  # fseek 0 
    61         gz = IO.Compression.GZipStream(ms, IO.Compression.CompressionMode.Decompress) 
     77        gz = IO.Compression.DeflateStream(ms, IO.Compression.CompressionMode.Decompress) 
    6278        bytes = _read_bytes(gz) 
    6379        retval = _bytearr_to_string(bytes) 
    6480        gz.Close() 
    6581        return retval 
    66     # implementation not complete or tested, but checked in just for fun. 
    67      
     82    def compress(data): 
     83        bytes = _string_to_bytearr(data) 
     84        ms = IO.MemoryStream() 
     85        gz = IO.Compression.DeflateStream(ms, IO.Compression.CompressionMode.Compress, True) 
     86        gz.Write(bytes, 0, bytes.Length) 
     87        gz.Close() 
     88        ms.Position = 0 # fseek 0 
     89        bytes = ms.ToArray() 
     90        retval = _bytearr_to_string(bytes) 
     91        ms.Close() 
     92        return retval 
    6893 
    6994