Changeset 733
- Timestamp:
- 06/05/06 09:00:36 (2 years ago)
- Files:
-
- pypdf/trunk/pyPdf/filters.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
pypdf/trunk/pyPdf/filters.py
r732 r733 44 44 return zlib.compress(data) 45 45 except 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 52 55 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 56 72 def decompress(data): 57 73 bytes = _string_to_bytearr(data) … … 59 75 ms.Write(bytes, 0, bytes.Length) 60 76 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) 62 78 bytes = _read_bytes(gz) 63 79 retval = _bytearr_to_string(bytes) 64 80 gz.Close() 65 81 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 68 93 69 94
