@@ -17,61 +17,54 @@ def seekread(f, offset = None, length = 0, relative = True):
1717 f .seek (offset , [0 , 1 , 2 ][relative ])
1818 if length :
1919 return f .read (length )
20+ return None
2021
2122
2223def parse_pbzx (pbzx_path ):
2324 section = 0
2425 xar_out_path = '%s.part%02d.cpio.xz' % (pbzx_path , section )
25- f = open (pbzx_path , 'rb' )
26- # pbzx = f.read()
27- # f.close()
28- magic = seekread (f , length = 4 )
29- if magic != 'pbzx' :
30- raise RuntimeError ("Error: Not a pbzx file" )
31- # Read 8 bytes for initial flags
32- flags = seekread (f , length = 8 )
33- # Interpret the flags as a 64-bit big-endian unsigned int
34- flags = struct .unpack ('>Q' , flags )[0 ]
35- xar_f = open (xar_out_path , 'wb' )
36- while flags & (1 << 24 ):
37- # Read in more flags
26+ with open (pbzx_path , 'rb' ) as f :
27+ # pbzx = f.read()
28+ # f.close()
29+ magic = seekread (f , length = 4 )
30+ if magic != 'pbzx' :
31+ raise RuntimeError ("Error: Not a pbzx file" )
32+ # Read 8 bytes for initial flags
3833 flags = seekread (f , length = 8 )
34+ # Interpret the flags as a 64-bit big-endian unsigned int
3935 flags = struct .unpack ('>Q' , flags )[0 ]
40- # Read in length
41- f_length = seekread (f , length = 8 )
42- f_length = struct .unpack ('>Q' , f_length )[0 ]
43- xzmagic = seekread (f , length = 6 )
44- if xzmagic != '\xfd 7zXZ\x00 ' :
45- # This isn't xz content, this is actually _raw decompressed cpio_ chunk of 16MB in size...
46- # Let's back up ...
47- seekread (f , offset = - 6 , length = 0 )
48- # ... and split it out ...
49- f_content = seekread (f , length = f_length )
50- section += 1
51- decomp_out = '%s.part%02d.cpio' % (pbzx_path , section )
52- g = open (decomp_out , 'wb' )
53- g .write (f_content )
54- g .close ()
55- # Now to start the next section, which should hopefully be .xz (we'll just assume it is ...)
56- xar_f .close ()
57- section += 1
58- new_out = '%s.part%02d.cpio.xz' % (pbzx_path , section )
59- xar_f = open (new_out , 'wb' )
60- else :
61- f_length -= 6
62- # This part needs buffering
63- f_content = seekread (f , length = f_length )
64- tail = seekread (f , offset = - 2 , length = 2 )
65- xar_f .write (xzmagic )
66- xar_f .write (f_content )
67- if tail != 'YZ' :
68- xar_f .close ()
69- raise RuntimeError ("Error: Footer is not xar file footer" )
70- try :
71- f .close ()
72- xar_f .close ()
73- except IOError :
74- pass
36+ while flags & (1 << 24 ):
37+ with open (xar_out_path , 'wb' ) as xar_f :
38+ xar_f .seek (0 , os .SEEK_END )
39+ # Read in more flags
40+ flags = seekread (f , length = 8 )
41+ flags = struct .unpack ('>Q' , flags )[0 ]
42+ # Read in length
43+ f_length = seekread (f , length = 8 )
44+ f_length = struct .unpack ('>Q' , f_length )[0 ]
45+ xzmagic = seekread (f , length = 6 )
46+ if xzmagic != '\xfd 7zXZ\x00 ' :
47+ # This isn't xz content, this is actually _raw decompressed cpio_ chunk of 16MB in size...
48+ # Let's back up ...
49+ seekread (f , offset = - 6 , length = 0 )
50+ # ... and split it out ...
51+ f_content = seekread (f , length = f_length )
52+ section += 1
53+ decomp_out = '%s.part%02d.cpio' % (pbzx_path , section )
54+ with open (decomp_out , 'wb' ) as g :
55+ g .write (f_content )
56+ # Now to start the next section, which should hopefully be .xz (we'll just assume it is ...)
57+ section += 1
58+ xar_out_path = '%s.part%02d.cpio.xz' % (pbzx_path , section )
59+ else :
60+ f_length -= 6
61+ # This part needs buffering
62+ f_content = seekread (f , length = f_length )
63+ tail = seekread (f , offset = - 2 , length = 2 )
64+ xar_f .write (xzmagic )
65+ xar_f .write (f_content )
66+ if tail != 'YZ' :
67+ raise RuntimeError ("Error: Footer is not xar file footer" )
7568
7669
7770def main ():
0 commit comments