-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathzlibStream.py
More file actions
63 lines (52 loc) · 1.33 KB
/
Copy pathzlibStream.py
File metadata and controls
63 lines (52 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
#
# Script to de/compress the streams of a PDF file
#
# http://eternal-todo.com
# Jose Miguel Esparza
#
import sys,zlib,os,re
usage = "Usage: " + sys.argv[0] + ''' -c|-d target
Arguments:
target: the string or file to be de/compressed.
Options:
-c: compress
-d: decompress
'''
def cleanStream(stream):
output = stream
for i in range(len(stream)):
if stream[i] == '\r' or stream[i] == '\n':
output = output[1:]
else:
break
for i in range(len(stream)-1,0,-1):
if stream[i] == '\r' or stream[i] == '\n':
output = output[:-1]
else:
break
return output
if len(sys.argv) != 3 or (sys.argv[1] != '-c' and sys.argv[1] != '-d'):
sys.exit(usage)
action = sys.argv[1]
target = sys.argv[2]
targets = []
if os.path.exists(target):
content = open(target,'r').read()
if action == '-d':
targets = re.findall('/Filter\s*?/FlateDecode.*?stream(.*?)endstream',content,re.DOTALL)
if targets == []:
targets.append(content)
else:
targets.append(content)
else:
targets.append(target)
for string in targets:
string = cleanStream(string)
try:
if action == '-c':
print zlib.compress(string)
else:
print zlib.decompress(string)
except:
print "Zlib error: "+str(sys.exc_info()[1])