2 # -*- coding: utf-8 -*-
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the Revised BSD License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 Revised BSD License for more details.
13 Copyright 2018-2024 Cool Dude 2k - http://idb.berlios.de/
14 Copyright 2018-2024 Game Maker 2k - http://intdb.sourceforge.net/
15 Copyright 2018-2024 Kazuki Przyborowski - https://github.com/KazukiPrzyborowski
17 $FileInfo: pycatfile.py - Last Update: 4/12/2024 Ver. 0.7.8 RC 1 - Author: cooldude2k $
20 from __future__
import absolute_import
, division
, print_function
, unicode_literals
;
21 import io
, os
, re
, sys
, time
, stat
, zlib
, base64
, shutil
, socket
, hashlib
, datetime
, logging
, binascii
, tempfile
, zipfile
, platform
;
22 from ftplib
import FTP
, FTP_TLS
;
23 if(sys
.version
[0]=="2"):
24 from urlparse
import urlparse
, urlunparse
;
25 elif(sys
.version
[0]>="3"):
26 from urllib
.parse
import urlunparse
;
27 from urllib
.parse
import urlparse
;
29 if os
.name
== 'nt': # Only modify if on Windows
30 if sys
.version
[0] == "2":
32 sys
.stdout
= codecs
.getwriter('utf-8')(sys
.stdout
);
33 sys
.stderr
= codecs
.getwriter('utf-8')(sys
.stderr
);
36 sys
.stdout
= io
.TextIOWrapper(sys
.stdout
.buffer, encoding
='utf-8', errors
='replace', line_buffering
=True);
37 sys
.stderr
= io
.TextIOWrapper(sys
.stderr
.buffer, encoding
='utf-8', errors
='replace', line_buffering
=True);
39 hashlib_guaranteed
= False;
40 os
.environ
["PYTHONIOENCODING"] = "UTF-8";
41 os
.environ
["LC_CTYPE"] = "UTF-8";
45 sys
.setdefaultencoding('UTF-8');
48 except AttributeError:
52 except AttributeError:
56 from zlib
import crc32
;
58 from binascii
import crc32
;
60 rarfile_support
= False;
63 rarfile_support
= True;
65 rarfile_support
= False;
67 py7zr_support
= False;
72 py7zr_support
= False;
75 from safetar
import is_tarfile
;
78 from xtarfile
import is_tarfile
;
80 from tarfile
import is_tarfile
;
83 import safetar
as tarfile
;
86 import xtarfile
as tarfile
;
104 haverequests
= False;
109 haverequests
= False;
113 logging
.getLogger("urllib3").setLevel(logging
.WARNING
);
117 from urllib
.request
import Request
, build_opener
, HTTPBasicAuthHandler
;
118 from urllib
.parse
import urlparse
;
121 from urllib2
import Request
, build_opener
, HTTPBasicAuthHandler
;
122 from urlparse
import urlparse
;
124 if(sys
.version
[0]=="2"):
126 from io
import StringIO
, BytesIO
;
129 from cStringIO
import StringIO
;
130 from cStringIO
import StringIO
as BytesIO
;
132 from StringIO
import StringIO
;
133 from StringIO
import StringIO
as BytesIO
;
134 elif(sys
.version
[0]>="3"):
135 from io
import StringIO
, BytesIO
;
140 from cStringIO
import StringIO
as BytesIO
;
146 from StringIO
import StringIO
as BytesIO
;
152 from io
import BytesIO
;
157 __use_pysftp__
= False;
159 __use_pysftp__
= False;
160 __file_format_name__
= "CatFile";
161 __program_name__
= "Py"+__file_format_name__
;
162 __file_format_lower__
= __file_format_name__
.lower();
163 __file_format_magic__
= __file_format_name__
;
164 __file_format_len__
= len(__file_format_magic__
);
165 __file_format_hex__
= binascii
.hexlify(__file_format_magic__
.encode("UTF-8")).decode("UTF-8");
166 __file_format_delimiter__
= "\x00";
167 __file_format_ver__
= "001";
168 __use_new_style__
= True;
169 __use_advanced_list__
= True;
170 __use_alt_inode__
= False;
171 __file_format_list__
= [__file_format_name__
, __file_format_magic__
, __file_format_lower__
, __file_format_len__
, __file_format_hex__
, __file_format_delimiter__
, __file_format_ver__
, __use_new_style__
, __use_advanced_list__
, __use_alt_inode__
];
172 __project__
= __program_name__
;
173 __project_url__
= "https://github.com/GameMaker2k/PyCatFile";
174 __version_info__
= (0, 7, 8, "RC 1", 1);
175 __version_date_info__
= (2024, 4, 12, "RC 1", 1);
176 __version_date__
= str(__version_date_info__
[0]) + "." + str(__version_date_info__
[1]).zfill(2) + "." + str(__version_date_info__
[2]).zfill(2);
177 __revision__
= __version_info__
[3];
178 __revision_id__
= "$Id$";
179 if(__version_info__
[4] is not None):
180 __version_date_plusrc__
= __version_date__
+ "-" + str(__version_date_info__
[4]);
181 if(__version_info__
[4] is None):
182 __version_date_plusrc__
= __version_date__
;
183 if(__version_info__
[3] is not None):
184 __version__
= str(__version_info__
[0]) + "." + str(__version_info__
[1]) + "." + str(__version_info__
[2]) + " " + str(__version_info__
[3]);
185 if(__version_info__
[3] is None):
186 __version__
= str(__version_info__
[0]) + "." + str(__version_info__
[1]) + "." + str(__version_info__
[2]);
188 PyBitness
= platform
.architecture();
189 if(PyBitness
=="32bit" or PyBitness
=="32"):
191 elif(PyBitness
=="64bit" or PyBitness
=="64"):
196 geturls_ua_pycatfile_python
= "Mozilla/5.0 (compatible; {proname}/{prover}; +{prourl})".format(proname
=__project__
, prover
=__version__
, prourl
=__project_url__
);
197 if(platform
.python_implementation()!=""):
198 py_implementation
= platform
.python_implementation();
199 if(platform
.python_implementation()==""):
200 py_implementation
= "Python";
201 geturls_ua_pycatfile_python_alt
= "Mozilla/5.0 ({osver}; {archtype}; +{prourl}) {pyimp}/{pyver} (KHTML, like Gecko) {proname}/{prover}".format(osver
=platform
.system()+" "+platform
.release(), archtype
=platform
.machine(), prourl
=__project_url__
, pyimp
=py_implementation
, pyver
=platform
.python_version(), proname
=__project__
, prover
=__version__
);
202 geturls_ua_googlebot_google
= "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
203 geturls_ua_googlebot_google_old
= "Googlebot/2.1 (+http://www.google.com/bot.html)";
204 geturls_headers_pycatfile_python
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_pycatfile_python
, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close", 'SEC-CH-UA': "\""+__project__
+"\";v=\""+str(__version__
)+"\", \"Not;A=Brand\";v=\"8\", \""+py_implementation
+"\";v=\""+str(platform
.release())+"\"", 'SEC-CH-UA-FULL-VERSION': str(__version__
), 'SEC-CH-UA-PLATFORM': ""+py_implementation
+"", 'SEC-CH-UA-ARCH': ""+platform
.machine()+"", 'SEC-CH-UA-PLATFORM': str(__version__
), 'SEC-CH-UA-BITNESS': str(PyBitness
)};
205 geturls_headers_pycatfile_python_alt
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_pycatfile_python_alt
, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close", 'SEC-CH-UA': "\""+__project__
+"\";v=\""+str(__version__
)+"\", \"Not;A=Brand\";v=\"8\", \""+py_implementation
+"\";v=\""+str(platform
.release())+"\"", 'SEC-CH-UA-FULL-VERSION': str(__version__
), 'SEC-CH-UA-PLATFORM': ""+py_implementation
+"", 'SEC-CH-UA-ARCH': ""+platform
.machine()+"", 'SEC-CH-UA-PLATFORM': str(__version__
), 'SEC-CH-UA-BITNESS': str(PyBitness
)};
206 geturls_headers_googlebot_google
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_googlebot_google
, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close"};
207 geturls_headers_googlebot_google_old
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_googlebot_google_old
, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close"};
209 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
210 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
211 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
213 tarfile_mimetype
= "application/tar";
214 tarfile_tar_mimetype
= tarfile_mimetype
;
215 zipfile_mimetype
= "application/zip";
216 zipfile_zip_mimetype
= zipfile_mimetype
;
217 rarfile_mimetype
= "application/rar";
218 rarfile_rar_mimetype
= rarfile_mimetype
;
219 archivefile_mimetype
= "application/x-"+__file_format_list__
[1]+"";
220 archivefile_cat_mimetype
= archivefile_mimetype
;
221 archivefile_gzip_mimetype
= "application/x-"+__file_format_list__
[1]+"+gzip";
222 archivefile_gz_mimetype
= archivefile_gzip_mimetype
;
223 archivefile_bzip2_mimetype
= "application/x-"+__file_format_list__
[1]+"+bzip2";
224 archivefile_bz2_mimetype
= archivefile_bzip2_mimetype
;
225 archivefile_lz4_mimetype
= "application/x-"+__file_format_list__
[1]+"+lz4";
226 archivefile_lzop_mimetype
= "application/x-"+__file_format_list__
[1]+"+lzop";
227 archivefile_lzo_mimetype
= archivefile_lzop_mimetype
;
228 archivefile_zstandard_mimetype
= "application/x-"+__file_format_list__
[1]+"+zstandard";
229 archivefile_zstd_mimetype
= archivefile_zstandard_mimetype
;
230 archivefile_lzma_mimetype
= "application/x-"+__file_format_list__
[1]+"+lzma";
231 archivefile_xz_mimetype
= "application/x-"+__file_format_list__
[1]+"+xz";
232 archivefile_extensions
= ['.cat', '.cat.gz', '.cat.bz2', '.cat.zst', '.cat.lz4', '.cat.lzo', '.cat.lzop', '.cat.lzma', '.cat.xz'];
234 if __name__
== "__main__":
236 curscrpath
= os
.path
.dirname(sys
.argv
[0]);
240 curscrpath
= curscrpath
.replace(os
.sep
, "/");
241 curscrpath
= curscrpath
+ "/";
242 scrfile
= curscrpath
+ "catfile.py";
243 if(os
.path
.exists(scrfile
) and os
.path
.isfile(scrfile
)):
244 scrcmd
= subprocess
.Popen([sys
.executable
, scrfile
] + sys
.argv
[1:]);
247 def VerbosePrintOut(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
253 "warning": logging
.warning
,
254 "error": logging
.error
,
255 "critical": logging
.critical
,
256 "exception": logging
.exception
,
257 "logalt": lambda x
: logging
.log(dgblevel
, x
),
258 "debug": logging
.debug
260 log_function
= log_functions
.get(outtype
);
262 log_function(dbgtxt
);
266 def VerbosePrintOutReturn(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
267 VerbosePrintOut(dbgtxt
, outtype
, dbgenable
, dgblevel
);
270 def RemoveWindowsPath(dpath
):
274 dpath
= dpath
.replace(os
.path
.sep
, "/");
275 dpath
= dpath
.rstrip("/");
276 if(dpath
=="." or dpath
==".."):
280 def NormalizeRelativePath(inpath
):
281 inpath
= RemoveWindowsPath(inpath
);
282 if(os
.path
.isabs(inpath
)):
285 if(inpath
.startswith("./") or inpath
.startswith("../")):
288 outpath
= "./" + inpath
;
291 def PrependPath(base_dir
, child_path
):
292 # Check if base_dir is None or empty, if so, return child_path as is
295 # Ensure base_dir ends with exactly one slash
296 if not base_dir
.endswith('/'):
298 # Check if child_path starts with ./ or ../ (indicating a relative path)
299 if child_path
.startswith('./') or child_path
.startswith('../'):
300 # For relative paths, we don't alter the child_path
301 return base_dir
+ child_path
;
303 # For non-relative paths, ensure there's no starting slash on child_path to avoid double slashes
304 return base_dir
+ child_path
.lstrip('/');
306 def ListDir(dirpath
, followlink
=False, duplicates
=False):
307 if(isinstance(dirpath
, (list, tuple, ))):
308 dirpath
= list(filter(None, dirpath
));
309 elif(isinstance(dirpath
, (str, ))):
310 dirpath
= list(filter(None, [dirpath
]));
312 for mydirfile
in dirpath
:
313 if(not os
.path
.exists(mydirfile
)):
315 mydirfile
= NormalizeRelativePath(mydirfile
);
316 if(os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
)):
317 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
));
318 if(os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
)):
319 for root
, dirs
, filenames
in os
.walk(mydirfile
):
321 dpath
= RemoveWindowsPath(dpath
);
322 if(dpath
not in retlist
and not duplicates
):
323 retlist
.append(dpath
);
325 retlist
.append(dpath
);
326 for file in filenames
:
327 fpath
= os
.path
.join(root
, file);
328 fpath
= RemoveWindowsPath(fpath
);
329 if(fpath
not in retlist
and not duplicates
):
330 retlist
.append(fpath
);
332 retlist
.append(fpath
);
334 retlist
.append(RemoveWindowsPath(mydirfile
));
337 def ListDirAdvanced(dirpath
, followlink
=False, duplicates
=False):
338 if isinstance(dirpath
, (list, tuple)):
339 dirpath
= list(filter(None, dirpath
));
340 elif isinstance(dirpath
, str):
341 dirpath
= list(filter(None, [dirpath
]));
343 for mydirfile
in dirpath
:
344 if not os
.path
.exists(mydirfile
):
346 mydirfile
= NormalizeRelativePath(mydirfile
);
347 if os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
) and followlink
:
348 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
))
349 if os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
):
350 for root
, dirs
, filenames
in os
.walk(mydirfile
):
351 # Sort dirs and filenames alphabetically in place
352 dirs
.sort(key
=lambda x
: x
.lower());
353 filenames
.sort(key
=lambda x
: x
.lower());
354 dpath
= RemoveWindowsPath(root
);
355 if not duplicates
and dpath
not in retlist
:
356 retlist
.append(dpath
);
358 retlist
.append(dpath
);
359 for file in filenames
:
360 fpath
= os
.path
.join(root
, file);
361 fpath
= RemoveWindowsPath(fpath
);
362 if not duplicates
and fpath
not in retlist
:
363 retlist
.append(fpath
);
365 retlist
.append(fpath
);
367 retlist
.append(RemoveWindowsPath(mydirfile
));
370 def create_alias_function(prefix
, base_name
, suffix
, target_function
):
371 # Define a new function that wraps the target function
372 def alias_function(*args
, **kwargs
):
373 return target_function(*args
, **kwargs
);
375 # Create the function name by combining the prefix, base name, and the suffix
376 function_name
= "{}{}{}".format(prefix
, base_name
, suffix
);
378 # Add the new function to the global namespace
379 globals()[function_name
] = alias_function
;
381 def create_alias_function_alt(prefix
, base_name
, suffix
, target_function
):
382 # Create the function name by combining the prefix, base name, and the suffix
383 # Use the format method for string formatting, compatible with Python 2 and 3
384 function_name
= "{}{}{}".format(prefix
, base_name
, suffix
)
385 # Add the new function (alias of the target_function) to the global namespace
386 # This line is compatible as-is with both Python 2 and 3
387 globals()[function_name
] = target_function
389 # initial_value can be 0xFFFF or 0x0000
390 def crc16_ansi(msg
, initial_value
=0xFFFF):
391 # CRC-16-IBM / CRC-16-ANSI polynomial and initial value
392 poly
= 0x8005; # Polynomial for CRC-16-IBM / CRC-16-ANSI
393 crc
= initial_value
; # Initial value
395 crc ^
= b
<< 8; # XOR byte into CRC top byte
396 for _
in range(8): # Process each bit
397 if crc
& 0x8000: # If the top bit is set
398 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
400 crc
= crc
<< 1; # Just shift left
401 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
404 # initial_value can be 0xFFFF or 0x0000
405 def crc16_ibm(msg
, initial_value
=0xFFFF):
406 return crc16_ansi(msg
, initial_value
);
408 # initial_value is 0xFFFF
410 return crc16_ansi(msg
, 0xFFFF);
412 # initial_value can be 0xFFFF, 0x1D0F or 0x0000
413 def crc16_ccitt(msg
, initial_value
=0xFFFF):
414 # CRC-16-CCITT polynomial
415 poly
= 0x1021; # Polynomial for CRC-16-CCITT
416 # Use the specified initial value
419 crc ^
= b
<< 8; # XOR byte into CRC top byte
420 for _
in range(8): # Process each bit
421 if crc
& 0x8000: # If the top bit is set
422 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
424 crc
= crc
<< 1; # Just shift left
425 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
428 # initial_value can be 0x42F0E1EBA9EA3693 or 0x0000000000000000
429 def crc64_ecma(msg
, initial_value
=0x0000000000000000):
430 # CRC-64-ECMA polynomial and initial value
431 poly
= 0x42F0E1EBA9EA3693;
432 crc
= initial_value
; # Initial value for CRC-64-ECMA
434 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
435 for _
in range(8): # Process each bit
436 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
437 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
439 crc
<<= 1; # Just shift left if the MSB is 0
440 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
443 # initial_value can be 0x000000000000001B or 0xFFFFFFFFFFFFFFFF
444 def crc64_iso(msg
, initial_value
=0xFFFFFFFFFFFFFFFF):
445 # CRC-64-ISO polynomial and initial value
446 poly
= 0x000000000000001B;
447 crc
= initial_value
; # Common initial value for CRC-64-ISO
449 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
450 for _
in range(8): # Process each bit
451 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
452 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
454 crc
<<= 1; # Just shift left if the MSB is 0
455 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
458 def GetDataFromArray(data
, path
, default
=None):
462 element
= element
[key
];
464 except (KeyError, TypeError, IndexError):
467 def GetDataFromArrayAlt(structure
, path
, default
=None):
470 if isinstance(element
, dict) and key
in element
:
471 element
= element
[key
];
472 elif isinstance(element
, list) and isinstance(key
, int) and -len(element
) <= key
< len(element
):
473 element
= element
[key
];
478 def GetHeaderChecksum(inlist
=[], checksumtype
="crc32", encodedata
=True, formatspecs
=__file_format_list__
):
479 if isinstance(inlist
, list):
480 fileheader
= AppendNullBytes(inlist
, formatspecs
[5]);
482 fileheader
= AppendNullByte(inlist
, formatspecs
[5]);
484 fileheader
= fileheader
.encode('UTF-8');
485 if(checksumtype
=="none" or checksumtype
==""):
486 catfileheadercshex
= format(0, 'x').lower();
487 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
488 catfileheadercshex
= format(crc16(fileheader
) & 0xffff, '04x').lower();
489 elif(checksumtype
=="crc16_ccitt"):
490 catfileheadercshex
= format(crc16_ccitt(fileheader
) & 0xffff, '04x').lower();
491 elif(checksumtype
=="adler32"):
492 catfileheadercshex
= format(zlib
.adler32(fileheader
) & 0xffffffff, '08x').lower();
493 elif(checksumtype
=="crc32"):
494 catfileheadercshex
= format(crc32(fileheader
) & 0xffffffff, '08x').lower();
495 elif(checksumtype
=="crc64_ecma"):
496 catfileheadercshex
= format(crc64_ecma(fileheader
) & 0xffffffffffffffff, '016x').lower();
497 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
498 catfileheadercshex
= format(crc64_iso(fileheader
) & 0xffffffffffffffff, '016x').lower();
499 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
500 checksumoutstr
= hashlib
.new(checksumtype
);
501 checksumoutstr
.update(fileheader
);
502 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
504 catfileheadercshex
= format(0, 'x').lower();
505 return catfileheadercshex
;
507 def GetFileChecksum(instr
, checksumtype
="crc32", encodedata
=True, formatspecs
=__file_format_list__
):
509 instr
= instr
.encode('UTF-8');
510 if(checksumtype
=="none" or checksumtype
==""):
511 catinstrcshex
= format(0, 'x').lower();
512 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
513 catinstrcshex
= format(crc16(instr
) & 0xffff, '04x').lower();
514 elif(checksumtype
=="crc16_ccitt"):
515 catinstrcshex
= format(crc16_ccitt(instr
) & 0xffff, '04x').lower();
516 elif(checksumtype
=="adler32"):
517 catinstrcshex
= format(zlib
.adler32(instr
) & 0xffffffff, '08x').lower();
518 elif(checksumtype
=="crc32"):
519 catinstrcshex
= format(crc32(instr
) & 0xffffffff, '08x').lower();
520 elif(checksumtype
=="crc64_ecma"):
521 catinstrcshex
= format(crc64_ecma(instr
) & 0xffffffffffffffff, '016x').lower();
522 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
523 catinstrcshex
= format(crc64_iso(instr
) & 0xffffffffffffffff, '016x').lower();
524 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
525 checksumoutstr
= hashlib
.new(checksumtype
);
526 checksumoutstr
.update(instr
);
527 catinstrcshex
= checksumoutstr
.hexdigest().lower();
529 catinstrcshex
= format(0, 'x').lower();
530 return catinstrcshex
;
532 def ValidateHeaderChecksum(inlist
=[], checksumtype
="crc32", inchecksum
="0", formatspecs
=__file_format_list__
):
533 catfileheadercshex
= GetHeaderChecksum(inlist
, checksumtype
, True, formatspecs
);
534 inchecksum
= inchecksum
.lower();
535 catfileheadercshex
= catfileheadercshex
.lower();
536 if(inchecksum
==catfileheadercshex
):
541 def ValidateFileChecksum(infile
, checksumtype
="crc32", inchecksum
="0", formatspecs
=__file_format_list__
):
542 catinfilecshex
= GetFileChecksum(infile
, checksumtype
, True, formatspecs
);
543 inchecksum
= inchecksum
.lower();
544 catinfilecshex
= catinfilecshex
.lower();
545 if(inchecksum
==catinfilecshex
):
550 def ReadTillNullByte(fp
, delimiter
=__file_format_delimiter__
):
553 nullbyte
= delimiter
.encode("UTF-8");
555 curbyte
= fp
.read(1);
556 if(curbyte
==nullbyte
or not curbyte
):
558 curfullbyte
= curfullbyte
+ curbyte
;
559 return curfullbyte
.decode('UTF-8');
561 def ReadUntilNullByte(fp
, delimiter
=__file_format_delimiter__
):
562 return ReadTillNullByte(fp
, delimiter
);
564 def SeekToEndOfFile(fp
):
568 if(lasttell
==fp
.tell()):
570 lasttell
= fp
.tell();
573 def ReadFileHeaderData(fp
, rounds
=0, delimiter
=__file_format_delimiter__
):
577 while(rocount
<roend
):
578 HeaderOut
.append(ReadTillNullByte(fp
, delimiter
));
579 rocount
= rocount
+ 1;
582 def ReadFileHeaderDataBySize(fp
, delimiter
=__file_format_delimiter__
):
583 headerpresize
= ReadTillNullByte(fp
, delimiter
);
584 headersize
= int(headerpresize
, 16);
587 headercontent
= str(fp
.read(headersize
).decode('UTF-8')).split(delimiter
);
590 roend
= int(len(headercontent
));
591 HeaderOut
= [headerpresize
];
592 while(rocount
<roend
):
593 HeaderOut
.append(headercontent
[rocount
]);
594 rocount
= rocount
+ 1;
597 def ReadFileHeaderDataWoSize(fp
, delimiter
=__file_format_delimiter__
):
598 preheaderdata
= ReadFileHeaderData(fp
, 2, delimiter
);
599 headersize
= int(preheaderdata
[0], 16);
600 headernumfields
= int(preheaderdata
[1], 16);
601 if(headersize
<=0 or headernumfields
<=0):
603 headerdata
= ReadFileHeaderData(fp
, headernumfields
, delimiter
);
604 HeaderOut
= preheaderdata
+ headerdata
;
607 def ReadFileHeaderDataBySizeWithContent(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
608 delimiter
= formatspecs
[5];
609 fheaderstart
= fp
.tell();
610 HeaderOut
= ReadFileHeaderDataBySize(fp
, delimiter
);
611 if(len(HeaderOut
)==0):
613 if(re
.findall("^[.|/]", HeaderOut
[3])):
614 fname
= HeaderOut
[3];
616 fname
= "./"+HeaderOut
[3];
617 fchecksumtype
= HeaderOut
[-3].lower();
618 fcs
= HeaderOut
[-2].lower();
619 fccs
= HeaderOut
[-1].lower();
620 fsize
= int(HeaderOut
[5], 16);
621 newfcs
= GetHeaderChecksum(HeaderOut
[:-2], HeaderOut
[-3].lower(), True, formatspecs
);
622 if(fcs
!=newfcs
and not skipchecksum
):
623 VerbosePrintOut("File Header Checksum Error with file " + fname
+ " at offset " + str(fheaderstart
));
625 fhend
= fp
.tell() - 1;
626 fcontentstart
= fp
.tell();
627 fcontents
= "".encode('UTF-8');
628 if(fsize
>0 and not listonly
):
629 fcontents
= fp
.read(fsize
);
630 elif(fsize
>0 and listonly
):
632 newfccs
= GetFileChecksum(fcontents
, HeaderOut
[-3].lower(), False, formatspecs
);
633 if(fccs
!=newfccs
and not skipchecksum
and not listonly
):
634 VerbosePrintOut("File Content Checksum Error with file " + fname
+ " at offset " + str(fcontentstart
));
637 fcontentend
= fp
.tell() - 1;
638 HeaderOut
.append(fcontents
);
641 def ReadFileHeaderDataBySizeWithContentToArray(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
642 delimiter
= formatspecs
[5];
643 fheaderstart
= fp
.tell();
645 HeaderOut
= ReadFileHeaderDataBySize(fp
, delimiter
);
647 HeaderOut
= ReadFileHeaderDataWoSize(fp
, delimiter
);
648 if(len(HeaderOut
)==0):
650 fheadsize
= int(HeaderOut
[0], 16);
651 fnumfields
= int(HeaderOut
[1], 16);
652 ftype
= int(HeaderOut
[2], 16);
653 if(re
.findall("^[.|/]", HeaderOut
[3])):
654 fname
= HeaderOut
[3];
656 fname
= "./"+HeaderOut
[3];
657 fbasedir
= os
.path
.dirname(fname
);
658 flinkname
= HeaderOut
[4];
659 fsize
= int(HeaderOut
[5], 16);
660 fatime
= int(HeaderOut
[6], 16);
661 fmtime
= int(HeaderOut
[7], 16);
662 fctime
= int(HeaderOut
[8], 16);
663 fbtime
= int(HeaderOut
[9], 16);
664 fmode
= int(HeaderOut
[10], 16);
665 fchmode
= stat
.S_IMODE(fmode
);
666 ftypemod
= stat
.S_IFMT(fmode
);
667 fwinattributes
= int(HeaderOut
[11], 16);
668 fuid
= int(HeaderOut
[12], 16);
669 funame
= HeaderOut
[13];
670 fgid
= int(HeaderOut
[14], 16);
671 fgname
= HeaderOut
[15];
672 fid
= int(HeaderOut
[16], 16);
673 finode
= int(HeaderOut
[17], 16);
674 flinkcount
= int(HeaderOut
[18], 16);
675 fdev_minor
= int(HeaderOut
[19], 16);
676 fdev_major
= int(HeaderOut
[20], 16);
677 frdev_minor
= int(HeaderOut
[21], 16);
678 frdev_major
= int(HeaderOut
[22], 16);
679 fextrasize
= int(HeaderOut
[23], 16);
680 fextrafields
= int(HeaderOut
[24], 16);
681 extrafieldslist
= [];
683 extraend
= extrastart
+ fextrafields
;
684 extrafieldslist
= [];
685 if(extrastart
<extraend
):
686 extrafieldslist
.append(HeaderOut
[extrastart
]);
687 extrastart
= extrastart
+ 1;
688 fchecksumtype
= HeaderOut
[extrastart
].lower();
689 fcs
= HeaderOut
[extrastart
+ 1].lower();
690 fccs
= HeaderOut
[extrastart
+ 2].lower();
691 newfcs
= GetHeaderChecksum(HeaderOut
[:-2], HeaderOut
[-3].lower(), True, formatspecs
);
692 if(fcs
!=newfcs
and not skipchecksum
):
693 VerbosePrintOut("File Header Checksum Error with file " + fname
+ " at offset " + str(fheaderstart
));
695 fhend
= fp
.tell() - 1;
696 fcontentstart
= fp
.tell();
697 fcontents
= "".encode('UTF-8');
698 pyhascontents
= False;
699 if(fsize
>0 and not listonly
):
700 fcontents
= fp
.read(fsize
);
701 pyhascontents
= True;
702 elif(fsize
>0 and listonly
):
704 pyhascontents
= False;
705 newfccs
= GetFileChecksum(fcontents
, HeaderOut
[-3].lower(), False, formatspecs
);
706 if(fccs
!=newfccs
and not skipchecksum
and not listonly
):
707 VerbosePrintOut("File Content Checksum Error with file " + fname
+ " at offset " + str(fcontentstart
));
710 fcontentend
= fp
.tell() - 1;
711 catlist
= {'fheadersize': fheadsize
, 'fhstart': fheaderstart
, 'fhend': fhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fwinattributes': fwinattributes
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': fchecksumtype
, 'fnumfields': fnumfields
+ 2, 'frawheader': HeaderOut
, 'fextrafields': fextrafields
, 'fextrafieldsize': fextrasize
, 'fextralist': extrafieldslist
, 'fheaderchecksum': fcs
, 'fcontentchecksum': fccs
, 'fhascontents': pyhascontents
, 'fcontentstart': fcontentstart
, 'fcontentend': fcontentend
, 'fcontents': fcontents
};
714 def ReadFileHeaderDataBySizeWithContentToList(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
715 delimiter
= formatspecs
[5];
716 fheaderstart
= fp
.tell();
718 HeaderOut
= ReadFileHeaderDataBySize(fp
, delimiter
);
720 HeaderOut
= ReadFileHeaderDataWoSize(fp
, delimiter
);
721 if(len(HeaderOut
)==0):
723 fheadsize
= int(HeaderOut
[0], 16);
724 fnumfields
= int(HeaderOut
[1], 16);
725 ftype
= int(HeaderOut
[2], 16);
726 if(re
.findall("^[.|/]", HeaderOut
[3])):
727 fname
= HeaderOut
[3];
729 fname
= "./"+HeaderOut
[3];
730 fbasedir
= os
.path
.dirname(fname
);
731 flinkname
= HeaderOut
[4];
732 fsize
= int(HeaderOut
[5], 16);
733 fatime
= int(HeaderOut
[6], 16);
734 fmtime
= int(HeaderOut
[7], 16);
735 fctime
= int(HeaderOut
[8], 16);
736 fbtime
= int(HeaderOut
[9], 16);
737 fmode
= int(HeaderOut
[10], 16);
738 fchmode
= stat
.S_IMODE(fmode
);
739 ftypemod
= stat
.S_IFMT(fmode
);
740 fwinattributes
= int(HeaderOut
[11], 16);
741 fuid
= int(HeaderOut
[12], 16);
742 funame
= HeaderOut
[13];
743 fgid
= int(HeaderOut
[14], 16);
744 fgname
= HeaderOut
[15];
745 fid
= int(HeaderOut
[16], 16);
746 finode
= int(HeaderOut
[17], 16);
747 flinkcount
= int(HeaderOut
[18], 16);
748 fdev_minor
= int(HeaderOut
[19], 16);
749 fdev_major
= int(HeaderOut
[20], 16);
750 frdev_minor
= int(HeaderOut
[21], 16);
751 frdev_major
= int(HeaderOut
[22], 16);
752 fextrasize
= int(HeaderOut
[23], 16);
753 fextrafields
= int(HeaderOut
[24], 16);
754 extrafieldslist
= [];
756 extraend
= extrastart
+ fextrafields
;
757 extrafieldslist
= [];
758 if(extrastart
<extraend
):
759 extrafieldslist
.append(HeaderOut
[extrastart
]);
760 extrastart
= extrastart
+ 1;
761 fchecksumtype
= HeaderOut
[extrastart
].lower();
762 fcs
= HeaderOut
[extrastart
+ 1].lower();
763 fccs
= HeaderOut
[extrastart
+ 2].lower();
764 newfcs
= GetHeaderChecksum(HeaderOut
[:-2], HeaderOut
[-3].lower(), True, formatspecs
);
765 if(fcs
!=newfcs
and not skipchecksum
):
766 VerbosePrintOut("File Header Checksum Error with file " + fname
+ " at offset " + str(fheaderstart
));
768 fhend
= fp
.tell() - 1;
769 fcontentstart
= fp
.tell();
770 fcontents
= "".encode('UTF-8');
771 pyhascontents
= False;
772 if(fsize
>0 and not listonly
):
773 fcontents
= fp
.read(fsize
);
774 pyhascontents
= True;
775 elif(fsize
>0 and listonly
):
777 pyhascontents
= False;
778 newfccs
= GetFileChecksum(fcontents
, HeaderOut
[-3].lower(), False, formatspecs
);
779 if(fccs
!=newfccs
and not skipchecksum
and not listonly
):
780 VerbosePrintOut("File Content Checksum Error with file " + fname
+ " at offset " + str(fcontentstart
));
783 fcontentend
= fp
.tell() - 1;
784 catlist
= [ftype
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fid
, finode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrafieldslist
, fchecksumtype
, fcontents
];
787 def ReadFileDataBySizeWithContent(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
788 delimiter
= formatspecs
[5];
792 catheader
= ReadFileHeaderData(fp
, 4, delimiter
);
795 headercheck
= ValidateHeaderChecksum(catheader
[:-1], catheader
[2], catheader
[3], formatspecs
);
796 if(not headercheck
and not skipchecksum
):
797 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
799 fnumfiles
= int(catheader
[1], 16);
802 while(countnum
< fnumfiles
):
803 HeaderOut
= ReadFileHeaderDataBySizeWithContent(fp
, listonly
, skipchecksum
, formatspecs
)
804 if(len(HeaderOut
)==0):
806 flist
.append(HeaderOut
);
807 countnum
= countnum
+ 1;
810 def ReadFileDataBySizeWithContentToArray(fp
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
811 delimiter
= formatspecs
[5];
815 catheader
= ReadFileHeaderData(fp
, 4, delimiter
);
818 headercheck
= ValidateHeaderChecksum(catheader
[:-1], catheader
[2], catheader
[3], formatspecs
);
819 if(not headercheck
and not skipchecksum
):
820 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
822 catstring
= catheader
[0];
823 catversion
= re
.findall(r
"([\d]+)$", catstring
);
824 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
825 fprenumfiles
= catheader
[1];
826 fnumfiles
= int(fprenumfiles
, 16);
827 fprechecksumtype
= catheader
[2];
828 fprechecksum
= catheader
[3];
829 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
830 if(seekstart
<0 and seekstart
>fnumfiles
):
832 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
834 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
835 seekend
= fnumfiles
- abs(seekend
);
838 while(il
< seekstart
):
839 prefhstart
= fp
.tell();
840 preheaderdata
= ReadFileHeaderDataBySize(fp
, formatspecs
[5]);
841 if(len(preheaderdata
)==0):
843 prefsize
= int(preheaderdata
[5], 16);
844 prenewfcs
= GetHeaderChecksum(preheaderdata
[:-2], preheaderdata
[-3].lower(), True, formatspecs
);
845 prefcs
= preheaderdata
[-2];
846 if(prefcs
!=prenewfcs
and not skipchecksum
):
847 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
849 valid_archive
= False;
850 invalid_archive
= True;
851 prefhend
= fp
.tell() - 1;
852 prefcontentstart
= fp
.tell();
854 pyhascontents
= False;
856 prefcontents
= fp
.read(prefsize
);
857 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
858 prefccs
= preheaderdata
[-1];
859 pyhascontents
= True;
860 if(prefccs
!=prenewfccs
and not skipchecksum
):
861 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
866 countnum
= seekstart
;
867 while(countnum
< seekend
):
868 catlist
['ffilelist'].update({realidnum
: {'fid': realidnum
, 'fidalt': realidnum
}});
869 HeaderOut
= ReadFileHeaderDataBySizeWithContentToArray(fp
, listonly
, skipchecksum
, formatspecs
);
872 catlist
['ffilelist'][realidnum
].update(HeaderOut
);
873 countnum
= countnum
+ 1;
874 realidnum
= realidnum
+ 1;
877 def ReadFileDataBySizeWithContentToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
878 if(isinstance(infile
, dict)):
879 listcatfiles
= infile
;
881 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
883 listcatfiles
= ReadFileDataBySizeWithContentToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
);
884 if(not listcatfiles
):
886 catarray
= {'list': listcatfiles
, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
887 lenlist
= len(listcatfiles
['ffilelist']);
889 lcfx
= int(listcatfiles
['fnumfiles']);
890 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
893 lcfx
= int(listcatfiles
['fnumfiles']);
895 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
896 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
897 catarray
['filetoid'].update(filetoidarray
);
898 catarray
['idtofile'].update(idtofilearray
);
899 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
900 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
901 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
902 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
903 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
904 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
905 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
906 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
907 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
908 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
909 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
910 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
911 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
912 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
913 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
914 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
915 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
916 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
917 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
918 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
919 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
920 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
921 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
922 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
923 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
924 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
925 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
926 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
927 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
928 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
929 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
933 def ReadFileDataBySizeWithContentToList(fp
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
934 delimiter
= formatspecs
[5];
938 catheader
= ReadFileHeaderData(fp
, 4, delimiter
);
941 headercheck
= ValidateHeaderChecksum(catheader
[:-1], catheader
[2], catheader
[3], formatspecs
);
942 if(not headercheck
and not skipchecksum
):
943 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
945 catstring
= catheader
[0];
946 catversion
= re
.findall(r
"([\d]+)$", catstring
);
947 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
948 fprenumfiles
= catheader
[1];
949 fnumfiles
= int(fprenumfiles
, 16);
950 fprechecksumtype
= catheader
[2];
951 fprechecksum
= catheader
[3];
953 if(seekstart
<0 and seekstart
>fnumfiles
):
955 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
957 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
958 seekend
= fnumfiles
- abs(seekend
);
961 while(il
< seekstart
):
962 prefhstart
= fp
.tell();
963 preheaderdata
= ReadFileHeaderDataBySize(fp
, formatspecs
[5]);
964 if(len(preheaderdata
)==0):
966 prefsize
= int(preheaderdata
[5], 16);
967 prenewfcs
= GetHeaderChecksum(preheaderdata
[:-2], preheaderdata
[-3].lower(), True, formatspecs
);
968 prefcs
= preheaderdata
[-2];
969 if(prefcs
!=prenewfcs
and not skipchecksum
):
970 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
972 valid_archive
= False;
973 invalid_archive
= True;
974 prefhend
= fp
.tell() - 1;
975 prefcontentstart
= fp
.tell();
977 pyhascontents
= False;
979 prefcontents
= fp
.read(prefsize
);
980 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
981 prefccs
= preheaderdata
[-1];
982 pyhascontents
= True;
983 if(prefccs
!=prenewfccs
and not skipchecksum
):
984 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
989 countnum
= seekstart
;
990 while(countnum
< seekend
):
991 HeaderOut
= ReadFileHeaderDataBySizeWithContentToList(fp
, listonly
, skipchecksum
, formatspecs
);
994 catlist
.append(HeaderOut
);
995 countnum
= countnum
+ 1;
996 realidnum
= realidnum
+ 1;
999 def ReadInFileBySizeWithContentToArray(infile
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
1000 delimiter
= formatspecs
[5];
1001 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1004 fp
= UncompressArchiveFile(fp
, formatspecs
);
1005 checkcompressfile
= CheckCompressionSubType(fp
, formatspecs
, True);
1006 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
1013 if(hasattr(sys
.stdin
, "buffer")):
1014 shutil
.copyfileobj(sys
.stdin
.buffer, fp
);
1016 shutil
.copyfileobj(sys
.stdin
, fp
);
1018 fp
= UncompressArchiveFile(fp
, formatspecs
);
1022 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
1023 fp
= download_file_from_internet_file(infile
);
1024 fp
= UncompressArchiveFile(fp
, formatspecs
);
1030 infile
= RemoveWindowsPath(infile
);
1031 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
1032 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
1034 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
1035 if(not compresscheck
):
1036 fextname
= os
.path
.splitext(infile
)[1];
1037 if(fextname
==".gz"):
1038 compresscheck
= "gzip";
1039 elif(fextname
==".bz2"):
1040 compresscheck
= "bzip2";
1041 elif(fextname
==".zst"):
1042 compresscheck
= "zstd";
1043 elif(fextname
==".lz4" or fextname
==".clz4"):
1044 compresscheck
= "lz4";
1045 elif(fextname
==".lzo" or fextname
==".lzop"):
1046 compresscheck
= "lzo";
1047 elif(fextname
==".lzma" or fextname
==".xz"):
1048 compresscheck
= "lzma";
1051 if(not compresscheck
):
1053 fp
= UncompressFile(infile
, formatspecs
, "rb");
1054 return ReadFileDataBySizeWithContentToArray(fp
, listonly
, skipchecksum
, formatspecs
);
1056 def ReadInFileBySizeWithContentToList(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
1057 delimiter
= formatspecs
[5];
1058 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1061 fp
= UncompressArchiveFile(fp
, formatspecs
);
1062 checkcompressfile
= CheckCompressionSubType(fp
, formatspecs
, True);
1063 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
1070 if(hasattr(sys
.stdin
, "buffer")):
1071 shutil
.copyfileobj(sys
.stdin
.buffer, fp
);
1073 shutil
.copyfileobj(sys
.stdin
, fp
);
1075 fp
= UncompressArchiveFile(fp
, formatspecs
);
1079 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
1080 fp
= download_file_from_internet_file(infile
);
1081 fp
= UncompressArchiveFile(fp
, formatspecs
);
1087 infile
= RemoveWindowsPath(infile
);
1088 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
1089 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
1091 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
1092 if(not compresscheck
):
1093 fextname
= os
.path
.splitext(infile
)[1];
1094 if(fextname
==".gz"):
1095 compresscheck
= "gzip";
1096 elif(fextname
==".bz2"):
1097 compresscheck
= "bzip2";
1098 elif(fextname
==".zst"):
1099 compresscheck
= "zstd";
1100 elif(fextname
==".lz4" or fextname
==".clz4"):
1101 compresscheck
= "lz4";
1102 elif(fextname
==".lzo" or fextname
==".lzop"):
1103 compresscheck
= "lzo";
1104 elif(fextname
==".lzma" or fextname
==".xz"):
1105 compresscheck
= "lzma";
1108 if(not compresscheck
):
1110 fp
= UncompressFile(infile
, formatspecs
, "rb");
1111 return ReadFileDataBySizeWithContentToList(fp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
);
1113 def ReadInFileBySizeWithContentToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
1114 if(isinstance(infile
, dict)):
1115 listcatfiles
= infile
;
1117 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
1118 infile
= RemoveWindowsPath(infile
);
1119 listcatfiles
= ReadInFileBySizeWithContentToArray(infile
, listonly
, skipchecksum
, formatspecs
);
1120 if(not listcatfiles
):
1122 catarray
= {'list': listcatfiles
, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
1123 lenlist
= len(listcatfiles
['ffilelist']);
1125 lcfx
= int(listcatfiles
['fnumfiles']);
1126 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
1127 lcfx
= int(lenlist
);
1129 lcfx
= int(listcatfiles
['fnumfiles']);
1131 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
1132 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
1133 catarray
['filetoid'].update(filetoidarray
);
1134 catarray
['idtofile'].update(idtofilearray
);
1135 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
1136 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
1137 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
1138 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
1139 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
1140 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
1141 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
1142 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
1143 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
1144 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
1145 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
1146 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
1147 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
1148 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
1149 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
1150 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
1151 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
1152 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
1153 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
1154 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
1155 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
1156 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
1157 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
1158 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
1159 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
1160 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
1161 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
1162 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
1163 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
1164 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
1165 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
1169 def AppendNullByte(indata
, delimiter
=__file_format_delimiter__
):
1170 outdata
= str(indata
) + delimiter
;
1173 def AppendNullBytes(indata
=[], delimiter
=__file_format_delimiter__
):
1178 outdata
= outdata
+ AppendNullByte(indata
[inum
], delimiter
);
1182 def AppendFileHeader(fp
, numfiles
, checksumtype
="crc32", formatspecs
=__file_format_list__
):
1183 delimiter
= formatspecs
[5];
1184 catver
= formatspecs
[6];
1185 fileheaderver
= str(int(catver
.replace(".", "")));
1186 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
1187 fp
.write(fileheader
.encode('UTF-8'));
1188 fnumfiles
= format(int(numfiles
), 'x').lower();
1189 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
1190 catfileheadercshex
= GetFileChecksum(fileheader
+ fnumfilesa
, checksumtype
, True, formatspecs
);
1191 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
1192 fp
.write(fnumfilesa
.encode('UTF-8'));
1195 os
.fsync(fp
.fileno());
1196 except io
.UnsupportedOperation
:
1198 except AttributeError:
1202 def AppendFileHeaderWithContent(fp
, filevalues
=[], extradata
=[], filecontent
="", checksumtype
="crc32", formatspecs
=__file_format_list__
):
1203 extrafields
= format(len(extradata
), 'x').lower();
1204 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
1205 if(len(extradata
)>0):
1206 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
1207 extrasizelen
= format(len(extrasizestr
), 'x').lower();
1208 catoutlen
= len(filevalues
) + len(extradata
) + 5;
1209 catoutlenhex
= format(catoutlen
, 'x').lower();
1210 catoutlist
= filevalues
;
1211 catoutlist
.insert(0, catoutlenhex
);
1212 catoutlist
.append(extrasizelen
);
1213 catoutlist
.append(extrafields
);
1214 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
1215 if(len(extradata
)>0):
1216 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
1217 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
1218 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
1219 catfilecontentcshex
= GetFileChecksum(filecontent
, checksumtype
, False, formatspecs
);
1220 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1221 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
1222 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
1223 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
1224 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1225 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
1226 nullstrecd
= formatspecs
[5].encode('UTF-8');
1227 catfileout
= catfileoutstrecd
+ filecontent
+ nullstrecd
;
1228 fp
.write(catfileout
);
1231 os
.fsync(fp
.fileno());
1232 except io
.UnsupportedOperation
:
1234 except AttributeError:
1238 def AppendFilesWithContent(infiles
, fp
, dirlistfromtxt
=False, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False):
1239 advancedlist
= formatspecs
[8];
1240 altinode
= formatspecs
[9];
1242 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1244 for line
in sys
.stdin
:
1245 infilelist
.append(line
.strip());
1246 infilelist
= list(filter(None, infilelist
));
1247 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
1248 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
1250 with
UncompressFile(infiles
, formatspecs
, "r") as finfile
:
1251 for line
in finfile
:
1252 infilelist
.append(line
.strip());
1253 infilelist
= list(filter(None, infilelist
));
1255 if(isinstance(infiles
, (list, tuple, ))):
1256 infilelist
= list(filter(None, infiles
));
1257 elif(isinstance(infiles
, (str, ))):
1258 infilelist
= list(filter(None, [infiles
]));
1260 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
1262 GetDirList
= ListDir(infilelist
, followlink
, False);
1270 inodetocatinode
= {};
1271 numfiles
= int(len(GetDirList
));
1272 fnumfiles
= format(numfiles
, 'x').lower();
1273 AppendFileHeader(fp
, fnumfiles
, checksumtype
, formatspecs
);
1274 for curfname
in GetDirList
:
1275 catfhstart
= fp
.tell();
1276 if(re
.findall("^[.|/]", curfname
)):
1279 fname
= "./"+curfname
;
1281 VerbosePrintOut(fname
);
1282 if(not followlink
or followlink
is None):
1283 fstatinfo
= os
.lstat(fname
);
1285 fstatinfo
= os
.stat(fname
);
1286 fpremode
= fstatinfo
.st_mode
;
1287 finode
= fstatinfo
.st_ino
;
1288 flinkcount
= fstatinfo
.st_nlink
;
1290 if(stat
.S_ISREG(fpremode
)):
1292 elif(stat
.S_ISLNK(fpremode
)):
1294 elif(stat
.S_ISCHR(fpremode
)):
1296 elif(stat
.S_ISBLK(fpremode
)):
1298 elif(stat
.S_ISDIR(fpremode
)):
1300 elif(stat
.S_ISFIFO(fpremode
)):
1302 elif(stat
.S_ISSOCK(fpremode
)):
1304 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
1306 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
1308 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
1313 fcurfid
= format(int(curfid
), 'x').lower();
1314 if(not followlink
and finode
!=0):
1316 if(finode
in inodelist
):
1318 flinkname
= inodetofile
[finode
];
1320 fcurinode
= format(int(finode
), 'x').lower();
1322 fcurinode
= format(int(inodetocatinode
[finode
]), 'x').lower();
1323 if(finode
not in inodelist
):
1324 inodelist
.append(finode
);
1325 inodetofile
.update({finode
: fname
});
1326 inodetocatinode
.update({finode
: curinode
});
1328 fcurinode
= format(int(finode
), 'x').lower();
1330 fcurinode
= format(int(curinode
), 'x').lower();
1331 curinode
= curinode
+ 1;
1333 fcurinode
= format(int(curinode
), 'x').lower();
1334 curinode
= curinode
+ 1;
1335 curfid
= curfid
+ 1;
1337 flinkname
= os
.readlink(fname
);
1338 fdev
= fstatinfo
.st_dev
;
1339 getfdev
= GetDevMajorMinor(fdev
);
1340 fdev_minor
= getfdev
[0];
1341 fdev_major
= getfdev
[1];
1342 frdev
= fstatinfo
.st_dev
;
1343 if(hasattr(fstatinfo
, "st_rdev")):
1344 frdev
= fstatinfo
.st_rdev
;
1346 frdev
= fstatinfo
.st_dev
;
1347 getfrdev
= GetDevMajorMinor(frdev
);
1348 frdev_minor
= getfrdev
[0];
1349 frdev_major
= getfrdev
[1];
1350 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
1351 fsize
= format(int("0"), 'x').lower();
1352 elif(ftype
==0 or ftype
==7):
1353 fsize
= format(int(fstatinfo
.st_size
), 'x').lower();
1355 fsize
= format(int(fstatinfo
.st_size
)).lower();
1356 fatime
= format(int(fstatinfo
.st_atime
), 'x').lower();
1357 fmtime
= format(int(fstatinfo
.st_mtime
), 'x').lower();
1358 fctime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1359 if(hasattr(fstatinfo
, "st_birthtime")):
1360 fbtime
= format(int(fstatinfo
.st_birthtime
), 'x').lower();
1362 fbtime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1363 fmode
= format(int(fstatinfo
.st_mode
), 'x').lower();
1364 fchmode
= format(int(stat
.S_IMODE(fstatinfo
.st_mode
)), 'x').lower();
1365 ftypemod
= format(int(stat
.S_IFMT(fstatinfo
.st_mode
)), 'x').lower();
1366 fuid
= format(int(fstatinfo
.st_uid
), 'x').lower();
1367 fgid
= format(int(fstatinfo
.st_gid
), 'x').lower();
1372 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
1373 funame
= userinfo
.pw_name
;
1382 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
1383 fgname
= groupinfo
.gr_name
;
1388 fdev_minor
= format(int(fdev_minor
), 'x').lower();
1389 fdev_major
= format(int(fdev_major
), 'x').lower();
1390 frdev_minor
= format(int(frdev_minor
), 'x').lower();
1391 frdev_major
= format(int(frdev_major
), 'x').lower();
1392 finode
= format(int(finode
), 'x').lower();
1393 flinkcount
= format(int(flinkcount
), 'x').lower();
1394 if(hasattr(fstatinfo
, "st_file_attributes")):
1395 fwinattributes
= format(int(fstatinfo
.st_file_attributes
), 'x').lower();
1397 fwinattributes
= format(int(0), 'x').lower();
1398 fcontents
= "".encode('UTF-8');
1400 if(ftype
==0 or ftype
==7):
1401 with
open(fname
, "rb") as fpc
:
1403 chunk
= fpc
.read(chunk_size
);
1407 if(followlink
and (ftype
==1 or ftype
==2)):
1408 flstatinfo
= os
.stat(flinkname
);
1409 with
open(flinkname
, "rb") as fpc
:
1411 chunk
= fpc
.read(chunk_size
);
1415 ftypehex
= format(ftype
, 'x').lower();
1416 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
];
1417 fp
= AppendFileHeaderWithContent(fp
, catoutlist
, extradata
, fcontents
, checksumtype
, formatspecs
);
1419 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
1423 def AppendListsWithContent(inlist
, fp
, dirlistfromtxt
=False, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False):
1425 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1426 GetDirList
= inlist
;
1434 inodetocatinode
= {};
1435 numfiles
= int(len(GetDirList
));
1436 fnumfiles
= format(numfiles
, 'x').lower();
1437 AppendFileHeader(fp
, fnumfiles
, checksumtype
, formatspecs
);
1438 for curfname
in GetDirList
:
1439 ftype
= format(curfname
[0], 'x').lower();
1440 if(re
.findall("^[.|/]", curfname
[1])):
1441 fname
= curfname
[1];
1443 fname
= "./"+curfname
[1];
1444 fbasedir
= os
.path
.dirname(fname
);
1445 flinkname
= curfname
[2];
1446 fsize
= format(curfname
[3], 'x').lower();
1447 fatime
= format(curfname
[4], 'x').lower();
1448 fmtime
= format(curfname
[5], 'x').lower();
1449 fctime
= format(curfname
[6], 'x').lower();
1450 fbtime
= format(curfname
[7], 'x').lower();
1451 fmode
= format(curfname
[8], 'x').lower();
1452 fwinattributes
= format(curfname
[9], 'x').lower();
1453 fuid
= format(curfname
[10], 'x').lower();
1454 funame
= curfname
[11];
1455 fgid
= format(curfname
[12], 'x').lower();
1456 fgname
= curfname
[13];
1457 fid
= format(curfname
[14], 'x').lower();
1458 finode
= format(curfname
[15], 'x').lower();
1459 flinkcount
= format(curfname
[16], 'x').lower();
1460 fdev_minor
= format(curfname
[17], 'x').lower();
1461 fdev_major
= format(curfname
[18], 'x').lower();
1462 frdev_minor
= format(curfname
[19], 'x').lower();
1463 frdev_major
= format(curfname
[20], 'x').lower();
1464 extradata
= curfname
[21];
1465 fchecksumtype
= curfname
[22];
1466 fcontents
= curfname
[23];
1467 catoutlist
= [ftype
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fid
, finode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
];
1468 fp
= AppendFileHeaderWithContent(fp
, catoutlist
, extradata
, fcontents
, checksumtype
, formatspecs
);
1470 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
1474 def AppendInFileWithContent(infile
, fp
, dirlistfromtxt
=False, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False):
1475 inlist
= ReadInFileBySizeWithContentToList(infile
, 0, 0, False, False, formatspecs
);
1476 return AppendListsWithContent(inlist
, fp
, dirlistfromtxt
, filevalues
, extradata
, followlink
, checksumtype
, formatspecs
, verbose
);
1478 def AppendFilesWithContentToOutFile(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1479 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1480 if(os
.path
.exists(outfile
)):
1484 catfpfp
= BytesIO();
1485 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1487 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1490 fbasename
= os
.path
.splitext(outfile
)[0];
1491 fextname
= os
.path
.splitext(outfile
)[1];
1492 catfp
= CompressOpenFile(outfile
, compressionlevel
);
1493 catfp
= AppendFilesWithContent(infiles
, catfp
, dirlistfromtxt
, filevalues
, extradata
, followlink
, checksumtype
, formatspecs
, verbose
);
1494 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1495 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1498 os
.fsync(catfp
.fileno());
1499 except io
.UnsupportedOperation
:
1501 except AttributeError:
1505 if(hasattr(sys
.stdout
, "buffer")):
1506 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1508 shutil
.copyfileobj(catfp
, sys
.stdout
);
1509 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1510 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1512 upload_file_to_internet_file(catfp
, outfile
);
1520 def AppendListsWithContentToOutFile(inlist
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1521 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1522 if(os
.path
.exists(outfile
)):
1526 catfpfp
= BytesIO();
1527 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1529 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1532 fbasename
= os
.path
.splitext(outfile
)[0];
1533 fextname
= os
.path
.splitext(outfile
)[1];
1534 catfp
= CompressOpenFile(outfile
, compressionlevel
);
1535 catfp
= AppendListsWithContent(inlist
, catfp
, dirlistfromtxt
, filevalues
, extradata
, followlink
, checksumtype
, formatspecs
, verbose
);
1536 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1537 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1540 os
.fsync(catfp
.fileno());
1541 except io
.UnsupportedOperation
:
1543 except AttributeError:
1547 if(hasattr(sys
.stdout
, "buffer")):
1548 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1550 shutil
.copyfileobj(catfp
, sys
.stdout
);
1551 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1552 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1554 upload_file_to_internet_file(catfp
, outfile
);
1562 def AppendInFileWithContentToOutFile(infile
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1563 inlist
= ReadInFileBySizeWithContentToList(infile
, 0, 0, False, False, formatspecs
);
1564 return AppendListsWithContentToOutFile(inlist
, outfile
, dirlistfromtxt
, compression
, compressionlevel
, filevalues
, extradata
, followlink
, checksumtype
, formatspecs
, verbose
, returnfp
);
1566 def PrintPermissionString(fchmode
, ftype
):
1567 permissions
= { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
1569 for fmodval
in str(oct(fchmode
))[-3:]:
1570 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
1571 if(ftype
==0 or ftype
==7):
1572 permissionstr
= "-" + permissionstr
;
1574 permissionstr
= "h" + permissionstr
;
1576 permissionstr
= "l" + permissionstr
;
1578 permissionstr
= "c" + permissionstr
;
1580 permissionstr
= "b" + permissionstr
;
1582 permissionstr
= "d" + permissionstr
;
1584 permissionstr
= "f" + permissionstr
;
1586 permissionstr
= "D" + permissionstr
;
1588 permissionstr
= "p" + permissionstr
;
1590 permissionstr
= "w" + permissionstr
;
1592 permissionoutstr
= stat
.filemode(fchmode
);
1593 except AttributeError:
1594 permissionoutstr
= permissionstr
;
1596 permissionoutstr
= permissionstr
;
1597 return permissionoutstr
;
1599 def PrintPermissionStringAlt(fchmode
, ftype
):
1601 '0': '---', '1': '--x', '2': '-w-', '3': '-wx',
1602 '4': 'r--', '5': 'r-x', '6': 'rw-', '7': 'rwx'
1604 # Translate file mode into permission string
1605 permissionstr
= ''.join([permissions
[i
] for i
in str(oct(fchmode
))[-3:]]);
1606 # Append file type indicator
1608 0: '-', 1: 'h', 2: 'l', 3: 'c', 4: 'b',
1609 5: 'd', 6: 'f', 8: 'D', 9: 'p', 10: 'w'
1611 file_type
= type_indicators
.get(ftype
, '-');
1612 permissionstr
= file_type
+ permissionstr
;
1614 permissionoutstr
= stat
.filemode(fchmode
);
1615 except AttributeError:
1616 permissionoutstr
= permissionstr
;
1617 return permissionoutstr
;
1619 def CompressionSupport():
1620 compression_list
= [];
1623 compression_list
.append("gz");
1624 compression_list
.append("gzip");
1629 compression_list
.append("bz2");
1630 compression_list
.append("bzip2");
1635 compression_list
.append("lz4");
1640 compression_list
.append("lzo");
1641 compression_list
.append("lzop");
1646 compression_list
.append("zstd");
1647 compression_list
.append("zstandard");
1652 compression_list
.append("lzma");
1653 compression_list
.append("xz");
1656 return compression_list
;
1658 def CheckCompressionType(infile
, formatspecs
=__file_format_list__
, closefp
=True):
1659 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1663 catfp
= open(infile
, "rb");
1664 except FileNotFoundError
:
1667 prefp
= catfp
.read(2);
1669 if(prefp
==binascii
.unhexlify("1f8b")):
1672 prefp
= catfp
.read(3);
1673 if(prefp
==binascii
.unhexlify("425a68")):
1675 if(prefp
==binascii
.unhexlify("5d0000")):
1678 prefp
= catfp
.read(4);
1679 if(prefp
==binascii
.unhexlify("28b52ffd")):
1681 if(prefp
==binascii
.unhexlify("04224d18")):
1683 if(prefp
==binascii
.unhexlify("504B0304")):
1684 filetype
= "zipfile";
1686 prefp
= catfp
.read(5);
1687 if(prefp
==binascii
.unhexlify("7573746172")):
1688 filetype
= "tarfile";
1690 prefp
= catfp
.read(6);
1691 if(prefp
==binascii
.unhexlify("fd377a585a00")):
1693 if(prefp
==binascii
.unhexlify("377abcaf271c")):
1694 filetype
= "7zipfile";
1696 prefp
= catfp
.read(7);
1697 if(prefp
==binascii
.unhexlify("526172211a0700")):
1698 filetype
= "rarfile";
1699 if(prefp
==binascii
.unhexlify("43617446696c65")):
1700 filetype
= "catfile";
1702 prefp
= catfp
.read(8);
1703 if(prefp
==binascii
.unhexlify("526172211a070100")):
1704 filetype
= "rarfile";
1706 prefp
= catfp
.read(formatspecs
[3]);
1707 if(prefp
==binascii
.unhexlify(formatspecs
[4])):
1708 filetype
= formatspecs
[2];
1710 prefp
= catfp
.read(9);
1711 if(prefp
==binascii
.unhexlify("894c5a4f000d0a1a0a")):
1714 prefp
= catfp
.read(10);
1715 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
1716 filetype
= "tarfile";
1722 def CheckCompressionTypeFromString(instring
, formatspecs
=__file_format_list__
, closefp
=True):
1724 instringsfile
= BytesIO(instring
);
1726 instringsfile
= BytesIO(instring
.encode("UTF-8"));
1727 return CheckCompressionType(instringsfile
, formatspecs
, closefp
);
1729 def GetCompressionMimeType(infile
, formatspecs
=__file_format_list__
):
1730 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
1731 if(compresscheck
=="gzip" or compresscheck
=="gz"):
1732 return archivefile_gzip_mimetype
;
1733 if(compresscheck
=="bzip2" or compresscheck
=="bz2"):
1734 return archivefile_bzip2_mimetype
;
1735 if(compresscheck
=="zstd" or compresscheck
=="zstandard"):
1736 return archivefile_zstandard_mimetype
;
1737 if(compresscheck
=="lz4"):
1738 return archivefile_lz4_mimetype
;
1739 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
1740 return archivefile_lzop_mimetype
;
1741 if(compresscheck
=="lzma"):
1742 return archivefile_lzma_mimetype
;
1743 if(compresscheck
=="xz"):
1744 return archivefile_xz_mimetype
;
1745 if(compresscheck
=="catfile" or compresscheck
=="cat" or compresscheck
==formatspecs
[2]):
1746 return archivefile_cat_mimetype
;
1747 if(not compresscheck
):
1751 def UncompressArchiveFile(fp
, formatspecs
=__file_format_list__
):
1752 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
1754 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
1755 if(compresscheck
=="gzip"):
1760 catfp
= gzip
.GzipFile(fileobj
=fp
, mode
="rb");
1761 if(compresscheck
=="bzip2"):
1767 catfp
.write(bz2
.decompress(fp
.read()));
1768 if(compresscheck
=="zstd"):
1774 catfp
.write(zstandard
.decompress(fp
.read()));
1775 if(compresscheck
=="lz4"):
1781 catfp
.write(lz4
.frame
.decompress(fp
.read()));
1782 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
1788 catfp
.write(lzo
.decompress(fp
.read()));
1789 if(compresscheck
=="lzma" or compresscheck
=="xz"):
1795 catfp
.write(lzma
.decompress(fp
.read()));
1796 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[2]):
1798 if(not compresscheck
):
1804 with fp
as fpcontent
:
1806 catfp
.write(lzma
.decompress(fp
.read()));
1807 except lzma
.LZMAError
:
1811 create_alias_function("Uncompress", __file_format_name__
, "", UncompressArchiveFile
);
1813 def UncompressFile(infile
, formatspecs
=__file_format_list__
, mode
="rb"):
1814 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
1815 if(sys
.version_info
[0]==2 and compresscheck
):
1821 if(compresscheck
=="gzip"):
1827 filefp
= gzip
.open(infile
, mode
, encoding
="UTF-8");
1828 except (ValueError, TypeError) as e
:
1829 filefp
= gzip
.open(infile
, mode
);
1830 if(compresscheck
=="bzip2"):
1836 filefp
= bz2
.open(infile
, mode
, encoding
="UTF-8");
1837 except (ValueError, TypeError) as e
:
1838 filefp
= bz2
.open(infile
, mode
);
1839 if(compresscheck
=="zstd"):
1845 filefp
= zstandard
.open(infile
, mode
, encoding
="UTF-8");
1846 except (ValueError, TypeError) as e
:
1847 filefp
= zstandard
.open(infile
, mode
);
1848 if(compresscheck
=="lz4"):
1854 filefp
= lz4
.frame
.open(infile
, mode
, encoding
="UTF-8");
1855 except (ValueError, TypeError) as e
:
1856 filefp
= lz4
.frame
.open(infile
, mode
);
1857 if(compresscheck
=="lzo"):
1863 filefp
= lzo
.open(infile
, mode
, encoding
="UTF-8");
1864 except (ValueError, TypeError) as e
:
1865 filefp
= lzo
.open(infile
, mode
);
1866 if(compresscheck
=="lzma"):
1872 filefp
= lzma
.open(infile
, mode
, encoding
="UTF-8");
1873 except (ValueError, TypeError) as e
:
1874 filefp
= lzma
.open(infile
, mode
);
1875 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[2]):
1877 filefp
= open(infile
, mode
, encoding
="UTF-8");
1878 except (ValueError, TypeError) as e
:
1879 filefp
= open(infile
, mode
);
1880 if(not compresscheck
):
1882 filefp
= open(infile
, mode
, encoding
="UTF-8");
1883 except (ValueError, TypeError) as e
:
1884 filefp
= open(infile
, mode
);
1885 except FileNotFoundError
:
1889 def UncompressString(infile
):
1890 compresscheck
= CheckCompressionTypeFromString(infile
, formatspecs
, False);
1891 if(compresscheck
=="gzip"):
1896 fileuz
= gzip
.decompress(infile
);
1897 if(compresscheck
=="bzip2"):
1902 fileuz
= bz2
.decompress(infile
);
1903 if(compresscheck
=="zstd"):
1908 fileuz
= zstandard
.decompress(infile
);
1909 if(compresscheck
=="lz4"):
1914 fileuz
= lz4
.frame
.decompress(infile
);
1915 if(compresscheck
=="lzo"):
1920 fileuz
= lzo
.decompress(infile
);
1921 if(compresscheck
=="lzma"):
1926 fileuz
= lzma
.decompress(infile
);
1927 if(not compresscheck
):
1929 if(hasattr(fileuz
, 'decode')):
1930 fileuz
= fileuz
.decode("UTF-8");
1933 def UncompressStringAlt(infile
):
1934 filefp
= StringIO();
1935 outstring
= UncompressString(infile
);
1936 filefp
.write(outstring
);
1940 def CheckCompressionSubType(infile
, formatspecs
=__file_format_list__
, closefp
=True):
1941 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
1942 if(not compresscheck
):
1943 fextname
= os
.path
.splitext(infile
)[1];
1944 if(fextname
==".gz"):
1945 compresscheck
= "gzip";
1946 elif(fextname
==".bz2"):
1947 compresscheck
= "bzip2";
1948 elif(fextname
==".zst"):
1949 compresscheck
= "zstd";
1950 elif(fextname
==".lz4"):
1951 compresscheck
= "lz4";
1952 elif(fextname
==".lzo" or fextname
==".lzop"):
1953 compresscheck
= "lzo";
1954 elif(fextname
==".lzma" or fextname
==".xz"):
1955 compresscheck
= "lzma";
1958 if(not compresscheck
):
1960 if(compresscheck
=="catfile"):
1962 if(compresscheck
==formatspecs
[2]):
1963 return formatspecs
[2];
1964 if(compresscheck
=="tarfile"):
1966 if(compresscheck
=="zipfile"):
1968 if(rarfile_support
and compresscheck
=="rarfile"):
1970 if(py7zr_support
and compresscheck
=="7zipfile"):
1972 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1973 catfp
= UncompressArchiveFile(infile
, formatspecs
[2]);
1976 if(compresscheck
=="gzip"):
1981 catfp
= gzip
.GzipFile(infile
, "rb");
1982 if(compresscheck
=="bzip2"):
1987 catfp
= bz2
.BZ2File(infile
, "rb");
1988 if(compresscheck
=="lz4"):
1993 catfp
= lz4
.frame
.open(infile
, "rb");
1994 if(compresscheck
=="zstd"):
1999 catfp
= zstandard
.open(infile
, "rb");
2000 if(compresscheck
=="lzma" or compresscheck
=="xz"):
2005 catfp
= lzma
.open(infile
, "rb");
2006 except FileNotFoundError
:
2009 prefp
= catfp
.read(5);
2010 if(prefp
==binascii
.unhexlify("7573746172")):
2011 filetype
= "tarfile";
2013 prefp
= catfp
.read(7);
2014 if(prefp
==binascii
.unhexlify("43617446696c65")):
2015 filetype
= "catfile";
2017 prefp
= catfp
.read(formatspecs
[3]);
2018 if(prefp
==binascii
.unhexlify(formatspecs
[4])):
2019 filetype
= formatspecs
[2];
2021 prefp
= catfp
.read(10);
2022 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
2023 filetype
= "tarfile";
2029 def GZipCompress(data
, compresslevel
=9):
2034 tmpfp
= tempfile
.NamedTemporaryFile("wb", delete
=False);
2036 tmpfp
= gzip
.GzipFile(tmpfp
.name
, mode
="wb", compresslevel
=compresslevel
);
2040 catfp
= open(tmpfp
.name
, "rb");
2041 except FileNotFoundError
:
2043 catdata
= catfp
.read();
2047 def CompressArchiveFile(fp
, compression
="auto", compressionlevel
=None, formatspecs
=__file_format_list__
):
2048 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
2051 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2053 if(compression
not in compressionlist
and compression
is None):
2054 compression
= "auto";
2055 if(compression
=="gzip"):
2061 if(compressionlevel
is None):
2062 compressionlevel
= 9;
2064 compressionlevel
= int(compressionlevel
);
2065 catfp
.write(gzip
.compress(fp
.read(), compresslevel
=compressionlevel
));
2066 if(compression
=="bzip2"):
2072 if(compressionlevel
is None):
2073 compressionlevel
= 9;
2075 compressionlevel
= int(compressionlevel
);
2076 catfp
.write(bz2
.compress(fp
.read(), compresslevel
=compressionlevel
));
2077 if(compression
=="lz4"):
2083 if(compressionlevel
is None):
2084 compressionlevel
= 9;
2086 compressionlevel
= int(compressionlevel
);
2087 catfp
.write(lz4
.frame
.compress(fp
.read(), compression_level
=compressionlevel
));
2088 if(compression
=="lzo" or compression
=="lzop"):
2094 if(compressionlevel
is None):
2095 compressionlevel
= 9;
2097 compressionlevel
= int(compressionlevel
);
2098 catfp
.write(lzo
.compress(fp
.read(), compresslevel
=compressionlevel
));
2099 if(compression
=="zstd"):
2105 if(compressionlevel
is None):
2106 compressionlevel
= 10;
2108 compressionlevel
= int(compressionlevel
);
2109 catfp
.write(zstandard
.compress(fp
.read(), level
=compressionlevel
));
2110 if(compression
=="lzma"):
2116 if(compressionlevel
is None):
2117 compressionlevel
= 9;
2119 compressionlevel
= int(compressionlevel
);
2120 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]));
2121 if(compression
=="xz"):
2127 if(compressionlevel
is None):
2128 compressionlevel
= 9;
2130 compressionlevel
= int(compressionlevel
);
2131 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]));
2132 if(compression
=="auto" or compression
is None):
2137 create_alias_function("Compress", __file_format_name__
, "", CompressArchiveFile
);
2139 def CompressOpenFile(outfile
, compressionlevel
=None):
2140 if(outfile
is None):
2142 fbasename
= os
.path
.splitext(outfile
)[0];
2143 fextname
= os
.path
.splitext(outfile
)[1];
2144 if(compressionlevel
is None and fextname
!=".zst"):
2145 compressionlevel
= 9;
2146 elif(compressionlevel
is None and fextname
==".zst"):
2147 compressionlevel
= 10;
2149 compressionlevel
= int(compressionlevel
);
2150 if(sys
.version_info
[0]==2):
2155 if(fextname
not in outextlistwd
):
2157 outfp
= open(outfile
, "wb", encoding
="UTF-8");
2158 except (ValueError, TypeError) as e
:
2159 outfp
= open(outfile
, "wb");
2160 elif(fextname
==".gz"):
2166 outfp
= gzip
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
2167 except (ValueError, TypeError) as e
:
2168 outfp
= gzip
.open(outfile
, mode
, compressionlevel
);
2169 elif(fextname
==".bz2"):
2175 outfp
= bz2
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
2176 except (ValueError, TypeError) as e
:
2177 outfp
= bz2
.open(outfile
, mode
, compressionlevel
);
2178 elif(fextname
==".zst"):
2184 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
), encoding
="UTF-8");
2185 except (ValueError, TypeError) as e
:
2186 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
));
2187 elif(fextname
==".xz"):
2193 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}], encoding
="UTF-8");
2194 except (ValueError, TypeError) as e
:
2195 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]);
2196 elif(fextname
==".lz4"):
2202 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
, encoding
="UTF-8");
2203 except (ValueError, TypeError) as e
:
2204 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
);
2205 elif(fextname
==".lzo"):
2211 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
, encoding
="UTF-8");
2212 except (ValueError, TypeError) as e
:
2213 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
);
2214 elif(fextname
==".lzma"):
2220 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}], encoding
="UTF-8");
2221 except (ValueError, TypeError) as e
:
2222 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]);
2223 except FileNotFoundError
:
2227 def GetDevMajorMinor(fdev
):
2229 if(hasattr(os
, "minor")):
2230 retdev
.append(os
.minor(fdev
));
2233 if(hasattr(os
, "major")):
2234 retdev
.append(os
.major(fdev
));
2239 def CheckSumSupport(checkfor
, guaranteed
=True):
2241 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
2243 hash_list
= sorted(list(hashlib
.algorithms_available
));
2244 checklistout
= sorted(hash_list
+ ['adler32', 'crc16', 'crc16_ansi', 'crc16_ibm', 'crc16_ccitt', 'crc32', 'crc64', 'crc64_ecma', 'crc64_iso', 'none']);
2245 if(checkfor
in checklistout
):
2250 def CheckSumSupportAlt(checkfor
, guaranteed
=True):
2252 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
2254 hash_list
= sorted(list(hashlib
.algorithms_available
));
2255 checklistout
= hash_list
;
2256 if(checkfor
in checklistout
):
2261 def PackArchiveFile(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2262 advancedlist
= formatspecs
[8];
2263 altinode
= formatspecs
[9];
2264 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2265 outfile
= RemoveWindowsPath(outfile
);
2266 checksumtype
= checksumtype
.lower();
2267 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2268 checksumtype
="crc32";
2269 if(checksumtype
=="none"):
2271 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2273 if(compression
not in compressionlist
and compression
is None):
2274 compression
= "auto";
2276 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2277 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2278 if(os
.path
.exists(outfile
)):
2283 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2285 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2288 fbasename
= os
.path
.splitext(outfile
)[0];
2289 fextname
= os
.path
.splitext(outfile
)[1];
2290 catfp
= CompressOpenFile(outfile
, compressionlevel
);
2291 catver
= formatspecs
[6];
2292 fileheaderver
= str(int(catver
.replace(".", "")));
2293 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
2294 catfp
.write(fileheader
.encode('UTF-8'));
2297 for line
in sys
.stdin
:
2298 infilelist
.append(line
.strip());
2299 infilelist
= list(filter(None, infilelist
));
2300 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
2301 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
2303 with
UncompressFile(infiles
, formatspecs
, "r") as finfile
:
2304 for line
in finfile
:
2305 infilelist
.append(line
.strip());
2306 infilelist
= list(filter(None, infilelist
));
2308 if(isinstance(infiles
, (list, tuple, ))):
2309 infilelist
= list(filter(None, infiles
));
2310 elif(isinstance(infiles
, (str, ))):
2311 infilelist
= list(filter(None, [infiles
]));
2313 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
2315 GetDirList
= ListDir(infilelist
, followlink
, False);
2323 inodetocatinode
= {};
2324 numfiles
= int(len(GetDirList
));
2325 fnumfiles
= format(int(len(GetDirList
)), 'x').lower();
2326 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2327 if(checksumtype
=="none" or checksumtype
==""):
2328 catfileheadercshex
= format(0, 'x').lower();
2329 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2330 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2331 elif(checksumtype
=="crc16_ccitt"):
2332 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2333 elif(checksumtype
=="adler32"):
2334 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2335 elif(checksumtype
=="crc32"):
2336 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2337 elif(checksumtype
=="crc64_ecma"):
2338 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2339 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2340 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2341 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2342 checksumoutstr
= hashlib
.new(checksumtype
);
2343 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
2344 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2346 catfileheadercshex
= format(0, 'x').lower
2347 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2348 catfp
.write(fnumfilesa
.encode('UTF-8'));
2351 os
.fsync(catfp
.fileno());
2352 except io
.UnsupportedOperation
:
2354 except AttributeError:
2356 for curfname
in GetDirList
:
2357 catfhstart
= catfp
.tell();
2358 if(re
.findall("^[.|/]", curfname
)):
2361 fname
= "./"+curfname
;
2363 VerbosePrintOut(fname
);
2364 if(not followlink
or followlink
is None):
2365 fstatinfo
= os
.lstat(fname
);
2367 fstatinfo
= os
.stat(fname
);
2368 fpremode
= fstatinfo
.st_mode
;
2369 finode
= fstatinfo
.st_ino
;
2370 flinkcount
= fstatinfo
.st_nlink
;
2372 if(stat
.S_ISREG(fpremode
)):
2374 elif(stat
.S_ISLNK(fpremode
)):
2376 elif(stat
.S_ISCHR(fpremode
)):
2378 elif(stat
.S_ISBLK(fpremode
)):
2380 elif(stat
.S_ISDIR(fpremode
)):
2382 elif(stat
.S_ISFIFO(fpremode
)):
2384 elif(stat
.S_ISSOCK(fpremode
)):
2386 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
2388 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
2390 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
2395 fcurfid
= format(int(curfid
), 'x').lower();
2396 if(not followlink
and finode
!=0):
2398 if(finode
in inodelist
):
2400 flinkname
= inodetofile
[finode
];
2402 fcurinode
= format(int(finode
), 'x').lower();
2404 fcurinode
= format(int(inodetocatinode
[finode
]), 'x').lower();
2405 if(finode
not in inodelist
):
2406 inodelist
.append(finode
);
2407 inodetofile
.update({finode
: fname
});
2408 inodetocatinode
.update({finode
: curinode
});
2410 fcurinode
= format(int(finode
), 'x').lower();
2412 fcurinode
= format(int(curinode
), 'x').lower();
2413 curinode
= curinode
+ 1;
2415 fcurinode
= format(int(curinode
), 'x').lower();
2416 curinode
= curinode
+ 1;
2417 curfid
= curfid
+ 1;
2419 flinkname
= os
.readlink(fname
);
2420 fdev
= fstatinfo
.st_dev
;
2421 getfdev
= GetDevMajorMinor(fdev
);
2422 fdev_minor
= getfdev
[0];
2423 fdev_major
= getfdev
[1];
2424 frdev
= fstatinfo
.st_dev
;
2425 if(hasattr(fstatinfo
, "st_rdev")):
2426 frdev
= fstatinfo
.st_rdev
;
2428 frdev
= fstatinfo
.st_dev
;
2429 getfrdev
= GetDevMajorMinor(frdev
);
2430 frdev_minor
= getfrdev
[0];
2431 frdev_major
= getfrdev
[1];
2432 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
2433 fsize
= format(int("0"), 'x').lower();
2434 elif(ftype
==0 or ftype
==7):
2435 fsize
= format(int(fstatinfo
.st_size
), 'x').lower();
2437 fsize
= format(int(fstatinfo
.st_size
)).lower();
2438 fatime
= format(int(fstatinfo
.st_atime
), 'x').lower();
2439 fmtime
= format(int(fstatinfo
.st_mtime
), 'x').lower();
2440 fctime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
2441 if(hasattr(fstatinfo
, "st_birthtime")):
2442 fbtime
= format(int(fstatinfo
.st_birthtime
), 'x').lower();
2444 fbtime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
2445 fmode
= format(int(fstatinfo
.st_mode
), 'x').lower();
2446 fchmode
= format(int(stat
.S_IMODE(fstatinfo
.st_mode
)), 'x').lower();
2447 ftypemod
= format(int(stat
.S_IFMT(fstatinfo
.st_mode
)), 'x').lower();
2448 fuid
= format(int(fstatinfo
.st_uid
), 'x').lower();
2449 fgid
= format(int(fstatinfo
.st_gid
), 'x').lower();
2454 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
2455 funame
= userinfo
.pw_name
;
2464 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
2465 fgname
= groupinfo
.gr_name
;
2470 fdev_minor
= format(int(fdev_minor
), 'x').lower();
2471 fdev_major
= format(int(fdev_major
), 'x').lower();
2472 frdev_minor
= format(int(frdev_minor
), 'x').lower();
2473 frdev_major
= format(int(frdev_major
), 'x').lower();
2474 finode
= format(int(finode
), 'x').lower();
2475 flinkcount
= format(int(flinkcount
), 'x').lower();
2476 if(hasattr(fstatinfo
, "st_file_attributes")):
2477 fwinattributes
= format(int(fstatinfo
.st_file_attributes
), 'x').lower();
2479 fwinattributes
= format(int(0), 'x').lower();
2480 fcontents
= "".encode('UTF-8');
2482 if(ftype
==0 or ftype
==7):
2483 with
open(fname
, "rb") as fpc
:
2485 chunk
= fpc
.read(chunk_size
);
2489 if(followlink
and (ftype
==1 or ftype
==2)):
2490 flstatinfo
= os
.stat(flinkname
);
2491 with
open(flinkname
, "rb") as fpc
:
2493 chunk
= fpc
.read(chunk_size
);
2497 ftypehex
= format(ftype
, 'x').lower();
2498 extrafields
= format(len(extradata
), 'x').lower();
2499 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
2500 if(len(extradata
)>0):
2501 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2502 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2503 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
];
2504 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
2505 catoutlenhex
= format(catoutlen
, 'x').lower();
2506 catoutlist
.insert(0, catoutlenhex
);
2507 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
2508 if(len(extradata
)>0):
2509 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2510 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
2511 if(checksumtype
=="none" or checksumtype
==""):
2512 catfileheadercshex
= format(0, 'x').lower();
2513 catfilecontentcshex
= format(0, 'x').lower();
2514 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2515 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2516 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
2517 elif(checksumtype
=="crc16_ccitt"):
2518 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2519 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
2520 elif(checksumtype
=="adler32"):
2521 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2522 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
2523 elif(checksumtype
=="crc32"):
2524 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2525 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
2526 elif(checksumtype
=="crc64_ecma"):
2527 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2528 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
2529 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2530 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2531 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
2532 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2533 checksumoutstr
= hashlib
.new(checksumtype
);
2534 checksumoutstr
.update("".encode('UTF-8'));
2535 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2536 checksumoutstr
= hashlib
.new(checksumtype
);
2537 checksumoutstr
.update(fcontents
);
2538 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
2540 catfileheadercshex
= format(0, 'x').lower();
2541 catfilecontentcshex
= format(0, 'x').lower();
2542 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2543 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2544 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
2545 if(checksumtype
=="none" or checksumtype
==""):
2546 catfileheadercshex
= format(0, 'x').lower();
2547 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2548 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2549 elif(checksumtype
=="crc16_ccitt"):
2550 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2551 elif(checksumtype
=="adler32"):
2552 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2553 elif(checksumtype
=="crc32"):
2554 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2555 elif(checksumtype
=="crc64_ecma"):
2556 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2557 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2558 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2559 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2560 checksumoutstr
= hashlib
.new(checksumtype
);
2561 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
2562 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2564 catfileheadercshex
= format(0, 'x').lower();
2565 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2566 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
2567 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
2568 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
2569 nullstrecd
= formatspecs
[5].encode('UTF-8');
2570 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
2571 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
2572 catfp
.write(catfileout
);
2575 os
.fsync(catfp
.fileno());
2576 except io
.UnsupportedOperation
:
2578 except AttributeError:
2581 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
2582 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2583 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2586 os
.fsync(catfp
.fileno());
2587 except io
.UnsupportedOperation
:
2589 except AttributeError:
2593 if(hasattr(sys
.stdout
, "buffer")):
2594 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2596 shutil
.copyfileobj(catfp
, sys
.stdout
);
2597 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2598 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2600 upload_file_to_internet_file(catfp
, outfile
);
2608 create_alias_function("Pack", __file_format_name__
, "", PackArchiveFile
);
2610 if(hasattr(shutil
, "register_archive_format")):
2611 def PackArchiveFileFunc(archive_name
, source_dir
, **kwargs
):
2612 return PackArchiveFile(source_dir
, archive_name
, False, "auto", None, False, "crc32", [], __file_format_delimiter__
, False, False);
2613 create_alias_function("Pack", __file_format_name__
, "Func", PackArchiveFileFunc
);
2615 def PackArchiveFileFromDirList(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2616 return PackArchiveFile(infiles
, outfile
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
2618 def PackArchiveFileFromTarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2619 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2620 outfile
= RemoveWindowsPath(outfile
);
2621 checksumtype
= checksumtype
.lower();
2622 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2623 checksumtype
="crc32";
2624 if(checksumtype
=="none"):
2626 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2628 if(compression
not in compressionlist
and compression
is None):
2629 compression
= "auto";
2631 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2632 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2633 if(os
.path
.exists(outfile
)):
2638 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2640 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2643 fbasename
= os
.path
.splitext(outfile
)[0];
2644 fextname
= os
.path
.splitext(outfile
)[1];
2645 catfp
= CompressOpenFile(outfile
, compressionlevel
);
2646 catver
= formatspecs
[6];
2647 fileheaderver
= str(int(catver
.replace(".", "")));
2648 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
2649 catfp
.write(fileheader
.encode('UTF-8'));
2655 inodetocatinode
= {};
2658 if(hasattr(sys
.stdin
, "buffer")):
2659 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
2661 shutil
.copyfileobj(sys
.stdin
, infile
);
2666 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
2667 infile
= download_file_from_internet_file(infile
);
2672 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2674 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
2676 if(not tarfile
.is_tarfile(infile
)):
2678 except AttributeError:
2679 if(not is_tarfile(infile
)):
2684 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2685 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
2687 tarfp
= tarfile
.open(infile
, "r");
2688 except FileNotFoundError
:
2690 numfiles
= int(len(tarfp
.getmembers()));
2691 fnumfiles
= format(int(len(tarfp
.getmembers())), 'x').lower();
2692 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2693 if(checksumtype
=="none" or checksumtype
==""):
2694 catfileheadercshex
= format(0, 'x').lower();
2695 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2696 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2697 elif(checksumtype
=="crc16_ccitt"):
2698 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2699 elif(checksumtype
=="adler32"):
2700 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2701 elif(checksumtype
=="crc32"):
2702 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2703 elif(checksumtype
=="crc64_ecma"):
2704 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2705 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2706 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2707 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2708 checksumoutstr
= hashlib
.new(checksumtype
);
2709 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
2710 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2712 catfileheadercshex
= format(0, 'x').lower();
2713 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2714 catfp
.write(fnumfilesa
.encode('UTF-8'));
2717 os
.fsync(catfp
.fileno());
2718 except io
.UnsupportedOperation
:
2720 except AttributeError:
2722 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
2723 catfhstart
= catfp
.tell();
2724 if(re
.findall("^[.|/]", member
.name
)):
2725 fname
= member
.name
;
2727 fname
= "./"+member
.name
;
2729 VerbosePrintOut(fname
);
2730 fpremode
= member
.mode
;
2731 ffullmode
= member
.mode
;
2735 ffullmode
= member
.mode
+ stat
.S_IFREG
;
2737 elif(member
.isdev()):
2738 ffullmode
= member
.mode
;
2740 elif(member
.islnk()):
2741 ffullmode
= member
.mode
+ stat
.S_IFREG
;
2743 elif(member
.issym()):
2744 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
2746 elif(member
.ischr()):
2747 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
2749 elif(member
.isblk()):
2750 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
2752 elif(member
.isdir()):
2753 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
2755 elif(member
.isfifo()):
2756 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
2758 elif(member
.issparse()):
2759 ffullmode
= member
.mode
;
2762 ffullmode
= member
.mode
;
2765 fcurfid
= format(int(curfid
), 'x').lower();
2766 fcurinode
= format(int(curfid
), 'x').lower();
2767 curfid
= curfid
+ 1;
2769 flinkname
= member
.linkname
;
2770 fdev_minor
= format(int(member
.devminor
), 'x').lower();
2771 fdev_major
= format(int(member
.devmajor
), 'x').lower();
2772 frdev_minor
= format(int(member
.devminor
), 'x').lower();
2773 frdev_major
= format(int(member
.devmajor
), 'x').lower();
2774 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
2775 fsize
= format(int("0"), 'x').lower();
2776 elif(ftype
==0 or ftype
==7):
2777 fsize
= format(int(member
.size
), 'x').lower();
2779 fsize
= format(int(member
.size
), 'x').lower();
2780 fatime
= format(int(member
.mtime
), 'x').lower();
2781 fmtime
= format(int(member
.mtime
), 'x').lower();
2782 fctime
= format(int(member
.mtime
), 'x').lower();
2783 fbtime
= format(int(member
.mtime
), 'x').lower();
2784 fmode
= format(int(ffullmode
), 'x').lower();
2785 fchmode
= format(int(stat
.S_IMODE(ffullmode
)), 'x').lower();
2786 ftypemod
= format(int(stat
.S_IFMT(ffullmode
)), 'x').lower();
2787 fuid
= format(int(member
.uid
), 'x').lower();
2788 fgid
= format(int(member
.gid
), 'x').lower();
2789 funame
= member
.uname
;
2790 fgname
= member
.gname
;
2791 flinkcount
= format(int(flinkcount
), 'x').lower();
2792 fwinattributes
= format(int(0), 'x').lower();
2793 fcontents
= "".encode('UTF-8');
2795 if(ftype
==0 or ftype
==7):
2796 with tarfp
.extractfile(member
) as fpc
:
2798 chunk
= fpc
.read(chunk_size
);
2802 ftypehex
= format(ftype
, 'x').lower();
2803 extrafields
= format(len(extradata
), 'x').lower();
2804 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
2805 if(len(extradata
)>0):
2806 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2807 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2808 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
];
2809 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
2810 catoutlenhex
= format(catoutlen
, 'x').lower();
2811 catoutlist
.insert(0, catoutlenhex
);
2812 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
2813 if(len(extradata
)>0):
2814 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2815 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
2816 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
2817 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
2818 if(checksumtype
=="none" or checksumtype
==""):
2819 catfileheadercshex
= format(0, 'x').lower();
2820 catfilecontentcshex
= format(0, 'x').lower();
2821 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2822 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2823 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
2824 elif(checksumtype
=="crc16_ccitt"):
2825 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2826 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
2827 elif(checksumtype
=="adler32"):
2828 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2829 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
2830 elif(checksumtype
=="crc32"):
2831 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2832 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
2833 elif(checksumtype
=="crc64_ecma"):
2834 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2835 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
2836 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2837 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2838 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
2839 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2840 checksumoutstr
= hashlib
.new(checksumtype
);
2841 checksumoutstr
.update("".encode('UTF-8'));
2842 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2843 checksumoutstr
= hashlib
.new(checksumtype
);
2844 checksumoutstr
.update(fcontents
);
2845 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
2847 catfileheadercshex
= format(0, 'x').lower();
2848 catfilecontentcshex
= format(0, 'x').lower();
2849 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2850 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2851 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
2852 if(checksumtype
=="none" or checksumtype
==""):
2853 catfileheadercshex
= format(0, 'x').lower();
2854 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2855 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2856 elif(checksumtype
=="crc16_ccitt"):
2857 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2858 elif(checksumtype
=="adler32"):
2859 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2860 elif(checksumtype
=="crc32"):
2861 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2862 elif(checksumtype
=="crc64_ecma"):
2863 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2864 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2865 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2866 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2867 checksumoutstr
= hashlib
.new(checksumtype
);
2868 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
2869 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2871 catfileheadercshex
= format(0, 'x').lower();
2872 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2873 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
2874 nullstrecd
= formatspecs
[5].encode('UTF-8');
2875 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
2876 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
2877 catfp
.write(catfileout
);
2880 os
.fsync(catfp
.fileno());
2881 except io
.UnsupportedOperation
:
2883 except AttributeError:
2886 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
2887 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2888 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2891 os
.fsync(catfp
.fileno());
2892 except io
.UnsupportedOperation
:
2894 except AttributeError:
2898 if(hasattr(sys
.stdout
, "buffer")):
2899 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2901 shutil
.copyfileobj(catfp
, sys
.stdout
);
2902 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2903 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2905 upload_file_from_internet_file(catfp
, outfile
);
2913 create_alias_function("Pack", __file_format_name__
, "FromTarFile", PackArchiveFileFromTarFile
);
2915 def PackArchiveFileFromZipFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2916 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2917 outfile
= RemoveWindowsPath(outfile
);
2918 checksumtype
= checksumtype
.lower();
2919 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2920 checksumtype
="crc32";
2921 if(checksumtype
=="none"):
2923 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2925 if(compression
not in compressionlist
and compression
is None):
2926 compression
= "auto";
2928 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2929 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2930 if(os
.path
.exists(outfile
)):
2935 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2937 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2940 fbasename
= os
.path
.splitext(outfile
)[0];
2941 fextname
= os
.path
.splitext(outfile
)[1];
2942 catfp
= CompressOpenFile(outfile
, compressionlevel
);
2943 catver
= formatspecs
[6];
2944 fileheaderver
= str(int(catver
.replace(".", "")));
2945 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
2946 catfp
.write(fileheader
.encode('UTF-8'));
2952 inodetocatinode
= {};
2955 if(hasattr(sys
.stdin
, "buffer")):
2956 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
2958 shutil
.copyfileobj(sys
.stdin
, infile
);
2963 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
2964 infile
= download_file_from_internet_file(infile
);
2969 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2973 if(not zipfile
.is_zipfile(infile
)):
2976 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
2977 except FileNotFoundError
:
2979 ziptest
= zipfp
.testzip();
2981 VerbosePrintOut("Bad file found!");
2982 numfiles
= int(len(zipfp
.infolist()));
2983 fnumfiles
= format(int(len(zipfp
.infolist())), 'x').lower();
2984 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2985 if(checksumtype
=="none" or checksumtype
==""):
2986 catfileheadercshex
= format(0, 'x').lower();
2987 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2988 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2989 elif(checksumtype
=="crc16_ccitt"):
2990 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2991 elif(checksumtype
=="adler32"):
2992 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2993 elif(checksumtype
=="crc32"):
2994 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2995 elif(checksumtype
=="crc64_ecma"):
2996 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2997 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2998 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2999 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3000 checksumoutstr
= hashlib
.new(checksumtype
);
3001 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
3002 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3004 catfileheadercshex
= format(0, 'x').lower();
3005 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3006 catfp
.write(fnumfilesa
.encode('UTF-8'));
3009 os
.fsync(catfp
.fileno());
3010 except io
.UnsupportedOperation
:
3012 except AttributeError:
3014 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
3015 catfhstart
= catfp
.tell();
3016 if(re
.findall("^[.|/]", member
.filename
)):
3017 fname
= member
.filename
;
3019 fname
= "./"+member
.filename
;
3020 zipinfo
= zipfp
.getinfo(member
.filename
);
3022 VerbosePrintOut(fname
);
3023 if(not member
.is_dir()):
3024 fpremode
= int(stat
.S_IFREG
+ 438);
3025 elif(member
.is_dir()):
3026 fpremode
= int(stat
.S_IFDIR
+ 511);
3029 if(not member
.is_dir()):
3031 elif(member
.is_dir()):
3034 fcurfid
= format(int(curfid
), 'x').lower();
3035 fcurinode
= format(int(curfid
), 'x').lower();
3036 curfid
= curfid
+ 1;
3037 fdev_minor
= format(int(0), 'x').lower();
3038 fdev_major
= format(int(0), 'x').lower();
3039 frdev_minor
= format(int(0), 'x').lower();
3040 frdev_major
= format(int(0), 'x').lower();
3042 fsize
= format(int("0"), 'x').lower();
3044 fsize
= format(int(member
.file_size
), 'x').lower();
3046 fsize
= format(int(member
.file_size
), 'x').lower();
3047 fatime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
3048 fmtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
3049 fctime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
3050 fbtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
3051 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
3052 fwinattributes
= format(int(zipinfo
.external_attr
), 'x').lower();
3053 if(not member
.is_dir()):
3054 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
3055 fchmode
= stat
.S_IMODE(fmode
);
3056 ftypemod
= stat
.S_IFMT(fmode
);
3057 elif(member
.is_dir()):
3058 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
3059 fchmode
= stat
.S_IMODE(fmode
);
3060 ftypemod
= stat
.S_IFMT(fmode
);
3061 elif(zipinfo
.create_system
==3):
3062 fwinattributes
= format(int(0), 'x').lower();
3063 fmode
= format(int(zipinfo
.external_attr
), 'x').lower();
3064 fchmode
= stat
.S_IMODE(fmode
);
3065 ftypemod
= stat
.S_IFMT(fmode
);
3067 fwinattributes
= format(int(0), 'x').lower();
3068 if(not member
.is_dir()):
3069 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
3070 fchmode
= stat
.S_IMODE(fmode
);
3071 ftypemod
= stat
.S_IFMT(fmode
);
3072 elif(member
.is_dir()):
3073 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
3074 fchmode
= stat
.S_IMODE(fmode
);
3075 ftypemod
= stat
.S_IFMT(fmode
);
3077 fuid
= format(int(os
.getuid()), 'x').lower();
3078 except AttributeError:
3079 fuid
= format(int(0), 'x').lower();
3081 fuid
= format(int(0), 'x').lower();
3083 fgid
= format(int(os
.getgid()), 'x').lower();
3084 except AttributeError:
3085 fgid
= format(int(0), 'x').lower();
3087 fgid
= format(int(0), 'x').lower();
3091 userinfo
= pwd
.getpwuid(os
.getuid());
3092 funame
= userinfo
.pw_name
;
3095 except AttributeError:
3103 groupinfo
= grp
.getgrgid(os
.getgid());
3104 fgname
= groupinfo
.gr_name
;
3107 except AttributeError:
3111 fcontents
= "".encode('UTF-8');
3113 fcontents
= zipfp
.read(member
.filename
);
3114 ftypehex
= format(ftype
, 'x').lower();
3115 extrafields
= format(len(extradata
), 'x').lower();
3116 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
3117 if(len(extradata
)>0):
3118 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3119 extrasizelen
= format(len(extrasizestr
), 'x').lower();
3120 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
];
3121 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
3122 catoutlenhex
= format(catoutlen
, 'x').lower();
3123 catoutlist
.insert(0, catoutlenhex
);
3124 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
3125 if(len(extradata
)>0):
3126 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3127 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
3128 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
3129 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
3130 if(checksumtype
=="none" or checksumtype
==""):
3131 catfileheadercshex
= format(0, 'x').lower();
3132 catfilecontentcshex
= format(0, 'x').lower();
3133 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3134 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3135 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
3136 elif(checksumtype
=="crc16_ccitt"):
3137 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3138 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
3139 elif(checksumtype
=="adler32"):
3140 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3141 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
3142 elif(checksumtype
=="crc32"):
3143 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3144 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
3145 elif(checksumtype
=="crc64_ecma"):
3146 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3147 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
3148 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3149 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3150 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
3151 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3152 checksumoutstr
= hashlib
.new(checksumtype
);
3153 checksumoutstr
.update("".encode('UTF-8'));
3154 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3155 checksumoutstr
= hashlib
.new(checksumtype
);
3156 checksumoutstr
.update(fcontents
);
3157 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
3159 catfileheadercshex
= format(0, 'x').lower();
3160 catfilecontentcshex
= format(0, 'x').lower();
3161 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
3162 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
3163 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
3164 if(checksumtype
=="none" or checksumtype
==""):
3165 catfileheadercshex
= format(0, 'x').lower();
3166 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3167 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3168 elif(checksumtype
=="crc16_ccitt"):
3169 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3170 elif(checksumtype
=="adler32"):
3171 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3172 elif(checksumtype
=="crc32"):
3173 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3174 elif(checksumtype
=="crc64_ecma"):
3175 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3176 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3177 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3178 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3179 checksumoutstr
= hashlib
.new(checksumtype
);
3180 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
3181 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3183 catfileheadercshex
= format(0, 'x').lower();
3184 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
3185 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
3186 nullstrecd
= formatspecs
[5].encode('UTF-8');
3187 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
3188 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
3189 catfp
.write(catfileout
);
3192 os
.fsync(catfp
.fileno());
3193 except io
.UnsupportedOperation
:
3195 except AttributeError:
3198 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
3199 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3200 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
3203 os
.fsync(catfp
.fileno());
3204 except io
.UnsupportedOperation
:
3206 except AttributeError:
3210 if(hasattr(sys
.stdout
, "buffer")):
3211 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
3213 shutil
.copyfileobj(catfp
, sys
.stdout
);
3214 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3215 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
3217 upload_file_to_internet_file(catfp
, outfile
);
3225 create_alias_function("Pack", __file_format_name__
, "FromZipFile", PackArchiveFileFromZipFile
);
3227 if(not rarfile_support
):
3228 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
3231 if(rarfile_support
):
3232 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
3233 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3234 outfile
= RemoveWindowsPath(outfile
);
3235 checksumtype
= checksumtype
.lower();
3236 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
3237 checksumtype
="crc32";
3238 if(checksumtype
=="none"):
3240 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
3242 if(compression
not in compressionlist
and compression
is None):
3243 compression
= "auto";
3245 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
3246 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3247 if(os
.path
.exists(outfile
)):
3252 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3254 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3257 fbasename
= os
.path
.splitext(outfile
)[0];
3258 fextname
= os
.path
.splitext(outfile
)[1];
3259 catfp
= CompressOpenFile(outfile
, compressionlevel
);
3260 catver
= formatspecs
[6];
3261 fileheaderver
= str(int(catver
.replace(".", "")));
3262 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
3263 catfp
.write(fileheader
.encode('UTF-8'));
3269 inodetocatinode
= {};
3270 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
3272 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
3274 rarfp
= rarfile
.RarFile(infile
, "r");
3275 rartest
= rarfp
.testrar();
3277 VerbosePrintOut("Bad file found!");
3278 numfiles
= int(len(rarfp
.infolist()));
3279 fnumfiles
= format(int(len(rarfp
.infolist())), 'x').lower();
3280 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
3281 if(checksumtype
=="none" or checksumtype
==""):
3282 catfileheadercshex
= format(0, 'x').lower();
3283 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3284 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
3285 elif(checksumtype
=="crc16_ccitt"):
3286 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
3287 elif(checksumtype
=="adler32"):
3288 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
3289 elif(checksumtype
=="crc32"):
3290 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
3291 elif(checksumtype
=="crc64_ecma"):
3292 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3293 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3294 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3295 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3296 checksumoutstr
= hashlib
.new(checksumtype
);
3297 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
3298 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3300 catfileheadercshex
= format(0, 'x').lower();
3301 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3302 catfp
.write(fnumfilesa
.encode('UTF-8'));
3305 os
.fsync(catfp
.fileno());
3306 except io
.UnsupportedOperation
:
3308 except AttributeError:
3310 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
3313 if(member
.host_os
==rarfile
.RAR_OS_UNIX
):
3316 member
.external_attr
3318 except AttributeError:
3320 elif(member
.host_os
==rarfile
.RAR_OS_WIN32
):
3323 member
.external_attr
3325 except AttributeError:
3330 catfhstart
= catfp
.tell();
3331 if(re
.findall("^[.|/]", member
.filename
)):
3332 fname
= member
.filename
;
3334 fname
= "./"+member
.filename
;
3335 rarinfo
= rarfp
.getinfo(member
.filename
);
3337 VerbosePrintOut(fname
);
3338 if(is_unix
and member
.external_attr
!=0):
3339 fpremode
= int(member
.external_attr
);
3340 elif(member
.is_file()):
3341 fpremode
= int(stat
.S_IFREG
+ 438);
3342 elif(member
.is_symlink()):
3343 fpremode
= int(stat
.S_IFLNK
+ 438);
3344 elif(member
.is_dir()):
3345 fpremode
= int(stat
.S_IFDIR
+ 511);
3346 if(is_windows
and member
.external_attr
!=0):
3347 fwinattributes
= format(int(member
.external_attr
), 'x').lower();
3349 fwinattributes
= format(int(0), 'x').lower();
3352 if(member
.is_file()):
3354 elif(member
.is_symlink()):
3356 elif(member
.is_dir()):
3360 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
3361 fcurfid
= format(int(curfid
), 'x').lower();
3362 fcurinode
= format(int(curfid
), 'x').lower();
3363 curfid
= curfid
+ 1;
3364 fdev_minor
= format(int(0), 'x').lower();
3365 fdev_major
= format(int(0), 'x').lower();
3366 frdev_minor
= format(int(0), 'x').lower();
3367 frdev_major
= format(int(0), 'x').lower();
3369 fsize
= format(int("0"), 'x').lower();
3371 fsize
= format(int(member
.file_size
), 'x').lower();
3373 fsize
= format(int(member
.file_size
), 'x').lower();
3376 fatime
= format(int(member
.atime
.timestamp()), 'x').lower();
3378 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3379 except AttributeError:
3380 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3381 fmtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3384 fctime
= format(int(member
.ctime
.timestamp()), 'x').lower();
3386 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3387 except AttributeError:
3388 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3389 fbtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3390 if(is_unix
and member
.external_attr
!=0):
3391 fmode
= format(int(member
.external_attr
), 'x').lower();
3392 fchmode
= format(int(stat
.S_IMODE(member
.external_attr
)), 'x').lower();
3393 ftypemod
= format(int(stat
.S_IFMT(member
.external_attr
)), 'x').lower();
3394 elif(member
.is_file()):
3395 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
3396 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
3397 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
3398 elif(member
.is_symlink()):
3399 fmode
= format(int(stat
.S_IFLNK
+ 438), 'x').lower();
3400 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
3401 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
3402 elif(member
.is_dir()):
3403 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
3404 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3405 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3407 fuid
= format(int(os
.getuid()), 'x').lower();
3408 except AttributeError:
3409 fuid
= format(int(0), 'x').lower();
3411 fuid
= format(int(0), 'x').lower();
3413 fgid
= format(int(os
.getgid()), 'x').lower();
3414 except AttributeError:
3415 fgid
= format(int(0), 'x').lower();
3417 fgid
= format(int(0), 'x').lower();
3421 userinfo
= pwd
.getpwuid(os
.getuid());
3422 funame
= userinfo
.pw_name
;
3425 except AttributeError:
3433 groupinfo
= grp
.getgrgid(os
.getgid());
3434 fgname
= groupinfo
.gr_name
;
3437 except AttributeError:
3441 fcontents
= "".encode('UTF-8');
3443 fcontents
= rarfp
.read(member
.filename
);
3444 ftypehex
= format(ftype
, 'x').lower();
3445 extrafields
= format(len(extradata
), 'x').lower();
3446 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
3447 if(len(extradata
)>0):
3448 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3449 extrasizelen
= format(len(extrasizestr
), 'x').lower();
3450 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
];
3451 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
3452 catoutlenhex
= format(catoutlen
, 'x').lower();
3453 catoutlist
.insert(0, catoutlenhex
);
3454 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
3455 if(len(extradata
)>0):
3456 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3457 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
3458 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
3459 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
3460 if(checksumtype
=="none" or checksumtype
==""):
3461 catfileheadercshex
= format(0, 'x').lower();
3462 catfilecontentcshex
= format(0, 'x').lower();
3463 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3464 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3465 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
3466 elif(checksumtype
=="crc16_ccitt"):
3467 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3468 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
3469 elif(checksumtype
=="adler32"):
3470 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3471 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
3472 elif(checksumtype
=="crc32"):
3473 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3474 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
3475 elif(checksumtype
=="crc64_ecma"):
3476 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3477 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
3478 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3479 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3480 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
3481 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3482 checksumoutstr
= hashlib
.new(checksumtype
);
3483 checksumoutstr
.update("".encode('UTF-8'));
3484 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3485 checksumoutstr
= hashlib
.new(checksumtype
);
3486 checksumoutstr
.update(fcontents
);
3487 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
3489 catfileheadercshex
= format(0, 'x').lower();
3490 catfilecontentcshex
= format(0, 'x').lower();
3491 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
3492 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
3493 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
3494 if(checksumtype
=="none" or checksumtype
==""):
3495 catfileheadercshex
= format(0, 'x').lower()
3496 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3497 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower()
3498 elif(checksumtype
=="crc16_ccitt"):
3499 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower()
3500 elif(checksumtype
=="adler32"):
3501 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower()
3502 elif(checksumtype
=="crc32"):
3503 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower()
3504 elif(checksumtype
=="crc64_ecma"):
3505 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
3506 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3507 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
3508 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3509 checksumoutstr
= hashlib
.new(checksumtype
)
3510 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'))
3511 catfileheadercshex
= checksumoutstr
.hexdigest().lower()
3513 catfileheadercshex
= format(0, 'x').lower()
3514 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5])
3515 catfileoutstrecd
= catfileoutstr
.encode('UTF-8')
3516 nullstrecd
= formatspecs
[5].encode('UTF-8')
3517 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
3518 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
)
3519 catfp
.write(catfileout
)
3522 os
.fsync(catfp
.fileno())
3523 except io
.UnsupportedOperation
:
3525 except AttributeError:
3528 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
3529 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3530 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
)
3533 os
.fsync(catfp
.fileno())
3534 except io
.UnsupportedOperation
:
3536 except AttributeError:
3540 if(hasattr(sys
.stdout
, "buffer")):
3541 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
3543 shutil
.copyfileobj(catfp
, sys
.stdout
);
3544 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3545 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
3547 upload_file_to_internet_file(catfp
, outfile
);
3555 create_alias_function("Pack", __file_format_name__
, "FromRarFile", PackArchiveFileFromRarFile
);
3557 if(not py7zr_support
):
3558 def PackArchiveFileFromSevenZipFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
3562 def PackArchiveFileFromSevenZipFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
3563 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3564 outfile
= RemoveWindowsPath(outfile
);
3565 checksumtype
= checksumtype
.lower();
3566 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
3567 checksumtype
="crc32";
3568 if(checksumtype
=="none"):
3570 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
3572 if(compression
not in compressionlist
and compression
is None):
3573 compression
= "auto";
3575 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
3576 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3577 if(os
.path
.exists(outfile
)):
3582 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3584 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3587 fbasename
= os
.path
.splitext(outfile
)[0];
3588 fextname
= os
.path
.splitext(outfile
)[1];
3589 catfp
= CompressOpenFile(outfile
, compressionlevel
);
3590 catver
= formatspecs
[6];
3591 fileheaderver
= str(int(catver
.replace(".", "")));
3592 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
3593 catfp
.write(fileheader
.encode('UTF-8'));
3599 inodetocatinode
= {};
3600 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
3602 szpfp
= py7zr
.SevenZipFile(infile
, mode
="r");
3603 file_content
= szpfp
.readall();
3604 #sztest = szpfp.testzip();
3605 sztestalt
= szpfp
.test();
3607 VerbosePrintOut("Bad file found!");
3608 numfiles
= int(len(szpfp
.list()));
3609 fnumfiles
= format(int(len(szpfp
.list())), 'x').lower();
3610 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
3611 if(checksumtype
=="none" or checksumtype
==""):
3612 catfileheadercshex
= format(0, 'x').lower();
3613 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3614 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
3615 elif(checksumtype
=="crc16_ccitt"):
3616 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
3617 elif(checksumtype
=="adler32"):
3618 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
3619 elif(checksumtype
=="crc32"):
3620 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
3621 elif(checksumtype
=="crc64_ecma"):
3622 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3623 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3624 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3625 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3626 checksumoutstr
= hashlib
.new(checksumtype
);
3627 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
3628 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3630 catfileheadercshex
= format(0, 'x').lower();
3631 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3632 catfp
.write(fnumfilesa
.encode('UTF-8'));
3635 os
.fsync(catfp
.fileno());
3636 except io
.UnsupportedOperation
:
3638 except AttributeError:
3640 for member
in sorted(szpfp
.list(), key
=lambda x
: x
.filename
):
3641 catfhstart
= catfp
.tell();
3642 if(re
.findall("^[.|/]", member
.filename
)):
3643 fname
= member
.filename
;
3645 fname
= "./"+member
.filename
;
3647 VerbosePrintOut(fname
);
3648 if(not member
.is_directory
):
3649 fpremode
= int(stat
.S_IFREG
+ 438);
3650 elif(member
.is_directory
):
3651 fpremode
= int(stat
.S_IFDIR
+ 511);
3652 fwinattributes
= format(int(0), 'x').lower();
3655 if(member
.is_directory
):
3660 fcurfid
= format(int(curfid
), 'x').lower();
3661 fcurinode
= format(int(curfid
), 'x').lower();
3662 curfid
= curfid
+ 1;
3663 fdev_minor
= format(int(0), 'x').lower();
3664 fdev_major
= format(int(0), 'x').lower();
3665 frdev_minor
= format(int(0), 'x').lower();
3666 frdev_major
= format(int(0), 'x').lower();
3668 fsize
= format(int("0"), 'x').lower();
3669 fatime
= format(int(member
.creationtime
.timestamp()), 'x').lower();
3670 fmtime
= format(int(member
.creationtime
.timestamp()), 'x').lower();
3671 fctime
= format(int(member
.creationtime
.timestamp()), 'x').lower();
3672 fbtime
= format(int(member
.creationtime
.timestamp()), 'x').lower();
3673 if(member
.is_directory
):
3674 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
3675 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3676 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3678 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
3679 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
3680 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
3682 fuid
= format(int(os
.getuid()), 'x').lower();
3683 except AttributeError:
3684 fuid
= format(int(0), 'x').lower();
3686 fuid
= format(int(0), 'x').lower();
3688 fgid
= format(int(os
.getgid()), 'x').lower();
3689 except AttributeError:
3690 fgid
= format(int(0), 'x').lower();
3692 fgid
= format(int(0), 'x').lower();
3696 userinfo
= pwd
.getpwuid(os
.getuid());
3697 funame
= userinfo
.pw_name
;
3700 except AttributeError:
3708 groupinfo
= grp
.getgrgid(os
.getgid());
3709 fgname
= groupinfo
.gr_name
;
3712 except AttributeError:
3716 fcontents
= "".encode('UTF-8');
3718 fcontents
= file_content
[member
.filename
].read();
3719 fsize
= format(len(fcontents
), 'x').lower();
3720 file_content
[member
.filename
].close();
3721 ftypehex
= format(ftype
, 'x').lower();
3722 extrafields
= format(len(extradata
), 'x').lower();
3723 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
3724 if(len(extradata
)>0):
3725 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3726 extrasizelen
= format(len(extrasizestr
), 'x').lower();
3727 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
];
3728 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
3729 catoutlenhex
= format(catoutlen
, 'x').lower();
3730 catoutlist
.insert(0, catoutlenhex
);
3731 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
3732 if(len(extradata
)>0):
3733 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3734 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
3735 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
3736 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
3737 if(checksumtype
=="none" or checksumtype
==""):
3738 catfileheadercshex
= format(0, 'x').lower();
3739 catfilecontentcshex
= format(0, 'x').lower();
3740 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3741 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3742 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
3743 elif(checksumtype
=="crc16_ccitt"):
3744 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3745 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
3746 elif(checksumtype
=="adler32"):
3747 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3748 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
3749 elif(checksumtype
=="crc32"):
3750 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3751 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
3752 elif(checksumtype
=="crc64_ecma"):
3753 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3754 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
3755 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3756 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3757 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
3758 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3759 checksumoutstr
= hashlib
.new(checksumtype
);
3760 checksumoutstr
.update("".encode('UTF-8'));
3761 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3762 checksumoutstr
= hashlib
.new(checksumtype
);
3763 checksumoutstr
.update(fcontents
);
3764 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
3766 catfileheadercshex
= format(0, 'x').lower();
3767 catfilecontentcshex
= format(0, 'x').lower();
3768 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
3769 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
3770 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
3771 if(checksumtype
=="none" or checksumtype
==""):
3772 catfileheadercshex
= format(0, 'x').lower()
3773 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3774 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower()
3775 elif(checksumtype
=="crc16_ccitt"):
3776 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower()
3777 elif(checksumtype
=="adler32"):
3778 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower()
3779 elif(checksumtype
=="crc32"):
3780 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower()
3781 elif(checksumtype
=="crc64_ecma"):
3782 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
3783 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3784 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
3785 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3786 checksumoutstr
= hashlib
.new(checksumtype
)
3787 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'))
3788 catfileheadercshex
= checksumoutstr
.hexdigest().lower()
3790 catfileheadercshex
= format(0, 'x').lower()
3791 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5])
3792 catfileoutstrecd
= catfileoutstr
.encode('UTF-8')
3793 nullstrecd
= formatspecs
[5].encode('UTF-8')
3794 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
3795 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
)
3796 catfp
.write(catfileout
)
3799 os
.fsync(catfp
.fileno())
3800 except io
.UnsupportedOperation
:
3802 except AttributeError:
3805 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
3806 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3807 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
)
3810 os
.fsync(catfp
.fileno())
3811 except io
.UnsupportedOperation
:
3813 except AttributeError:
3817 if(hasattr(sys
.stdout
, "buffer")):
3818 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
3820 shutil
.copyfileobj(catfp
, sys
.stdout
);
3821 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3822 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
3824 upload_file_to_internet_file(catfp
, outfile
);
3832 create_alias_function("Pack", __file_format_name__
, "FromSevenZipFile", PackArchiveFileFromSevenZipFile
);
3834 def ArchiveFileSeekToFileNum(infile
, seekto
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3835 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3838 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3839 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
3840 if(checkcompressfile
=="tarfile"):
3841 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3842 if(checkcompressfile
=="zipfile"):
3843 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3844 if(rarfile_support
and checkcompressfile
=="rarfile"):
3845 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3846 if(py7zr_support
and checkcompressfile
=="7zipfile"):
3847 return SevenZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3848 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3855 if(hasattr(sys
.stdin
, "buffer")):
3856 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
3858 shutil
.copyfileobj(sys
.stdin
, catfp
);
3860 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3864 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
3865 catfp
= download_file_from_internet_file(infile
);
3867 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3872 infile
= RemoveWindowsPath(infile
);
3873 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
3874 if(checkcompressfile
=="tarfile"):
3875 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3876 if(checkcompressfile
=="zipfile"):
3877 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3878 if(rarfile_support
and checkcompressfile
=="rarfile"):
3879 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3880 if(py7zr_support
and checkcompressfile
=="7zipfile"):
3881 return SevenZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3882 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3884 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
3885 if(not compresscheck
):
3886 fextname
= os
.path
.splitext(infile
)[1];
3887 if(fextname
==".gz"):
3888 compresscheck
= "gzip";
3889 elif(fextname
==".bz2"):
3890 compresscheck
= "bzip2";
3891 elif(fextname
==".zst"):
3892 compresscheck
= "zstd";
3893 elif(fextname
==".lz4" or fextname
==".clz4"):
3894 compresscheck
= "lz4";
3895 elif(fextname
==".lzo" or fextname
==".lzop"):
3896 compresscheck
= "lzo";
3897 elif(fextname
==".lzma" or fextname
==".xz"):
3898 compresscheck
= "lzma";
3901 if(not compresscheck
):
3903 catfp
= UncompressFile(infile
, formatspecs
, "rb");
3908 SeekToEndOfFile(catfp);
3910 SeekToEndOfFile(catfp);
3911 CatSize = catfp.tell();
3912 CatSizeEnd = CatSize;
3920 curloc
= catfp
.tell();
3923 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
3925 catfp
.seek(curloc
, 0);
3926 catstring
= catheader
[0];
3927 catversion
= re
.findall(r
"([\d]+)$", catstring
);
3928 fprenumfiles
= catheader
[1];
3929 fnumfiles
= int(fprenumfiles
, 16);
3930 fprechecksumtype
= catheader
[2];
3931 fprechecksum
= catheader
[3];
3932 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
3933 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3934 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
3935 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
3936 catfileheadercshex
= format(0, 'x').lower();
3937 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
3938 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3939 elif(fprechecksumtype
=="crc16_ccitt"):
3940 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3941 elif(fprechecksumtype
=="adler32"):
3942 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3943 elif(fprechecksumtype
=="crc32"):
3944 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3945 elif(fprechecksumtype
=="crc64_ecma"):
3946 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3947 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
3948 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3949 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
3950 checksumoutstr
= hashlib
.new(fprechecksumtype
);
3951 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3952 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3954 catfileheadercshex
= format(0, 'x').lower();
3955 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3956 fheadtell
= len(fileheader
);
3957 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
3958 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
3960 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
3961 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
3962 if(seekto
>=fnumfiles
):
3963 seekto
= fnumfiles
- 1;
3969 prefhstart
= catfp
.tell();
3971 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3973 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
3974 if(len(preheaderdata
)==0):
3976 prefheadsize
= int(preheaderdata
[0], 16);
3977 prefnumfields
= int(preheaderdata
[1], 16);
3978 preftype
= int(preheaderdata
[2], 16);
3979 if(re
.findall("^[.|/]", preheaderdata
[3])):
3980 prefname
= preheaderdata
[3];
3982 prefname
= "./"+preheaderdata
[3];
3983 prefbasedir
= os
.path
.dirname(prefname
);
3984 preflinkname
= preheaderdata
[4];
3985 prefsize
= int(preheaderdata
[5], 16);
3986 prefatime
= int(preheaderdata
[6], 16);
3987 prefmtime
= int(preheaderdata
[7], 16);
3988 prefctime
= int(preheaderdata
[8], 16);
3989 prefbtime
= int(preheaderdata
[9], 16);
3990 prefmode
= int(preheaderdata
[10], 16);
3991 prefchmode
= stat
.S_IMODE(prefmode
);
3992 preftypemod
= stat
.S_IFMT(prefmode
);
3993 prefwinattributes
= int(preheaderdata
[11], 16);
3994 prefuid
= int(preheaderdata
[12], 16);
3995 prefuname
= preheaderdata
[13];
3996 prefgid
= int(preheaderdata
[14], 16);
3997 prefgname
= preheaderdata
[15];
3998 fid
= int(preheaderdata
[16], 16);
3999 finode
= int(preheaderdata
[17], 16);
4000 flinkcount
= int(preheaderdata
[18], 16);
4001 prefdev_minor
= int(preheaderdata
[19], 16);
4002 prefdev_major
= int(preheaderdata
[20], 16);
4003 prefrdev_minor
= int(preheaderdata
[22], 16);
4004 prefrdev_major
= int(preheaderdata
[23], 16);
4005 prefextrasize
= int(preheaderdata
[24], 16);
4006 prefextrafields
= int(preheaderdata
[25], 16);
4007 extrafieldslist
= [];
4009 extraend
= extrastart
+ prefextrafields
;
4010 extrafieldslist
= [];
4011 if(extrastart
<extraend
):
4012 extrafieldslist
.append(preheaderdata
[extrastart
]);
4013 extrastart
= extrastart
+ 1;
4014 prefchecksumtype
= preheaderdata
[extrastart
].lower();
4015 prefcs
= preheaderdata
[extrastart
+ 1].lower();
4016 prefccs
= preheaderdata
[extrastart
+ 2].lower();
4018 hcmax
= len(preheaderdata
) - 2;
4021 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
4023 prenewfcs
= GetFileChecksum(hout
, preheaderdata
[-3].lower(), True, formatspecs
);
4024 if(prefcs
!=prenewfcs
and not skipchecksum
):
4025 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
4027 valid_archive
= False;
4028 invalid_archive
= True;
4029 prefhend
= catfp
.tell() - 1;
4030 prefcontentstart
= catfp
.tell();
4032 pyhascontents
= False;
4034 prefcontents
= catfp
.read(prefsize
);
4035 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
4036 pyhascontents
= True;
4037 if(prefccs
!=prenewfccs
and not skipchecksum
):
4038 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
4042 catfp
.seek(seekstart
, 0);
4044 catfheadsize
= int(preheaderdata
[0], 16);
4045 catfnumfields
= int(preheaderdata
[1], 16);
4046 catftype
= int(preheaderdata
[2], 16);
4047 if(re
.findall("^[.|/]", preheaderdata
[3])):
4048 catfname
= preheaderdata
[3];
4050 catfname
= "./"+preheaderdata
[3];
4051 catflinkname
= preheaderdata
[4];
4052 catfsize
= int(preheaderdata
[5], 16);
4053 catfbasedir
= os
.path
.dirname(catfname
);
4054 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
4056 catlist
.update({'catfp': catfp
});
4061 create_alias_function("", __file_format_name__
, "SeekToFileNum", ArchiveFileSeekToFileNum
);
4063 def ArchiveFileSeekToFileName(infile
, seekfile
=None, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4064 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4067 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4068 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
4069 if(checkcompressfile
=="tarfile"):
4070 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4071 if(checkcompressfile
=="zipfile"):
4072 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4073 if(rarfile_support
and checkcompressfile
=="rarfile"):
4074 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4075 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4076 return SevenZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4077 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4084 if(hasattr(sys
.stdin
, "buffer")):
4085 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
4087 shutil
.copyfileobj(sys
.stdin
, catfp
);
4089 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4093 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4094 catfp
= download_file_from_internet_file(infile
);
4095 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4101 infile
= RemoveWindowsPath(infile
);
4102 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
4103 if(checkcompressfile
=="tarfile"):
4104 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4105 if(checkcompressfile
=="zipfile"):
4106 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4107 if(rarfile_support
and checkcompressfile
=="rarfile"):
4108 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4109 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4110 return SevenZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4111 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4113 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
4114 if(not compresscheck
):
4115 fextname
= os
.path
.splitext(infile
)[1];
4116 if(fextname
==".gz"):
4117 compresscheck
= "gzip";
4118 elif(fextname
==".bz2"):
4119 compresscheck
= "bzip2";
4120 elif(fextname
==".zst"):
4121 compresscheck
= "zstd";
4122 elif(fextname
==".lz4" or fextname
==".clz4"):
4123 compresscheck
= "lz4";
4124 elif(fextname
==".lzo" or fextname
==".lzop"):
4125 compresscheck
= "lzo";
4126 elif(fextname
==".lzma" or fextname
==".xz"):
4127 compresscheck
= "lzma";
4130 if(not compresscheck
):
4132 catfp
= UncompressFile(infile
, formatspecs
, "rb");
4137 SeekToEndOfFile(catfp);
4139 SeekToEndOfFile(catfp);
4140 CatSize = catfp.tell();
4141 CatSizeEnd = CatSize;
4149 curloc
= catfp
.tell();
4152 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
4154 catfp
.seek(curloc
, 0);
4155 catstring
= catheader
[0];
4156 catversion
= re
.findall(r
"([\d]+)$", catstring
);
4157 fprenumfiles
= catheader
[1];
4158 fnumfiles
= int(fprenumfiles
, 16);
4159 fprechecksumtype
= catheader
[2];
4160 fprechecksum
= catheader
[3];
4161 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
4162 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4163 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
4164 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
4165 catfileheadercshex
= format(0, 'x').lower();
4166 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
4167 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4168 elif(fprechecksumtype
=="crc16_ccitt"):
4169 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4170 elif(fprechecksumtype
=="adler32"):
4171 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4172 elif(fprechecksumtype
=="crc32"):
4173 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4174 elif(fprechecksumtype
=="crc64_ecma"):
4175 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4176 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
4177 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4178 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
4179 checksumoutstr
= hashlib
.new(fprechecksumtype
);
4180 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4181 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4183 catfileheadercshex
= format(0, 'x').lower();
4184 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4185 fheadtell
= len(fileheader
);
4186 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
4187 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
4189 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4190 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
4191 seekto
= fnumfiles
- 1
4196 prefhstart
= catfp
.tell();
4198 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
4200 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
4201 if(len(preheaderdata
)==0):
4203 prefheadsize
= int(preheaderdata
[0], 16);
4204 prefnumfields
= int(preheaderdata
[1], 16);
4205 preftype
= int(preheaderdata
[2], 16);
4206 if(re
.findall("^[.|/]", preheaderdata
[3])):
4207 prefname
= preheaderdata
[3];
4209 prefname
= "./"+preheaderdata
[3];
4210 prefbasedir
= os
.path
.dirname(prefname
);
4211 preflinkname
= preheaderdata
[4];
4212 prefsize
= int(preheaderdata
[5], 16);
4213 prefatime
= int(preheaderdata
[6], 16);
4214 prefmtime
= int(preheaderdata
[7], 16);
4215 prefctime
= int(preheaderdata
[8], 16);
4216 prefbtime
= int(preheaderdata
[9], 16);
4217 prefmode
= int(preheaderdata
[10], 16);
4218 prefchmode
= stat
.S_IMODE(prefmode
);
4219 preftypemod
= stat
.S_IFMT(prefmode
);
4220 prefwinattributes
= int(preheaderdata
[11], 16);
4221 prefuid
= int(preheaderdata
[12], 16);
4222 prefuname
= preheaderdata
[13];
4223 prefgid
= int(preheaderdata
[14], 16);
4224 prefgname
= preheaderdata
[15];
4225 fid
= int(preheaderdata
[16], 16);
4226 finode
= int(preheaderdata
[17], 16);
4227 flinkcount
= int(preheaderdata
[18], 16);
4228 prefdev_minor
= int(preheaderdata
[19], 16);
4229 prefdev_major
= int(preheaderdata
[20], 16);
4230 prefrdev_minor
= int(preheaderdata
[22], 16);
4231 prefrdev_major
= int(preheaderdata
[23], 16);
4232 prefextrasize
= int(preheaderdata
[24], 16);
4233 prefextrafields
= int(preheaderdata
[25], 16);
4234 extrafieldslist
= [];
4236 extraend
= extrastart
+ prefextrafields
;
4237 extrafieldslist
= [];
4238 if(extrastart
<extraend
):
4239 extrafieldslist
.append(preheaderdata
[extrastart
]);
4240 extrastart
= extrastart
+ 1;
4241 prefchecksumtype
= preheaderdata
[extrastart
].lower();
4242 prefcs
= preheaderdata
[extrastart
+ 1].lower();
4243 prefccs
= preheaderdata
[extrastart
+ 2].lower();
4245 hcmax
= len(preheaderdata
) - 2;
4248 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
4250 prenewfcs
= GetFileChecksum(hout
, preheaderdata
[-3].lower(), True, formatspecs
);
4251 if(prefcs
!=prenewfcs
and not skipchecksum
):
4252 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
4254 valid_archive
= False;
4255 invalid_archive
= True;
4256 prefhend
= catfp
.tell() - 1;
4257 prefcontentstart
= catfp
.tell();
4259 pyhascontents
= False;
4261 prefcontents
= catfp
.read(prefsize
);
4262 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
4263 pyhascontents
= True;
4264 if(prefccs
!=prenewfccs
and not skipchecksum
):
4265 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
4270 prefname
= preheaderdata
[2];
4271 if(re
.findall("^[.|/]", preheaderdata
[2])):
4272 prefname
= preheaderdata
[2];
4274 prefname
= "./"+preheaderdata
[2];
4275 if(prefname
==seekfile
):
4278 catfp
.seek(seekstart
, 0);
4280 catfheadsize
= int(preheaderdata
[0], 16);
4281 catfnumfields
= int(preheaderdata
[1], 16);
4282 catftype
= int(preheaderdata
[2], 16);
4283 if(re
.findall("^[.|/]", preheaderdata
[3])):
4284 catfname
= preheaderdata
[3];
4286 catfname
= "./"+preheaderdata
[3];
4287 catflinkname
= preheaderdata
[4];
4288 catfsize
= int(preheaderdata
[5], 16);
4289 catfbasedir
= os
.path
.dirname(catfname
);
4291 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
4295 catlist
.update({'catfp': catfp
});
4300 create_alias_function("", __file_format_name__
, "SeekToFileName", ArchiveFileSeekToFileName
);
4302 def ArchiveFileValidate(infile
, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
4304 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
4305 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4308 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4309 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
4310 if(checkcompressfile
=="tarfile"):
4311 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4312 if(checkcompressfile
=="zipfile"):
4313 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4314 if(rarfile_support
and checkcompressfile
=="rarfile"):
4315 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4316 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4317 return SevenZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4318 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4325 if(hasattr(sys
.stdin
, "buffer")):
4326 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
4328 shutil
.copyfileobj(sys
.stdin
, catfp
);
4330 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4334 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4335 catfp
= download_file_from_internet_file(infile
);
4336 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4342 infile
= RemoveWindowsPath(infile
);
4343 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
4344 if(checkcompressfile
=="tarfile"):
4345 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4346 if(checkcompressfile
=="zipfile"):
4347 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4348 if(rarfile_support
and checkcompressfile
=="rarfile"):
4349 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4350 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4351 return SevenZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4352 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4354 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
4355 if(not compresscheck
):
4356 fextname
= os
.path
.splitext(infile
)[1];
4357 if(fextname
==".gz"):
4358 compresscheck
= "gzip";
4359 elif(fextname
==".bz2"):
4360 compresscheck
= "bzip2";
4361 elif(fextname
==".zst"):
4362 compresscheck
= "zstd";
4363 elif(fextname
==".lz4" or fextname
==".clz4"):
4364 compresscheck
= "lz4";
4365 elif(fextname
==".lzo" or fextname
==".lzop"):
4366 compresscheck
= "lzo";
4367 elif(fextname
==".lzma" or fextname
==".xz"):
4368 compresscheck
= "lzma";
4371 if(not compresscheck
):
4373 catfp
= UncompressFile(infile
, formatspecs
, "rb");
4378 SeekToEndOfFile(catfp);
4380 SeekToEndOfFile(catfp);
4381 CatSize = catfp.tell();
4382 CatSizeEnd = CatSize;
4390 curloc
= catfp
.tell();
4393 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
4395 catfp
.seek(curloc
, 0);
4396 catstring
= catheader
[0];
4397 catversion
= re
.findall(r
"([\d]+)$", catstring
);
4398 fprenumfiles
= catheader
[1];
4399 fnumfiles
= int(fprenumfiles
, 16);
4400 fprechecksumtype
= catheader
[2];
4401 fprechecksum
= catheader
[3];
4403 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
4404 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4405 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
4406 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
4407 catfileheadercshex
= format(0, 'x').lower();
4408 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
4409 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4410 elif(fprechecksumtype
=="crc16_ccitt"):
4411 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4412 elif(fprechecksumtype
=="adler32"):
4413 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4414 elif(fprechecksumtype
=="crc32"):
4415 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4416 elif(fprechecksumtype
=="crc64_ecma"):
4417 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4418 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
4419 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4420 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
4421 checksumoutstr
= hashlib
.new(fprechecksumtype
);
4422 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4423 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4425 catfileheadercshex
= format(0, 'x').lower();
4426 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4427 valid_archive
= True;
4428 invalid_archive
= False;
4430 VerbosePrintOut(infile
);
4431 VerbosePrintOut("Number of Records " + str(fnumfiles
));
4432 if(fprechecksum
==catfileheadercshex
):
4434 VerbosePrintOut("File Header Checksum Passed at offset " + str(0));
4437 VerbosePrintOut("File Header Checksum Failed at offset " + str(0));
4438 valid_archive
= False;
4439 invalid_archive
= True;
4441 VerbosePrintOut("");
4442 while(il
<fnumfiles
):
4443 catfhstart
= catfp
.tell();
4445 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
4447 catheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
4448 if(len(catheaderdata
)==0):
4450 catfheadsize
= int(catheaderdata
[0], 16);
4451 catfnumfields
= int(catheaderdata
[1], 16);
4452 catftype
= int(catheaderdata
[2], 16);
4453 if(re
.findall("^[.|/]", catheaderdata
[3])):
4454 catfname
= catheaderdata
[3];
4456 catfname
= "./"+catheaderdata
[3];
4457 catfbasedir
= os
.path
.dirname(catfname
);
4458 catflinkname
= catheaderdata
[4];
4459 catfsize
= int(catheaderdata
[5], 16);
4460 catfatime
= int(catheaderdata
[6], 16);
4461 catfmtime
= int(catheaderdata
[7], 16);
4462 catfctime
= int(catheaderdata
[8], 16);
4463 catfbtime
= int(catheaderdata
[9], 16);
4464 catfmode
= int(catheaderdata
[10], 16);
4465 catfchmode
= stat
.S_IMODE(catfmode
);
4466 catftypemod
= stat
.S_IFMT(catfmode
);
4467 prefwinattributes
= int(catheaderdata
[11], 16);
4468 catfuid
= int(catheaderdata
[12], 16);
4469 catfuname
= catheaderdata
[13];
4470 catfgid
= int(catheaderdata
[14], 16);
4471 catfgname
= catheaderdata
[15];
4472 fid
= int(catheaderdata
[16], 16);
4473 finode
= int(catheaderdata
[17], 16);
4474 flinkcount
= int(catheaderdata
[18], 16);
4475 catfdev_minor
= int(catheaderdata
[19], 16);
4476 catfdev_major
= int(catheaderdata
[20], 16);
4477 catfrdev_minor
= int(catheaderdata
[21], 16);
4478 catfrdev_major
= int(catheaderdata
[22], 16);
4479 catfextrasize
= int(catheaderdata
[23], 16);
4480 catfextrafields
= int(catheaderdata
[24], 16);
4481 extrafieldslist
= [];
4483 extraend
= extrastart
+ catfextrafields
;
4484 extrafieldslist
= [];
4485 if(extrastart
<extraend
):
4486 extrafieldslist
.append(catheaderdata
[extrastart
]);
4487 extrastart
= extrastart
+ 1;
4488 catfchecksumtype
= catheaderdata
[extrastart
].lower();
4489 catfcs
= catheaderdata
[extrastart
+ 1].lower();
4490 catfccs
= catheaderdata
[extrastart
+ 2].lower();
4492 hcmax
= len(catheaderdata
) - 2;
4495 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[5]);
4497 catnewfcs
= GetFileChecksum(hout
, catheaderdata
[-3].lower(), True, formatspecs
);
4499 VerbosePrintOut(catfname
);
4500 VerbosePrintOut("Record Number " + str(il
) + "; File ID " + str(fid
) + "; iNode Number " + str(finode
));
4501 if(catfcs
==catnewfcs
):
4503 VerbosePrintOut("File Header Checksum Passed at offset " + str(catfhstart
));
4506 VerbosePrintOut("File Header Checksum Failed at offset " + str(catfhstart
));
4507 valid_archive
= False;
4508 invalid_archive
= True;
4509 catfhend
= catfp
.tell() - 1;
4510 catfcontentstart
= catfp
.tell();
4512 pyhascontents
= False;
4514 catfcontents
= catfp
.read(catfsize
);
4515 catnewfccs
= GetFileChecksum(catfcontents
, catheaderdata
[-3].lower(), False, formatspecs
);
4516 pyhascontents
= True;
4517 if(catfccs
==catnewfccs
):
4519 VerbosePrintOut("File Content Checksum Passed at offset " + str(catfcontentstart
));
4522 VerbosePrintOut("File Content Checksum Failed at offset " + str(catfcontentstart
));
4523 valid_archive
= False;
4524 invalid_archive
= True;
4526 VerbosePrintOut("");
4539 create_alias_function("", __file_format_name__
, "Validate", ArchiveFileValidate
);
4541 def ArchiveFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4542 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4545 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4546 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
4547 if(checkcompressfile
=="tarfile"):
4548 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4549 if(checkcompressfile
=="zipfile"):
4550 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4551 if(rarfile_support
and checkcompressfile
=="rarfile"):
4552 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4553 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4554 return SevenZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4555 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4562 if(hasattr(sys
.stdin
, "buffer")):
4563 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
4565 shutil
.copyfileobj(sys
.stdin
, catfp
);
4567 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4571 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4572 catfp
= download_file_from_internet_file(infile
);
4573 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4579 infile
= RemoveWindowsPath(infile
);
4580 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
4581 if(checkcompressfile
=="tarfile"):
4582 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4583 if(checkcompressfile
=="zipfile"):
4584 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4585 if(rarfile_support
and checkcompressfile
=="rarfile"):
4586 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4587 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4588 return SevenZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4589 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4591 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
4592 if(not compresscheck
):
4593 fextname
= os
.path
.splitext(infile
)[1];
4594 if(fextname
==".gz"):
4595 compresscheck
= "gzip";
4596 elif(fextname
==".bz2"):
4597 compresscheck
= "bzip2";
4598 elif(fextname
==".zst"):
4599 compresscheck
= "zstd";
4600 elif(fextname
==".lz4" or fextname
==".clz4"):
4601 compresscheck
= "lz4";
4602 elif(fextname
==".lzo" or fextname
==".lzop"):
4603 compresscheck
= "lzo";
4604 elif(fextname
==".lzma" or fextname
==".xz"):
4605 compresscheck
= "lzma";
4608 if(not compresscheck
):
4610 catfp
= UncompressFile(infile
, formatspecs
, "rb");
4615 SeekToEndOfFile(catfp);
4617 SeekToEndOfFile(catfp);
4618 CatSize = catfp.tell();
4619 CatSizeEnd = CatSize;
4627 curloc
= catfp
.tell();
4630 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
4632 catfp
.seek(curloc
, 0);
4633 catstring
= catheader
[0];
4634 catversion
= re
.findall(r
"([\d]+)$", catstring
);
4635 fprenumfiles
= catheader
[1];
4636 fnumfiles
= int(fprenumfiles
, 16);
4637 fprechecksumtype
= catheader
[2];
4638 fprechecksum
= catheader
[3];
4639 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
4640 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4641 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
4642 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
4643 catfileheadercshex
= format(0, 'x').lower();
4644 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
4645 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4646 elif(fprechecksumtype
=="crc16_ccitt"):
4647 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4648 elif(fprechecksumtype
=="adler32"):
4649 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4650 elif(fprechecksumtype
=="crc32"):
4651 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4652 elif(fprechecksumtype
=="crc64_ecma"):
4653 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4654 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
4655 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4656 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
4657 checksumoutstr
= hashlib
.new(fprechecksumtype
);
4658 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4659 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4661 catfileheadercshex
= format(0, 'x').lower();
4662 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4663 fheadtell
= len(fileheader
);
4664 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
4665 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
4667 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4668 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
4669 if(seekstart
<0 and seekstart
>fnumfiles
):
4671 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
4672 seekend
= fnumfiles
;
4673 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
4674 seekend
= fnumfiles
- abs(seekend
);
4677 while(il
< seekstart
):
4678 prefhstart
= catfp
.tell();
4680 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
4682 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
4683 if(len(preheaderdata
)==0):
4685 prefheadsize
= int(preheaderdata
[0], 16);
4686 prefnumfields
= int(preheaderdata
[1], 16);
4687 if(re
.findall("^[.|/]", preheaderdata
[3])):
4688 prefname
= preheaderdata
[3];
4690 prefname
= "./"+preheaderdata
[3];
4691 prefsize
= int(preheaderdata
[5], 16);
4692 prefextrasize
= int(preheaderdata
[23], 16);
4693 prefextrafields
= int(preheaderdata
[24], 16);
4694 extrafieldslist
= [];
4696 extraend
= extrastart
+ prefextrafields
;
4697 extrafieldslist
= [];
4698 if(extrastart
<extraend
):
4699 extrafieldslist
.append(preheaderdata
[extrastart
]);
4700 extrastart
= extrastart
+ 1;
4701 prefchecksumtype
= preheaderdata
[extrastart
].lower();
4702 prefcs
= preheaderdata
[extrastart
+ 1].lower();
4703 prefccs
= preheaderdata
[extrastart
+ 2].lower();
4705 hcmax
= len(preheaderdata
) - 2;
4708 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
4710 prenewfcs
= GetFileChecksum(hout
, preheaderdata
[-3].lower(), True, formatspecs
);
4711 if(prefcs
!=prenewfcs
and not skipchecksum
):
4712 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
4714 valid_archive
= False;
4715 invalid_archive
= True;
4716 prefhend
= catfp
.tell() - 1;
4717 prefcontentstart
= catfp
.tell();
4719 pyhascontents
= False;
4721 prefcontents
= catfp
.read(prefsize
);
4722 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
4723 pyhascontents
= True;
4724 if(prefccs
!=prenewfccs
and not skipchecksum
):
4725 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
4729 fileidnum
= seekstart
;
4731 while(fileidnum
<seekend
):
4732 catfhstart
= catfp
.tell();
4734 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
4736 catheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
4737 if(len(catheaderdata
)==0):
4739 catfheadsize
= int(catheaderdata
[0], 16);
4740 catfnumfields
= int(catheaderdata
[1], 16);
4741 catftype
= int(catheaderdata
[2], 16);
4742 if(re
.findall("^[.|/]", catheaderdata
[3])):
4743 catfname
= catheaderdata
[3];
4745 catfname
= "./"+catheaderdata
[3];
4746 catfbasedir
= os
.path
.dirname(catfname
);
4747 catflinkname
= catheaderdata
[4];
4748 catfsize
= int(catheaderdata
[5], 16);
4749 catfatime
= int(catheaderdata
[6], 16);
4750 catfmtime
= int(catheaderdata
[7], 16);
4751 catfctime
= int(catheaderdata
[8], 16);
4752 catfbtime
= int(catheaderdata
[9], 16);
4753 catfmode
= int(catheaderdata
[10], 16);
4754 catfchmode
= stat
.S_IMODE(catfmode
);
4755 catftypemod
= stat
.S_IFMT(catfmode
);
4756 catfwinattributes
= int(catheaderdata
[11], 16);
4757 catfuid
= int(catheaderdata
[12], 16);
4758 catfuname
= catheaderdata
[13];
4759 catfgid
= int(catheaderdata
[14], 16);
4760 catfgname
= catheaderdata
[15];
4761 fid
= int(catheaderdata
[16], 16);
4762 finode
= int(catheaderdata
[17], 16);
4763 flinkcount
= int(catheaderdata
[18], 16);
4764 catfdev_minor
= int(catheaderdata
[19], 16);
4765 catfdev_major
= int(catheaderdata
[20], 16);
4766 catfrdev_minor
= int(catheaderdata
[21], 16);
4767 catfrdev_major
= int(catheaderdata
[22], 16);
4768 catfextrasize
= int(catheaderdata
[23], 16);
4769 catfextrafields
= int(catheaderdata
[24], 16);
4770 extrafieldslist
= [];
4772 extraend
= extrastart
+ catfextrafields
;
4773 extrafieldslist
= [];
4774 if(extrastart
<extraend
):
4775 extrafieldslist
.append(catheaderdata
[extrastart
]);
4776 extrastart
= extrastart
+ 1;
4777 catfchecksumtype
= catheaderdata
[extrastart
].lower();
4778 catfcs
= catheaderdata
[extrastart
+ 1].lower();
4779 catfccs
= catheaderdata
[extrastart
+ 2].lower();
4781 hcmax
= len(catheaderdata
) - 2;
4784 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[5]);
4786 catnewfcs
= GetFileChecksum(hout
, catheaderdata
[-3].lower(), True, formatspecs
);
4787 if(catfcs
!=catnewfcs
and not skipchecksum
):
4788 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfhstart
));
4790 catfhend
= catfp
.tell() - 1;
4791 catfcontentstart
= catfp
.tell();
4793 pyhascontents
= False;
4794 if(catfsize
>0 and not listonly
):
4795 catfcontents
= catfp
.read(catfsize
);
4796 catnewfccs
= GetFileChecksum(catfcontents
, catheaderdata
[-3].lower(), False, formatspecs
);
4797 pyhascontents
= True;
4798 if(catfccs
!=catnewfccs
and skipchecksum
):
4799 VerbosePrintOut("File Content Checksum Error with file " + catfname
+ " at offset " + str(catfcontentstart
));
4801 if(catfsize
>0 and listonly
):
4802 catfp
.seek(catfsize
, 1);
4803 pyhascontents
= False;
4805 catfcontentend
= catfp
.tell() - 1;
4806 catlist
['ffilelist'].update({realidnum
: {'fid': realidnum
, 'fidalt': fileidnum
, 'fheadersize': catfheadsize
, 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
, 'fatime': catfatime
, 'fmtime': catfmtime
, 'fctime': catfctime
, 'fbtime': catfbtime
, 'fmode': catfmode
, 'fchmode': catfchmode
, 'ftypemod': catftypemod
, 'fwinattributes': catfwinattributes
, 'fuid': catfuid
, 'funame': catfuname
, 'fgid': catfgid
, 'fgname': catfgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': catfdev_minor
, 'fmajor': catfdev_major
, 'frminor': catfrdev_minor
, 'frmajor': catfrdev_major
, 'fchecksumtype': catfchecksumtype
, 'fnumfields': catfnumfields
+ 2, 'frawheader': catheaderdata
, 'fextrafields': catfextrafields
, 'fextrafieldsize': catfextrasize
, 'fextralist': extrafieldslist
, 'fheaderchecksum': catfcs
, 'fcontentchecksum': catfccs
, 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': catfcontents
} });
4807 fileidnum
= fileidnum
+ 1;
4808 realidnum
= realidnum
+ 1;
4810 catlist
.update({'catfp': catfp
});
4815 create_alias_function("", __file_format_name__
, "ToArray", ArchiveFileToArray
);
4817 def ArchiveFileStringToArray(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4818 catfp
= BytesIO(catstr
);
4819 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4820 return listcatfiles
;
4822 create_alias_function("", __file_format_name__
, "StringToArray", ArchiveFileStringToArray
);
4824 def TarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4826 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4827 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4828 return listcatfiles
;
4830 def ZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4832 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4833 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4834 return listcatfiles
;
4836 if(not rarfile_support
):
4837 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4840 if(rarfile_support
):
4841 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4843 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4844 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4845 return listcatfiles
;
4847 def ListDirToArrayAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4848 catver
= formatspecs
[6];
4849 fileheaderver
= str(int(catver
.replace(".", "")));
4850 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4851 advancedlist
= formatspecs
[8];
4852 altinode
= formatspecs
[9];
4855 for line
in sys
.stdin
:
4856 infilelist
.append(line
.strip());
4857 infilelist
= list(filter(None, infilelist
));
4858 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
4859 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
4861 with
UncompressFile(infiles
, formatspecs
, "r") as finfile
:
4862 for line
in finfile
:
4863 infilelist
.append(line
.strip());
4864 infilelist
= list(filter(None, infilelist
));
4866 if(isinstance(infiles
, (list, tuple, ))):
4867 infilelist
= list(filter(None, infiles
));
4868 elif(isinstance(infiles
, (str, ))):
4869 infilelist
= list(filter(None, [infiles
]));
4871 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
4873 GetDirList
= ListDir(infilelist
, followlink
, False);
4881 inodetocatinode
= {};
4883 fnumfiles
= int(len(GetDirList
));
4884 catver
= formatspecs
[6];
4885 fileheaderver
= str(int(catver
.replace(".", "")));
4886 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4887 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4888 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4889 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4890 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4891 if(checksumtype
=="none" or checksumtype
==""):
4892 catfileheadercshex
= format(0, 'x').lower();
4893 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4894 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4895 elif(checksumtype
=="crc16_ccitt"):
4896 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4897 elif(checksumtype
=="adler32"):
4898 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4899 elif(checksumtype
=="crc32"):
4900 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4901 elif(checksumtype
=="crc64_ecma"):
4902 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4903 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4904 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4905 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4906 checksumoutstr
= hashlib
.new(checksumtype
);
4907 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4908 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4910 catfileheadercshex
= format(0, 'x').lower();
4911 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4912 fheadtell
= len(fileheader
);
4913 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4914 for curfname
in GetDirList
:
4915 if(re
.findall("^[.|/]", curfname
)):
4918 fname
= "./"+curfname
;
4920 VerbosePrintOut(fname
);
4921 if(not followlink
or followlink
is None):
4922 fstatinfo
= os
.lstat(fname
);
4924 fstatinfo
= os
.stat(fname
);
4925 fpremode
= fstatinfo
.st_mode
;
4926 finode
= fstatinfo
.st_ino
;
4927 flinkcount
= fstatinfo
.st_nlink
;
4929 if(stat
.S_ISREG(fpremode
)):
4931 elif(stat
.S_ISLNK(fpremode
)):
4933 elif(stat
.S_ISCHR(fpremode
)):
4935 elif(stat
.S_ISBLK(fpremode
)):
4937 elif(stat
.S_ISDIR(fpremode
)):
4939 elif(stat
.S_ISFIFO(fpremode
)):
4941 elif(stat
.S_ISSOCK(fpremode
)):
4943 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
4945 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
4947 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
4952 fbasedir
= os
.path
.dirname(fname
);
4954 if(not followlink
and finode
!=0):
4956 if(finode
in inodelist
):
4958 flinkname
= inodetofile
[finode
];
4962 fcurinode
= inodetocatinode
[finode
];
4963 if(finode
not in inodelist
):
4964 inodelist
.append(finode
);
4965 inodetofile
.update({finode
: fname
});
4966 inodetocatinode
.update({finode
: curinode
});
4970 fcurinode
= curinode
;
4971 curinode
= curinode
+ 1;
4973 fcurinode
= curinode
;
4974 curinode
= curinode
+ 1;
4975 curfid
= curfid
+ 1;
4977 flinkname
= os
.readlink(fname
);
4978 fdev
= fstatinfo
.st_dev
;
4979 getfdev
= GetDevMajorMinor(fdev
);
4980 fdev_minor
= getfdev
[0];
4981 fdev_major
= getfdev
[1];
4982 frdev
= fstatinfo
.st_dev
;
4983 if(hasattr(fstatinfo
, "st_rdev")):
4984 frdev
= fstatinfo
.st_rdev
;
4986 frdev
= fstatinfo
.st_dev
;
4987 getfrdev
= GetDevMajorMinor(frdev
);
4988 frdev_minor
= getfrdev
[0];
4989 frdev_major
= getfrdev
[1];
4990 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
4992 if(ftype
==0 or ftype
==7):
4993 fsize
= fstatinfo
.st_size
;
4994 fatime
= fstatinfo
.st_atime
;
4995 fmtime
= fstatinfo
.st_mtime
;
4996 fctime
= fstatinfo
.st_ctime
;
4997 if(hasattr(fstatinfo
, "st_birthtime")):
4998 fbtime
= fstatinfo
.st_birthtime
;
5000 fbtime
= fstatinfo
.st_ctime
;
5001 fmode
= fstatinfo
.st_mode
;
5002 fchmode
= stat
.S_IMODE(fstatinfo
.st_mode
);
5003 ftypemod
= stat
.S_IFMT(fstatinfo
.st_mode
);
5004 fuid
= fstatinfo
.st_uid
;
5005 fgid
= fstatinfo
.st_gid
;
5010 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
5011 funame
= userinfo
.pw_name
;
5020 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
5021 fgname
= groupinfo
.gr_name
;
5026 fdev_minor
= fdev_minor
;
5027 fdev_major
= fdev_major
;
5028 frdev_minor
= frdev_minor
;
5029 frdev_major
= frdev_major
;
5031 flinkcount
= flinkcount
;
5032 if(hasattr(fstatinfo
, "st_file_attributes")):
5033 fwinattributes
= fstatinfo
.st_file_attributes
;
5036 fcontents
= "".encode('UTF-8');
5038 if(ftype
==0 or ftype
==7):
5039 with
open(fname
, "rb") as fpc
:
5041 chunk
= fpc
.read(chunk_size
);
5045 if(followlink
and (ftype
==1 or ftype
==2)):
5046 flstatinfo
= os
.stat(flinkname
);
5047 with
open(flinkname
, "rb") as fpc
:
5049 chunk
= fpc
.read(chunk_size
);
5053 ftypehex
= format(ftype
, 'x').lower();
5054 extrafields
= len(extradata
);
5055 extrafieldslist
= extradata
;
5056 catfextrafields
= extrafields
;
5057 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5058 if(len(extradata
)>0):
5059 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5060 extrasizelen
= len(extrasizestr
);
5061 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5062 catoutlist
= [ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fwinattributes
), 'x').lower(), format(int(fuid
), 'x').lower(), funame
, format(int(fgid
), 'x').lower(), fgname
, format(int(fcurfid
), 'x').lower(), format(int(fcurinode
), 'x').lower(), format(int(flinkcount
), 'x').lower(), format(int(fdev_minor
), 'x').lower(), format(int(fdev_major
), 'x').lower(), format(int(frdev_minor
), 'x').lower(), format(int(frdev_major
), 'x').lower(), extrasizelenhex
, format(catfextrafields
, 'x').lower()];
5063 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5064 catoutlenhex
= format(catoutlen
, 'x').lower();
5065 catoutlist
.insert(0, catoutlenhex
);
5066 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5067 if(len(extradata
)>0):
5068 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5069 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5070 catfnumfields
= catoutlen
;
5071 if(checksumtype
=="none" or checksumtype
==""):
5072 catfileheadercshex
= format(0, 'x').lower();
5073 catfilecontentcshex
= format(0, 'x').lower();
5074 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5075 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5076 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
5077 elif(checksumtype
=="crc16_ccitt"):
5078 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5079 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
5080 elif(checksumtype
=="adler32"):
5081 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5082 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
5083 elif(checksumtype
=="crc32"):
5084 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5085 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
5086 elif(checksumtype
=="crc64_ecma"):
5087 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5088 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
5089 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5090 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5091 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
5092 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5093 checksumoutstr
= hashlib
.new(checksumtype
);
5094 checksumoutstr
.update("".encode('UTF-8'));
5095 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5096 checksumoutstr
= hashlib
.new(checksumtype
);
5097 checksumoutstr
.update(fcontents
);
5098 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
5100 catfileheadercshex
= format(0, 'x').lower();
5101 catfilecontentcshex
= format(0, 'x').lower();
5102 catfhstart
= fheadtell
;
5103 fheadtell
+= len(catfileoutstr
);
5104 catfhend
= fheadtell
- 1;
5105 catfcontentstart
= fheadtell
;
5106 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5107 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
5108 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5109 catheaderdata
= [catheaersize
] + catoutlist
+ extradata
+ [checksumtype
, catfileheadercshex
, catfilecontentcshex
]
5110 if(checksumtype
=="none" or checksumtype
==""):
5111 catfileheadercshex
= format(0, 'x').lower();
5112 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5113 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5114 elif(checksumtype
=="crc16_ccitt"):
5115 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5116 elif(checksumtype
=="adler32"):
5117 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5118 elif(checksumtype
=="crc32"):
5119 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5120 elif(checksumtype
=="crc64_ecma"):
5121 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5122 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5123 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5124 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5125 checksumoutstr
= hashlib
.new(checksumtype
);
5126 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5127 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5129 catfileheadercshex
= format(0, 'x').lower();
5130 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5131 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5132 nullstrecd
= formatspecs
[5].encode('UTF-8');
5133 fheadtell
+= len(catfileoutstr
) + 1;
5134 catfcontentend
= fheadtell
- 1;
5135 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
5136 pyhascontents
= False;
5137 if(int(fsize
)>0 and not listonly
):
5138 pyhascontents
= True;
5139 if(int(fsize
)>0 and listonly
):
5141 pyhascontents
= False;
5142 catlist
['ffilelist'].update({fileidnum
: {'fid': fileidnum
, 'fidalt': fileidnum
, 'fheadersize': int(catheaersize
, 16), 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fwinattributes': fwinattributes
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': checksumtype
, 'fnumfields': catfnumfields
+ 2, 'frawheader': catheaderdata
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
5143 fileidnum
= fileidnum
+ 1;
5146 def TarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5152 inodetocatinode
= {};
5156 if(hasattr(sys
.stdin
, "buffer")):
5157 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
5159 shutil
.copyfileobj(sys
.stdin
, infile
);
5164 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
5165 infile
= download_file_from_internet_file(infile
);
5170 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5172 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
5174 if(not tarfile
.is_tarfile(infile
)):
5176 except AttributeError:
5177 if(not is_tarfile(infile
)):
5182 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
5183 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
5185 tarfp
= tarfile
.open(infile
, "r");
5186 except FileNotFoundError
:
5188 fnumfiles
= int(len(tarfp
.getmembers()));
5189 catver
= formatspecs
[6];
5190 fileheaderver
= str(int(catver
.replace(".", "")));
5191 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5192 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5193 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
5194 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5195 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5196 if(checksumtype
=="none" or checksumtype
==""):
5197 catfileheadercshex
= format(0, 'x').lower();
5198 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5199 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
5200 elif(checksumtype
=="crc16_ccitt"):
5201 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
5202 elif(checksumtype
=="adler32"):
5203 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5204 elif(checksumtype
=="crc32"):
5205 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5206 elif(checksumtype
=="crc64_ecma"):
5207 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5208 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5209 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5210 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5211 checksumoutstr
= hashlib
.new(checksumtype
);
5212 checksumoutstr
.update(fileheader
.encode('UTF-8'));
5213 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5215 catfileheadercshex
= format(0, 'x').lower();
5216 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
5217 fheadtell
= len(fileheader
);
5218 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5219 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
5220 if(re
.findall("^[.|/]", member
.name
)):
5221 fname
= member
.name
;
5223 fname
= "./"+member
.name
;
5225 VerbosePrintOut(fname
);
5226 fpremode
= member
.mode
;
5227 ffullmode
= member
.mode
;
5231 ffullmode
= member
.mode
+ stat
.S_IFREG
;
5233 elif(member
.isdev()):
5234 ffullmode
= member
.mode
;
5236 elif(member
.islnk()):
5237 ffullmode
= member
.mode
+ stat
.S_IFREG
;
5239 elif(member
.issym()):
5240 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
5242 elif(member
.ischr()):
5243 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
5245 elif(member
.isblk()):
5246 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
5248 elif(member
.isdir()):
5249 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
5251 elif(member
.isfifo()):
5252 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
5254 elif(member
.issparse()):
5255 ffullmode
= member
.mode
;
5258 ffullmode
= member
.mode
;
5261 fbasedir
= os
.path
.dirname(fname
);
5265 curfid
= curfid
+ 1;
5267 flinkname
= member
.linkname
;
5268 fdev_minor
= member
.devminor
;
5269 fdev_major
= member
.devmajor
;
5270 frdev_minor
= member
.devminor
;
5271 frdev_major
= member
.devmajor
;
5272 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
5274 elif(ftype
==0 or ftype
==7):
5275 fsize
= member
.size
;
5277 fsize
= member
.size
;
5278 fatime
= member
.mtime
;
5279 fmtime
= member
.mtime
;
5280 fctime
= member
.mtime
;
5281 fbtime
= member
.mtime
;
5283 fchmode
= stat
.S_IMODE(ffullmode
);
5284 ftypemod
= stat
.S_IFMT(ffullmode
);
5287 funame
= member
.uname
;
5288 fgname
= member
.gname
;
5289 flinkcount
= flinkcount
;
5290 fwinattributes
= int(0);
5291 fcontents
= "".encode('UTF-8');
5293 if(ftype
==0 or ftype
==7):
5294 with tarfp
.extractfile(member
) as fpc
:
5296 chunk
= fpc
.read(chunk_size
);
5300 ftypehex
= format(ftype
, 'x').lower();
5301 extrafields
= len(extradata
);
5302 extrafieldslist
= extradata
;
5303 catfextrafields
= extrafields
;
5304 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5305 if(len(extradata
)>0):
5306 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5307 extrasizelen
= len(extrasizestr
);
5308 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5309 catoutlist
= [ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fwinattributes
), 'x').lower(), format(int(fuid
), 'x').lower(), funame
, format(int(fgid
), 'x').lower(), fgname
, format(int(fcurfid
), 'x').lower(), format(int(fcurinode
), 'x').lower(), format(int(flinkcount
), 'x').lower(), format(int(fdev_minor
), 'x').lower(), format(int(fdev_major
), 'x').lower(), format(int(frdev_minor
), 'x').lower(), format(int(frdev_major
), 'x').lower(), extrasizelenhex
, format(catfextrafields
, 'x').lower()];
5310 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5311 catoutlenhex
= format(catoutlen
, 'x').lower();
5312 catoutlist
.insert(0, catoutlenhex
);
5313 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5314 if(len(extradata
)>0):
5315 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5316 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5317 catfnumfields
= catoutlen
;
5318 if(checksumtype
=="none" or checksumtype
==""):
5319 catfileheadercshex
= format(0, 'x').lower();
5320 catfilecontentcshex
= format(0, 'x').lower();
5321 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5322 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5323 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
5324 elif(checksumtype
=="crc16_ccitt"):
5325 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5326 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
5327 elif(checksumtype
=="adler32"):
5328 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5329 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
5330 elif(checksumtype
=="crc32"):
5331 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5332 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
5333 elif(checksumtype
=="crc64_ecma"):
5334 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5335 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
5336 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5337 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5338 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
5339 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5340 checksumoutstr
= hashlib
.new(checksumtype
);
5341 checksumoutstr
.update("".encode('UTF-8'));
5342 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5343 checksumoutstr
= hashlib
.new(checksumtype
);
5344 checksumoutstr
.update(fcontents
);
5345 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
5347 catfileheadercshex
= format(0, 'x').lower();
5348 catfilecontentcshex
= format(0, 'x').lower();
5349 catfhstart
= fheadtell
;
5350 fheadtell
+= len(catfileoutstr
);
5351 catfhend
= fheadtell
- 1;
5352 catfcontentstart
= fheadtell
;
5353 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5354 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
5355 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5356 catheaderdata
= [catheaersize
] + catoutlist
+ extradata
+ [checksumtype
, catfileheadercshex
, catfilecontentcshex
]
5357 if(checksumtype
=="none" or checksumtype
==""):
5358 catfileheadercshex
= format(0, 'x').lower();
5359 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5360 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5361 elif(checksumtype
=="crc16_ccitt"):
5362 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5363 elif(checksumtype
=="adler32"):
5364 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5365 elif(checksumtype
=="crc32"):
5366 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5367 elif(checksumtype
=="crc64_ecma"):
5368 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5369 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5370 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5371 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5372 checksumoutstr
= hashlib
.new(checksumtype
);
5373 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5374 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5376 catfileheadercshex
= format(0, 'x').lower();
5377 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5378 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5379 nullstrecd
= formatspecs
[5].encode('UTF-8');
5380 fheadtell
+= len(catfileoutstr
) + 1;
5381 catfcontentend
= fheadtell
- 1;
5382 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
5383 pyhascontents
= False;
5384 if(int(fsize
)>0 and not listonly
):
5385 pyhascontents
= True;
5386 if(int(fsize
)>0 and listonly
):
5388 pyhascontents
= False;
5389 catlist
['ffilelist'].update({fileidnum
: {'fid': fileidnum
, 'fidalt': fileidnum
, 'fheadersize': int(catheaersize
, 16), 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fwinattributes': fwinattributes
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': checksumtype
, 'fnumfields': catfnumfields
+ 2, 'frawheader': catheaderdata
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
5390 fileidnum
= fileidnum
+ 1;
5393 def ZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5399 inodetocatinode
= {};
5403 if(hasattr(sys
.stdin
, "buffer")):
5404 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
5406 shutil
.copyfileobj(sys
.stdin
, infile
);
5411 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
5412 infile
= download_file_from_internet_file(infile
);
5417 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5421 if(not zipfile
.is_zipfile(infile
)):
5424 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
5425 except FileNotFoundError
:
5427 ziptest
= zipfp
.testzip();
5429 VerbosePrintOut("Bad file found!");
5430 fnumfiles
= int(len(zipfp
.infolist()));
5431 catver
= formatspecs
[6];
5432 fileheaderver
= str(int(catver
.replace(".", "")));
5433 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5434 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5435 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5436 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5437 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
5438 if(checksumtype
=="none" or checksumtype
==""):
5439 catfileheadercshex
= format(0, 'x').lower();
5440 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5441 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
5442 elif(checksumtype
=="crc16_ccitt"):
5443 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
5444 elif(checksumtype
=="adler32"):
5445 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5446 elif(checksumtype
=="crc32"):
5447 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5448 elif(checksumtype
=="crc64_ecma"):
5449 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5450 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5451 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5452 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5453 checksumoutstr
= hashlib
.new(checksumtype
);
5454 checksumoutstr
.update(fileheader
.encode('UTF-8'));
5455 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5457 catfileheadercshex
= format(0, 'x').lower();
5458 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
5459 fheadtell
= len(fileheader
);
5460 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5461 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
5462 if(re
.findall("^[.|/]", member
.filename
)):
5463 fname
= member
.filename
;
5465 fname
= "./"+member
.filename
;
5466 zipinfo
= zipfp
.getinfo(member
.filename
);
5468 VerbosePrintOut(fname
);
5469 if(not member
.is_dir()):
5470 fpremode
= stat
.S_IFREG
+ 438;
5471 elif(member
.is_dir()):
5472 fpremode
= stat
.S_IFDIR
+ 511;
5475 if(not member
.is_dir()):
5477 elif(member
.is_dir()):
5480 fbasedir
= os
.path
.dirname(fname
);
5484 curfid
= curfid
+ 1;
5492 fsize
= member
.file_size
;
5494 fsize
= member
.file_size
;
5495 fatime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5496 fmtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5497 fctime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5498 fbtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5499 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
5500 fwinattributes
= int(zipinfo
.external_attr
);
5501 if(not member
.is_dir()):
5502 fmode
= int(stat
.S_IFREG
+ 438);
5503 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
5504 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
5505 elif(member
.is_dir()):
5506 fmode
= int(stat
.S_IFDIR
+ 511);
5507 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
5508 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
5509 elif(zipinfo
.create_system
==3):
5510 fwinattributes
= int(0);
5511 fmode
= int(zipinfo
.external_attr
);
5513 fwinattributes
= int(0);
5514 if(not member
.is_dir()):
5515 fmode
= int(stat
.S_IFREG
+ 438);
5516 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
5517 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
5518 elif(member
.is_dir()):
5519 fmode
= int(stat
.S_IFDIR
+ 511);
5520 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
5521 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
5524 except AttributeError:
5530 except AttributeError:
5537 userinfo
= pwd
.getpwuid(os
.getuid());
5538 funame
= userinfo
.pw_name
;
5541 except AttributeError:
5549 groupinfo
= grp
.getgrgid(os
.getgid());
5550 fgname
= groupinfo
.gr_name
;
5553 except AttributeError:
5557 fcontents
= "".encode('UTF-8');
5559 fcontents
= zipfp
.read(member
.filename
);
5560 ftypehex
= format(ftype
, 'x').lower();
5561 extrafields
= len(extradata
);
5562 extrafieldslist
= extradata
;
5563 catfextrafields
= extrafields
;
5564 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5565 if(len(extradata
)>0):
5566 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5567 extrasizelen
= len(extrasizestr
);
5568 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5569 catoutlist
= [ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fwinattributes
), 'x').lower(), format(int(fuid
), 'x').lower(), funame
, format(int(fgid
), 'x').lower(), fgname
, format(int(fcurfid
), 'x').lower(), format(int(fcurinode
), 'x').lower(), format(int(flinkcount
), 'x').lower(), format(int(fdev_minor
), 'x').lower(), format(int(fdev_major
), 'x').lower(), format(int(frdev_minor
), 'x').lower(), format(int(frdev_major
), 'x').lower(), extrasizelenhex
, format(catfextrafields
, 'x').lower()];
5570 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5571 catoutlenhex
= format(catoutlen
, 'x').lower();
5572 catoutlist
.insert(0, catoutlenhex
);
5573 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5574 if(len(extradata
)>0):
5575 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5576 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5577 catfnumfields
= catoutlen
;
5578 if(checksumtype
=="none" or checksumtype
==""):
5579 catfileheadercshex
= format(0, 'x').lower();
5580 catfilecontentcshex
= format(0, 'x').lower();
5581 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5582 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5583 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
5584 elif(checksumtype
=="crc16_ccitt"):
5585 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5586 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
5587 elif(checksumtype
=="adler32"):
5588 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5589 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
5590 elif(checksumtype
=="crc32"):
5591 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5592 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
5593 elif(checksumtype
=="crc64_ecma"):
5594 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5595 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
5596 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5597 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5598 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
5599 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5600 checksumoutstr
= hashlib
.new(checksumtype
);
5601 checksumoutstr
.update("".encode('UTF-8'));
5602 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5603 checksumoutstr
= hashlib
.new(checksumtype
);
5604 checksumoutstr
.update(fcontents
);
5605 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
5607 catfileheadercshex
= format(0, 'x').lower();
5608 catfilecontentcshex
= format(0, 'x').lower();
5609 catfhstart
= fheadtell
;
5610 fheadtell
+= len(catfileoutstr
);
5611 catfhend
= fheadtell
- 1;
5612 catfcontentstart
= fheadtell
;
5613 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5614 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
5615 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5616 catheaderdata
= [catheaersize
] + catoutlist
+ extradata
+ [checksumtype
, catfileheadercshex
, catfilecontentcshex
];
5617 if(checksumtype
=="none" or checksumtype
==""):
5618 catfileheadercshex
= format(0, 'x').lower();
5619 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5620 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5621 elif(checksumtype
=="crc16_ccitt"):
5622 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5623 elif(checksumtype
=="adler32"):
5624 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5625 elif(checksumtype
=="crc32"):
5626 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5627 elif(checksumtype
=="crc64_ecma"):
5628 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5629 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5630 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5631 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5632 checksumoutstr
= hashlib
.new(checksumtype
);
5633 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5634 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5636 catfileheadercshex
= format(0, 'x').lower();
5637 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5638 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5639 nullstrecd
= formatspecs
[5].encode('UTF-8');
5640 fheadtell
+= len(catfileoutstr
) + 1;
5641 catfcontentend
= fheadtell
- 1;
5642 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
5643 pyhascontents
= False;
5644 if(int(fsize
)>0 and not listonly
):
5645 pyhascontents
= True;
5646 if(int(fsize
)>0 and listonly
):
5648 pyhascontents
= False;
5649 catlist
['ffilelist'].update({fileidnum
: {'fid': fileidnum
, 'fidalt': fileidnum
, 'fheadersize': int(catheaersize
, 16), 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fwinattributes': fwinattributes
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': checksumtype
, 'fnumfields': catfnumfields
+ 2, 'frawheader': catheaderdata
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
5650 fileidnum
= fileidnum
+ 1;
5653 if(not rarfile_support
):
5654 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5657 if(rarfile_support
):
5658 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5664 inodetocatinode
= {};
5666 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
5668 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
5670 rarfp
= rarfile
.RarFile(infile
, "r");
5671 rartest
= rarfp
.testrar();
5673 VerbosePrintOut("Bad file found!");
5674 fnumfiles
= int(len(rarfp
.infolist()));
5675 catver
= formatspecs
[6];
5676 fileheaderver
= str(int(catver
.replace(".", "")));
5677 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5678 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5679 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5680 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5681 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
5682 if(checksumtype
=="none" or checksumtype
==""):
5683 catfileheadercshex
= format(0, 'x').lower();
5684 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5685 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
5686 elif(checksumtype
=="crc16_ccitt"):
5687 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
5688 elif(checksumtype
=="adler32"):
5689 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5690 elif(checksumtype
=="crc32"):
5691 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5692 elif(checksumtype
=="crc64_ecma"):
5693 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5694 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5695 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5696 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5697 checksumoutstr
= hashlib
.new(checksumtype
);
5698 checksumoutstr
.update(fileheader
.encode('UTF-8'));
5699 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5701 catfileheadercshex
= format(0, 'x').lower();
5702 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
5703 fheadtell
= len(fileheader
);
5704 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5705 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
5708 if(member
.host_os
==rarfile
.RAR_OS_UNIX
):
5711 member
.external_attr
5713 except AttributeError:
5715 elif(member
.host_os
==rarfile
.RAR_OS_WIN32
):
5718 member
.external_attr
5720 except AttributeError:
5725 if(re
.findall("^[.|/]", member
.filename
)):
5726 fname
= member
.filename
;
5728 fname
= "./"+member
.filename
;
5729 rarinfo
= rarfp
.getinfo(member
.filename
);
5731 VerbosePrintOut(fname
);
5732 if(is_unix
and member
.external_attr
!=0):
5733 fpremode
= int(member
.external_attr
);
5734 elif(member
.is_file()):
5735 fpremode
= stat
.S_IFREG
+ 438;
5736 elif(member
.is_symlink()):
5737 fpremode
= stat
.S_IFLNK
+ 438;
5738 elif(member
.is_dir()):
5739 fpremode
= stat
.S_IFDIR
+ 511;
5740 if(is_windows
and member
.external_attr
!=0):
5741 fwinattributes
= int(member
.external_attr
);
5743 fwinattributes
= int(0);
5746 if(member
.is_file()):
5748 elif(member
.is_symlink()):
5750 elif(member
.is_dir()):
5754 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
5755 fbasedir
= os
.path
.dirname(fname
);
5759 curfid
= curfid
+ 1;
5767 fsize
= member
.file_size
;
5770 fatime
= int(member
.atime
.timestamp());
5772 fatime
= int(member
.mtime
.timestamp());
5773 except AttributeError:
5774 fatime
= int(member
.mtime
.timestamp());
5775 fmtime
= int(member
.mtime
.timestamp());
5778 fctime
= int(member
.ctime
.timestamp());
5780 fctime
= int(member
.mtime
.timestamp());
5781 except AttributeError:
5782 fctime
= int(member
.mtime
.timestamp());
5783 fbtime
= int(member
.mtime
.timestamp());
5784 if(is_unix
and member
.external_attr
!=0):
5785 fmode
= int(member
.external_attr
);
5786 fchmode
= int(stat
.S_IMODE(member
.external_attr
));
5787 ftypemod
= int(stat
.S_IFMT(member
.external_attr
));
5788 elif(member
.is_file()):
5789 fmode
= int(stat
.S_IFREG
+ 438)
5790 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
5791 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
5792 elif(member
.is_symlink()):
5793 fmode
= int(stat
.S_IFLNK
+ 438)
5794 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
5795 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
5796 elif(member
.is_dir()):
5797 fmode
= int(stat
.S_IFDIR
+ 511)
5798 fchmode
= int(stat
.S_IMODE(stat
.S_IFDIR
+ 511));
5799 ftypemod
= int(stat
.S_IFMT(stat
.S_IFDIR
+ 511));
5802 except AttributeError:
5808 except AttributeError:
5815 userinfo
= pwd
.getpwuid(os
.getuid());
5816 funame
= userinfo
.pw_name
;
5819 except AttributeError:
5827 groupinfo
= grp
.getgrgid(os
.getgid());
5828 fgname
= groupinfo
.gr_name
;
5831 except AttributeError:
5835 fcontents
= "".encode('UTF-8');
5837 fcontents
= rarfp
.read(member
.filename
);
5838 ftypehex
= format(ftype
, 'x').lower();
5839 extrafields
= len(extradata
);
5840 extrafieldslist
= extradata
;
5841 catfextrafields
= extrafields
;
5842 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5843 if(len(extradata
)>0):
5844 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5845 extrasizelen
= len(extrasizestr
);
5846 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5847 catoutlist
= [ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fwinattributes
), 'x').lower(), format(int(fuid
), 'x').lower(), funame
, format(int(fgid
), 'x').lower(), fgname
, format(int(fcurfid
), 'x').lower(), format(int(fcurinode
), 'x').lower(), format(int(flinkcount
), 'x').lower(), format(int(fdev_minor
), 'x').lower(), format(int(fdev_major
), 'x').lower(), format(int(frdev_minor
), 'x').lower(), format(int(frdev_major
), 'x').lower(), extrasizelenhex
, format(catfextrafields
, 'x').lower()];
5848 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5849 catoutlenhex
= format(catoutlen
, 'x').lower();
5850 catoutlist
.insert(0, catoutlenhex
);
5851 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5852 if(len(extradata
)>0):
5853 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5854 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5855 catfnumfields
= 24 + catfextrafields
;
5856 if(checksumtype
=="none" or checksumtype
==""):
5857 catfileheadercshex
= format(0, 'x').lower();
5858 catfilecontentcshex
= format(0, 'x').lower();
5859 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5860 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5861 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
5862 elif(checksumtype
=="crc16_ccitt"):
5863 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5864 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
5865 elif(checksumtype
=="adler32"):
5866 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5867 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
5868 elif(checksumtype
=="crc32"):
5869 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5870 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
5871 elif(checksumtype
=="crc64_ecma"):
5872 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5873 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
5874 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5875 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5876 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
5877 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5878 checksumoutstr
= hashlib
.new(checksumtype
);
5879 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5880 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5882 catfileheadercshex
= format(0, 'x').lower();
5883 catfilecontentcshex
= format(0, 'x').lower();
5884 catfhstart
= fheadtell
;
5885 fheadtell
+= len(catfileoutstr
);
5886 catfhend
= fheadtell
- 1;
5887 catfcontentstart
= fheadtell
;
5888 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5889 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
5890 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5891 catheaderdata
= [catheaersize
] + catoutlist
+ extradata
+ [checksumtype
, catfileheadercshex
, catfilecontentcshex
];
5892 if(checksumtype
=="none" or checksumtype
==""):
5893 catfileheadercshex
= format(0, 'x').lower();
5894 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5895 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5896 elif(checksumtype
=="crc16_ccitt"):
5897 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5898 elif(checksumtype
=="adler32"):
5899 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5900 elif(checksumtype
=="crc32"):
5901 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5902 elif(checksumtype
=="crc64_ecma"):
5903 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5904 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5905 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5906 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5907 checksumoutstr
= hashlib
.new(checksumtype
);
5908 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5909 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5911 catfileheadercshex
= format(0, 'x').lower();
5912 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5913 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5914 nullstrecd
= formatspecs
[5].encode('UTF-8');
5915 fheadtell
+= len(catfileoutstr
) + 1;
5916 catfcontentend
= fheadtell
- 1;
5917 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
5918 pyhascontents
= False;
5919 if(int(fsize
)>0 and not listonly
):
5920 pyhascontents
= True;
5921 if(int(fsize
)>0 and listonly
):
5923 pyhascontents
= False;
5924 catlist
['ffilelist'].update({fileidnum
: {'fid': fileidnum
, 'fidalt': fileidnum
, 'fheadersize': int(catheaersize
, 16), 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fwinattributes': fwinattributes
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': checksumtype
, 'fnumfields': catfnumfields
+ 2, 'frawheader': catheaderdata
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
5925 fileidnum
= fileidnum
+ 1;
5928 if(not py7zr_support
):
5929 def SevenZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5933 def SevenZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5939 inodetocatinode
= {};
5941 szpfp
= py7zr
.SevenZipFile(infile
, mode
="r");
5942 file_content
= szpfp
.readall();
5943 #sztest = szpfp.testzip();
5944 sztestalt
= szpfp
.test();
5946 VerbosePrintOut("Bad file found!");
5947 numfiles
= int(len(szpfp
.list()));
5948 catver
= formatspecs
[6];
5949 fileheaderver
= str(int(catver
.replace(".", "")));
5950 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5951 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5952 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5953 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5954 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
5955 if(checksumtype
=="none" or checksumtype
==""):
5956 catfileheadercshex
= format(0, 'x').lower();
5957 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5958 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
5959 elif(checksumtype
=="crc16_ccitt"):
5960 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
5961 elif(checksumtype
=="adler32"):
5962 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5963 elif(checksumtype
=="crc32"):
5964 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5965 elif(checksumtype
=="crc64_ecma"):
5966 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5967 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5968 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5969 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5970 checksumoutstr
= hashlib
.new(checksumtype
);
5971 checksumoutstr
.update(fileheader
.encode('UTF-8'));
5972 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5974 catfileheadercshex
= format(0, 'x').lower();
5975 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
5976 fheadtell
= len(fileheader
);
5977 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5978 for member
in sorted(szpfp
.list(), key
=lambda x
: x
.filename
):
5979 if(re
.findall("^[.|/]", member
.filename
)):
5980 fname
= member
.filename
;
5982 fname
= "./"+member
.filename
;
5983 if(not member
.is_directory
):
5984 fpremode
= int(stat
.S_IFREG
+ 438);
5985 elif(member
.is_directory
):
5986 fpremode
= int(stat
.S_IFDIR
+ 511);
5987 fwinattributes
= int(0);
5990 if(member
.is_directory
):
5995 fbasedir
= os
.path
.dirname(fname
);
5999 curfid
= curfid
+ 1;
6006 fatime
= int(member
.creationtime
.timestamp());
6007 fmtime
= int(member
.creationtime
.timestamp());
6008 fctime
= int(member
.creationtime
.timestamp());
6009 fbtime
= int(member
.creationtime
.timestamp());
6010 if(member
.is_directory
):
6011 fmode
= int(stat
.S_IFDIR
+ 511)
6012 fchmode
= int(stat
.S_IMODE(stat
.S_IFDIR
+ 511));
6013 ftypemod
= int(stat
.S_IFMT(stat
.S_IFDIR
+ 511));
6015 fmode
= int(stat
.S_IFLNK
+ 438)
6016 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
6017 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
6020 except AttributeError:
6026 except AttributeError:
6033 userinfo
= pwd
.getpwuid(os
.getuid());
6034 funame
= userinfo
.pw_name
;
6037 except AttributeError:
6045 groupinfo
= grp
.getgrgid(os
.getgid());
6046 fgname
= groupinfo
.gr_name
;
6049 except AttributeError:
6053 fcontents
= "".encode('UTF-8');
6055 fcontents
= file_content
[member
.filename
].read();
6056 fsize
= format(len(fcontents
), 'x').lower();
6058 ftypehex
= format(ftype
, 'x').lower();
6059 extrafields
= len(extradata
);
6060 extrafieldslist
= extradata
;
6061 catfextrafields
= extrafields
;
6062 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
6063 if(len(extradata
)>0):
6064 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
6065 extrasizelen
= len(extrasizestr
);
6066 extrasizelenhex
= format(extrasizelen
, 'x').lower();
6067 catoutlist
= [ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fwinattributes
), 'x').lower(), format(int(fuid
), 'x').lower(), funame
, format(int(fgid
), 'x').lower(), fgname
, format(int(fcurfid
), 'x').lower(), format(int(fcurinode
), 'x').lower(), format(int(flinkcount
), 'x').lower(), format(int(fdev_minor
), 'x').lower(), format(int(fdev_major
), 'x').lower(), format(int(frdev_minor
), 'x').lower(), format(int(frdev_major
), 'x').lower(), extrasizelenhex
, format(catfextrafields
, 'x').lower()];
6068 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
6069 catoutlenhex
= format(catoutlen
, 'x').lower();
6070 catoutlist
.insert(0, catoutlenhex
);
6071 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
6072 if(len(extradata
)>0):
6073 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
6074 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
6075 catfnumfields
= 24 + catfextrafields
;
6076 if(checksumtype
=="none" or checksumtype
==""):
6077 catfileheadercshex
= format(0, 'x').lower();
6078 catfilecontentcshex
= format(0, 'x').lower();
6079 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
6080 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
6081 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
6082 elif(checksumtype
=="crc16_ccitt"):
6083 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
6084 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
6085 elif(checksumtype
=="adler32"):
6086 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
6087 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
6088 elif(checksumtype
=="crc32"):
6089 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
6090 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
6091 elif(checksumtype
=="crc64_ecma"):
6092 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6093 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
6094 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
6095 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6096 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
6097 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
6098 checksumoutstr
= hashlib
.new(checksumtype
);
6099 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
6100 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
6102 catfileheadercshex
= format(0, 'x').lower();
6103 catfilecontentcshex
= format(0, 'x').lower();
6104 catfhstart
= fheadtell
;
6105 fheadtell
+= len(catfileoutstr
);
6106 catfhend
= fheadtell
- 1;
6107 catfcontentstart
= fheadtell
;
6108 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
6109 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
6110 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
6111 catheaderdata
= [catheaersize
] + catoutlist
+ extradata
+ [checksumtype
, catfileheadercshex
, catfilecontentcshex
];
6112 if(checksumtype
=="none" or checksumtype
==""):
6113 catfileheadercshex
= format(0, 'x').lower();
6114 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
6115 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
6116 elif(checksumtype
=="crc16_ccitt"):
6117 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
6118 elif(checksumtype
=="adler32"):
6119 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
6120 elif(checksumtype
=="crc32"):
6121 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
6122 elif(checksumtype
=="crc64_ecma"):
6123 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6124 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
6125 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6126 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
6127 checksumoutstr
= hashlib
.new(checksumtype
);
6128 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
6129 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
6131 catfileheadercshex
= format(0, 'x').lower();
6132 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
6133 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
6134 nullstrecd
= formatspecs
[5].encode('UTF-8');
6135 fheadtell
+= len(catfileoutstr
) + 1;
6136 catfcontentend
= fheadtell
- 1;
6137 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
6138 pyhascontents
= False;
6139 if(int(fsize
)>0 and not listonly
):
6140 pyhascontents
= True;
6141 if(int(fsize
)>0 and listonly
):
6143 pyhascontents
= False;
6144 catlist
['ffilelist'].update({fileidnum
: {'fid': fileidnum
, 'fidalt': fileidnum
, 'fheadersize': int(catheaersize
, 16), 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fwinattributes': fwinattributes
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': checksumtype
, 'fnumfields': catfnumfields
+ 2, 'frawheader': catheaderdata
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
6145 fileidnum
= fileidnum
+ 1;
6148 def ListDirToArray(infiles
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6149 outarray
= BytesIO();
6150 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
6151 listcatfiles
= ArchiveFileToArray(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6152 return listcatfiles
;
6154 def ArchiveFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
6155 if(isinstance(infile
, dict)):
6156 listcatfiles
= infile
;
6158 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6159 infile
= RemoveWindowsPath(infile
);
6160 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6161 if(not listcatfiles
):
6163 catarray
= {'list': listcatfiles
, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
6165 catarray
.update({'catfp': listcatfiles
['catfp']});
6166 lenlist
= len(listcatfiles
['ffilelist']);
6168 lcfx
= int(listcatfiles
['fnumfiles']);
6169 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6170 lcfx
= int(lenlist
);
6172 lcfx
= int(listcatfiles
['fnumfiles']);
6174 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
6175 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
6176 catarray
['filetoid'].update(filetoidarray
);
6177 catarray
['idtofile'].update(idtofilearray
);
6178 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6179 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
6180 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
6181 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6182 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
6183 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
6184 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6185 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6186 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6187 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6188 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6189 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6190 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6191 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
6192 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
6193 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
6194 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6195 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6196 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
6197 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
6198 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
6199 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6200 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6201 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6202 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
6203 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
6204 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
6205 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6206 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6207 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6208 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6212 create_alias_function("", __file_format_name__
, "ToArrayIndex", ArchiveFileToArrayIndex
);
6214 def ListDirToArrayIndexAlt(infiles
, dirlistfromtxt
=False, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
6215 listcatfiles
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
6216 if(not listcatfiles
):
6218 catarray
= {'list': listcatfiles
, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
6219 lenlist
= len(listcatfiles
['ffilelist']);
6221 lcfx
= int(listcatfiles
['fnumfiles']);
6222 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6223 lcfx
= int(lenlist
);
6225 lcfx
= int(listcatfiles
['fnumfiles']);
6227 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
6228 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
6229 catarray
['filetoid'].update(filetoidarray
);
6230 catarray
['idtofile'].update(idtofilearray
);
6231 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6232 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
6233 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
6234 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6235 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
6236 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
6237 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6238 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6239 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6240 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6241 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6242 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6243 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6244 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
6245 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
6246 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
6247 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6248 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6249 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
6250 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
6251 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
6252 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6253 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6254 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6255 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
6256 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
6257 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
6258 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6259 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6260 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6261 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6265 def TarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
6266 listcatfiles
= TarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
6267 if(not listcatfiles
):
6269 catarray
= {'list': listcatfiles
, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
6270 lenlist
= len(listcatfiles
['ffilelist']);
6272 lcfx
= int(listcatfiles
['fnumfiles']);
6273 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6274 lcfx
= int(lenlist
);
6276 lcfx
= int(listcatfiles
['fnumfiles']);
6278 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
6279 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
6280 catarray
['filetoid'].update(filetoidarray
);
6281 catarray
['idtofile'].update(idtofilearray
);
6282 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6283 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
6284 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
6285 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6286 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
6287 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
6288 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6289 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6290 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6291 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6292 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6293 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6294 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6295 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
6296 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
6297 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
6298 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6299 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6300 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
6301 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
6302 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
6303 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6304 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6305 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6306 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
6307 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
6308 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
6309 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6310 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6311 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6312 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6316 def ZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
6317 listcatfiles
= ZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
6318 if(not listcatfiles
):
6320 catarray
= {'list': listcatfiles
, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
6321 lenlist
= len(listcatfiles
['ffilelist']);
6323 lcfx
= int(listcatfiles
['fnumfiles']);
6324 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6325 lcfx
= int(lenlist
);
6327 lcfx
= int(listcatfiles
['fnumfiles']);
6329 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
6330 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
6331 catarray
['filetoid'].update(filetoidarray
);
6332 catarray
['idtofile'].update(idtofilearray
);
6333 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6334 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
6335 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
6336 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6337 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
6338 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
6339 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6340 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6341 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6342 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6343 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6344 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6345 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6346 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
6347 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
6348 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
6349 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6350 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6351 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
6352 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
6353 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
6354 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6355 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6356 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6357 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
6358 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
6359 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
6360 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6361 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6362 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6363 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6367 if(not rarfile_support
):
6368 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
6371 if(rarfile_support
):
6372 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
6373 listcatfiles
= RarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
6374 if(not listcatfiles
):
6376 catarray
= {'list': listcatfiles
, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
6377 lenlist
= len(listcatfiles
['ffilelist']);
6379 lcfx
= int(listcatfiles
['fnumfiles']);
6380 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6381 lcfx
= int(lenlist
);
6383 lcfx
= int(listcatfiles
['fnumfiles']);
6385 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
6386 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
6387 catarray
['filetoid'].update(filetoidarray
);
6388 catarray
['idtofile'].update(idtofilearray
);
6389 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6390 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
6391 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
6392 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6393 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
6394 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
6395 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6396 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6397 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6398 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6399 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6400 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6401 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6402 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
6403 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
6404 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
6405 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6406 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6407 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
6408 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
6409 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
6410 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6411 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6412 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6413 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
6414 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
6415 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
6416 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6417 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6418 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6419 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6423 if(not py7zr_support
):
6424 def SevenZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
6428 def SevenZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
6429 listcatfiles
= SevenZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
6430 if(not listcatfiles
):
6432 catarray
= {'list': listcatfiles
, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
6433 lenlist
= len(listcatfiles
['ffilelist']);
6435 lcfx
= int(listcatfiles
['fnumfiles']);
6436 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6437 lcfx
= int(lenlist
);
6439 lcfx
= int(listcatfiles
['fnumfiles']);
6441 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
6442 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
6443 catarray
['filetoid'].update(filetoidarray
);
6444 catarray
['idtofile'].update(idtofilearray
);
6445 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6446 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
6447 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
6448 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6449 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
6450 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
6451 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6452 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6453 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6454 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6455 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6456 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6457 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6458 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
6459 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
6460 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
6461 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6462 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6463 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
6464 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
6465 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
6466 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6467 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6468 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6469 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
6470 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
6471 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
6472 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6473 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6474 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6475 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6479 def ArchiveFileStringToArrayIndex(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
6480 catfp
= BytesIO(catstr
);
6481 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6482 return listcatfiles
;
6484 create_alias_function("", __file_format_name__
, "StringToArrayIndex", ArchiveFileStringToArrayIndex
);
6486 def TarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
6488 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
6489 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6490 return listcatfiles
;
6492 def ZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
6494 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
6495 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6496 return listcatfiles
;
6498 if(not rarfile_support
):
6499 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
6502 if(rarfile_support
):
6503 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
6505 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
6506 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6507 return listcatfiles
;
6509 if(not rarfile_support
):
6510 def SevenZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
6513 if(rarfile_support
):
6514 def SevenZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
6516 catfp
= PackArchiveFileFromSevenZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
6517 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6518 return listcatfiles
;
6520 def ListDirToArrayIndex(infiles
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6521 outarray
= BytesIO();
6522 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, verbose
, True);
6523 listcatfiles
= ArchiveFileToArrayIndex(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
)
6524 return listcatfiles
;
6526 def RePackArchiveFile(infile
, outfile
, compression
="auto", compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6527 if(isinstance(infile
, dict)):
6528 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6529 listcatfiles
= prelistcatfiles
['list'];
6531 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6532 infile
= RemoveWindowsPath(infile
);
6534 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6535 listcatfiles
= prelistcatfiles
['list'];
6537 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6538 if(outfile
!="-" and not hasattr(infile
, "read") and not hasattr(outfile
, "write")):
6539 outfile
= RemoveWindowsPath(outfile
);
6540 checksumtype
= checksumtype
.lower();
6541 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
6542 checksumtype
="crc32";
6543 if(checksumtype
=="none"):
6545 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
6547 if(compression
not in compressionlist
and compression
is None):
6548 compression
= "auto";
6550 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6551 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
6552 if(os
.path
.exists(outfile
)):
6554 if(not listcatfiles
):
6559 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
6561 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
6564 fbasename
= os
.path
.splitext(outfile
)[0];
6565 fextname
= os
.path
.splitext(outfile
)[1];
6566 catfp
= CompressOpenFile(outfile
, compressionlevel
);
6567 catver
= formatspecs
[6];
6568 fileheaderver
= str(int(catver
.replace(".", "")));
6569 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
6570 catfp
.write(fileheader
.encode('UTF-8'));
6571 lenlist
= len(listcatfiles
['ffilelist']);
6572 fnumfiles
= int(listcatfiles
['fnumfiles']);
6573 if(lenlist
>fnumfiles
or lenlist
<fnumfiles
):
6574 fnumfiles
= lenlist
;
6575 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
6576 fnumfilesa
= AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
6577 if(checksumtype
=="none" or checksumtype
==""):
6578 catfileheadercshex
= format(0, 'x').lower();
6579 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
6580 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
6581 elif(checksumtype
=="crc16_ccitt"):
6582 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
6583 elif(checksumtype
=="adler32"):
6584 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
6585 elif(checksumtype
=="crc32"):
6586 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
6587 elif(checksumtype
=="crc64_ecma"):
6588 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6589 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
6590 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6591 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
6592 checksumoutstr
= hashlib
.new(checksumtype
);
6593 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
6594 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
6596 catfileheadercshex
= format(0, 'x').lower();
6597 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
6598 catfp
.write(fnumfilesa
.encode('UTF-8'));
6601 os
.fsync(catfp
.fileno());
6602 except io
.UnsupportedOperation
:
6604 except AttributeError:
6606 lenlist
= len(listcatfiles
['ffilelist']);
6607 fnumfiles
= int(listcatfiles
['fnumfiles']);
6609 lcfx
= int(listcatfiles
['fnumfiles']);
6610 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6611 lcfx
= int(lenlist
);
6613 lcfx
= int(listcatfiles
['fnumfiles']);
6621 catfhstart
= catfp
.tell();
6622 if(re
.findall("^[.|/]", listcatfiles
['ffilelist'][reallcfi
]['fname'])):
6623 fname
= listcatfiles
['ffilelist'][reallcfi
]['fname'];
6625 fname
= "./"+listcatfiles
['ffilelist'][reallcfi
]['fname'];
6627 VerbosePrintOut(fname
);
6628 fheadersize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fheadersize']), 'x').lower();
6629 fsize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fsize']), 'x').lower();
6630 flinkname
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
6631 fatime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fatime']), 'x').lower();
6632 fmtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmtime']), 'x').lower();
6633 fctime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fctime']), 'x').lower();
6634 fbtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fbtime']), 'x').lower();
6635 fmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmode']), 'x').lower();
6636 fchmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fchmode']), 'x').lower();
6637 fuid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fuid']), 'x').lower();
6638 funame
= listcatfiles
['ffilelist'][reallcfi
]['funame'];
6639 fgid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fgid']), 'x').lower();
6640 fgname
= listcatfiles
['ffilelist'][reallcfi
]['fgname'];
6641 finode
= listcatfiles
['ffilelist'][reallcfi
]['finode'];
6642 flinkcount
= listcatfiles
['ffilelist'][reallcfi
]['flinkcount'];
6643 fwinattributes
= flinkinfo
['ffilelist'][reallcfi
]['fwinattributes'];
6644 fdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fminor']), 'x').lower();
6645 fdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmajor']), 'x').lower();
6646 frdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frminor']), 'x').lower();
6647 frdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frmajor']), 'x').lower();
6648 if(len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] and len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>0):
6649 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
6650 if(len(extradata
)>0):
6651 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(extradata
);
6652 listcatfiles
['ffilelist'][reallcfi
]['fextralist'] = extradata
;
6653 extrafields
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']), 'x').lower();
6654 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
6655 if(len(extradata
)>0):
6656 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
6657 extrasizelen
= format(len(extrasizestr
), 'x').lower();
6658 fcontents
= listcatfiles
['ffilelist'][reallcfi
]['fcontents'];
6660 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']==1 or listcatfiles
['ffilelist'][reallcfi
]['ftype']==2):
6661 getflinkpath
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
6662 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
6663 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
6664 fheadersize
= format(int(flinkinfo
['fheadersize']), 'x').lower();
6665 fsize
= format(int(flinkinfo
['fsize']), 'x').lower();
6666 flinkname
= flinkinfo
['flinkname'];
6667 fatime
= format(int(flinkinfo
['fatime']), 'x').lower();
6668 fmtime
= format(int(flinkinfo
['fmtime']), 'x').lower();
6669 fctime
= format(int(flinkinfo
['fctime']), 'x').lower();
6670 fbtime
= format(int(flinkinfo
['fbtime']), 'x').lower();
6671 fmode
= format(int(flinkinfo
['fmode']), 'x').lower();
6672 fchmode
= format(int(flinkinfo
['fchmode']), 'x').lower();
6673 fuid
= format(int(flinkinfo
['fuid']), 'x').lower();
6674 funame
= flinkinfo
['funame'];
6675 fgid
= format(int(flinkinfo
['fgid']), 'x').lower();
6676 fgname
= flinkinfo
['fgname'];
6677 finode
= flinkinfo
['finode'];
6678 flinkcount
= flinkinfo
['flinkcount'];
6679 fwinattributes
= flinkinfo
['fwinattributes'];
6680 fdev_minor
= format(int(flinkinfo
['fminor']), 'x').lower();
6681 fdev_major
= format(int(flinkinfo
['fmajor']), 'x').lower();
6682 frdev_minor
= format(int(flinkinfo
['frminor']), 'x').lower();
6683 frdev_major
= format(int(flinkinfo
['frmajor']), 'x').lower();
6684 if(len(flinkinfo
['fextralist'])>flinkinfo
['fextrafields'] and len(flinkinfo
['fextralist'])>0):
6685 flinkinfo
['fextrafields'] = len(flinkinfo
['fextralist']);
6686 if(len(extradata
)>0):
6687 flinkinfo
['fextrafields'] = len(extradata
);
6688 flinkinfo
['fextralist'] = extradata
;
6689 extrafields
= format(int(flinkinfo
['fextrafields']), 'x').lower();
6690 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
6691 if(len(extradata
)>0):
6692 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
6693 extrasizelen
= format(len(extrasizestr
), 'x').lower();
6694 fcontents
= flinkinfo
['fcontents'];
6695 if(flinkinfo
['ftype']!=0 and flinkinfo
['ftype']!=7):
6697 fcontents
= fcontents
.encode('UTF-8');
6698 except AttributeError:
6700 ftypehex
= format(flinkinfo
['ftype'], 'x').lower();
6702 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=0 and listcatfiles
['ffilelist'][reallcfi
]['ftype']!=7):
6704 fcontents
= fcontents
.encode('UTF-8');
6705 except AttributeError:
6707 ftypehex
= format(listcatfiles
['ffilelist'][reallcfi
]['ftype'], 'x').lower();
6708 fcurfid
= format(curfid
, 'x').lower();
6709 if(not followlink
and finode
!=0):
6710 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=1):
6711 fcurinode
= format(int(curinode
), 'x').lower();
6712 inodetofile
.update({curinode
: fname
});
6713 filetoinode
.update({fname
: curinode
});
6714 curinode
= curinode
+ 1;
6716 fcurinode
= format(int(filetoinode
[flinkname
]), 'x').lower();
6718 fcurinode
= format(int(curinode
), 'x').lower();
6719 curinode
= curinode
+ 1;
6720 curfid
= curfid
+ 1;
6721 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
];
6722 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
6723 catoutlenhex
= format(catoutlen
, 'x').lower();
6724 catoutlist
.insert(0, catoutlenhex
);
6725 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
6726 if(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']>0):
6727 extrafieldslist
= [];
6729 exil
= listcatfiles
['ffilelist'][reallcfi
]['fextrafields'];
6731 extrafieldslist
.append(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
6733 catfileoutstr
+= AppendNullBytes([extrafieldslist
], formatspecs
[5]);
6734 catfileoutstr
+= AppendNullBytes([checksumtype
], formatspecs
[5]);
6735 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
6736 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
6737 if(checksumtype
=="none" or checksumtype
==""):
6738 catfileheadercshex
= format(0, 'x').lower();
6739 catfilecontentcshex
= format(0, 'x').lower();
6740 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
6741 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
6742 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
6743 elif(checksumtype
=="crc16_ccitt"):
6744 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
6745 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
6746 elif(checksumtype
=="adler32"):
6747 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
6748 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
6749 elif(checksumtype
=="crc32"):
6750 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
6751 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
6752 elif(checksumtype
=="crc64_ecma"):
6753 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6754 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
6755 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
6756 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6757 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
6758 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
6759 checksumoutstr
= hashlib
.new(checksumtype
);
6760 checksumoutstr
.update("".encode('UTF-8'));
6761 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
6762 checksumoutstr
= hashlib
.new(checksumtype
);
6763 checksumoutstr
.update(fcontents
);
6764 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
6766 catfileheadercshex
= format(0, 'x').lower();
6767 catfilecontentcshex
= format(0, 'x').lower();
6768 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
6769 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
6770 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
6771 if(checksumtype
=="none" or checksumtype
==""):
6772 catfileheadercshex
= format(0, 'x').lower();
6773 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
6774 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
6775 elif(checksumtype
=="crc16_ccitt"):
6776 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
6777 elif(checksumtype
=="adler32"):
6778 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
6779 elif(checksumtype
=="crc32"):
6780 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
6781 elif(checksumtype
=="crc64_ecma"):
6782 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6783 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
6784 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6785 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
6786 checksumoutstr
= hashlib
.new(checksumtype
);
6787 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
6788 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
6790 catfileheadercshex
= format(0, 'x').lower();
6791 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
6792 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
6793 nullstrecd
= formatspecs
[5].encode('UTF-8');
6794 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
6795 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
6796 catfp
.write(catfileout
);
6799 os
.fsync(catfp
.fileno());
6800 except io
.UnsupportedOperation
:
6802 except AttributeError:
6805 reallcfi
= reallcfi
+ 1;
6807 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
6808 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
6809 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
6812 os
.fsync(catfp
.fileno());
6813 except io
.UnsupportedOperation
:
6815 except AttributeError:
6819 if(hasattr(sys
.stdout
, "buffer")):
6820 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
6822 shutil
.copyfileobj(catfp
, sys
.stdout
);
6823 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
6824 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
6826 upload_file_to_internet_file(catfp
, outfile
);
6834 create_alias_function("RePack", __file_format_name__
, "", RePackArchiveFile
);
6836 def RePackArchiveFileFromString(catstr
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6837 catfp
= BytesIO(catstr
);
6838 listcatfiles
= RePackArchiveFile(catfp
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
6839 return listcatfiles
;
6841 create_alias_function("RePack", __file_format_name__
, "FromString", RePackArchiveFileFromString
);
6843 def PackArchiveFileFromListDir(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, skipchecksum
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6844 outarray
= BytesIO();
6845 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
6846 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
6847 return listcatfiles
;
6849 create_alias_function("Pack", __file_format_name__
, "FromListDir", PackArchiveFileFromListDir
);
6851 def ArchiveFileArrayBase64Encode(infile
, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6853 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6854 if(isinstance(infile
, dict)):
6855 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6856 listcatfiles
= prelistcatfiles
['list'];
6858 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6859 infile
= RemoveWindowsPath(infile
);
6861 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6862 listcatfiles
= prelistcatfiles
['list'];
6864 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6865 if(not listcatfiles
):
6867 lenlist
= len(listcatfiles
['ffilelist']);
6868 fnumfiles
= int(listcatfiles
['fnumfiles']);
6870 lcfx
= int(listcatfiles
['fnumfiles']);
6871 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6872 lcfx
= int(lenlist
);
6874 lcfx
= int(listcatfiles
['fnumfiles']);
6876 if(listcatfiles
['ffilelist'][lcfi
]['fhascontents']):
6877 listcatfiles
['ffilelist'][lcfi
]['fcontents'] = base64
.b64encode(listcatfiles
['ffilelist'][lcfi
]['fcontents']).decode("UTF-8");
6879 return listcatfiles
;
6881 create_alias_function("", __file_format_name__
, "ArrayBase64Encode", ArchiveFileArrayBase64Encode
);
6883 def ArchiveFileArrayBase64Decode(infile
, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6885 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6886 if(isinstance(infile
, dict)):
6887 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6888 listcatfiles
= prelistcatfiles
['list'];
6890 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6891 infile
= RemoveWindowsPath(infile
);
6893 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6894 listcatfiles
= prelistcatfiles
['list'];
6896 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6897 if(not listcatfiles
):
6899 lenlist
= len(listcatfiles
['ffilelist']);
6900 fnumfiles
= int(listcatfiles
['fnumfiles']);
6902 lcfx
= int(listcatfiles
['fnumfiles']);
6903 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6904 lcfx
= int(lenlist
);
6906 lcfx
= int(listcatfiles
['fnumfiles']);
6908 if(listcatfiles
['ffilelist'][lcfi
]['fhascontents']):
6909 listcatfiles
['ffilelist'][lcfi
]['fcontents'] = base64
.b64decode(listcatfiles
['ffilelist'][lcfi
]['fcontents'].encode("UTF-8"));
6911 return listcatfiles
;
6913 create_alias_function("", __file_format_name__
, "ArrayBase64Decode", ArchiveFileArrayBase64Decode
);
6915 def UnPackArchiveFile(infile
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, preservepermissions
=True, preservetime
=True, verbose
=False, returnfp
=False):
6916 if(outdir
is not None):
6917 outdir
= RemoveWindowsPath(outdir
);
6919 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6920 if(isinstance(infile
, dict)):
6921 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6922 listcatfiles
= prelistcatfiles
['list'];
6924 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6925 infile
= RemoveWindowsPath(infile
);
6927 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6928 listcatfiles
= prelistcatfiles
['list'];
6930 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6931 if(not listcatfiles
):
6933 lenlist
= len(listcatfiles
['ffilelist']);
6934 fnumfiles
= int(listcatfiles
['fnumfiles']);
6936 lcfx
= int(listcatfiles
['fnumfiles']);
6937 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6938 lcfx
= int(lenlist
);
6940 lcfx
= int(listcatfiles
['fnumfiles']);
6946 userinfo
= pwd
.getpwuid(listcatfiles
['ffilelist'][lcfi
]['fuid']);
6947 funame
= userinfo
.pw_name
;
6956 groupinfo
= grp
.getgrgid(listcatfiles
['ffilelist'][lcfi
]['fgid']);
6957 fgname
= groupinfo
.gr_name
;
6963 VerbosePrintOut(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6964 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6965 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
6966 fpc
.write(listcatfiles
['ffilelist'][lcfi
]['fcontents'])
6969 os
.fsync(fpc
.fileno())
6970 except io
.UnsupportedOperation
:
6972 except AttributeError:
6974 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
6975 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
6976 if(preservepermissions
):
6977 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6979 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
6980 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6982 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6983 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
6984 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
6989 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
6990 funame
= userinfo
.pw_name
;
6999 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
7000 fgname
= groupinfo
.gr_name
;
7005 if(flinkinfo
['ftype']==0 or flinkinfo
['ftype']==7):
7006 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
7007 fpc
.write(flinkinfo
['fcontents'])
7010 os
.fsync(fpc
.fileno())
7011 except io
.UnsupportedOperation
:
7013 except AttributeError:
7015 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
7016 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
7017 if(preservepermissions
):
7018 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
7020 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
7021 if(flinkinfo
['ftype']==1):
7022 os
.link(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
7023 if(flinkinfo
['ftype']==2):
7024 os
.symlink(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
7025 if(flinkinfo
['ftype']==5):
7026 if(preservepermissions
):
7027 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
7029 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
7030 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
7031 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
7032 if(preservepermissions
):
7033 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
7035 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
7036 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
7037 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
7039 os
.link(listcatfiles
['ffilelist'][lcfi
]['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
7040 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
7042 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
7043 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
7044 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
7049 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
7050 funame
= userinfo
.pw_name
;
7059 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
7060 fgname
= groupinfo
.gr_name
;
7065 if(flinkinfo
['ftype']==0 or flinkinfo
['ftype']==7):
7066 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
7067 fpc
.write(flinkinfo
['fcontents'])
7070 os
.fsync(fpc
.fileno())
7071 except io
.UnsupportedOperation
:
7073 except AttributeError:
7075 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
7076 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
7077 if(preservepermissions
):
7078 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
7080 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
7081 if(flinkinfo
['ftype']==1):
7082 os
.link(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
7083 if(flinkinfo
['ftype']==2):
7084 os
.symlink(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
7085 if(flinkinfo
['ftype']==5):
7086 if(preservepermissions
):
7087 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
7089 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
7090 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
7091 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
7092 if(preservepermissions
):
7093 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
7095 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
7096 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
7097 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
7099 os
.symlink(listcatfiles
['ffilelist'][lcfi
]['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
7100 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
7101 if(preservepermissions
):
7102 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
7104 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
7105 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
7106 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
7107 if(preservepermissions
):
7108 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
7110 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
7111 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6 and hasattr(os
, "mkfifo")):
7112 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
7115 return listcatfiles
['ffilelist']['catfp'];
7119 create_alias_function("UnPack", __file_format_name__
, "", UnPackArchiveFile
);
7121 if(hasattr(shutil
, "register_unpack_format")):
7122 def UnPackArchiveFileFunc(archive_name
, extract_dir
=None, **kwargs
):
7123 return UnPackArchiveFile(archive_name
, extract_dir
, False, 0, 0, False, __file_format_delimiter__
, False, False);
7124 create_alias_function("UnPack", __file_format_name__
, "Func", UnPackArchiveFileFunc
);
7126 def UnPackArchiveFileString(catstr
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7127 catfp
= BytesIO(catstr
);
7128 listcatfiles
= UnPackArchiveFile(catfp
, outdir
, followlink
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
7129 return listcatfiles
;
7131 create_alias_function("UnPack", __file_format_name__
, "String", UnPackArchiveFileString
);
7133 def ArchiveFileListFiles(infile
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7134 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7135 if(isinstance(infile
, dict)):
7136 listcatfiles
= infile
;
7138 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
7139 infile
= RemoveWindowsPath(infile
);
7140 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, True, skipchecksum
, formatspecs
, returnfp
);
7141 if(not listcatfiles
):
7143 lenlist
= len(listcatfiles
['ffilelist']);
7144 fnumfiles
= int(listcatfiles
['fnumfiles']);
7146 lcfx
= int(listcatfiles
['fnumfiles']);
7147 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
7148 lcfx
= int(lenlist
);
7150 lcfx
= int(listcatfiles
['fnumfiles']);
7153 returnval
.update({lcfi
: listcatfiles
['ffilelist'][lcfi
]['fname']});
7155 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
7157 permissions
= { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
7158 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'];
7159 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
7160 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " link to " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
7161 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
7162 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " -> " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
7163 fuprint
= listcatfiles
['ffilelist'][lcfi
]['funame'];
7164 if(len(fuprint
)<=0):
7165 fuprint
= listcatfiles
['ffilelist'][lcfi
]['fuid'];
7166 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgname'];
7167 if(len(fgprint
)<=0):
7168 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgid'];
7169 VerbosePrintOut(PrintPermissionString(listcatfiles
['ffilelist'][lcfi
]['fmode'], listcatfiles
['ffilelist'][lcfi
]['ftype']) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(listcatfiles
['ffilelist'][lcfi
]['fsize']).rjust(15) + " " + datetime
.datetime
.utcfromtimestamp(listcatfiles
['ffilelist'][lcfi
]['fmtime']).strftime('%Y-%m-%d %H:%M') + " " + printfname
));
7172 return listcatfiles
['catfp'];
7176 create_alias_function("", __file_format_name__
, "ListFiles", ArchiveFileListFiles
);
7178 def ArchiveFileStringListFiles(catstr
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7179 catfp
= BytesIO(catstr
);
7180 listcatfiles
= ArchiveFileListFiles(catstr
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
7181 return listcatfiles
;
7183 create_alias_function("", __file_format_name__
, "StringListFiles", ArchiveFileListFiles
);
7185 def ArchiveFileListFilesAlt(infile
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7186 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7187 if(isinstance(infile
, dict)):
7188 listcatfiles
= infile
;
7190 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
7191 infile
= RemoveWindowsPath(infile
);
7192 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, True, skipchecksum
, formatspecs
, returnfp
);
7193 if(not listcatfiles
):
7195 lenlist
= len(listcatfiles
['ffilelist']);
7196 fnumfiles
= int(listcatfiles
['fnumfiles']);
7198 lcfx
= int(listcatfiles
['fnumfiles']);
7199 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
7200 lcfx
= int(lenlist
);
7202 lcfx
= int(listcatfiles
['fnumfiles']);
7205 returnval
.update({lcfi
: listcatfiles
['ffilelist'][lcfi
]['fname']});
7207 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
7209 permissions
= { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
7210 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'];
7211 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
7212 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " link to " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
7213 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
7214 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " -> " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
7215 fuprint
= listcatfiles
['ffilelist'][lcfi
]['funame'];
7216 if(len(fuprint
)<=0):
7217 fuprint
= listcatfiles
['ffilelist'][lcfi
]['fuid'];
7218 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgname'];
7219 if(len(fgprint
)<=0):
7220 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgid'];
7221 VerbosePrintOut(PrintPermissionString(listcatfiles
['ffilelist'][lcfi
]['fmode'], listcatfiles
['ffilelist'][lcfi
]['ftype']) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(listcatfiles
['ffilelist'][lcfi
]['fsize']).rjust(15) + " " + datetime
.datetime
.utcfromtimestamp(listcatfiles
['ffilelist'][lcfi
]['fmtime']).strftime('%Y-%m-%d %H:%M') + " " + printfname
));
7224 return listcatfiles
['catfp'];
7228 create_alias_function("", __file_format_name__
, "ListFilesAlt", ArchiveFileListFilesAlt
);
7230 def ArchiveFileStringListFilesAlt(catstr
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7231 catfp
= BytesIO(catstr
);
7232 listcatfiles
= ArchiveFileListFilesAlt(catstr
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
7233 return listcatfiles
;
7235 create_alias_function("", __file_format_name__
, "StringListFilesAlt", ArchiveFileListFilesAlt
);
7237 def TarFileListFiles(infile
, verbose
=False, returnfp
=False):
7238 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7241 if(hasattr(sys
.stdin
, "buffer")):
7242 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
7244 shutil
.copyfileobj(sys
.stdin
, infile
);
7249 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
7250 infile
= download_file_from_internet_file(infile
);
7255 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
7257 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
7259 if(not tarfile
.is_tarfile(infile
)):
7261 except AttributeError:
7262 if(not is_tarfile(infile
)):
7267 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
7268 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
7270 tarfp
= tarfile
.open(infile
, "r");
7271 except FileNotFoundError
:
7275 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
7276 returnval
.update({lcfi
: member
.name
});
7277 fpremode
= member
.mode
;
7278 ffullmode
= member
.mode
;
7282 ffullmode
= member
.mode
+ stat
.S_IFREG
;
7284 elif(member
.isdev()):
7285 ffullmode
= member
.mode
;
7287 elif(member
.islnk()):
7288 ffullmode
= member
.mode
+ stat
.S_IFREG
;
7290 elif(member
.issym()):
7291 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
7293 elif(member
.ischr()):
7294 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
7296 elif(member
.isblk()):
7297 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
7299 elif(member
.isdir()):
7300 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
7302 elif(member
.isfifo()):
7303 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
7305 elif(member
.issparse()):
7306 ffullmode
= member
.mode
;
7309 VerbosePrintOut(member
.name
);
7311 permissions
= { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
7312 printfname
= member
.name
;
7314 printfname
= member
.name
+ " link to " + member
.linkname
;
7315 elif(member
.issym()):
7316 printfname
= member
.name
+ " -> " + member
.linkname
;
7317 fuprint
= member
.uname
;
7318 if(len(fuprint
)<=0):
7319 fuprint
= member
.uid
;
7320 fgprint
= member
.gname
;
7321 if(len(fgprint
)<=0):
7322 fgprint
= member
.gid
;
7323 VerbosePrintOut(PrintPermissionString(ffullmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(member
.size
).rjust(15) + " " + datetime
.datetime
.utcfromtimestamp(member
.mtime
).strftime('%Y-%m-%d %H:%M') + " " + printfname
));
7326 return listcatfiles
['catfp'];
7330 def ZipFileListFiles(infile
, verbose
=False, returnfp
=False):
7331 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7334 if(hasattr(sys
.stdin
, "buffer")):
7335 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
7337 shutil
.copyfileobj(sys
.stdin
, infile
);
7342 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
7343 infile
= download_file_from_internet_file(infile
);
7348 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
7352 if(not zipfile
.is_zipfile(infile
)):
7355 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
7356 except FileNotFoundError
:
7360 ziptest
= zipfp
.testzip();
7362 VerbosePrintOut("Bad file found!");
7363 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
7364 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
7365 fwinattributes
= int(zipinfo
.external_attr
);
7366 if(not member
.is_dir()):
7367 fmode
= int(stat
.S_IFREG
+ 438);
7368 fchmode
= int(stat
.S_IMODE(fmode
));
7369 ftypemod
= int(stat
.S_IFMT(fmode
));
7370 elif(member
.is_dir()):
7371 fmode
= int(stat
.S_IFDIR
+ 511);
7372 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
7373 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
7374 elif(zipinfo
.create_system
==3):
7375 fwinattributes
=int(0);
7376 fmode
= int(zipinfo
.external_attr
);
7377 fchmode
= int(stat
.S_IMODE(fmode
));
7378 ftypemod
= int(stat
.S_IFMT(fmode
));
7380 fwinattributes
= int(0);
7381 if(not member
.is_dir()):
7382 fmode
= int(stat
.S_IFREG
+ 438);
7383 fchmode
= int(stat
.S_IMODE(fmode
));
7384 ftypemod
= int(stat
.S_IFMT(fmode
));
7385 elif(member
.is_dir()):
7386 fmode
= int(stat
.S_IFDIR
+ 511);
7387 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
7388 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
7389 returnval
.update({lcfi
: member
.filename
});
7391 VerbosePrintOut(member
.filename
);
7393 permissions
= { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
7395 for fmodval
in str(oct(fmode
))[-3:]:
7396 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
7397 if(not member
.is_dir()):
7399 permissionstr
= "-" + permissionstr
;
7400 elif(member
.is_dir()):
7402 permissionstr
= "d" + permissionstr
;
7403 printfname
= member
.filename
;
7405 fuid
= int(os
.getuid());
7406 except AttributeError:
7411 fgid
= int(os
.getgid());
7412 except AttributeError:
7419 userinfo
= pwd
.getpwuid(os
.getuid());
7420 funame
= userinfo
.pw_name
;
7423 except AttributeError:
7431 groupinfo
= grp
.getgrgid(os
.getgid());
7432 fgname
= groupinfo
.gr_name
;
7435 except AttributeError:
7440 if(len(fuprint
)<=0):
7441 fuprint
= str(fuid
);
7443 if(len(fgprint
)<=0):
7444 fgprint
= str(fgid
);
7445 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(member
.file_size
).rjust(15) + " " + datetime
.datetime
.utcfromtimestamp(int(time
.mktime(member
.date_time
+ (0, 0, -1)))).strftime('%Y-%m-%d %H:%M') + " " + printfname
));
7448 return listcatfiles
['catfp'];
7452 if(not rarfile_support
):
7453 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
7454 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7455 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
7458 if(rarfile_support
):
7459 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
7460 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7461 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
7463 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
7467 rarfp
= rarfile
.RarFile(infile
, "r");
7468 rartest
= rarfp
.testrar();
7470 VerbosePrintOut("Bad file found!");
7471 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
7474 if(member
.host_os
==rarfile
.RAR_OS_UNIX
):
7477 member
.external_attr
7479 except AttributeError:
7481 elif(member
.host_os
==rarfile
.RAR_OS_WIN32
):
7484 member
.external_attr
7486 except AttributeError:
7491 if(is_unix
and member
.external_attr
!=0):
7492 fpremode
= int(member
.external_attr
);
7493 elif(member
.is_file()):
7494 fpremode
= int(stat
.S_IFREG
+ 438);
7495 elif(member
.is_symlink()):
7496 fpremode
= int(stat
.S_IFLNK
+ 438);
7497 elif(member
.is_dir()):
7498 fpremode
= int(stat
.S_IFDIR
+ 511);
7499 if(is_windows
and member
.external_attr
!=0):
7500 fwinattributes
= int(member
.external_attr
);
7502 fwinattributes
= int(0);
7503 if(is_unix
and member
.external_attr
!=0):
7504 fmode
= int(member
.external_attr
);
7505 fchmode
= int(stat
.S_IMODE(member
.external_attr
));
7506 ftypemod
= int(stat
.S_IFMT(member
.external_attr
));
7507 elif(member
.is_file()):
7508 fmode
= int(stat
.S_IFREG
+ 438);
7509 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
7510 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
7511 elif(member
.is_symlink()):
7512 fmode
= int(stat
.S_IFLNK
+ 438);
7513 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFLNK
+ 438)));
7514 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFLNK
+ 438)));
7515 elif(member
.is_dir()):
7516 fmode
= int(stat
.S_IFDIR
+ 511);
7517 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
7518 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
7519 returnval
.update({lcfi
: member
.filename
});
7521 VerbosePrintOut(member
.filename
);
7523 permissions
= { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
7525 for fmodval
in str(oct(fmode
))[-3:]:
7526 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
7527 if(member
.is_file()):
7529 permissionstr
= "-" + permissionstr
;
7530 printfname
= member
.filename
;
7531 elif(member
.is_symlink()):
7533 permissionstr
= "l" + permissionstr
;
7534 printfname
= member
.name
+ " -> " + member
.read().decode("UTF-8");
7535 elif(member
.is_dir()):
7537 permissionstr
= "d" + permissionstr
;
7538 printfname
= member
.filename
;
7540 fuid
= int(os
.getuid());
7541 except AttributeError:
7546 fgid
= int(os
.getgid());
7547 except AttributeError:
7554 userinfo
= pwd
.getpwuid(os
.getuid());
7555 funame
= userinfo
.pw_name
;
7558 except AttributeError:
7566 groupinfo
= grp
.getgrgid(os
.getgid());
7567 fgname
= groupinfo
.gr_name
;
7570 except AttributeError:
7575 if(len(fuprint
)<=0):
7576 fuprint
= str(fuid
);
7578 if(len(fgprint
)<=0):
7579 fgprint
= str(fgid
);
7580 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(member
.file_size
).rjust(15) + " " + member
.mtime
.strftime('%Y-%m-%d %H:%M') + " " + printfname
));
7583 return listcatfiles
['catfp'];
7587 if(not py7zr_support
):
7588 def SevenZipFileListFiles(infile
, verbose
=False, returnfp
=False):
7589 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7590 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
7594 def SevenZipFileListFiles(infile
, verbose
=False, returnfp
=False):
7595 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7596 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
7600 szpfp
= py7zr
.SevenZipFile(infile
, mode
="r");
7601 file_content
= szpfp
.readall();
7602 #sztest = szpfp.testzip();
7603 sztestalt
= szpfp
.test();
7605 VerbosePrintOut("Bad file found!");
7606 for member
in sorted(szpfp
.list(), key
=lambda x
: x
.filename
):
7607 if(re
.findall("^[.|/]", member
.filename
)):
7608 fname
= member
.filename
;
7610 fname
= "./"+member
.filename
;
7611 if(not member
.is_directory
):
7612 fpremode
= int(stat
.S_IFREG
+ 438);
7613 elif(member
.is_directory
):
7614 fpremode
= int(stat
.S_IFDIR
+ 511);
7615 fwinattributes
= int(0);
7616 if(member
.is_directory
):
7617 fmode
= int(stat
.S_IFDIR
+ 511);
7618 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
7619 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
7621 fmode
= int(stat
.S_IFLNK
+ 438);
7622 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFLNK
+ 438)));
7623 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFLNK
+ 438)));
7624 returnval
.update({lcfi
: member
.filename
});
7626 VerbosePrintOut(member
.filename
);
7628 permissions
= { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
7630 for fmodval
in str(oct(fmode
))[-3:]:
7631 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
7633 if(not member
.is_directory
):
7635 permissionstr
= "-" + permissionstr
;
7636 printfname
= member
.filename
;
7637 elif(member
.is_directory
):
7639 permissionstr
= "d" + permissionstr
;
7640 printfname
= member
.filename
;
7642 fsize
= len(file_content
[member
.filename
].read());
7643 file_content
[member
.filename
].close();
7645 fuid
= int(os
.getuid());
7646 except AttributeError:
7651 fgid
= int(os
.getgid());
7652 except AttributeError:
7659 userinfo
= pwd
.getpwuid(os
.getuid());
7660 funame
= userinfo
.pw_name
;
7663 except AttributeError:
7671 groupinfo
= grp
.getgrgid(os
.getgid());
7672 fgname
= groupinfo
.gr_name
;
7675 except AttributeError:
7680 if(len(fuprint
)<=0):
7681 fuprint
= str(fuid
);
7683 if(len(fgprint
)<=0):
7684 fgprint
= str(fgid
);
7685 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(fsize
).rjust(15) + " " + member
.creationtime
.strftime('%Y-%m-%d %H:%M') + " " + printfname
));
7688 return listcatfiles
['catfp'];
7692 def ListDirListFiles(infiles
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7693 outarray
= BytesIO();
7694 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, False, True);
7695 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
7696 return listcatfiles
;
7698 def ListDirListFilesAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=True, seekstart
=0, seekend
=0, skipchecksum
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7699 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, formatspecs
, verbose
);
7700 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
7701 return listcatfiles
;
7703 def PackArchiveFileFromListDirAlt(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, skipchecksum
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7704 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, False, checksumtype
, extradata
, formatspecs
, False);
7705 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, followlink
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
7706 return listcatfiles
;
7708 create_alias_function("Pack", __file_format_name__
, "FromListDirAlt", PackArchiveFileFromListDirAlt
);
7710 def PackArchiveFileFromTarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7711 outarray
= TarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
7712 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
7713 return listcatfiles
;
7715 create_alias_function("Pack", __file_format_name__
, "FromTarFileAlt", PackArchiveFileFromTarFileAlt
);
7717 def PackArchiveFileFromZipFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7718 outarray
= ZipFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
7719 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
7720 return listcatfiles
;
7722 create_alias_function("Pack", __file_format_name__
, "FromZipFileAlt", PackArchiveFileFromZipFileAlt
);
7724 if(not rarfile_support
):
7725 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7728 if(rarfile_support
):
7729 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7730 outarray
= RarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
7731 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
7732 return listcatfiles
;
7734 create_alias_function("Pack", __file_format_name__
, "FromRarFileAlt", PackArchiveFileFromRarFileAlt
);
7736 def download_file_from_ftp_file(url
):
7737 urlparts
= urlparse(url
);
7738 file_name
= os
.path
.basename(urlparts
.path
);
7739 file_dir
= os
.path
.dirname(urlparts
.path
);
7740 if(urlparts
.username
is not None):
7741 ftp_username
= urlparts
.username
;
7743 ftp_username
= "anonymous";
7744 if(urlparts
.password
is not None):
7745 ftp_password
= urlparts
.password
;
7746 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7747 ftp_password
= "anonymous";
7750 if(urlparts
.scheme
=="ftp"):
7752 elif(urlparts
.scheme
=="ftps"):
7756 if(urlparts
.scheme
=="sftp"):
7758 return download_file_from_pysftp_file(url
);
7760 return download_file_from_sftp_file(url
);
7761 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7762 return download_file_from_http_file(url
);
7763 ftp_port
= urlparts
.port
;
7764 if(urlparts
.port
is None):
7767 ftp
.connect(urlparts
.hostname
, ftp_port
);
7768 except socket
.gaierror
:
7769 log
.info("Error With URL "+url
);
7771 except socket
.timeout
:
7772 log
.info("Error With URL "+url
);
7774 ftp
.login(urlparts
.username
, urlparts
.password
);
7775 if(urlparts
.scheme
=="ftps"):
7777 ftpfile
= BytesIO();
7778 ftp
.retrbinary("RETR "+urlparts
.path
, ftpfile
.write
);
7779 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
7784 def download_file_from_ftp_string(url
):
7785 ftpfile
= download_file_from_ftp_file(url
);
7786 return ftpfile
.read();
7788 def upload_file_to_ftp_file(ftpfile
, url
):
7789 urlparts
= urlparse(url
);
7790 file_name
= os
.path
.basename(urlparts
.path
);
7791 file_dir
= os
.path
.dirname(urlparts
.path
);
7792 if(urlparts
.username
is not None):
7793 ftp_username
= urlparts
.username
;
7795 ftp_username
= "anonymous";
7796 if(urlparts
.password
is not None):
7797 ftp_password
= urlparts
.password
;
7798 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7799 ftp_password
= "anonymous";
7802 if(urlparts
.scheme
=="ftp"):
7804 elif(urlparts
.scheme
=="ftps"):
7808 if(urlparts
.scheme
=="sftp"):
7810 return upload_file_from_pysftp_file(url
);
7812 return upload_file_from_sftp_file(url
);
7813 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7815 ftp_port
= urlparts
.port
;
7816 if(urlparts
.port
is None):
7819 ftp
.connect(urlparts
.hostname
, ftp_port
);
7820 except socket
.gaierror
:
7821 log
.info("Error With URL "+url
);
7823 except socket
.timeout
:
7824 log
.info("Error With URL "+url
);
7826 ftp
.login(urlparts
.username
, urlparts
.password
);
7827 if(urlparts
.scheme
=="ftps"):
7829 ftp
.storbinary("STOR "+urlparts
.path
, ftpfile
);
7834 def upload_file_to_ftp_string(ftpstring
, url
):
7835 ftpfileo
= BytesIO(ftpstring
);
7836 ftpfile
= upload_file_to_ftp_file(ftpfileo
, url
);
7840 def download_file_from_http_file(url
, headers
=geturls_headers_pycatfile_python_alt
):
7841 # Parse the URL to extract username and password if present
7842 urlparts
= urlparse(url
);
7843 username
= urlparts
.username
;
7844 password
= urlparts
.password
;
7845 # Rebuild the URL without the username and password
7846 netloc
= urlparts
.hostname
;
7847 if(urlparts
.scheme
=="sftp"):
7849 return download_file_from_pysftp_file(url
);
7851 return download_file_from_sftp_file(url
);
7852 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7853 return download_file_from_ftp_file(url
);
7855 netloc
+= ':' + str(urlparts
.port
);
7856 rebuilt_url
= urlunparse((urlparts
.scheme
, netloc
, urlparts
.path
, urlparts
.params
, urlparts
.query
, urlparts
.fragment
));
7857 # Create a temporary file object
7858 httpfile
= BytesIO();
7860 # Use the requests library if available
7861 if username
and password
:
7862 response
= requests
.get(rebuilt_url
, headers
=headers
, auth
=(username
, password
), stream
=True);
7864 response
= requests
.get(rebuilt_url
, headers
=headers
, stream
=True);
7865 response
.raw
.decode_content
= True
7866 shutil
.copyfileobj(response
.raw
, httpfile
);
7868 # Build a Request object for urllib
7869 request
= Request(rebuilt_url
, headers
=headers
);
7870 # Create an opener object for handling URLs
7871 if username
and password
:
7872 # Create a password manager
7873 password_mgr
= HTTPPasswordMgrWithDefaultRealm();
7874 # Add the username and password
7875 password_mgr
.add_password(None, rebuilt_url
, username
, password
);
7876 # Create an authentication handler using the password manager
7877 auth_handler
= HTTPBasicAuthHandler(password_mgr
);
7878 # Build the opener with the authentication handler
7879 opener
= build_opener(auth_handler
);
7881 opener
= build_opener();
7882 with opener
.open(request
) as response
:
7883 shutil
.copyfileobj(response
, httpfile
);
7884 # Reset file pointer to the start
7885 httpfile
.seek(0, 0);
7886 # Return the temporary file object
7889 def download_file_from_http_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
7890 httpfile
= download_file_from_http_file(url
, headers
);
7891 return ftpfile
.read();
7894 def download_file_from_sftp_file(url
):
7895 urlparts
= urlparse(url
);
7896 file_name
= os
.path
.basename(urlparts
.path
);
7897 file_dir
= os
.path
.dirname(urlparts
.path
);
7898 sftp_port
= urlparts
.port
;
7899 if(urlparts
.port
is None):
7902 sftp_port
= urlparts
.port
;
7903 if(urlparts
.username
is not None):
7904 sftp_username
= urlparts
.username
;
7906 sftp_username
= "anonymous";
7907 if(urlparts
.password
is not None):
7908 sftp_password
= urlparts
.password
;
7909 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7910 sftp_password
= "anonymous";
7913 if(urlparts
.scheme
=="ftp"):
7914 return download_file_from_ftp_file(url
);
7915 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7916 return download_file_from_http_file(url
);
7917 if(urlparts
.scheme
!="sftp"):
7919 ssh
= paramiko
.SSHClient();
7920 ssh
.load_system_host_keys();
7921 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
7923 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7924 except paramiko
.ssh_exception
.SSHException
:
7926 except socket
.gaierror
:
7927 log
.info("Error With URL "+url
);
7929 except socket
.timeout
:
7930 log
.info("Error With URL "+url
);
7932 sftp
= ssh
.open_sftp();
7933 sftpfile
= BytesIO();
7934 sftp
.getfo(urlparts
.path
, sftpfile
);
7937 sftpfile
.seek(0, 0);
7940 def download_file_from_sftp_file(url
):
7944 def download_file_from_sftp_string(url
):
7945 sftpfile
= download_file_from_sftp_file(url
);
7946 return sftpfile
.read();
7948 def download_file_from_ftp_string(url
):
7952 def upload_file_to_sftp_file(sftpfile
, url
):
7953 urlparts
= urlparse(url
);
7954 file_name
= os
.path
.basename(urlparts
.path
);
7955 file_dir
= os
.path
.dirname(urlparts
.path
);
7956 sftp_port
= urlparts
.port
;
7957 if(urlparts
.port
is None):
7960 sftp_port
= urlparts
.port
;
7961 if(urlparts
.username
is not None):
7962 sftp_username
= urlparts
.username
;
7964 sftp_username
= "anonymous";
7965 if(urlparts
.password
is not None):
7966 sftp_password
= urlparts
.password
;
7967 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7968 sftp_password
= "anonymous";
7971 if(urlparts
.scheme
=="ftp"):
7972 return upload_file_from_ftp_file(url
);
7973 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7975 if(urlparts
.scheme
!="sftp"):
7977 ssh
= paramiko
.SSHClient();
7978 ssh
.load_system_host_keys();
7979 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
7981 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7982 except paramiko
.ssh_exception
.SSHException
:
7984 except socket
.gaierror
:
7985 log
.info("Error With URL "+url
);
7987 except socket
.timeout
:
7988 log
.info("Error With URL "+url
);
7990 sftp
= ssh
.open_sftp();
7991 sftp
.putfo(sftpfile
, urlparts
.path
);
7994 sftpfile
.seek(0, 0);
7997 def upload_file_to_sftp_file(sftpfile
, url
):
8001 def upload_file_to_sftp_string(sftpstring
, url
):
8002 sftpfileo
= BytesIO(sftpstring
);
8003 sftpfile
= upload_file_to_sftp_files(ftpfileo
, url
);
8007 def upload_file_to_sftp_string(url
):
8011 def download_file_from_pysftp_file(url
):
8012 urlparts
= urlparse(url
);
8013 file_name
= os
.path
.basename(urlparts
.path
);
8014 file_dir
= os
.path
.dirname(urlparts
.path
);
8015 sftp_port
= urlparts
.port
;
8016 if(urlparts
.port
is None):
8019 sftp_port
= urlparts
.port
;
8020 if(urlparts
.username
is not None):
8021 sftp_username
= urlparts
.username
;
8023 sftp_username
= "anonymous";
8024 if(urlparts
.password
is not None):
8025 sftp_password
= urlparts
.password
;
8026 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
8027 sftp_password
= "anonymous";
8030 if(urlparts
.scheme
=="ftp"):
8031 return download_file_from_ftp_file(url
);
8032 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
8033 return download_file_from_http_file(url
);
8034 if(urlparts
.scheme
!="sftp"):
8037 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
8038 except paramiko
.ssh_exception
.SSHException
:
8040 except socket
.gaierror
:
8041 log
.info("Error With URL "+url
);
8043 except socket
.timeout
:
8044 log
.info("Error With URL "+url
);
8046 sftp
= ssh
.open_sftp();
8047 sftpfile
= BytesIO();
8048 sftp
.getfo(urlparts
.path
, sftpfile
);
8051 sftpfile
.seek(0, 0);
8054 def download_file_from_pysftp_file(url
):
8058 def download_file_from_pysftp_string(url
):
8059 sftpfile
= download_file_from_pysftp_file(url
);
8060 return sftpfile
.read();
8062 def download_file_from_ftp_string(url
):
8066 def upload_file_to_pysftp_file(sftpfile
, url
):
8067 urlparts
= urlparse(url
);
8068 file_name
= os
.path
.basename(urlparts
.path
);
8069 file_dir
= os
.path
.dirname(urlparts
.path
);
8070 sftp_port
= urlparts
.port
;
8071 if(urlparts
.port
is None):
8074 sftp_port
= urlparts
.port
;
8075 if(urlparts
.username
is not None):
8076 sftp_username
= urlparts
.username
;
8078 sftp_username
= "anonymous";
8079 if(urlparts
.password
is not None):
8080 sftp_password
= urlparts
.password
;
8081 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
8082 sftp_password
= "anonymous";
8085 if(urlparts
.scheme
=="ftp"):
8086 return upload_file_from_ftp_file(url
);
8087 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
8089 if(urlparts
.scheme
!="sftp"):
8092 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
8093 except paramiko
.ssh_exception
.SSHException
:
8095 except socket
.gaierror
:
8096 log
.info("Error With URL "+url
);
8098 except socket
.timeout
:
8099 log
.info("Error With URL "+url
);
8101 sftp
= ssh
.open_sftp();
8102 sftp
.putfo(sftpfile
, urlparts
.path
);
8105 sftpfile
.seek(0, 0);
8108 def upload_file_to_pysftp_file(sftpfile
, url
):
8112 def upload_file_to_pysftp_string(sftpstring
, url
):
8113 sftpfileo
= BytesIO(sftpstring
);
8114 sftpfile
= upload_file_to_pysftp_files(ftpfileo
, url
);
8118 def upload_file_to_pysftp_string(url
):
8121 def download_file_from_internet_file(url
, headers
=geturls_headers_pycatfile_python_alt
):
8122 urlparts
= urlparse(url
);
8123 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
8124 return download_file_from_http_file(url
, headers
);
8125 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
8126 return download_file_from_ftp_file(url
);
8127 elif(urlparts
.scheme
=="sftp"):
8128 if(__use_pysftp__
and havepysftp
):
8129 return download_file_from_pysftp_file(url
);
8131 return download_file_from_sftp_file(url
);
8136 def download_file_from_internet_uncompress_file(url
, headers
=geturls_headers_pycatfile_python_alt
, formatspecs
=__file_format_list__
):
8137 fp
= download_file_from_internet_file(url
);
8138 fp
= UncompressArchiveFile(fp
, formatspecs
);
8144 def download_file_from_internet_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
8145 urlparts
= urlparse(url
);
8146 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
8147 return download_file_from_http_string(url
, headers
);
8148 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
8149 return download_file_from_ftp_string(url
);
8150 elif(urlparts
.scheme
=="sftp"):
8151 if(__use_pysftp__
and havepysftp
):
8152 return download_file_from_pysftp_string(url
);
8154 return download_file_from_sftp_string(url
);
8159 def download_file_from_internet_uncompress_string(url
, headers
=geturls_headers_pycatfile_python_alt
, formatspecs
=__file_format_list__
):
8160 fp
= download_file_from_internet_string(url
);
8161 fp
= UncompressArchiveFile(fp
, formatspecs
);
8167 def upload_file_to_internet_file(ifp
, url
):
8168 urlparts
= urlparse(url
);
8169 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
8171 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
8172 return upload_file_from_ftp_file(ifp
, url
);
8173 elif(urlparts
.scheme
=="sftp"):
8174 if(__use_pysftp__
and havepysftp
):
8175 return upload_file_from_pysftp_file(ifp
, url
);
8177 return download_file_from_sftp_file(ifp
, url
);
8182 def upload_file_to_internet_compress_file(ifp
, url
, formatspecs
=__file_format_list__
):
8183 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
8187 upload_file_to_internet_file(catfp
, outfile
);
8190 def upload_file_to_internet_string(ifp
, url
):
8191 urlparts
= urlparse(url
);
8192 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
8194 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
8195 return upload_file_from_ftp_string(ifp
, url
);
8196 elif(urlparts
.scheme
=="sftp"):
8197 if(__use_pysftp__
and havepysftp
):
8198 return upload_file_from_pysftp_string(ifp
, url
);
8200 return download_file_from_sftp_string(ifp
, url
);
8205 def upload_file_to_internet_compress_string(ifp
, url
, formatspecs
=__file_format_list__
):
8206 catfp
= CompressArchiveFile(BytesIO(ifp
), compression
, formatspecs
);
8210 upload_file_to_internet_file(catfp
, outfile
);
8214 if(hasattr(shutil
, "register_archive_format")):
8215 # Register the packing format
8216 shutil
.register_archive_format(__file_format_name__
, PackArchiveFileFunc
, description
='Pack concatenated files');
8217 except shutil
.RegistryError
:
8221 if(hasattr(shutil
, "register_unpack_format")):
8222 # Register the unpacking format
8223 shutil
.register_unpack_format(__file_format_name__
, archivefile_extensions
, UnPackArchiveFileFunc
, description
='UnPack concatenated files');
8224 except shutil
.RegistryError
: