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/26/2024 Ver. 0.8.6 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, 8, 6, "RC 1", 1);
175 __version_date_info__
= (2024, 4, 26, "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 PackArchiveFileFromInFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
3835 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
3837 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
3838 if(checkcompressfile
=="tarfile"):
3839 return PackArchiveFileFromTarFile(infile
, outfile
, compression
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
3840 elif(checkcompressfile
=="zipfile"):
3841 return PackArchiveFileFromZipFile(infile
, outfile
, compression
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
3842 elif(checkcompressfile
=="catfile"):
3843 return RePackArchiveFile(infile
, outfile
, compression
, compressionlevel
, False, 0, 0, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
3844 elif(rarfile_support
and checkcompressfile
=="rarfile"):
3845 return PackArchiveFileFromRarFile(infile
, outfile
, compression
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
3846 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
3847 return PackArchiveFileFromSevenZipFile(infile
, outfile
, compression
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
3852 create_alias_function("Pack", __file_format_name__
, "FromInFile", PackArchiveFileFromInFile
);
3854 def ArchiveFileSeekToFileNum(infile
, seekto
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3855 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3858 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3859 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
3860 if(checkcompressfile
=="tarfile"):
3861 return TarFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3862 if(checkcompressfile
=="zipfile"):
3863 return ZipFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3864 if(rarfile_support
and checkcompressfile
=="rarfile"):
3865 return RarFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3866 if(py7zr_support
and checkcompressfile
=="7zipfile"):
3867 return SevenZipFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3868 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3875 if(hasattr(sys
.stdin
, "buffer")):
3876 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
3878 shutil
.copyfileobj(sys
.stdin
, catfp
);
3880 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3884 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
3885 catfp
= download_file_from_internet_file(infile
);
3887 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3892 infile
= RemoveWindowsPath(infile
);
3893 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
3894 if(checkcompressfile
=="tarfile"):
3895 return TarFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3896 if(checkcompressfile
=="zipfile"):
3897 return ZipFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3898 if(rarfile_support
and checkcompressfile
=="rarfile"):
3899 return RarFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3900 if(py7zr_support
and checkcompressfile
=="7zipfile"):
3901 return SevenZipFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3902 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3904 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
3905 if(not compresscheck
):
3906 fextname
= os
.path
.splitext(infile
)[1];
3907 if(fextname
==".gz"):
3908 compresscheck
= "gzip";
3909 elif(fextname
==".bz2"):
3910 compresscheck
= "bzip2";
3911 elif(fextname
==".zst"):
3912 compresscheck
= "zstd";
3913 elif(fextname
==".lz4" or fextname
==".clz4"):
3914 compresscheck
= "lz4";
3915 elif(fextname
==".lzo" or fextname
==".lzop"):
3916 compresscheck
= "lzo";
3917 elif(fextname
==".lzma" or fextname
==".xz"):
3918 compresscheck
= "lzma";
3921 if(not compresscheck
):
3923 catfp
= UncompressFile(infile
, formatspecs
, "rb");
3928 SeekToEndOfFile(catfp);
3930 SeekToEndOfFile(catfp);
3931 CatSize = catfp.tell();
3932 CatSizeEnd = CatSize;
3940 curloc
= catfp
.tell();
3943 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
3945 catfp
.seek(curloc
, 0);
3946 catstring
= catheader
[0];
3947 catversion
= re
.findall(r
"([\d]+)$", catstring
);
3948 fprenumfiles
= catheader
[1];
3949 fnumfiles
= int(fprenumfiles
, 16);
3950 fprechecksumtype
= catheader
[2];
3951 fprechecksum
= catheader
[3];
3952 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
3953 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3954 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
3955 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
3956 catfileheadercshex
= format(0, 'x').lower();
3957 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
3958 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3959 elif(fprechecksumtype
=="crc16_ccitt"):
3960 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3961 elif(fprechecksumtype
=="adler32"):
3962 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3963 elif(fprechecksumtype
=="crc32"):
3964 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3965 elif(fprechecksumtype
=="crc64_ecma"):
3966 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3967 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
3968 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3969 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
3970 checksumoutstr
= hashlib
.new(fprechecksumtype
);
3971 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3972 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3974 catfileheadercshex
= format(0, 'x').lower();
3975 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3976 fheadtell
= len(fileheader
);
3977 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
3978 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
3980 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
3981 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
3982 if(seekto
>=fnumfiles
):
3983 seekto
= fnumfiles
- 1;
3989 prefhstart
= catfp
.tell();
3991 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3993 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
3994 if(len(preheaderdata
)==0):
3996 prefheadsize
= int(preheaderdata
[0], 16);
3997 prefnumfields
= int(preheaderdata
[1], 16);
3998 preftype
= int(preheaderdata
[2], 16);
3999 if(re
.findall("^[.|/]", preheaderdata
[3])):
4000 prefname
= preheaderdata
[3];
4002 prefname
= "./"+preheaderdata
[3];
4003 prefbasedir
= os
.path
.dirname(prefname
);
4004 preflinkname
= preheaderdata
[4];
4005 prefsize
= int(preheaderdata
[5], 16);
4006 prefatime
= int(preheaderdata
[6], 16);
4007 prefmtime
= int(preheaderdata
[7], 16);
4008 prefctime
= int(preheaderdata
[8], 16);
4009 prefbtime
= int(preheaderdata
[9], 16);
4010 prefmode
= int(preheaderdata
[10], 16);
4011 prefchmode
= stat
.S_IMODE(prefmode
);
4012 preftypemod
= stat
.S_IFMT(prefmode
);
4013 prefwinattributes
= int(preheaderdata
[11], 16);
4014 prefuid
= int(preheaderdata
[12], 16);
4015 prefuname
= preheaderdata
[13];
4016 prefgid
= int(preheaderdata
[14], 16);
4017 prefgname
= preheaderdata
[15];
4018 fid
= int(preheaderdata
[16], 16);
4019 finode
= int(preheaderdata
[17], 16);
4020 flinkcount
= int(preheaderdata
[18], 16);
4021 prefdev_minor
= int(preheaderdata
[19], 16);
4022 prefdev_major
= int(preheaderdata
[20], 16);
4023 prefrdev_minor
= int(preheaderdata
[22], 16);
4024 prefrdev_major
= int(preheaderdata
[23], 16);
4025 prefextrasize
= int(preheaderdata
[24], 16);
4026 prefextrafields
= int(preheaderdata
[25], 16);
4027 extrafieldslist
= [];
4029 extraend
= extrastart
+ prefextrafields
;
4030 extrafieldslist
= [];
4031 if(extrastart
<extraend
):
4032 extrafieldslist
.append(preheaderdata
[extrastart
]);
4033 extrastart
= extrastart
+ 1;
4034 prefchecksumtype
= preheaderdata
[extrastart
].lower();
4035 prefcs
= preheaderdata
[extrastart
+ 1].lower();
4036 prefccs
= preheaderdata
[extrastart
+ 2].lower();
4038 hcmax
= len(preheaderdata
) - 2;
4041 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
4043 prenewfcs
= GetFileChecksum(hout
, preheaderdata
[-3].lower(), True, formatspecs
);
4044 if(prefcs
!=prenewfcs
and not skipchecksum
):
4045 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
4047 valid_archive
= False;
4048 invalid_archive
= True;
4049 prefhend
= catfp
.tell() - 1;
4050 prefcontentstart
= catfp
.tell();
4052 pyhascontents
= False;
4054 prefcontents
= catfp
.read(prefsize
);
4055 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
4056 pyhascontents
= True;
4057 if(prefccs
!=prenewfccs
and not skipchecksum
):
4058 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
4062 catfp
.seek(seekstart
, 0);
4064 catfheadsize
= int(preheaderdata
[0], 16);
4065 catfnumfields
= int(preheaderdata
[1], 16);
4066 catftype
= int(preheaderdata
[2], 16);
4067 if(re
.findall("^[.|/]", preheaderdata
[3])):
4068 catfname
= preheaderdata
[3];
4070 catfname
= "./"+preheaderdata
[3];
4071 catflinkname
= preheaderdata
[4];
4072 catfsize
= int(preheaderdata
[5], 16);
4073 catfbasedir
= os
.path
.dirname(catfname
);
4074 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
4076 catlist
.update({'catfp': catfp
});
4081 create_alias_function("", __file_format_name__
, "SeekToFileNum", ArchiveFileSeekToFileNum
);
4083 def ArchiveFileSeekToFileName(infile
, seekfile
=None, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4084 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4087 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4088 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
4089 if(checkcompressfile
=="tarfile"):
4090 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4091 if(checkcompressfile
=="zipfile"):
4092 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4093 if(rarfile_support
and checkcompressfile
=="rarfile"):
4094 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4095 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4096 return SevenZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4097 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4104 if(hasattr(sys
.stdin
, "buffer")):
4105 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
4107 shutil
.copyfileobj(sys
.stdin
, catfp
);
4109 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4113 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4114 catfp
= download_file_from_internet_file(infile
);
4115 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4121 infile
= RemoveWindowsPath(infile
);
4122 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
4123 if(checkcompressfile
=="tarfile"):
4124 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4125 if(checkcompressfile
=="zipfile"):
4126 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4127 if(rarfile_support
and checkcompressfile
=="rarfile"):
4128 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4129 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4130 return SevenZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4131 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4133 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
4134 if(not compresscheck
):
4135 fextname
= os
.path
.splitext(infile
)[1];
4136 if(fextname
==".gz"):
4137 compresscheck
= "gzip";
4138 elif(fextname
==".bz2"):
4139 compresscheck
= "bzip2";
4140 elif(fextname
==".zst"):
4141 compresscheck
= "zstd";
4142 elif(fextname
==".lz4" or fextname
==".clz4"):
4143 compresscheck
= "lz4";
4144 elif(fextname
==".lzo" or fextname
==".lzop"):
4145 compresscheck
= "lzo";
4146 elif(fextname
==".lzma" or fextname
==".xz"):
4147 compresscheck
= "lzma";
4150 if(not compresscheck
):
4152 catfp
= UncompressFile(infile
, formatspecs
, "rb");
4157 SeekToEndOfFile(catfp);
4159 SeekToEndOfFile(catfp);
4160 CatSize = catfp.tell();
4161 CatSizeEnd = CatSize;
4169 curloc
= catfp
.tell();
4172 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
4174 catfp
.seek(curloc
, 0);
4175 catstring
= catheader
[0];
4176 catversion
= re
.findall(r
"([\d]+)$", catstring
);
4177 fprenumfiles
= catheader
[1];
4178 fnumfiles
= int(fprenumfiles
, 16);
4179 fprechecksumtype
= catheader
[2];
4180 fprechecksum
= catheader
[3];
4181 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
4182 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4183 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
4184 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
4185 catfileheadercshex
= format(0, 'x').lower();
4186 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
4187 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4188 elif(fprechecksumtype
=="crc16_ccitt"):
4189 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4190 elif(fprechecksumtype
=="adler32"):
4191 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4192 elif(fprechecksumtype
=="crc32"):
4193 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4194 elif(fprechecksumtype
=="crc64_ecma"):
4195 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4196 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
4197 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4198 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
4199 checksumoutstr
= hashlib
.new(fprechecksumtype
);
4200 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4201 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4203 catfileheadercshex
= format(0, 'x').lower();
4204 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4205 fheadtell
= len(fileheader
);
4206 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
4207 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
4209 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4210 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
4211 seekto
= fnumfiles
- 1
4216 prefhstart
= catfp
.tell();
4218 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
4220 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
4221 if(len(preheaderdata
)==0):
4223 prefheadsize
= int(preheaderdata
[0], 16);
4224 prefnumfields
= int(preheaderdata
[1], 16);
4225 preftype
= int(preheaderdata
[2], 16);
4226 if(re
.findall("^[.|/]", preheaderdata
[3])):
4227 prefname
= preheaderdata
[3];
4229 prefname
= "./"+preheaderdata
[3];
4230 prefbasedir
= os
.path
.dirname(prefname
);
4231 preflinkname
= preheaderdata
[4];
4232 prefsize
= int(preheaderdata
[5], 16);
4233 prefatime
= int(preheaderdata
[6], 16);
4234 prefmtime
= int(preheaderdata
[7], 16);
4235 prefctime
= int(preheaderdata
[8], 16);
4236 prefbtime
= int(preheaderdata
[9], 16);
4237 prefmode
= int(preheaderdata
[10], 16);
4238 prefchmode
= stat
.S_IMODE(prefmode
);
4239 preftypemod
= stat
.S_IFMT(prefmode
);
4240 prefwinattributes
= int(preheaderdata
[11], 16);
4241 prefuid
= int(preheaderdata
[12], 16);
4242 prefuname
= preheaderdata
[13];
4243 prefgid
= int(preheaderdata
[14], 16);
4244 prefgname
= preheaderdata
[15];
4245 fid
= int(preheaderdata
[16], 16);
4246 finode
= int(preheaderdata
[17], 16);
4247 flinkcount
= int(preheaderdata
[18], 16);
4248 prefdev_minor
= int(preheaderdata
[19], 16);
4249 prefdev_major
= int(preheaderdata
[20], 16);
4250 prefrdev_minor
= int(preheaderdata
[22], 16);
4251 prefrdev_major
= int(preheaderdata
[23], 16);
4252 prefextrasize
= int(preheaderdata
[24], 16);
4253 prefextrafields
= int(preheaderdata
[25], 16);
4254 extrafieldslist
= [];
4256 extraend
= extrastart
+ prefextrafields
;
4257 extrafieldslist
= [];
4258 if(extrastart
<extraend
):
4259 extrafieldslist
.append(preheaderdata
[extrastart
]);
4260 extrastart
= extrastart
+ 1;
4261 prefchecksumtype
= preheaderdata
[extrastart
].lower();
4262 prefcs
= preheaderdata
[extrastart
+ 1].lower();
4263 prefccs
= preheaderdata
[extrastart
+ 2].lower();
4265 hcmax
= len(preheaderdata
) - 2;
4268 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
4270 prenewfcs
= GetFileChecksum(hout
, preheaderdata
[-3].lower(), True, formatspecs
);
4271 if(prefcs
!=prenewfcs
and not skipchecksum
):
4272 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
4274 valid_archive
= False;
4275 invalid_archive
= True;
4276 prefhend
= catfp
.tell() - 1;
4277 prefcontentstart
= catfp
.tell();
4279 pyhascontents
= False;
4281 prefcontents
= catfp
.read(prefsize
);
4282 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
4283 pyhascontents
= True;
4284 if(prefccs
!=prenewfccs
and not skipchecksum
):
4285 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
4290 prefname
= preheaderdata
[2];
4291 if(re
.findall("^[.|/]", preheaderdata
[2])):
4292 prefname
= preheaderdata
[2];
4294 prefname
= "./"+preheaderdata
[2];
4295 if(prefname
==seekfile
):
4298 catfp
.seek(seekstart
, 0);
4300 catfheadsize
= int(preheaderdata
[0], 16);
4301 catfnumfields
= int(preheaderdata
[1], 16);
4302 catftype
= int(preheaderdata
[2], 16);
4303 if(re
.findall("^[.|/]", preheaderdata
[3])):
4304 catfname
= preheaderdata
[3];
4306 catfname
= "./"+preheaderdata
[3];
4307 catflinkname
= preheaderdata
[4];
4308 catfsize
= int(preheaderdata
[5], 16);
4309 catfbasedir
= os
.path
.dirname(catfname
);
4311 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
4315 catlist
.update({'catfp': catfp
});
4320 create_alias_function("", __file_format_name__
, "SeekToFileName", ArchiveFileSeekToFileName
);
4322 def ArchiveFileValidate(infile
, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
4324 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
4325 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4328 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4329 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
4330 if(checkcompressfile
=="tarfile"):
4331 return TarFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4332 if(checkcompressfile
=="zipfile"):
4333 return ZipFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4334 if(rarfile_support
and checkcompressfile
=="rarfile"):
4335 return RarFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4336 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4337 return SevenZipFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4338 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4345 if(hasattr(sys
.stdin
, "buffer")):
4346 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
4348 shutil
.copyfileobj(sys
.stdin
, catfp
);
4350 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4354 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4355 catfp
= download_file_from_internet_file(infile
);
4356 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4362 infile
= RemoveWindowsPath(infile
);
4363 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
4364 if(checkcompressfile
=="tarfile"):
4365 return TarFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4366 if(checkcompressfile
=="zipfile"):
4367 return ZipFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4368 if(rarfile_support
and checkcompressfile
=="rarfile"):
4369 return RarFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4370 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4371 return SevenZipFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4372 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4374 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
4375 if(not compresscheck
):
4376 fextname
= os
.path
.splitext(infile
)[1];
4377 if(fextname
==".gz"):
4378 compresscheck
= "gzip";
4379 elif(fextname
==".bz2"):
4380 compresscheck
= "bzip2";
4381 elif(fextname
==".zst"):
4382 compresscheck
= "zstd";
4383 elif(fextname
==".lz4" or fextname
==".clz4"):
4384 compresscheck
= "lz4";
4385 elif(fextname
==".lzo" or fextname
==".lzop"):
4386 compresscheck
= "lzo";
4387 elif(fextname
==".lzma" or fextname
==".xz"):
4388 compresscheck
= "lzma";
4391 if(not compresscheck
):
4393 catfp
= UncompressFile(infile
, formatspecs
, "rb");
4398 SeekToEndOfFile(catfp);
4400 SeekToEndOfFile(catfp);
4401 CatSize = catfp.tell();
4402 CatSizeEnd = CatSize;
4410 curloc
= catfp
.tell();
4413 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
4415 catfp
.seek(curloc
, 0);
4416 catstring
= catheader
[0];
4417 catversion
= re
.findall(r
"([\d]+)$", catstring
);
4418 fprenumfiles
= catheader
[1];
4419 fnumfiles
= int(fprenumfiles
, 16);
4420 fprechecksumtype
= catheader
[2];
4421 fprechecksum
= catheader
[3];
4423 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
4424 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4425 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
4426 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
4427 catfileheadercshex
= format(0, 'x').lower();
4428 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
4429 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4430 elif(fprechecksumtype
=="crc16_ccitt"):
4431 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4432 elif(fprechecksumtype
=="adler32"):
4433 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4434 elif(fprechecksumtype
=="crc32"):
4435 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4436 elif(fprechecksumtype
=="crc64_ecma"):
4437 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4438 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
4439 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4440 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
4441 checksumoutstr
= hashlib
.new(fprechecksumtype
);
4442 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4443 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4445 catfileheadercshex
= format(0, 'x').lower();
4446 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4447 valid_archive
= True;
4448 invalid_archive
= False;
4450 VerbosePrintOut(infile
);
4451 VerbosePrintOut("Number of Records " + str(fnumfiles
));
4452 if(fprechecksum
==catfileheadercshex
):
4454 VerbosePrintOut("File Header Checksum Passed at offset " + str(0));
4457 VerbosePrintOut("File Header Checksum Failed at offset " + str(0));
4458 valid_archive
= False;
4459 invalid_archive
= True;
4461 VerbosePrintOut("");
4462 while(il
<fnumfiles
):
4463 catfhstart
= catfp
.tell();
4465 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
4467 catheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
4468 if(len(catheaderdata
)==0):
4470 catfheadsize
= int(catheaderdata
[0], 16);
4471 catfnumfields
= int(catheaderdata
[1], 16);
4472 catftype
= int(catheaderdata
[2], 16);
4473 if(re
.findall("^[.|/]", catheaderdata
[3])):
4474 catfname
= catheaderdata
[3];
4476 catfname
= "./"+catheaderdata
[3];
4477 catfbasedir
= os
.path
.dirname(catfname
);
4478 catflinkname
= catheaderdata
[4];
4479 catfsize
= int(catheaderdata
[5], 16);
4480 catfatime
= int(catheaderdata
[6], 16);
4481 catfmtime
= int(catheaderdata
[7], 16);
4482 catfctime
= int(catheaderdata
[8], 16);
4483 catfbtime
= int(catheaderdata
[9], 16);
4484 catfmode
= int(catheaderdata
[10], 16);
4485 catfchmode
= stat
.S_IMODE(catfmode
);
4486 catftypemod
= stat
.S_IFMT(catfmode
);
4487 prefwinattributes
= int(catheaderdata
[11], 16);
4488 catfuid
= int(catheaderdata
[12], 16);
4489 catfuname
= catheaderdata
[13];
4490 catfgid
= int(catheaderdata
[14], 16);
4491 catfgname
= catheaderdata
[15];
4492 fid
= int(catheaderdata
[16], 16);
4493 finode
= int(catheaderdata
[17], 16);
4494 flinkcount
= int(catheaderdata
[18], 16);
4495 catfdev_minor
= int(catheaderdata
[19], 16);
4496 catfdev_major
= int(catheaderdata
[20], 16);
4497 catfrdev_minor
= int(catheaderdata
[21], 16);
4498 catfrdev_major
= int(catheaderdata
[22], 16);
4499 catfextrasize
= int(catheaderdata
[23], 16);
4500 catfextrafields
= int(catheaderdata
[24], 16);
4501 extrafieldslist
= [];
4503 extraend
= extrastart
+ catfextrafields
;
4504 extrafieldslist
= [];
4505 if(extrastart
<extraend
):
4506 extrafieldslist
.append(catheaderdata
[extrastart
]);
4507 extrastart
= extrastart
+ 1;
4508 catfchecksumtype
= catheaderdata
[extrastart
].lower();
4509 catfcs
= catheaderdata
[extrastart
+ 1].lower();
4510 catfccs
= catheaderdata
[extrastart
+ 2].lower();
4512 hcmax
= len(catheaderdata
) - 2;
4515 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[5]);
4517 catnewfcs
= GetFileChecksum(hout
, catheaderdata
[-3].lower(), True, formatspecs
);
4519 VerbosePrintOut(catfname
);
4520 VerbosePrintOut("Record Number " + str(il
) + "; File ID " + str(fid
) + "; iNode Number " + str(finode
));
4521 if(catfcs
==catnewfcs
):
4523 VerbosePrintOut("File Header Checksum Passed at offset " + str(catfhstart
));
4526 VerbosePrintOut("File Header Checksum Failed at offset " + str(catfhstart
));
4527 valid_archive
= False;
4528 invalid_archive
= True;
4529 catfhend
= catfp
.tell() - 1;
4530 catfcontentstart
= catfp
.tell();
4532 pyhascontents
= False;
4534 catfcontents
= catfp
.read(catfsize
);
4535 catnewfccs
= GetFileChecksum(catfcontents
, catheaderdata
[-3].lower(), False, formatspecs
);
4536 pyhascontents
= True;
4537 if(catfccs
==catnewfccs
):
4539 VerbosePrintOut("File Content Checksum Passed at offset " + str(catfcontentstart
));
4542 VerbosePrintOut("File Content Checksum Failed at offset " + str(catfcontentstart
));
4543 valid_archive
= False;
4544 invalid_archive
= True;
4546 VerbosePrintOut("");
4559 create_alias_function("", __file_format_name__
, "Validate", ArchiveFileValidate
);
4561 def ArchiveFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4562 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4565 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4566 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
4567 if(checkcompressfile
=="tarfile"):
4568 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4569 if(checkcompressfile
=="zipfile"):
4570 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4571 if(rarfile_support
and checkcompressfile
=="rarfile"):
4572 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4573 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4574 return SevenZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4575 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4582 if(hasattr(sys
.stdin
, "buffer")):
4583 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
4585 shutil
.copyfileobj(sys
.stdin
, catfp
);
4587 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4591 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4592 catfp
= download_file_from_internet_file(infile
);
4593 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4599 infile
= RemoveWindowsPath(infile
);
4600 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
4601 if(checkcompressfile
=="tarfile"):
4602 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4603 if(checkcompressfile
=="zipfile"):
4604 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4605 if(rarfile_support
and checkcompressfile
=="rarfile"):
4606 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4607 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4608 return SevenZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4609 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4611 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
4612 if(not compresscheck
):
4613 fextname
= os
.path
.splitext(infile
)[1];
4614 if(fextname
==".gz"):
4615 compresscheck
= "gzip";
4616 elif(fextname
==".bz2"):
4617 compresscheck
= "bzip2";
4618 elif(fextname
==".zst"):
4619 compresscheck
= "zstd";
4620 elif(fextname
==".lz4" or fextname
==".clz4"):
4621 compresscheck
= "lz4";
4622 elif(fextname
==".lzo" or fextname
==".lzop"):
4623 compresscheck
= "lzo";
4624 elif(fextname
==".lzma" or fextname
==".xz"):
4625 compresscheck
= "lzma";
4628 if(not compresscheck
):
4630 catfp
= UncompressFile(infile
, formatspecs
, "rb");
4635 SeekToEndOfFile(catfp);
4637 SeekToEndOfFile(catfp);
4638 CatSize = catfp.tell();
4639 CatSizeEnd = CatSize;
4647 curloc
= catfp
.tell();
4650 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
4652 catfp
.seek(curloc
, 0);
4653 catstring
= catheader
[0];
4654 catversion
= re
.findall(r
"([\d]+)$", catstring
);
4655 fprenumfiles
= catheader
[1];
4656 fnumfiles
= int(fprenumfiles
, 16);
4657 fprechecksumtype
= catheader
[2];
4658 fprechecksum
= catheader
[3];
4659 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
4660 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4661 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
4662 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
4663 catfileheadercshex
= format(0, 'x').lower();
4664 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
4665 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4666 elif(fprechecksumtype
=="crc16_ccitt"):
4667 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4668 elif(fprechecksumtype
=="adler32"):
4669 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4670 elif(fprechecksumtype
=="crc32"):
4671 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4672 elif(fprechecksumtype
=="crc64_ecma"):
4673 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4674 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
4675 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4676 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
4677 checksumoutstr
= hashlib
.new(fprechecksumtype
);
4678 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4679 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4681 catfileheadercshex
= format(0, 'x').lower();
4682 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4683 fheadtell
= len(fileheader
);
4684 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
4685 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
4687 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4688 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
4689 if(seekstart
<0 and seekstart
>fnumfiles
):
4691 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
4692 seekend
= fnumfiles
;
4693 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
4694 seekend
= fnumfiles
- abs(seekend
);
4697 while(il
< seekstart
):
4698 prefhstart
= catfp
.tell();
4700 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
4702 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
4703 if(len(preheaderdata
)==0):
4705 prefheadsize
= int(preheaderdata
[0], 16);
4706 prefnumfields
= int(preheaderdata
[1], 16);
4707 if(re
.findall("^[.|/]", preheaderdata
[3])):
4708 prefname
= preheaderdata
[3];
4710 prefname
= "./"+preheaderdata
[3];
4711 prefsize
= int(preheaderdata
[5], 16);
4712 prefextrasize
= int(preheaderdata
[23], 16);
4713 prefextrafields
= int(preheaderdata
[24], 16);
4714 extrafieldslist
= [];
4716 extraend
= extrastart
+ prefextrafields
;
4717 extrafieldslist
= [];
4718 if(extrastart
<extraend
):
4719 extrafieldslist
.append(preheaderdata
[extrastart
]);
4720 extrastart
= extrastart
+ 1;
4721 prefchecksumtype
= preheaderdata
[extrastart
].lower();
4722 prefcs
= preheaderdata
[extrastart
+ 1].lower();
4723 prefccs
= preheaderdata
[extrastart
+ 2].lower();
4725 hcmax
= len(preheaderdata
) - 2;
4728 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
4730 prenewfcs
= GetFileChecksum(hout
, preheaderdata
[-3].lower(), True, formatspecs
);
4731 if(prefcs
!=prenewfcs
and not skipchecksum
):
4732 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
4734 valid_archive
= False;
4735 invalid_archive
= True;
4736 prefhend
= catfp
.tell() - 1;
4737 prefcontentstart
= catfp
.tell();
4739 pyhascontents
= False;
4741 prefcontents
= catfp
.read(prefsize
);
4742 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
4743 pyhascontents
= True;
4744 if(prefccs
!=prenewfccs
and not skipchecksum
):
4745 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
4749 fileidnum
= seekstart
;
4751 while(fileidnum
<seekend
):
4752 catfhstart
= catfp
.tell();
4754 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
4756 catheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
4757 if(len(catheaderdata
)==0):
4759 catfheadsize
= int(catheaderdata
[0], 16);
4760 catfnumfields
= int(catheaderdata
[1], 16);
4761 catftype
= int(catheaderdata
[2], 16);
4762 if(re
.findall("^[.|/]", catheaderdata
[3])):
4763 catfname
= catheaderdata
[3];
4765 catfname
= "./"+catheaderdata
[3];
4766 catfbasedir
= os
.path
.dirname(catfname
);
4767 catflinkname
= catheaderdata
[4];
4768 catfsize
= int(catheaderdata
[5], 16);
4769 catfatime
= int(catheaderdata
[6], 16);
4770 catfmtime
= int(catheaderdata
[7], 16);
4771 catfctime
= int(catheaderdata
[8], 16);
4772 catfbtime
= int(catheaderdata
[9], 16);
4773 catfmode
= int(catheaderdata
[10], 16);
4774 catfchmode
= stat
.S_IMODE(catfmode
);
4775 catftypemod
= stat
.S_IFMT(catfmode
);
4776 catfwinattributes
= int(catheaderdata
[11], 16);
4777 catfuid
= int(catheaderdata
[12], 16);
4778 catfuname
= catheaderdata
[13];
4779 catfgid
= int(catheaderdata
[14], 16);
4780 catfgname
= catheaderdata
[15];
4781 fid
= int(catheaderdata
[16], 16);
4782 finode
= int(catheaderdata
[17], 16);
4783 flinkcount
= int(catheaderdata
[18], 16);
4784 catfdev_minor
= int(catheaderdata
[19], 16);
4785 catfdev_major
= int(catheaderdata
[20], 16);
4786 catfrdev_minor
= int(catheaderdata
[21], 16);
4787 catfrdev_major
= int(catheaderdata
[22], 16);
4788 catfextrasize
= int(catheaderdata
[23], 16);
4789 catfextrafields
= int(catheaderdata
[24], 16);
4790 extrafieldslist
= [];
4792 extraend
= extrastart
+ catfextrafields
;
4793 extrafieldslist
= [];
4794 if(extrastart
<extraend
):
4795 extrafieldslist
.append(catheaderdata
[extrastart
]);
4796 extrastart
= extrastart
+ 1;
4797 catfchecksumtype
= catheaderdata
[extrastart
].lower();
4798 catfcs
= catheaderdata
[extrastart
+ 1].lower();
4799 catfccs
= catheaderdata
[extrastart
+ 2].lower();
4801 hcmax
= len(catheaderdata
) - 2;
4804 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[5]);
4806 catnewfcs
= GetFileChecksum(hout
, catheaderdata
[-3].lower(), True, formatspecs
);
4807 if(catfcs
!=catnewfcs
and not skipchecksum
):
4808 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfhstart
));
4810 catfhend
= catfp
.tell() - 1;
4811 catfcontentstart
= catfp
.tell();
4813 pyhascontents
= False;
4814 if(catfsize
>0 and not listonly
):
4815 catfcontents
= catfp
.read(catfsize
);
4816 catnewfccs
= GetFileChecksum(catfcontents
, catheaderdata
[-3].lower(), False, formatspecs
);
4817 pyhascontents
= True;
4818 if(catfccs
!=catnewfccs
and skipchecksum
):
4819 VerbosePrintOut("File Content Checksum Error with file " + catfname
+ " at offset " + str(catfcontentstart
));
4821 if(catfsize
>0 and listonly
):
4822 catfp
.seek(catfsize
, 1);
4823 pyhascontents
= False;
4825 catfcontentend
= catfp
.tell() - 1;
4826 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
} });
4827 fileidnum
= fileidnum
+ 1;
4828 realidnum
= realidnum
+ 1;
4830 catlist
.update({'catfp': catfp
});
4835 create_alias_function("", __file_format_name__
, "ToArray", ArchiveFileToArray
);
4837 def ArchiveFileStringToArray(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4838 catfp
= BytesIO(catstr
);
4839 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4840 return listcatfiles
;
4842 create_alias_function("", __file_format_name__
, "StringToArray", ArchiveFileStringToArray
);
4844 def TarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4846 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4847 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4848 return listcatfiles
;
4850 def ZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4852 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4853 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4854 return listcatfiles
;
4856 if(not rarfile_support
):
4857 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4860 if(rarfile_support
):
4861 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4863 catfp
= PackArchiveFileFromSevenZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4864 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4865 return listcatfiles
;
4867 if(not py7zr_support
):
4868 def SevenZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4872 def SevenZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4874 catfp
= PackArchiveFileFromSevenZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4875 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4876 return listcatfiles
;
4878 def InFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4879 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
4880 if(checkcompressfile
=="tarfile"):
4881 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4882 elif(checkcompressfile
=="zipfile"):
4883 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4884 elif(checkcompressfile
=="catfile"):
4885 return ArchiveFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4886 elif(rarfile_support
and checkcompressfile
=="rarfile"):
4887 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4888 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
4889 return SevenZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4894 def ListDirToArrayAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4895 catver
= formatspecs
[6];
4896 fileheaderver
= str(int(catver
.replace(".", "")));
4897 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4898 advancedlist
= formatspecs
[8];
4899 altinode
= formatspecs
[9];
4902 for line
in sys
.stdin
:
4903 infilelist
.append(line
.strip());
4904 infilelist
= list(filter(None, infilelist
));
4905 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
4906 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
4908 with
UncompressFile(infiles
, formatspecs
, "r") as finfile
:
4909 for line
in finfile
:
4910 infilelist
.append(line
.strip());
4911 infilelist
= list(filter(None, infilelist
));
4913 if(isinstance(infiles
, (list, tuple, ))):
4914 infilelist
= list(filter(None, infiles
));
4915 elif(isinstance(infiles
, (str, ))):
4916 infilelist
= list(filter(None, [infiles
]));
4918 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
4920 GetDirList
= ListDir(infilelist
, followlink
, False);
4928 inodetocatinode
= {};
4930 fnumfiles
= int(len(GetDirList
));
4931 catver
= formatspecs
[6];
4932 fileheaderver
= str(int(catver
.replace(".", "")));
4933 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4934 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4935 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4936 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4937 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4938 if(checksumtype
=="none" or checksumtype
==""):
4939 catfileheadercshex
= format(0, 'x').lower();
4940 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4941 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4942 elif(checksumtype
=="crc16_ccitt"):
4943 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4944 elif(checksumtype
=="adler32"):
4945 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4946 elif(checksumtype
=="crc32"):
4947 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4948 elif(checksumtype
=="crc64_ecma"):
4949 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4950 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4951 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4952 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4953 checksumoutstr
= hashlib
.new(checksumtype
);
4954 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4955 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4957 catfileheadercshex
= format(0, 'x').lower();
4958 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4959 fheadtell
= len(fileheader
);
4960 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4961 for curfname
in GetDirList
:
4962 if(re
.findall("^[.|/]", curfname
)):
4965 fname
= "./"+curfname
;
4967 VerbosePrintOut(fname
);
4968 if(not followlink
or followlink
is None):
4969 fstatinfo
= os
.lstat(fname
);
4971 fstatinfo
= os
.stat(fname
);
4972 fpremode
= fstatinfo
.st_mode
;
4973 finode
= fstatinfo
.st_ino
;
4974 flinkcount
= fstatinfo
.st_nlink
;
4976 if(stat
.S_ISREG(fpremode
)):
4978 elif(stat
.S_ISLNK(fpremode
)):
4980 elif(stat
.S_ISCHR(fpremode
)):
4982 elif(stat
.S_ISBLK(fpremode
)):
4984 elif(stat
.S_ISDIR(fpremode
)):
4986 elif(stat
.S_ISFIFO(fpremode
)):
4988 elif(stat
.S_ISSOCK(fpremode
)):
4990 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
4992 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
4994 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
4999 fbasedir
= os
.path
.dirname(fname
);
5001 if(not followlink
and finode
!=0):
5003 if(finode
in inodelist
):
5005 flinkname
= inodetofile
[finode
];
5009 fcurinode
= inodetocatinode
[finode
];
5010 if(finode
not in inodelist
):
5011 inodelist
.append(finode
);
5012 inodetofile
.update({finode
: fname
});
5013 inodetocatinode
.update({finode
: curinode
});
5017 fcurinode
= curinode
;
5018 curinode
= curinode
+ 1;
5020 fcurinode
= curinode
;
5021 curinode
= curinode
+ 1;
5022 curfid
= curfid
+ 1;
5024 flinkname
= os
.readlink(fname
);
5025 fdev
= fstatinfo
.st_dev
;
5026 getfdev
= GetDevMajorMinor(fdev
);
5027 fdev_minor
= getfdev
[0];
5028 fdev_major
= getfdev
[1];
5029 frdev
= fstatinfo
.st_dev
;
5030 if(hasattr(fstatinfo
, "st_rdev")):
5031 frdev
= fstatinfo
.st_rdev
;
5033 frdev
= fstatinfo
.st_dev
;
5034 getfrdev
= GetDevMajorMinor(frdev
);
5035 frdev_minor
= getfrdev
[0];
5036 frdev_major
= getfrdev
[1];
5037 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
5039 if(ftype
==0 or ftype
==7):
5040 fsize
= fstatinfo
.st_size
;
5041 fatime
= fstatinfo
.st_atime
;
5042 fmtime
= fstatinfo
.st_mtime
;
5043 fctime
= fstatinfo
.st_ctime
;
5044 if(hasattr(fstatinfo
, "st_birthtime")):
5045 fbtime
= fstatinfo
.st_birthtime
;
5047 fbtime
= fstatinfo
.st_ctime
;
5048 fmode
= fstatinfo
.st_mode
;
5049 fchmode
= stat
.S_IMODE(fstatinfo
.st_mode
);
5050 ftypemod
= stat
.S_IFMT(fstatinfo
.st_mode
);
5051 fuid
= fstatinfo
.st_uid
;
5052 fgid
= fstatinfo
.st_gid
;
5057 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
5058 funame
= userinfo
.pw_name
;
5067 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
5068 fgname
= groupinfo
.gr_name
;
5073 fdev_minor
= fdev_minor
;
5074 fdev_major
= fdev_major
;
5075 frdev_minor
= frdev_minor
;
5076 frdev_major
= frdev_major
;
5078 flinkcount
= flinkcount
;
5079 if(hasattr(fstatinfo
, "st_file_attributes")):
5080 fwinattributes
= fstatinfo
.st_file_attributes
;
5083 fcontents
= "".encode('UTF-8');
5085 if(ftype
==0 or ftype
==7):
5086 with
open(fname
, "rb") as fpc
:
5088 chunk
= fpc
.read(chunk_size
);
5092 if(followlink
and (ftype
==1 or ftype
==2)):
5093 flstatinfo
= os
.stat(flinkname
);
5094 with
open(flinkname
, "rb") as fpc
:
5096 chunk
= fpc
.read(chunk_size
);
5100 ftypehex
= format(ftype
, 'x').lower();
5101 extrafields
= len(extradata
);
5102 extrafieldslist
= extradata
;
5103 catfextrafields
= extrafields
;
5104 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5105 if(len(extradata
)>0):
5106 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5107 extrasizelen
= len(extrasizestr
);
5108 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5109 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()];
5110 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5111 catoutlenhex
= format(catoutlen
, 'x').lower();
5112 catoutlist
.insert(0, catoutlenhex
);
5113 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5114 if(len(extradata
)>0):
5115 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5116 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5117 catfnumfields
= catoutlen
;
5118 if(checksumtype
=="none" or checksumtype
==""):
5119 catfileheadercshex
= format(0, 'x').lower();
5120 catfilecontentcshex
= format(0, 'x').lower();
5121 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5122 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5123 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
5124 elif(checksumtype
=="crc16_ccitt"):
5125 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5126 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
5127 elif(checksumtype
=="adler32"):
5128 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5129 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
5130 elif(checksumtype
=="crc32"):
5131 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5132 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
5133 elif(checksumtype
=="crc64_ecma"):
5134 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5135 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
5136 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5137 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5138 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
5139 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5140 checksumoutstr
= hashlib
.new(checksumtype
);
5141 checksumoutstr
.update("".encode('UTF-8'));
5142 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5143 checksumoutstr
= hashlib
.new(checksumtype
);
5144 checksumoutstr
.update(fcontents
);
5145 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
5147 catfileheadercshex
= format(0, 'x').lower();
5148 catfilecontentcshex
= format(0, 'x').lower();
5149 catfhstart
= fheadtell
;
5150 fheadtell
+= len(catfileoutstr
);
5151 catfhend
= fheadtell
- 1;
5152 catfcontentstart
= fheadtell
;
5153 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5154 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
5155 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5156 catheaderdata
= [catheaersize
] + catoutlist
+ extradata
+ [checksumtype
, catfileheadercshex
, catfilecontentcshex
]
5157 if(checksumtype
=="none" or checksumtype
==""):
5158 catfileheadercshex
= format(0, 'x').lower();
5159 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5160 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5161 elif(checksumtype
=="crc16_ccitt"):
5162 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5163 elif(checksumtype
=="adler32"):
5164 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5165 elif(checksumtype
=="crc32"):
5166 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5167 elif(checksumtype
=="crc64_ecma"):
5168 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5169 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5170 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5171 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5172 checksumoutstr
= hashlib
.new(checksumtype
);
5173 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5174 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5176 catfileheadercshex
= format(0, 'x').lower();
5177 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5178 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5179 nullstrecd
= formatspecs
[5].encode('UTF-8');
5180 fheadtell
+= len(catfileoutstr
) + 1;
5181 catfcontentend
= fheadtell
- 1;
5182 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
5183 pyhascontents
= False;
5184 if(int(fsize
)>0 and not listonly
):
5185 pyhascontents
= True;
5186 if(int(fsize
)>0 and listonly
):
5188 pyhascontents
= False;
5189 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
} });
5190 fileidnum
= fileidnum
+ 1;
5193 def TarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5199 inodetocatinode
= {};
5203 if(hasattr(sys
.stdin
, "buffer")):
5204 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
5206 shutil
.copyfileobj(sys
.stdin
, infile
);
5211 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
5212 infile
= download_file_from_internet_file(infile
);
5217 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5219 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
5221 if(not tarfile
.is_tarfile(infile
)):
5223 except AttributeError:
5224 if(not is_tarfile(infile
)):
5229 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
5230 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
5232 tarfp
= tarfile
.open(infile
, "r");
5233 except FileNotFoundError
:
5235 fnumfiles
= int(len(tarfp
.getmembers()));
5236 catver
= formatspecs
[6];
5237 fileheaderver
= str(int(catver
.replace(".", "")));
5238 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5239 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5240 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
5241 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5242 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5243 if(checksumtype
=="none" or checksumtype
==""):
5244 catfileheadercshex
= format(0, 'x').lower();
5245 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5246 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
5247 elif(checksumtype
=="crc16_ccitt"):
5248 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
5249 elif(checksumtype
=="adler32"):
5250 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5251 elif(checksumtype
=="crc32"):
5252 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5253 elif(checksumtype
=="crc64_ecma"):
5254 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5255 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5256 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5257 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5258 checksumoutstr
= hashlib
.new(checksumtype
);
5259 checksumoutstr
.update(fileheader
.encode('UTF-8'));
5260 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5262 catfileheadercshex
= format(0, 'x').lower();
5263 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
5264 fheadtell
= len(fileheader
);
5265 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5266 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
5267 if(re
.findall("^[.|/]", member
.name
)):
5268 fname
= member
.name
;
5270 fname
= "./"+member
.name
;
5272 VerbosePrintOut(fname
);
5273 fpremode
= member
.mode
;
5274 ffullmode
= member
.mode
;
5278 ffullmode
= member
.mode
+ stat
.S_IFREG
;
5280 elif(member
.isdev()):
5281 ffullmode
= member
.mode
;
5283 elif(member
.islnk()):
5284 ffullmode
= member
.mode
+ stat
.S_IFREG
;
5286 elif(member
.issym()):
5287 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
5289 elif(member
.ischr()):
5290 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
5292 elif(member
.isblk()):
5293 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
5295 elif(member
.isdir()):
5296 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
5298 elif(member
.isfifo()):
5299 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
5301 elif(member
.issparse()):
5302 ffullmode
= member
.mode
;
5305 ffullmode
= member
.mode
;
5308 fbasedir
= os
.path
.dirname(fname
);
5312 curfid
= curfid
+ 1;
5314 flinkname
= member
.linkname
;
5315 fdev_minor
= member
.devminor
;
5316 fdev_major
= member
.devmajor
;
5317 frdev_minor
= member
.devminor
;
5318 frdev_major
= member
.devmajor
;
5319 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
5321 elif(ftype
==0 or ftype
==7):
5322 fsize
= member
.size
;
5324 fsize
= member
.size
;
5325 fatime
= member
.mtime
;
5326 fmtime
= member
.mtime
;
5327 fctime
= member
.mtime
;
5328 fbtime
= member
.mtime
;
5330 fchmode
= stat
.S_IMODE(ffullmode
);
5331 ftypemod
= stat
.S_IFMT(ffullmode
);
5334 funame
= member
.uname
;
5335 fgname
= member
.gname
;
5336 flinkcount
= flinkcount
;
5337 fwinattributes
= int(0);
5338 fcontents
= "".encode('UTF-8');
5340 if(ftype
==0 or ftype
==7):
5341 with tarfp
.extractfile(member
) as fpc
:
5343 chunk
= fpc
.read(chunk_size
);
5347 ftypehex
= format(ftype
, 'x').lower();
5348 extrafields
= len(extradata
);
5349 extrafieldslist
= extradata
;
5350 catfextrafields
= extrafields
;
5351 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5352 if(len(extradata
)>0):
5353 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5354 extrasizelen
= len(extrasizestr
);
5355 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5356 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()];
5357 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5358 catoutlenhex
= format(catoutlen
, 'x').lower();
5359 catoutlist
.insert(0, catoutlenhex
);
5360 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5361 if(len(extradata
)>0):
5362 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5363 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5364 catfnumfields
= catoutlen
;
5365 if(checksumtype
=="none" or checksumtype
==""):
5366 catfileheadercshex
= format(0, 'x').lower();
5367 catfilecontentcshex
= format(0, 'x').lower();
5368 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5369 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5370 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
5371 elif(checksumtype
=="crc16_ccitt"):
5372 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5373 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
5374 elif(checksumtype
=="adler32"):
5375 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5376 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
5377 elif(checksumtype
=="crc32"):
5378 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5379 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
5380 elif(checksumtype
=="crc64_ecma"):
5381 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5382 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
5383 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5384 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5385 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
5386 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5387 checksumoutstr
= hashlib
.new(checksumtype
);
5388 checksumoutstr
.update("".encode('UTF-8'));
5389 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5390 checksumoutstr
= hashlib
.new(checksumtype
);
5391 checksumoutstr
.update(fcontents
);
5392 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
5394 catfileheadercshex
= format(0, 'x').lower();
5395 catfilecontentcshex
= format(0, 'x').lower();
5396 catfhstart
= fheadtell
;
5397 fheadtell
+= len(catfileoutstr
);
5398 catfhend
= fheadtell
- 1;
5399 catfcontentstart
= fheadtell
;
5400 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5401 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
5402 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5403 catheaderdata
= [catheaersize
] + catoutlist
+ extradata
+ [checksumtype
, catfileheadercshex
, catfilecontentcshex
]
5404 if(checksumtype
=="none" or checksumtype
==""):
5405 catfileheadercshex
= format(0, 'x').lower();
5406 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5407 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5408 elif(checksumtype
=="crc16_ccitt"):
5409 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5410 elif(checksumtype
=="adler32"):
5411 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5412 elif(checksumtype
=="crc32"):
5413 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5414 elif(checksumtype
=="crc64_ecma"):
5415 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5416 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5417 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5418 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5419 checksumoutstr
= hashlib
.new(checksumtype
);
5420 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5421 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5423 catfileheadercshex
= format(0, 'x').lower();
5424 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5425 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5426 nullstrecd
= formatspecs
[5].encode('UTF-8');
5427 fheadtell
+= len(catfileoutstr
) + 1;
5428 catfcontentend
= fheadtell
- 1;
5429 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
5430 pyhascontents
= False;
5431 if(int(fsize
)>0 and not listonly
):
5432 pyhascontents
= True;
5433 if(int(fsize
)>0 and listonly
):
5435 pyhascontents
= False;
5436 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
} });
5437 fileidnum
= fileidnum
+ 1;
5440 def ZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5446 inodetocatinode
= {};
5450 if(hasattr(sys
.stdin
, "buffer")):
5451 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
5453 shutil
.copyfileobj(sys
.stdin
, infile
);
5458 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
5459 infile
= download_file_from_internet_file(infile
);
5464 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5468 if(not zipfile
.is_zipfile(infile
)):
5471 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
5472 except FileNotFoundError
:
5474 ziptest
= zipfp
.testzip();
5476 VerbosePrintOut("Bad file found!");
5477 fnumfiles
= int(len(zipfp
.infolist()));
5478 catver
= formatspecs
[6];
5479 fileheaderver
= str(int(catver
.replace(".", "")));
5480 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5481 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5482 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5483 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5484 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
5485 if(checksumtype
=="none" or checksumtype
==""):
5486 catfileheadercshex
= format(0, 'x').lower();
5487 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5488 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
5489 elif(checksumtype
=="crc16_ccitt"):
5490 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
5491 elif(checksumtype
=="adler32"):
5492 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5493 elif(checksumtype
=="crc32"):
5494 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5495 elif(checksumtype
=="crc64_ecma"):
5496 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5497 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5498 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5499 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5500 checksumoutstr
= hashlib
.new(checksumtype
);
5501 checksumoutstr
.update(fileheader
.encode('UTF-8'));
5502 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5504 catfileheadercshex
= format(0, 'x').lower();
5505 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
5506 fheadtell
= len(fileheader
);
5507 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5508 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
5509 if(re
.findall("^[.|/]", member
.filename
)):
5510 fname
= member
.filename
;
5512 fname
= "./"+member
.filename
;
5513 zipinfo
= zipfp
.getinfo(member
.filename
);
5515 VerbosePrintOut(fname
);
5516 if(not member
.is_dir()):
5517 fpremode
= stat
.S_IFREG
+ 438;
5518 elif(member
.is_dir()):
5519 fpremode
= stat
.S_IFDIR
+ 511;
5522 if(not member
.is_dir()):
5524 elif(member
.is_dir()):
5527 fbasedir
= os
.path
.dirname(fname
);
5531 curfid
= curfid
+ 1;
5539 fsize
= member
.file_size
;
5541 fsize
= member
.file_size
;
5542 fatime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5543 fmtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5544 fctime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5545 fbtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5546 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
5547 fwinattributes
= int(zipinfo
.external_attr
);
5548 if(not member
.is_dir()):
5549 fmode
= int(stat
.S_IFREG
+ 438);
5550 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
5551 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
5552 elif(member
.is_dir()):
5553 fmode
= int(stat
.S_IFDIR
+ 511);
5554 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
5555 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
5556 elif(zipinfo
.create_system
==3):
5557 fwinattributes
= int(0);
5558 fmode
= int(zipinfo
.external_attr
);
5560 fwinattributes
= int(0);
5561 if(not member
.is_dir()):
5562 fmode
= int(stat
.S_IFREG
+ 438);
5563 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
5564 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
5565 elif(member
.is_dir()):
5566 fmode
= int(stat
.S_IFDIR
+ 511);
5567 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
5568 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
5571 except AttributeError:
5577 except AttributeError:
5584 userinfo
= pwd
.getpwuid(os
.getuid());
5585 funame
= userinfo
.pw_name
;
5588 except AttributeError:
5596 groupinfo
= grp
.getgrgid(os
.getgid());
5597 fgname
= groupinfo
.gr_name
;
5600 except AttributeError:
5604 fcontents
= "".encode('UTF-8');
5606 fcontents
= zipfp
.read(member
.filename
);
5607 ftypehex
= format(ftype
, 'x').lower();
5608 extrafields
= len(extradata
);
5609 extrafieldslist
= extradata
;
5610 catfextrafields
= extrafields
;
5611 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5612 if(len(extradata
)>0):
5613 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5614 extrasizelen
= len(extrasizestr
);
5615 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5616 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()];
5617 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5618 catoutlenhex
= format(catoutlen
, 'x').lower();
5619 catoutlist
.insert(0, catoutlenhex
);
5620 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5621 if(len(extradata
)>0):
5622 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5623 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5624 catfnumfields
= catoutlen
;
5625 if(checksumtype
=="none" or checksumtype
==""):
5626 catfileheadercshex
= format(0, 'x').lower();
5627 catfilecontentcshex
= format(0, 'x').lower();
5628 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5629 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5630 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
5631 elif(checksumtype
=="crc16_ccitt"):
5632 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5633 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
5634 elif(checksumtype
=="adler32"):
5635 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5636 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
5637 elif(checksumtype
=="crc32"):
5638 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5639 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
5640 elif(checksumtype
=="crc64_ecma"):
5641 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5642 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
5643 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5644 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5645 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
5646 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5647 checksumoutstr
= hashlib
.new(checksumtype
);
5648 checksumoutstr
.update("".encode('UTF-8'));
5649 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5650 checksumoutstr
= hashlib
.new(checksumtype
);
5651 checksumoutstr
.update(fcontents
);
5652 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
5654 catfileheadercshex
= format(0, 'x').lower();
5655 catfilecontentcshex
= format(0, 'x').lower();
5656 catfhstart
= fheadtell
;
5657 fheadtell
+= len(catfileoutstr
);
5658 catfhend
= fheadtell
- 1;
5659 catfcontentstart
= fheadtell
;
5660 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5661 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
5662 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5663 catheaderdata
= [catheaersize
] + catoutlist
+ extradata
+ [checksumtype
, catfileheadercshex
, catfilecontentcshex
];
5664 if(checksumtype
=="none" or checksumtype
==""):
5665 catfileheadercshex
= format(0, 'x').lower();
5666 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5667 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5668 elif(checksumtype
=="crc16_ccitt"):
5669 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5670 elif(checksumtype
=="adler32"):
5671 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5672 elif(checksumtype
=="crc32"):
5673 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5674 elif(checksumtype
=="crc64_ecma"):
5675 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5676 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5677 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5678 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5679 checksumoutstr
= hashlib
.new(checksumtype
);
5680 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5681 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5683 catfileheadercshex
= format(0, 'x').lower();
5684 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5685 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5686 nullstrecd
= formatspecs
[5].encode('UTF-8');
5687 fheadtell
+= len(catfileoutstr
) + 1;
5688 catfcontentend
= fheadtell
- 1;
5689 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
5690 pyhascontents
= False;
5691 if(int(fsize
)>0 and not listonly
):
5692 pyhascontents
= True;
5693 if(int(fsize
)>0 and listonly
):
5695 pyhascontents
= False;
5696 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
} });
5697 fileidnum
= fileidnum
+ 1;
5700 if(not rarfile_support
):
5701 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5704 if(rarfile_support
):
5705 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5711 inodetocatinode
= {};
5713 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
5715 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
5717 rarfp
= rarfile
.RarFile(infile
, "r");
5718 rartest
= rarfp
.testrar();
5720 VerbosePrintOut("Bad file found!");
5721 fnumfiles
= int(len(rarfp
.infolist()));
5722 catver
= formatspecs
[6];
5723 fileheaderver
= str(int(catver
.replace(".", "")));
5724 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5725 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5726 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5727 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5728 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
5729 if(checksumtype
=="none" or checksumtype
==""):
5730 catfileheadercshex
= format(0, 'x').lower();
5731 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5732 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
5733 elif(checksumtype
=="crc16_ccitt"):
5734 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
5735 elif(checksumtype
=="adler32"):
5736 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5737 elif(checksumtype
=="crc32"):
5738 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5739 elif(checksumtype
=="crc64_ecma"):
5740 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5741 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5742 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5743 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5744 checksumoutstr
= hashlib
.new(checksumtype
);
5745 checksumoutstr
.update(fileheader
.encode('UTF-8'));
5746 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5748 catfileheadercshex
= format(0, 'x').lower();
5749 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
5750 fheadtell
= len(fileheader
);
5751 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5752 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
5755 if(member
.host_os
==rarfile
.RAR_OS_UNIX
):
5758 member
.external_attr
5760 except AttributeError:
5762 elif(member
.host_os
==rarfile
.RAR_OS_WIN32
):
5765 member
.external_attr
5767 except AttributeError:
5772 if(re
.findall("^[.|/]", member
.filename
)):
5773 fname
= member
.filename
;
5775 fname
= "./"+member
.filename
;
5776 rarinfo
= rarfp
.getinfo(member
.filename
);
5778 VerbosePrintOut(fname
);
5779 if(is_unix
and member
.external_attr
!=0):
5780 fpremode
= int(member
.external_attr
);
5781 elif(member
.is_file()):
5782 fpremode
= stat
.S_IFREG
+ 438;
5783 elif(member
.is_symlink()):
5784 fpremode
= stat
.S_IFLNK
+ 438;
5785 elif(member
.is_dir()):
5786 fpremode
= stat
.S_IFDIR
+ 511;
5787 if(is_windows
and member
.external_attr
!=0):
5788 fwinattributes
= int(member
.external_attr
);
5790 fwinattributes
= int(0);
5793 if(member
.is_file()):
5795 elif(member
.is_symlink()):
5797 elif(member
.is_dir()):
5801 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
5802 fbasedir
= os
.path
.dirname(fname
);
5806 curfid
= curfid
+ 1;
5814 fsize
= member
.file_size
;
5817 fatime
= int(member
.atime
.timestamp());
5819 fatime
= int(member
.mtime
.timestamp());
5820 except AttributeError:
5821 fatime
= int(member
.mtime
.timestamp());
5822 fmtime
= int(member
.mtime
.timestamp());
5825 fctime
= int(member
.ctime
.timestamp());
5827 fctime
= int(member
.mtime
.timestamp());
5828 except AttributeError:
5829 fctime
= int(member
.mtime
.timestamp());
5830 fbtime
= int(member
.mtime
.timestamp());
5831 if(is_unix
and member
.external_attr
!=0):
5832 fmode
= int(member
.external_attr
);
5833 fchmode
= int(stat
.S_IMODE(member
.external_attr
));
5834 ftypemod
= int(stat
.S_IFMT(member
.external_attr
));
5835 elif(member
.is_file()):
5836 fmode
= int(stat
.S_IFREG
+ 438)
5837 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
5838 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
5839 elif(member
.is_symlink()):
5840 fmode
= int(stat
.S_IFLNK
+ 438)
5841 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
5842 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
5843 elif(member
.is_dir()):
5844 fmode
= int(stat
.S_IFDIR
+ 511)
5845 fchmode
= int(stat
.S_IMODE(stat
.S_IFDIR
+ 511));
5846 ftypemod
= int(stat
.S_IFMT(stat
.S_IFDIR
+ 511));
5849 except AttributeError:
5855 except AttributeError:
5862 userinfo
= pwd
.getpwuid(os
.getuid());
5863 funame
= userinfo
.pw_name
;
5866 except AttributeError:
5874 groupinfo
= grp
.getgrgid(os
.getgid());
5875 fgname
= groupinfo
.gr_name
;
5878 except AttributeError:
5882 fcontents
= "".encode('UTF-8');
5884 fcontents
= rarfp
.read(member
.filename
);
5885 ftypehex
= format(ftype
, 'x').lower();
5886 extrafields
= len(extradata
);
5887 extrafieldslist
= extradata
;
5888 catfextrafields
= extrafields
;
5889 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5890 if(len(extradata
)>0):
5891 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5892 extrasizelen
= len(extrasizestr
);
5893 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5894 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()];
5895 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5896 catoutlenhex
= format(catoutlen
, 'x').lower();
5897 catoutlist
.insert(0, catoutlenhex
);
5898 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5899 if(len(extradata
)>0):
5900 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5901 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5902 catfnumfields
= 24 + catfextrafields
;
5903 if(checksumtype
=="none" or checksumtype
==""):
5904 catfileheadercshex
= format(0, 'x').lower();
5905 catfilecontentcshex
= format(0, 'x').lower();
5906 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5907 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5908 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
5909 elif(checksumtype
=="crc16_ccitt"):
5910 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5911 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
5912 elif(checksumtype
=="adler32"):
5913 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5914 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
5915 elif(checksumtype
=="crc32"):
5916 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5917 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
5918 elif(checksumtype
=="crc64_ecma"):
5919 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5920 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
5921 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5922 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5923 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
5924 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5925 checksumoutstr
= hashlib
.new(checksumtype
);
5926 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5927 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5929 catfileheadercshex
= format(0, 'x').lower();
5930 catfilecontentcshex
= format(0, 'x').lower();
5931 catfhstart
= fheadtell
;
5932 fheadtell
+= len(catfileoutstr
);
5933 catfhend
= fheadtell
- 1;
5934 catfcontentstart
= fheadtell
;
5935 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5936 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
5937 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5938 catheaderdata
= [catheaersize
] + catoutlist
+ extradata
+ [checksumtype
, catfileheadercshex
, catfilecontentcshex
];
5939 if(checksumtype
=="none" or checksumtype
==""):
5940 catfileheadercshex
= format(0, 'x').lower();
5941 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5942 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5943 elif(checksumtype
=="crc16_ccitt"):
5944 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5945 elif(checksumtype
=="adler32"):
5946 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5947 elif(checksumtype
=="crc32"):
5948 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5949 elif(checksumtype
=="crc64_ecma"):
5950 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5951 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5952 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5953 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5954 checksumoutstr
= hashlib
.new(checksumtype
);
5955 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5956 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5958 catfileheadercshex
= format(0, 'x').lower();
5959 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5960 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5961 nullstrecd
= formatspecs
[5].encode('UTF-8');
5962 fheadtell
+= len(catfileoutstr
) + 1;
5963 catfcontentend
= fheadtell
- 1;
5964 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
5965 pyhascontents
= False;
5966 if(int(fsize
)>0 and not listonly
):
5967 pyhascontents
= True;
5968 if(int(fsize
)>0 and listonly
):
5970 pyhascontents
= False;
5971 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
} });
5972 fileidnum
= fileidnum
+ 1;
5975 if(not py7zr_support
):
5976 def SevenZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5980 def SevenZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5986 inodetocatinode
= {};
5988 szpfp
= py7zr
.SevenZipFile(infile
, mode
="r");
5989 file_content
= szpfp
.readall();
5990 #sztest = szpfp.testzip();
5991 sztestalt
= szpfp
.test();
5993 VerbosePrintOut("Bad file found!");
5994 numfiles
= int(len(szpfp
.list()));
5995 catver
= formatspecs
[6];
5996 fileheaderver
= str(int(catver
.replace(".", "")));
5997 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5998 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5999 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
6000 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
6001 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
6002 if(checksumtype
=="none" or checksumtype
==""):
6003 catfileheadercshex
= format(0, 'x').lower();
6004 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
6005 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
6006 elif(checksumtype
=="crc16_ccitt"):
6007 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
6008 elif(checksumtype
=="adler32"):
6009 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
6010 elif(checksumtype
=="crc32"):
6011 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
6012 elif(checksumtype
=="crc64_ecma"):
6013 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6014 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
6015 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6016 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
6017 checksumoutstr
= hashlib
.new(checksumtype
);
6018 checksumoutstr
.update(fileheader
.encode('UTF-8'));
6019 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
6021 catfileheadercshex
= format(0, 'x').lower();
6022 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
6023 fheadtell
= len(fileheader
);
6024 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
6025 for member
in sorted(szpfp
.list(), key
=lambda x
: x
.filename
):
6026 if(re
.findall("^[.|/]", member
.filename
)):
6027 fname
= member
.filename
;
6029 fname
= "./"+member
.filename
;
6030 if(not member
.is_directory
):
6031 fpremode
= int(stat
.S_IFREG
+ 438);
6032 elif(member
.is_directory
):
6033 fpremode
= int(stat
.S_IFDIR
+ 511);
6034 fwinattributes
= int(0);
6037 if(member
.is_directory
):
6042 fbasedir
= os
.path
.dirname(fname
);
6046 curfid
= curfid
+ 1;
6053 fatime
= int(member
.creationtime
.timestamp());
6054 fmtime
= int(member
.creationtime
.timestamp());
6055 fctime
= int(member
.creationtime
.timestamp());
6056 fbtime
= int(member
.creationtime
.timestamp());
6057 if(member
.is_directory
):
6058 fmode
= int(stat
.S_IFDIR
+ 511)
6059 fchmode
= int(stat
.S_IMODE(stat
.S_IFDIR
+ 511));
6060 ftypemod
= int(stat
.S_IFMT(stat
.S_IFDIR
+ 511));
6062 fmode
= int(stat
.S_IFLNK
+ 438)
6063 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
6064 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
6067 except AttributeError:
6073 except AttributeError:
6080 userinfo
= pwd
.getpwuid(os
.getuid());
6081 funame
= userinfo
.pw_name
;
6084 except AttributeError:
6092 groupinfo
= grp
.getgrgid(os
.getgid());
6093 fgname
= groupinfo
.gr_name
;
6096 except AttributeError:
6100 fcontents
= "".encode('UTF-8');
6102 fcontents
= file_content
[member
.filename
].read();
6103 fsize
= format(len(fcontents
), 'x').lower();
6105 ftypehex
= format(ftype
, 'x').lower();
6106 extrafields
= len(extradata
);
6107 extrafieldslist
= extradata
;
6108 catfextrafields
= extrafields
;
6109 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
6110 if(len(extradata
)>0):
6111 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
6112 extrasizelen
= len(extrasizestr
);
6113 extrasizelenhex
= format(extrasizelen
, 'x').lower();
6114 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()];
6115 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
6116 catoutlenhex
= format(catoutlen
, 'x').lower();
6117 catoutlist
.insert(0, catoutlenhex
);
6118 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
6119 if(len(extradata
)>0):
6120 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
6121 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
6122 catfnumfields
= 24 + catfextrafields
;
6123 if(checksumtype
=="none" or checksumtype
==""):
6124 catfileheadercshex
= format(0, 'x').lower();
6125 catfilecontentcshex
= format(0, 'x').lower();
6126 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
6127 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
6128 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
6129 elif(checksumtype
=="crc16_ccitt"):
6130 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
6131 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
6132 elif(checksumtype
=="adler32"):
6133 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
6134 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
6135 elif(checksumtype
=="crc32"):
6136 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
6137 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
6138 elif(checksumtype
=="crc64_ecma"):
6139 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6140 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
6141 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
6142 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6143 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
6144 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
6145 checksumoutstr
= hashlib
.new(checksumtype
);
6146 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
6147 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
6149 catfileheadercshex
= format(0, 'x').lower();
6150 catfilecontentcshex
= format(0, 'x').lower();
6151 catfhstart
= fheadtell
;
6152 fheadtell
+= len(catfileoutstr
);
6153 catfhend
= fheadtell
- 1;
6154 catfcontentstart
= fheadtell
;
6155 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
6156 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
6157 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
6158 catheaderdata
= [catheaersize
] + catoutlist
+ extradata
+ [checksumtype
, catfileheadercshex
, catfilecontentcshex
];
6159 if(checksumtype
=="none" or checksumtype
==""):
6160 catfileheadercshex
= format(0, 'x').lower();
6161 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
6162 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
6163 elif(checksumtype
=="crc16_ccitt"):
6164 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
6165 elif(checksumtype
=="adler32"):
6166 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
6167 elif(checksumtype
=="crc32"):
6168 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
6169 elif(checksumtype
=="crc64_ecma"):
6170 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6171 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
6172 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6173 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
6174 checksumoutstr
= hashlib
.new(checksumtype
);
6175 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
6176 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
6178 catfileheadercshex
= format(0, 'x').lower();
6179 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
6180 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
6181 nullstrecd
= formatspecs
[5].encode('UTF-8');
6182 fheadtell
+= len(catfileoutstr
) + 1;
6183 catfcontentend
= fheadtell
- 1;
6184 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
6185 pyhascontents
= False;
6186 if(int(fsize
)>0 and not listonly
):
6187 pyhascontents
= True;
6188 if(int(fsize
)>0 and listonly
):
6190 pyhascontents
= False;
6191 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
} });
6192 fileidnum
= fileidnum
+ 1;
6195 def InFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
6196 checkcompressfile
= CheckCompressionSubType(infiles
, formatspecs
, True);
6197 if(checkcompressfile
=="tarfile"):
6198 return TarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
6199 elif(checkcompressfile
=="zipfile"):
6200 return ZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
6201 elif(checkcompressfile
=="catfile"):
6202 return ArchiveFileToArray(infile
, 0, 0, listonly
, False, formatspecs
, False);
6203 elif(rarfile_support
and checkcompressfile
=="rarfile"):
6204 return RarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
6205 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
6206 return SevenZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
6211 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):
6212 outarray
= BytesIO();
6213 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
6214 listcatfiles
= ArchiveFileToArray(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6215 return listcatfiles
;
6217 def ArchiveFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
6218 if(isinstance(infile
, dict)):
6219 listcatfiles
= infile
;
6221 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6222 infile
= RemoveWindowsPath(infile
);
6223 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6224 if(not listcatfiles
):
6226 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': {}}}};
6228 catarray
.update({'catfp': listcatfiles
['catfp']});
6229 lenlist
= len(listcatfiles
['ffilelist']);
6231 lcfx
= int(listcatfiles
['fnumfiles']);
6232 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6233 lcfx
= int(lenlist
);
6235 lcfx
= int(listcatfiles
['fnumfiles']);
6237 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
6238 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
6239 catarray
['filetoid'].update(filetoidarray
);
6240 catarray
['idtofile'].update(idtofilearray
);
6241 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6242 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
6243 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
6244 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6245 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
6246 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
6247 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6248 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6249 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6250 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6251 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6252 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6253 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6254 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
6255 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
6256 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
6257 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6258 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6259 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
6260 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
6261 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
6262 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6263 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6264 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6265 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
6266 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
6267 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
6268 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6269 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6270 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6271 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6275 create_alias_function("", __file_format_name__
, "ToArrayIndex", ArchiveFileToArrayIndex
);
6277 def ListDirToArrayIndexAlt(infiles
, dirlistfromtxt
=False, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
6278 listcatfiles
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
6279 if(not listcatfiles
):
6281 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': {}}}};
6282 lenlist
= len(listcatfiles
['ffilelist']);
6284 lcfx
= int(listcatfiles
['fnumfiles']);
6285 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6286 lcfx
= int(lenlist
);
6288 lcfx
= int(listcatfiles
['fnumfiles']);
6290 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
6291 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
6292 catarray
['filetoid'].update(filetoidarray
);
6293 catarray
['idtofile'].update(idtofilearray
);
6294 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6295 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
6296 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
6297 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6298 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
6299 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
6300 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6301 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6302 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6303 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6304 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6305 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6306 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6307 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
6308 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
6309 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
6310 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6311 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6312 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
6313 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
6314 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
6315 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6316 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6317 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6318 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
6319 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
6320 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
6321 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6322 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6323 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6324 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6328 def TarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
6329 listcatfiles
= TarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
6330 if(not listcatfiles
):
6332 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': {}}}};
6333 lenlist
= len(listcatfiles
['ffilelist']);
6335 lcfx
= int(listcatfiles
['fnumfiles']);
6336 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6337 lcfx
= int(lenlist
);
6339 lcfx
= int(listcatfiles
['fnumfiles']);
6341 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
6342 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
6343 catarray
['filetoid'].update(filetoidarray
);
6344 catarray
['idtofile'].update(idtofilearray
);
6345 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6346 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
6347 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
6348 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6349 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
6350 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
6351 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6352 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6353 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6354 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6355 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6356 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6357 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6358 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
6359 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
6360 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
6361 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6362 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6363 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
6364 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
6365 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
6366 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6367 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6368 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6369 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
6370 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
6371 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
6372 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6373 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6374 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6375 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6379 def ZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
6380 listcatfiles
= ZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
6381 if(not listcatfiles
):
6383 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': {}}}};
6384 lenlist
= len(listcatfiles
['ffilelist']);
6386 lcfx
= int(listcatfiles
['fnumfiles']);
6387 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6388 lcfx
= int(lenlist
);
6390 lcfx
= int(listcatfiles
['fnumfiles']);
6392 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
6393 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
6394 catarray
['filetoid'].update(filetoidarray
);
6395 catarray
['idtofile'].update(idtofilearray
);
6396 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6397 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
6398 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
6399 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6400 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
6401 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
6402 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6403 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6404 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6405 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6406 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6407 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6408 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6409 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
6410 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
6411 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
6412 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6413 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6414 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
6415 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
6416 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
6417 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6418 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6419 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6420 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
6421 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
6422 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
6423 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6424 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6425 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6426 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6430 if(not rarfile_support
):
6431 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
6434 if(rarfile_support
):
6435 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
6436 listcatfiles
= RarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
6437 if(not listcatfiles
):
6439 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': {}}}};
6440 lenlist
= len(listcatfiles
['ffilelist']);
6442 lcfx
= int(listcatfiles
['fnumfiles']);
6443 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6444 lcfx
= int(lenlist
);
6446 lcfx
= int(listcatfiles
['fnumfiles']);
6448 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
6449 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
6450 catarray
['filetoid'].update(filetoidarray
);
6451 catarray
['idtofile'].update(idtofilearray
);
6452 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6453 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
6454 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
6455 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6456 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
6457 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
6458 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6459 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6460 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6461 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6462 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6463 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6464 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6465 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
6466 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
6467 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
6468 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6469 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6470 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
6471 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
6472 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
6473 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6474 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6475 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6476 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
6477 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
6478 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
6479 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6480 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6481 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6482 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6486 if(not py7zr_support
):
6487 def SevenZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
6491 def SevenZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
6492 listcatfiles
= SevenZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
6493 if(not listcatfiles
):
6495 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': {}}}};
6496 lenlist
= len(listcatfiles
['ffilelist']);
6498 lcfx
= int(listcatfiles
['fnumfiles']);
6499 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6500 lcfx
= int(lenlist
);
6502 lcfx
= int(listcatfiles
['fnumfiles']);
6504 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
6505 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
6506 catarray
['filetoid'].update(filetoidarray
);
6507 catarray
['idtofile'].update(idtofilearray
);
6508 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6509 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
6510 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
6511 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6512 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
6513 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
6514 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6515 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6516 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6517 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6518 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6519 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6520 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6521 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
6522 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
6523 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
6524 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6525 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6526 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
6527 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
6528 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
6529 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6530 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6531 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6532 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
6533 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
6534 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
6535 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6536 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6537 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6538 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6542 def ArchiveFileStringToArrayIndex(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
6543 catfp
= BytesIO(catstr
);
6544 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6545 return listcatfiles
;
6547 create_alias_function("", __file_format_name__
, "StringToArrayIndex", ArchiveFileStringToArrayIndex
);
6549 def TarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
6551 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
6552 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6553 return listcatfiles
;
6555 def ZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
6557 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
6558 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6559 return listcatfiles
;
6561 if(not rarfile_support
):
6562 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
6565 if(rarfile_support
):
6566 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
6568 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
6569 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6570 return listcatfiles
;
6572 if(not py7zr_support
):
6573 def SevenZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
6577 def SevenZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
6579 catfp
= PackArchiveFileFromSevenZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
6580 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6581 return listcatfiles
;
6583 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):
6584 outarray
= BytesIO();
6585 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, verbose
, True);
6586 listcatfiles
= ArchiveFileToArrayIndex(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
)
6587 return listcatfiles
;
6589 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):
6590 if(isinstance(infile
, dict)):
6591 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6592 listcatfiles
= prelistcatfiles
['list'];
6594 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6595 infile
= RemoveWindowsPath(infile
);
6597 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6598 listcatfiles
= prelistcatfiles
['list'];
6600 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6601 if(outfile
!="-" and not hasattr(infile
, "read") and not hasattr(outfile
, "write")):
6602 outfile
= RemoveWindowsPath(outfile
);
6603 checksumtype
= checksumtype
.lower();
6604 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
6605 checksumtype
="crc32";
6606 if(checksumtype
=="none"):
6608 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
6610 if(compression
not in compressionlist
and compression
is None):
6611 compression
= "auto";
6613 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6614 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
6615 if(os
.path
.exists(outfile
)):
6617 if(not listcatfiles
):
6622 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
6624 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
6627 fbasename
= os
.path
.splitext(outfile
)[0];
6628 fextname
= os
.path
.splitext(outfile
)[1];
6629 catfp
= CompressOpenFile(outfile
, compressionlevel
);
6630 catver
= formatspecs
[6];
6631 fileheaderver
= str(int(catver
.replace(".", "")));
6632 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
6633 catfp
.write(fileheader
.encode('UTF-8'));
6634 lenlist
= len(listcatfiles
['ffilelist']);
6635 fnumfiles
= int(listcatfiles
['fnumfiles']);
6636 if(lenlist
>fnumfiles
or lenlist
<fnumfiles
):
6637 fnumfiles
= lenlist
;
6638 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
6639 fnumfilesa
= AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
6640 if(checksumtype
=="none" or checksumtype
==""):
6641 catfileheadercshex
= format(0, 'x').lower();
6642 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
6643 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
6644 elif(checksumtype
=="crc16_ccitt"):
6645 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
6646 elif(checksumtype
=="adler32"):
6647 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
6648 elif(checksumtype
=="crc32"):
6649 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
6650 elif(checksumtype
=="crc64_ecma"):
6651 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6652 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
6653 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6654 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
6655 checksumoutstr
= hashlib
.new(checksumtype
);
6656 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
6657 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
6659 catfileheadercshex
= format(0, 'x').lower();
6660 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
6661 catfp
.write(fnumfilesa
.encode('UTF-8'));
6664 os
.fsync(catfp
.fileno());
6665 except io
.UnsupportedOperation
:
6667 except AttributeError:
6669 lenlist
= len(listcatfiles
['ffilelist']);
6670 fnumfiles
= int(listcatfiles
['fnumfiles']);
6672 lcfx
= int(listcatfiles
['fnumfiles']);
6673 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6674 lcfx
= int(lenlist
);
6676 lcfx
= int(listcatfiles
['fnumfiles']);
6684 catfhstart
= catfp
.tell();
6685 if(re
.findall("^[.|/]", listcatfiles
['ffilelist'][reallcfi
]['fname'])):
6686 fname
= listcatfiles
['ffilelist'][reallcfi
]['fname'];
6688 fname
= "./"+listcatfiles
['ffilelist'][reallcfi
]['fname'];
6690 VerbosePrintOut(fname
);
6691 fheadersize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fheadersize']), 'x').lower();
6692 fsize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fsize']), 'x').lower();
6693 flinkname
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
6694 fatime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fatime']), 'x').lower();
6695 fmtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmtime']), 'x').lower();
6696 fctime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fctime']), 'x').lower();
6697 fbtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fbtime']), 'x').lower();
6698 fmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmode']), 'x').lower();
6699 fchmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fchmode']), 'x').lower();
6700 fuid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fuid']), 'x').lower();
6701 funame
= listcatfiles
['ffilelist'][reallcfi
]['funame'];
6702 fgid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fgid']), 'x').lower();
6703 fgname
= listcatfiles
['ffilelist'][reallcfi
]['fgname'];
6704 finode
= listcatfiles
['ffilelist'][reallcfi
]['finode'];
6705 flinkcount
= listcatfiles
['ffilelist'][reallcfi
]['flinkcount'];
6706 fwinattributes
= flinkinfo
['ffilelist'][reallcfi
]['fwinattributes'];
6707 fdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fminor']), 'x').lower();
6708 fdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmajor']), 'x').lower();
6709 frdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frminor']), 'x').lower();
6710 frdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frmajor']), 'x').lower();
6711 if(len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] and len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>0):
6712 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
6713 if(len(extradata
)>0):
6714 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(extradata
);
6715 listcatfiles
['ffilelist'][reallcfi
]['fextralist'] = extradata
;
6716 extrafields
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']), 'x').lower();
6717 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
6718 if(len(extradata
)>0):
6719 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
6720 extrasizelen
= format(len(extrasizestr
), 'x').lower();
6721 fcontents
= listcatfiles
['ffilelist'][reallcfi
]['fcontents'];
6723 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']==1 or listcatfiles
['ffilelist'][reallcfi
]['ftype']==2):
6724 getflinkpath
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
6725 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
6726 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
6727 fheadersize
= format(int(flinkinfo
['fheadersize']), 'x').lower();
6728 fsize
= format(int(flinkinfo
['fsize']), 'x').lower();
6729 flinkname
= flinkinfo
['flinkname'];
6730 fatime
= format(int(flinkinfo
['fatime']), 'x').lower();
6731 fmtime
= format(int(flinkinfo
['fmtime']), 'x').lower();
6732 fctime
= format(int(flinkinfo
['fctime']), 'x').lower();
6733 fbtime
= format(int(flinkinfo
['fbtime']), 'x').lower();
6734 fmode
= format(int(flinkinfo
['fmode']), 'x').lower();
6735 fchmode
= format(int(flinkinfo
['fchmode']), 'x').lower();
6736 fuid
= format(int(flinkinfo
['fuid']), 'x').lower();
6737 funame
= flinkinfo
['funame'];
6738 fgid
= format(int(flinkinfo
['fgid']), 'x').lower();
6739 fgname
= flinkinfo
['fgname'];
6740 finode
= flinkinfo
['finode'];
6741 flinkcount
= flinkinfo
['flinkcount'];
6742 fwinattributes
= flinkinfo
['fwinattributes'];
6743 fdev_minor
= format(int(flinkinfo
['fminor']), 'x').lower();
6744 fdev_major
= format(int(flinkinfo
['fmajor']), 'x').lower();
6745 frdev_minor
= format(int(flinkinfo
['frminor']), 'x').lower();
6746 frdev_major
= format(int(flinkinfo
['frmajor']), 'x').lower();
6747 if(len(flinkinfo
['fextralist'])>flinkinfo
['fextrafields'] and len(flinkinfo
['fextralist'])>0):
6748 flinkinfo
['fextrafields'] = len(flinkinfo
['fextralist']);
6749 if(len(extradata
)>0):
6750 flinkinfo
['fextrafields'] = len(extradata
);
6751 flinkinfo
['fextralist'] = extradata
;
6752 extrafields
= format(int(flinkinfo
['fextrafields']), 'x').lower();
6753 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
6754 if(len(extradata
)>0):
6755 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
6756 extrasizelen
= format(len(extrasizestr
), 'x').lower();
6757 fcontents
= flinkinfo
['fcontents'];
6758 if(flinkinfo
['ftype']!=0 and flinkinfo
['ftype']!=7):
6760 fcontents
= fcontents
.encode('UTF-8');
6761 except AttributeError:
6763 ftypehex
= format(flinkinfo
['ftype'], 'x').lower();
6765 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=0 and listcatfiles
['ffilelist'][reallcfi
]['ftype']!=7):
6767 fcontents
= fcontents
.encode('UTF-8');
6768 except AttributeError:
6770 ftypehex
= format(listcatfiles
['ffilelist'][reallcfi
]['ftype'], 'x').lower();
6771 fcurfid
= format(curfid
, 'x').lower();
6772 if(not followlink
and finode
!=0):
6773 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=1):
6774 fcurinode
= format(int(curinode
), 'x').lower();
6775 inodetofile
.update({curinode
: fname
});
6776 filetoinode
.update({fname
: curinode
});
6777 curinode
= curinode
+ 1;
6779 fcurinode
= format(int(filetoinode
[flinkname
]), 'x').lower();
6781 fcurinode
= format(int(curinode
), 'x').lower();
6782 curinode
= curinode
+ 1;
6783 curfid
= curfid
+ 1;
6784 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
];
6785 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
6786 catoutlenhex
= format(catoutlen
, 'x').lower();
6787 catoutlist
.insert(0, catoutlenhex
);
6788 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
6789 if(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']>0):
6790 extrafieldslist
= [];
6792 exil
= listcatfiles
['ffilelist'][reallcfi
]['fextrafields'];
6794 extrafieldslist
.append(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
6796 catfileoutstr
+= AppendNullBytes([extrafieldslist
], formatspecs
[5]);
6797 catfileoutstr
+= AppendNullBytes([checksumtype
], formatspecs
[5]);
6798 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
6799 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
6800 if(checksumtype
=="none" or checksumtype
==""):
6801 catfileheadercshex
= format(0, 'x').lower();
6802 catfilecontentcshex
= format(0, 'x').lower();
6803 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
6804 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
6805 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
6806 elif(checksumtype
=="crc16_ccitt"):
6807 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
6808 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
6809 elif(checksumtype
=="adler32"):
6810 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
6811 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
6812 elif(checksumtype
=="crc32"):
6813 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
6814 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
6815 elif(checksumtype
=="crc64_ecma"):
6816 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6817 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
6818 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
6819 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6820 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
6821 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
6822 checksumoutstr
= hashlib
.new(checksumtype
);
6823 checksumoutstr
.update("".encode('UTF-8'));
6824 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
6825 checksumoutstr
= hashlib
.new(checksumtype
);
6826 checksumoutstr
.update(fcontents
);
6827 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
6829 catfileheadercshex
= format(0, 'x').lower();
6830 catfilecontentcshex
= format(0, 'x').lower();
6831 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
6832 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
6833 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
6834 if(checksumtype
=="none" or checksumtype
==""):
6835 catfileheadercshex
= format(0, 'x').lower();
6836 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
6837 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
6838 elif(checksumtype
=="crc16_ccitt"):
6839 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
6840 elif(checksumtype
=="adler32"):
6841 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
6842 elif(checksumtype
=="crc32"):
6843 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
6844 elif(checksumtype
=="crc64_ecma"):
6845 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6846 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
6847 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6848 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
6849 checksumoutstr
= hashlib
.new(checksumtype
);
6850 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
6851 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
6853 catfileheadercshex
= format(0, 'x').lower();
6854 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
6855 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
6856 nullstrecd
= formatspecs
[5].encode('UTF-8');
6857 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
6858 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
6859 catfp
.write(catfileout
);
6862 os
.fsync(catfp
.fileno());
6863 except io
.UnsupportedOperation
:
6865 except AttributeError:
6868 reallcfi
= reallcfi
+ 1;
6870 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
6871 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
6872 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
6875 os
.fsync(catfp
.fileno());
6876 except io
.UnsupportedOperation
:
6878 except AttributeError:
6882 if(hasattr(sys
.stdout
, "buffer")):
6883 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
6885 shutil
.copyfileobj(catfp
, sys
.stdout
);
6886 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
6887 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
6889 upload_file_to_internet_file(catfp
, outfile
);
6897 create_alias_function("RePack", __file_format_name__
, "", RePackArchiveFile
);
6899 def RePackArchiveFileFromString(catstr
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6900 catfp
= BytesIO(catstr
);
6901 listcatfiles
= RePackArchiveFile(catfp
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
6902 return listcatfiles
;
6904 create_alias_function("RePack", __file_format_name__
, "FromString", RePackArchiveFileFromString
);
6906 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):
6907 outarray
= BytesIO();
6908 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
6909 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
6910 return listcatfiles
;
6912 create_alias_function("Pack", __file_format_name__
, "FromListDir", PackArchiveFileFromListDir
);
6914 def ArchiveFileArrayBase64Encode(infile
, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6916 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6917 if(isinstance(infile
, dict)):
6918 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6919 listcatfiles
= prelistcatfiles
['list'];
6921 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6922 infile
= RemoveWindowsPath(infile
);
6924 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6925 listcatfiles
= prelistcatfiles
['list'];
6927 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6928 if(not listcatfiles
):
6930 lenlist
= len(listcatfiles
['ffilelist']);
6931 fnumfiles
= int(listcatfiles
['fnumfiles']);
6933 lcfx
= int(listcatfiles
['fnumfiles']);
6934 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6935 lcfx
= int(lenlist
);
6937 lcfx
= int(listcatfiles
['fnumfiles']);
6939 if(listcatfiles
['ffilelist'][lcfi
]['fhascontents']):
6940 listcatfiles
['ffilelist'][lcfi
]['fcontents'] = base64
.b64encode(listcatfiles
['ffilelist'][lcfi
]['fcontents']).decode("UTF-8");
6942 return listcatfiles
;
6944 create_alias_function("", __file_format_name__
, "ArrayBase64Encode", ArchiveFileArrayBase64Encode
);
6946 def ArchiveFileArrayBase64Decode(infile
, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6948 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6949 if(isinstance(infile
, dict)):
6950 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6951 listcatfiles
= prelistcatfiles
['list'];
6953 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6954 infile
= RemoveWindowsPath(infile
);
6956 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6957 listcatfiles
= prelistcatfiles
['list'];
6959 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6960 if(not listcatfiles
):
6962 lenlist
= len(listcatfiles
['ffilelist']);
6963 fnumfiles
= int(listcatfiles
['fnumfiles']);
6965 lcfx
= int(listcatfiles
['fnumfiles']);
6966 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6967 lcfx
= int(lenlist
);
6969 lcfx
= int(listcatfiles
['fnumfiles']);
6971 if(listcatfiles
['ffilelist'][lcfi
]['fhascontents']):
6972 listcatfiles
['ffilelist'][lcfi
]['fcontents'] = base64
.b64decode(listcatfiles
['ffilelist'][lcfi
]['fcontents'].encode("UTF-8"));
6974 return listcatfiles
;
6976 create_alias_function("", __file_format_name__
, "ArrayBase64Decode", ArchiveFileArrayBase64Decode
);
6978 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):
6979 if(outdir
is not None):
6980 outdir
= RemoveWindowsPath(outdir
);
6982 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6983 if(isinstance(infile
, dict)):
6984 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6985 listcatfiles
= prelistcatfiles
['list'];
6987 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6988 infile
= RemoveWindowsPath(infile
);
6990 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6991 listcatfiles
= prelistcatfiles
['list'];
6993 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6994 if(not listcatfiles
):
6996 lenlist
= len(listcatfiles
['ffilelist']);
6997 fnumfiles
= int(listcatfiles
['fnumfiles']);
6999 lcfx
= int(listcatfiles
['fnumfiles']);
7000 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
7001 lcfx
= int(lenlist
);
7003 lcfx
= int(listcatfiles
['fnumfiles']);
7009 userinfo
= pwd
.getpwuid(listcatfiles
['ffilelist'][lcfi
]['fuid']);
7010 funame
= userinfo
.pw_name
;
7019 groupinfo
= grp
.getgrgid(listcatfiles
['ffilelist'][lcfi
]['fgid']);
7020 fgname
= groupinfo
.gr_name
;
7026 VerbosePrintOut(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
7027 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
7028 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
7029 fpc
.write(listcatfiles
['ffilelist'][lcfi
]['fcontents'])
7032 os
.fsync(fpc
.fileno())
7033 except io
.UnsupportedOperation
:
7035 except AttributeError:
7037 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
7038 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
7039 if(preservepermissions
):
7040 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
7042 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
7043 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
7045 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
7046 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
7047 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
7052 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
7053 funame
= userinfo
.pw_name
;
7062 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
7063 fgname
= groupinfo
.gr_name
;
7068 if(flinkinfo
['ftype']==0 or flinkinfo
['ftype']==7):
7069 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
7070 fpc
.write(flinkinfo
['fcontents'])
7073 os
.fsync(fpc
.fileno())
7074 except io
.UnsupportedOperation
:
7076 except AttributeError:
7078 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
7079 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
7080 if(preservepermissions
):
7081 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
7083 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
7084 if(flinkinfo
['ftype']==1):
7085 os
.link(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
7086 if(flinkinfo
['ftype']==2):
7087 os
.symlink(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
7088 if(flinkinfo
['ftype']==5):
7089 if(preservepermissions
):
7090 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
7092 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
7093 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
7094 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
7095 if(preservepermissions
):
7096 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
7098 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
7099 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
7100 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
7102 os
.link(listcatfiles
['ffilelist'][lcfi
]['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
7103 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
7105 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
7106 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
7107 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
7112 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
7113 funame
= userinfo
.pw_name
;
7122 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
7123 fgname
= groupinfo
.gr_name
;
7128 if(flinkinfo
['ftype']==0 or flinkinfo
['ftype']==7):
7129 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
7130 fpc
.write(flinkinfo
['fcontents'])
7133 os
.fsync(fpc
.fileno())
7134 except io
.UnsupportedOperation
:
7136 except AttributeError:
7138 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
7139 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
7140 if(preservepermissions
):
7141 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
7143 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
7144 if(flinkinfo
['ftype']==1):
7145 os
.link(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
7146 if(flinkinfo
['ftype']==2):
7147 os
.symlink(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
7148 if(flinkinfo
['ftype']==5):
7149 if(preservepermissions
):
7150 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
7152 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
7153 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
7154 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
7155 if(preservepermissions
):
7156 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
7158 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
7159 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
7160 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
7162 os
.symlink(listcatfiles
['ffilelist'][lcfi
]['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
7163 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
7164 if(preservepermissions
):
7165 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
7167 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
7168 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
7169 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
7170 if(preservepermissions
):
7171 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
7173 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
7174 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6 and hasattr(os
, "mkfifo")):
7175 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
7178 return listcatfiles
['ffilelist']['catfp'];
7182 create_alias_function("UnPack", __file_format_name__
, "", UnPackArchiveFile
);
7184 if(hasattr(shutil
, "register_unpack_format")):
7185 def UnPackArchiveFileFunc(archive_name
, extract_dir
=None, **kwargs
):
7186 return UnPackArchiveFile(archive_name
, extract_dir
, False, 0, 0, False, __file_format_delimiter__
, False, False);
7187 create_alias_function("UnPack", __file_format_name__
, "Func", UnPackArchiveFileFunc
);
7189 def UnPackArchiveFileString(catstr
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7190 catfp
= BytesIO(catstr
);
7191 listcatfiles
= UnPackArchiveFile(catfp
, outdir
, followlink
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
7192 return listcatfiles
;
7194 create_alias_function("UnPack", __file_format_name__
, "String", UnPackArchiveFileString
);
7196 def ArchiveFileListFiles(infile
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7197 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7198 if(isinstance(infile
, dict)):
7199 listcatfiles
= infile
;
7201 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
7202 infile
= RemoveWindowsPath(infile
);
7203 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, True, skipchecksum
, formatspecs
, returnfp
);
7204 if(not listcatfiles
):
7206 lenlist
= len(listcatfiles
['ffilelist']);
7207 fnumfiles
= int(listcatfiles
['fnumfiles']);
7209 lcfx
= int(listcatfiles
['fnumfiles']);
7210 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
7211 lcfx
= int(lenlist
);
7213 lcfx
= int(listcatfiles
['fnumfiles']);
7216 returnval
.update({lcfi
: listcatfiles
['ffilelist'][lcfi
]['fname']});
7218 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
7220 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' } };
7221 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'];
7222 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
7223 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " link to " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
7224 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
7225 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " -> " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
7226 fuprint
= listcatfiles
['ffilelist'][lcfi
]['funame'];
7227 if(len(fuprint
)<=0):
7228 fuprint
= listcatfiles
['ffilelist'][lcfi
]['fuid'];
7229 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgname'];
7230 if(len(fgprint
)<=0):
7231 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgid'];
7232 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
));
7235 return listcatfiles
['catfp'];
7239 create_alias_function("", __file_format_name__
, "ListFiles", ArchiveFileListFiles
);
7241 def ArchiveFileStringListFiles(catstr
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7242 catfp
= BytesIO(catstr
);
7243 listcatfiles
= ArchiveFileListFiles(catstr
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
7244 return listcatfiles
;
7246 create_alias_function("", __file_format_name__
, "StringListFiles", ArchiveFileListFiles
);
7248 def ArchiveFileListFilesAlt(infile
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7249 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7250 if(isinstance(infile
, dict)):
7251 listcatfiles
= infile
;
7253 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
7254 infile
= RemoveWindowsPath(infile
);
7255 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, True, skipchecksum
, formatspecs
, returnfp
);
7256 if(not listcatfiles
):
7258 lenlist
= len(listcatfiles
['ffilelist']);
7259 fnumfiles
= int(listcatfiles
['fnumfiles']);
7261 lcfx
= int(listcatfiles
['fnumfiles']);
7262 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
7263 lcfx
= int(lenlist
);
7265 lcfx
= int(listcatfiles
['fnumfiles']);
7268 returnval
.update({lcfi
: listcatfiles
['ffilelist'][lcfi
]['fname']});
7270 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
7272 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' } };
7273 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'];
7274 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
7275 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " link to " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
7276 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
7277 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " -> " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
7278 fuprint
= listcatfiles
['ffilelist'][lcfi
]['funame'];
7279 if(len(fuprint
)<=0):
7280 fuprint
= listcatfiles
['ffilelist'][lcfi
]['fuid'];
7281 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgname'];
7282 if(len(fgprint
)<=0):
7283 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgid'];
7284 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
));
7287 return listcatfiles
['catfp'];
7291 create_alias_function("", __file_format_name__
, "ListFilesAlt", ArchiveFileListFilesAlt
);
7293 def ArchiveFileStringListFilesAlt(catstr
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7294 catfp
= BytesIO(catstr
);
7295 listcatfiles
= ArchiveFileListFilesAlt(catstr
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
7296 return listcatfiles
;
7298 create_alias_function("", __file_format_name__
, "StringListFilesAlt", ArchiveFileListFilesAlt
);
7300 def TarFileListFiles(infile
, verbose
=False, returnfp
=False):
7301 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7304 if(hasattr(sys
.stdin
, "buffer")):
7305 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
7307 shutil
.copyfileobj(sys
.stdin
, infile
);
7312 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
7313 infile
= download_file_from_internet_file(infile
);
7318 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
7320 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
7322 if(not tarfile
.is_tarfile(infile
)):
7324 except AttributeError:
7325 if(not is_tarfile(infile
)):
7330 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
7331 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
7333 tarfp
= tarfile
.open(infile
, "r");
7334 except FileNotFoundError
:
7338 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
7339 returnval
.update({lcfi
: member
.name
});
7340 fpremode
= member
.mode
;
7341 ffullmode
= member
.mode
;
7345 ffullmode
= member
.mode
+ stat
.S_IFREG
;
7347 elif(member
.isdev()):
7348 ffullmode
= member
.mode
;
7350 elif(member
.islnk()):
7351 ffullmode
= member
.mode
+ stat
.S_IFREG
;
7353 elif(member
.issym()):
7354 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
7356 elif(member
.ischr()):
7357 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
7359 elif(member
.isblk()):
7360 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
7362 elif(member
.isdir()):
7363 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
7365 elif(member
.isfifo()):
7366 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
7368 elif(member
.issparse()):
7369 ffullmode
= member
.mode
;
7372 VerbosePrintOut(member
.name
);
7374 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' } };
7375 printfname
= member
.name
;
7377 printfname
= member
.name
+ " link to " + member
.linkname
;
7378 elif(member
.issym()):
7379 printfname
= member
.name
+ " -> " + member
.linkname
;
7380 fuprint
= member
.uname
;
7381 if(len(fuprint
)<=0):
7382 fuprint
= member
.uid
;
7383 fgprint
= member
.gname
;
7384 if(len(fgprint
)<=0):
7385 fgprint
= member
.gid
;
7386 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
));
7389 return listcatfiles
['catfp'];
7393 def ZipFileListFiles(infile
, verbose
=False, returnfp
=False):
7394 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7397 if(hasattr(sys
.stdin
, "buffer")):
7398 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
7400 shutil
.copyfileobj(sys
.stdin
, infile
);
7405 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
7406 infile
= download_file_from_internet_file(infile
);
7411 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
7415 if(not zipfile
.is_zipfile(infile
)):
7418 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
7419 except FileNotFoundError
:
7423 ziptest
= zipfp
.testzip();
7425 VerbosePrintOut("Bad file found!");
7426 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
7427 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
7428 fwinattributes
= int(zipinfo
.external_attr
);
7429 if(not member
.is_dir()):
7430 fmode
= int(stat
.S_IFREG
+ 438);
7431 fchmode
= int(stat
.S_IMODE(fmode
));
7432 ftypemod
= int(stat
.S_IFMT(fmode
));
7433 elif(member
.is_dir()):
7434 fmode
= int(stat
.S_IFDIR
+ 511);
7435 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
7436 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
7437 elif(zipinfo
.create_system
==3):
7438 fwinattributes
=int(0);
7439 fmode
= int(zipinfo
.external_attr
);
7440 fchmode
= int(stat
.S_IMODE(fmode
));
7441 ftypemod
= int(stat
.S_IFMT(fmode
));
7443 fwinattributes
= int(0);
7444 if(not member
.is_dir()):
7445 fmode
= int(stat
.S_IFREG
+ 438);
7446 fchmode
= int(stat
.S_IMODE(fmode
));
7447 ftypemod
= int(stat
.S_IFMT(fmode
));
7448 elif(member
.is_dir()):
7449 fmode
= int(stat
.S_IFDIR
+ 511);
7450 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
7451 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
7452 returnval
.update({lcfi
: member
.filename
});
7454 VerbosePrintOut(member
.filename
);
7456 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' } };
7458 for fmodval
in str(oct(fmode
))[-3:]:
7459 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
7460 if(not member
.is_dir()):
7462 permissionstr
= "-" + permissionstr
;
7463 elif(member
.is_dir()):
7465 permissionstr
= "d" + permissionstr
;
7466 printfname
= member
.filename
;
7468 fuid
= int(os
.getuid());
7469 except AttributeError:
7474 fgid
= int(os
.getgid());
7475 except AttributeError:
7482 userinfo
= pwd
.getpwuid(os
.getuid());
7483 funame
= userinfo
.pw_name
;
7486 except AttributeError:
7494 groupinfo
= grp
.getgrgid(os
.getgid());
7495 fgname
= groupinfo
.gr_name
;
7498 except AttributeError:
7503 if(len(fuprint
)<=0):
7504 fuprint
= str(fuid
);
7506 if(len(fgprint
)<=0):
7507 fgprint
= str(fgid
);
7508 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
));
7511 return listcatfiles
['catfp'];
7515 if(not rarfile_support
):
7516 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
7517 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7518 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
7521 if(rarfile_support
):
7522 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
7523 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7524 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
7526 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
7530 rarfp
= rarfile
.RarFile(infile
, "r");
7531 rartest
= rarfp
.testrar();
7533 VerbosePrintOut("Bad file found!");
7534 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
7537 if(member
.host_os
==rarfile
.RAR_OS_UNIX
):
7540 member
.external_attr
7542 except AttributeError:
7544 elif(member
.host_os
==rarfile
.RAR_OS_WIN32
):
7547 member
.external_attr
7549 except AttributeError:
7554 if(is_unix
and member
.external_attr
!=0):
7555 fpremode
= int(member
.external_attr
);
7556 elif(member
.is_file()):
7557 fpremode
= int(stat
.S_IFREG
+ 438);
7558 elif(member
.is_symlink()):
7559 fpremode
= int(stat
.S_IFLNK
+ 438);
7560 elif(member
.is_dir()):
7561 fpremode
= int(stat
.S_IFDIR
+ 511);
7562 if(is_windows
and member
.external_attr
!=0):
7563 fwinattributes
= int(member
.external_attr
);
7565 fwinattributes
= int(0);
7566 if(is_unix
and member
.external_attr
!=0):
7567 fmode
= int(member
.external_attr
);
7568 fchmode
= int(stat
.S_IMODE(member
.external_attr
));
7569 ftypemod
= int(stat
.S_IFMT(member
.external_attr
));
7570 elif(member
.is_file()):
7571 fmode
= int(stat
.S_IFREG
+ 438);
7572 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
7573 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
7574 elif(member
.is_symlink()):
7575 fmode
= int(stat
.S_IFLNK
+ 438);
7576 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFLNK
+ 438)));
7577 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFLNK
+ 438)));
7578 elif(member
.is_dir()):
7579 fmode
= int(stat
.S_IFDIR
+ 511);
7580 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
7581 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
7582 returnval
.update({lcfi
: member
.filename
});
7584 VerbosePrintOut(member
.filename
);
7586 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' } };
7588 for fmodval
in str(oct(fmode
))[-3:]:
7589 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
7590 if(member
.is_file()):
7592 permissionstr
= "-" + permissionstr
;
7593 printfname
= member
.filename
;
7594 elif(member
.is_symlink()):
7596 permissionstr
= "l" + permissionstr
;
7597 printfname
= member
.name
+ " -> " + member
.read().decode("UTF-8");
7598 elif(member
.is_dir()):
7600 permissionstr
= "d" + permissionstr
;
7601 printfname
= member
.filename
;
7603 fuid
= int(os
.getuid());
7604 except AttributeError:
7609 fgid
= int(os
.getgid());
7610 except AttributeError:
7617 userinfo
= pwd
.getpwuid(os
.getuid());
7618 funame
= userinfo
.pw_name
;
7621 except AttributeError:
7629 groupinfo
= grp
.getgrgid(os
.getgid());
7630 fgname
= groupinfo
.gr_name
;
7633 except AttributeError:
7638 if(len(fuprint
)<=0):
7639 fuprint
= str(fuid
);
7641 if(len(fgprint
)<=0):
7642 fgprint
= str(fgid
);
7643 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(member
.file_size
).rjust(15) + " " + member
.mtime
.strftime('%Y-%m-%d %H:%M') + " " + printfname
));
7646 return listcatfiles
['catfp'];
7650 if(not py7zr_support
):
7651 def SevenZipFileListFiles(infile
, verbose
=False, returnfp
=False):
7652 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7653 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
7657 def SevenZipFileListFiles(infile
, verbose
=False, returnfp
=False):
7658 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7659 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
7663 szpfp
= py7zr
.SevenZipFile(infile
, mode
="r");
7664 file_content
= szpfp
.readall();
7665 #sztest = szpfp.testzip();
7666 sztestalt
= szpfp
.test();
7668 VerbosePrintOut("Bad file found!");
7669 for member
in sorted(szpfp
.list(), key
=lambda x
: x
.filename
):
7670 if(re
.findall("^[.|/]", member
.filename
)):
7671 fname
= member
.filename
;
7673 fname
= "./"+member
.filename
;
7674 if(not member
.is_directory
):
7675 fpremode
= int(stat
.S_IFREG
+ 438);
7676 elif(member
.is_directory
):
7677 fpremode
= int(stat
.S_IFDIR
+ 511);
7678 fwinattributes
= int(0);
7679 if(member
.is_directory
):
7680 fmode
= int(stat
.S_IFDIR
+ 511);
7681 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
7682 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
7684 fmode
= int(stat
.S_IFLNK
+ 438);
7685 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFLNK
+ 438)));
7686 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFLNK
+ 438)));
7687 returnval
.update({lcfi
: member
.filename
});
7689 VerbosePrintOut(member
.filename
);
7691 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' } };
7693 for fmodval
in str(oct(fmode
))[-3:]:
7694 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
7696 if(not member
.is_directory
):
7698 permissionstr
= "-" + permissionstr
;
7699 printfname
= member
.filename
;
7700 elif(member
.is_directory
):
7702 permissionstr
= "d" + permissionstr
;
7703 printfname
= member
.filename
;
7705 fsize
= len(file_content
[member
.filename
].read());
7706 file_content
[member
.filename
].close();
7708 fuid
= int(os
.getuid());
7709 except AttributeError:
7714 fgid
= int(os
.getgid());
7715 except AttributeError:
7722 userinfo
= pwd
.getpwuid(os
.getuid());
7723 funame
= userinfo
.pw_name
;
7726 except AttributeError:
7734 groupinfo
= grp
.getgrgid(os
.getgid());
7735 fgname
= groupinfo
.gr_name
;
7738 except AttributeError:
7743 if(len(fuprint
)<=0):
7744 fuprint
= str(fuid
);
7746 if(len(fgprint
)<=0):
7747 fgprint
= str(fgid
);
7748 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(fsize
).rjust(15) + " " + member
.creationtime
.strftime('%Y-%m-%d %H:%M') + " " + printfname
));
7751 return listcatfiles
['catfp'];
7755 def InFileListFiles(infile
, verbose
=False, formatspecs
=__file_format_list__
, returnfp
=False):
7756 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7757 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
7758 if(checkcompressfile
=="tarfile"):
7759 return TarFileListFiles(infile
, verbose
, returnfp
);
7760 elif(checkcompressfile
=="zipfile"):
7761 return ZipFileListFiles(infile
, verbose
, returnfp
);
7762 elif(checkcompressfile
=="catfile"):
7763 return ArchiveFileListFilesAlt(infile
, 0, 0, False, formatspecs
, verbose
, returnfp
):
7764 elif(rarfile_support
and checkcompressfile
=="rarfile"):
7765 return RarFileListFiles(infile
, verbose
, returnfp
);
7766 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
7767 return SevenZipFileListFiles(infile
, verbose
, returnfp
);
7772 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):
7773 outarray
= BytesIO();
7774 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, False, True);
7775 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
7776 return listcatfiles
;
7778 def ListDirListFilesAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=False, seekstart
=0, seekend
=0, skipchecksum
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7779 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, formatspecs
, verbose
);
7780 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
7781 return listcatfiles
;
7783 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):
7784 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, False, checksumtype
, extradata
, formatspecs
, False);
7785 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, followlink
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
7786 return listcatfiles
;
7788 create_alias_function("Pack", __file_format_name__
, "FromListDirAlt", PackArchiveFileFromListDirAlt
);
7790 def PackArchiveFileFromTarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7791 outarray
= TarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
7792 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
7793 return listcatfiles
;
7795 create_alias_function("Pack", __file_format_name__
, "FromTarFileAlt", PackArchiveFileFromTarFileAlt
);
7797 def PackArchiveFileFromZipFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7798 outarray
= ZipFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
7799 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
7800 return listcatfiles
;
7802 create_alias_function("Pack", __file_format_name__
, "FromZipFileAlt", PackArchiveFileFromZipFileAlt
);
7804 if(not rarfile_support
):
7805 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7808 if(rarfile_support
):
7809 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7810 outarray
= RarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
7811 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
7812 return listcatfiles
;
7814 create_alias_function("Pack", __file_format_name__
, "FromRarFileAlt", PackArchiveFileFromRarFileAlt
);
7816 if(not py7zr_support
):
7817 def PackArchiveFileFromSevenZipFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7821 def PackArchiveFileFromSevenZipFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7822 outarray
= SevenZipFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
7823 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
7824 return listcatfiles
;
7826 create_alias_function("Pack", __file_format_name__
, "FromSevenZipFileAlt", PackArchiveFileFromSevenZipFileAlt
);
7828 def download_file_from_ftp_file(url
):
7829 urlparts
= urlparse(url
);
7830 file_name
= os
.path
.basename(urlparts
.path
);
7831 file_dir
= os
.path
.dirname(urlparts
.path
);
7832 if(urlparts
.username
is not None):
7833 ftp_username
= urlparts
.username
;
7835 ftp_username
= "anonymous";
7836 if(urlparts
.password
is not None):
7837 ftp_password
= urlparts
.password
;
7838 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7839 ftp_password
= "anonymous";
7842 if(urlparts
.scheme
=="ftp"):
7844 elif(urlparts
.scheme
=="ftps"):
7848 if(urlparts
.scheme
=="sftp"):
7850 return download_file_from_pysftp_file(url
);
7852 return download_file_from_sftp_file(url
);
7853 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7854 return download_file_from_http_file(url
);
7855 ftp_port
= urlparts
.port
;
7856 if(urlparts
.port
is None):
7859 ftp
.connect(urlparts
.hostname
, ftp_port
);
7860 except socket
.gaierror
:
7861 log
.info("Error With URL "+url
);
7863 except socket
.timeout
:
7864 log
.info("Error With URL "+url
);
7866 ftp
.login(urlparts
.username
, urlparts
.password
);
7867 if(urlparts
.scheme
=="ftps"):
7869 ftpfile
= BytesIO();
7870 ftp
.retrbinary("RETR "+urlparts
.path
, ftpfile
.write
);
7871 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
7876 def download_file_from_ftp_string(url
):
7877 ftpfile
= download_file_from_ftp_file(url
);
7878 return ftpfile
.read();
7880 def upload_file_to_ftp_file(ftpfile
, url
):
7881 urlparts
= urlparse(url
);
7882 file_name
= os
.path
.basename(urlparts
.path
);
7883 file_dir
= os
.path
.dirname(urlparts
.path
);
7884 if(urlparts
.username
is not None):
7885 ftp_username
= urlparts
.username
;
7887 ftp_username
= "anonymous";
7888 if(urlparts
.password
is not None):
7889 ftp_password
= urlparts
.password
;
7890 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7891 ftp_password
= "anonymous";
7894 if(urlparts
.scheme
=="ftp"):
7896 elif(urlparts
.scheme
=="ftps"):
7900 if(urlparts
.scheme
=="sftp"):
7902 return upload_file_from_pysftp_file(url
);
7904 return upload_file_from_sftp_file(url
);
7905 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7907 ftp_port
= urlparts
.port
;
7908 if(urlparts
.port
is None):
7911 ftp
.connect(urlparts
.hostname
, ftp_port
);
7912 except socket
.gaierror
:
7913 log
.info("Error With URL "+url
);
7915 except socket
.timeout
:
7916 log
.info("Error With URL "+url
);
7918 ftp
.login(urlparts
.username
, urlparts
.password
);
7919 if(urlparts
.scheme
=="ftps"):
7921 ftp
.storbinary("STOR "+urlparts
.path
, ftpfile
);
7926 def upload_file_to_ftp_string(ftpstring
, url
):
7927 ftpfileo
= BytesIO(ftpstring
);
7928 ftpfile
= upload_file_to_ftp_file(ftpfileo
, url
);
7932 def download_file_from_http_file(url
, headers
=geturls_headers_pycatfile_python_alt
):
7933 # Parse the URL to extract username and password if present
7934 urlparts
= urlparse(url
);
7935 username
= urlparts
.username
;
7936 password
= urlparts
.password
;
7937 # Rebuild the URL without the username and password
7938 netloc
= urlparts
.hostname
;
7939 if(urlparts
.scheme
=="sftp"):
7941 return download_file_from_pysftp_file(url
);
7943 return download_file_from_sftp_file(url
);
7944 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7945 return download_file_from_ftp_file(url
);
7947 netloc
+= ':' + str(urlparts
.port
);
7948 rebuilt_url
= urlunparse((urlparts
.scheme
, netloc
, urlparts
.path
, urlparts
.params
, urlparts
.query
, urlparts
.fragment
));
7949 # Create a temporary file object
7950 httpfile
= BytesIO();
7952 # Use the requests library if available
7953 if username
and password
:
7954 response
= requests
.get(rebuilt_url
, headers
=headers
, auth
=(username
, password
), stream
=True);
7956 response
= requests
.get(rebuilt_url
, headers
=headers
, stream
=True);
7957 response
.raw
.decode_content
= True
7958 shutil
.copyfileobj(response
.raw
, httpfile
);
7960 # Build a Request object for urllib
7961 request
= Request(rebuilt_url
, headers
=headers
);
7962 # Create an opener object for handling URLs
7963 if username
and password
:
7964 # Create a password manager
7965 password_mgr
= HTTPPasswordMgrWithDefaultRealm();
7966 # Add the username and password
7967 password_mgr
.add_password(None, rebuilt_url
, username
, password
);
7968 # Create an authentication handler using the password manager
7969 auth_handler
= HTTPBasicAuthHandler(password_mgr
);
7970 # Build the opener with the authentication handler
7971 opener
= build_opener(auth_handler
);
7973 opener
= build_opener();
7974 with opener
.open(request
) as response
:
7975 shutil
.copyfileobj(response
, httpfile
);
7976 # Reset file pointer to the start
7977 httpfile
.seek(0, 0);
7978 # Return the temporary file object
7981 def download_file_from_http_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
7982 httpfile
= download_file_from_http_file(url
, headers
);
7983 return ftpfile
.read();
7986 def download_file_from_sftp_file(url
):
7987 urlparts
= urlparse(url
);
7988 file_name
= os
.path
.basename(urlparts
.path
);
7989 file_dir
= os
.path
.dirname(urlparts
.path
);
7990 sftp_port
= urlparts
.port
;
7991 if(urlparts
.port
is None):
7994 sftp_port
= urlparts
.port
;
7995 if(urlparts
.username
is not None):
7996 sftp_username
= urlparts
.username
;
7998 sftp_username
= "anonymous";
7999 if(urlparts
.password
is not None):
8000 sftp_password
= urlparts
.password
;
8001 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
8002 sftp_password
= "anonymous";
8005 if(urlparts
.scheme
=="ftp"):
8006 return download_file_from_ftp_file(url
);
8007 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
8008 return download_file_from_http_file(url
);
8009 if(urlparts
.scheme
!="sftp"):
8011 ssh
= paramiko
.SSHClient();
8012 ssh
.load_system_host_keys();
8013 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
8015 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
8016 except paramiko
.ssh_exception
.SSHException
:
8018 except socket
.gaierror
:
8019 log
.info("Error With URL "+url
);
8021 except socket
.timeout
:
8022 log
.info("Error With URL "+url
);
8024 sftp
= ssh
.open_sftp();
8025 sftpfile
= BytesIO();
8026 sftp
.getfo(urlparts
.path
, sftpfile
);
8029 sftpfile
.seek(0, 0);
8032 def download_file_from_sftp_file(url
):
8036 def download_file_from_sftp_string(url
):
8037 sftpfile
= download_file_from_sftp_file(url
);
8038 return sftpfile
.read();
8040 def download_file_from_ftp_string(url
):
8044 def upload_file_to_sftp_file(sftpfile
, url
):
8045 urlparts
= urlparse(url
);
8046 file_name
= os
.path
.basename(urlparts
.path
);
8047 file_dir
= os
.path
.dirname(urlparts
.path
);
8048 sftp_port
= urlparts
.port
;
8049 if(urlparts
.port
is None):
8052 sftp_port
= urlparts
.port
;
8053 if(urlparts
.username
is not None):
8054 sftp_username
= urlparts
.username
;
8056 sftp_username
= "anonymous";
8057 if(urlparts
.password
is not None):
8058 sftp_password
= urlparts
.password
;
8059 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
8060 sftp_password
= "anonymous";
8063 if(urlparts
.scheme
=="ftp"):
8064 return upload_file_from_ftp_file(url
);
8065 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
8067 if(urlparts
.scheme
!="sftp"):
8069 ssh
= paramiko
.SSHClient();
8070 ssh
.load_system_host_keys();
8071 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
8073 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
8074 except paramiko
.ssh_exception
.SSHException
:
8076 except socket
.gaierror
:
8077 log
.info("Error With URL "+url
);
8079 except socket
.timeout
:
8080 log
.info("Error With URL "+url
);
8082 sftp
= ssh
.open_sftp();
8083 sftp
.putfo(sftpfile
, urlparts
.path
);
8086 sftpfile
.seek(0, 0);
8089 def upload_file_to_sftp_file(sftpfile
, url
):
8093 def upload_file_to_sftp_string(sftpstring
, url
):
8094 sftpfileo
= BytesIO(sftpstring
);
8095 sftpfile
= upload_file_to_sftp_files(ftpfileo
, url
);
8099 def upload_file_to_sftp_string(url
):
8103 def download_file_from_pysftp_file(url
):
8104 urlparts
= urlparse(url
);
8105 file_name
= os
.path
.basename(urlparts
.path
);
8106 file_dir
= os
.path
.dirname(urlparts
.path
);
8107 sftp_port
= urlparts
.port
;
8108 if(urlparts
.port
is None):
8111 sftp_port
= urlparts
.port
;
8112 if(urlparts
.username
is not None):
8113 sftp_username
= urlparts
.username
;
8115 sftp_username
= "anonymous";
8116 if(urlparts
.password
is not None):
8117 sftp_password
= urlparts
.password
;
8118 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
8119 sftp_password
= "anonymous";
8122 if(urlparts
.scheme
=="ftp"):
8123 return download_file_from_ftp_file(url
);
8124 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
8125 return download_file_from_http_file(url
);
8126 if(urlparts
.scheme
!="sftp"):
8129 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
8130 except paramiko
.ssh_exception
.SSHException
:
8132 except socket
.gaierror
:
8133 log
.info("Error With URL "+url
);
8135 except socket
.timeout
:
8136 log
.info("Error With URL "+url
);
8138 sftp
= ssh
.open_sftp();
8139 sftpfile
= BytesIO();
8140 sftp
.getfo(urlparts
.path
, sftpfile
);
8143 sftpfile
.seek(0, 0);
8146 def download_file_from_pysftp_file(url
):
8150 def download_file_from_pysftp_string(url
):
8151 sftpfile
= download_file_from_pysftp_file(url
);
8152 return sftpfile
.read();
8154 def download_file_from_ftp_string(url
):
8158 def upload_file_to_pysftp_file(sftpfile
, url
):
8159 urlparts
= urlparse(url
);
8160 file_name
= os
.path
.basename(urlparts
.path
);
8161 file_dir
= os
.path
.dirname(urlparts
.path
);
8162 sftp_port
= urlparts
.port
;
8163 if(urlparts
.port
is None):
8166 sftp_port
= urlparts
.port
;
8167 if(urlparts
.username
is not None):
8168 sftp_username
= urlparts
.username
;
8170 sftp_username
= "anonymous";
8171 if(urlparts
.password
is not None):
8172 sftp_password
= urlparts
.password
;
8173 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
8174 sftp_password
= "anonymous";
8177 if(urlparts
.scheme
=="ftp"):
8178 return upload_file_from_ftp_file(url
);
8179 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
8181 if(urlparts
.scheme
!="sftp"):
8184 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
8185 except paramiko
.ssh_exception
.SSHException
:
8187 except socket
.gaierror
:
8188 log
.info("Error With URL "+url
);
8190 except socket
.timeout
:
8191 log
.info("Error With URL "+url
);
8193 sftp
= ssh
.open_sftp();
8194 sftp
.putfo(sftpfile
, urlparts
.path
);
8197 sftpfile
.seek(0, 0);
8200 def upload_file_to_pysftp_file(sftpfile
, url
):
8204 def upload_file_to_pysftp_string(sftpstring
, url
):
8205 sftpfileo
= BytesIO(sftpstring
);
8206 sftpfile
= upload_file_to_pysftp_files(ftpfileo
, url
);
8210 def upload_file_to_pysftp_string(url
):
8213 def download_file_from_internet_file(url
, headers
=geturls_headers_pycatfile_python_alt
):
8214 urlparts
= urlparse(url
);
8215 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
8216 return download_file_from_http_file(url
, headers
);
8217 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
8218 return download_file_from_ftp_file(url
);
8219 elif(urlparts
.scheme
=="sftp"):
8220 if(__use_pysftp__
and havepysftp
):
8221 return download_file_from_pysftp_file(url
);
8223 return download_file_from_sftp_file(url
);
8228 def download_file_from_internet_uncompress_file(url
, headers
=geturls_headers_pycatfile_python_alt
, formatspecs
=__file_format_list__
):
8229 fp
= download_file_from_internet_file(url
);
8230 fp
= UncompressArchiveFile(fp
, formatspecs
);
8236 def download_file_from_internet_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
8237 urlparts
= urlparse(url
);
8238 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
8239 return download_file_from_http_string(url
, headers
);
8240 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
8241 return download_file_from_ftp_string(url
);
8242 elif(urlparts
.scheme
=="sftp"):
8243 if(__use_pysftp__
and havepysftp
):
8244 return download_file_from_pysftp_string(url
);
8246 return download_file_from_sftp_string(url
);
8251 def download_file_from_internet_uncompress_string(url
, headers
=geturls_headers_pycatfile_python_alt
, formatspecs
=__file_format_list__
):
8252 fp
= download_file_from_internet_string(url
);
8253 fp
= UncompressArchiveFile(fp
, formatspecs
);
8259 def upload_file_to_internet_file(ifp
, url
):
8260 urlparts
= urlparse(url
);
8261 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
8263 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
8264 return upload_file_from_ftp_file(ifp
, url
);
8265 elif(urlparts
.scheme
=="sftp"):
8266 if(__use_pysftp__
and havepysftp
):
8267 return upload_file_from_pysftp_file(ifp
, url
);
8269 return download_file_from_sftp_file(ifp
, url
);
8274 def upload_file_to_internet_compress_file(ifp
, url
, formatspecs
=__file_format_list__
):
8275 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
8279 upload_file_to_internet_file(catfp
, outfile
);
8282 def upload_file_to_internet_string(ifp
, url
):
8283 urlparts
= urlparse(url
);
8284 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
8286 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
8287 return upload_file_from_ftp_string(ifp
, url
);
8288 elif(urlparts
.scheme
=="sftp"):
8289 if(__use_pysftp__
and havepysftp
):
8290 return upload_file_from_pysftp_string(ifp
, url
);
8292 return download_file_from_sftp_string(ifp
, url
);
8297 def upload_file_to_internet_compress_string(ifp
, url
, formatspecs
=__file_format_list__
):
8298 catfp
= CompressArchiveFile(BytesIO(ifp
), compression
, formatspecs
);
8302 upload_file_to_internet_file(catfp
, outfile
);
8306 if(hasattr(shutil
, "register_archive_format")):
8307 # Register the packing format
8308 shutil
.register_archive_format(__file_format_name__
, PackArchiveFileFunc
, description
='Pack concatenated files');
8309 except shutil
.RegistryError
:
8313 if(hasattr(shutil
, "register_unpack_format")):
8314 # Register the unpacking format
8315 shutil
.register_unpack_format(__file_format_name__
, archivefile_extensions
, UnPackArchiveFileFunc
, description
='UnPack concatenated files');
8316 except shutil
.RegistryError
: