annotate lib/python/unpackz.py @ 240:51bd09d4289e

eof pblms fixed, seems to work
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Tue, 01 Oct 2024 15:59:26 +0100
parents 992f59d21832
children 96021cfee209
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
239
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
1 #!/usr/bin/env python3
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
2 '''See https://stackoverflow.com/a/37042747/2595465'''
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
3 import sys
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
4 import isal.isal_zlib
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
5 offset = 0
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
6 obuf_len = 0
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
7 nbuf = lastbuf = False
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
8 BUFSIZE = 1048576
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
9 outfile = None
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
10 if sys.argv[1] == '-o':
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
11 sys.argv.pop(1)
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
12 if len(sys.argv)==3:
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
13 outfile = open(sys.argv.pop(1),'wb')
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
14 else:
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
15 print('need an outfile', file=sys.stderr)
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
16 exit(1)
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
17 with open(sys.argv[1],'rb') as f:
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
18 z = isal.isal_zlib.decompressobj(31)
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
19 count = 0
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
20 while True:
240
51bd09d4289e eof pblms fixed, seems to work
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 239
diff changeset
21 if z.unused_data == b"":
239
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
22 #print('n', obuf_len, file=sys.stderr)
240
51bd09d4289e eof pblms fixed, seems to work
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 239
diff changeset
23 if lastbuf: # buf == b"":
51bd09d4289e eof pblms fixed, seems to work
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 239
diff changeset
24 if outfile is None:
51bd09d4289e eof pblms fixed, seems to work
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 239
diff changeset
25 print(obuf_len, offset)
51bd09d4289e eof pblms fixed, seems to work
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 239
diff changeset
26 else:
51bd09d4289e eof pblms fixed, seems to work
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 239
diff changeset
27 outfile.write(b'\000%d\000%d\000'%(obuf_len, offset))
51bd09d4289e eof pblms fixed, seems to work
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 239
diff changeset
28 if count!=0:
51bd09d4289e eof pblms fixed, seems to work
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 239
diff changeset
29 print("Unused data: count=%s offset=%s ?"%(count, offset),
51bd09d4289e eof pblms fixed, seems to work
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 239
diff changeset
30 file=sys.stderr)
51bd09d4289e eof pblms fixed, seems to work
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 239
diff changeset
31 break
239
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
32 if nbuf:
240
51bd09d4289e eof pblms fixed, seems to work
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 239
diff changeset
33 obuf_len += BUFSIZE # still no EOS after a full buffer processed
239
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
34 buf = f.read(BUFSIZE)
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
35 nbuf = True
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
36 lastbuf = ((truesize:=len(buf)) < BUFSIZE) # will only succeed if now at EOF
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
37 else:
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
38 buf_len = len(buf)
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
39 #print('b', obuf_len, buf_len, len(z.unused_data), len(buf)-len(z.unused_data),
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
40 # nbuf, lastbuf, file=sys.stderr)
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
41 count = (obuf_len if (buf_len == truesize) else 0) + \
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
42 (len(buf)-len(z.unused_data))
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
43 if outfile is None:
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
44 print(count, offset)
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
45 else:
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
46 outfile.write(b'\000%d\000%d\000'%(count, offset))
240
51bd09d4289e eof pblms fixed, seems to work
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 239
diff changeset
47 # if (offset == 1352249):
51bd09d4289e eof pblms fixed, seems to work
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 239
diff changeset
48 # breakpoint()
239
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
49 offset += count
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
50 count = 0
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
51 buf = z.unused_data
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
52 obuf_len = len(buf)
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
53 nbuf = False
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
54 z = isal.isal_zlib.decompressobj(31)
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
55 got = z.decompress(buf)
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
56 if outfile is not None:
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
57 outfile.write(got)
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
58 if outfile is not None:
992f59d21832 working, but last count/offset not being written
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
59 outfile.close()