@@ -123,6 +123,14 @@ def compress_to_xz_auto(src_path, dst_path, threads=0):
123123 with open (src_path , 'rb' ) as src , lzma .open (dst_path , 'wb' ) as dst :
124124 shutil .copyfileobj (src , dst )
125125
126+ def compress_to_7z_auto (src_path , dst_path , threads = 0 ):
127+ if shutil .which ("7z" ):
128+ print ("Compress with system's 7z command" )
129+ cmd = ["7z" , "a" , "-v2g" , "-mx9" , f"-mmt{ threads } " , dst_path , src_path ]
130+ subprocess .run (cmd , check = True )
131+ else :
132+ print ("7z command not found" )
133+
126134def check_env ():
127135 if not shutil .which ("simg2img" ):
128136 print ("[ERROR] simg2img command not found, please install first" )
@@ -135,7 +143,10 @@ def axp2img(arg_input, arg_output):
135143 if not check_env ():
136144 return - 1
137145
138- file_name = os .path .splitext (os .path .basename (arg_input ))[0 ]
146+ file_name = os .path .splitext (os .path .basename (arg_output ))[0 ]
147+ while file_name .endswith (".img" ):
148+ file_name = os .path .splitext (file_name )[0 ]
149+
139150 if os .path .isfile (arg_input ):
140151 input_dir = os .path .abspath (os .path .dirname (arg_input ))
141152 temp_dir = os .path .join (input_dir , "temp" )
@@ -153,7 +164,7 @@ def axp2img(arg_input, arg_output):
153164 if not arg_output :
154165 arg_output = os .path .join (out_dir , f"{ file_name } .img.xz" )
155166
156- supported_format = [".xz" , ".img" ]
167+ supported_format = [".xz" , ".img" , ".7z" ]
157168 format = os .path .splitext (arg_output )[1 ]
158169 if format not in supported_format :
159170 print (f"Not support output format { format } , supported: { supported_format } " )
@@ -251,7 +262,14 @@ def axp2img(arg_input, arg_output):
251262 print ("xz compress complete, remove img file" )
252263 os .remove (out_img_path )
253264 print ("xz file saved to" , arg_output )
254-
265+ elif arg_output .endswith (".7z" ):
266+ print ("\n Compress to 7z format:" )
267+ print (" from:" , out_img_path )
268+ print (" to:" , arg_output )
269+ compress_to_7z_auto (out_img_path , arg_output )
270+ print ("7z compress complete, remove img file" )
271+ os .remove (out_img_path )
272+ print ("7z file saved to" , arg_output )
255273 if temp_dir :
256274 shutil .rmtree (temp_dir )
257275
0 commit comments