Skip to content

Commit 98f72e4

Browse files
MAINT: Remove IronPython Fallback for zlib (#872)
See #863 Co-authored-by: Martin Thoma <info@martin-thoma.de>
1 parent 2478309 commit 98f72e4

File tree

1 file changed

+15
-71
lines changed

1 file changed

+15
-71
lines changed

PyPDF2/filters.py

+15-71
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import struct
3535
from io import StringIO
3636
from typing import Any, Dict, Optional, Tuple, Union
37+
import zlib
3738

3839
from PyPDF2.generic import ArrayObject, DictionaryObject, NameObject
3940

@@ -52,80 +53,23 @@
5253
from PyPDF2.errors import PdfReadError, PdfStreamError
5354
from PyPDF2.utils import ord_, paethPredictor
5455

55-
try:
56-
import zlib
57-
58-
def decompress(data: bytes) -> bytes:
59-
try:
60-
return zlib.decompress(data)
61-
except zlib.error:
62-
d = zlib.decompressobj(zlib.MAX_WBITS | 32)
63-
result_str = b""
64-
for b in [data[i : i + 1] for i in range(len(data))]:
65-
try:
66-
result_str += d.decompress(b)
67-
except zlib.error:
68-
pass
69-
return result_str
70-
71-
def compress(data: bytes) -> bytes:
72-
return zlib.compress(data)
73-
74-
except ImportError: # pragma: no cover
75-
# Unable to import zlib. Attempt to use the System.IO.Compression
76-
# library from the .NET framework. (IronPython only)
77-
import System # type: ignore[import]
78-
from System import IO, Array
79-
80-
def _string_to_bytearr(buf): # type: ignore[no-untyped-def]
81-
retval = Array.CreateInstance(System.Byte, len(buf))
82-
for i in range(len(buf)):
83-
retval[i] = ord(buf[i])
84-
return retval
85-
86-
def _bytearr_to_string(bytes) -> str: # type: ignore[no-untyped-def]
87-
retval = ""
88-
for i in range(bytes.Length):
89-
retval += chr(bytes[i])
90-
return retval
9156

92-
def _read_bytes(stream): # type: ignore[no-untyped-def]
93-
ms = IO.MemoryStream()
94-
buf = Array.CreateInstance(System.Byte, 2048)
95-
while True:
96-
bytes = stream.Read(buf, 0, buf.Length)
97-
if bytes == 0:
98-
break
99-
else:
100-
ms.Write(buf, 0, bytes)
101-
retval = ms.ToArray()
102-
ms.Close()
103-
return retval
57+
def decompress(data: bytes) -> bytes:
58+
try:
59+
return zlib.decompress(data)
60+
except zlib.error:
61+
d = zlib.decompressobj(zlib.MAX_WBITS | 32)
62+
result_str = b""
63+
for b in [data[i : i + 1] for i in range(len(data))]:
64+
try:
65+
result_str += d.decompress(b)
66+
except zlib.error:
67+
pass
68+
return result_str
10469

105-
def decompress(data): # type: ignore
106-
bytes = _string_to_bytearr(data)
107-
ms = IO.MemoryStream()
108-
ms.Write(bytes, 0, bytes.Length)
109-
ms.Position = 0 # fseek 0
110-
gz = IO.Compression.DeflateStream(ms, IO.Compression.CompressionMode.Decompress)
111-
bytes = _read_bytes(gz)
112-
retval = _bytearr_to_string(bytes)
113-
gz.Close()
114-
return retval
11570

116-
def compress(data): # type: ignore
117-
bytes = _string_to_bytearr(data)
118-
ms = IO.MemoryStream()
119-
gz = IO.Compression.DeflateStream(
120-
ms, IO.Compression.CompressionMode.Compress, True
121-
)
122-
gz.Write(bytes, 0, bytes.Length)
123-
gz.Close()
124-
ms.Position = 0 # fseek 0
125-
bytes = ms.ToArray()
126-
retval = _bytearr_to_string(bytes)
127-
ms.Close()
128-
return retval
71+
def compress(data: bytes) -> bytes:
72+
return zlib.compress(data)
12973

13074

13175
class FlateDecode:

0 commit comments

Comments
 (0)