Mercurial > hg > cc > cirrus_work
comparison lib/python/unpackz.py @ 239:992f59d21832
working, but last count/offset not being written
author | Henry S. Thompson <ht@inf.ed.ac.uk> |
---|---|
date | Sat, 28 Sep 2024 15:19:05 +0100 |
parents | |
children | 51bd09d4289e |
comparison
equal
deleted
inserted
replaced
238:a4538bdfa92a | 239:992f59d21832 |
---|---|
1 #!/usr/bin/env python3 | |
2 '''See https://stackoverflow.com/a/37042747/2595465''' | |
3 import sys | |
4 import isal.isal_zlib | |
5 offset = 0 | |
6 obuf_len = 0 | |
7 nbuf = lastbuf = False | |
8 BUFSIZE = 1048576 | |
9 outfile = None | |
10 if sys.argv[1] == '-o': | |
11 sys.argv.pop(1) | |
12 if len(sys.argv)==3: | |
13 outfile = open(sys.argv.pop(1),'wb') | |
14 else: | |
15 print('need an outfile', file=sys.stderr) | |
16 exit(1) | |
17 with open(sys.argv[1],'rb') as f: | |
18 z = isal.isal_zlib.decompressobj(31) | |
19 count = 0 | |
20 while True: | |
21 if z.unused_data == b"": | |
22 #print('n', obuf_len, file=sys.stderr) | |
23 if nbuf: | |
24 obuf_len += BUFSIZE # still no EOS after a full buffer processed | |
25 buf = f.read(BUFSIZE) | |
26 nbuf = True | |
27 lastbuf = ((truesize:=len(buf)) < BUFSIZE) # will only succeed if now at EOF | |
28 if buf == b"": | |
29 if outfile is None: | |
30 print(count, offset) | |
31 else: | |
32 outfile.write(b'\000%d\000%d\000'%(count, offset)) | |
33 break | |
34 else: | |
35 buf_len = len(buf) | |
36 #print('b', obuf_len, buf_len, len(z.unused_data), len(buf)-len(z.unused_data), | |
37 # nbuf, lastbuf, file=sys.stderr) | |
38 count = (obuf_len if (buf_len == truesize) else 0) + \ | |
39 (len(buf)-len(z.unused_data)) | |
40 if outfile is None: | |
41 print(count, offset) | |
42 else: | |
43 outfile.write(b'\000%d\000%d\000'%(count, offset)) | |
44 offset += count | |
45 count = 0 | |
46 buf = z.unused_data | |
47 obuf_len = len(buf) | |
48 nbuf = False | |
49 z = isal.isal_zlib.decompressobj(31) | |
50 got = z.decompress(buf) | |
51 if outfile is not None: | |
52 outfile.write(got) | |
53 if count!=0: | |
54 print("Unused data: count=%s offset=%s ?"%(count, offset), | |
55 file=sys.stderr) | |
56 if outfile is not None: | |
57 outfile.close() |