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: 11/17/2024 Ver. 0.14.4 RC 1 - Author: cooldude2k $
20 from __future__
import absolute_import
, division
, print_function
, unicode_literals
, generators
, with_statement
, nested_scopes
38 from backports
import tempfile
44 from ftplib
import FTP
, FTP_TLS
47 from ftplib
import FTP
53 import simplejson
as json
61 import SafeConfigParser
as configparser
63 import ConfigParser
as configparser
73 baseint
.insert(0, int)
76 baseint
= tuple(baseint
)
80 from urllib
.parse
import urlparse
, urlunparse
82 from urlparse
import urlparse
, urlunparse
84 # Windows-specific setup
86 if sys
.version_info
[0] == 2:
88 sys
.stdout
= codecs
.getwriter('utf-8')(sys
.stdout
)
89 sys
.stderr
= codecs
.getwriter('utf-8')(sys
.stderr
)
91 sys
.stdout
= io
.TextIOWrapper(
92 sys
.stdout
.buffer, encoding
='utf-8', errors
='replace', line_buffering
=True)
93 sys
.stderr
= io
.TextIOWrapper(
94 sys
.stderr
.buffer, encoding
='utf-8', errors
='replace', line_buffering
=True)
96 hashlib_guaranteed
= False
98 os
.environ
["PYTHONIOENCODING"] = "UTF-8"
99 os
.environ
["LC_CTYPE"] = "UTF-8"
101 # Reload sys to set default encoding to UTF-8 (Python 2 only)
102 if sys
.version_info
[0] == 2:
105 sys
.setdefaultencoding('UTF-8')
106 except (NameError, AttributeError):
111 from zlib
import crc32
113 from binascii
import crc32
115 # Define FileNotFoundError for Python 2
119 FileNotFoundError
= IOError
122 rarfile_support
= False
125 rarfile_support
= True
130 py7zr_support
= False
139 from xtarfile
import is_tarfile
142 from safetar
import is_tarfile
144 from tarfile
import is_tarfile
148 import xtarfile
as tarfile
151 import safetar
as tarfile
177 logging
.getLogger("urllib3").setLevel(logging
.WARNING
)
186 logging
.getLogger("httpx").setLevel(logging
.WARNING
)
187 logging
.getLogger("httpcore").setLevel(logging
.WARNING
)
191 # HTTP and URL parsing
193 from urllib
.request
import Request
, build_opener
, HTTPBasicAuthHandler
194 from urllib
.parse
import urlparse
196 from urllib2
import Request
, build_opener
, HTTPBasicAuthHandler
197 from urlparse
import urlparse
199 # StringIO and BytesIO
201 from io
import StringIO
, BytesIO
204 from cStringIO
import StringIO
205 from cStringIO
import StringIO
as BytesIO
207 from StringIO
import StringIO
208 from StringIO
import StringIO
as BytesIO
210 __use_pysftp__
= False
211 __use_alt_format__
= False
212 scriptconf
= os
.path
.join(os
.path
.dirname(get_importing_script_path()), "catfile.ini")
213 if os
.path
.exists(scriptconf
):
214 __config_file__
= scriptconf
216 __config_file__
= os
.path
.join(os
.path
.dirname(os
.path
.realpath(__file__
)), "catfile.ini")
217 __use_ini_file__
= True
219 __use_pysftp__
= False
220 __use_http_lib__
= "httpx"
221 if(__use_http_lib__
== "httpx" and haverequests
and not havehttpx
):
222 __use_http_lib__
= "requests"
223 if(__use_http_lib__
== "requests" and havehttpx
and not haverequests
):
224 __use_http_lib__
= "httpx"
225 if((__use_http_lib__
== "httpx" or __use_http_lib__
== "requests") and not havehttpx
and not haverequests
):
226 __use_http_lib__
= "urllib"
227 if os
.path
.exists(__config_file__
) and __use_ini_file__
:
228 # Create a ConfigParser object
229 config
= configparser
.ConfigParser()
230 # Read the configuration file
231 config
.read(__config_file__
)
232 # Accessing values from the config file
233 __file_format_name__
= config
['main']['name']
234 __program_name__
= config
['main']['proname']
235 __file_format_lower__
= config
['main']['lower']
236 __file_format_magic__
= config
['main']['magic'].encode("UTF-8").decode('unicode_escape')
237 __file_format_len__
= int(config
['main']['len'])
238 __file_format_hex__
= config
['main']['hex']
239 __file_format_delimiter__
= config
['main']['delimiter'].encode("UTF-8").decode('unicode_escape')
240 __file_format_ver__
= config
['main']['ver']
241 __use_new_style__
= config
.getboolean('main', 'newstyle')
242 __use_advanced_list__
= config
.getboolean('main', 'advancedlist')
243 __use_alt_inode__
= config
.getboolean('main', 'altinode')
244 __file_format_extension__
= config
['main']['extension']
246 if not __use_alt_format__
:
247 # Format Info by Kazuki Przyborowski
248 __file_format_name__
= "CatFile"
249 __program_name__
= "Py" + __file_format_name__
250 __file_format_lower__
= __file_format_name__
.lower()
251 __file_format_magic__
= __file_format_name__
252 __file_format_len__
= len(__file_format_magic__
)
253 __file_format_hex__
= binascii
.hexlify(
254 __file_format_magic__
.encode("UTF-8")).decode("UTF-8")
255 __file_format_delimiter__
= "\x00"
256 __file_format_ver__
= "001"
257 __use_new_style__
= True
258 __use_advanced_list__
= True
259 __use_alt_inode__
= False
260 __file_format_extension__
= ".cat"
262 # Format Info Generated by ChatGPT
263 __file_format_name__
= "FastArchive"
264 __program_name__
= "Py" + __file_format_name__
265 __file_format_lower__
= __file_format_name__
.lower()
266 __file_format_magic__
= "FstArch"
267 __file_format_len__
= len(__file_format_magic__
)
268 __file_format_hex__
= binascii
.hexlify(
269 __file_format_magic__
.encode("UTF-8")).decode("UTF-8")
270 # Using a non-printable ASCII character as delimiter
271 __file_format_delimiter__
= "\x1F"
272 __file_format_ver__
= "001"
273 __use_new_style__
= True
274 __use_advanced_list__
= False
275 __use_alt_inode__
= False
276 __file_format_extension__
= ".fast"
277 __file_format_list__
= [__file_format_name__
, __file_format_magic__
, __file_format_lower__
, __file_format_len__
,
278 __file_format_hex__
, __file_format_delimiter__
, __file_format_ver__
, __use_new_style__
, __use_advanced_list__
, __use_alt_inode__
]
279 __file_format_dict__
= {'format_name': __file_format_name__
, 'format_magic': __file_format_magic__
, 'format_lower': __file_format_lower__
, 'format_len': __file_format_len__
, 'format_hex': __file_format_hex__
,
280 'format_delimiter': __file_format_delimiter__
, 'format_ver': __file_format_ver__
, 'new_style': __use_new_style__
, 'use_advanced_list': __use_advanced_list__
, 'use_alt_inode': __use_alt_inode__
}
281 __project__
= __program_name__
282 __project_url__
= "https://github.com/GameMaker2k/PyCatFile"
283 __version_info__
= (0, 14, 4, "RC 1", 1)
284 __version_date_info__
= (2024, 11, 17, "RC 1", 1)
285 __version_date__
= str(__version_date_info__
[0]) + "." + str(
286 __version_date_info__
[1]).zfill(2) + "." + str(__version_date_info__
[2]).zfill(2)
287 __revision__
= __version_info__
[3]
288 __revision_id__
= "$Id$"
289 if(__version_info__
[4] is not None):
290 __version_date_plusrc__
= __version_date__
+ \
291 "-" + str(__version_date_info__
[4])
292 if(__version_info__
[4] is None):
293 __version_date_plusrc__
= __version_date__
294 if(__version_info__
[3] is not None):
295 __version__
= str(__version_info__
[0]) + "." + str(__version_info__
[
296 1]) + "." + str(__version_info__
[2]) + " " + str(__version_info__
[3])
297 if(__version_info__
[3] is None):
298 __version__
= str(__version_info__
[
299 0]) + "." + str(__version_info__
[1]) + "." + str(__version_info__
[2])
301 PyBitness
= platform
.architecture()
302 if(PyBitness
== "32bit" or PyBitness
== "32"):
304 elif(PyBitness
== "64bit" or PyBitness
== "64"):
309 geturls_ua_pycatfile_python
= "Mozilla/5.0 (compatible; {proname}/{prover}; +{prourl})".format(
310 proname
=__project__
, prover
=__version__
, prourl
=__project_url__
)
311 if(platform
.python_implementation() != ""):
312 py_implementation
= platform
.python_implementation()
313 if(platform
.python_implementation() == ""):
314 py_implementation
= "Python"
315 geturls_ua_pycatfile_python_alt
= "Mozilla/5.0 ({osver}; {archtype}; +{prourl}) {pyimp}/{pyver} (KHTML, like Gecko) {proname}/{prover}".format(osver
=platform
.system(
316 )+" "+platform
.release(), archtype
=platform
.machine(), prourl
=__project_url__
, pyimp
=py_implementation
, pyver
=platform
.python_version(), proname
=__project__
, prover
=__version__
)
317 geturls_ua_googlebot_google
= "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
318 geturls_ua_googlebot_google_old
= "Googlebot/2.1 (+http://www.google.com/bot.html)"
319 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",
320 '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
)}
321 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",
322 '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
)}
323 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",
324 '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"}
325 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",
326 '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"}
328 compressionsupport
= []
331 compressionsupport
.append("gz")
332 compressionsupport
.append("gzip")
337 compressionsupport
.append("bz2")
338 compressionsupport
.append("bzip2")
344 compressionsupport
.append("lz4")
349 compressionsupport
.append("lzo")
350 compressionsupport
.append("lzop")
355 compressionsupport
.append("zstd")
356 compressionsupport
.append("zstandard")
361 compressionsupport
.append("lzma")
362 compressionsupport
.append("xz")
365 from backports
import lzma
366 compressionsupport
.append("lzma")
367 compressionsupport
.append("xz")
370 compressionsupport
.append("zlib")
372 compressionlist
= ['auto']
373 compressionlistalt
= []
376 if('gzip' in compressionsupport
):
377 compressionlist
.append('gzip')
378 compressionlistalt
.append('gzip')
379 outextlist
.append('gz')
380 outextlistwd
.append('.gz')
381 if('bzip2' in compressionsupport
):
382 compressionlist
.append('bzip2')
383 compressionlistalt
.append('bzip2')
384 outextlist
.append('bz2')
385 outextlistwd
.append('.bz2')
386 if('zstd' in compressionsupport
):
387 compressionlist
.append('zstd')
388 compressionlistalt
.append('zstd')
389 outextlist
.append('zst')
390 outextlistwd
.append('.zst')
391 if('lz4' in compressionsupport
):
392 compressionlist
.append('lz4')
393 compressionlistalt
.append('lz4')
394 outextlist
.append('lz4')
395 outextlistwd
.append('.lz4')
396 if('lzo' in compressionsupport
):
397 compressionlist
.append('lzo')
398 compressionlistalt
.append('lzo')
399 outextlist
.append('lzo')
400 outextlistwd
.append('.lzo')
401 if('lzop' in compressionsupport
):
402 compressionlist
.append('lzop')
403 compressionlistalt
.append('lzop')
404 outextlist
.append('lzop')
405 outextlistwd
.append('.lzop')
406 if('lzma' in compressionsupport
):
407 compressionlist
.append('lzma')
408 compressionlistalt
.append('lzma')
409 outextlist
.append('lzma')
410 outextlistwd
.append('.lzma')
411 if('xz' in compressionsupport
):
412 compressionlist
.append('xz')
413 compressionlistalt
.append('xz')
414 outextlist
.append('xz')
415 outextlistwd
.append('.xz')
416 if('zlib' in compressionsupport
):
417 compressionlist
.append('zlib')
418 compressionlistalt
.append('zlib')
419 outextlist
.append('zz')
420 outextlistwd
.append('.zz')
421 outextlist
.append('zl')
422 outextlistwd
.append('.zl')
423 outextlist
.append('zlib')
424 outextlistwd
.append('.zlib')
426 tarfile_mimetype
= "application/tar"
427 tarfile_tar_mimetype
= tarfile_mimetype
428 zipfile_mimetype
= "application/zip"
429 zipfile_zip_mimetype
= zipfile_mimetype
430 rarfile_mimetype
= "application/rar"
431 rarfile_rar_mimetype
= rarfile_mimetype
432 archivefile_mimetype
= "application/x-"+__file_format_dict__
['format_lower']+""
433 archivefile_cat_mimetype
= archivefile_mimetype
434 archivefile_gzip_mimetype
= "application/x-" + \
435 __file_format_dict__
['format_lower']+"+gzip"
436 archivefile_gz_mimetype
= archivefile_gzip_mimetype
437 archivefile_bzip2_mimetype
= "application/x-" + \
438 __file_format_dict__
['format_lower']+"+bzip2"
439 archivefile_bz2_mimetype
= archivefile_bzip2_mimetype
440 archivefile_lz4_mimetype
= "application/x-" + \
441 __file_format_dict__
['format_lower']+"+lz4"
442 archivefile_lzop_mimetype
= "application/x-" + \
443 __file_format_dict__
['format_lower']+"+lzop"
444 archivefile_lzo_mimetype
= archivefile_lzop_mimetype
445 archivefile_zstandard_mimetype
= "application/x-" + \
446 __file_format_dict__
['format_lower']+"+zstandard"
447 archivefile_zstd_mimetype
= archivefile_zstandard_mimetype
448 archivefile_lzma_mimetype
= "application/x-" + \
449 __file_format_dict__
['format_lower']+"+lzma"
450 archivefile_xz_mimetype
= "application/x-" + \
451 __file_format_dict__
['format_lower']+"+xz"
452 archivefile_zlib_mimetype
= "application/x-" + \
453 __file_format_dict__
['format_lower']+"+zlib"
454 archivefile_zz_mimetype
= archivefile_zlib_mimetype
455 archivefile_zl_mimetype
= archivefile_zlib_mimetype
456 archivefile_extensions
= [__file_format_extension__
, __file_format_extension__
+".gz", __file_format_extension__
+".bz2", __file_format_extension__
+".zst", __file_format_extension__
+".lz4", __file_format_extension__
+
457 ".lzo", __file_format_extension__
+".lzop", __file_format_extension__
+".lzma", __file_format_extension__
+".xz", __file_format_extension__
+".zz", __file_format_extension__
+".zl", __file_format_extension__
+".zlib"]
459 if __name__
== "__main__":
461 curscrpath
= os
.path
.dirname(sys
.argv
[0])
462 if(curscrpath
== ""):
465 curscrpath
= curscrpath
.replace(os
.sep
, "/")
466 curscrpath
= curscrpath
+ "/"
467 scrfile
= curscrpath
+ "catfile.py"
468 if(os
.path
.exists(scrfile
) and os
.path
.isfile(scrfile
)):
469 scrcmd
= subprocess
.Popen([sys
.executable
, scrfile
] + sys
.argv
[1:])
473 def VerbosePrintOut(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
479 "warning": logging
.warning
,
480 "error": logging
.error
,
481 "critical": logging
.critical
,
482 "exception": logging
.exception
,
483 "logalt": lambda x
: logging
.log(dgblevel
, x
),
484 "debug": logging
.debug
486 log_function
= log_functions
.get(outtype
)
493 def VerbosePrintOutReturn(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
494 VerbosePrintOut(dbgtxt
, outtype
, dbgenable
, dgblevel
)
498 def RemoveWindowsPath(dpath
):
502 dpath
= dpath
.replace(os
.path
.sep
, "/")
503 dpath
= dpath
.rstrip("/")
504 if(dpath
== "." or dpath
== ".."):
509 def NormalizeRelativePath(inpath
):
510 inpath
= RemoveWindowsPath(inpath
)
511 if(os
.path
.isabs(inpath
)):
514 if(inpath
.startswith("./") or inpath
.startswith("../")):
517 outpath
= "./" + inpath
521 def PrependPath(base_dir
, child_path
):
522 # Check if base_dir is None or empty, if so, return child_path as is
525 # Ensure base_dir ends with exactly one slash
526 if not base_dir
.endswith('/'):
528 # Check if child_path starts with ./ or ../ (indicating a relative path)
529 if child_path
.startswith('./') or child_path
.startswith('../'):
530 # For relative paths, we don't alter the child_path
531 return base_dir
+ child_path
533 # For non-relative paths, ensure there's no starting slash on child_path to avoid double slashes
534 return base_dir
+ child_path
.lstrip('/')
537 def ListDir(dirpath
, followlink
=False, duplicates
=False):
538 if isinstance(dirpath
, (list, tuple, )):
539 dirpath
= list(filter(None, dirpath
))
540 elif isinstance(dirpath
, str):
541 dirpath
= list(filter(None, [dirpath
]))
543 fs_encoding
= sys
.getfilesystemencoding()
544 for mydirfile
in dirpath
:
545 if not os
.path
.exists(mydirfile
):
547 mydirfile
= NormalizeRelativePath(mydirfile
)
548 if os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
):
549 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
))
550 if os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
):
551 for root
, dirs
, filenames
in os
.walk(mydirfile
):
553 dpath
= RemoveWindowsPath(dpath
)
554 if fs_encoding
!= 'utf-8':
555 dpath
= dpath
.encode(fs_encoding
).decode('utf-8')
556 if dpath
not in retlist
and not duplicates
:
557 retlist
.append(dpath
)
559 retlist
.append(dpath
)
560 for file in filenames
:
561 fpath
= os
.path
.join(root
, file)
562 fpath
= RemoveWindowsPath(fpath
)
563 if fs_encoding
!= 'utf-8':
564 fpath
= fpath
.encode(fs_encoding
).decode('utf-8')
565 if fpath
not in retlist
and not duplicates
:
566 retlist
.append(fpath
)
568 retlist
.append(fpath
)
570 path
= RemoveWindowsPath(mydirfile
)
571 if fs_encoding
!= 'utf-8':
572 path
= path
.encode(fs_encoding
).decode('utf-8')
577 def ListDirAdvanced(dirpath
, followlink
=False, duplicates
=False):
578 if isinstance(dirpath
, (list, tuple, )):
579 dirpath
= list(filter(None, dirpath
))
580 elif isinstance(dirpath
, str):
581 dirpath
= list(filter(None, [dirpath
]))
583 fs_encoding
= sys
.getfilesystemencoding()
584 for mydirfile
in dirpath
:
585 if not os
.path
.exists(mydirfile
):
587 mydirfile
= NormalizeRelativePath(mydirfile
)
588 if os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
) and followlink
:
589 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
))
590 if os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
):
591 for root
, dirs
, filenames
in os
.walk(mydirfile
):
592 # Sort dirs and filenames alphabetically in place
593 dirs
.sort(key
=lambda x
: x
.lower())
594 filenames
.sort(key
=lambda x
: x
.lower())
595 dpath
= RemoveWindowsPath(root
)
596 if fs_encoding
!= 'utf-8':
597 dpath
= dpath
.encode(fs_encoding
).decode('utf-8')
598 if not duplicates
and dpath
not in retlist
:
599 retlist
.append(dpath
)
601 retlist
.append(dpath
)
602 for file in filenames
:
603 fpath
= os
.path
.join(root
, file)
604 fpath
= RemoveWindowsPath(fpath
)
605 if fs_encoding
!= 'utf-8':
606 fpath
= fpath
.encode(fs_encoding
).decode('utf-8')
607 if not duplicates
and fpath
not in retlist
:
608 retlist
.append(fpath
)
610 retlist
.append(fpath
)
612 path
= RemoveWindowsPath(mydirfile
)
613 if fs_encoding
!= 'utf-8':
614 path
= path
.encode(fs_encoding
).decode('utf-8')
619 def create_alias_function(prefix
, base_name
, suffix
, target_function
):
620 # Define a new function that wraps the target function
621 def alias_function(*args
, **kwargs
):
622 return target_function(*args
, **kwargs
)
623 # Create the function name by combining the prefix, base name, and the suffix
624 function_name
= "{}{}{}".format(prefix
, base_name
, suffix
)
625 # Add the new function to the global namespace
626 globals()[function_name
] = alias_function
629 def create_alias_function_alt(prefix
, base_name
, suffix
, target_function
):
630 # Create the function name by combining the prefix, base name, and the suffix
631 # Use the format method for string formatting, compatible with Python 2 and 3
632 function_name
= "{}{}{}".format(prefix
, base_name
, suffix
)
633 # Add the new function (alias of the target_function) to the global namespace
634 # This line is compatible as-is with both Python 2 and 3
635 globals()[function_name
] = target_function
638 def FormatSpecsListToDict(formatspecs
=__file_format_list__
):
639 if(isinstance(formatspecs
, (list, tuple, ))):
640 return {'format_name': formatspecs
[0], 'format_magic': formatspecs
[1], 'format_lower': formatspecs
[2], 'format_len': formatspecs
[3], 'format_hex': formatspecs
[4], 'format_delimiter': formatspecs
[5], 'format_ver': formatspecs
[6], 'new_style': formatspecs
[7], 'use_advanced_list': formatspecs
[8], 'use_alt_inode': formatspecs
[9]}
641 elif(isinstance(formatspecs
, (dict, ))):
644 return __file_format_dict__
645 return __file_format_dict__
649 def __init__(self
, file_path
=None, fileobj
=None, mode
='rb', level
=9, wbits
=15, encoding
=None, errors
=None, newline
=None):
650 if file_path
is None and fileobj
is None:
651 raise ValueError("Either file_path or fileobj must be provided")
652 if file_path
is not None and fileobj
is not None:
654 "Only one of file_path or fileobj should be provided")
656 self
.file_path
= file_path
657 self
.fileobj
= fileobj
661 self
.encoding
= encoding
663 self
.newline
= newline
664 self
._compressed
_data
= b
''
665 self
._decompressed
_data
= b
''
667 self
._text
_mode
= 't' in mode
669 # Force binary mode for internal handling
670 internal_mode
= mode
.replace('t', 'b')
672 if 'w' in mode
or 'a' in mode
or 'x' in mode
:
674 file_path
, internal_mode
) if file_path
else fileobj
675 self
._compressor
= zlib
.compressobj(level
, zlib
.DEFLATED
, wbits
)
678 if os
.path
.exists(file_path
):
679 self
.file = open(file_path
, internal_mode
)
682 raise FileNotFoundError(
683 "No such file: '{}'".format(file_path
))
688 raise ValueError("Mode should be 'rb' or 'wb'")
690 def _load_file(self
):
692 self
._compressed
_data
= self
.file.read()
693 if not self
._compressed
_data
.startswith((b
'\x78\x01', b
'\x78\x5E', b
'\x78\x9C', b
'\x78\xDA')):
694 raise ValueError("Invalid zlib file header")
695 self
._decompressed
_data
= zlib
.decompress(
696 self
._compressed
_data
, self
.wbits
)
698 self
._decompressed
_data
= self
._decompressed
_data
.decode(
699 self
.encoding
or 'utf-8', self
.errors
or 'strict')
701 def write(self
, data
):
703 data
= data
.encode(self
.encoding
or 'utf-8',
704 self
.errors
or 'strict')
705 compressed_data
= self
._compressor
.compress(
706 data
) + self
._compressor
.flush(zlib
.Z_SYNC_FLUSH
)
707 self
.file.write(compressed_data
)
709 def read(self
, size
=-1):
711 size
= len(self
._decompressed
_data
) - self
._position
712 data
= self
._decompressed
_data
[self
._position
:self
._position
+ size
]
713 self
._position
+= size
716 def seek(self
, offset
, whence
=0):
717 if whence
== 0: # absolute file positioning
718 self
._position
= offset
719 elif whence
== 1: # seek relative to the current position
720 self
._position
+= offset
721 elif whence
== 2: # seek relative to the file's end
722 self
._position
= len(self
._decompressed
_data
) + offset
724 raise ValueError("Invalid value for whence")
726 # Ensure the position is within bounds
727 self
._position
= max(
728 0, min(self
._position
, len(self
._decompressed
_data
)))
731 return self
._position
737 if hasattr(self
.file, 'fileno'):
738 return self
.file.fileno()
739 raise OSError("The underlying file object does not support fileno()")
742 if hasattr(self
.file, 'isatty'):
743 return self
.file.isatty()
746 def truncate(self
, size
=None):
747 if hasattr(self
.file, 'truncate'):
748 return self
.file.truncate(size
)
749 raise OSError("The underlying file object does not support truncate()")
752 if 'w' in self
.mode
or 'a' in self
.mode
or 'x' in self
.mode
:
753 self
.file.write(self
._compressor
.flush(zlib
.Z_FINISH
))
760 def __exit__(self
, exc_type
, exc_value
, traceback
):
765 def __init__(self
, file_path
=None, fileobj
=None, mode
='rb', compresslevel
=9, encoding
=None, errors
=None, newline
=None):
766 if file_path
is None and fileobj
is None:
767 raise ValueError("Either file_path or fileobj must be provided")
768 if file_path
is not None and fileobj
is not None:
770 "Only one of file_path or fileobj should be provided")
772 self
.file_path
= file_path
773 self
.fileobj
= fileobj
775 self
.compresslevel
= compresslevel
776 self
.encoding
= encoding
778 self
.newline
= newline
779 self
._compressed
_data
= b
''
780 self
._decompressed
_data
= b
''
782 self
._text
_mode
= 't' in mode
784 # Force binary mode for internal handling
785 internal_mode
= mode
.replace('t', 'b')
787 if 'w' in mode
or 'a' in mode
or 'x' in mode
:
788 self
.file = gzip
.open(file_path
, internal_mode
, compresslevel
=compresslevel
) if file_path
else gzip
.GzipFile(
789 fileobj
=fileobj
, mode
=internal_mode
, compresslevel
=compresslevel
)
790 self
._compressor
= gzip
.GzipFile(
791 fileobj
=self
.file, mode
=internal_mode
, compresslevel
=compresslevel
)
794 if os
.path
.exists(file_path
):
795 self
.file = gzip
.open(file_path
, internal_mode
)
798 raise FileNotFoundError(
799 "No such file: '{}'".format(file_path
))
801 self
.file = gzip
.GzipFile(fileobj
=fileobj
, mode
=internal_mode
)
804 raise ValueError("Mode should be 'rb' or 'wb'")
806 def _load_file(self
):
808 self
._compressed
_data
= self
.file.read()
809 if not self
._compressed
_data
.startswith(b
'\x1f\x8b'):
810 raise ValueError("Invalid gzip file header")
811 self
._decompressed
_data
= gzip
.decompress(self
._compressed
_data
)
813 self
._decompressed
_data
= self
._decompressed
_data
.decode(
814 self
.encoding
or 'utf-8', self
.errors
or 'strict')
816 def write(self
, data
):
818 data
= data
.encode(self
.encoding
or 'utf-8',
819 self
.errors
or 'strict')
820 compressed_data
= self
._compressor
.compress(data
)
821 self
.file.write(compressed_data
)
824 def read(self
, size
=-1):
826 size
= len(self
._decompressed
_data
) - self
._position
827 data
= self
._decompressed
_data
[self
._position
:self
._position
+ size
]
828 self
._position
+= size
831 def seek(self
, offset
, whence
=0):
832 if whence
== 0: # absolute file positioning
833 self
._position
= offset
834 elif whence
== 1: # seek relative to the current position
835 self
._position
+= offset
836 elif whence
== 2: # seek relative to the file's end
837 self
._position
= len(self
._decompressed
_data
) + offset
839 raise ValueError("Invalid value for whence")
841 # Ensure the position is within bounds
842 self
._position
= max(
843 0, min(self
._position
, len(self
._decompressed
_data
)))
846 return self
._position
852 if hasattr(self
.file, 'fileno'):
853 return self
.file.fileno()
854 raise OSError("The underlying file object does not support fileno()")
857 if hasattr(self
.file, 'isatty'):
858 return self
.file.isatty()
861 def truncate(self
, size
=None):
862 if hasattr(self
.file, 'truncate'):
863 return self
.file.truncate(size
)
864 raise OSError("The underlying file object does not support truncate()")
867 if 'w' in self
.mode
or 'a' in self
.mode
or 'x' in self
.mode
:
868 self
.file.write(self
._compressor
.flush())
875 def __exit__(self
, exc_type
, exc_value
, traceback
):
881 def __init__(self, file_path=None, fileobj=None, mode='rb', level=9, encoding=None, errors=None, newline=None):
882 if file_path is None and fileobj is None:
883 raise ValueError("Either file_path or fileobj must be provided");
884 if file_path is not None and fileobj is not None:
885 raise ValueError("Only one of file_path or fileobj should be provided");
887 self.file_path = file_path;
888 self.fileobj = fileobj;
891 self.encoding = encoding;
892 self.errors = errors;
893 self.newline = newline;
894 self._compressed_data = b'';
895 self._decompressed_data = b'';
897 self._text_mode = 't' in mode;
899 # Force binary mode for internal handling
900 internal_mode = mode.replace('t', 'b');
902 if 'w' in mode or 'a' in mode or 'x' in mode:
903 self.file = open(file_path, internal_mode) if file_path else fileobj;
904 self._compressor = blosc.Blosc(level);
907 if os.path.exists(file_path):
908 self.file = open(file_path, internal_mode);
911 raise FileNotFoundError("No such file: '{}'".format(file_path));
916 raise ValueError("Mode should be 'rb' or 'wb'");
918 def _load_file(self):
920 self._compressed_data = self.file.read();
921 if not self._compressed_data:
922 raise ValueError("Invalid blosc file header");
923 self._decompressed_data = blosc.decompress(self._compressed_data);
925 self._decompressed_data = self._decompressed_data.decode(self.encoding or 'utf-8', self.errors or 'strict');
927 def write(self, data):
929 data = data.encode(self.encoding or 'utf-8', self.errors or 'strict');
930 compressed_data = blosc.compress(data, cname='blosclz', clevel=self.level);
931 self.file.write(compressed_data);
934 def read(self, size=-1):
936 size = len(self._decompressed_data) - self._position;
937 data = self._decompressed_data[self._position:self._position + size];
938 self._position += size;
941 def seek(self, offset, whence=0):
942 if whence == 0: # absolute file positioning
943 self._position = offset;
944 elif whence == 1: # seek relative to the current position
945 self._position += offset;
946 elif whence == 2: # seek relative to the file's end
947 self._position = len(self._decompressed_data) + offset;
949 raise ValueError("Invalid value for whence");
951 # Ensure the position is within bounds
952 self._position = max(0, min(self._position, len(self._decompressed_data)));
955 return self._position;
961 if hasattr(self.file, 'fileno'):
962 return self.file.fileno();
963 raise OSError("The underlying file object does not support fileno()");
966 if hasattr(self.file, 'isatty'):
967 return self.file.isatty();
970 def truncate(self, size=None):
971 if hasattr(self.file, 'truncate'):
972 return self.file.truncate(size);
973 raise OSError("The underlying file object does not support truncate()");
976 if 'w' in self.mode or 'a' in self.mode or 'x' in self.mode:
977 self.file.write(blosc.compress(self._compressor.flush(), cname='blosclz', clevel=self.level));
984 def __exit__(self, exc_type, exc_value, traceback):
988 def __init__(self, file_path=None, fileobj=None, mode='rb', level=11, encoding=None, errors=None, newline=None):
989 if file_path is None and fileobj is None:
990 raise ValueError("Either file_path or fileobj must be provided");
991 if file_path is not None and fileobj is not None:
992 raise ValueError("Only one of file_path or fileobj should be provided");
994 self.file_path = file_path;
995 self.fileobj = fileobj;
998 self.encoding = encoding;
999 self.errors = errors;
1000 self.newline = newline;
1001 self._compressed_data = b'';
1002 self._decompressed_data = b'';
1004 self._text_mode = 't' in mode;
1006 # Force binary mode for internal handling
1007 internal_mode = mode.replace('t', 'b');
1009 if 'w' in mode or 'a' in mode or 'x' in mode:
1010 self.file = open(file_path, internal_mode) if file_path else fileobj;
1011 self._compressor = brotli.Compressor(quality=self.level);
1014 if os.path.exists(file_path):
1015 self.file = open(file_path, internal_mode);
1018 raise FileNotFoundError("No such file: '{}'".format(file_path));
1020 self.file = fileobj;
1023 raise ValueError("Mode should be 'rb' or 'wb'");
1025 def _load_file(self):
1027 self._compressed_data = self.file.read();
1028 if not self._compressed_data:
1029 raise ValueError("Invalid brotli file header");
1030 self._decompressed_data = brotli.decompress(self._compressed_data);
1032 self._decompressed_data = self._decompressed_data.decode(self.encoding or 'utf-8', self.errors or 'strict');
1034 def write(self, data):
1036 data = data.encode(self.encoding or 'utf-8', self.errors or 'strict');
1037 compressed_data = self._compressor.process(data);
1038 self.file.write(compressed_data);
1041 def read(self, size=-1):
1043 size = len(self._decompressed_data) - self._position;
1044 data = self._decompressed_data[self._position:self._position + size];
1045 self._position += size;
1048 def seek(self, offset, whence=0):
1049 if whence == 0: # absolute file positioning
1050 self._position = offset;
1051 elif whence == 1: # seek relative to the current position
1052 self._position += offset;
1053 elif whence == 2: # seek relative to the file's end
1054 self._position = len(self._decompressed_data) + offset;
1056 raise ValueError("Invalid value for whence");
1058 # Ensure the position is within bounds
1059 self._position = max(0, min(self._position, len(self._decompressed_data)));
1062 return self._position;
1068 if hasattr(self.file, 'fileno'):
1069 return self.file.fileno();
1070 raise OSError("The underlying file object does not support fileno()");
1073 if hasattr(self.file, 'isatty'):
1074 return self.file.isatty();
1077 def truncate(self, size=None):
1078 if hasattr(self.file, 'truncate'):
1079 return self.file.truncate(size);
1080 raise OSError("The underlying file object does not support truncate()");
1083 if 'w' in self.mode or 'a' in self.mode or 'x' in self.mode:
1084 self.file.write(self._compressor.finish());
1088 def __enter__(self):
1091 def __exit__(self, exc_type, exc_value, traceback):
1096 def TarFileCheck(infile
):
1098 if is_tarfile(infile
):
1104 # Check if the input is a file object
1105 if hasattr(infile
, 'read'):
1106 # Save the current file position
1107 current_position
= infile
.tell()
1108 # Attempt to open the file object as a tar file
1109 tar
= tarfile
.open(fileobj
=infile
)
1111 # Restore the file position
1112 infile
.seek(current_position
)
1115 # Assume it's a filename
1116 tar
= tarfile
.open(name
=infile
)
1119 except tarfile
.TarError
:
1123 def TarFileCheckAlt(infile
):
1125 if is_tarfile(infile
):
1130 # Check if the input is a file-like object
1131 if hasattr(infile
, 'read'):
1132 # Save the current file position
1133 current_position
= infile
.tell()
1134 # Attempt to open the file object as a tar file
1135 with tarfile
.open(fileobj
=infile
) as tar
:
1137 # Restore the file position
1138 infile
.seek(current_position
)
1140 # Assume it's a filename and attempt to open it as a tar file
1141 with tarfile
.open(name
=infile
) as tar
:
1144 except (tarfile
.TarError
, AttributeError, IOError):
1148 def ZipFileCheck(infile
):
1150 if zipfile
.is_zipfile(infile
):
1155 # Check if the input is a file-like object
1156 if hasattr(infile
, 'read'):
1157 # Save the current file position
1158 current_position
= infile
.tell()
1159 # Attempt to open the file object as a zip file
1160 with zipfile
.ZipFile(infile
) as zipf
:
1162 # Restore the file position
1163 infile
.seek(current_position
)
1165 # Assume it's a filename and attempt to open it as a zip file
1166 with zipfile
.ZipFile(infile
) as zipf
:
1169 except (zipfile
.BadZipFile
, AttributeError, IOError):
1173 def RarFileCheck(infile
):
1175 if rarfile
.is_rarfile(infile
):
1180 # Check if the input is a file-like object
1181 if hasattr(infile
, 'read'):
1182 # Save the current file position
1183 current_position
= infile
.tell()
1184 # Attempt to open the file object as a rar file
1185 with rarfile
.RarFile(infile
) as rarf
:
1187 # Restore the file position
1188 infile
.seek(current_position
)
1190 # Assume it's a filename and attempt to open it as a rar file
1191 with rarfile
.RarFile(infile
) as rarf
:
1194 except (rarfile
.Error
, AttributeError, IOError):
1198 def SevenZipFileCheck(infile
):
1200 # Check if the input is a file-like object
1201 if hasattr(infile
, 'read'):
1202 # Save the current file position
1203 current_position
= infile
.tell()
1204 # Attempt to open the file object as a 7z file
1205 with py7zr
.SevenZipFile(infile
, 'r') as archive
:
1207 # Restore the file position
1208 infile
.seek(current_position
)
1210 # Assume it's a filename and attempt to open it as a 7z file
1211 with py7zr
.SevenZipFile(infile
, 'r') as archive
:
1214 except (py7zr
.Bad7zFile
, AttributeError, IOError):
1217 # initial_value can be 0xFFFF or 0x0000
1220 def crc16_ansi(msg
, initial_value
=0xFFFF):
1221 # CRC-16-IBM / CRC-16-ANSI polynomial and initial value
1222 poly
= 0x8005 # Polynomial for CRC-16-IBM / CRC-16-ANSI
1223 crc
= initial_value
# Initial value
1225 crc ^
= b
<< 8 # XOR byte into CRC top byte
1226 for _
in range(8): # Process each bit
1227 if crc
& 0x8000: # If the top bit is set
1228 # Shift left and XOR with the polynomial
1229 crc
= (crc
<< 1) ^ poly
1231 crc
= crc
<< 1 # Just shift left
1232 crc
&= 0xFFFF # Ensure CRC remains 16-bit
1235 # initial_value can be 0xFFFF or 0x0000
1238 def crc16_ibm(msg
, initial_value
=0xFFFF):
1239 return crc16_ansi(msg
, initial_value
)
1241 # initial_value is 0xFFFF
1245 return crc16_ansi(msg
, 0xFFFF)
1247 # initial_value can be 0xFFFF, 0x1D0F or 0x0000
1250 def crc16_ccitt(msg
, initial_value
=0xFFFF):
1251 # CRC-16-CCITT polynomial
1252 poly
= 0x1021 # Polynomial for CRC-16-CCITT
1253 # Use the specified initial value
1256 crc ^
= b
<< 8 # XOR byte into CRC top byte
1257 for _
in range(8): # Process each bit
1258 if crc
& 0x8000: # If the top bit is set
1259 # Shift left and XOR with the polynomial
1260 crc
= (crc
<< 1) ^ poly
1262 crc
= crc
<< 1 # Just shift left
1263 crc
&= 0xFFFF # Ensure CRC remains 16-bit
1266 # initial_value can be 0x42F0E1EBA9EA3693 or 0x0000000000000000
1269 def crc64_ecma(msg
, initial_value
=0x0000000000000000):
1270 # CRC-64-ECMA polynomial and initial value
1271 poly
= 0x42F0E1EBA9EA3693
1272 crc
= initial_value
# Initial value for CRC-64-ECMA
1274 crc ^
= b
<< 56 # XOR byte into the most significant byte of the CRC
1275 for _
in range(8): # Process each bit
1276 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
1277 # Shift left and XOR with poly if the MSB is 1
1278 crc
= (crc
<< 1) ^ poly
1280 crc
<<= 1 # Just shift left if the MSB is 0
1281 crc
&= 0xFFFFFFFFFFFFFFFF # Ensure CRC remains 64-bit
1284 # initial_value can be 0x000000000000001B or 0xFFFFFFFFFFFFFFFF
1287 def crc64_iso(msg
, initial_value
=0xFFFFFFFFFFFFFFFF):
1288 # CRC-64-ISO polynomial and initial value
1289 poly
= 0x000000000000001B
1290 crc
= initial_value
# Common initial value for CRC-64-ISO
1292 crc ^
= b
<< 56 # XOR byte into the most significant byte of the CRC
1293 for _
in range(8): # Process each bit
1294 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
1295 # Shift left and XOR with poly if the MSB is 1
1296 crc
= (crc
<< 1) ^ poly
1298 crc
<<= 1 # Just shift left if the MSB is 0
1299 crc
&= 0xFFFFFFFFFFFFFFFF # Ensure CRC remains 64-bit
1303 def GetDataFromArray(data
, path
, default
=None):
1307 element
= element
[key
]
1309 except (KeyError, TypeError, IndexError):
1313 def GetDataFromArrayAlt(structure
, path
, default
=None):
1316 if isinstance(element
, dict) and key
in element
:
1317 element
= element
[key
]
1318 elif isinstance(element
, list) and isinstance(key
, int) and -len(element
) <= key
< len(element
):
1319 element
= element
[key
]
1325 def GetHeaderChecksum(inlist
=[], checksumtype
="crc32", encodedata
=True, formatspecs
=__file_format_dict__
):
1326 formatspecs
= FormatSpecsListToDict(formatspecs
)
1327 fileheader
= AppendNullBytes(inlist
, formatspecs
['format_delimiter']) if isinstance(
1328 inlist
, list) else AppendNullByte(inlist
, formatspecs
['format_delimiter'])
1330 fileheader
= fileheader
.encode('UTF-8')
1331 checksum_methods
= {
1332 "crc16": lambda data
: format(crc16(data
) & 0xffff, '04x').lower(),
1333 "crc16_ansi": lambda data
: format(crc16(data
) & 0xffff, '04x').lower(),
1334 "crc16_ibm": lambda data
: format(crc16(data
) & 0xffff, '04x').lower(),
1335 "crc16_ccitt": lambda data
: format(crc16_ccitt(data
) & 0xffff, '04x').lower(),
1336 "adler32": lambda data
: format(zlib
.adler32(data
) & 0xffffffff, '08x').lower(),
1337 "crc32": lambda data
: format(crc32(data
) & 0xffffffff, '08x').lower(),
1338 "crc64_ecma": lambda data
: format(crc64_ecma(data
) & 0xffffffffffffffff, '016x').lower(),
1339 "crc64": lambda data
: format(crc64_iso(data
) & 0xffffffffffffffff, '016x').lower(),
1340 "crc64_iso": lambda data
: format(crc64_iso(data
) & 0xffffffffffffffff, '016x').lower(),
1342 if checksumtype
in checksum_methods
:
1343 return checksum_methods
[checksumtype
](fileheader
)
1344 elif CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
):
1345 checksumoutstr
= hashlib
.new(checksumtype
)
1346 checksumoutstr
.update(fileheader
)
1347 return checksumoutstr
.hexdigest().lower()
1348 return format(0, 'x').lower()
1351 def GetFileChecksum(instr
, checksumtype
="crc32", encodedata
=True, formatspecs
=__file_format_dict__
):
1352 formatspecs
= FormatSpecsListToDict(formatspecs
)
1354 instr
= instr
.encode('UTF-8')
1355 checksum_methods
= {
1356 "crc16": lambda data
: format(crc16(data
) & 0xffff, '04x').lower(),
1357 "crc16_ansi": lambda data
: format(crc16(data
) & 0xffff, '04x').lower(),
1358 "crc16_ibm": lambda data
: format(crc16(data
) & 0xffff, '04x').lower(),
1359 "crc16_ccitt": lambda data
: format(crc16_ccitt(data
) & 0xffff, '04x').lower(),
1360 "adler32": lambda data
: format(zlib
.adler32(data
) & 0xffffffff, '08x').lower(),
1361 "crc32": lambda data
: format(crc32(data
) & 0xffffffff, '08x').lower(),
1362 "crc64_ecma": lambda data
: format(crc64_ecma(data
) & 0xffffffffffffffff, '016x').lower(),
1363 "crc64": lambda data
: format(crc64_iso(data
) & 0xffffffffffffffff, '016x').lower(),
1364 "crc64_iso": lambda data
: format(crc64_iso(data
) & 0xffffffffffffffff, '016x').lower(),
1366 if checksumtype
in checksum_methods
:
1367 return checksum_methods
[checksumtype
](instr
)
1368 elif CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
):
1369 checksumoutstr
= hashlib
.new(checksumtype
)
1370 checksumoutstr
.update(instr
)
1371 return checksumoutstr
.hexdigest().lower()
1372 return format(0, 'x').lower()
1375 def ValidateHeaderChecksum(inlist
=[], checksumtype
="crc32", inchecksum
="0", formatspecs
=__file_format_dict__
):
1376 formatspecs
= FormatSpecsListToDict(formatspecs
)
1377 catfileheadercshex
= GetHeaderChecksum(
1378 inlist
, checksumtype
, True, formatspecs
).lower()
1379 return inchecksum
.lower() == catfileheadercshex
1382 def ValidateFileChecksum(infile
, checksumtype
="crc32", inchecksum
="0", formatspecs
=__file_format_dict__
):
1383 formatspecs
= FormatSpecsListToDict(formatspecs
)
1384 catinfilecshex
= GetFileChecksum(
1385 infile
, checksumtype
, True, formatspecs
).lower()
1386 return inchecksum
.lower() == catinfilecshex
1389 def ReadTillNullByteOld(fp
, delimiter
=__file_format_dict__
['format_delimiter']):
1392 nullbyte
= delimiter
.encode("UTF-8")
1394 curbyte
= fp
.read(1)
1395 if(curbyte
== nullbyte
or not curbyte
):
1397 curfullbyte
= curfullbyte
+ curbyte
1398 return curfullbyte
.decode('UTF-8')
1401 def ReadUntilNullByteOld(fp
, delimiter
=__file_format_dict__
['format_delimiter']):
1402 return ReadTillNullByteOld(fp
, delimiter
)
1405 def ReadTillNullByteAlt(fp
, delimiter
=__file_format_dict__
['format_delimiter'], chunk_size
=1024, max_read
=1024000):
1406 delimiter
= delimiter
.encode('UTF-8') # Ensure the delimiter is in bytes
1407 buffer = bytearray()
1409 delimiter_length
= len(delimiter
)
1411 chunk
= fp
.read(chunk_size
)
1413 # End of file reached without finding the delimiter
1415 buffer.extend(chunk
)
1416 total_read
+= len(chunk
)
1417 if delimiter
in buffer:
1418 # Delimiter found, calculate where to reset the file pointer
1419 index
= buffer.find(delimiter
)
1420 # Calculate how many extra bytes were read after the delimiter
1421 extra_bytes_read
= len(buffer) - (index
+ delimiter_length
)
1422 # Move the file pointer back to just after the delimiter
1423 fp
.seek(-extra_bytes_read
, 1)
1424 buffer = buffer[:index
]
1426 if total_read
>= max_read
:
1427 # Stop reading if max limit is reached to prevent excessive memory usage
1429 "Maximum read limit reached without finding the delimiter.")
1430 # Check for incomplete UTF-8 sequences at the end of the buffer
1431 if len(buffer) > 1 and 128 <= buffer[-1] < 192:
1432 # This suggests that the last byte might be the start of a multi-byte character
1433 # Try to read one more byte to complete the character
1434 extra_byte
= fp
.read(1)
1436 buffer.extend(extra_byte
)
1438 # No more data available
1441 return buffer.decode('UTF-8', errors
='replace')
1442 except UnicodeDecodeError:
1443 return buffer.decode('UTF-8', errors
='replace')
1446 def ReadUntilNullByteAlt(fp
, delimiter
=__file_format_dict__
['format_delimiter'], chunk_size
=1024, max_read
=1024000):
1447 return ReadTillNullByteAlt(fp
, delimiter
, chunk_size
, max_read
)
1450 def ReadTillNullByte(fp
, delimiter
=__file_format_dict__
['format_delimiter'], max_read
=1024000):
1451 curfullbyte
= bytearray()
1452 nullbyte
= delimiter
.encode("UTF-8")
1453 total_read
= 0 # Track the total number of bytes read
1455 curbyte
= fp
.read(1)
1456 if curbyte
== nullbyte
or not curbyte
:
1458 curfullbyte
.extend(curbyte
)
1460 if total_read
>= max_read
:
1462 "Maximum read limit reached without finding the delimiter.")
1463 # Decode the full byte array to string once out of the loop
1465 return curfullbyte
.decode('UTF-8')
1466 except UnicodeDecodeError:
1467 # Handle potential partial UTF-8 characters
1468 for i
in range(1, 4):
1470 return curfullbyte
[:-i
].decode('UTF-8')
1471 except UnicodeDecodeError:
1473 raise # Re-raise if decoding fails even after trimming
1476 def ReadUntilNullByte(fp
, delimiter
=__file_format_dict__
['format_delimiter'], max_read
=1024000):
1477 return ReadTillNullByte(fp
, delimiter
, max_read
)
1480 def ReadTillNullByteByNum(fp
, delimiter
=__file_format_dict__
['format_delimiter'], num_delimiters
=1, chunk_size
=1024, max_read
=1024000):
1481 delimiter
= delimiter
.encode('UTF-8') # Ensure the delimiter is in bytes
1482 buffer = bytearray()
1484 delimiter_length
= len(delimiter
)
1486 while len(results
) < num_delimiters
:
1487 chunk
= fp
.read(chunk_size
)
1489 # End of file reached; decode whatever is collected if it's the last needed part
1491 results
.append(buffer.decode('UTF-8', errors
='replace'))
1493 buffer.extend(chunk
)
1494 total_read
+= len(chunk
)
1495 # Check if we have found the delimiter
1496 while delimiter
in buffer:
1497 index
= buffer.find(delimiter
)
1498 # Decode the section before the delimiter
1499 results
.append(buffer[:index
].decode('UTF-8', errors
='replace'))
1500 # Remove the processed part from the buffer
1501 buffer = buffer[index
+ delimiter_length
:]
1502 if len(results
) == num_delimiters
:
1503 # If reached the required number of delimiters, adjust the file pointer and stop
1504 fp
.seek(-len(buffer), 1)
1506 if total_read
>= max_read
:
1507 # Stop reading if max limit is reached to prevent excessive memory usage
1509 "Maximum read limit reached without finding the delimiter.")
1510 # Check for incomplete UTF-8 sequences at the end of the buffer
1511 if len(buffer) > 1 and 128 <= buffer[-1] < 192:
1512 # This suggests that the last byte might be the start of a multi-byte character
1513 # Try to read one more byte to complete the character
1514 extra_byte
= fp
.read(1)
1516 buffer.extend(extra_byte
)
1518 # No more data available
1520 # Process remaining buffer if less than the required number of delimiters were found
1521 if len(buffer) > 0 and len(results
) < num_delimiters
:
1522 results
.append(buffer.decode('UTF-8', errors
='replace'))
1526 def ReadUntilNullByteByNum(fp
, delimiter
=__file_format_dict__
['format_delimiter'], num_delimiters
=1, chunk_size
=1024, max_read
=1024000):
1527 return ReadTillNullByteByNum(fp
, delimiter
, num_delimiters
, chunk_size
, max_read
)
1530 def SeekToEndOfFile(fp
):
1534 if(lasttell
== fp
.tell()):
1536 lasttell
= fp
.tell()
1540 def ReadFileHeaderData(fp
, rounds
=0, delimiter
=__file_format_dict__
['format_delimiter']):
1544 while(rocount
< roend
):
1545 HeaderOut
.append(ReadTillNullByte(fp
, delimiter
))
1546 rocount
= rocount
+ 1
1550 def ReadFileHeaderDataBySize(fp
, delimiter
=__file_format_dict__
['format_delimiter']):
1551 headerpresize
= ReadTillNullByte(fp
, delimiter
)
1552 headersize
= int(headerpresize
, 16)
1553 if(headersize
<= 0):
1555 headercontent
= str(fp
.read(headersize
).decode('UTF-8')).split(delimiter
)
1558 roend
= int(len(headercontent
))
1559 HeaderOut
= [headerpresize
]
1560 while(rocount
< roend
):
1561 HeaderOut
.append(headercontent
[rocount
])
1562 rocount
= rocount
+ 1
1566 def ReadFileHeaderDataWoSize(fp
, delimiter
=__file_format_dict__
['format_delimiter']):
1567 preheaderdata
= ReadFileHeaderData(fp
, 2, delimiter
)
1568 headersize
= int(preheaderdata
[0], 16)
1569 headernumfields
= int(preheaderdata
[1], 16)
1570 if(headersize
<= 0 or headernumfields
<= 0):
1572 headerdata
= ReadTillNullByteByNum(fp
, delimiter
, headernumfields
)
1573 #headerdata = ReadFileHeaderData(fp, headernumfields, delimiter);
1574 HeaderOut
= preheaderdata
+ headerdata
1578 def ReadFileHeaderDataBySizeWithContent(fp
, listonly
=False, uncompress
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
):
1579 formatspecs
= FormatSpecsListToDict(formatspecs
)
1580 delimiter
= formatspecs
['format_delimiter']
1581 fheaderstart
= fp
.tell()
1582 HeaderOut
= ReadFileHeaderDataBySize(fp
, delimiter
)
1583 if(len(HeaderOut
) == 0):
1585 if(re
.findall("^[.|/]", HeaderOut
[3])):
1586 fname
= HeaderOut
[3]
1588 fname
= "./"+HeaderOut
[3]
1589 fcs
= HeaderOut
[-2].lower()
1590 fccs
= HeaderOut
[-1].lower()
1591 fsize
= int(HeaderOut
[5], 16)
1592 fcompression
= HeaderOut
[12]
1593 fcsize
= int(HeaderOut
[13], 16)
1594 fseeknextfile
= HeaderOut
[25]
1595 newfcs
= GetHeaderChecksum(
1596 HeaderOut
[:-2], HeaderOut
[-4].lower(), True, formatspecs
)
1597 if(fcs
!= newfcs
and not skipchecksum
):
1598 VerbosePrintOut("File Header Checksum Error with file " +
1599 fname
+ " at offset " + str(fheaderstart
))
1600 VerbosePrintOut("'" + str(fcs
) + "' != " + "'" + str(newfcs
) + "'")
1602 fhend
= fp
.tell() - 1
1603 fcontentstart
= fp
.tell()
1604 fcontents
= BytesIO()
1605 if(fsize
> 0 and not listonly
):
1606 if(fcompression
== "none" or fcompression
== "" or fcompression
== "auto"):
1607 fcontents
.write(fp
.read(fsize
))
1609 fcontents
.write(fp
.read(fcsize
))
1610 elif(fsize
> 0 and listonly
):
1611 if(fcompression
== "none" or fcompression
== "" or fcompression
== "auto"):
1615 fcontents
.seek(0, 0)
1616 newfccs
= GetFileChecksum(
1617 fcontents
.read(), HeaderOut
[-3].lower(), False, formatspecs
)
1618 if(fccs
!= newfccs
and not skipchecksum
and not listonly
):
1619 VerbosePrintOut("File Content Checksum Error with file " +
1620 fname
+ " at offset " + str(fcontentstart
))
1621 VerbosePrintOut("'" + str(fccs
) + "' != " + "'" + str(newfccs
) + "'")
1623 if(fcompression
== "none" or fcompression
== "" or fcompression
== "auto"):
1626 fcontents
.seek(0, 0)
1628 fcontents
= UncompressArchiveFile(fcontents
, formatspecs
)
1629 fcontentend
= fp
.tell()
1630 if(re
.findall("^\\+([0-9]+)", fseeknextfile
)):
1631 fseeknextasnum
= int(fseeknextfile
.replace("+", ""))
1632 if(abs(fseeknextasnum
) == 0):
1634 fp
.seek(fseeknextasnum
, 1)
1635 elif(re
.findall("^\\-([0-9]+)", fseeknextfile
)):
1636 fseeknextasnum
= int(fseeknextfile
)
1637 if(abs(fseeknextasnum
) == 0):
1639 fp
.seek(fseeknextasnum
, 1)
1640 elif(re
.findall("^([0-9]+)", fseeknextfile
)):
1641 fseeknextasnum
= int(fseeknextfile
)
1642 if(abs(fseeknextasnum
) == 0):
1644 fp
.seek(fseeknextasnum
, 0)
1647 HeaderOut
.append(fcontents
)
1651 def ReadFileHeaderDataBySizeWithContentToArray(fp
, listonly
=False, contentasfile
=True, uncompress
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
):
1652 formatspecs
= FormatSpecsListToDict(formatspecs
)
1653 delimiter
= formatspecs
['format_delimiter']
1654 fheaderstart
= fp
.tell()
1655 if(formatspecs
['new_style']):
1656 HeaderOut
= ReadFileHeaderDataBySize(fp
, delimiter
)
1658 HeaderOut
= ReadFileHeaderDataWoSize(fp
, delimiter
)
1659 if(len(HeaderOut
) == 0):
1661 fheadsize
= int(HeaderOut
[0], 16)
1662 fnumfields
= int(HeaderOut
[1], 16)
1663 ftype
= int(HeaderOut
[2], 16)
1664 if(re
.findall("^[.|/]", HeaderOut
[3])):
1665 fname
= HeaderOut
[3]
1667 fname
= "./"+HeaderOut
[3]
1668 fbasedir
= os
.path
.dirname(fname
)
1669 flinkname
= HeaderOut
[4]
1670 fsize
= int(HeaderOut
[5], 16)
1671 fatime
= int(HeaderOut
[6], 16)
1672 fmtime
= int(HeaderOut
[7], 16)
1673 fctime
= int(HeaderOut
[8], 16)
1674 fbtime
= int(HeaderOut
[9], 16)
1675 fmode
= int(HeaderOut
[10], 16)
1676 fchmode
= stat
.S_IMODE(fmode
)
1677 ftypemod
= stat
.S_IFMT(fmode
)
1678 fwinattributes
= int(HeaderOut
[11], 16)
1679 fcompression
= HeaderOut
[12]
1680 fcsize
= int(HeaderOut
[13], 16)
1681 fuid
= int(HeaderOut
[14], 16)
1682 funame
= HeaderOut
[15]
1683 fgid
= int(HeaderOut
[16], 16)
1684 fgname
= HeaderOut
[17]
1685 fid
= int(HeaderOut
[18], 16)
1686 finode
= int(HeaderOut
[19], 16)
1687 flinkcount
= int(HeaderOut
[20], 16)
1688 fdev_minor
= int(HeaderOut
[21], 16)
1689 fdev_major
= int(HeaderOut
[22], 16)
1690 frdev_minor
= int(HeaderOut
[23], 16)
1691 frdev_major
= int(HeaderOut
[24], 16)
1692 fseeknextfile
= HeaderOut
[25]
1693 fextrasize
= int(HeaderOut
[26], 16)
1694 fextrafields
= int(HeaderOut
[27], 16)
1695 extrafieldslist
= []
1697 extraend
= extrastart
+ fextrafields
1698 extrafieldslist
= []
1699 if(extrastart
< extraend
):
1700 extrafieldslist
.append(HeaderOut
[extrastart
])
1701 extrastart
= extrastart
+ 1
1702 fcs
= HeaderOut
[-2].lower()
1703 fccs
= HeaderOut
[-1].lower()
1704 newfcs
= GetHeaderChecksum(
1705 HeaderOut
[:-2], HeaderOut
[-4].lower(), True, formatspecs
)
1706 if(fcs
!= newfcs
and not skipchecksum
):
1707 VerbosePrintOut("File Header Checksum Error with file " +
1708 fname
+ " at offset " + str(fheaderstart
))
1709 VerbosePrintOut("'" + str(fcs
) + "' != " + "'" + str(newfcs
) + "'")
1711 fhend
= fp
.tell() - 1
1712 fcontentstart
= fp
.tell()
1713 fcontents
= BytesIO()
1714 pyhascontents
= False
1715 if(fsize
> 0 and not listonly
):
1716 if(fcompression
== "none" or fcompression
== "" or fcompression
== "auto"):
1717 fcontents
.write(fp
.read(fsize
))
1719 fcontents
.write(fp
.read(fcsize
))
1720 pyhascontents
= True
1721 elif(fsize
> 0 and listonly
):
1722 if(fcompression
== "none" or fcompression
== "" or fcompression
== "auto"):
1726 pyhascontents
= False
1727 fcontents
.seek(0, 0)
1728 newfccs
= GetFileChecksum(
1729 fcontents
.read(), HeaderOut
[-3].lower(), False, formatspecs
)
1730 if(fccs
!= newfccs
and not skipchecksum
and not listonly
):
1731 VerbosePrintOut("File Content Checksum Error with file " +
1732 fname
+ " at offset " + str(fcontentstart
))
1733 VerbosePrintOut("'" + str(fccs
) + "' != " + "'" + str(newfccs
) + "'")
1735 if(fcompression
== "none" or fcompression
== "" or fcompression
== "auto"):
1738 fcontents
.seek(0, 0)
1740 fcontents
= UncompressArchiveFile(fcontents
, formatspecs
)
1741 fcontents
.seek(0, 0)
1742 fccs
= GetFileChecksum(
1743 fcontents
.read(), HeaderOut
[-3].lower(), False, formatspecs
)
1744 fcontentend
= fp
.tell() - 1
1745 if(re
.findall("^\\+([0-9]+)", fseeknextfile
)):
1746 fseeknextasnum
= int(fseeknextfile
.replace("+", ""))
1747 if(abs(fseeknextasnum
) == 0):
1749 fp
.seek(fseeknextasnum
, 1)
1750 elif(re
.findall("^\\-([0-9]+)", fseeknextfile
)):
1751 fseeknextasnum
= int(fseeknextfile
)
1752 if(abs(fseeknextasnum
) == 0):
1754 fp
.seek(fseeknextasnum
, 1)
1755 elif(re
.findall("^([0-9]+)", fseeknextfile
)):
1756 fseeknextasnum
= int(fseeknextfile
)
1757 if(abs(fseeknextasnum
) == 0):
1759 fp
.seek(fseeknextasnum
, 0)
1762 fcontents
.seek(0, 0)
1763 if(not contentasfile
):
1764 fcontents
= fcontents
.read()
1765 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
, 'fcompression': fcompression
, 'fcsize': fcsize
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
,
1766 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fseeknextfile': fseeknextfile
, 'fheaderchecksumtype': HeaderOut
[-4], 'fcontentchecksumtype': HeaderOut
[-3], 'fnumfields': fnumfields
+ 2, 'frawheader': HeaderOut
, 'fextrafields': fextrafields
, 'fextrafieldsize': fextrasize
, 'fextralist': extrafieldslist
, 'fheaderchecksum': fcs
, 'fcontentchecksum': fccs
, 'fhascontents': pyhascontents
, 'fcontentstart': fcontentstart
, 'fcontentend': fcontentend
, 'fcontentasfile': contentasfile
, 'fcontents': fcontents
}
1770 def ReadFileHeaderDataBySizeWithContentToList(fp
, listonly
=False, uncompress
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
):
1771 formatspecs
= FormatSpecsListToDict(formatspecs
)
1772 delimiter
= formatspecs
['format_delimiter']
1773 fheaderstart
= fp
.tell()
1774 if(formatspecs
['new_style']):
1775 HeaderOut
= ReadFileHeaderDataBySize(fp
, delimiter
)
1777 HeaderOut
= ReadFileHeaderDataWoSize(fp
, delimiter
)
1778 if(len(HeaderOut
) == 0):
1780 fheadsize
= int(HeaderOut
[0], 16)
1781 fnumfields
= int(HeaderOut
[1], 16)
1782 ftype
= int(HeaderOut
[2], 16)
1783 if(re
.findall("^[.|/]", HeaderOut
[3])):
1784 fname
= HeaderOut
[3]
1786 fname
= "./"+HeaderOut
[3]
1787 fbasedir
= os
.path
.dirname(fname
)
1788 flinkname
= HeaderOut
[4]
1789 fsize
= int(HeaderOut
[5], 16)
1790 fatime
= int(HeaderOut
[6], 16)
1791 fmtime
= int(HeaderOut
[7], 16)
1792 fctime
= int(HeaderOut
[8], 16)
1793 fbtime
= int(HeaderOut
[9], 16)
1794 fmode
= int(HeaderOut
[10], 16)
1795 fchmode
= stat
.S_IMODE(fmode
)
1796 ftypemod
= stat
.S_IFMT(fmode
)
1797 fwinattributes
= int(HeaderOut
[11], 16)
1798 fcompression
= HeaderOut
[12]
1799 fcsize
= int(HeaderOut
[13], 16)
1800 fuid
= int(HeaderOut
[14], 16)
1801 funame
= HeaderOut
[15]
1802 fgid
= int(HeaderOut
[16], 16)
1803 fgname
= HeaderOut
[17]
1804 fid
= int(HeaderOut
[18], 16)
1805 finode
= int(HeaderOut
[19], 16)
1806 flinkcount
= int(HeaderOut
[20], 16)
1807 fdev_minor
= int(HeaderOut
[21], 16)
1808 fdev_major
= int(HeaderOut
[22], 16)
1809 frdev_minor
= int(HeaderOut
[23], 16)
1810 frdev_major
= int(HeaderOut
[24], 16)
1811 fseeknextfile
= HeaderOut
[25]
1812 fextrasize
= int(HeaderOut
[26], 16)
1813 fextrafields
= int(HeaderOut
[27], 16)
1814 extrafieldslist
= []
1816 extraend
= extrastart
+ fextrafields
1817 extrafieldslist
= []
1818 if(extrastart
< extraend
):
1819 extrafieldslist
.append(HeaderOut
[extrastart
])
1820 extrastart
= extrastart
+ 1
1821 fheaderchecksumtype
= HeaderOut
[extrastart
].lower()
1822 fcontentchecksumtype
= HeaderOut
[extrastart
+ 1].lower()
1823 fcs
= HeaderOut
[-2].lower()
1824 fccs
= HeaderOut
[-1].lower()
1825 newfcs
= GetHeaderChecksum(
1826 HeaderOut
[:-2], HeaderOut
[-4].lower(), True, formatspecs
)
1827 if(fcs
!= newfcs
and not skipchecksum
):
1828 VerbosePrintOut("File Header Checksum Error with file " +
1829 fname
+ " at offset " + str(fheaderstart
))
1830 VerbosePrintOut("'" + str(fcs
) + "' != " + "'" + str(newfcs
) + "'")
1832 fhend
= fp
.tell() - 1
1833 fcontentstart
= fp
.tell()
1834 fcontents
= BytesIO()
1835 pyhascontents
= False
1836 if(fsize
> 0 and not listonly
):
1837 if(fcompression
== "none" or fcompression
== "" or fcompression
== "auto"):
1838 fcontents
.write(fp
.read(fsize
))
1840 fcontents
.write(fp
.read(fcsize
))
1841 pyhascontents
= True
1842 elif(fsize
> 0 and listonly
):
1843 if(fcompression
== "none" or fcompression
== "" or fcompression
== "atuo"):
1847 pyhascontents
= False
1848 fcontents
.seek(0, 0)
1849 newfccs
= GetFileChecksum(
1850 fcontents
.read(), HeaderOut
[-3].lower(), False, formatspecs
)
1851 if(fccs
!= newfccs
and not skipchecksum
and not listonly
):
1852 VerbosePrintOut("File Content Checksum Error with file " +
1853 fname
+ " at offset " + str(fcontentstart
))
1854 VerbosePrintOut("'" + str(fccs
) + "' != " + "'" + str(newfccs
) + "'")
1856 if(fcompression
== "none" or fcompression
== "" or fcompression
== "auto"):
1859 fcontents
.seek(0, 0)
1861 fcontents
= UncompressArchiveFile(fcontents
, formatspecs
)
1862 fcontents
.seek(0, 0)
1863 fcontentend
= fp
.tell() - 1
1864 if(re
.findall("^\\+([0-9]+)", fseeknextfile
)):
1865 fseeknextasnum
= int(fseeknextfile
.replace("+", ""))
1866 if(abs(fseeknextasnum
) == 0):
1868 fp
.seek(fseeknextasnum
, 1)
1869 elif(re
.findall("^\\-([0-9]+)", fseeknextfile
)):
1870 fseeknextasnum
= int(fseeknextfile
)
1871 if(abs(fseeknextasnum
) == 0):
1873 fp
.seek(fseeknextasnum
, 1)
1874 elif(re
.findall("^([0-9]+)", fseeknextfile
)):
1875 fseeknextasnum
= int(fseeknextfile
)
1876 if(abs(fseeknextasnum
) == 0):
1878 fp
.seek(fseeknextasnum
, 0)
1881 catlist
= [ftype
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fcompression
, fcsize
, fuid
, funame
, fgid
, fgname
, fid
,
1882 finode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, fseeknextfile
, extrafieldslist
, fheaderchecksumtype
, fcontentchecksumtype
, fcontents
]
1886 def ReadFileDataBySizeWithContent(fp
, listonly
=False, uncompress
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
):
1887 formatspecs
= FormatSpecsListToDict(formatspecs
)
1888 delimiter
= formatspecs
['format_delimiter']
1892 catheader
= ReadFileHeaderData(fp
, 4, delimiter
)
1895 headercheck
= ValidateHeaderChecksum(
1896 catheader
[:-1], catheader
[2], catheader
[3], formatspecs
)
1897 newfcs
= GetHeaderChecksum(catheader
[:-2], catheader
[2], True, formatspecs
)
1898 if(not headercheck
and not skipchecksum
):
1900 "File Header Checksum Error with file at offset " + str(0))
1901 VerbosePrintOut("'" + str(newfcs
) + "' != " +
1902 "'" + str(catheader
[3]) + "'")
1904 fnumfiles
= int(catheader
[1], 16)
1907 while(countnum
< fnumfiles
):
1908 HeaderOut
= ReadFileHeaderDataBySizeWithContent(
1909 fp
, listonly
, uncompress
, skipchecksum
, formatspecs
)
1910 if(len(HeaderOut
) == 0):
1912 flist
.append(HeaderOut
)
1913 countnum
= countnum
+ 1
1917 def ReadFileDataBySizeWithContentToArray(fp
, seekstart
=0, seekend
=0, listonly
=False, contentasfile
=True, uncompress
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
):
1918 formatspecs
= FormatSpecsListToDict(formatspecs
)
1919 delimiter
= formatspecs
['format_delimiter']
1923 catheader
= ReadFileHeaderData(fp
, 4, delimiter
)
1926 headercheck
= ValidateHeaderChecksum(
1927 catheader
[:-1], catheader
[2], catheader
[3], formatspecs
)
1928 newfcs
= GetHeaderChecksum(catheader
[:-2], catheader
[2], True, formatspecs
)
1929 if(not headercheck
and not skipchecksum
):
1931 "File Header Checksum Error with file at offset " + str(0))
1932 VerbosePrintOut("'" + str(newfcs
) + "' != " +
1933 "'" + str(catheader
[3]) + "'")
1935 catstring
= catheader
[0]
1936 catversion
= re
.findall("([\\d]+)", catstring
)
1937 catversions
= re
.search('(.*?)(\\d+)', catstring
).groups()
1938 fprenumfiles
= catheader
[1]
1939 fnumfiles
= int(fprenumfiles
, 16)
1940 fprechecksumtype
= catheader
[2]
1941 fprechecksum
= catheader
[3]
1942 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1],
1943 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': []}
1944 if(seekstart
< 0 and seekstart
> fnumfiles
):
1946 if(seekend
== 0 or seekend
> fnumfiles
and seekend
< seekstart
):
1948 elif(seekend
< 0 and abs(seekend
) <= fnumfiles
and abs(seekend
) >= seekstart
):
1949 seekend
= fnumfiles
- abs(seekend
)
1952 while(il
< seekstart
):
1953 prefhstart
= fp
.tell()
1954 preheaderdata
= ReadFileHeaderDataBySize(
1955 fp
, formatspecs
['format_delimiter'])
1956 if(len(preheaderdata
) == 0):
1958 prefsize
= int(preheaderdata
[5], 16)
1959 prefseeknextfile
= preheaderdata
[25]
1960 prenewfcs
= GetHeaderChecksum(
1961 preheaderdata
[:-2], preheaderdata
[-4].lower(), True, formatspecs
)
1962 prefcs
= preheaderdata
[-2]
1963 if(prefcs
!= prenewfcs
and not skipchecksum
):
1964 VVerbosePrintOut("File Header Checksum Error with file " +
1965 prefname
+ " at offset " + str(prefhstart
))
1966 VerbosePrintOut("'" + str(prefcs
) + "' != " +
1967 "'" + str(prenewfcs
) + "'")
1969 valid_archive
= False
1970 invalid_archive
= True
1971 prefhend
= fp
.tell() - 1
1972 prefcontentstart
= fp
.tell()
1973 prefcontents
= BytesIO()
1974 pyhascontents
= False
1976 prefcontents
.write(fp
.read(prefsize
))
1977 prefcontents
.seek(0, 0)
1978 prenewfccs
= GetFileChecksum(
1979 prefcontents
.read(), preheaderdata
[-3].lower(), False, formatspecs
)
1980 prefccs
= preheaderdata
[-1]
1981 pyhascontents
= True
1982 if(prefccs
!= prenewfccs
and not skipchecksum
):
1983 VerbosePrintOut("File Content Checksum Error with file " +
1984 prefname
+ " at offset " + str(prefcontentstart
))
1985 VerbosePrintOut("'" + str(prefccs
) +
1986 "' != " + "'" + str(prenewfccs
) + "'")
1988 if(re
.findall("^\\+([0-9]+)", prefseeknextfile
)):
1989 fseeknextasnum
= int(prefseeknextfile
.replace("+", ""))
1990 if(abs(fseeknextasnum
) == 0):
1992 fp
.seek(fseeknextasnum
, 1)
1993 elif(re
.findall("^\\-([0-9]+)", prefseeknextfile
)):
1994 fseeknextasnum
= int(prefseeknextfile
)
1995 if(abs(fseeknextasnum
) == 0):
1997 fp
.seek(fseeknextasnum
, 1)
1998 elif(re
.findall("^([0-9]+)", prefseeknextfile
)):
1999 fseeknextasnum
= int(prefseeknextfile
)
2000 if(abs(fseeknextasnum
) == 0):
2002 fp
.seek(fseeknextasnum
, 0)
2007 countnum
= seekstart
2008 while(countnum
< seekend
):
2009 HeaderOut
= ReadFileHeaderDataBySizeWithContentToArray(
2010 fp
, listonly
, contentasfile
, uncompress
, skipchecksum
, formatspecs
)
2011 if(len(HeaderOut
) == 0):
2013 HeaderOut
.update({'fid': realidnum
, 'fidalt': realidnum
})
2014 catlist
['ffilelist'].append(HeaderOut
)
2015 countnum
= countnum
+ 1
2016 realidnum
= realidnum
+ 1
2020 def ReadFileDataBySizeWithContentToList(fp
, seekstart
=0, seekend
=0, listonly
=False, uncompress
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
):
2021 formatspecs
= FormatSpecsListToDict(formatspecs
)
2022 delimiter
= formatspecs
['format_delimiter']
2026 catheader
= ReadFileHeaderData(fp
, 4, delimiter
)
2029 headercheck
= ValidateHeaderChecksum(
2030 catheader
[:-1], catheader
[2], catheader
[3], formatspecs
)
2031 newfcs
= GetHeaderChecksum(catheader
[:-2], catheader
[2], True, formatspecs
)
2032 if(not headercheck
and not skipchecksum
):
2034 "File Header Checksum Error with file at offset " + str(0))
2035 VerbosePrintOut("'" + str(newfcs
) + "' != " +
2036 "'" + str(catheader
[3]) + "'")
2038 catstring
= catheader
[0]
2039 catversion
= re
.findall("([\\d]+)", catstring
)
2040 catversions
= re
.search('(.*?)(\\d+)', catstring
).groups()
2041 fprenumfiles
= catheader
[1]
2042 fnumfiles
= int(fprenumfiles
, 16)
2043 fprechecksumtype
= catheader
[2]
2044 fprechecksum
= catheader
[3]
2046 if(seekstart
< 0 and seekstart
> fnumfiles
):
2048 if(seekend
== 0 or seekend
> fnumfiles
and seekend
< seekstart
):
2050 elif(seekend
< 0 and abs(seekend
) <= fnumfiles
and abs(seekend
) >= seekstart
):
2051 seekend
= fnumfiles
- abs(seekend
)
2054 while(il
< seekstart
):
2055 prefhstart
= fp
.tell()
2056 preheaderdata
= ReadFileHeaderDataBySize(
2057 fp
, formatspecs
['format_delimiter'])
2058 if(len(preheaderdata
) == 0):
2060 prefsize
= int(preheaderdata
[5], 16)
2061 prefcompression
= preheaderdata
[12]
2062 prefcsize
= int(preheaderdata
[13], 16)
2063 prefseeknextfile
= HeaderOut
[25]
2064 prenewfcs
= GetHeaderChecksum(
2065 preheaderdata
[:-2], preheaderdata
[-4].lower(), True, formatspecs
)
2066 prefcs
= preheaderdata
[-2]
2067 if(prefcs
!= prenewfcs
and not skipchecksum
):
2068 VerbosePrintOut("File Header Checksum Error with file " +
2069 prefname
+ " at offset " + str(prefhstart
))
2070 VerbosePrintOut("'" + str(prefcs
) + "' != " +
2071 "'" + str(prenewfcs
) + "'")
2073 valid_archive
= False
2074 invalid_archive
= True
2075 prefhend
= fp
.tell() - 1
2076 prefcontentstart
= fp
.tell()
2078 pyhascontents
= False
2080 if(prefcompression
== "none" or prefcompression
== "" or prefcompression
== "auto"):
2081 prefcontents
= catfp
.read(prefsize
)
2083 prefcontents
= catfp
.read(prefcsize
)
2084 prenewfccs
= GetFileChecksum(
2085 prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
)
2086 prefccs
= preheaderdata
[-1]
2087 pyhascontents
= True
2088 if(prefccs
!= prenewfccs
and not skipchecksum
):
2089 VerbosePrintOut("File Content Checksum Error with file " +
2090 prefname
+ " at offset " + str(prefcontentstart
))
2091 VerbosePrintOut("'" + str(prefccs
) +
2092 "' != " + "'" + str(prenewfccs
) + "'")
2094 if(re
.findall("^\\+([0-9]+)", prefseeknextfile
)):
2095 fseeknextasnum
= int(prefseeknextfile
.replace("+", ""))
2096 if(abs(fseeknextasnum
) == 0):
2098 catfp
.seek(fseeknextasnum
, 1)
2099 elif(re
.findall("^\\-([0-9]+)", prefseeknextfile
)):
2100 fseeknextasnum
= int(prefseeknextfile
)
2101 if(abs(fseeknextasnum
) == 0):
2103 catfp
.seek(fseeknextasnum
, 1)
2104 elif(re
.findall("^([0-9]+)", prefseeknextfile
)):
2105 fseeknextasnum
= int(prefseeknextfile
)
2106 if(abs(fseeknextasnum
) == 0):
2108 catfp
.seek(fseeknextasnum
, 0)
2113 countnum
= seekstart
2114 while(countnum
< seekend
):
2115 HeaderOut
= ReadFileHeaderDataBySizeWithContentToList(
2116 fp
, listonly
, uncompress
, skipchecksum
, formatspecs
)
2117 if(len(HeaderOut
) == 0):
2119 catlist
.append(HeaderOut
)
2120 countnum
= countnum
+ 1
2121 realidnum
= realidnum
+ 1
2125 def ReadInFileBySizeWithContentToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, contentasfile
=True, uncompress
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
):
2126 formatspecs
= FormatSpecsListToDict(formatspecs
)
2127 delimiter
= formatspecs
['format_delimiter']
2128 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2131 fp
= UncompressArchiveFile(fp
, formatspecs
)
2132 checkcompressfile
= CheckCompressionSubType(fp
, formatspecs
, True)
2133 if(checkcompressfile
!= "catfile" and checkcompressfile
!= formatspecs
['format_lower']):
2138 elif(infile
== "-"):
2140 if(hasattr(sys
.stdin
, "buffer")):
2141 shutil
.copyfileobj(sys
.stdin
.buffer, fp
)
2143 shutil
.copyfileobj(sys
.stdin
, fp
)
2145 fp
= UncompressArchiveFile(fp
, formatspecs
)
2149 elif(re
.findall("^(http|https|ftp|ftps|sftp):\\/\\/", str(infile
))):
2150 fp
= download_file_from_internet_file(infile
)
2151 fp
= UncompressArchiveFile(fp
, formatspecs
)
2157 infile
= RemoveWindowsPath(infile
)
2158 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True)
2159 if(checkcompressfile
!= "catfile" and checkcompressfile
!= formatspecs
['format_lower']):
2161 compresscheck
= CheckCompressionType(infile
, formatspecs
, True)
2162 if(not compresscheck
):
2163 fextname
= os
.path
.splitext(infile
)[1]
2164 if(fextname
== ".gz"):
2165 compresscheck
= "gzip"
2166 elif(fextname
== ".bz2"):
2167 compresscheck
= "bzip2"
2168 elif(fextname
== ".zst"):
2169 compresscheck
= "zstd"
2170 elif(fextname
== ".lz4" or fextname
== ".clz4"):
2171 compresscheck
= "lz4"
2172 elif(fextname
== ".lzo" or fextname
== ".lzop"):
2173 compresscheck
= "lzo"
2174 elif(fextname
== ".lzma"):
2175 compresscheck
= "lzma"
2176 elif(fextname
== ".xz"):
2177 compresscheck
= "xz"
2178 elif(fextname
== ".zz" or fextname
== ".zl" or fextname
== ".zlib"):
2179 compresscheck
= "zlib"
2182 if(not compresscheck
):
2184 fp
= UncompressFile(infile
, formatspecs
, "rb")
2185 return ReadFileDataBySizeWithContentToArray(fp
, seekstart
, seekend
, listonly
, contentasfile
, uncompress
, skipchecksum
, formatspecs
)
2188 def ReadInFileBySizeWithContentToList(infile
, seekstart
=0, seekend
=0, listonly
=False, uncompress
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
):
2189 formatspecs
= FormatSpecsListToDict(formatspecs
)
2190 delimiter
= formatspecs
['format_delimiter']
2191 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2194 fp
= UncompressArchiveFile(fp
, formatspecs
)
2195 checkcompressfile
= CheckCompressionSubType(fp
, formatspecs
, True)
2196 if(checkcompressfile
!= "catfile" and checkcompressfile
!= formatspecs
['format_lower']):
2201 elif(infile
== "-"):
2203 if(hasattr(sys
.stdin
, "buffer")):
2204 shutil
.copyfileobj(sys
.stdin
.buffer, fp
)
2206 shutil
.copyfileobj(sys
.stdin
, fp
)
2208 fp
= UncompressArchiveFile(fp
, formatspecs
)
2212 elif(re
.findall("^(http|https|ftp|ftps|sftp):\\/\\/", str(infile
))):
2213 fp
= download_file_from_internet_file(infile
)
2214 fp
= UncompressArchiveFile(fp
, formatspecs
)
2220 infile
= RemoveWindowsPath(infile
)
2221 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True)
2222 if(checkcompressfile
!= "catfile" and checkcompressfile
!= formatspecs
['format_lower']):
2224 compresscheck
= CheckCompressionType(infile
, formatspecs
, True)
2225 if(not compresscheck
):
2226 fextname
= os
.path
.splitext(infile
)[1]
2227 if(fextname
== ".gz"):
2228 compresscheck
= "gzip"
2229 elif(fextname
== ".bz2"):
2230 compresscheck
= "bzip2"
2231 elif(fextname
== ".zst"):
2232 compresscheck
= "zstd"
2233 elif(fextname
== ".lz4" or fextname
== ".clz4"):
2234 compresscheck
= "lz4"
2235 elif(fextname
== ".lzo" or fextname
== ".lzop"):
2236 compresscheck
= "lzo"
2237 elif(fextname
== ".lzma"):
2238 compresscheck
= "lzma"
2239 elif(fextname
== ".xz"):
2240 compresscheck
= "xz"
2241 elif(fextname
== ".zz" or fextname
== ".zl" or fextname
== ".zlib"):
2242 compresscheck
= "zlib"
2245 if(not compresscheck
):
2247 fp
= UncompressFile(infile
, formatspecs
, "rb")
2248 return ReadFileDataBySizeWithContentToList(fp
, seekstart
, seekend
, listonly
, uncompress
, skipchecksum
, formatspecs
)
2251 def AppendNullByte(indata
, delimiter
=__file_format_dict__
['format_delimiter']):
2252 outdata
= str(indata
) + delimiter
2256 def AppendNullBytes(indata
=[], delimiter
=__file_format_dict__
['format_delimiter']):
2261 outdata
= outdata
+ AppendNullByte(indata
[inum
], delimiter
)
2266 def AppendFileHeader(fp
, numfiles
, checksumtype
="crc32", formatspecs
=__file_format_dict__
):
2267 formatspecs
= FormatSpecsListToDict(formatspecs
)
2268 delimiter
= formatspecs
['format_delimiter']
2269 catver
= formatspecs
['format_ver']
2270 fileheaderver
= str(int(catver
.replace(".", "")))
2271 fileheader
= AppendNullByte(
2272 formatspecs
['format_magic'] + fileheaderver
, formatspecs
['format_delimiter'])
2273 fp
.write(fileheader
.encode('UTF-8'))
2274 fnumfiles
= format(int(numfiles
), 'x').lower()
2275 fnumfilesa
= AppendNullBytes(
2276 [fnumfiles
, checksumtype
], formatspecs
['format_delimiter'])
2277 catfileheadercshex
= GetFileChecksum(
2278 fileheader
+ fnumfilesa
, checksumtype
, True, formatspecs
)
2279 fnumfilesa
= fnumfilesa
+ \
2280 AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter'])
2281 fp
.write(fnumfilesa
.encode('UTF-8'))
2284 os
.fsync(fp
.fileno())
2285 except io
.UnsupportedOperation
:
2287 except AttributeError:
2289 except OSError as e
:
2294 def MakeEmptyFilePointer(fp
, checksumtype
="crc32", formatspecs
=__file_format_dict__
):
2295 formatspecs
= FormatSpecsListToDict(formatspecs
)
2296 AppendFileHeader(fp
, 0, checksumtype
, formatspecs
)
2300 def MakeEmptyFile(outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", formatspecs
=__file_format_dict__
, returnfp
=False):
2301 formatspecs
= FormatSpecsListToDict(formatspecs
)
2302 if(outfile
!= "-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2303 if(os
.path
.exists(outfile
)):
2306 except OSError as e
:
2311 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2313 elif(re
.findall("^(ftp|ftps|sftp):\\/\\/", str(outfile
))):
2316 fbasename
= os
.path
.splitext(outfile
)[0]
2317 fextname
= os
.path
.splitext(outfile
)[1]
2318 if(not compresswholefile
and fextname
in outextlistwd
):
2319 compresswholefile
= True
2320 catfp
= CompressOpenFile(outfile
, True, compressionlevel
)
2321 catfp
= AppendFileHeader(catfp
, 0, checksumtype
, formatspecs
)
2322 if(outfile
== "-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2323 catfp
= CompressArchiveFile(
2324 catfp
, compression
, compressionlevel
, formatspecs
)
2327 os
.fsync(catfp
.fileno())
2328 except io
.UnsupportedOperation
:
2330 except AttributeError:
2332 except OSError as e
:
2336 if(hasattr(sys
.stdout
, "buffer")):
2337 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer)
2339 shutil
.copyfileobj(catfp
, sys
.stdout
)
2340 elif(re
.findall("^(ftp|ftps|sftp):\\/\\/", str(outfile
))):
2341 catfp
= CompressArchiveFile(
2342 catfp
, compression
, compressionlevel
, formatspecs
)
2344 upload_file_to_internet_file(catfp
, outfile
)
2353 def AppendFileHeaderWithContent(fp
, filevalues
=[], extradata
=[], filecontent
="", checksumtype
="crc32", formatspecs
=__file_format_dict__
):
2354 formatspecs
= FormatSpecsListToDict(formatspecs
)
2355 extrafields
= format(len(extradata
), 'x').lower()
2356 extrasizestr
= AppendNullByte(extrafields
, formatspecs
['format_delimiter'])
2357 if(len(extradata
) > 0):
2358 extrasizestr
= extrasizestr
+ \
2359 AppendNullBytes(extradata
, formatspecs
['format_delimiter'])
2360 extrasizelen
= format(len(extrasizestr
), 'x').lower()
2361 catoutlen
= len(filevalues
) + len(extradata
) + 5
2362 catoutlenhex
= format(catoutlen
, 'x').lower()
2363 catoutlist
= filevalues
2364 catoutlist
.insert(0, catoutlenhex
)
2365 catoutlist
.append(extrasizelen
)
2366 catoutlist
.append(extrafields
)
2367 catfileoutstr
= AppendNullBytes(
2368 catoutlist
, formatspecs
['format_delimiter'])
2369 if(len(extradata
) > 0):
2370 catfileoutstr
= catfileoutstr
+ \
2371 AppendNullBytes(extradata
, formatspecs
['format_delimiter'])
2372 if(len(filecontent
) == 0):
2373 checksumlist
= [checksumtype
, "none"]
2375 checksumlist
= [checksumtype
, checksumtype
]
2376 catfileoutstr
= catfileoutstr
+ \
2377 AppendNullBytes(checksumlist
, formatspecs
['format_delimiter'])
2378 catfileheadercshex
= GetFileChecksum(
2379 catfileoutstr
, checksumtype
, True, formatspecs
)
2380 if(len(filecontent
) == 0):
2381 catfilecontentcshex
= GetFileChecksum(
2382 filecontent
, "none", False, formatspecs
)
2384 catfilecontentcshex
= GetFileChecksum(
2385 filecontent
, checksumtype
, False, formatspecs
)
2386 tmpfileoutstr
= catfileoutstr
+ \
2387 AppendNullBytes([catfileheadercshex
, catfilecontentcshex
],
2388 formatspecs
['format_delimiter'])
2389 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2390 catfileoutstr
= AppendNullByte(
2391 catheaersize
, formatspecs
['format_delimiter']) + catfileoutstr
2392 catfileheadercshex
= GetFileChecksum(
2393 catfileoutstr
, checksumtype
, True, formatspecs
)
2394 catfileoutstr
= catfileoutstr
+ \
2395 AppendNullBytes([catfileheadercshex
, catfilecontentcshex
],
2396 formatspecs
['format_delimiter'])
2397 catfileoutstrecd
= catfileoutstr
.encode('UTF-8')
2398 nullstrecd
= formatspecs
['format_delimiter'].encode('UTF-8')
2399 catfileout
= catfileoutstrecd
+ filecontent
+ nullstrecd
2400 fp
.write(catfileout
)
2403 os
.fsync(fp
.fileno())
2404 except io
.UnsupportedOperation
:
2406 except AttributeError:
2408 except OSError as e
:
2413 def AppendFilesWithContent(infiles
, fp
, dirlistfromtxt
=False, filevalues
=[], extradata
=[], compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_dict__
, verbose
=False):
2414 formatspecs
= FormatSpecsListToDict(formatspecs
)
2415 advancedlist
= formatspecs
['use_advanced_list']
2416 altinode
= formatspecs
['use_alt_inode']
2418 logging
.basicConfig(format
="%(message)s",
2419 stream
=sys
.stdout
, level
=logging
.DEBUG
)
2421 for line
in sys
.stdin
:
2422 infilelist
.append(line
.strip())
2423 infilelist
= list(filter(None, infilelist
))
2424 elif(infiles
!= "-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
== "/dev/null" or infiles
== "NUL")):
2425 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
2427 with
UncompressFile(infiles
, formatspecs
, "r") as finfile
:
2428 for line
in finfile
:
2429 infilelist
.append(line
.strip())
2430 infilelist
= list(filter(None, infilelist
))
2432 if(isinstance(infiles
, (list, tuple, ))):
2433 infilelist
= list(filter(None, infiles
))
2434 elif(isinstance(infiles
, (str, ))):
2435 infilelist
= list(filter(None, [infiles
]))
2437 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False)
2439 GetDirList
= ListDir(infilelist
, followlink
, False)
2447 inodetocatinode
= {}
2448 numfiles
= int(len(GetDirList
))
2449 fnumfiles
= format(numfiles
, 'x').lower()
2450 AppendFileHeader(fp
, fnumfiles
, checksumtype
, formatspecs
)
2451 for curfname
in GetDirList
:
2452 if(re
.findall("^[.|/]", curfname
)):
2455 fname
= "./"+curfname
2457 VerbosePrintOut(fname
)
2458 if(not followlink
or followlink
is None):
2459 fstatinfo
= os
.lstat(fname
)
2461 fstatinfo
= os
.stat(fname
)
2462 fpremode
= fstatinfo
.st_mode
2463 finode
= fstatinfo
.st_ino
2464 flinkcount
= fstatinfo
.st_nlink
2466 if(stat
.S_ISREG(fpremode
)):
2468 elif(stat
.S_ISLNK(fpremode
)):
2470 elif(stat
.S_ISCHR(fpremode
)):
2472 elif(stat
.S_ISBLK(fpremode
)):
2474 elif(stat
.S_ISDIR(fpremode
)):
2476 elif(stat
.S_ISFIFO(fpremode
)):
2478 elif(stat
.S_ISSOCK(fpremode
)):
2480 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
2482 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
2484 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
2489 fcurfid
= format(int(curfid
), 'x').lower()
2490 if(not followlink
and finode
!= 0):
2492 if(finode
in inodelist
):
2494 flinkname
= inodetofile
[finode
]
2496 fcurinode
= format(int(finode
), 'x').lower()
2499 int(inodetocatinode
[finode
]), 'x').lower()
2500 if(finode
not in inodelist
):
2501 inodelist
.append(finode
)
2502 inodetofile
.update({finode
: fname
})
2503 inodetocatinode
.update({finode
: curinode
})
2505 fcurinode
= format(int(finode
), 'x').lower()
2507 fcurinode
= format(int(curinode
), 'x').lower()
2508 curinode
= curinode
+ 1
2510 fcurinode
= format(int(curinode
), 'x').lower()
2511 curinode
= curinode
+ 1
2514 flinkname
= os
.readlink(fname
)
2515 fdev
= fstatinfo
.st_dev
2516 getfdev
= GetDevMajorMinor(fdev
)
2517 fdev_minor
= getfdev
[0]
2518 fdev_major
= getfdev
[1]
2519 frdev
= fstatinfo
.st_dev
2520 if(hasattr(fstatinfo
, "st_rdev")):
2521 frdev
= fstatinfo
.st_rdev
2523 frdev
= fstatinfo
.st_dev
2524 getfrdev
= GetDevMajorMinor(frdev
)
2525 frdev_minor
= getfrdev
[0]
2526 frdev_major
= getfrdev
[1]
2527 if(ftype
== 1 or ftype
== 2 or ftype
== 3 or ftype
== 4 or ftype
== 5 or ftype
== 6):
2528 fsize
= format(int("0"), 'x').lower()
2529 elif(ftype
== 0 or ftype
== 7):
2530 fsize
= format(int(fstatinfo
.st_size
), 'x').lower()
2532 fsize
= format(int(fstatinfo
.st_size
)).lower()
2533 fatime
= format(int(fstatinfo
.st_atime
), 'x').lower()
2534 fmtime
= format(int(fstatinfo
.st_mtime
), 'x').lower()
2535 fctime
= format(int(fstatinfo
.st_ctime
), 'x').lower()
2536 if(hasattr(fstatinfo
, "st_birthtime")):
2537 fbtime
= format(int(fstatinfo
.st_birthtime
), 'x').lower()
2539 fbtime
= format(int(fstatinfo
.st_ctime
), 'x').lower()
2540 fmode
= format(int(fstatinfo
.st_mode
), 'x').lower()
2541 fchmode
= format(int(stat
.S_IMODE(fstatinfo
.st_mode
)), 'x').lower()
2542 ftypemod
= format(int(stat
.S_IFMT(fstatinfo
.st_mode
)), 'x').lower()
2543 fuid
= format(int(fstatinfo
.st_uid
), 'x').lower()
2544 fgid
= format(int(fstatinfo
.st_gid
), 'x').lower()
2549 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
)
2550 funame
= userinfo
.pw_name
2559 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
)
2560 fgname
= groupinfo
.gr_name
2565 fdev_minor
= format(int(fdev_minor
), 'x').lower()
2566 fdev_major
= format(int(fdev_major
), 'x').lower()
2567 frdev_minor
= format(int(frdev_minor
), 'x').lower()
2568 frdev_major
= format(int(frdev_major
), 'x').lower()
2569 finode
= format(int(finode
), 'x').lower()
2570 flinkcount
= format(int(flinkcount
), 'x').lower()
2571 if(hasattr(fstatinfo
, "st_file_attributes")):
2572 fwinattributes
= format(
2573 int(fstatinfo
.st_file_attributes
), 'x').lower()
2575 fwinattributes
= format(int(0), 'x').lower()
2577 fcsize
= format(int(0), 'x').lower()
2578 fcontents
= BytesIO()
2580 if(ftype
== 0 or ftype
== 7):
2581 with
open(fname
, "rb") as fpc
:
2582 shutil
.copyfileobj(fpc
, fcontents
)
2583 if(not compresswholefile
):
2584 fcontents
.seek(0, 2)
2585 ucfsize
= fcontents
.tell()
2586 fcontents
.seek(0, 0)
2587 if(compression
== "auto"):
2588 ilsize
= len(compressionlistalt
)
2591 while(ilmin
< ilsize
):
2592 cfcontents
= BytesIO()
2593 shutil
.copyfileobj(fcontents
, cfcontents
)
2594 fcontents
.seek(0, 0)
2595 cfcontents
.seek(0, 0)
2596 cfcontents
= CompressArchiveFile(
2597 cfcontents
, compressionlistalt
[ilmin
], compressionlevel
, formatspecs
)
2599 cfcontents
.seek(0, 2)
2600 ilcsize
.append(cfcontents
.tell())
2604 ilcsize
.append(sys
.maxint
)
2605 except AttributeError:
2606 ilcsize
.append(sys
.maxsize
)
2608 ilcmin
= ilcsize
.index(min(ilcsize
))
2609 compression
= compressionlistalt
[ilcmin
]
2610 fcontents
.seek(0, 0)
2611 cfcontents
= BytesIO()
2612 shutil
.copyfileobj(fcontents
, cfcontents
)
2613 cfcontents
.seek(0, 0)
2614 cfcontents
= CompressArchiveFile(
2615 cfcontents
, compression
, compressionlevel
, formatspecs
)
2616 cfcontents
.seek(0, 2)
2617 cfsize
= cfcontents
.tell()
2618 if(ucfsize
> cfsize
):
2619 fcsize
= format(int(cfsize
), 'x').lower()
2620 fcompression
= compression
2622 fcontents
= cfcontents
2623 if(followlink
and (ftype
== 1 or ftype
== 2)):
2624 flstatinfo
= os
.stat(flinkname
)
2625 with
open(flinkname
, "rb") as fpc
:
2626 shutil
.copyfileobj(fpc
, fcontents
)
2627 if(not compresswholefile
):
2628 fcontents
.seek(0, 2)
2629 ucfsize
= fcontents
.tell()
2630 fcontents
.seek(0, 0)
2631 if(compression
== "auto"):
2632 ilsize
= len(compressionlistalt
)
2635 while(ilmin
< ilsize
):
2636 cfcontents
= BytesIO()
2637 shutil
.copyfileobj(fcontents
, cfcontents
)
2638 fcontents
.seek(0, 0)
2639 cfcontents
.seek(0, 0)
2640 cfcontents
= CompressArchiveFile(
2641 cfcontents
, compressionlistalt
[ilmin
], compressionlevel
, formatspecs
)
2643 cfcontents
.seek(0, 2)
2644 ilcsize
.append(cfcontents
.tell())
2648 ilcsize
.append(sys
.maxint
)
2649 except AttributeError:
2650 ilcsize
.append(sys
.maxsize
)
2652 ilcmin
= ilcsize
.index(min(ilcsize
))
2653 compression
= compressionlistalt
[ilcmin
]
2654 fcontents
.seek(0, 0)
2655 cfcontents
= BytesIO()
2656 shutil
.copyfileobj(fcontents
, cfcontents
)
2657 cfcontents
.seek(0, 0)
2658 cfcontents
= CompressArchiveFile(
2659 cfcontents
, compression
, compressionlevel
, formatspecs
)
2660 cfcontents
.seek(0, 2)
2661 cfsize
= cfcontents
.tell()
2662 if(ucfsize
> cfsize
):
2663 fcsize
= format(int(cfsize
), 'x').lower()
2664 fcompression
= compression
2666 fcontents
= cfcontents
2667 if(fcompression
== "none"):
2669 fcontents
.seek(0, 0)
2670 ftypehex
= format(ftype
, 'x').lower()
2671 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fcompression
,
2672 fcsize
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, "+1"]
2673 fp
= AppendFileHeaderWithContent(
2674 fp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
)
2676 catfp
.write(AppendNullBytes(
2677 [0, 0], formatspecs
['format_delimiter']).encode("UTF-8"))
2682 def AppendListsWithContent(inlist
, fp
, dirlistfromtxt
=False, filevalues
=[], extradata
=[], compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_dict__
, verbose
=False):
2683 formatspecs
= FormatSpecsListToDict(formatspecs
)
2685 logging
.basicConfig(format
="%(message)s",
2686 stream
=sys
.stdout
, level
=logging
.DEBUG
)
2695 inodetocatinode
= {}
2696 numfiles
= int(len(GetDirList
))
2697 fnumfiles
= format(numfiles
, 'x').lower()
2698 AppendFileHeader(fp
, fnumfiles
, checksumtype
, formatspecs
)
2699 for curfname
in GetDirList
:
2700 ftype
= format(curfname
[0], 'x').lower()
2701 if(re
.findall("^[.|/]", curfname
[1])):
2704 fname
= "./"+curfname
[1]
2705 fbasedir
= os
.path
.dirname(fname
)
2706 flinkname
= curfname
[2]
2707 fsize
= format(curfname
[3], 'x').lower()
2708 fatime
= format(curfname
[4], 'x').lower()
2709 fmtime
= format(curfname
[5], 'x').lower()
2710 fctime
= format(curfname
[6], 'x').lower()
2711 fbtime
= format(curfname
[7], 'x').lower()
2712 fmode
= format(curfname
[8], 'x').lower()
2713 fwinattributes
= format(curfname
[9], 'x').lower()
2714 fcompression
= curfname
[10]
2715 fcsize
= format(curfname
[11], 'x').lower()
2716 fuid
= format(curfname
[12], 'x').lower()
2717 funame
= curfname
[13]
2718 fgid
= format(curfname
[14], 'x').lower()
2719 fgname
= curfname
[15]
2720 fid
= format(curfname
[16], 'x').lower()
2721 finode
= format(curfname
[17], 'x').lower()
2722 flinkcount
= format(curfname
[18], 'x').lower()
2723 fdev_minor
= format(curfname
[19], 'x').lower()
2724 fdev_major
= format(curfname
[20], 'x').lower()
2725 frdev_minor
= format(curfname
[21], 'x').lower()
2726 frdev_major
= format(curfname
[22], 'x').lower()
2727 fseeknextfile
= curfname
[23]
2728 extradata
= curfname
[24]
2729 fheaderchecksumtype
= curfname
[25]
2730 fcontentchecksumtype
= curfname
[26]
2731 fcontents
= curfname
[27]
2732 catoutlist
= [ftype
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fcompression
, fcsize
,
2733 fuid
, funame
, fgid
, fgname
, fid
, finode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, fseeknextfile
]
2734 fcontents
.seek(0, 0)
2735 fp
= AppendFileHeaderWithContent(
2736 fp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
)
2738 fp
.write(AppendNullBytes(
2739 [0, 0], formatspecs
['format_delimiter']).encode("UTF-8"))
2743 def AppendInFileWithContent(infile
, fp
, dirlistfromtxt
=False, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_dict__
, verbose
=False):
2744 formatspecs
= FormatSpecsListToDict(formatspecs
)
2745 inlist
= ReadInFileBySizeWithContentToList(
2746 infile
, 0, 0, False, True, False, formatspecs
)
2747 return AppendListsWithContent(inlist
, fp
, dirlistfromtxt
, filevalues
, extradata
, followlink
, checksumtype
, formatspecs
, verbose
)
2750 def AppendFilesWithContentToOutFile(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
2751 formatspecs
= FormatSpecsListToDict(formatspecs
)
2752 if(outfile
!= "-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2753 if(os
.path
.exists(outfile
)):
2756 except OSError as e
:
2761 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2763 elif(re
.findall("^(ftp|ftps|sftp):\\/\\/", str(outfile
))):
2766 fbasename
= os
.path
.splitext(outfile
)[0]
2767 fextname
= os
.path
.splitext(outfile
)[1]
2768 if(not compresswholefile
and fextname
in outextlistwd
):
2769 compresswholefile
= True
2770 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
)
2771 catfp
= AppendFilesWithContent(infiles
, catfp
, dirlistfromtxt
, filevalues
, extradata
, compression
,
2772 compresswholefile
, compressionlevel
, followlink
, checksumtype
, formatspecs
, verbose
)
2773 if(outfile
== "-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2774 catfp
= CompressArchiveFile(
2775 catfp
, compression
, compressionlevel
, formatspecs
)
2778 os
.fsync(catfp
.fileno())
2779 except io
.UnsupportedOperation
:
2781 except AttributeError:
2783 except OSError as e
:
2787 if(hasattr(sys
.stdout
, "buffer")):
2788 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer)
2790 shutil
.copyfileobj(catfp
, sys
.stdout
)
2791 elif(re
.findall("^(ftp|ftps|sftp):\\/\\/", str(outfile
))):
2792 catfp
= CompressArchiveFile(
2793 catfp
, compression
, compressionlevel
, formatspecs
)
2795 upload_file_to_internet_file(catfp
, outfile
)
2804 def AppendListsWithContentToOutFile(inlist
, outfile
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
2805 formatspecs
= FormatSpecsListToDict(formatspecs
)
2806 if(outfile
!= "-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2807 if(os
.path
.exists(outfile
)):
2810 except OSError as e
:
2815 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2817 elif(re
.findall("^(ftp|ftps|sftp):\\/\\/", str(outfile
))):
2820 fbasename
= os
.path
.splitext(outfile
)[0]
2821 fextname
= os
.path
.splitext(outfile
)[1]
2822 if(not compresswholefile
and fextname
in outextlistwd
):
2823 compresswholefile
= True
2824 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
)
2825 catfp
= AppendListsWithContent(inlist
, catfp
, dirlistfromtxt
, filevalues
, extradata
, compression
,
2826 compresswholefile
, compressionlevel
, followlink
, checksumtype
, formatspecs
, verbose
)
2827 if(outfile
== "-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2828 catfp
= CompressArchiveFile(
2829 catfp
, compression
, compressionlevel
, formatspecs
)
2832 os
.fsync(catfp
.fileno())
2833 except io
.UnsupportedOperation
:
2835 except AttributeError:
2837 except OSError as e
:
2841 if(hasattr(sys
.stdout
, "buffer")):
2842 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer)
2844 shutil
.copyfileobj(catfp
, sys
.stdout
)
2845 elif(re
.findall("^(ftp|ftps|sftp):\\/\\/", str(outfile
))):
2846 catfp
= CompressArchiveFile(
2847 catfp
, compression
, compressionlevel
, formatspecs
)
2849 upload_file_to_internet_file(catfp
, outfile
)
2858 def AppendInFileWithContentToOutFile(infile
, outfile
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
2859 formatspecs
= FormatSpecsListToDict(formatspecs
)
2860 inlist
= ReadInFileBySizeWithContentToList(
2861 infile
, 0, 0, False, True, False, formatspecs
)
2862 return AppendListsWithContentToOutFile(inlist
, outfile
, dirlistfromtxt
, compression
, compresswholefile
, compressionlevel
, filevalues
, extradata
, followlink
, checksumtype
, formatspecs
, verbose
, returnfp
)
2865 def PrintPermissionString(fchmode
, ftype
):
2866 permissions
= {'access': {'0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': (
2867 'r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx')}, 'roles': {0: 'owner', 1: 'group', 2: 'other'}}
2869 for fmodval
in str(oct(fchmode
))[-3:]:
2870 permissionstr
= permissionstr
+ \
2871 permissions
['access'].get(fmodval
, '---')
2872 if(ftype
== 0 or ftype
== 7):
2873 permissionstr
= "-" + permissionstr
2875 permissionstr
= "h" + permissionstr
2877 permissionstr
= "l" + permissionstr
2879 permissionstr
= "c" + permissionstr
2881 permissionstr
= "b" + permissionstr
2883 permissionstr
= "d" + permissionstr
2885 permissionstr
= "f" + permissionstr
2887 permissionstr
= "D" + permissionstr
2889 permissionstr
= "p" + permissionstr
2891 permissionstr
= "w" + permissionstr
2893 permissionoutstr
= stat
.filemode(fchmode
)
2894 except AttributeError:
2895 permissionoutstr
= permissionstr
2897 permissionoutstr
= permissionstr
2898 return permissionoutstr
2901 def PrintPermissionStringAlt(fchmode
, ftype
):
2903 '0': '---', '1': '--x', '2': '-w-', '3': '-wx',
2904 '4': 'r--', '5': 'r-x', '6': 'rw-', '7': 'rwx'
2906 # Translate file mode into permission string
2907 permissionstr
= ''.join([permissions
[i
] for i
in str(oct(fchmode
))[-3:]])
2908 # Append file type indicator
2910 0: '-', 1: 'h', 2: 'l', 3: 'c', 4: 'b',
2911 5: 'd', 6: 'f', 8: 'D', 9: 'p', 10: 'w'
2913 file_type
= type_indicators
.get(ftype
, '-')
2914 permissionstr
= file_type
+ permissionstr
2916 permissionoutstr
= stat
.filemode(fchmode
)
2917 except AttributeError:
2918 permissionoutstr
= permissionstr
2919 return permissionoutstr
2922 def GzipCompressData(data
, compresslevel
=9):
2924 # Try using modern gzip.compress if available
2925 compressed_data
= gzip
.compress(data
, compresslevel
=compresslevel
)
2926 except AttributeError:
2927 # Fallback to older method for Python 2.x and older 3.x versions
2929 with gzip
.GzipFile(fileobj
=out
, mode
="wb", compresslevel
=compresslevel
) as f
:
2931 compressed_data
= out
.getvalue()
2932 return compressed_data
2935 def GzipDecompressData(compressed_data
):
2937 # Try using modern gzip.decompress if available
2938 decompressed_data
= gzip
.decompress(compressed_data
)
2939 except AttributeError:
2940 # Fallback to older method for Python 2.x and older 3.x versions
2941 inp
= BytesIO(compressed_data
)
2942 with gzip
.GzipFile(fileobj
=inp
, mode
="rb") as f
:
2943 decompressed_data
= f
.read()
2944 return decompressed_data
2947 def BzipCompressData(data
, compresslevel
=9):
2949 # Try using modern bz2.compress if available
2950 compressed_data
= bz2
.compress(data
, compresslevel
=compresslevel
)
2951 except AttributeError:
2952 # Fallback to older method for Python 2.x and older 3.x versions
2953 compressor
= bz2
.BZ2Compressor(compresslevel
)
2954 compressed_data
= compressor
.compress(data
)
2955 compressed_data
+= compressor
.flush()
2956 return compressed_data
2959 def BzipDecompressData(compressed_data
):
2961 # Try using modern bz2.decompress if available
2962 decompressed_data
= bz2
.decompress(compressed_data
)
2963 except AttributeError:
2964 # Fallback to older method for Python 2.x and older 3.x versions
2965 decompressor
= bz2
.BZ2Decompressor()
2966 decompressed_data
= decompressor
.decompress(compressed_data
)
2967 return decompressed_data
2970 def CheckCompressionType(infile
, formatspecs
=__file_format_dict__
, closefp
=True):
2971 formatspecs
= FormatSpecsListToDict(formatspecs
)
2972 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2976 catfp
= open(infile
, "rb")
2977 except FileNotFoundError
:
2981 prefp
= catfp
.read(2)
2982 if(prefp
== binascii
.unhexlify("1f8b")):
2984 if(prefp
== binascii
.unhexlify("7801")):
2986 if(prefp
== binascii
.unhexlify("785e")):
2988 if(prefp
== binascii
.unhexlify("789c")):
2990 if(prefp
== binascii
.unhexlify("78da")):
2993 prefp
= catfp
.read(3)
2994 if(prefp
== binascii
.unhexlify("425a68")):
2996 if(prefp
== binascii
.unhexlify("5d0000")):
2999 prefp
= catfp
.read(4)
3000 if(prefp
== binascii
.unhexlify("28b52ffd")):
3002 if(prefp
== binascii
.unhexlify("04224d18")):
3004 if(prefp
== binascii
.unhexlify("504B0304")):
3005 filetype
= "zipfile"
3007 prefp
= catfp
.read(5)
3008 if(prefp
== binascii
.unhexlify("7573746172")):
3009 filetype
= "tarfile"
3011 prefp
= catfp
.read(6)
3012 if(prefp
== binascii
.unhexlify("fd377a585a00")):
3014 if(prefp
== binascii
.unhexlify("377abcaf271c")):
3015 filetype
= "7zipfile"
3017 prefp
= catfp
.read(7)
3018 if(prefp
== binascii
.unhexlify("526172211a0700")):
3019 filetype
= "rarfile"
3020 if(prefp
== binascii
.unhexlify("43617446696c65")):
3021 filetype
= "catfile"
3023 prefp
= catfp
.read(8)
3024 if(prefp
== binascii
.unhexlify("526172211a070100")):
3025 filetype
= "rarfile"
3027 prefp
= catfp
.read(formatspecs
['format_len'])
3028 if(prefp
== binascii
.unhexlify(formatspecs
['format_hex'])):
3029 filetype
= formatspecs
['format_lower']
3031 prefp
= catfp
.read(9)
3032 if(prefp
== binascii
.unhexlify("894c5a4f000d0a1a0a")):
3035 prefp
= catfp
.read(10)
3036 if(prefp
== binascii
.unhexlify("7061785f676c6f62616c")):
3037 filetype
= "tarfile"
3039 if(filetype
== "gzip" or filetype
== "bzip2" or filetype
== "lzma" or filetype
== "zstd" or filetype
== "lz4" or filetype
== "zlib"):
3040 if(TarFileCheck(catfp
)):
3041 filetype
= "tarfile"
3043 if(TarFileCheck(catfp
)):
3044 filetype
= "tarfile"
3045 elif(zipfile
.is_zipfile(catfp
)):
3046 filetype
= "zipfile"
3047 elif(rarfile_support
and (rarfile
.is_rarfile(catfp
) or rarfile
.is_rarfile_sfx(catfp
))):
3049 elif(py7zr_support
and py7zr
.is_7zfile(catfp
)):
3059 def CheckCompressionTypeFromString(instring
, formatspecs
=__file_format_dict__
, closefp
=True):
3060 formatspecs
= FormatSpecsListToDict(formatspecs
)
3062 instringsfile
= BytesIO(instring
)
3064 instringsfile
= BytesIO(instring
.encode("UTF-8"))
3065 return CheckCompressionType(instringsfile
, formatspecs
, closefp
)
3068 def GetCompressionMimeType(infile
, formatspecs
=__file_format_dict__
):
3069 formatspecs
= FormatSpecsListToDict(formatspecs
)
3070 compresscheck
= CheckCompressionType(fp
, formatspecs
, False)
3071 if(compresscheck
== "gzip" or compresscheck
== "gz"):
3072 return archivefile_gzip_mimetype
3073 if(compresscheck
== "zlib" or (compresscheck
== "zz" or compresscheck
== "zl" or compresscheck
== "zlib")):
3074 return archivefile_zlib_mimetype
3075 if(compresscheck
== "bzip2" or compresscheck
== "bz2"):
3076 return archivefile_bzip2_mimetype
3077 if(compresscheck
== "zstd" or compresscheck
== "zstandard"):
3078 return archivefile_zstandard_mimetype
3079 if(compresscheck
== "lz4"):
3080 return archivefile_lz4_mimetype
3081 if(compresscheck
== "lzo" or compresscheck
== "lzop"):
3082 return archivefile_lzop_mimetype
3083 if(compresscheck
== "lzma"):
3084 return archivefile_lzma_mimetype
3085 if(compresscheck
== "xz"):
3086 return archivefile_xz_mimetype
3087 if(compresscheck
== "catfile" or compresscheck
== "cat" or compresscheck
== formatspecs
['format_lower']):
3088 return archivefile_cat_mimetype
3089 if(not compresscheck
):
3094 def UncompressArchiveFile(fp
, formatspecs
=__file_format_dict__
):
3095 formatspecs
= FormatSpecsListToDict(formatspecs
)
3096 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
3098 compresscheck
= CheckCompressionType(fp
, formatspecs
, False)
3099 if(compresscheck
== "gzip" and compresscheck
in compressionsupport
):
3100 catfp
= gzip
.GzipFile(fileobj
=fp
, mode
="rb")
3101 elif(compresscheck
== "bzip2" and compresscheck
in compressionsupport
):
3102 catfp
= bz2
.BZ2File(fp
)
3103 elif(compresscheck
== "zstd" and compresscheck
in compressionsupport
):
3104 catfp
= zstd
.ZstdDecompressor().stream_reader(fp
)
3105 elif(compresscheck
== "lz4" and compresscheck
in compressionsupport
):
3106 catfp
= lz4
.frame
.open_fp(fp
, mode
='rb')
3107 elif((compresscheck
== "lzo" or compresscheck
== "lzop") and compresscheck
in compressionsupport
):
3109 catfp
.write(lzo
.decompress(fp
.read()))
3110 elif((compresscheck
== "lzma" or compresscheck
== "xz") and compresscheck
in compressionsupport
):
3111 catfp
= lzma
.LZMAFile(fp
)
3112 elif(compresscheck
== "zlib" and compresscheck
in compressionsupport
):
3113 catfp
= ZlibFile(fileobj
=fp
, mode
="rb")
3114 if(compresscheck
== "catfile" or compresscheck
== formatspecs
['format_lower']):
3116 if(not compresscheck
):
3121 from backports
import lzma
3125 with fp
as fpcontent
:
3127 catfp
.write(lzma
.decompress(fp
.read()))
3128 except lzma
.LZMAError
:
3130 if(compresscheck
!= "catfile" or compresscheck
!= formatspecs
['format_lower']):
3135 create_alias_function("Uncompress", __file_format_name__
,
3136 "", UncompressArchiveFile
)
3139 def UncompressFile(infile
, formatspecs
=__file_format_dict__
, mode
="rb"):
3140 formatspecs
= FormatSpecsListToDict(formatspecs
)
3141 compresscheck
= CheckCompressionType(infile
, formatspecs
, False)
3142 if(sys
.version_info
[0] == 2 and compresscheck
):
3148 if(compresscheck
== "gzip" and compresscheck
in compressionsupport
):
3150 filefp
= gzip
.open(infile
, mode
, encoding
="UTF-8")
3151 except (ValueError, TypeError) as e
:
3152 filefp
= gzip
.open(infile
, mode
)
3153 if(compresscheck
== "bzip2" and compresscheck
in compressionsupport
):
3155 filefp
= bz2
.open(infile
, mode
, encoding
="UTF-8")
3156 except (ValueError, TypeError) as e
:
3157 filefp
= bz2
.open(infile
, mode
)
3158 if(compresscheck
== "zstd" and compresscheck
in compressionsupport
):
3160 filefp
= zstandard
.open(infile
, mode
, encoding
="UTF-8")
3161 except (ValueError, TypeError) as e
:
3162 filefp
= zstandard
.open(infile
, mode
)
3163 if(compresscheck
== "lz4" and compresscheck
in compressionsupport
):
3165 filefp
= lz4
.frame
.open(infile
, mode
, encoding
="UTF-8")
3166 except (ValueError, TypeError) as e
:
3167 filefp
= lz4
.frame
.open(infile
, mode
)
3168 if((compresscheck
== "lzo" or compresscheck
== "lzop") and compresscheck
in compressionsupport
):
3170 filefp
= lzo
.open(infile
, mode
, encoding
="UTF-8")
3171 except (ValueError, TypeError) as e
:
3172 filefp
= lzo
.open(infile
, mode
)
3173 if((compresscheck
== "lzma" or compresscheck
== "xz") and compresscheck
in compressionsupport
):
3175 filefp
= lzma
.open(infile
, mode
, encoding
="UTF-8")
3176 except (ValueError, TypeError) as e
:
3177 filefp
= lzma
.open(infile
, mode
)
3178 if(compresscheck
== "zlib" and compresscheck
in compressionsupport
):
3179 filefp
= ZlibFile(infile
, mode
=mode
)
3180 if(compresscheck
== "catfile" or compresscheck
== formatspecs
['format_lower']):
3182 filefp
= open(infile
, mode
, encoding
="UTF-8")
3183 except (ValueError, TypeError) as e
:
3184 filefp
= open(infile
, mode
)
3185 if(not compresscheck
):
3187 filefp
= open(infile
, mode
, encoding
="UTF-8")
3188 except (ValueError, TypeError) as e
:
3189 filefp
= open(infile
, mode
)
3190 except FileNotFoundError
:
3193 filefp
.write_through
= True
3194 except AttributeError:
3199 def UncompressString(infile
):
3200 compresscheck
= CheckCompressionTypeFromString(infile
, formatspecs
, False)
3201 if(compresscheck
== "gzip" and compresscheck
in compressionsupport
):
3202 fileuz
= GzipDecompressData(infile
)
3203 if(compresscheck
== "bzip2" and compresscheck
in compressionsupport
):
3204 fileuz
= BzipDecompressData(infile
)
3205 if(compresscheck
== "zstd" and compresscheck
in compressionsupport
):
3210 fileuz
= zstandard
.decompress(infile
)
3211 if(compresscheck
== "lz4" and compresscheck
in compressionsupport
):
3212 fileuz
= lz4
.frame
.decompress(infile
)
3213 if((compresscheck
== "lzo" or compresscheck
== "lzop") and compresscheck
in compressionsupport
):
3214 fileuz
= lzo
.decompress(infile
)
3215 if((compresscheck
== "lzma" or compresscheck
== "xz") and compresscheck
in compressionsupport
):
3216 fileuz
= lzma
.decompress(infile
)
3217 if(compresscheck
== "zlib" and compresscheck
in compressionsupport
):
3218 fileuz
= zlib
.decompress(infile
)
3219 if(not compresscheck
):
3221 if(hasattr(fileuz
, 'decode')):
3222 fileuz
= fileuz
.decode("UTF-8")
3226 def UncompressStringAlt(infile
):
3228 outstring
= UncompressString(infile
)
3229 filefp
.write(outstring
)
3234 def CheckCompressionSubType(infile
, formatspecs
=__file_format_dict__
, closefp
=True):
3235 formatspecs
= FormatSpecsListToDict(formatspecs
)
3236 compresscheck
= CheckCompressionType(infile
, formatspecs
, False)
3237 if(not compresscheck
):
3238 fextname
= os
.path
.splitext(infile
)[1]
3239 if(fextname
== ".gz"):
3240 compresscheck
= "gzip"
3241 elif(fextname
== ".bz2"):
3242 compresscheck
= "bzip2"
3243 elif(fextname
== ".zst"):
3244 compresscheck
= "zstd"
3245 elif(fextname
== ".lz4"):
3246 compresscheck
= "lz4"
3247 elif(fextname
== ".lzo" or fextname
== ".lzop"):
3248 compresscheck
= "lzo"
3249 elif(fextname
== ".lzma"):
3250 compresscheck
= "lzma"
3251 elif(fextname
== ".xz"):
3252 compresscheck
= "xz"
3253 elif(fextname
== ".zz" or fextname
== ".zl" or fextname
== ".zlib"):
3254 compresscheck
= "zlib"
3257 if(compresscheck
== "gzip" or compresscheck
== "bzip2" or compresscheck
== "lzma" or compresscheck
== "zstd" or compresscheck
== "lz4" or compresscheck
== "zlib"):
3258 if(TarFileCheck(infile
)):
3259 filetype
= "tarfile"
3260 if(not compresscheck
):
3261 if(TarFileCheck(infile
)):
3263 elif(zipfile
.is_zipfile(infile
)):
3265 elif(rarfile_support
and (rarfile
.is_rarfile(infile
) or rarfile
.is_rarfile_sfx(infile
))):
3267 elif(py7zr_support
and py7zr
.is_7zfile(infile
)):
3272 if(compresscheck
== "catfile"):
3274 if(compresscheck
== formatspecs
['format_lower']):
3275 return formatspecs
['format_lower']
3276 if(compresscheck
== "tarfile"):
3278 if(compresscheck
== "zipfile"):
3280 if(rarfile_support
and compresscheck
== "rarfile"):
3282 if(py7zr_support
and compresscheck
== "7zipfile" and py7zr
.is_7zfile(infile
)):
3284 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3285 catfp
= UncompressArchiveFile(infile
, formatspecs
['format_lower'])
3288 if(compresscheck
== "gzip" and compresscheck
in compressionsupport
):
3289 catfp
= gzip
.GzipFile(infile
, "rb")
3290 elif(compresscheck
== "bzip2" and compresscheck
in compressionsupport
):
3291 catfp
= bz2
.BZ2File(infile
, "rb")
3292 elif(compresscheck
== "lz4" and compresscheck
in compressionsupport
):
3293 catfp
= lz4
.frame
.open(infile
, "rb")
3294 elif(compresscheck
== "zstd" and compresscheck
in compressionsupport
):
3295 catfp
= zstandard
.open(infile
, "rb")
3296 elif((compresscheck
== "lzo" or compresscheck
== "lzop") and compresscheck
in compressionsupport
):
3297 catfp
= lzo
.open(infile
, "rb")
3298 elif((compresscheck
== "lzma" or compresscheck
== "xz") and compresscheck
in compressionsupport
):
3299 catfp
= lzma
.open(infile
, "rb")
3300 elif(compresscheck
== "zlib" and compresscheck
in compressionsupport
):
3301 catfp
= ZlibFile(infile
, mode
="rb")
3303 catfp
= open(infile
, "rb")
3304 except FileNotFoundError
:
3307 prefp
= catfp
.read(5)
3308 if(prefp
== binascii
.unhexlify("7573746172")):
3309 filetype
= "tarfile"
3311 prefp
= catfp
.read(7)
3312 if(prefp
== binascii
.unhexlify("43617446696c65")):
3313 filetype
= "catfile"
3315 prefp
= catfp
.read(formatspecs
['format_len'])
3316 if(prefp
== binascii
.unhexlify(formatspecs
['format_hex'])):
3317 filetype
= formatspecs
['format_lower']
3319 prefp
= catfp
.read(10)
3320 if(prefp
== binascii
.unhexlify("7061785f676c6f62616c")):
3321 filetype
= "tarfile"
3328 def GZipCompress(data
, compresslevel
=9):
3329 if("gzip" not in compressionsupport
):
3331 tmpfp
= tempfile
.NamedTemporaryFile("wb", delete
=False)
3333 tmpfp
= gzip
.GzipFile(tmpfp
.name
, mode
="wb", compresslevel
=compresslevel
)
3337 catfp
= open(tmpfp
.name
, "rb")
3338 except FileNotFoundError
:
3340 catdata
= catfp
.read()
3345 def CompressArchiveFile(fp
, compression
="auto", compressionlevel
=None, formatspecs
=__file_format_dict__
):
3346 formatspecs
= FormatSpecsListToDict(formatspecs
)
3347 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
3350 if(not compression
or compression
== "catfile" or compression
== formatspecs
['format_lower']):
3351 compression
= "auto"
3352 if(compression
not in compressionlist
and compression
is None):
3353 compression
= "auto"
3354 if(compression
== "gzip" and compression
in compressionsupport
):
3356 if(compressionlevel
is None):
3357 compressionlevel
= 9
3359 compressionlevel
= int(compressionlevel
)
3360 catfp
.write(GzipCompressData(
3361 fp
.read(), compresslevel
=compressionlevel
))
3362 if(compression
== "bzip2" and compression
in compressionsupport
):
3364 if(compressionlevel
is None):
3365 compressionlevel
= 9
3367 compressionlevel
= int(compressionlevel
)
3368 catfp
.write(BzipCompressData(
3369 fp
.read(), compresslevel
=compressionlevel
))
3370 if(compression
== "lz4" and compression
in compressionsupport
):
3372 if(compressionlevel
is None):
3373 compressionlevel
= 9
3375 compressionlevel
= int(compressionlevel
)
3376 catfp
.write(lz4
.frame
.compress(
3377 fp
.read(), compression_level
=compressionlevel
))
3378 if((compression
== "lzo" or compression
== "lzop") and compression
in compressionsupport
):
3380 if(compressionlevel
is None):
3381 compressionlevel
= 9
3383 compressionlevel
= int(compressionlevel
)
3384 catfp
.write(lzo
.compress(fp
.read(), compresslevel
=compressionlevel
))
3385 if(compression
== "zstd" and compression
in compressionsupport
):
3387 if(compressionlevel
is None):
3388 compressionlevel
= 10
3390 compressionlevel
= int(compressionlevel
)
3391 catfp
.write(zstandard
.compress(fp
.read(), level
=compressionlevel
))
3392 if(compression
== "lzma" and compression
in compressionsupport
):
3394 if(compressionlevel
is None):
3395 compressionlevel
= 9
3397 compressionlevel
= int(compressionlevel
)
3398 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_ALONE
, filters
=[
3399 {"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]))
3400 if(compression
== "xz" and compression
in compressionsupport
):
3402 if(compressionlevel
is None):
3403 compressionlevel
= 9
3405 compressionlevel
= int(compressionlevel
)
3406 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_XZ
, filters
=[
3407 {"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]))
3408 if(compression
== "zlib" and compression
in compressionsupport
):
3410 if(compressionlevel
is None):
3411 compressionlevel
= 9
3413 compressionlevel
= int(compressionlevel
)
3414 catfp
.write(zlib
.compress(fp
.read(), compressionlevel
))
3415 if(compression
== "auto" or compression
is None):
3421 create_alias_function("Compress", __file_format_name__
,
3422 "", CompressArchiveFile
)
3425 def CompressOpenFile(outfile
, compressionenable
=True, compressionlevel
=None):
3426 if(outfile
is None):
3428 fbasename
= os
.path
.splitext(outfile
)[0]
3429 fextname
= os
.path
.splitext(outfile
)[1]
3430 if(compressionlevel
is None and fextname
!= ".zst"):
3431 compressionlevel
= 9
3432 elif(compressionlevel
is None and fextname
== ".zst"):
3433 compressionlevel
= 10
3435 compressionlevel
= int(compressionlevel
)
3436 if(sys
.version_info
[0] == 2):
3441 if(fextname
not in outextlistwd
or not compressionenable
):
3443 outfp
= open(outfile
, "wb", encoding
="UTF-8")
3444 except (ValueError, TypeError) as e
:
3445 outfp
= open(outfile
, "wb")
3446 elif(fextname
== ".gz" and "gzip" in compressionsupport
):
3449 outfile
, mode
, compressionlevel
, encoding
="UTF-8")
3450 except (ValueError, TypeError) as e
:
3451 outfp
= gzip
.open(outfile
, mode
, compressionlevel
)
3452 elif(fextname
== ".bz2" and "bzip2" in compressionsupport
):
3455 outfile
, mode
, compressionlevel
, encoding
="UTF-8")
3456 except (ValueError, TypeError) as e
:
3457 outfp
= bz2
.open(outfile
, mode
, compressionlevel
)
3458 elif(fextname
== ".zst" and "zstandard" in compressionsupport
):
3460 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(
3461 level
=compressionlevel
), encoding
="UTF-8")
3462 except (ValueError, TypeError) as e
:
3463 outfp
= zstandard
.open(
3464 outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
))
3465 elif(fextname
== ".xz" and "xz" in compressionsupport
):
3467 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[
3468 {"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}], encoding
="UTF-8")
3469 except (ValueError, TypeError) as e
:
3470 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[
3471 {"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}])
3472 elif(fextname
== ".lz4" and "lz4" in compressionsupport
):
3474 outfp
= lz4
.frame
.open(
3475 outfile
, mode
, compression_level
=compressionlevel
, encoding
="UTF-8")
3476 except (ValueError, TypeError) as e
:
3477 outfp
= lz4
.frame
.open(
3478 outfile
, mode
, compression_level
=compressionlevel
)
3479 elif(fextname
== ".lzo" and "lzop" in compressionsupport
):
3482 outfile
, mode
, compresslevel
=compressionlevel
, encoding
="UTF-8")
3483 except (ValueError, TypeError) as e
:
3484 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
)
3485 elif(fextname
== ".lzma" and "lzma" in compressionsupport
):
3487 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[
3488 {"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}], encoding
="UTF-8")
3489 except (ValueError, TypeError) as e
:
3490 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[
3491 {"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}])
3492 elif((fextname
== ".zz" or fextname
== ".zl" or fextname
== ".zlib") and "zlib" in compressionsupport
):
3493 outfp
= ZlibFile(outfile
, mode
=mode
, level
=compressionlevel
)
3494 except FileNotFoundError
:
3497 outfp
.write_through
= True
3498 except AttributeError:
3503 def GetDevMajorMinor(fdev
):
3505 if(hasattr(os
, "minor")):
3506 retdev
.append(os
.minor(fdev
))
3509 if(hasattr(os
, "major")):
3510 retdev
.append(os
.major(fdev
))
3516 def CheckSumSupport(checkfor
, guaranteed
=True):
3519 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
))
3520 except AttributeError:
3521 hash_list
= sorted(list(hashlib
.algorithms
))
3524 hash_list
= sorted(list(hashlib
.algorithms_available
))
3525 except AttributeError:
3526 hash_list
= sorted(list(hashlib
.algorithms
))
3527 checklistout
= sorted(hash_list
+ ['adler32', 'crc16', 'crc16_ansi', 'crc16_ibm',
3528 'crc16_ccitt', 'crc32', 'crc64', 'crc64_ecma', 'crc64_iso', 'none'])
3529 if(checkfor
in checklistout
):
3535 def CheckSumSupportAlt(checkfor
, guaranteed
=True):
3538 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
))
3539 except AttributeError:
3540 hash_list
= sorted(list(hashlib
.algorithms
))
3543 hash_list
= sorted(list(hashlib
.algorithms_available
))
3544 except AttributeError:
3545 hash_list
= sorted(list(hashlib
.algorithms
))
3546 checklistout
= hash_list
3547 if(checkfor
in checklistout
):
3553 def PackArchiveFile(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
3554 formatspecs
= FormatSpecsListToDict(formatspecs
)
3555 advancedlist
= formatspecs
['use_advanced_list']
3556 altinode
= formatspecs
['use_alt_inode']
3557 if(outfile
!= "-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3558 outfile
= RemoveWindowsPath(outfile
)
3559 checksumtype
= checksumtype
.lower()
3560 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
3561 checksumtype
= "crc32"
3562 if(checksumtype
== "none"):
3564 if(not compression
or compression
== "catfile" or compression
== formatspecs
['format_lower']):
3565 compression
= "auto"
3566 if(compression
not in compressionlist
and compression
is None):
3567 compression
= "auto"
3569 logging
.basicConfig(format
="%(message)s",
3570 stream
=sys
.stdout
, level
=logging
.DEBUG
)
3571 if(outfile
!= "-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3572 if(os
.path
.exists(outfile
)):
3575 except OSError as e
:
3580 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3582 elif(re
.findall("^(ftp|ftps|sftp):\\/\\/", str(outfile
))):
3585 fbasename
= os
.path
.splitext(outfile
)[0]
3586 fextname
= os
.path
.splitext(outfile
)[1]
3587 if(not compresswholefile
and fextname
in outextlistwd
):
3588 compresswholefile
= True
3589 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
)
3590 catver
= formatspecs
['format_ver']
3591 fileheaderver
= str(int(catver
.replace(".", "")))
3594 for line
in sys
.stdin
:
3595 infilelist
.append(line
.strip())
3596 infilelist
= list(filter(None, infilelist
))
3597 elif(infiles
!= "-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
== "/dev/null" or infiles
== "NUL")):
3598 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
3600 with
UncompressFile(infiles
, formatspecs
, "r") as finfile
:
3601 for line
in finfile
:
3602 infilelist
.append(line
.strip())
3603 infilelist
= list(filter(None, infilelist
))
3605 if(isinstance(infiles
, (list, tuple, ))):
3606 infilelist
= list(filter(None, infiles
))
3607 elif(isinstance(infiles
, (str, ))):
3608 infilelist
= list(filter(None, [infiles
]))
3610 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False)
3612 GetDirList
= ListDir(infilelist
, followlink
, False)
3620 inodetocatinode
= {}
3621 numfiles
= int(len(GetDirList
))
3622 catfp
= AppendFileHeader(catfp
, numfiles
, checksumtype
, formatspecs
)
3623 for curfname
in GetDirList
:
3624 if(re
.findall("^[.|/]", curfname
)):
3627 fname
= "./"+curfname
3629 VerbosePrintOut(fname
)
3630 if(not followlink
or followlink
is None):
3631 fstatinfo
= os
.lstat(fname
)
3633 fstatinfo
= os
.stat(fname
)
3634 fpremode
= fstatinfo
.st_mode
3635 finode
= fstatinfo
.st_ino
3636 flinkcount
= fstatinfo
.st_nlink
3638 if(stat
.S_ISREG(fpremode
)):
3640 elif(stat
.S_ISLNK(fpremode
)):
3642 elif(stat
.S_ISCHR(fpremode
)):
3644 elif(stat
.S_ISBLK(fpremode
)):
3646 elif(stat
.S_ISDIR(fpremode
)):
3648 elif(stat
.S_ISFIFO(fpremode
)):
3650 elif(stat
.S_ISSOCK(fpremode
)):
3652 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
3654 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
3656 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
3661 fcurfid
= format(int(curfid
), 'x').lower()
3662 if(not followlink
and finode
!= 0):
3664 if(finode
in inodelist
):
3666 flinkname
= inodetofile
[finode
]
3668 fcurinode
= format(int(finode
), 'x').lower()
3671 int(inodetocatinode
[finode
]), 'x').lower()
3672 if(finode
not in inodelist
):
3673 inodelist
.append(finode
)
3674 inodetofile
.update({finode
: fname
})
3675 inodetocatinode
.update({finode
: curinode
})
3677 fcurinode
= format(int(finode
), 'x').lower()
3679 fcurinode
= format(int(curinode
), 'x').lower()
3680 curinode
= curinode
+ 1
3682 fcurinode
= format(int(curinode
), 'x').lower()
3683 curinode
= curinode
+ 1
3686 flinkname
= os
.readlink(fname
)
3687 fdev
= fstatinfo
.st_dev
3688 getfdev
= GetDevMajorMinor(fdev
)
3689 fdev_minor
= getfdev
[0]
3690 fdev_major
= getfdev
[1]
3691 frdev
= fstatinfo
.st_dev
3692 if(hasattr(fstatinfo
, "st_rdev")):
3693 frdev
= fstatinfo
.st_rdev
3695 frdev
= fstatinfo
.st_dev
3696 getfrdev
= GetDevMajorMinor(frdev
)
3697 frdev_minor
= getfrdev
[0]
3698 frdev_major
= getfrdev
[1]
3699 if(ftype
== 1 or ftype
== 2 or ftype
== 3 or ftype
== 4 or ftype
== 5 or ftype
== 6):
3700 fsize
= format(int("0"), 'x').lower()
3701 elif(ftype
== 0 or ftype
== 7):
3702 fsize
= format(int(fstatinfo
.st_size
), 'x').lower()
3704 fsize
= format(int(fstatinfo
.st_size
)).lower()
3705 fatime
= format(int(fstatinfo
.st_atime
), 'x').lower()
3706 fmtime
= format(int(fstatinfo
.st_mtime
), 'x').lower()
3707 fctime
= format(int(fstatinfo
.st_ctime
), 'x').lower()
3708 if(hasattr(fstatinfo
, "st_birthtime")):
3709 fbtime
= format(int(fstatinfo
.st_birthtime
), 'x').lower()
3711 fbtime
= format(int(fstatinfo
.st_ctime
), 'x').lower()
3712 fmode
= format(int(fstatinfo
.st_mode
), 'x').lower()
3713 fchmode
= format(int(stat
.S_IMODE(fstatinfo
.st_mode
)), 'x').lower()
3714 ftypemod
= format(int(stat
.S_IFMT(fstatinfo
.st_mode
)), 'x').lower()
3715 fuid
= format(int(fstatinfo
.st_uid
), 'x').lower()
3716 fgid
= format(int(fstatinfo
.st_gid
), 'x').lower()
3721 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
)
3722 funame
= userinfo
.pw_name
3731 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
)
3732 fgname
= groupinfo
.gr_name
3737 fdev_minor
= format(int(fdev_minor
), 'x').lower()
3738 fdev_major
= format(int(fdev_major
), 'x').lower()
3739 frdev_minor
= format(int(frdev_minor
), 'x').lower()
3740 frdev_major
= format(int(frdev_major
), 'x').lower()
3741 finode
= format(int(finode
), 'x').lower()
3742 flinkcount
= format(int(flinkcount
), 'x').lower()
3743 if(hasattr(fstatinfo
, "st_file_attributes")):
3744 fwinattributes
= format(
3745 int(fstatinfo
.st_file_attributes
), 'x').lower()
3747 fwinattributes
= format(int(0), 'x').lower()
3749 fcsize
= format(int(0), 'x').lower()
3750 fcontents
= BytesIO()
3751 if(ftype
== 0 or ftype
== 7):
3752 with
open(fname
, "rb") as fpc
:
3753 shutil
.copyfileobj(fpc
, fcontents
)
3754 if(not compresswholefile
):
3755 fcontents
.seek(0, 2)
3756 ucfsize
= fcontents
.tell()
3757 fcontents
.seek(0, 0)
3758 if(compression
== "auto"):
3759 ilsize
= len(compressionlistalt
)
3762 while(ilmin
< ilsize
):
3763 cfcontents
= BytesIO()
3764 shutil
.copyfileobj(fcontents
, cfcontents
)
3765 fcontents
.seek(0, 0)
3766 cfcontents
.seek(0, 0)
3767 cfcontents
= CompressArchiveFile(
3768 cfcontents
, compressionlistalt
[ilmin
], compressionlevel
, formatspecs
)
3770 cfcontents
.seek(0, 2)
3771 ilcsize
.append(cfcontents
.tell())
3775 ilcsize
.append(sys
.maxint
)
3776 except AttributeError:
3777 ilcsize
.append(sys
.maxsize
)
3779 ilcmin
= ilcsize
.index(min(ilcsize
))
3780 compression
= compressionlistalt
[ilcmin
]
3781 fcontents
.seek(0, 0)
3782 cfcontents
= BytesIO()
3783 shutil
.copyfileobj(fcontents
, cfcontents
)
3784 cfcontents
.seek(0, 0)
3785 cfcontents
= CompressArchiveFile(
3786 cfcontents
, compression
, compressionlevel
, formatspecs
)
3787 cfcontents
.seek(0, 2)
3788 cfsize
= cfcontents
.tell()
3789 if(ucfsize
> cfsize
):
3790 fcsize
= format(int(cfsize
), 'x').lower()
3791 fcompression
= compression
3793 fcontents
= cfcontents
3794 if(fcompression
== "none"):
3796 if(followlink
and (ftype
== 1 or ftype
== 2)):
3797 flstatinfo
= os
.stat(flinkname
)
3798 with
open(flinkname
, "rb") as fpc
:
3799 shutil
.copyfileobj(fpc
, fcontents
)
3800 if(not compresswholefile
):
3801 fcontents
.seek(0, 2)
3802 ucfsize
= fcontents
.tell()
3803 fcontents
.seek(0, 0)
3804 if(compression
== "auto"):
3805 ilsize
= len(compressionlistalt
)
3808 while(ilmin
< ilsize
):
3809 cfcontents
= BytesIO()
3810 shutil
.copyfileobj(fcontents
, cfcontents
)
3811 fcontents
.seek(0, 0)
3812 cfcontents
.seek(0, 0)
3813 cfcontents
= CompressArchiveFile(
3814 cfcontents
, compressionlistalt
[ilmin
], compressionlevel
, formatspecs
)
3816 cfcontents
.seek(0, 2)
3817 ilcsize
.append(cfcontents
.tell())
3821 ilcsize
.append(sys
.maxint
)
3822 except AttributeError:
3823 ilcsize
.append(sys
.maxsize
)
3825 ilcmin
= ilcsize
.index(min(ilcsize
))
3826 compression
= compressionlistalt
[ilcmin
]
3827 fcontents
.seek(0, 0)
3828 cfcontents
= BytesIO()
3829 shutil
.copyfileobj(fcontents
, cfcontents
)
3830 cfcontents
.seek(0, 0)
3831 cfcontents
= CompressArchiveFile(
3832 cfcontents
, compression
, compressionlevel
, formatspecs
)
3833 cfcontents
.seek(0, 2)
3834 cfsize
= cfcontents
.tell()
3835 if(ucfsize
> cfsize
):
3836 fcsize
= format(int(cfsize
), 'x').lower()
3837 fcompression
= compression
3839 fcontents
= cfcontents
3840 fcontents
.seek(0, 0)
3841 ftypehex
= format(ftype
, 'x').lower()
3842 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fcompression
,
3843 fcsize
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, "+1"]
3844 catfp
= AppendFileHeaderWithContent(
3845 catfp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
)
3848 catfp
.write(AppendNullBytes(
3849 [0, 0], formatspecs
['format_delimiter']).encode("UTF-8"))
3850 if(outfile
== "-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3851 catfp
= CompressArchiveFile(
3852 catfp
, compression
, compressionlevel
, formatspecs
)
3855 os
.fsync(catfp
.fileno())
3856 except io
.UnsupportedOperation
:
3858 except AttributeError:
3860 except OSError as e
:
3864 if(hasattr(sys
.stdout
, "buffer")):
3865 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer)
3867 shutil
.copyfileobj(catfp
, sys
.stdout
)
3868 elif(re
.findall("^(ftp|ftps|sftp):\\/\\/", str(outfile
))):
3869 catfp
= CompressArchiveFile(
3870 catfp
, compression
, compressionlevel
, formatspecs
)
3872 upload_file_to_internet_file(catfp
, outfile
)
3881 create_alias_function("Pack", __file_format_name__
, "", PackArchiveFile
)
3883 if(hasattr(shutil
, "register_archive_format")):
3884 def PackArchiveFileFunc(archive_name
, source_dir
, **kwargs
):
3885 return PackArchiveFile(source_dir
, archive_name
, False, "auto", True, None, False, "crc32", [], __file_format_dict__
['format_delimiter'], False, False)
3886 create_alias_function("Pack", __file_format_name__
,
3887 "Func", PackArchiveFileFunc
)
3890 def PackArchiveFileFromDirList(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
3891 formatspecs
= FormatSpecsListToDict(formatspecs
)
3892 return PackArchiveFile(infiles
, outfile
, dirlistfromtxt
, compression
, compresswholefile
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
)
3895 create_alias_function("Pack", __file_format_name__
,
3896 "FromDirList", PackArchiveFileFromDirList
)
3899 def PackArchiveFileFromTarFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
3900 formatspecs
= FormatSpecsListToDict(formatspecs
)
3901 if(outfile
!= "-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3902 outfile
= RemoveWindowsPath(outfile
)
3903 checksumtype
= checksumtype
.lower()
3904 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
3905 checksumtype
= "crc32"
3906 if(checksumtype
== "none"):
3908 if(not compression
or compression
== "catfile" or compression
== formatspecs
['format_lower']):
3909 compression
= "auto"
3910 if(compression
not in compressionlist
and compression
is None):
3911 compression
= "auto"
3913 logging
.basicConfig(format
="%(message)s",
3914 stream
=sys
.stdout
, level
=logging
.DEBUG
)
3915 if(outfile
!= "-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3916 if(os
.path
.exists(outfile
)):
3919 except OSError as e
:
3924 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3926 elif(re
.findall("^(ftp|ftps|sftp):\\/\\/", str(outfile
))):
3929 fbasename
= os
.path
.splitext(outfile
)[0]
3930 fextname
= os
.path
.splitext(outfile
)[1]
3931 if(not compresswholefile
and fextname
in outextlistwd
):
3932 compresswholefile
= True
3933 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
)
3934 catver
= formatspecs
['format_ver']
3935 fileheaderver
= str(int(catver
.replace(".", "")))
3941 inodetocatinode
= {}
3944 if(hasattr(sys
.stdin
, "buffer")):
3945 shutil
.copyfileobj(sys
.stdin
.buffer, infile
)
3947 shutil
.copyfileobj(sys
.stdin
, infile
)
3952 elif(re
.findall("^(http|https|ftp|ftps|sftp):\\/\\/", str(infile
))):
3953 infile
= download_file_from_internet_file(infile
)
3958 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
3960 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
3962 if(not tarfile
.TarFileCheck(infile
)):
3964 except AttributeError:
3965 if(not TarFileCheck(infile
)):
3968 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3969 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r")
3971 tarfp
= tarfile
.open(infile
, "r")
3972 except FileNotFoundError
:
3974 numfiles
= int(len(tarfp
.getmembers()))
3975 catfp
= AppendFileHeader(catfp
, numfiles
, checksumtype
, formatspecs
)
3976 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
3977 if(re
.findall("^[.|/]", member
.name
)):
3980 fname
= "./"+member
.name
3982 VerbosePrintOut(fname
)
3983 fpremode
= member
.mode
3984 ffullmode
= member
.mode
3988 ffullmode
= member
.mode
+ stat
.S_IFREG
3990 elif(member
.isdev()):
3991 ffullmode
= member
.mode
3993 elif(member
.islnk()):
3994 ffullmode
= member
.mode
+ stat
.S_IFREG
3996 elif(member
.issym()):
3997 ffullmode
= member
.mode
+ stat
.S_IFLNK
3999 elif(member
.ischr()):
4000 ffullmode
= member
.mode
+ stat
.S_IFCHR
4002 elif(member
.isblk()):
4003 ffullmode
= member
.mode
+ stat
.S_IFBLK
4005 elif(member
.isdir()):
4006 ffullmode
= member
.mode
+ stat
.S_IFDIR
4008 elif(member
.isfifo()):
4009 ffullmode
= member
.mode
+ stat
.S_IFIFO
4011 elif(member
.issparse()):
4012 ffullmode
= member
.mode
4015 ffullmode
= member
.mode
4018 fcurfid
= format(int(curfid
), 'x').lower()
4019 fcurinode
= format(int(curfid
), 'x').lower()
4022 flinkname
= member
.linkname
4023 fdev_minor
= format(int(member
.devminor
), 'x').lower()
4024 fdev_major
= format(int(member
.devmajor
), 'x').lower()
4025 frdev_minor
= format(int(member
.devminor
), 'x').lower()
4026 frdev_major
= format(int(member
.devmajor
), 'x').lower()
4027 if(ftype
== 1 or ftype
== 2 or ftype
== 3 or ftype
== 4 or ftype
== 5 or ftype
== 6):
4028 fsize
= format(int("0"), 'x').lower()
4029 elif(ftype
== 0 or ftype
== 7):
4030 fsize
= format(int(member
.size
), 'x').lower()
4032 fsize
= format(int(member
.size
), 'x').lower()
4033 fatime
= format(int(member
.mtime
), 'x').lower()
4034 fmtime
= format(int(member
.mtime
), 'x').lower()
4035 fctime
= format(int(member
.mtime
), 'x').lower()
4036 fbtime
= format(int(member
.mtime
), 'x').lower()
4037 fmode
= format(int(ffullmode
), 'x').lower()
4038 fchmode
= format(int(stat
.S_IMODE(ffullmode
)), 'x').lower()
4039 ftypemod
= format(int(stat
.S_IFMT(ffullmode
)), 'x').lower()
4040 fuid
= format(int(member
.uid
), 'x').lower()
4041 fgid
= format(int(member
.gid
), 'x').lower()
4042 funame
= member
.uname
4043 fgname
= member
.gname
4044 flinkcount
= format(int(flinkcount
), 'x').lower()
4045 fwinattributes
= format(int(0), 'x').lower()
4047 fcsize
= format(int(0), 'x').lower()
4048 fcontents
= BytesIO()
4049 if(ftype
== 0 or ftype
== 7):
4050 with tarfp
.extractfile(member
) as fpc
:
4051 shutil
.copyfileobj(fpc
, fcontents
)
4052 if(not compresswholefile
):
4053 fcontents
.seek(0, 2)
4054 ucfsize
= fcontents
.tell()
4055 fcontents
.seek(0, 0)
4056 if(compression
== "auto"):
4057 ilsize
= len(compressionlistalt
)
4060 while(ilmin
< ilsize
):
4061 cfcontents
= BytesIO()
4062 shutil
.copyfileobj(fcontents
, cfcontents
)
4063 fcontents
.seek(0, 0)
4064 cfcontents
.seek(0, 0)
4065 cfcontents
= CompressArchiveFile(
4066 cfcontents
, compressionlistalt
[ilmin
], compressionlevel
, formatspecs
)
4068 cfcontents
.seek(0, 2)
4069 ilcsize
.append(cfcontents
.tell())
4073 ilcsize
.append(sys
.maxint
)
4074 except AttributeError:
4075 ilcsize
.append(sys
.maxsize
)
4077 ilcmin
= ilcsize
.index(min(ilcsize
))
4078 compression
= compressionlistalt
[ilcmin
]
4079 fcontents
.seek(0, 0)
4080 cfcontents
= BytesIO()
4081 shutil
.copyfileobj(fcontents
, cfcontents
)
4082 cfcontents
.seek(0, 0)
4083 cfcontents
= CompressArchiveFile(
4084 cfcontents
, compression
, compressionlevel
, formatspecs
)
4085 cfcontents
.seek(0, 2)
4086 cfsize
= cfcontents
.tell()
4087 if(ucfsize
> cfsize
):
4088 fcsize
= format(int(cfsize
), 'x').lower()
4089 fcompression
= compression
4091 fcontents
= cfcontents
4092 if(fcompression
== "none"):
4094 fcontents
.seek(0, 0)
4095 ftypehex
= format(ftype
, 'x').lower()
4096 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fcompression
,
4097 fcsize
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, "+1"]
4098 catfp
= AppendFileHeaderWithContent(
4099 catfp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
)
4102 catfp
.write(AppendNullBytes(
4103 [0, 0], formatspecs
['format_delimiter']).encode("UTF-8"))
4104 if(outfile
== "-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
4105 catfp
= CompressArchiveFile(
4106 catfp
, compression
, compressionlevel
, formatspecs
)
4109 os
.fsync(catfp
.fileno())
4110 except io
.UnsupportedOperation
:
4112 except AttributeError:
4114 except OSError as e
:
4118 if(hasattr(sys
.stdout
, "buffer")):
4119 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer)
4121 shutil
.copyfileobj(catfp
, sys
.stdout
)
4122 elif(re
.findall("^(ftp|ftps|sftp):\\/\\/", str(outfile
))):
4123 catfp
= CompressArchiveFile(
4124 catfp
, compression
, compressionlevel
, formatspecs
)
4126 upload_file_to_internet_file(catfp
, outfile
)
4135 create_alias_function("Pack", __file_format_name__
,
4136 "FromTarFile", PackArchiveFileFromTarFile
)
4139 def PackArchiveFileFromZipFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
4140 formatspecs
= FormatSpecsListToDict(formatspecs
)
4141 if(outfile
!= "-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
4142 outfile
= RemoveWindowsPath(outfile
)
4143 checksumtype
= checksumtype
.lower()
4144 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
4145 checksumtype
= "crc32"
4146 if(checksumtype
== "none"):
4148 if(not compression
or compression
== "catfile" or compression
== formatspecs
['format_lower']):
4149 compression
= "auto"
4150 if(compression
not in compressionlist
and compression
is None):
4151 compression
= "auto"
4153 logging
.basicConfig(format
="%(message)s",
4154 stream
=sys
.stdout
, level
=logging
.DEBUG
)
4155 if(outfile
!= "-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
4156 if(os
.path
.exists(outfile
)):
4159 except OSError as e
:
4164 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
4166 elif(re
.findall("^(ftp|ftps|sftp):\\/\\/", str(outfile
))):
4169 fbasename
= os
.path
.splitext(outfile
)[0]
4170 fextname
= os
.path
.splitext(outfile
)[1]
4171 if(not compresswholefile
and fextname
in outextlistwd
):
4172 compresswholefile
= True
4173 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
)
4174 catver
= formatspecs
['format_ver']
4175 fileheaderver
= str(int(catver
.replace(".", "")))
4181 inodetocatinode
= {}
4184 if(hasattr(sys
.stdin
, "buffer")):
4185 shutil
.copyfileobj(sys
.stdin
.buffer, infile
)
4187 shutil
.copyfileobj(sys
.stdin
, infile
)
4192 elif(re
.findall("^(http|https|ftp|ftps|sftp):\\/\\/", str(infile
))):
4193 infile
= download_file_from_internet_file(infile
)
4198 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
4200 if(not zipfile
.is_zipfile(infile
)):
4203 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True)
4204 except FileNotFoundError
:
4206 ziptest
= zipfp
.testzip()
4208 VerbosePrintOut("Bad file found!")
4209 numfiles
= int(len(zipfp
.infolist()))
4210 catfp
= AppendFileHeader(catfp
, numfiles
, checksumtype
, formatspecs
)
4211 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
4212 if(re
.findall("^[.|/]", member
.filename
)):
4213 fname
= member
.filename
4215 fname
= "./"+member
.filename
4216 zipinfo
= zipfp
.getinfo(member
.filename
)
4218 VerbosePrintOut(fname
)
4219 if(not member
.is_dir()):
4220 fpremode
= int(stat
.S_IFREG
+ 438)
4221 elif(member
.is_dir()):
4222 fpremode
= int(stat
.S_IFDIR
+ 511)
4225 if(not member
.is_dir()):
4227 elif(member
.is_dir()):
4230 fcurfid
= format(int(curfid
), 'x').lower()
4231 fcurinode
= format(int(curfid
), 'x').lower()
4233 fdev_minor
= format(int(0), 'x').lower()
4234 fdev_major
= format(int(0), 'x').lower()
4235 frdev_minor
= format(int(0), 'x').lower()
4236 frdev_major
= format(int(0), 'x').lower()
4238 fsize
= format(int("0"), 'x').lower()
4240 fsize
= format(int(member
.file_size
), 'x').lower()
4242 fsize
= format(int(member
.file_size
), 'x').lower()
4244 int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower()
4246 int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower()
4248 int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower()
4250 int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower()
4251 if(zipinfo
.create_system
== 0 or zipinfo
.create_system
== 10):
4252 fwinattributes
= format(int(zipinfo
.external_attr
), 'x').lower()
4253 if(not member
.is_dir()):
4254 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower()
4255 fchmode
= stat
.S_IMODE(int(stat
.S_IFREG
+ 438))
4256 ftypemod
= stat
.S_IFMT(int(stat
.S_IFREG
+ 438))
4257 elif(member
.is_dir()):
4258 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower()
4259 fchmode
= stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))
4260 ftypemod
= stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))
4261 elif(zipinfo
.create_system
== 3):
4262 fwinattributes
= format(int(0), 'x').lower()
4264 fmode
= format(int(zipinfo
.external_attr
), 'x').lower()
4265 prefmode
= int(zipinfo
.external_attr
)
4266 fchmode
= stat
.S_IMODE(prefmode
)
4267 ftypemod
= stat
.S_IFMT(prefmode
)
4268 except OverflowError:
4269 fmode
= format(int(zipinfo
.external_attr
>> 16), 'x').lower()
4270 prefmode
= int(zipinfo
.external_attr
>> 16)
4271 fchmode
= stat
.S_IMODE(prefmode
)
4272 ftypemod
= stat
.S_IFMT(prefmode
)
4274 fwinattributes
= format(int(0), 'x').lower()
4275 if(not member
.is_dir()):
4276 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower()
4277 prefmode
= int(stat
.S_IFREG
+ 438)
4278 fchmode
= stat
.S_IMODE(prefmode
)
4279 ftypemod
= stat
.S_IFMT(prefmode
)
4280 elif(member
.is_dir()):
4281 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower()
4282 prefmode
= int(stat
.S_IFDIR
+ 511)
4283 fchmode
= stat
.S_IMODE(prefmode
)
4284 ftypemod
= stat
.S_IFMT(prefmode
)
4286 fcsize
= format(int(0), 'x').lower()
4288 fuid
= format(int(os
.getuid()), 'x').lower()
4289 except AttributeError:
4290 fuid
= format(int(0), 'x').lower()
4292 fuid
= format(int(0), 'x').lower()
4294 fgid
= format(int(os
.getgid()), 'x').lower()
4295 except AttributeError:
4296 fgid
= format(int(0), 'x').lower()
4298 fgid
= format(int(0), 'x').lower()
4302 userinfo
= pwd
.getpwuid(os
.getuid())
4303 funame
= userinfo
.pw_name
4306 except AttributeError:
4314 groupinfo
= grp
.getgrgid(os
.getgid())
4315 fgname
= groupinfo
.gr_name
4318 except AttributeError:
4322 fcontents
= BytesIO()
4324 fcontents
.write(zipfp
.read(member
.filename
))
4325 if(not compresswholefile
):
4326 fcontents
.seek(0, 2)
4327 ucfsize
= fcontents
.tell()
4328 fcontents
.seek(0, 0)
4329 if(compression
== "auto"):
4330 ilsize
= len(compressionlistalt
)
4333 while(ilmin
< ilsize
):
4334 cfcontents
= BytesIO()
4335 shutil
.copyfileobj(fcontents
, cfcontents
)
4336 fcontents
.seek(0, 0)
4337 cfcontents
.seek(0, 0)
4338 cfcontents
= CompressArchiveFile(
4339 cfcontents
, compressionlistalt
[ilmin
], compressionlevel
, formatspecs
)
4340 cfcontents
.seek(0, 2)
4341 ilcsize
.append(cfcontents
.tell())
4344 ilcmin
= ilcsize
.index(min(ilcsize
))
4345 compression
= compressionlistalt
[ilcmin
]
4346 fcontents
.seek(0, 0)
4347 cfcontents
= BytesIO()
4348 shutil
.copyfileobj(fcontents
, cfcontents
)
4349 cfcontents
.seek(0, 0)
4350 cfcontents
= CompressArchiveFile(
4351 cfcontents
, compression
, compressionlevel
, formatspecs
)
4352 cfcontents
.seek(0, 2)
4353 cfsize
= cfcontents
.tell()
4354 if(ucfsize
> cfsize
):
4355 fcsize
= format(int(cfsize
), 'x').lower()
4356 fcompression
= compression
4358 fcontents
= cfcontents
4359 if(fcompression
== "none"):
4361 fcontents
.seek(0, 0)
4362 ftypehex
= format(ftype
, 'x').lower()
4363 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fcompression
,
4364 fcsize
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, "+1"]
4365 catfp
= AppendFileHeaderWithContent(
4366 catfp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
)
4369 catfp
.write(AppendNullBytes(
4370 [0, 0], formatspecs
['format_delimiter']).encode("UTF-8"))
4371 if(outfile
== "-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
4372 catfp
= CompressArchiveFile(
4373 catfp
, compression
, compressionlevel
, formatspecs
)
4376 os
.fsync(catfp
.fileno())
4377 except io
.UnsupportedOperation
:
4379 except AttributeError:
4381 except OSError as e
:
4385 if(hasattr(sys
.stdout
, "buffer")):
4386 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer)
4388 shutil
.copyfileobj(catfp
, sys
.stdout
)
4389 elif(re
.findall("^(ftp|ftps|sftp):\\/\\/", str(outfile
))):
4390 catfp
= CompressArchiveFile(
4391 catfp
, compression
, compressionlevel
, formatspecs
)
4393 upload_file_to_internet_file(catfp
, outfile
)
4402 create_alias_function("Pack", __file_format_name__
,
4403 "FromZipFile", PackArchiveFileFromZipFile
)
4405 if(not rarfile_support
):
4406 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
4409 if(rarfile_support
):
4410 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
4411 formatspecs
= FormatSpecsListToDict(formatspecs
)
4412 if(outfile
!= "-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
4413 outfile
= RemoveWindowsPath(outfile
)
4414 checksumtype
= checksumtype
.lower()
4415 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
4416 checksumtype
= "crc32"
4417 if(checksumtype
== "none"):
4419 if(not compression
or compression
== "catfile" or compression
== formatspecs
['format_lower']):
4420 compression
= "auto"
4421 if(compression
not in compressionlist
and compression
is None):
4422 compression
= "auto"
4424 logging
.basicConfig(format
="%(message)s",
4425 stream
=sys
.stdout
, level
=logging
.DEBUG
)
4426 if(outfile
!= "-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
4427 if(os
.path
.exists(outfile
)):
4430 except OSError as e
:
4435 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
4437 elif(re
.findall("^(ftp|ftps|sftp):\\/\\/", str(outfile
))):
4440 fbasename
= os
.path
.splitext(outfile
)[0]
4441 fextname
= os
.path
.splitext(outfile
)[1]
4442 if(not compresswholefile
and fextname
in outextlistwd
):
4443 compresswholefile
= True
4444 catfp
= CompressOpenFile(
4445 outfile
, compresswholefile
, compressionlevel
)
4446 catver
= formatspecs
['format_ver']
4447 fileheaderver
= str(int(catver
.replace(".", "")))
4453 inodetocatinode
= {}
4454 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
4456 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
4458 rarfp
= rarfile
.RarFile(infile
, "r")
4459 rartest
= rarfp
.testrar()
4461 VerbosePrintOut("Bad file found!")
4462 numfiles
= int(len(rarfp
.infolist()))
4463 catfp
= AppendFileHeader(catfp
, numfiles
, checksumtype
, formatspecs
)
4466 os
.fsync(catfp
.fileno())
4467 except io
.UnsupportedOperation
:
4469 except AttributeError:
4471 except OSError as e
:
4473 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
4476 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
4479 member
.external_attr
4481 except AttributeError:
4483 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
4486 member
.external_attr
4488 except AttributeError:
4493 if(re
.findall("^[.|/]", member
.filename
)):
4494 fname
= member
.filename
4496 fname
= "./"+member
.filename
4497 rarinfo
= rarfp
.getinfo(member
.filename
)
4499 VerbosePrintOut(fname
)
4500 if(is_unix
and member
.external_attr
!= 0):
4501 fpremode
= int(member
.external_attr
)
4502 elif(member
.is_file()):
4503 fpremode
= int(stat
.S_IFREG
+ 438)
4504 elif(member
.is_symlink()):
4505 fpremode
= int(stat
.S_IFLNK
+ 438)
4506 elif(member
.is_dir()):
4507 fpremode
= int(stat
.S_IFDIR
+ 511)
4508 if(is_windows
and member
.external_attr
!= 0):
4509 fwinattributes
= format(int(member
.external_attr
), 'x').lower()
4511 fwinattributes
= format(int(0), 'x').lower()
4513 fcsize
= format(int(0), 'x').lower()
4516 if(member
.is_file()):
4518 elif(member
.is_symlink()):
4520 elif(member
.is_dir()):
4524 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8")
4525 fcurfid
= format(int(curfid
), 'x').lower()
4526 fcurinode
= format(int(curfid
), 'x').lower()
4528 fdev_minor
= format(int(0), 'x').lower()
4529 fdev_major
= format(int(0), 'x').lower()
4530 frdev_minor
= format(int(0), 'x').lower()
4531 frdev_major
= format(int(0), 'x').lower()
4533 fsize
= format(int("0"), 'x').lower()
4535 fsize
= format(int(member
.file_size
), 'x').lower()
4537 fsize
= format(int(member
.file_size
), 'x').lower()
4540 fatime
= format(int(member
.atime
.timestamp()), 'x').lower()
4542 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower()
4543 except AttributeError:
4544 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower()
4545 fmtime
= format(int(member
.mtime
.timestamp()), 'x').lower()
4548 fctime
= format(int(member
.ctime
.timestamp()), 'x').lower()
4550 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower()
4551 except AttributeError:
4552 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower()
4553 fbtime
= format(int(member
.mtime
.timestamp()), 'x').lower()
4554 if(is_unix
and member
.external_attr
!= 0):
4555 fmode
= format(int(member
.external_attr
), 'x').lower()
4557 int(stat
.S_IMODE(member
.external_attr
)), 'x').lower()
4559 int(stat
.S_IFMT(member
.external_attr
)), 'x').lower()
4560 elif(member
.is_file()):
4561 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower()
4563 int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower()
4565 int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower()
4566 elif(member
.is_symlink()):
4567 fmode
= format(int(stat
.S_IFLNK
+ 438), 'x').lower()
4569 int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower()
4571 int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower()
4572 elif(member
.is_dir()):
4573 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower()
4575 int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower()
4577 int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower()
4579 fuid
= format(int(os
.getuid()), 'x').lower()
4580 except AttributeError:
4581 fuid
= format(int(0), 'x').lower()
4583 fuid
= format(int(0), 'x').lower()
4585 fgid
= format(int(os
.getgid()), 'x').lower()
4586 except AttributeError:
4587 fgid
= format(int(0), 'x').lower()
4589 fgid
= format(int(0), 'x').lower()
4593 userinfo
= pwd
.getpwuid(os
.getuid())
4594 funame
= userinfo
.pw_name
4597 except AttributeError:
4605 groupinfo
= grp
.getgrgid(os
.getgid())
4606 fgname
= groupinfo
.gr_name
4609 except AttributeError:
4613 fcontents
= BytesIO()
4615 fcontents
.write(rarfp
.read(member
.filename
))
4616 if(not compresswholefile
):
4617 fcontents
.seek(0, 2)
4618 ucfsize
= fcontents
.tell()
4619 fcontents
.seek(0, 0)
4620 if(compression
== "auto"):
4621 ilsize
= len(compressionlistalt
)
4624 while(ilmin
< ilsize
):
4625 cfcontents
= BytesIO()
4626 shutil
.copyfileobj(fcontents
, cfcontents
)
4627 fcontents
.seek(0, 0)
4628 cfcontents
.seek(0, 0)
4629 cfcontents
= CompressArchiveFile(
4630 cfcontents
, compressionlistalt
[ilmin
], compressionlevel
, formatspecs
)
4632 cfcontents
.seek(0, 2)
4633 ilcsize
.append(cfcontents
.tell())
4637 ilcsize
.append(sys
.maxint
)
4638 except AttributeError:
4639 ilcsize
.append(sys
.maxsize
)
4641 ilcmin
= ilcsize
.index(min(ilcsize
))
4642 compression
= compressionlistalt
[ilcmin
]
4643 fcontents
.seek(0, 0)
4644 cfcontents
= BytesIO()
4645 shutil
.copyfileobj(fcontents
, cfcontents
)
4646 cfcontents
.seek(0, 0)
4647 cfcontents
= CompressArchiveFile(
4648 cfcontents
, compression
, compressionlevel
, formatspecs
)
4649 cfcontents
.seek(0, 2)
4650 cfsize
= cfcontents
.tell()
4651 if(ucfsize
> cfsize
):
4652 fcsize
= format(int(cfsize
), 'x').lower()
4653 fcompression
= compression
4655 fcontents
= cfcontents
4656 if(fcompression
== "none"):
4658 fcontents
.seek(0, 0)
4659 ftypehex
= format(ftype
, 'x').lower()
4660 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fcompression
,
4661 fcsize
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, "+1"]
4662 catfp
= AppendFileHeaderWithContent(
4663 catfp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
)
4666 catfp
.write(AppendNullBytes(
4667 [0, 0], formatspecs
['format_delimiter']).encode("UTF-8"))
4668 if(outfile
== "-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
4669 catfp
= CompressArchiveFile(
4670 catfp
, compression
, compressionlevel
, formatspecs
)
4673 os
.fsync(catfp
.fileno())
4674 except io
.UnsupportedOperation
:
4676 except AttributeError:
4678 except OSError as e
:
4682 if(hasattr(sys
.stdout
, "buffer")):
4683 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer)
4685 shutil
.copyfileobj(catfp
, sys
.stdout
)
4686 elif(re
.findall("^(ftp|ftps|sftp):\\/\\/", str(outfile
))):
4687 catfp
= CompressArchiveFile(
4688 catfp
, compression
, compressionlevel
, formatspecs
)
4690 upload_file_to_internet_file(catfp
, outfile
)
4698 create_alias_function("Pack", __file_format_name__
,
4699 "FromRarFile", PackArchiveFileFromRarFile
)
4701 if(not py7zr_support
):
4702 def PackArchiveFileFromSevenZipFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
4706 def PackArchiveFileFromSevenZipFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
4707 formatspecs
= FormatSpecsListToDict(formatspecs
)
4708 if(outfile
!= "-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
4709 outfile
= RemoveWindowsPath(outfile
)
4710 checksumtype
= checksumtype
.lower()
4711 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
4712 checksumtype
= "crc32"
4713 if(checksumtype
== "none"):
4715 if(not compression
or compression
== "catfile" or compression
== formatspecs
['format_lower']):
4716 compression
= "auto"
4717 if(compression
not in compressionlist
and compression
is None):
4718 compression
= "auto"
4720 logging
.basicConfig(format
="%(message)s",
4721 stream
=sys
.stdout
, level
=logging
.DEBUG
)
4722 if(outfile
!= "-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
4723 if(os
.path
.exists(outfile
)):
4726 except OSError as e
:
4731 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
4733 elif(re
.findall("^(ftp|ftps|sftp):\\/\\/", str(outfile
))):
4736 fbasename
= os
.path
.splitext(outfile
)[0]
4737 fextname
= os
.path
.splitext(outfile
)[1]
4738 if(not compresswholefile
and fextname
in outextlistwd
):
4739 compresswholefile
= True
4740 catfp
= CompressOpenFile(
4741 outfile
, compresswholefile
, compressionlevel
)
4742 catver
= formatspecs
['format_ver']
4743 fileheaderver
= str(int(catver
.replace(".", "")))
4749 inodetocatinode
= {}
4750 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
4752 szpfp
= py7zr
.SevenZipFile(infile
, mode
="r")
4753 file_content
= szpfp
.readall()
4754 #sztest = szpfp.testzip();
4755 sztestalt
= szpfp
.test()
4757 VerbosePrintOut("Bad file found!")
4758 numfiles
= int(len(szpfp
.list()))
4759 AppendFileHeader(catfp
, numfiles
, checksumtype
, formatspecs
)
4760 for member
in sorted(szpfp
.list(), key
=lambda x
: x
.filename
):
4761 if(re
.findall("^[.|/]", member
.filename
)):
4762 fname
= member
.filename
4764 fname
= "./"+member
.filename
4766 VerbosePrintOut(fname
)
4767 if(not member
.is_directory
):
4768 fpremode
= int(stat
.S_IFREG
+ 438)
4769 elif(member
.is_directory
):
4770 fpremode
= int(stat
.S_IFDIR
+ 511)
4771 fwinattributes
= format(int(0), 'x').lower()
4773 fcsize
= format(int(0), 'x').lower()
4776 if(member
.is_directory
):
4781 fcurfid
= format(int(curfid
), 'x').lower()
4782 fcurinode
= format(int(curfid
), 'x').lower()
4784 fdev_minor
= format(int(0), 'x').lower()
4785 fdev_major
= format(int(0), 'x').lower()
4786 frdev_minor
= format(int(0), 'x').lower()
4787 frdev_major
= format(int(0), 'x').lower()
4789 fsize
= format(int("0"), 'x').lower()
4790 fatime
= format(int(member
.creationtime
.timestamp()), 'x').lower()
4791 fmtime
= format(int(member
.creationtime
.timestamp()), 'x').lower()
4792 fctime
= format(int(member
.creationtime
.timestamp()), 'x').lower()
4793 fbtime
= format(int(member
.creationtime
.timestamp()), 'x').lower()
4794 if(member
.is_directory
):
4795 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower()
4797 int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower()
4799 int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower()
4801 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower()
4803 int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower()
4805 int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower()
4807 fuid
= format(int(os
.getuid()), 'x').lower()
4808 except AttributeError:
4809 fuid
= format(int(0), 'x').lower()
4811 fuid
= format(int(0), 'x').lower()
4813 fgid
= format(int(os
.getgid()), 'x').lower()
4814 except AttributeError:
4815 fgid
= format(int(0), 'x').lower()
4817 fgid
= format(int(0), 'x').lower()
4821 userinfo
= pwd
.getpwuid(os
.getuid())
4822 funame
= userinfo
.pw_name
4825 except AttributeError:
4833 groupinfo
= grp
.getgrgid(os
.getgid())
4834 fgname
= groupinfo
.gr_name
4837 except AttributeError:
4841 fcontents
= BytesIO()
4843 fcontents
.write(file_content
[member
.filename
].read())
4844 fsize
= format(fcontents
.tell(), 'x').lower()
4845 file_content
[member
.filename
].close()
4846 if(not compresswholefile
):
4847 fcontents
.seek(0, 2)
4848 ucfsize
= fcontents
.tell()
4849 fcontents
.seek(0, 0)
4850 if(compression
== "auto"):
4851 ilsize
= len(compressionlistalt
)
4854 while(ilmin
< ilsize
):
4855 cfcontents
= BytesIO()
4856 shutil
.copyfileobj(fcontents
, cfcontents
)
4857 fcontents
.seek(0, 0)
4858 cfcontents
.seek(0, 0)
4859 cfcontents
= CompressArchiveFile(
4860 cfcontents
, compressionlistalt
[ilmin
], compressionlevel
, formatspecs
)
4862 cfcontents
.seek(0, 2)
4863 ilcsize
.append(cfcontents
.tell())
4867 ilcsize
.append(sys
.maxint
)
4868 except AttributeError:
4869 ilcsize
.append(sys
.maxsize
)
4871 ilcmin
= ilcsize
.index(min(ilcsize
))
4872 compression
= compressionlistalt
[ilcmin
]
4873 fcontents
.seek(0, 0)
4874 cfcontents
= BytesIO()
4875 shutil
.copyfileobj(fcontents
, cfcontents
)
4876 cfcontents
.seek(0, 0)
4877 cfcontents
= CompressArchiveFile(
4878 cfcontents
, compression
, compressionlevel
, formatspecs
)
4879 cfcontents
.seek(0, 2)
4880 cfsize
= cfcontents
.tell()
4881 if(ucfsize
> cfsize
):
4882 fcsize
= format(int(cfsize
), 'x').lower()
4883 fcompression
= compression
4885 fcontents
= cfcontents
4886 if(fcompression
== "none"):
4888 fcontents
.seek(0, 0)
4889 ftypehex
= format(ftype
, 'x').lower()
4890 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fcompression
,
4891 fcsize
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, "+1"]
4892 catfp
= AppendFileHeaderWithContent(
4893 catfp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
)
4896 catfp
.write(AppendNullBytes(
4897 [0, 0], formatspecs
['format_delimiter']).encode("UTF-8"))
4898 if(outfile
== "-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
4899 catfp
= CompressArchiveFile(
4900 catfp
, compression
, compressionlevel
, formatspecs
)
4903 os
.fsync(catfp
.fileno())
4904 except io
.UnsupportedOperation
:
4906 except AttributeError:
4908 except OSError as e
:
4912 if(hasattr(sys
.stdout
, "buffer")):
4913 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer)
4915 shutil
.copyfileobj(catfp
, sys
.stdout
)
4916 elif(re
.findall("^(ftp|ftps|sftp):\\/\\/", str(outfile
))):
4917 catfp
= CompressArchiveFile(
4918 catfp
, compression
, compressionlevel
, formatspecs
)
4920 upload_file_to_internet_file(catfp
, outfile
)
4928 create_alias_function("Pack", __file_format_name__
,
4929 "FromSevenZipFile", PackArchiveFileFromSevenZipFile
)
4932 def PackArchiveFileFromInFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
4933 formatspecs
= FormatSpecsListToDict(formatspecs
)
4934 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True)
4936 logging
.basicConfig(format
="%(message)s",
4937 stream
=sys
.stdout
, level
=logging
.DEBUG
)
4938 if(checkcompressfile
== "tarfile" and TarFileCheck(infile
)):
4939 return PackArchiveFileFromTarFile(infile
, outfile
, compression
, compresswholefile
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
)
4940 elif(checkcompressfile
== "zipfile" and zipfile
.is_zipfile(infile
)):
4941 return PackArchiveFileFromZipFile(infile
, outfile
, compression
, compresswholefile
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
)
4942 elif(rarfile_support
and checkcompressfile
== "rarfile" and (rarfile
.is_rarfile(infile
) or rarfile
.is_rarfile_sfx(infile
))):
4943 return PackArchiveFileFromRarFile(infile
, outfile
, compression
, compresswholefile
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
)
4944 elif(py7zr_support
and checkcompressfile
== "7zipfile" and py7zr
.is_7zfile(infile
)):
4945 return PackArchiveFileFromSevenZipFile(infile
, outfile
, compression
, compresswholefile
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
)
4946 elif(checkcompressfile
== "catfile"):
4947 return RePackArchiveFile(infile
, outfile
, compression
, compresswholefile
, compressionlevel
, False, 0, 0, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
)
4953 create_alias_function("Pack", __file_format_name__
,
4954 "FromInFile", PackArchiveFileFromInFile
)
4957 def ArchiveFileSeekToFileNum(infile
, seekto
=0, listonly
=False, contentasfile
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
4958 formatspecs
= FormatSpecsListToDict(formatspecs
)
4959 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4962 catfp
= UncompressArchiveFile(catfp
, formatspecs
)
4963 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True)
4964 if(checkcompressfile
== "tarfile" and TarFileCheck(infile
)):
4965 return TarFileToArray(infile
, seekto
, 0, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
4966 if(checkcompressfile
== "zipfile" and zipfile
.is_zipfile(infile
)):
4967 return ZipFileToArray(infile
, seekto
, 0, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
4968 if(rarfile_support
and checkcompressfile
== "rarfile" and (rarfile
.is_rarfile(infile
) or rarfile
.is_rarfile_sfx(infile
))):
4969 return RarFileToArray(infile
, seekto
, 0, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
4970 if(py7zr_support
and checkcompressfile
== "7zipfile" and py7zr
.is_7zfile(infile
)):
4971 return SevenZipFileToArray(infile
, seekto
, 0, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
4972 if(checkcompressfile
!= "catfile" and checkcompressfile
!= formatspecs
['format_lower']):
4977 elif(infile
== "-"):
4979 if(hasattr(sys
.stdin
, "buffer")):
4980 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
)
4982 shutil
.copyfileobj(sys
.stdin
, catfp
)
4984 catfp
= UncompressArchiveFile(catfp
, formatspecs
)
4988 elif(re
.findall("^(http|https|ftp|ftps|sftp):\\/\\/", str(infile
))):
4989 catfp
= download_file_from_internet_file(infile
)
4991 catfp
= UncompressArchiveFile(catfp
, formatspecs
)
4996 infile
= RemoveWindowsPath(infile
)
4997 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True)
4998 if(checkcompressfile
== "tarfile" and TarFileCheck(infile
)):
4999 return TarFileToArray(infile
, seekto
, 0, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
5000 if(checkcompressfile
== "zipfile" and zipfile
.is_zipfile(infile
)):
5001 return ZipFileToArray(infile
, seekto
, 0, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
5002 if(rarfile_support
and checkcompressfile
== "rarfile" and (rarfile
.is_rarfile(infile
) or rarfile
.is_rarfile_sfx(infile
))):
5003 return RarFileToArray(infile
, seekto
, 0, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
5004 if(py7zr_support
and checkcompressfile
== "7zipfile" and py7zr
.is_7zfile(infile
)):
5005 return SevenZipFileToArray(infile
, seekto
, 0, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
5006 if(checkcompressfile
!= "catfile" and checkcompressfile
!= formatspecs
['format_lower']):
5008 compresscheck
= CheckCompressionType(infile
, formatspecs
, True)
5009 if(not compresscheck
):
5010 fextname
= os
.path
.splitext(infile
)[1]
5011 if(fextname
== ".gz"):
5012 compresscheck
= "gzip"
5013 elif(fextname
== ".bz2"):
5014 compresscheck
= "bzip2"
5015 elif(fextname
== ".zst"):
5016 compresscheck
= "zstd"
5017 elif(fextname
== ".lz4" or fextname
== ".clz4"):
5018 compresscheck
= "lz4"
5019 elif(fextname
== ".lzo" or fextname
== ".lzop"):
5020 compresscheck
= "lzo"
5021 elif(fextname
== ".lzma"):
5022 compresscheck
= "lzma"
5023 elif(fextname
== ".xz"):
5024 compresscheck
= "xz"
5025 elif(fextname
== ".zz" or fextname
== ".zl" or fextname
== ".zlib"):
5026 compresscheck
= "zlib"
5029 if(not compresscheck
):
5031 catfp
= UncompressFile(infile
, formatspecs
, "rb")
5036 SeekToEndOfFile(catfp);
5038 SeekToEndOfFile(catfp);
5039 CatSize = catfp.tell();
5040 CatSizeEnd = CatSize;
5048 curloc
= catfp
.tell()
5051 catheaderver
= str(int(formatspecs
['format_ver'].replace(".", "")))
5052 catstring
= catfp
.read(len(formatspecs
['format_magic']+catheaderver
)).decode("UTF-8")
5053 catdel
= catfp
.read(1).decode("UTF-8")
5054 if(catstring
!= formatspecs
['format_magic']+catheaderver
):
5056 if(catdel
!= formatspecs
['format_delimiter']):
5058 catheader
= ReadFileHeaderData(catfp
, 3, formatspecs
['format_delimiter'])
5060 catfp
.seek(curloc
, 0)
5061 catversion
= re
.findall("([\\d]+)", catstring
)
5062 fprenumfiles
= catheader
[0]
5063 fnumfiles
= int(fprenumfiles
, 16)
5064 fprechecksumtype
= catheader
[1]
5065 fprechecksum
= catheader
[2]
5066 fileheader
= AppendNullByte(catstring
, formatspecs
['format_delimiter'])
5067 fnumfileshex
= format(int(fnumfiles
), 'x').lower()
5068 fileheader
= fileheader
+ \
5069 AppendNullBytes([fnumfileshex
, fprechecksumtype
],
5070 formatspecs
['format_delimiter'])
5071 catfileheadercshex
= GetFileChecksum(
5072 fileheader
, fprechecksumtype
, True, formatspecs
)
5073 fileheader
= fileheader
+ \
5074 AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter'])
5075 fheadtell
= len(fileheader
)
5076 if(fprechecksum
!= catfileheadercshex
and not skipchecksum
):
5077 VerbosePrintOut("File Header Checksum Error with file " +
5078 infile
+ " at offset " + str(0))
5079 VerbosePrintOut("'" + str(fprechecksum
) + "' != " +
5080 "'" + str(catfileheadercshex
) + "'")
5082 catversions
= re
.search('(.*?)(\\d+)', catstring
).groups()
5083 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1],
5084 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}}
5085 if(seekto
>= fnumfiles
):
5086 seekto
= fnumfiles
- 1
5092 prefhstart
= catfp
.tell()
5093 if(formatspecs
['new_style']):
5094 preheaderdata
= ReadFileHeaderDataBySize(
5095 catfp
, formatspecs
['format_delimiter'])
5097 preheaderdata
= ReadFileHeaderDataWoSize(
5098 catfp
, formatspecs
['format_delimiter'])
5099 if(len(preheaderdata
) == 0):
5101 prefheadsize
= int(preheaderdata
[0], 16)
5102 prefnumfields
= int(preheaderdata
[1], 16)
5103 preftype
= int(preheaderdata
[2], 16)
5104 if(re
.findall("^[.|/]", preheaderdata
[3])):
5105 prefname
= preheaderdata
[3]
5107 prefname
= "./"+preheaderdata
[3]
5108 prefbasedir
= os
.path
.dirname(prefname
)
5109 preflinkname
= preheaderdata
[4]
5110 prefsize
= int(preheaderdata
[5], 16)
5111 prefatime
= int(preheaderdata
[6], 16)
5112 prefmtime
= int(preheaderdata
[7], 16)
5113 prefctime
= int(preheaderdata
[8], 16)
5114 prefbtime
= int(preheaderdata
[9], 16)
5115 prefmode
= int(preheaderdata
[10], 16)
5116 prefchmode
= stat
.S_IMODE(prefmode
)
5117 preftypemod
= stat
.S_IFMT(prefmode
)
5118 prefwinattributes
= int(preheaderdata
[11], 16)
5119 prefcompression
= preheaderdata
[12]
5120 prefcsize
= int(preheaderdata
[13], 16)
5121 prefuid
= int(preheaderdata
[14], 16)
5122 prefuname
= preheaderdata
[15]
5123 prefgid
= int(preheaderdata
[16], 16)
5124 prefgname
= preheaderdata
[17]
5125 fid
= int(preheaderdata
[18], 16)
5126 finode
= int(preheaderdata
[19], 16)
5127 flinkcount
= int(preheaderdata
[20], 16)
5128 prefdev_minor
= int(preheaderdata
[21], 16)
5129 prefdev_major
= int(preheaderdata
[22], 16)
5130 prefrdev_minor
= int(preheaderdata
[23], 16)
5131 prefrdev_major
= int(preheaderdata
[24], 16)
5132 prefseeknextfile
= preheaderdata
[25]
5133 prefextrasize
= int(preheaderdata
[26], 16)
5134 prefextrafields
= int(preheaderdata
[27], 16)
5135 extrafieldslist
= []
5137 extraend
= extrastart
+ prefextrafields
5138 extrafieldslist
= []
5139 if(extrastart
< extraend
):
5140 extrafieldslist
.append(preheaderdata
[extrastart
])
5141 extrastart
= extrastart
+ 1
5142 prefcs
= preheaderdata
[-2].lower()
5143 prenewfcs
= preheaderdata
[-1].lower()
5144 prenewfcs
= GetHeaderChecksum(
5145 preheaderdata
[:-2], preheaderdata
[-4].lower(), True, formatspecs
)
5146 if(prefcs
!= prenewfcs
and not skipchecksum
):
5147 VerbosePrintOut("File Header Checksum Error with file " +
5148 prefname
+ " at offset " + str(prefhstart
))
5149 VerbosePrintOut("'" + str(prefcs
) + "' != " +
5150 "'" + str(prenewfcs
) + "'")
5152 valid_archive
= False
5153 invalid_archive
= True
5154 prefhend
= catfp
.tell() - 1
5155 prefcontentstart
= catfp
.tell()
5157 pyhascontents
= False
5159 if(prefcompression
):
5160 prefcontents
= catfp
.read(prefsize
)
5162 prefcontents
= catfp
.read(prefcsize
)
5163 prenewfccs
= GetFileChecksum(
5164 prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
)
5165 pyhascontents
= True
5166 if(prefccs
!= prenewfccs
and not skipchecksum
):
5167 VerbosePrintOut("File Content Checksum Error with file " +
5168 prefname
+ " at offset " + str(prefcontentstart
))
5169 VerbosePrintOut("'" + str(prefccs
) +
5170 "' != " + "'" + str(prenewfccs
) + "'")
5172 if(re
.findall("^\\+([0-9]+)", prefseeknextfile
)):
5173 fseeknextasnum
= int(prefseeknextfile
.replace("+", ""))
5174 if(abs(fseeknextasnum
) == 0):
5176 catfp
.seek(fseeknextasnum
, 1)
5177 elif(re
.findall("^\\-([0-9]+)", prefseeknextfile
)):
5178 fseeknextasnum
= int(prefseeknextfile
)
5179 if(abs(fseeknextasnum
) == 0):
5181 catfp
.seek(fseeknextasnum
, 1)
5182 elif(re
.findall("^([0-9]+)", prefseeknextfile
)):
5183 fseeknextasnum
= int(prefseeknextfile
)
5184 if(abs(fseeknextasnum
) == 0):
5186 catfp
.seek(fseeknextasnum
, 0)
5190 catfp
.seek(seekstart
, 0)
5192 catfheadsize
= int(preheaderdata
[0], 16)
5193 catfnumfields
= int(preheaderdata
[1], 16)
5194 catftype
= int(preheaderdata
[2], 16)
5195 if(re
.findall("^[.|/]", preheaderdata
[3])):
5196 catfname
= preheaderdata
[3]
5198 catfname
= "./"+preheaderdata
[3]
5199 catflinkname
= preheaderdata
[4]
5200 catfsize
= int(preheaderdata
[5], 16)
5201 catfbasedir
= os
.path
.dirname(catfname
)
5202 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
,
5203 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
}
5205 catlist
.update({'catfp': catfp
})
5211 create_alias_function("", __file_format_name__
,
5212 "SeekToFileNum", ArchiveFileSeekToFileNum
)
5215 def ArchiveFileSeekToFileName(infile
, seekfile
=None, listonly
=False, contentasfile
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
5216 formatspecs
= FormatSpecsListToDict(formatspecs
)
5217 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
5220 catfp
= UncompressArchiveFile(catfp
, formatspecs
)
5221 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True)
5222 if(checkcompressfile
== "tarfile" and TarFileCheck(infile
)):
5223 return TarFileToArray(infile
, 0, 0, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
5224 if(checkcompressfile
== "zipfile" and zipfile
.is_zipfile(infile
)):
5225 return ZipFileToArray(infile
, 0, 0, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
5226 if(rarfile_support
and checkcompressfile
== "rarfile" and (rarfile
.is_rarfile(infile
) or rarfile
.is_rarfile_sfx(infile
))):
5227 return RarFileToArray(infile
, 0, 0, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
5228 if(py7zr_support
and checkcompressfile
== "7zipfile" and py7zr
.is_7zfile(infile
)):
5229 return SevenZipFileToArray(infile
, 0, 0, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
5230 if(checkcompressfile
!= "catfile" and checkcompressfile
!= formatspecs
['format_lower']):
5235 elif(infile
== "-"):
5237 if(hasattr(sys
.stdin
, "buffer")):
5238 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
)
5240 shutil
.copyfileobj(sys
.stdin
, catfp
)
5242 catfp
= UncompressArchiveFile(catfp
, formatspecs
)
5246 elif(re
.findall("^(http|https|ftp|ftps|sftp):\\/\\/", str(infile
))):
5247 catfp
= download_file_from_internet_file(infile
)
5248 catfp
= UncompressArchiveFile(catfp
, formatspecs
)
5254 infile
= RemoveWindowsPath(infile
)
5255 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True)
5256 if(checkcompressfile
== "tarfile" and TarFileCheck(infile
)):
5257 return TarFileToArray(infile
, 0, 0, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
5258 if(checkcompressfile
== "zipfile" and zipfile
.is_zipfile(infile
)):
5259 return ZipFileToArray(infile
, 0, 0, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
5260 if(rarfile_support
and checkcompressfile
== "rarfile" and (rarfile
.is_rarfile(infile
) or rarfile
.is_rarfile_sfx(infile
))):
5261 return RarFileToArray(infile
, 0, 0, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
5262 if(py7zr_support
and checkcompressfile
== "7zipfile" and py7zr
.is_7zfile(infile
)):
5263 return SevenZipFileToArray(infile
, 0, 0, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
5264 if(checkcompressfile
!= "catfile" and checkcompressfile
!= formatspecs
['format_lower']):
5266 compresscheck
= CheckCompressionType(infile
, formatspecs
, True)
5267 if(not compresscheck
):
5268 fextname
= os
.path
.splitext(infile
)[1]
5269 if(fextname
== ".gz"):
5270 compresscheck
= "gzip"
5271 elif(fextname
== ".bz2"):
5272 compresscheck
= "bzip2"
5273 elif(fextname
== ".zst"):
5274 compresscheck
= "zstd"
5275 elif(fextname
== ".lz4" or fextname
== ".clz4"):
5276 compresscheck
= "lz4"
5277 elif(fextname
== ".lzo" or fextname
== ".lzop"):
5278 compresscheck
= "lzo"
5279 elif(fextname
== ".lzma"):
5280 compresscheck
= "lzma"
5281 elif(fextname
== ".xz"):
5282 compresscheck
= "xz"
5283 elif(fextname
== ".zz" or fextname
== ".zl" or fextname
== ".zlib"):
5284 compresscheck
= "zlib"
5287 if(not compresscheck
):
5289 catfp
= UncompressFile(infile
, formatspecs
, "rb")
5294 SeekToEndOfFile(catfp);
5296 SeekToEndOfFile(catfp);
5297 CatSize = catfp.tell();
5298 CatSizeEnd = CatSize;
5306 curloc
= catfp
.tell()
5309 catheaderver
= str(int(formatspecs
['format_ver'].replace(".", "")))
5310 catstring
= catfp
.read(len(formatspecs
['format_magic']+catheaderver
)).decode("UTF-8")
5311 catdel
= catfp
.read(1).decode("UTF-8")
5312 if(catstring
!= formatspecs
['format_magic']+catheaderver
):
5314 if(catdel
!= formatspecs
['format_delimiter']):
5316 catheader
= ReadFileHeaderData(catfp
, 3, formatspecs
['format_delimiter'])
5318 catfp
.seek(curloc
, 0)
5319 catversion
= re
.findall("([\\d]+)", catstring
)
5320 fprenumfiles
= catheader
[0]
5321 fnumfiles
= int(fprenumfiles
, 16)
5322 fprechecksumtype
= catheader
[1]
5323 fprechecksum
= catheader
[2]
5324 fileheader
= AppendNullByte(catstring
, formatspecs
['format_delimiter'])
5325 fnumfileshex
= format(int(fnumfiles
), 'x').lower()
5326 fileheader
= fileheader
+ \
5327 AppendNullBytes([fnumfileshex
, fprechecksumtype
],
5328 formatspecs
['format_delimiter'])
5329 catfileheadercshex
= GetFileChecksum(
5330 fileheader
, fprechecksumtype
, True, formatspecs
)
5331 fileheader
= fileheader
+ \
5332 AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter'])
5333 fheadtell
= len(fileheader
)
5334 if(fprechecksum
!= catfileheadercshex
and not skipchecksum
):
5335 VerbosePrintOut("File Header Checksum Error with file " +
5336 infile
+ " at offset " + str(0))
5337 VerbosePrintOut("'" + str(fprechecksum
) + "' != " +
5338 "'" + str(catfileheadercshex
) + "'")
5340 catversions
= re
.search('(.*?)(\\d+)', catstring
).groups()
5341 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1],
5342 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}}
5343 seekto
= fnumfiles
- 1
5348 prefhstart
= catfp
.tell()
5349 if(formatspecs
['new_style']):
5350 preheaderdata
= ReadFileHeaderDataBySize(
5351 catfp
, formatspecs
['format_delimiter'])
5353 preheaderdata
= ReadFileHeaderDataWoSize(
5354 catfp
, formatspecs
['format_delimiter'])
5355 if(len(preheaderdata
) == 0):
5357 prefheadsize
= int(preheaderdata
[0], 16)
5358 prefnumfields
= int(preheaderdata
[1], 16)
5359 preftype
= int(preheaderdata
[2], 16)
5360 if(re
.findall("^[.|/]", preheaderdata
[3])):
5361 prefname
= preheaderdata
[3]
5363 prefname
= "./"+preheaderdata
[3]
5364 prefbasedir
= os
.path
.dirname(prefname
)
5365 preflinkname
= preheaderdata
[4]
5366 prefsize
= int(preheaderdata
[5], 16)
5367 prefatime
= int(preheaderdata
[6], 16)
5368 prefmtime
= int(preheaderdata
[7], 16)
5369 prefctime
= int(preheaderdata
[8], 16)
5370 prefbtime
= int(preheaderdata
[9], 16)
5371 prefmode
= int(preheaderdata
[10], 16)
5372 prefchmode
= stat
.S_IMODE(prefmode
)
5373 preftypemod
= stat
.S_IFMT(prefmode
)
5374 prefwinattributes
= int(preheaderdata
[11], 16)
5375 prefcompression
= preheaderdata
[12]
5376 prefcsize
= int(preheaderdata
[13], 16)
5377 prefuid
= int(preheaderdata
[14], 16)
5378 prefuname
= preheaderdata
[15]
5379 prefgid
= int(preheaderdata
[16], 16)
5380 prefgname
= preheaderdata
[17]
5381 fid
= int(preheaderdata
[18], 16)
5382 finode
= int(preheaderdata
[19], 16)
5383 flinkcount
= int(preheaderdata
[20], 16)
5384 prefdev_minor
= int(preheaderdata
[21], 16)
5385 prefdev_major
= int(preheaderdata
[22], 16)
5386 prefrdev_minor
= int(preheaderdata
[23], 16)
5387 prefrdev_major
= int(preheaderdata
[24], 16)
5388 prefseeknextfile
= preheaderdata
[25]
5389 prefextrasize
= int(preheaderdata
[26], 16)
5390 prefextrafields
= int(preheaderdata
[27], 16)
5391 extrafieldslist
= []
5393 extraend
= extrastart
+ prefextrafields
5394 extrafieldslist
= []
5395 if(extrastart
< extraend
):
5396 extrafieldslist
.append(preheaderdata
[extrastart
])
5397 extrastart
= extrastart
+ 1
5398 prefcs
= preheaderdata
[-2].lower()
5399 prenewfcs
= preheaderdata
[-1].lower()
5400 prenewfcs
= GetHeaderChecksum(
5401 preheaderdata
[:-2], preheaderdata
[-4].lower(), True, formatspecs
)
5402 if(prefcs
!= prenewfcs
and not skipchecksum
):
5403 VerbosePrintOut("File Header Checksum Error with file " +
5404 prefname
+ " at offset " + str(prefhstart
))
5405 VerbosePrintOut("'" + str(prefcs
) + "' != " +
5406 "'" + str(prenewfcs
) + "'")
5408 valid_archive
= False
5409 invalid_archive
= True
5410 prefhend
= catfp
.tell() - 1
5411 prefcontentstart
= catfp
.tell()
5413 pyhascontents
= False
5415 if(prefcompression
):
5416 prefcontents
= catfp
.read(prefsize
)
5418 prefcontents
= catfp
.read(prefcsize
)
5419 prenewfccs
= GetFileChecksum(
5420 prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
)
5421 pyhascontents
= True
5422 if(prefccs
!= prenewfccs
and not skipchecksum
):
5423 VerbosePrintOut("File Content Checksum Error with file " +
5424 prefname
+ " at offset " + str(prefcontentstart
))
5425 VerbosePrintOut("'" + str(prefccs
) +
5426 "' != " + "'" + str(prenewfccs
) + "'")
5428 if(re
.findall("^\\+([0-9]+)", prefseeknextfile
)):
5429 fseeknextasnum
= int(prefseeknextfile
.replace("+", ""))
5430 if(abs(fseeknextasnum
) == 0):
5432 catfp
.seek(fseeknextasnum
, 1)
5433 elif(re
.findall("^\\-([0-9]+)", prefseeknextfile
)):
5434 fseeknextasnum
= int(prefseeknextfile
)
5435 if(abs(fseeknextasnum
) == 0):
5437 catfp
.seek(fseeknextasnum
, 1)
5438 elif(re
.findall("^([0-9]+)", prefseeknextfile
)):
5439 fseeknextasnum
= int(prefseeknextfile
)
5440 if(abs(fseeknextasnum
) == 0):
5442 catfp
.seek(fseeknextasnum
, 0)
5447 prefname
= preheaderdata
[2]
5448 if(re
.findall("^[.|/]", preheaderdata
[2])):
5449 prefname
= preheaderdata
[2]
5451 prefname
= "./"+preheaderdata
[2]
5452 if(prefname
== seekfile
):
5455 catfp
.seek(seekstart
, 0)
5457 catfheadsize
= int(preheaderdata
[0], 16)
5458 catfnumfields
= int(preheaderdata
[1], 16)
5459 catftype
= int(preheaderdata
[2], 16)
5460 if(re
.findall("^[.|/]", preheaderdata
[3])):
5461 catfname
= preheaderdata
[3]
5463 catfname
= "./"+preheaderdata
[3]
5464 catflinkname
= preheaderdata
[4]
5465 catfsize
= int(preheaderdata
[5], 16)
5466 catfbasedir
= os
.path
.dirname(catfname
)
5468 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
,
5469 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
}
5473 catlist
.update({'catfp': catfp
})
5479 create_alias_function("", __file_format_name__
,
5480 "SeekToFileName", ArchiveFileSeekToFileName
)
5483 def ArchiveFileValidate(infile
, formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
5484 formatspecs
= FormatSpecsListToDict(formatspecs
)
5486 logging
.basicConfig(format
="%(message)s",
5487 stream
=sys
.stdout
, level
=logging
.DEBUG
)
5488 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
5491 catfp
= UncompressArchiveFile(catfp
, formatspecs
)
5492 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True)
5493 if(checkcompressfile
== "tarfile" and TarFileCheck(infile
)):
5494 return TarFileToArray(infile
, 0, 0, False, True, False, formatspecs
, returnfp
)
5495 if(checkcompressfile
== "zipfile" and zipfile
.is_zipfile(infile
)):
5496 return ZipFileToArray(infile
, 0, 0, False, True, False, formatspecs
, returnfp
)
5497 if(rarfile_support
and checkcompressfile
== "rarfile" and (rarfile
.is_rarfile(infile
) or rarfile
.is_rarfile_sfx(infile
))):
5498 return RarFileToArray(infile
, 0, 0, False, True, False, formatspecs
, returnfp
)
5499 if(py7zr_support
and checkcompressfile
== "7zipfile" and py7zr
.is_7zfile(infile
)):
5500 return SevenZipFileToArray(infile
, 0, 0, False, True, False, formatspecs
, returnfp
)
5501 if(checkcompressfile
!= "catfile" and checkcompressfile
!= formatspecs
['format_lower']):
5506 elif(infile
== "-"):
5508 if(hasattr(sys
.stdin
, "buffer")):
5509 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
)
5511 shutil
.copyfileobj(sys
.stdin
, catfp
)
5513 catfp
= UncompressArchiveFile(catfp
, formatspecs
)
5517 elif(re
.findall("^(http|https|ftp|ftps|sftp):\\/\\/", str(infile
))):
5518 catfp
= download_file_from_internet_file(infile
)
5519 catfp
= UncompressArchiveFile(catfp
, formatspecs
)
5525 infile
= RemoveWindowsPath(infile
)
5526 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True)
5527 if(checkcompressfile
== "tarfile" and TarFileCheck(infile
)):
5528 return TarFileToArray(infile
, 0, 0, False, True, False, formatspecs
, returnfp
)
5529 if(checkcompressfile
== "zipfile" and zipfile
.is_zipfile(infile
)):
5530 return ZipFileToArray(infile
, 0, 0, False, True, False, formatspecs
, returnfp
)
5531 if(rarfile_support
and checkcompressfile
== "rarfile" and (rarfile
.is_rarfile(infile
) or rarfile
.is_rarfile_sfx(infile
))):
5532 return RarFileToArray(infile
, 0, 0, False, True, False, formatspecs
, returnfp
)
5533 if(py7zr_support
and checkcompressfile
== "7zipfile" and py7zr
.is_7zfile(infile
)):
5534 return SevenZipFileToArray(infile
, 0, 0, False, True, False, formatspecs
, returnfp
)
5535 if(checkcompressfile
!= "catfile" and checkcompressfile
!= formatspecs
['format_lower']):
5537 compresscheck
= CheckCompressionType(infile
, formatspecs
, True)
5538 if(not compresscheck
):
5539 fextname
= os
.path
.splitext(infile
)[1]
5540 if(fextname
== ".gz"):
5541 compresscheck
= "gzip"
5542 elif(fextname
== ".bz2"):
5543 compresscheck
= "bzip2"
5544 elif(fextname
== ".zst"):
5545 compresscheck
= "zstd"
5546 elif(fextname
== ".lz4" or fextname
== ".clz4"):
5547 compresscheck
= "lz4"
5548 elif(fextname
== ".lzo" or fextname
== ".lzop"):
5549 compresscheck
= "lzo"
5550 elif(fextname
== ".lzma"):
5551 compresscheck
= "lzma"
5552 elif(fextname
== ".xz"):
5553 compresscheck
= "xz"
5554 elif(fextname
== ".zz" or fextname
== ".zl" or fextname
== ".zlib"):
5555 compresscheck
= "zlib"
5558 if(not compresscheck
):
5560 catfp
= UncompressFile(infile
, formatspecs
, "rb")
5565 SeekToEndOfFile(catfp);
5567 SeekToEndOfFile(catfp);
5568 CatSize = catfp.tell();
5569 CatSizeEnd = CatSize;
5577 curloc
= catfp
.tell()
5580 catheaderver
= str(int(formatspecs
['format_ver'].replace(".", "")))
5581 catstring
= catfp
.read(len(formatspecs
['format_magic']+catheaderver
)).decode("UTF-8")
5582 catdel
= catfp
.read(1).decode("UTF-8")
5583 if(catstring
!= formatspecs
['format_magic']+catheaderver
):
5585 if(catdel
!= formatspecs
['format_delimiter']):
5587 catheader
= ReadFileHeaderData(catfp
, 3, formatspecs
['format_delimiter'])
5589 catfp
.seek(curloc
, 0)
5590 catversion
= re
.findall("([\\d]+)", catstring
)
5591 fprenumfiles
= catheader
[0]
5592 fnumfiles
= int(fprenumfiles
, 16)
5593 fprechecksumtype
= catheader
[1]
5594 fprechecksum
= catheader
[2]
5596 fileheader
= AppendNullByte(catstring
, formatspecs
['format_delimiter'])
5597 fnumfileshex
= format(int(fnumfiles
), 'x').lower()
5598 fileheader
= fileheader
+ \
5599 AppendNullBytes([fnumfileshex
, fprechecksumtype
],
5600 formatspecs
['format_delimiter'])
5601 catfileheadercshex
= GetFileChecksum(
5602 fileheader
, fprechecksumtype
, True, formatspecs
)
5603 fileheader
= fileheader
+ \
5604 AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter'])
5605 valid_archive
= True
5606 invalid_archive
= False
5608 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
5610 VerbosePrintOut(infile
.name
)
5611 except AttributeError:
5612 VerbosePrintOut(infile
)
5614 VerbosePrintOut(infile
)
5615 VerbosePrintOut("Number of Records " + str(fnumfiles
))
5616 if(fprechecksum
== catfileheadercshex
):
5618 VerbosePrintOut("File Header Checksum Passed at offset " + str(0))
5619 VerbosePrintOut("'" + str(fprechecksum
) + "' == " +
5620 "'" + str(catfileheadercshex
) + "'")
5623 VerbosePrintOut("File Header Checksum Failed at offset " + str(0))
5624 VerbosePrintOut("'" + str(fprechecksum
) + "' != " +
5625 "'" + str(catfileheadercshex
) + "'")
5626 valid_archive
= False
5627 invalid_archive
= True
5630 while(il
< fnumfiles
):
5631 catfhstart
= catfp
.tell()
5632 if(formatspecs
['new_style']):
5633 catheaderdata
= ReadFileHeaderDataBySize(
5634 catfp
, formatspecs
['format_delimiter'])
5636 catheaderdata
= ReadFileHeaderDataWoSize(
5637 catfp
, formatspecs
['format_delimiter'])
5638 if(len(catheaderdata
) == 0):
5640 catfheadsize
= int(catheaderdata
[0], 16)
5641 catfnumfields
= int(catheaderdata
[1], 16)
5642 catftype
= int(catheaderdata
[2], 16)
5643 if(re
.findall("^[.|/]", catheaderdata
[3])):
5644 catfname
= catheaderdata
[3]
5646 catfname
= "./"+catheaderdata
[3]
5647 catfbasedir
= os
.path
.dirname(catfname
)
5648 catflinkname
= catheaderdata
[4]
5649 catfsize
= int(catheaderdata
[5], 16)
5650 catfatime
= int(catheaderdata
[6], 16)
5651 catfmtime
= int(catheaderdata
[7], 16)
5652 catfctime
= int(catheaderdata
[8], 16)
5653 catfbtime
= int(catheaderdata
[9], 16)
5654 catfmode
= int(catheaderdata
[10], 16)
5655 catfchmode
= stat
.S_IMODE(catfmode
)
5656 catftypemod
= stat
.S_IFMT(catfmode
)
5657 catfwinattributes
= int(catheaderdata
[11], 16)
5658 catfcompression
= catheaderdata
[12]
5659 catfcsize
= int(catheaderdata
[13], 16)
5660 catfuid
= int(catheaderdata
[14], 16)
5661 catfuname
= catheaderdata
[15]
5662 catfgid
= int(catheaderdata
[16], 16)
5663 catfgname
= catheaderdata
[17]
5664 fid
= int(catheaderdata
[18], 16)
5665 finode
= int(catheaderdata
[19], 16)
5666 flinkcount
= int(catheaderdata
[20], 16)
5667 catfdev_minor
= int(catheaderdata
[21], 16)
5668 catfdev_major
= int(catheaderdata
[22], 16)
5669 catfrdev_minor
= int(catheaderdata
[23], 16)
5670 catfrdev_major
= int(catheaderdata
[24], 16)
5671 catfseeknextfile
= catheaderdata
[25]
5672 catfextrasize
= int(catheaderdata
[26], 16)
5673 catfextrafields
= int(catheaderdata
[27], 16)
5674 extrafieldslist
= []
5676 extraend
= extrastart
+ catfextrafields
5677 extrafieldslist
= []
5678 if(extrastart
< extraend
):
5679 extrafieldslist
.append(catheaderdata
[extrastart
])
5680 extrastart
= extrastart
+ 1
5681 catfcs
= catheaderdata
[-2].lower()
5682 catfccs
= catheaderdata
[-1].lower()
5683 catnewfcs
= GetHeaderChecksum(
5684 catheaderdata
[:-2], catheaderdata
[-4].lower(), True, formatspecs
)
5686 VerbosePrintOut(catfname
)
5687 VerbosePrintOut("Record Number " + str(il
) + "; File ID " +
5688 str(fid
) + "; iNode Number " + str(finode
))
5689 if(catfcs
== catnewfcs
):
5692 "File Header Checksum Passed at offset " + str(catfhstart
))
5693 VerbosePrintOut("'" + str(catfcs
) + "' == " +
5694 "'" + str(catnewfcs
) + "'")
5698 "File Header Checksum Failed at offset " + str(catfhstart
))
5699 VerbosePrintOut("'" + str(catfcs
) + "' != " +
5700 "'" + str(catnewfcs
) + "'")
5701 valid_archive
= False
5702 invalid_archive
= True
5703 catfhend
= catfp
.tell() - 1
5704 catfcontentstart
= catfp
.tell()
5706 pyhascontents
= False
5708 if(catfcompression
== "none" or catfcompression
== "" or catfcompression
== "auto"):
5709 catfcontents
= catfp
.read(catfsize
)
5711 catfcontents
= catfp
.read(catfcsize
)
5712 catnewfccs
= GetFileChecksum(
5713 catfcontents
, catheaderdata
[-3].lower(), False, formatspecs
)
5714 pyhascontents
= True
5715 if(catfccs
== catnewfccs
):
5718 "File Content Checksum Passed at offset " + str(catfcontentstart
))
5719 VerbosePrintOut("'" + str(catfccs
) +
5720 "' == " + "'" + str(catnewfccs
) + "'")
5724 "File Content Checksum Failed at offset " + str(catfcontentstart
))
5725 VerbosePrintOut("'" + str(catfccs
) +
5726 "' != " + "'" + str(catnewfccs
) + "'")
5727 valid_archive
= False
5728 invalid_archive
= True
5731 if(re
.findall("^\\+([0-9]+)", catfseeknextfile
)):
5732 fseeknextasnum
= int(catfseeknextfile
.replace("+", ""))
5733 if(abs(fseeknextasnum
) == 0):
5735 catfp
.seek(fseeknextasnum
, 1)
5736 elif(re
.findall("^\\-([0-9]+)", catfseeknextfile
)):
5737 fseeknextasnum
= int(catfseeknextfile
)
5738 if(abs(fseeknextasnum
) == 0):
5740 catfp
.seek(fseeknextasnum
, 1)
5741 elif(re
.findall("^([0-9]+)", catfseeknextfile
)):
5742 fseeknextasnum
= int(catfseeknextfile
)
5743 if(abs(fseeknextasnum
) == 0):
5745 catfp
.seek(fseeknextasnum
, 0)
5760 create_alias_function("", __file_format_name__
,
5761 "Validate", ArchiveFileValidate
)
5764 def ArchiveFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, contentasfile
=True, uncompress
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
5765 formatspecs
= FormatSpecsListToDict(formatspecs
)
5766 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
5769 catfp
= UncompressArchiveFile(catfp
, formatspecs
)
5770 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True)
5771 if(checkcompressfile
== "tarfile" and TarFileCheck(infile
)):
5772 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
5773 if(checkcompressfile
== "zipfile" and zipfile
.is_zipfile(infile
)):
5774 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
5775 if(rarfile_support
and checkcompressfile
== "rarfile" and (rarfile
.is_rarfile(infile
) or rarfile
.is_rarfile_sfx(infile
))):
5776 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
5777 if(py7zr_support
and checkcompressfile
== "7zipfile" and py7zr
.is_7zfile(infile
)):
5778 return SevenZipFileToArray(infile
, seekstart
, seekend
, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
5779 if(checkcompressfile
!= "catfile" and checkcompressfile
!= formatspecs
['format_lower']):
5784 elif(infile
== "-"):
5786 if(hasattr(sys
.stdin
, "buffer")):
5787 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
)
5789 shutil
.copyfileobj(sys
.stdin
, catfp
)
5791 catfp
= UncompressArchiveFile(catfp
, formatspecs
)
5795 elif(re
.findall("^(http|https|ftp|ftps|sftp):\\/\\/", str(infile
))):
5796 catfp
= download_file_from_internet_file(infile
)
5797 catfp
= UncompressArchiveFile(catfp
, formatspecs
)
5803 infile
= RemoveWindowsPath(infile
)
5804 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True)
5805 if(checkcompressfile
== "tarfile" and TarFileCheck(infile
)):
5806 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
)
5807 if(checkcompressfile
== "zipfile" and zipfile
.is_zipfile(infile
)):
5808 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
)
5809 if(rarfile_support
and checkcompressfile
== "rarfile" and (rarfile
.is_rarfile(infile
) or rarfile
.is_rarfile_sfx(infile
))):
5810 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
)
5811 if(py7zr_support
and checkcompressfile
== "7zipfile" and py7zr
.is_7zfile(infile
)):
5812 return SevenZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
)
5813 if(checkcompressfile
!= "catfile" and checkcompressfile
!= formatspecs
['format_lower']):
5815 compresscheck
= CheckCompressionType(infile
, formatspecs
, True)
5816 if(not compresscheck
):
5817 fextname
= os
.path
.splitext(infile
)[1]
5818 if(fextname
== ".gz"):
5819 compresscheck
= "gzip"
5820 elif(fextname
== ".bz2"):
5821 compresscheck
= "bzip2"
5822 elif(fextname
== ".zst"):
5823 compresscheck
= "zstd"
5824 elif(fextname
== ".lz4" or fextname
== ".clz4"):
5825 compresscheck
= "lz4"
5826 elif(fextname
== ".lzo" or fextname
== ".lzop"):
5827 compresscheck
= "lzo"
5828 elif(fextname
== ".lzma"):
5829 compresscheck
= "lzma"
5830 elif(fextname
== ".xz"):
5831 compresscheck
= "xz"
5832 elif(fextname
== ".zz" or fextname
== ".zl" or fextname
== ".zlib"):
5833 compresscheck
= "zlib"
5836 if(not compresscheck
):
5838 catfp
= UncompressFile(infile
, formatspecs
, "rb")
5843 SeekToEndOfFile(catfp);
5845 SeekToEndOfFile(catfp);
5846 CatSize = catfp.tell();
5847 CatSizeEnd = CatSize;
5855 curloc
= catfp
.tell()
5858 catheaderver
= str(int(formatspecs
['format_ver'].replace(".", "")))
5859 catstring
= catfp
.read(len(formatspecs
['format_magic']+catheaderver
)).decode("UTF-8")
5860 catdel
= catfp
.read(1).decode("UTF-8")
5861 if(catstring
!= formatspecs
['format_magic']+catheaderver
):
5863 if(catdel
!= formatspecs
['format_delimiter']):
5865 catheader
= ReadFileHeaderData(catfp
, 3, formatspecs
['format_delimiter'])
5867 catfp
.seek(curloc
, 0)
5868 catversion
= re
.findall("([\\d]+)", catstring
)
5869 fprenumfiles
= catheader
[0]
5870 fnumfiles
= int(fprenumfiles
, 16)
5871 fprechecksumtype
= catheader
[1]
5872 fprechecksum
= catheader
[2]
5873 fileheader
= AppendNullByte(catstring
, formatspecs
['format_delimiter'])
5874 fnumfileshex
= format(int(fnumfiles
), 'x').lower()
5875 fileheader
= fileheader
+ \
5876 AppendNullBytes([fnumfileshex
, fprechecksumtype
],
5877 formatspecs
['format_delimiter'])
5878 catfileheadercshex
= GetFileChecksum(
5879 fileheader
, fprechecksumtype
, True, formatspecs
)
5880 fileheader
= fileheader
+ \
5881 AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter'])
5882 fheadtell
= len(fileheader
)
5883 if(fprechecksum
!= catfileheadercshex
and not skipchecksum
):
5885 "File Header Checksum Error with file at offset " + str(0))
5886 VerbosePrintOut("'" + str(fprechecksum
) + "' != " +
5887 "'" + str(catfileheadercshex
) + "'")
5889 catversions
= re
.search('(.*?)(\\d+)', catstring
).groups()
5890 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1],
5891 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': []}
5892 if(seekstart
< 0 and seekstart
> fnumfiles
):
5894 if(seekend
== 0 or seekend
> fnumfiles
and seekend
< seekstart
):
5896 elif(seekend
< 0 and abs(seekend
) <= fnumfiles
and abs(seekend
) >= seekstart
):
5897 seekend
= fnumfiles
- abs(seekend
)
5900 while(il
< seekstart
):
5901 prefhstart
= catfp
.tell()
5902 if(formatspecs
['new_style']):
5903 preheaderdata
= ReadFileHeaderDataBySize(
5904 catfp
, formatspecs
['format_delimiter'])
5906 preheaderdata
= ReadFileHeaderDataWoSize(
5907 catfp
, formatspecs
['format_delimiter'])
5908 if(len(preheaderdata
) == 0):
5910 prefheadsize
= int(preheaderdata
[0], 16)
5911 prefnumfields
= int(preheaderdata
[1], 16)
5912 if(re
.findall("^[.|/]", preheaderdata
[3])):
5913 prefname
= preheaderdata
[3]
5915 prefname
= "./"+preheaderdata
[3]
5916 prefsize
= int(preheaderdata
[5], 16)
5917 prefcompression
= preheaderdata
[12]
5918 prefcsize
= int(preheaderdata
[13], 16)
5919 prefseeknextfile
= preheaderdata
[25]
5920 prefextrasize
= int(preheaderdata
[26], 16)
5921 prefextrafields
= int(preheaderdata
[27], 16)
5922 extrafieldslist
= []
5924 extraend
= extrastart
+ prefextrafields
5925 extrafieldslist
= []
5926 if(extrastart
< extraend
):
5927 extrafieldslist
.append(preheaderdata
[extrastart
])
5928 extrastart
= extrastart
+ 1
5929 prefcs
= preheaderdata
[-2].lower()
5930 prenewfcs
= preheaderdata
[-1].lower()
5931 prenewfcs
= GetHeaderChecksum(
5932 preheaderdata
[:-2], preheaderdata
[-4].lower(), True, formatspecs
)
5933 if(prefcs
!= prenewfcs
and not skipchecksum
):
5934 VerbosePrintOut("File Header Checksum Error with file " +
5935 prefname
+ " at offset " + str(prefhstart
))
5936 VerbosePrintOut("'" + str(prefcs
) + "' != " +
5937 "'" + str(prenewfcs
) + "'")
5939 valid_archive
= False
5940 invalid_archive
= True
5941 prefhend
= catfp
.tell() - 1
5942 prefcontentstart
= catfp
.tell()
5944 pyhascontents
= False
5946 if(prefcompression
== "none" or prefcompression
== "" or prefcompression
== "auto"):
5947 prefcontents
= catfp
.read(prefsize
)
5949 prefcontents
= catfp
.read(prefcsize
)
5950 prenewfccs
= GetFileChecksum(
5951 prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
)
5952 pyhascontents
= True
5953 if(prefccs
!= prenewfccs
and not skipchecksum
):
5954 VerbosePrintOut("File Content Checksum Error with file " +
5955 prefname
+ " at offset " + str(prefcontentstart
))
5956 VerbosePrintOut("'" + str(prefccs
) +
5957 "' != " + "'" + str(prenewfccs
) + "'")
5959 if(re
.findall("^\\+([0-9]+)", prefseeknextfile
)):
5960 fseeknextasnum
= int(prefseeknextfile
.replace("+", ""))
5961 if(abs(fseeknextasnum
) == 0):
5963 catfp
.seek(fseeknextasnum
, 1)
5964 elif(re
.findall("^\\-([0-9]+)", prefseeknextfile
)):
5965 fseeknextasnum
= int(prefseeknextfile
)
5966 if(abs(fseeknextasnum
) == 0):
5968 catfp
.seek(fseeknextasnum
, 1)
5969 elif(re
.findall("^([0-9]+)", prefseeknextfile
)):
5970 fseeknextasnum
= int(prefseeknextfile
)
5971 if(abs(fseeknextasnum
) == 0):
5973 catfp
.seek(fseeknextasnum
, 0)
5977 fileidnum
= seekstart
5979 while(fileidnum
< seekend
):
5980 catfhstart
= catfp
.tell()
5981 if(formatspecs
['new_style']):
5982 catheaderdata
= ReadFileHeaderDataBySize(
5983 catfp
, formatspecs
['format_delimiter'])
5985 catheaderdata
= ReadFileHeaderDataWoSize(
5986 catfp
, formatspecs
['format_delimiter'])
5987 if(len(catheaderdata
) == 0):
5989 catfheadsize
= int(catheaderdata
[0], 16)
5990 catfnumfields
= int(catheaderdata
[1], 16)
5991 catftype
= int(catheaderdata
[2], 16)
5992 if(re
.findall("^[.|/]", catheaderdata
[3])):
5993 catfname
= catheaderdata
[3]
5995 catfname
= "./"+catheaderdata
[3]
5996 catfbasedir
= os
.path
.dirname(catfname
)
5997 catflinkname
= catheaderdata
[4]
5998 catfsize
= int(catheaderdata
[5], 16)
5999 catfatime
= int(catheaderdata
[6], 16)
6000 catfmtime
= int(catheaderdata
[7], 16)
6001 catfctime
= int(catheaderdata
[8], 16)
6002 catfbtime
= int(catheaderdata
[9], 16)
6003 catfmode
= int(catheaderdata
[10], 16)
6004 catfchmode
= stat
.S_IMODE(catfmode
)
6005 catftypemod
= stat
.S_IFMT(catfmode
)
6006 catfwinattributes
= int(catheaderdata
[11], 16)
6007 catfcompression
= catheaderdata
[12]
6008 catfcsize
= int(catheaderdata
[13], 16)
6009 catfuid
= int(catheaderdata
[14], 16)
6010 catfuname
= catheaderdata
[15]
6011 catfgid
= int(catheaderdata
[16], 16)
6012 catfgname
= catheaderdata
[17]
6013 catfid
= int(catheaderdata
[18], 16)
6014 catfinode
= int(catheaderdata
[19], 16)
6015 catflinkcount
= int(catheaderdata
[20], 16)
6016 catfdev_minor
= int(catheaderdata
[21], 16)
6017 catfdev_major
= int(catheaderdata
[22], 16)
6018 catfrdev_minor
= int(catheaderdata
[23], 16)
6019 catfrdev_major
= int(catheaderdata
[24], 16)
6020 catfseeknextfile
= catheaderdata
[25]
6021 catfextrasize
= int(catheaderdata
[26], 16)
6022 catfextrafields
= int(catheaderdata
[27], 16)
6023 extrafieldslist
= []
6025 extraend
= extrastart
+ catfextrafields
6026 extrafieldslist
= []
6027 if(extrastart
< extraend
):
6028 extrafieldslist
.append(catheaderdata
[extrastart
])
6029 extrastart
= extrastart
+ 1
6030 catfcs
= catheaderdata
[-2].lower()
6031 catfccs
= catheaderdata
[-1].lower()
6032 catnewfcs
= GetHeaderChecksum(
6033 catheaderdata
[:-2], catheaderdata
[-4].lower(), True, formatspecs
)
6034 if(catfcs
!= catnewfcs
and not skipchecksum
):
6035 VerbosePrintOut("File Header Checksum Error with file " +
6036 catfname
+ " at offset " + str(catfhstart
))
6037 VerbosePrintOut("'" + str(catfcs
) + "' != " +
6038 "'" + str(catnewfcs
) + "'")
6040 catfhend
= catfp
.tell() - 1
6041 catfcontentstart
= catfp
.tell()
6042 catfcontents
= BytesIO()
6043 pyhascontents
= False
6044 if(catfsize
> 0 and not listonly
):
6045 if(catfcompression
== "none" or catfcompression
== "" or catfcompression
== "auto"):
6046 catfcontents
.write(catfp
.read(catfsize
))
6048 catfcontents
.write(catfp
.read(catfcsize
))
6049 catfcontents
.seek(0, 0)
6050 catnewfccs
= GetFileChecksum(
6051 catfcontents
.read(), catheaderdata
[-3].lower(), False, formatspecs
)
6052 pyhascontents
= True
6053 if(catfccs
!= catnewfccs
and skipchecksum
):
6054 VerbosePrintOut("File Content Checksum Error with file " +
6055 catfname
+ " at offset " + str(catfcontentstart
))
6056 VerbosePrintOut("'" + str(catfccs
) + "' != " +
6057 "'" + str(catnewfccs
) + "'")
6059 if(catfcompression
== "none" or catfcompression
== "" or catfcompression
== "auto"):
6062 catfcontents
.seek(0, 0)
6064 catfcontents
= UncompressArchiveFile(
6065 catfcontents
, formatspecs
)
6066 catfcontents
.seek(0, 0)
6067 catfccs
= GetFileChecksum(
6068 catfcontents
.read(), catheaderdata
[-3].lower(), False, formatspecs
)
6069 if(catfsize
> 0 and listonly
):
6070 if(catfcompression
== "none" or catfcompression
== "" or catfcompression
== "auto"):
6071 catfp
.seek(catfsize
, 1)
6073 catfp
.seek(catfcsize
, 1)
6074 pyhascontents
= False
6075 catfcontentend
= catfp
.tell()
6076 if(re
.findall("^\\+([0-9]+)", catfseeknextfile
)):
6077 fseeknextasnum
= int(catfseeknextfile
.replace("+", ""))
6078 if(abs(fseeknextasnum
) == 0):
6080 catfp
.seek(fseeknextasnum
, 1)
6081 elif(re
.findall("^\\-([0-9]+)", catfseeknextfile
)):
6082 fseeknextasnum
= int(catfseeknextfile
)
6083 if(abs(fseeknextasnum
) == 0):
6085 catfp
.seek(fseeknextasnum
, 1)
6086 elif(re
.findall("^([0-9]+)", catfseeknextfile
)):
6087 fseeknextasnum
= int(catfseeknextfile
)
6088 if(abs(fseeknextasnum
) == 0):
6090 catfp
.seek(fseeknextasnum
, 0)
6093 catfcontents
.seek(0, 0)
6094 if(not contentasfile
):
6095 catfcontents
= catfcontents
.read()
6096 catlist
['ffilelist'].append({'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
, 'fcompression': catfcompression
, 'fcsize': catfcsize
, 'fuid': catfuid
, 'funame': catfuname
, 'fgid': catfgid
, 'fgname': catfgname
, 'finode': catfinode
, 'flinkcount': catflinkcount
,
6097 'fminor': catfdev_minor
, 'fmajor': catfdev_major
, 'frminor': catfrdev_minor
, 'frmajor': catfrdev_major
, 'fseeknextfile': catfseeknextfile
, 'fheaderchecksumtype': catheaderdata
[-4], 'fcontentchecksumtype': catheaderdata
[-3], 'fnumfields': catfnumfields
+ 2, 'frawheader': catheaderdata
, 'fextrafields': catfextrafields
, 'fextrafieldsize': catfextrasize
, 'fextralist': extrafieldslist
, 'fheaderchecksum': catfcs
, 'fcontentchecksum': catfccs
, 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontentasfile': contentasfile
, 'fcontents': catfcontents
})
6098 fileidnum
= fileidnum
+ 1
6099 realidnum
= realidnum
+ 1
6101 catlist
.update({'catfp': catfp
})
6107 create_alias_function("", __file_format_name__
, "ToArray", ArchiveFileToArray
)
6110 def ArchiveFileStringToArray(catstr
, seekstart
=0, seekend
=0, listonly
=False, contentasfile
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
6111 formatspecs
= FormatSpecsListToDict(formatspecs
)
6112 catfp
= BytesIO(catstr
)
6113 listcatfiles
= ArchiveFileToArray(
6114 catfp
, seekstart
, seekend
, listonly
, contentasfile
, True, skipchecksum
, formatspecs
, returnfp
)
6118 create_alias_function("", __file_format_name__
,
6119 "StringToArray", ArchiveFileStringToArray
)
6122 def TarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, contentasfile
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
6123 formatspecs
= FormatSpecsListToDict(formatspecs
)
6125 catfp
= PackArchiveFileFromTarFile(
6126 infile
, catfp
, "auto", True, None, "crc32", [], formatspecs
, False, True)
6127 listcatfiles
= ArchiveFileToArray(
6128 catfp
, seekstart
, seekend
, listonly
, contentasfile
, True, skipchecksum
, formatspecs
, returnfp
)
6132 def ZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, contentasfile
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
6133 formatspecs
= FormatSpecsListToDict(formatspecs
)
6135 catfp
= PackArchiveFileFromZipFile(
6136 infile
, catfp
, "auto", True, None, "crc32", [], formatspecs
, False, True)
6137 listcatfiles
= ArchiveFileToArray(
6138 catfp
, seekstart
, seekend
, listonly
, contentasfile
, True, skipchecksum
, formatspecs
, returnfp
)
6142 if(not rarfile_support
):
6143 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, contentasfile
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
6146 if(rarfile_support
):
6147 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, contentasfile
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
6148 formatspecs
= FormatSpecsListToDict(formatspecs
)
6150 catfp
= PackArchiveFileFromSevenZipFile(
6151 infile
, catfp
, "auto", True, None, "crc32", [], formatspecs
, False, True)
6152 listcatfiles
= ArchiveFileToArray(
6153 catfp
, seekstart
, seekend
, listonly
, contentasfile
, True, skipchecksum
, formatspecs
, returnfp
)
6156 if(not py7zr_support
):
6157 def SevenZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, contentasfile
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
6161 def SevenZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, contentasfile
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
6162 formatspecs
= FormatSpecsListToDict(formatspecs
)
6164 catfp
= PackArchiveFileFromSevenZipFile(
6165 infile
, catfp
, "auto", True, None, "crc32", [], formatspecs
, False, True)
6166 listcatfiles
= ArchiveFileToArray(
6167 catfp
, seekstart
, seekend
, listonly
, contentasfile
, True, skipchecksum
, formatspecs
, returnfp
)
6171 def InFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, contentasfile
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
6172 formatspecs
= FormatSpecsListToDict(formatspecs
)
6173 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True)
6174 if(checkcompressfile
== "tarfile" and TarFileCheck(infile
)):
6175 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
6176 elif(checkcompressfile
== "zipfile" and zipfile
.is_zipfile(infile
)):
6177 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
6178 elif(rarfile_support
and checkcompressfile
== "rarfile" and (rarfile
.is_rarfile(infile
) or rarfile
.is_rarfile_sfx(infile
))):
6179 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
6180 elif(py7zr_support
and checkcompressfile
== "7zipfile" and py7zr
.is_7zfile(infile
)):
6181 return SevenZipFileToArray(infile
, seekstart
, seekend
, listonly
, contentasfile
, skipchecksum
, formatspecs
, returnfp
)
6182 elif(checkcompressfile
== "catfile"):
6183 return ArchiveFileToArray(infile
, seekstart
, seekend
, listonly
, contentasfile
, True, skipchecksum
, formatspecs
, returnfp
)
6189 def ListDirToArrayAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=False, contentasfile
=True, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
6190 formatspecs
= FormatSpecsListToDict(formatspecs
)
6191 catver
= formatspecs
['format_ver']
6192 fileheaderver
= str(int(catver
.replace(".", "")))
6193 fileheader
= AppendNullByte(
6194 formatspecs
['format_magic'] + fileheaderver
, formatspecs
['format_delimiter'])
6195 advancedlist
= formatspecs
['use_advanced_list']
6196 altinode
= formatspecs
['use_alt_inode']
6199 for line
in sys
.stdin
:
6200 infilelist
.append(line
.strip())
6201 infilelist
= list(filter(None, infilelist
))
6202 elif(infiles
!= "-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
== "/dev/null" or infiles
== "NUL")):
6203 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
6205 with
UncompressFile(infiles
, formatspecs
, "r") as finfile
:
6206 for line
in finfile
:
6207 infilelist
.append(line
.strip())
6208 infilelist
= list(filter(None, infilelist
))
6210 if(isinstance(infiles
, (list, tuple, ))):
6211 infilelist
= list(filter(None, infiles
))
6212 elif(isinstance(infiles
, (str, ))):
6213 infilelist
= list(filter(None, [infiles
]))
6215 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False)
6217 GetDirList
= ListDir(infilelist
, followlink
, False)
6225 inodetocatinode
= {}
6227 fnumfiles
= int(len(GetDirList
))
6228 catver
= formatspecs
['format_ver']
6229 fileheaderver
= str(int(catver
.replace(".", "")))
6230 fileheader
= AppendNullByte(
6231 formatspecs
['format_magic'] + fileheaderver
, formatspecs
['format_delimiter'])
6232 fnumfileshex
= format(int(fnumfiles
), 'x').lower()
6233 fileheader
= fileheader
+ \
6234 AppendNullBytes([fnumfileshex
, checksumtype
],
6235 formatspecs
['format_delimiter'])
6236 catversion
= re
.findall("([\\d]+)", fileheader
)
6237 catversions
= re
.search('(.*?)(\\d+)', fileheader
).groups()
6238 catfileheadercshex
= GetFileChecksum(
6239 fileheader
, checksumtype
, True, formatspecs
)
6240 fileheader
= fileheader
+ \
6241 AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter'])
6242 fheadtell
= len(fileheader
)
6243 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1],
6244 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': []}
6245 for curfname
in GetDirList
:
6246 catfhstart
= fheadtell
6247 if(re
.findall("^[.|/]", curfname
)):
6250 fname
= "./"+curfname
6252 VerbosePrintOut(fname
)
6253 if(not followlink
or followlink
is None):
6254 fstatinfo
= os
.lstat(fname
)
6256 fstatinfo
= os
.stat(fname
)
6257 fpremode
= fstatinfo
.st_mode
6258 finode
= fstatinfo
.st_ino
6259 flinkcount
= fstatinfo
.st_nlink
6261 if(stat
.S_ISREG(fpremode
)):
6263 elif(stat
.S_ISLNK(fpremode
)):
6265 elif(stat
.S_ISCHR(fpremode
)):
6267 elif(stat
.S_ISBLK(fpremode
)):
6269 elif(stat
.S_ISDIR(fpremode
)):
6271 elif(stat
.S_ISFIFO(fpremode
)):
6273 elif(stat
.S_ISSOCK(fpremode
)):
6275 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
6277 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
6279 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
6284 fbasedir
= os
.path
.dirname(fname
)
6286 if(not followlink
and finode
!= 0):
6288 if(finode
in inodelist
):
6290 flinkname
= inodetofile
[finode
]
6294 fcurinode
= inodetocatinode
[finode
]
6295 if(finode
not in inodelist
):
6296 inodelist
.append(finode
)
6297 inodetofile
.update({finode
: fname
})
6298 inodetocatinode
.update({finode
: curinode
})
6302 fcurinode
= curinode
6303 curinode
= curinode
+ 1
6305 fcurinode
= curinode
6306 curinode
= curinode
+ 1
6309 flinkname
= os
.readlink(fname
)
6310 fdev
= fstatinfo
.st_dev
6311 getfdev
= GetDevMajorMinor(fdev
)
6312 fdev_minor
= getfdev
[0]
6313 fdev_major
= getfdev
[1]
6314 frdev
= fstatinfo
.st_dev
6315 if(hasattr(fstatinfo
, "st_rdev")):
6316 frdev
= fstatinfo
.st_rdev
6318 frdev
= fstatinfo
.st_dev
6319 getfrdev
= GetDevMajorMinor(frdev
)
6320 frdev_minor
= getfrdev
[0]
6321 frdev_major
= getfrdev
[1]
6322 if(ftype
== 1 or ftype
== 2 or ftype
== 3 or ftype
== 4 or ftype
== 5 or ftype
== 6):
6324 if(ftype
== 0 or ftype
== 7):
6325 fsize
= fstatinfo
.st_size
6326 fatime
= fstatinfo
.st_atime
6327 fmtime
= fstatinfo
.st_mtime
6328 fctime
= fstatinfo
.st_ctime
6329 if(hasattr(fstatinfo
, "st_birthtime")):
6330 fbtime
= fstatinfo
.st_birthtime
6332 fbtime
= fstatinfo
.st_ctime
6333 fmode
= fstatinfo
.st_mode
6334 fchmode
= stat
.S_IMODE(fstatinfo
.st_mode
)
6335 ftypemod
= stat
.S_IFMT(fstatinfo
.st_mode
)
6336 fuid
= fstatinfo
.st_uid
6337 fgid
= fstatinfo
.st_gid
6342 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
)
6343 funame
= userinfo
.pw_name
6352 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
)
6353 fgname
= groupinfo
.gr_name
6358 fdev_minor
= fdev_minor
6359 fdev_major
= fdev_major
6360 frdev_minor
= frdev_minor
6361 frdev_major
= frdev_major
6362 flinkcount
= flinkcount
6363 if(hasattr(fstatinfo
, "st_file_attributes")):
6364 fwinattributes
= fstatinfo
.st_file_attributes
6369 fcontents
= BytesIO()
6370 if(ftype
== 0 or ftype
== 7):
6371 with
open(fname
, "rb") as fpc
:
6372 shutil
.copyfileobj(fpc
, fcontents
)
6373 if(followlink
and (ftype
== 1 or ftype
== 2)):
6374 flstatinfo
= os
.stat(flinkname
)
6375 with
open(flinkname
, "rb") as fpc
:
6376 shutil
.copyfileobj(fpc
, fcontents
)
6377 fcontents
.seek(0, 0)
6378 ftypehex
= format(ftype
, 'x').lower()
6379 extrafields
= len(extradata
)
6380 extrafieldslist
= extradata
6381 catfextrafields
= extrafields
6382 extrasizestr
= AppendNullByte(
6383 extrafields
, formatspecs
['format_delimiter'])
6384 if(len(extradata
) > 0):
6385 extrasizestr
= extrasizestr
+ \
6386 AppendNullBytes(extradata
, formatspecs
['format_delimiter'])
6387 extrasizelen
= len(extrasizestr
)
6388 extrasizelenhex
= format(extrasizelen
, 'x').lower()
6389 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(), fcompression
, format(int(fcsize
), 'x').lower(), format(int(fuid
), 'x').lower(
6390 ), 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(), "+1", extrasizelenhex
, format(catfextrafields
, 'x').lower()]
6391 catoutlen
= len(catoutlist
) + len(extradata
) + 3
6392 catoutlenhex
= format(catoutlen
, 'x').lower()
6393 catoutlist
.insert(0, catoutlenhex
)
6394 catfileoutstr
= AppendNullBytes(
6395 catoutlist
, formatspecs
['format_delimiter'])
6396 catheaderdata
= catoutlist
6397 if(len(extradata
) > 0):
6398 catfileoutstr
= catfileoutstr
+ \
6399 AppendNullBytes(extradata
, formatspecs
['format_delimiter'])
6401 checksumlist
= [checksumtype
, "none"]
6403 checksumlist
= [checksumtype
, checksumtype
]
6404 catfileoutstr
= catfileoutstr
+ \
6405 AppendNullBytes(checksumlist
, formatspecs
['format_delimiter'])
6406 catfnumfields
= catoutlen
6407 catfileheadercshex
= GetFileChecksum(
6408 catfileoutstr
, checksumtype
, True, formatspecs
)
6409 fcontents
.seek(0, 0)
6411 catfilecontentcshex
= GetFileChecksum(
6412 fcontents
.read(), "none", False, formatspecs
)
6414 catfilecontentcshex
= GetFileChecksum(
6415 fcontents
.read(), checksumtype
, False, formatspecs
)
6416 tmpfileoutstr
= catfileoutstr
+ \
6417 AppendNullBytes([catfileheadercshex
, catfilecontentcshex
],
6418 formatspecs
['format_delimiter'])
6419 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
6420 catfileoutstr
= AppendNullByte(
6421 catheaersize
, formatspecs
['format_delimiter']) + catfileoutstr
6422 catfileheadercshex
= GetFileChecksum(
6423 catfileoutstr
, checksumtype
, True, formatspecs
)
6424 catfileoutstr
= catfileoutstr
+ \
6425 AppendNullBytes([catfileheadercshex
, catfilecontentcshex
],
6426 formatspecs
['format_delimiter'])
6427 catfileoutstrecd
= catfileoutstr
.encode('UTF-8')
6428 nullstrecd
= formatspecs
['format_delimiter'].encode('UTF-8')
6429 catfcontentstart
= fheadtell
6430 fheadtell
+= len(catfileoutstr
) + 1
6431 catfcontentend
= fheadtell
- 1
6432 catfhend
= catfcontentend
6433 fcontents
.seek(0, 0)
6434 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
6435 pyhascontents
= False
6436 if(int(fsize
) > 0 and not listonly
):
6437 pyhascontents
= True
6438 if(int(fsize
) > 0 and listonly
):
6439 fcontents
= BytesIO()
6440 pyhascontents
= False
6441 fcontents
.seek(0, 0)
6442 if(not contentasfile
):
6443 fcontents
= fcontents
.read()
6444 catlist
['ffilelist'].append({'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
, 'fcompression': fcompression
, 'fcsize': fcsize
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
,
6445 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fseeknextfile': "+1", 'fheaderchecksumtype': checksumtype
, 'fcontentchecksumtype': 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
, 'fcontentasfile': contentasfile
, 'fcontents': fcontents
})
6446 fileidnum
= fileidnum
+ 1
6450 def TarFileToArrayAlt(infile
, listonly
=False, contentasfile
=True, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
6451 formatspecs
= FormatSpecsListToDict(formatspecs
)
6457 inodetocatinode
= {}
6461 if(hasattr(sys
.stdin
, "buffer")):
6462 shutil
.copyfileobj(sys
.stdin
.buffer, infile
)
6464 shutil
.copyfileobj(sys
.stdin
, infile
)
6469 elif(re
.findall("^(http|https|ftp|ftps|sftp):\\/\\/", str(infile
))):
6470 infile
= download_file_from_internet_file(infile
)
6475 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6477 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
6479 if(not tarfile
.TarFileCheck(infile
)):
6481 except AttributeError:
6482 if(not TarFileCheck(infile
)):
6485 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
6486 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r")
6488 tarfp
= tarfile
.open(infile
, "r")
6489 except FileNotFoundError
:
6491 fnumfiles
= int(len(tarfp
.getmembers()))
6492 catver
= formatspecs
['format_ver']
6493 fileheaderver
= str(int(catver
.replace(".", "")))
6494 fileheader
= AppendNullByte(
6495 formatspecs
['format_magic'] + fileheaderver
, formatspecs
['format_delimiter'])
6496 fnumfileshex
= format(int(fnumfiles
), 'x').lower()
6497 fileheader
= fileheader
+ \
6498 AppendNullBytes([fnumfileshex
, checksumtype
],
6499 formatspecs
['format_delimiter'])
6500 catversion
= re
.findall("([\\d]+)", fileheader
)
6501 catversions
= re
.search('(.*?)(\\d+)', fileheader
).groups()
6502 catfileheadercshex
= GetFileChecksum(
6503 fileheader
, checksumtype
, True, formatspecs
)
6504 fileheader
= fileheader
+ \
6505 AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter'])
6506 fheadtell
= len(fileheader
)
6507 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1],
6508 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': []}
6509 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
6510 catfhstart
= fheadtell
6511 if(re
.findall("^[.|/]", member
.name
)):
6514 fname
= "./"+member
.name
6516 VerbosePrintOut(fname
)
6517 fpremode
= member
.mode
6518 ffullmode
= member
.mode
6522 ffullmode
= member
.mode
+ stat
.S_IFREG
6524 elif(member
.isdev()):
6525 ffullmode
= member
.mode
6527 elif(member
.islnk()):
6528 ffullmode
= member
.mode
+ stat
.S_IFREG
6530 elif(member
.issym()):
6531 ffullmode
= member
.mode
+ stat
.S_IFLNK
6533 elif(member
.ischr()):
6534 ffullmode
= member
.mode
+ stat
.S_IFCHR
6536 elif(member
.isblk()):
6537 ffullmode
= member
.mode
+ stat
.S_IFBLK
6539 elif(member
.isdir()):
6540 ffullmode
= member
.mode
+ stat
.S_IFDIR
6542 elif(member
.isfifo()):
6543 ffullmode
= member
.mode
+ stat
.S_IFIFO
6545 elif(member
.issparse()):
6546 ffullmode
= member
.mode
6549 ffullmode
= member
.mode
6552 fbasedir
= os
.path
.dirname(fname
)
6558 flinkname
= member
.linkname
6559 fdev_minor
= member
.devminor
6560 fdev_major
= member
.devmajor
6561 frdev_minor
= member
.devminor
6562 frdev_major
= member
.devmajor
6563 if(ftype
== 1 or ftype
== 2 or ftype
== 3 or ftype
== 4 or ftype
== 5 or ftype
== 6):
6565 elif(ftype
== 0 or ftype
== 7):
6569 fatime
= member
.mtime
6570 fmtime
= member
.mtime
6571 fctime
= member
.mtime
6572 fbtime
= member
.mtime
6574 fchmode
= stat
.S_IMODE(ffullmode
)
6575 ftypemod
= stat
.S_IFMT(ffullmode
)
6578 funame
= member
.uname
6579 fgname
= member
.gname
6580 flinkcount
= flinkcount
6581 fwinattributes
= int(0)
6584 fcontents
= BytesIO()
6585 if(ftype
== 0 or ftype
== 7):
6586 with tarfp
.extractfile(member
) as fpc
:
6587 shutil
.copyfileobj(fpc
, fcontents
)
6588 fcontents
.seek(0, 0)
6589 ftypehex
= format(ftype
, 'x').lower()
6590 extrafields
= len(extradata
)
6591 extrafieldslist
= extradata
6592 catfextrafields
= extrafields
6593 extrasizestr
= AppendNullByte(
6594 extrafields
, formatspecs
['format_delimiter'])
6595 if(len(extradata
) > 0):
6596 extrasizestr
= extrasizestr
+ \
6597 AppendNullBytes(extradata
, formatspecs
['format_delimiter'])
6598 extrasizelen
= len(extrasizestr
)
6599 extrasizelenhex
= format(extrasizelen
, 'x').lower()
6600 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(), fcompression
, format(int(fcsize
), 'x').lower(), format(int(fuid
), 'x').lower(
6601 ), 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(), "+1", extrasizelenhex
, format(catfextrafields
, 'x').lower()]
6602 catoutlen
= len(catoutlist
) + len(extradata
) + 3
6603 catoutlenhex
= format(catoutlen
, 'x').lower()
6604 catoutlist
.insert(0, catoutlenhex
)
6605 catfileoutstr
= AppendNullBytes(
6606 catoutlist
, formatspecs
['format_delimiter'])
6607 catheaderdata
= catoutlist
6608 if(len(extradata
) > 0):
6609 catfileoutstr
= catfileoutstr
+ \
6610 AppendNullBytes(extradata
, formatspecs
['format_delimiter'])
6612 checksumlist
= [checksumtype
, "none"]
6614 checksumlist
= [checksumtype
, checksumtype
]
6615 catfileoutstr
= catfileoutstr
+ \
6616 AppendNullBytes(checksumlist
, formatspecs
['format_delimiter'])
6617 catfnumfields
= catoutlen
6618 catfileheadercshex
= GetFileChecksum(
6619 catfileoutstr
, checksumtype
, True, formatspecs
)
6620 fcontents
.seek(0, 0)
6622 catfilecontentcshex
= GetFileChecksum(
6623 fcontents
.read(), "none", False, formatspecs
)
6625 catfilecontentcshex
= GetFileChecksum(
6626 fcontents
.read(), checksumtype
, False, formatspecs
)
6627 tmpfileoutstr
= catfileoutstr
+ \
6628 AppendNullBytes([catfileheadercshex
, catfilecontentcshex
],
6629 formatspecs
['format_delimiter'])
6630 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
6631 catfileoutstr
= AppendNullByte(
6632 catheaersize
, formatspecs
['format_delimiter']) + catfileoutstr
6633 catfileheadercshex
= GetFileChecksum(
6634 catfileoutstr
, checksumtype
, True, formatspecs
)
6635 catfileoutstr
= catfileoutstr
+ \
6636 AppendNullBytes([catfileheadercshex
, catfilecontentcshex
],
6637 formatspecs
['format_delimiter'])
6638 catfileoutstrecd
= catfileoutstr
.encode('UTF-8')
6639 nullstrecd
= formatspecs
['format_delimiter'].encode('UTF-8')
6640 catfcontentstart
= fheadtell
6641 fheadtell
+= len(catfileoutstr
) + 1
6642 catfcontentend
= fheadtell
- 1
6643 catfhend
= catfcontentend
6644 fcontents
.seek(0, 0)
6645 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
6646 pyhascontents
= False
6647 if(int(fsize
) > 0 and not listonly
):
6648 pyhascontents
= True
6649 if(int(fsize
) > 0 and listonly
):
6650 fcontents
= BytesIO()
6651 pyhascontents
= False
6652 fcontents
.seek(0, 0)
6653 if(not contentasfile
):
6654 fcontents
= fcontents
.read()
6655 catlist
['ffilelist'].append({'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
, 'fcompression': fcompression
, 'fcsize': fcsize
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
,
6656 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fseeknextfile': "+1", 'fheaderchecksumtype': checksumtype
, 'fcontentchecksumtype': 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
, 'fcontentasfile': contentasfile
, 'fcontents': fcontents
})
6657 fileidnum
= fileidnum
+ 1
6661 def ZipFileToArrayAlt(infile
, listonly
=False, contentasfile
=True, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
6662 formatspecs
= FormatSpecsListToDict(formatspecs
)
6668 inodetocatinode
= {}
6672 if(hasattr(sys
.stdin
, "buffer")):
6673 shutil
.copyfileobj(sys
.stdin
.buffer, infile
)
6675 shutil
.copyfileobj(sys
.stdin
, infile
)
6680 elif(re
.findall("^(http|https|ftp|ftps|sftp):\\/\\/", str(infile
))):
6681 infile
= download_file_from_internet_file(infile
)
6686 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6688 if(not zipfile
.is_zipfile(infile
)):
6691 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True)
6692 except FileNotFoundError
:
6694 ziptest
= zipfp
.testzip()
6696 VerbosePrintOut("Bad file found!")
6697 fnumfiles
= int(len(zipfp
.infolist()))
6698 catver
= formatspecs
['format_ver']
6699 fileheaderver
= str(int(catver
.replace(".", "")))
6700 fileheader
= AppendNullByte(
6701 formatspecs
['format_magic'] + fileheaderver
, formatspecs
['format_delimiter'])
6702 catversion
= re
.findall("([\\d]+)", fileheader
)
6703 catversions
= re
.search('(.*?)(\\d+)', fileheader
).groups()
6704 fnumfileshex
= format(int(fnumfiles
), 'x').lower()
6705 fileheader
= fileheader
+ \
6706 AppendNullBytes([fnumfileshex
, checksumtype
],
6707 formatspecs
['format_delimiter'])
6708 catfileheadercshex
= GetFileChecksum(
6709 fileheader
, checksumtype
, True, formatspecs
)
6710 fileheader
= fileheader
+ \
6711 AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter'])
6712 fheadtell
= len(fileheader
)
6713 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1],
6714 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': []}
6715 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
6716 catfhstart
= fheadtell
6717 if(re
.findall("^[.|/]", member
.filename
)):
6718 fname
= member
.filename
6720 fname
= "./"+member
.filename
6721 zipinfo
= zipfp
.getinfo(member
.filename
)
6723 VerbosePrintOut(fname
)
6724 if(not member
.is_dir()):
6725 fpremode
= stat
.S_IFREG
+ 438
6726 elif(member
.is_dir()):
6727 fpremode
= stat
.S_IFDIR
+ 511
6730 if(not member
.is_dir()):
6732 elif(member
.is_dir()):
6735 fbasedir
= os
.path
.dirname(fname
)
6747 fsize
= member
.file_size
6749 fsize
= member
.file_size
6750 fatime
= time
.mktime(member
.date_time
+ (0, 0, -1))
6751 fmtime
= time
.mktime(member
.date_time
+ (0, 0, -1))
6752 fctime
= time
.mktime(member
.date_time
+ (0, 0, -1))
6753 fbtime
= time
.mktime(member
.date_time
+ (0, 0, -1))
6754 if(zipinfo
.create_system
== 0 or zipinfo
.create_system
== 10):
6755 fwinattributes
= int(zipinfo
.external_attr
)
6756 if(not member
.is_dir()):
6757 fmode
= int(stat
.S_IFREG
+ 438)
6758 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)))
6759 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)))
6760 elif(member
.is_dir()):
6761 fmode
= int(stat
.S_IFDIR
+ 511)
6762 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)))
6763 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)))
6764 elif(zipinfo
.create_system
== 3):
6765 fwinattributes
= int(0)
6767 fmode
= int(zipinfo
.external_attr
)
6768 fchmode
= stat
.S_IMODE(fmode
)
6769 ftypemod
= stat
.S_IFMT(fmode
)
6770 except OverflowError:
6771 fmode
= int(zipinfo
.external_attr
>> 16)
6772 fchmode
= stat
.S_IMODE(fmode
)
6773 ftypemod
= stat
.S_IFMT(fmode
)
6775 fwinattributes
= int(0)
6776 if(not member
.is_dir()):
6777 fmode
= int(stat
.S_IFREG
+ 438)
6778 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)))
6779 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)))
6780 elif(member
.is_dir()):
6781 fmode
= int(stat
.S_IFDIR
+ 511)
6782 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)))
6783 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)))
6788 except AttributeError:
6794 except AttributeError:
6801 userinfo
= pwd
.getpwuid(os
.getuid())
6802 funame
= userinfo
.pw_name
6805 except AttributeError:
6813 groupinfo
= grp
.getgrgid(os
.getgid())
6814 fgname
= groupinfo
.gr_name
6817 except AttributeError:
6821 fcontents
= BytesIO()
6823 fcontents
.write(zipfp
.read(member
.filename
))
6824 fcontents
.seek(0, 0)
6825 ftypehex
= format(ftype
, 'x').lower()
6826 extrafields
= len(extradata
)
6827 extrafieldslist
= extradata
6828 catfextrafields
= extrafields
6829 extrasizestr
= AppendNullByte(
6830 extrafields
, formatspecs
['format_delimiter'])
6831 if(len(extradata
) > 0):
6832 extrasizestr
= extrasizestr
+ \
6833 AppendNullBytes(extradata
, formatspecs
['format_delimiter'])
6834 extrasizelen
= len(extrasizestr
)
6835 extrasizelenhex
= format(extrasizelen
, 'x').lower()
6836 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(), fcompression
, format(int(fcsize
), 'x').lower(), format(int(fuid
), 'x').lower(
6837 ), 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(), "+1", extrasizelenhex
, format(catfextrafields
, 'x').lower()]
6838 catoutlen
= len(catoutlist
) + len(extradata
) + 3
6839 catoutlenhex
= format(catoutlen
, 'x').lower()
6840 catoutlist
.insert(0, catoutlenhex
)
6841 catfileoutstr
= AppendNullBytes(
6842 catoutlist
, formatspecs
['format_delimiter'])
6843 catheaderdata
= catoutlist
6844 if(len(extradata
) > 0):
6845 catfileoutstr
= catfileoutstr
+ \
6846 AppendNullBytes(extradata
, formatspecs
['format_delimiter'])
6848 checksumlist
= [checksumtype
, "none"]
6850 checksumlist
= [checksumtype
, checksumtype
]
6851 catfileoutstr
= catfileoutstr
+ \
6852 AppendNullBytes(checksumlist
, formatspecs
['format_delimiter'])
6853 catfnumfields
= catoutlen
6854 catfileheadercshex
= GetFileChecksum(
6855 catfileoutstr
, checksumtype
, True, formatspecs
)
6856 fcontents
.seek(0, 0)
6858 catfilecontentcshex
= GetFileChecksum(
6859 fcontents
.read(), "none", False, formatspecs
)
6861 catfilecontentcshex
= GetFileChecksum(
6862 fcontents
.read(), checksumtype
, False, formatspecs
)
6863 tmpfileoutstr
= catfileoutstr
+ \
6864 AppendNullBytes([catfileheadercshex
, catfilecontentcshex
],
6865 formatspecs
['format_delimiter'])
6866 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
6867 catfileoutstr
= AppendNullByte(
6868 catheaersize
, formatspecs
['format_delimiter']) + catfileoutstr
6869 catfileheadercshex
= GetFileChecksum(
6870 catfileoutstr
, checksumtype
, True, formatspecs
)
6871 catfileoutstr
= catfileoutstr
+ \
6872 AppendNullBytes([catfileheadercshex
, catfilecontentcshex
],
6873 formatspecs
['format_delimiter'])
6874 catfileoutstrecd
= catfileoutstr
.encode('UTF-8')
6875 nullstrecd
= formatspecs
['format_delimiter'].encode('UTF-8')
6876 catfcontentstart
= fheadtell
6877 fheadtell
+= len(catfileoutstr
) + 1
6878 catfcontentend
= fheadtell
- 1
6879 catfhend
= catfcontentend
6880 fcontents
.seek(0, 0)
6881 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
6882 pyhascontents
= False
6883 if(int(fsize
) > 0 and not listonly
):
6884 pyhascontents
= True
6885 if(int(fsize
) > 0 and listonly
):
6886 fcontents
= BytesIO()
6887 pyhascontents
= False
6888 fcontents
.seek(0, 0)
6889 if(not contentasfile
):
6890 fcontents
= fcontents
.read()
6891 catlist
['ffilelist'].append({'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
, 'fcompression': fcompression
, 'fcsize': fcsize
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
,
6892 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fseeknextfile': "+1", 'fheaderchecksumtype': checksumtype
, 'fcontentchecksumtype': 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
, 'fcontentasfile': contentasfile
, 'fcontents': fcontents
})
6893 fileidnum
= fileidnum
+ 1
6897 if(not rarfile_support
):
6898 def RarFileToArrayAlt(infile
, listonly
=False, contentasfile
=True, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
6901 if(rarfile_support
):
6902 def RarFileToArrayAlt(infile
, listonly
=False, contentasfile
=True, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
6903 formatspecs
= FormatSpecsListToDict(formatspecs
)
6909 inodetocatinode
= {}
6911 if(not os
.path
.exists(infile
,) or not os
.path
.isfile(infile
,)):
6913 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
6915 rarfp
= rarfile
.RarFile(infile
, "r")
6916 rartest
= rarfp
.testrar()
6918 VerbosePrintOut("Bad file found!")
6919 fnumfiles
= int(len(rarfp
.infolist()))
6920 catver
= formatspecs
['format_ver']
6921 fileheaderver
= str(int(catver
.replace(".", "")))
6922 fileheader
= AppendNullByte(
6923 formatspecs
['format_magic'] + fileheaderver
, formatspecs
['format_delimiter'])
6924 catversion
= re
.findall("([\\d]+)", fileheader
)
6925 catversions
= re
.search('(.*?)(\\d+)', fileheader
).groups()
6926 fnumfileshex
= format(int(fnumfiles
), 'x').lower()
6927 fileheader
= fileheader
+ \
6928 AppendNullBytes([fnumfileshex
, checksumtype
],
6929 formatspecs
['format_delimiter'])
6930 catfileheadercshex
= GetFileChecksum(
6931 fileheader
, checksumtype
, True, formatspecs
)
6932 fileheader
= fileheader
+ \
6933 AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter'])
6934 fheadtell
= len(fileheader
)
6935 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1],
6936 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': []}
6937 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
6938 catfhstart
= fheadtell
6941 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
6944 member
.external_attr
6946 except AttributeError:
6948 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
6951 member
.external_attr
6953 except AttributeError:
6958 if(re
.findall("^[.|/]", member
.filename
)):
6959 fname
= member
.filename
6961 fname
= "./"+member
.filename
6962 rarinfo
= rarfp
.getinfo(member
.filename
)
6964 VerbosePrintOut(fname
)
6965 if(is_unix
and member
.external_attr
!= 0):
6966 fpremode
= int(member
.external_attr
)
6967 elif(member
.is_file()):
6968 fpremode
= stat
.S_IFREG
+ 438
6969 elif(member
.is_symlink()):
6970 fpremode
= stat
.S_IFLNK
+ 438
6971 elif(member
.is_dir()):
6972 fpremode
= stat
.S_IFDIR
+ 511
6973 if(is_windows
and member
.external_attr
!= 0):
6974 fwinattributes
= int(member
.external_attr
)
6976 fwinattributes
= int(0)
6981 if(member
.is_file()):
6983 elif(member
.is_symlink()):
6985 elif(member
.is_dir()):
6989 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8")
6990 fbasedir
= os
.path
.dirname(fname
)
7002 fsize
= member
.file_size
7005 fatime
= int(member
.atime
.timestamp())
7007 fatime
= int(member
.mtime
.timestamp())
7008 except AttributeError:
7009 fatime
= int(member
.mtime
.timestamp())
7010 fmtime
= int(member
.mtime
.timestamp())
7013 fctime
= int(member
.ctime
.timestamp())
7015 fctime
= int(member
.mtime
.timestamp())
7016 except AttributeError:
7017 fctime
= int(member
.mtime
.timestamp())
7018 fbtime
= int(member
.mtime
.timestamp())
7019 if(is_unix
and member
.external_attr
!= 0):
7020 fmode
= int(member
.external_attr
)
7021 fchmode
= int(stat
.S_IMODE(member
.external_attr
))
7022 ftypemod
= int(stat
.S_IFMT(member
.external_attr
))
7023 elif(member
.is_file()):
7024 fmode
= int(stat
.S_IFREG
+ 438)
7025 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438))
7026 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438))
7027 elif(member
.is_symlink()):
7028 fmode
= int(stat
.S_IFLNK
+ 438)
7029 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438))
7030 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438))
7031 elif(member
.is_dir()):
7032 fmode
= int(stat
.S_IFDIR
+ 511)
7033 fchmode
= int(stat
.S_IMODE(stat
.S_IFDIR
+ 511))
7034 ftypemod
= int(stat
.S_IFMT(stat
.S_IFDIR
+ 511))
7037 except AttributeError:
7043 except AttributeError:
7050 userinfo
= pwd
.getpwuid(os
.getuid())
7051 funame
= userinfo
.pw_name
7054 except AttributeError:
7062 groupinfo
= grp
.getgrgid(os
.getgid())
7063 fgname
= groupinfo
.gr_name
7066 except AttributeError:
7070 fcontents
= BytesIO()
7072 fcontents
.write(rarfp
.read(member
.filename
))
7073 fcontents
.seek(0, 0)
7074 ftypehex
= format(ftype
, 'x').lower()
7075 extrafields
= len(extradata
)
7076 extrafieldslist
= extradata
7077 catfextrafields
= extrafields
7078 extrasizestr
= AppendNullByte(
7079 extrafields
, formatspecs
['format_delimiter'])
7080 if(len(extradata
) > 0):
7081 extrasizestr
= extrasizestr
+ \
7082 AppendNullBytes(extradata
, formatspecs
['format_delimiter'])
7083 extrasizelen
= len(extrasizestr
)
7084 extrasizelenhex
= format(extrasizelen
, 'x').lower()
7085 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(), fcompression
, format(int(fcsize
), 'x').lower(), format(int(fuid
), 'x').lower(
7086 ), 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(), "+1", extrasizelenhex
, format(catfextrafields
, 'x').lower()]
7087 catoutlen
= len(catoutlist
) + len(extradata
) + 3
7088 catoutlenhex
= format(catoutlen
, 'x').lower()
7089 catoutlist
.insert(0, catoutlenhex
)
7090 catfileoutstr
= AppendNullBytes(
7091 catoutlist
, formatspecs
['format_delimiter'])
7092 if(len(extradata
) > 0):
7093 catfileoutstr
= catfileoutstr
+ \
7094 AppendNullBytes(extradata
, formatspecs
['format_delimiter'])
7096 checksumlist
= [checksumtype
, "none"]
7098 checksumlist
= [checksumtype
, checksumtype
]
7099 ccatfileoutstr
= catfileoutstr
+ \
7100 AppendNullBytes(checksumlist
, formatspecs
['format_delimiter'])
7101 catfnumfields
= 24 + catfextrafields
7102 catfileheadercshex
= GetFileChecksum(
7103 catfileoutstr
, checksumtype
, True, formatspecs
)
7104 fcontents
.seek(0, 0)
7106 catfilecontentcshex
= GetFileChecksum(
7107 fcontents
.read(), "none", False, formatspecs
)
7109 catfilecontentcshex
= GetFileChecksum(
7110 fcontents
.read(), checksumtype
, False, formatspecs
)
7111 tmpfileoutstr
= catfileoutstr
+ \
7113 [catfileheadercshex
, catfilecontentcshex
], formatspecs
['format_delimiter'])
7114 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
7115 catfileoutstr
= AppendNullByte(
7116 catheaersize
, formatspecs
['format_delimiter']) + catfileoutstr
7117 catfileheadercshex
= GetFileChecksum(
7118 catfileoutstr
, checksumtype
, True, formatspecs
)
7119 catfileoutstr
= catfileoutstr
+ \
7121 [catfileheadercshex
, catfilecontentcshex
], formatspecs
['format_delimiter'])
7122 catheaderdata
= catoutlist
7123 catfileoutstrecd
= catfileoutstr
.encode('UTF-8')
7124 nullstrecd
= formatspecs
['format_delimiter'].encode('UTF-8')
7125 catfcontentstart
= fheadtell
7126 fheadtell
+= len(catfileoutstr
) + 1
7127 catfcontentend
= fheadtell
- 1
7128 catfhend
= catfcontentend
7129 fcontents
.seek(0, 0)
7130 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
7131 pyhascontents
= False
7132 if(int(fsize
) > 0 and not listonly
):
7133 pyhascontents
= True
7134 if(int(fsize
) > 0 and listonly
):
7135 fcontents
= BytesIO()
7136 pyhascontents
= False
7137 fcontents
.seek(0, 0)
7138 if(not contentasfile
):
7139 fcontents
= fcontents
.read()
7140 catlist
['ffilelist'].append({'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
, 'fcompression': fcompression
, 'fcsize': fcsize
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
,
7141 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fseeknextfile': "+1", 'fheaderchecksumtype': checksumtype
, 'fcontentchecksumtype': 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
, 'fcontentasfile': contentasfile
, 'fcontents': fcontents
})
7142 fileidnum
= fileidnum
+ 1
7145 if(not py7zr_support
):
7146 def SevenZipFileToArrayAlt(infile
, listonly
=False, contentasfile
=True, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
7150 def SevenZipFileToArrayAlt(infile
, listonly
=False, contentasfile
=True, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
7151 formatspecs
= FormatSpecsListToDict(formatspecs
)
7157 inodetocatinode
= {}
7159 szpfp
= py7zr
.SevenZipFile(infile
, mode
="r")
7160 file_content
= szpfp
.readall()
7161 #sztest = szpfp.testzip();
7162 sztestalt
= szpfp
.test()
7164 VerbosePrintOut("Bad file found!")
7165 numfiles
= int(len(szpfp
.list()))
7166 catver
= formatspecs
['format_ver']
7167 fileheaderver
= str(int(catver
.replace(".", "")))
7168 fileheader
= AppendNullByte(
7169 formatspecs
['format_magic'] + fileheaderver
, formatspecs
['format_delimiter'])
7170 catversion
= re
.findall("([\\d]+)", fileheader
)
7171 catversions
= re
.search('(.*?)(\\d+)', fileheader
).groups()
7172 fnumfileshex
= format(int(fnumfiles
), 'x').lower()
7173 fileheader
= fileheader
+ \
7174 AppendNullBytes([fnumfileshex
, checksumtype
],
7175 formatspecs
['format_delimiter'])
7176 catfileheadercshex
= GetFileChecksum(
7177 fileheader
, checksumtype
, True, formatspecs
)
7178 fileheader
= fileheader
+ \
7179 AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter'])
7180 fheadtell
= len(fileheader
)
7181 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1],
7182 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': []}
7183 for member
in sorted(szpfp
.list(), key
=lambda x
: x
.filename
):
7184 catfhstart
= fheadtell
7185 if(re
.findall("^[.|/]", member
.filename
)):
7186 fname
= member
.filename
7188 fname
= "./"+member
.filename
7189 if(not member
.is_directory
):
7190 fpremode
= int(stat
.S_IFREG
+ 438)
7191 elif(member
.is_directory
):
7192 fpremode
= int(stat
.S_IFDIR
+ 511)
7193 fwinattributes
= int(0)
7198 if(member
.is_directory
):
7203 fbasedir
= os
.path
.dirname(fname
)
7214 fatime
= int(member
.creationtime
.timestamp())
7215 fmtime
= int(member
.creationtime
.timestamp())
7216 fctime
= int(member
.creationtime
.timestamp())
7217 fbtime
= int(member
.creationtime
.timestamp())
7218 if(member
.is_directory
):
7219 fmode
= int(stat
.S_IFDIR
+ 511)
7220 fchmode
= int(stat
.S_IMODE(stat
.S_IFDIR
+ 511))
7221 ftypemod
= int(stat
.S_IFMT(stat
.S_IFDIR
+ 511))
7223 fmode
= int(stat
.S_IFLNK
+ 438)
7224 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438))
7225 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438))
7228 except AttributeError:
7234 except AttributeError:
7241 userinfo
= pwd
.getpwuid(os
.getuid())
7242 funame
= userinfo
.pw_name
7245 except AttributeError:
7253 groupinfo
= grp
.getgrgid(os
.getgid())
7254 fgname
= groupinfo
.gr_name
7257 except AttributeError:
7261 fcontents
= BytesIO()
7263 fcontents
.write(file_content
[member
.filename
].read())
7264 fsize
= format(fcontents
.tell(), 'x').lower()
7266 fcontents
.seek(0, 0)
7267 ftypehex
= format(ftype
, 'x').lower()
7268 extrafields
= len(extradata
)
7269 extrafieldslist
= extradata
7270 catfextrafields
= extrafields
7271 extrasizestr
= AppendNullByte(
7272 extrafields
, formatspecs
['format_delimiter'])
7273 if(len(extradata
) > 0):
7274 extrasizestr
= extrasizestr
+ \
7275 AppendNullBytes(extradata
, formatspecs
['format_delimiter'])
7276 extrasizelen
= len(extrasizestr
)
7277 extrasizelenhex
= format(extrasizelen
, 'x').lower()
7278 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(), fcompression
, format(int(fcsize
), 'x').lower(), format(int(fuid
), 'x').lower(
7279 ), 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(), "+1", extrasizelenhex
, format(catfextrafields
, 'x').lower()]
7280 catoutlen
= len(catoutlist
) + len(extradata
) + 3
7281 catoutlenhex
= format(catoutlen
, 'x').lower()
7282 catoutlist
.insert(0, catoutlenhex
)
7283 catfileoutstr
= AppendNullBytes(
7284 catoutlist
, formatspecs
['format_delimiter'])
7285 catheaderdata
= catoutlist
7286 if(len(extradata
) > 0):
7287 catfileoutstr
= catfileoutstr
+ \
7288 AppendNullBytes(extradata
, formatspecs
['format_delimiter'])
7290 checksumlist
= [checksumtype
, "none"]
7292 checksumlist
= [checksumtype
, checksumtype
]
7293 catfileoutstr
= catfileoutstr
+ \
7294 AppendNullBytes(checksumlist
, formatspecs
['format_delimiter'])
7295 catfnumfields
= 24 + catfextrafields
7296 catfileheadercshex
= GetFileChecksum(
7297 catfileoutstr
, checksumtype
, True, formatspecs
)
7298 fcontents
.seek(0, 0)
7300 catfilecontentcshex
= GetFileChecksum(
7301 fcontents
.read(), "none", False, formatspecs
)
7303 catfilecontentcshex
= GetFileChecksum(
7304 fcontents
.read(), checksumtype
, False, formatspecs
)
7305 tmpfileoutstr
= catfileoutstr
+ \
7307 [catfileheadercshex
, catfilecontentcshex
], formatspecs
['format_delimiter'])
7308 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
7309 catfileoutstr
= AppendNullByte(
7310 catheaersize
, formatspecs
['format_delimiter']) + catfileoutstr
7311 catfileheadercshex
= GetFileChecksum(
7312 catfileoutstr
, checksumtype
, True, formatspecs
)
7313 catfileoutstr
= catfileoutstr
+ \
7315 [catfileheadercshex
, catfilecontentcshex
], formatspecs
['format_delimiter'])
7316 catfileoutstrecd
= catfileoutstr
.encode('UTF-8')
7317 nullstrecd
= formatspecs
['format_delimiter'].encode('UTF-8')
7318 catfcontentstart
= fheadtell
7319 fheadtell
+= len(catfileoutstr
) + 1
7320 catfcontentend
= fheadtell
- 1
7321 catfhend
= catfcontentend
7322 fcontents
.seek(0, 0)
7323 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
7324 pyhascontents
= False
7325 if(int(fsize
) > 0 and not listonly
):
7326 pyhascontents
= True
7327 if(int(fsize
) > 0 and listonly
):
7328 fcontents
= BytesIO()
7329 pyhascontents
= False
7330 fcontents
.seek(0, 0)
7331 if(not contentasfile
):
7332 fcontents
= fcontents
.read()
7333 catlist
['ffilelist'].append({'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
, 'fcompression': fcompression
, 'fcsize': fcsize
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
,
7334 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fseeknextfile': "+1", 'fheaderchecksumtype': checksumtype
, 'fcontentchecksumtype': 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
, 'fcontentasfile': contentasfile
, 'fcontents': fcontents
})
7335 fileidnum
= fileidnum
+ 1
7339 def InFileToArrayAlt(infile
, listonly
=False, contentasfile
=True, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
7340 formatspecs
= FormatSpecsListToDict(formatspecs
)
7341 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True)
7342 if(checkcompressfile
== "tarfile" and TarFileCheck(infile
)):
7343 return TarFileToArrayAlt(infile
, listonly
, contentasfile
, checksumtype
, extradata
, formatspecs
, verbose
)
7344 elif(checkcompressfile
== "zipfile" and zipfile
.is_zipfile(infile
)):
7345 return ZipFileToArrayAlt(infile
, listonly
, contentasfile
, checksumtype
, extradata
, formatspecs
, verbose
)
7346 elif(rarfile_support
and checkcompressfile
== "rarfile" and (rarfile
.is_rarfile(infile
) or rarfile
.is_rarfile_sfx(infile
))):
7347 return RarFileToArrayAlt(infile
, listonly
, contentasfile
, checksumtype
, extradata
, formatspecs
, verbose
)
7348 elif(py7zr_support
and checkcompressfile
== "7zipfile" and py7zr
.is_7zfile(infile
)):
7349 return SevenZipFileToArrayAlt(infile
, listonly
, contentasfile
, checksumtype
, extradata
, formatspecs
, verbose
)
7350 elif(checkcompressfile
== "catfile"):
7351 return ArchiveFileToArray(infile
, 0, 0, listonly
, contentasfile
, True, False, formatspecs
, False)
7357 def ListDirToArray(infiles
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
7358 formatspecs
= FormatSpecsListToDict(formatspecs
)
7359 outarray
= BytesIO()
7360 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compresswholefile
,
7361 compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True)
7362 listcatfiles
= ArchiveFileToArray(
7363 outarray
, seekstart
, seekend
, listonly
, True, skipchecksum
, formatspecs
, returnfp
)
7367 def ArchiveFileArrayToArrayIndex(inarray
, seekstart
=0, seekend
=0, listonly
=False, uncompress
=True, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
7368 formatspecs
= FormatSpecsListToDict(formatspecs
)
7369 if(isinstance(inarray
, dict)):
7370 listcatfiles
= inarray
7373 if(not listcatfiles
):
7375 catarray
= {'list': listcatfiles
, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {
7376 }, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}}
7378 catarray
.update({'catfp': listcatfiles
['catfp']})
7379 lenlist
= len(listcatfiles
['ffilelist'])
7381 lcfx
= int(listcatfiles
['fnumfiles'])
7382 if(lenlist
> listcatfiles
['fnumfiles'] or lenlist
< listcatfiles
['fnumfiles']):
7385 lcfx
= int(listcatfiles
['fnumfiles'])
7387 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]
7388 ['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']}
7389 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]
7390 ['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']}
7391 catarray
['filetoid'].update(filetoidarray
)
7392 catarray
['idtofile'].update(idtofilearray
)
7393 if(listcatfiles
['ffilelist'][lcfi
]['ftype'] == 0 or listcatfiles
['ffilelist'][lcfi
]['ftype'] == 7):
7394 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
)
7395 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
)
7396 if(listcatfiles
['ffilelist'][lcfi
]['ftype'] == 1):
7397 catarray
['filetypes']['hardlinks']['filetoid'].update(
7399 catarray
['filetypes']['hardlinks']['idtofile'].update(
7401 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
)
7402 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
)
7403 if(listcatfiles
['ffilelist'][lcfi
]['ftype'] == 2):
7404 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
)
7405 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
)
7406 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
)
7407 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
)
7408 if(listcatfiles
['ffilelist'][lcfi
]['ftype'] == 3):
7409 catarray
['filetypes']['character']['filetoid'].update(
7411 catarray
['filetypes']['character']['idtofile'].update(
7413 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
)
7414 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
)
7415 if(listcatfiles
['ffilelist'][lcfi
]['ftype'] == 4):
7416 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
)
7417 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
)
7418 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
)
7419 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
)
7420 if(listcatfiles
['ffilelist'][lcfi
]['ftype'] == 5):
7421 catarray
['filetypes']['directories']['filetoid'].update(
7423 catarray
['filetypes']['directories']['idtofile'].update(
7425 if(listcatfiles
['ffilelist'][lcfi
]['ftype'] == 6):
7426 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
)
7427 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
)
7428 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
)
7429 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
)
7434 create_alias_function("", __file_format_name__
,
7435 "ArrayToArrayIndex", ArchiveFileArrayToArrayIndex
)
7438 def RePackArchiveFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
7439 formatspecs
= FormatSpecsListToDict(formatspecs
)
7440 if(isinstance(infile
, dict)):
7441 listcatfiles
= infile
7443 if(infile
!= "-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
7444 infile
= RemoveWindowsPath(infile
)
7445 listcatfiles
= ArchiveFileToArray(
7446 infile
, seekstart
, seekend
, False, True, skipchecksum
, formatspecs
, returnfp
)
7447 if(outfile
!= "-" and not hasattr(infile
, "read") and not hasattr(outfile
, "write")):
7448 outfile
= RemoveWindowsPath(outfile
)
7449 checksumtype
= checksumtype
.lower()
7450 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
7451 checksumtype
= "crc32"
7452 if(checksumtype
== "none"):
7454 if(not compression
or compression
== "catfile" or compression
== formatspecs
['format_lower']):
7455 compression
= "auto"
7456 if(compression
not in compressionlist
and compression
is None):
7457 compression
= "auto"
7459 logging
.basicConfig(format
="%(message)s",
7460 stream
=sys
.stdout
, level
=logging
.DEBUG
)
7461 if(outfile
!= "-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
7462 if(os
.path
.exists(outfile
)):
7465 except OSError as e
:
7467 if(not listcatfiles
):
7472 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
7474 elif(re
.findall("^(ftp|ftps|sftp):\\/\\/", str(outfile
))):
7477 fbasename
= os
.path
.splitext(outfile
)[0]
7478 fextname
= os
.path
.splitext(outfile
)[1]
7479 if(not compresswholefile
and fextname
in outextlistwd
):
7480 compresswholefile
= True
7481 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
)
7482 catver
= formatspecs
['format_ver']
7483 fileheaderver
= str(int(catver
.replace(".", "")))
7484 lenlist
= len(listcatfiles
['ffilelist'])
7485 fnumfiles
= int(listcatfiles
['fnumfiles'])
7486 if(lenlist
> fnumfiles
or lenlist
< fnumfiles
):
7488 AppendFileHeader(catfp
, fnumfiles
, checksumtype
, formatspecs
)
7489 lenlist
= len(listcatfiles
['ffilelist'])
7490 fnumfiles
= int(listcatfiles
['fnumfiles'])
7492 lcfx
= int(listcatfiles
['fnumfiles'])
7493 if(lenlist
> listcatfiles
['fnumfiles'] or lenlist
< listcatfiles
['fnumfiles']):
7496 lcfx
= int(listcatfiles
['fnumfiles'])
7504 if(re
.findall("^[.|/]", listcatfiles
['ffilelist'][reallcfi
]['fname'])):
7505 fname
= listcatfiles
['ffilelist'][reallcfi
]['fname']
7507 fname
= "./"+listcatfiles
['ffilelist'][reallcfi
]['fname']
7509 VerbosePrintOut(fname
)
7510 fheadersize
= format(
7511 int(listcatfiles
['ffilelist'][reallcfi
]['fheadersize']), 'x').lower()
7513 int(listcatfiles
['ffilelist'][reallcfi
]['fsize']), 'x').lower()
7514 flinkname
= listcatfiles
['ffilelist'][reallcfi
]['flinkname']
7516 int(listcatfiles
['ffilelist'][reallcfi
]['fatime']), 'x').lower()
7518 int(listcatfiles
['ffilelist'][reallcfi
]['fmtime']), 'x').lower()
7520 int(listcatfiles
['ffilelist'][reallcfi
]['fctime']), 'x').lower()
7522 int(listcatfiles
['ffilelist'][reallcfi
]['fbtime']), 'x').lower()
7524 int(listcatfiles
['ffilelist'][reallcfi
]['fmode']), 'x').lower()
7526 int(listcatfiles
['ffilelist'][reallcfi
]['fchmode']), 'x').lower()
7528 int(listcatfiles
['ffilelist'][reallcfi
]['fuid']), 'x').lower()
7529 funame
= listcatfiles
['ffilelist'][reallcfi
]['funame']
7531 int(listcatfiles
['ffilelist'][reallcfi
]['fgid']), 'x').lower()
7532 fgname
= listcatfiles
['ffilelist'][reallcfi
]['fgname']
7534 int(listcatfiles
['ffilelist'][reallcfi
]['finode']), 'x').lower()
7535 flinkcount
= format(
7536 int(listcatfiles
['ffilelist'][reallcfi
]['flinkcount']), 'x').lower()
7537 fwinattributes
= format(
7538 int(listcatfiles
['ffilelist'][reallcfi
]['fwinattributes']), 'x').lower()
7539 fcompression
= listcatfiles
['ffilelist'][reallcfi
]['fcompression']
7541 int(listcatfiles
['ffilelist'][reallcfi
]['fcsize']), 'x').lower()
7542 fdev_minor
= format(
7543 int(listcatfiles
['ffilelist'][reallcfi
]['fminor']), 'x').lower()
7544 fdev_major
= format(
7545 int(listcatfiles
['ffilelist'][reallcfi
]['fmajor']), 'x').lower()
7546 frdev_minor
= format(
7547 int(listcatfiles
['ffilelist'][reallcfi
]['frminor']), 'x').lower()
7548 frdev_major
= format(
7549 int(listcatfiles
['ffilelist'][reallcfi
]['frmajor']), 'x').lower()
7550 fseeknextfile
= listcatfiles
['ffilelist'][reallcfi
]['fseeknextfile']
7551 if(len(listcatfiles
['ffilelist'][reallcfi
]['fextralist']) > listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] and len(listcatfiles
['ffilelist'][reallcfi
]['fextralist']) > 0):
7552 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(
7553 listcatfiles
['ffilelist'][reallcfi
]['fextralist'])
7554 if(not followlink
and len(extradata
) < 0):
7555 extradata
= listcatfiles
['ffilelist'][reallcfi
]['fextralist']
7556 fcontents
= listcatfiles
['ffilelist'][reallcfi
]['fcontents']
7557 if(not listcatfiles
['ffilelist'][reallcfi
]['fcontentasfile']):
7558 fcontents
= BytesIO(fcontents
)
7560 fcsize
= format(int(0), 'x').lower()
7561 if(not compresswholefile
):
7562 fcontents
.seek(0, 2)
7563 ucfsize
= fcontents
.tell()
7564 fcontents
.seek(0, 0)
7565 if(compression
== "auto"):
7566 ilsize
= len(compressionlistalt
)
7569 while(ilmin
< ilsize
):
7570 cfcontents
= BytesIO()
7571 shutil
.copyfileobj(fcontents
, cfcontents
)
7572 fcontents
.seek(0, 0)
7573 cfcontents
.seek(0, 0)
7574 cfcontents
= CompressArchiveFile(
7575 cfcontents
, compressionlistalt
[ilmin
], compressionlevel
, formatspecs
)
7577 cfcontents
.seek(0, 2)
7578 ilcsize
.append(cfcontents
.tell())
7582 ilcsize
.append(sys
.maxint
)
7583 except AttributeError:
7584 ilcsize
.append(sys
.maxsize
)
7586 ilcmin
= ilcsize
.index(min(ilcsize
))
7587 compression
= compressionlistalt
[ilcmin
]
7588 fcontents
.seek(0, 0)
7589 cfcontents
= BytesIO()
7590 shutil
.copyfileobj(fcontents
, cfcontents
)
7591 cfcontents
.seek(0, 0)
7592 cfcontents
= CompressArchiveFile(
7593 cfcontents
, compression
, compressionlevel
, formatspecs
)
7594 cfcontents
.seek(0, 2)
7595 cfsize
= cfcontents
.tell()
7596 if(ucfsize
> cfsize
):
7597 fcsize
= format(int(cfsize
), 'x').lower()
7598 fcompression
= compression
7600 fcontents
= cfcontents
7602 if(listcatfiles
['ffilelist'][reallcfi
]['ftype'] == 1 or listcatfiles
['ffilelist'][reallcfi
]['ftype'] == 2):
7603 getflinkpath
= listcatfiles
['ffilelist'][reallcfi
]['flinkname']
7604 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
]
7605 flinkinfo
= listcatfiles
['ffilelist'][flinkid
]
7606 fheadersize
= format(
7607 int(flinkinfo
['fheadersize']), 'x').lower()
7608 fsize
= format(int(flinkinfo
['fsize']), 'x').lower()
7609 flinkname
= flinkinfo
['flinkname']
7610 fatime
= format(int(flinkinfo
['fatime']), 'x').lower()
7611 fmtime
= format(int(flinkinfo
['fmtime']), 'x').lower()
7612 fctime
= format(int(flinkinfo
['fctime']), 'x').lower()
7613 fbtime
= format(int(flinkinfo
['fbtime']), 'x').lower()
7614 fmode
= format(int(flinkinfo
['fmode']), 'x').lower()
7615 fchmode
= format(int(flinkinfo
['fchmode']), 'x').lower()
7616 fuid
= format(int(flinkinfo
['fuid']), 'x').lower()
7617 funame
= flinkinfo
['funame']
7618 fgid
= format(int(flinkinfo
['fgid']), 'x').lower()
7619 fgname
= flinkinfo
['fgname']
7620 finode
= format(int(flinkinfo
['finode']), 'x').lower()
7621 flinkcount
= format(int(flinkinfo
['flinkcount']), 'x').lower()
7622 fwinattributes
= format(
7623 int(flinkinfo
['fwinattributes']), 'x').lower()
7624 fcompression
= flinkinfo
['fcompression']
7625 fcsize
= format(int(flinkinfo
['fcsize']), 'x').lower()
7626 fdev_minor
= format(int(flinkinfo
['fminor']), 'x').lower()
7627 fdev_major
= format(int(flinkinfo
['fmajor']), 'x').lower()
7628 frdev_minor
= format(int(flinkinfo
['frminor']), 'x').lower()
7629 frdev_major
= format(int(flinkinfo
['frmajor']), 'x').lower()
7630 fseeknextfile
= flinkinfo
['fseeknextfile']
7631 if(len(flinkinfo
['fextralist']) > flinkinfo
['fextrafields'] and len(flinkinfo
['fextralist']) > 0):
7632 flinkinfo
['fextrafields'] = len(flinkinfo
['fextralist'])
7633 if(len(extradata
) < 0):
7634 extradata
= flinkinfo
['fextralist']
7635 fcontents
= flinkinfo
['fcontents']
7636 if(not flinkinfo
['fcontentasfile']):
7637 fcontents
= BytesIO(fcontents
)
7638 ftypehex
= format(flinkinfo
['ftype'], 'x').lower()
7641 listcatfiles
['ffilelist'][reallcfi
]['ftype'], 'x').lower()
7642 fcurfid
= format(curfid
, 'x').lower()
7643 if(not followlink
and finode
!= 0):
7644 if(listcatfiles
['ffilelist'][reallcfi
]['ftype'] != 1):
7645 fcurinode
= format(int(curinode
), 'x').lower()
7646 inodetofile
.update({curinode
: fname
})
7647 filetoinode
.update({fname
: curinode
})
7648 curinode
= curinode
+ 1
7650 fcurinode
= format(int(filetoinode
[flinkname
]), 'x').lower()
7652 fcurinode
= format(int(curinode
), 'x').lower()
7653 curinode
= curinode
+ 1
7655 if(fcompression
== "none"):
7657 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fcompression
, fcsize
,
7658 fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, fseeknextfile
]
7659 catfp
= AppendFileHeaderWithContent(
7660 catfp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
)
7663 reallcfi
= reallcfi
+ 1
7665 catfp
.write(AppendNullBytes(
7666 [0, 0], formatspecs
['format_delimiter']).encode("UTF-8"))
7667 if(outfile
== "-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
7668 catfp
= CompressArchiveFile(
7669 catfp
, compression
, compressionlevel
, formatspecs
)
7672 os
.fsync(catfp
.fileno())
7673 except io
.UnsupportedOperation
:
7675 except AttributeError:
7677 except OSError as e
:
7681 if(hasattr(sys
.stdout
, "buffer")):
7682 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer)
7684 shutil
.copyfileobj(catfp
, sys
.stdout
)
7685 elif(re
.findall("^(ftp|ftps|sftp):\\/\\/", str(outfile
))):
7686 catfp
= CompressArchiveFile(
7687 catfp
, compression
, compressionlevel
, formatspecs
)
7689 upload_file_to_internet_file(catfp
, outfile
)
7698 create_alias_function("RePack", __file_format_name__
, "", RePackArchiveFile
)
7701 def RePackArchiveFileFromString(catstr
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
7702 formatspecs
= FormatSpecsListToDict(formatspecs
)
7703 catfp
= BytesIO(catstr
)
7704 listcatfiles
= RePackArchiveFile(catfp
, compression
, compresswholefile
, compressionlevel
,
7705 checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
)
7709 create_alias_function("RePack", __file_format_name__
,
7710 "FromString", RePackArchiveFileFromString
)
7713 def PackArchiveFileFromListDir(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, skipchecksum
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
7714 formatspecs
= FormatSpecsListToDict(formatspecs
)
7715 outarray
= BytesIO()
7716 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compresswholefile
,
7717 compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True)
7718 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compresswholefile
,
7719 compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
)
7723 create_alias_function("Pack", __file_format_name__
,
7724 "FromListDir", PackArchiveFileFromListDir
)
7727 def UnPackArchiveFile(infile
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_dict__
, preservepermissions
=True, preservetime
=True, verbose
=False, returnfp
=False):
7728 formatspecs
= FormatSpecsListToDict(formatspecs
)
7729 if(outdir
is not None):
7730 outdir
= RemoveWindowsPath(outdir
)
7732 logging
.basicConfig(format
="%(message)s",
7733 stream
=sys
.stdout
, level
=logging
.DEBUG
)
7734 if(isinstance(infile
, dict)):
7735 listcatfiles
= infile
7737 if(infile
!= "-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
7738 infile
= RemoveWindowsPath(infile
)
7739 listcatfiles
= ArchiveFileToArray(
7740 infile
, seekstart
, seekend
, False, True, skipchecksum
, formatspecs
, returnfp
)
7741 if(not listcatfiles
):
7743 lenlist
= len(listcatfiles
['ffilelist'])
7744 fnumfiles
= int(listcatfiles
['fnumfiles'])
7746 lcfx
= int(listcatfiles
['fnumfiles'])
7747 if(lenlist
> listcatfiles
['fnumfiles'] or lenlist
< listcatfiles
['fnumfiles']):
7750 lcfx
= int(listcatfiles
['fnumfiles'])
7756 userinfo
= pwd
.getpwuid(
7757 listcatfiles
['ffilelist'][lcfi
]['fuid'])
7758 funame
= userinfo
.pw_name
7767 groupinfo
= grp
.getgrgid(
7768 listcatfiles
['ffilelist'][lcfi
]['fgid'])
7769 fgname
= groupinfo
.gr_name
7775 VerbosePrintOut(PrependPath(
7776 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']))
7777 if(listcatfiles
['ffilelist'][lcfi
]['ftype'] == 0 or listcatfiles
['ffilelist'][lcfi
]['ftype'] == 7):
7778 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
7779 if(not listcatfiles
['ffilelist'][lcfi
]['fcontentasfile']):
7780 listcatfiles
['ffilelist'][lcfi
]['fcontents'] = BytesIO(
7781 listcatfiles
['ffilelist'][lcfi
]['fcontents'])
7782 listcatfiles
['ffilelist'][lcfi
]['fcontents'].seek(0, 0)
7784 listcatfiles
['ffilelist'][lcfi
]['fcontents'], fpc
)
7787 os
.fsync(fpc
.fileno())
7788 except io
.UnsupportedOperation
:
7790 except AttributeError:
7792 except OSError as e
:
7794 if(hasattr(os
, "chown") and funame
== listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
== listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
7795 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']),
7796 listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid'])
7797 if(preservepermissions
):
7798 os
.chmod(PrependPath(
7799 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode'])
7801 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (
7802 listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']))
7803 if(listcatfiles
['ffilelist'][lcfi
]['ftype'] == 1):
7805 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname']
7806 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
]
7807 flinkinfo
= listcatfiles
['ffilelist'][flinkid
]
7812 userinfo
= pwd
.getpwuid(flinkinfo
['fuid'])
7813 funame
= userinfo
.pw_name
7822 groupinfo
= grp
.getgrgid(flinkinfo
['fgid'])
7823 fgname
= groupinfo
.gr_name
7828 if(flinkinfo
['ftype'] == 0 or flinkinfo
['ftype'] == 7):
7829 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
7830 if(not flinkinfo
['fcontentasfile']):
7831 flinkinfo
['fcontents'] = BytesIO(
7832 flinkinfo
['fcontents'])
7833 flinkinfo
['fcontents'].seek(0, 0)
7834 shutil
.copyfileobj(flinkinfo
['fcontents'], fpc
)
7837 os
.fsync(fpc
.fileno())
7838 except io
.UnsupportedOperation
:
7840 except AttributeError:
7842 except OSError as e
:
7844 if(hasattr(os
, "chown") and funame
== flinkinfo
['funame'] and fgname
== flinkinfo
['fgname'] and preservepermissions
):
7845 os
.chown(PrependPath(
7846 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid'])
7847 if(preservepermissions
):
7848 os
.chmod(PrependPath(
7849 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode'])
7851 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (
7852 flinkinfo
['fatime'], flinkinfo
['fmtime']))
7853 if(flinkinfo
['ftype'] == 1):
7854 os
.link(flinkinfo
['flinkname'], PrependPath(
7855 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']))
7856 if(flinkinfo
['ftype'] == 2):
7857 os
.symlink(flinkinfo
['flinkname'], PrependPath(
7858 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']))
7859 if(flinkinfo
['ftype'] == 5):
7860 if(preservepermissions
):
7861 os
.mkdir(PrependPath(
7862 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode'])
7864 os
.mkdir(PrependPath(
7865 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']))
7866 if(hasattr(os
, "chown") and funame
== flinkinfo
['funame'] and fgname
== flinkinfo
['fgname'] and preservepermissions
):
7867 os
.chown(PrependPath(
7868 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid'])
7869 if(preservepermissions
):
7870 os
.chmod(PrependPath(
7871 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode'])
7873 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (
7874 flinkinfo
['fatime'], flinkinfo
['fmtime']))
7875 if(flinkinfo
['ftype'] == 6 and hasattr(os
, "mkfifo")):
7876 os
.mkfifo(PrependPath(
7877 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode'])
7879 os
.link(listcatfiles
['ffilelist'][lcfi
]['flinkname'], PrependPath(
7880 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']))
7881 if(listcatfiles
['ffilelist'][lcfi
]['ftype'] == 2):
7883 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname']
7884 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
]
7885 flinkinfo
= listcatfiles
['ffilelist'][flinkid
]
7890 userinfo
= pwd
.getpwuid(flinkinfo
['fuid'])
7891 funame
= userinfo
.pw_name
7900 groupinfo
= grp
.getgrgid(flinkinfo
['fgid'])
7901 fgname
= groupinfo
.gr_name
7906 if(flinkinfo
['ftype'] == 0 or flinkinfo
['ftype'] == 7):
7907 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
7908 if(not flinkinfo
['fcontentasfile']):
7909 flinkinfo
['fcontents'] = BytesIO(
7910 flinkinfo
['fcontents'])
7911 flinkinfo
['fcontents'].seek(0, 0)
7912 shutil
.copyfileobj(flinkinfo
['fcontents'], fpc
)
7915 os
.fsync(fpc
.fileno())
7916 except io
.UnsupportedOperation
:
7918 except AttributeError:
7920 except OSError as e
:
7922 if(hasattr(os
, "chown") and funame
== flinkinfo
['funame'] and fgname
== flinkinfo
['fgname'] and preservepermissions
):
7923 os
.chown(PrependPath(
7924 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid'])
7925 if(preservepermissions
):
7926 os
.chmod(PrependPath(
7927 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode'])
7929 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (
7930 flinkinfo
['fatime'], flinkinfo
['fmtime']))
7931 if(flinkinfo
['ftype'] == 1):
7932 os
.link(flinkinfo
['flinkname'], PrependPath(
7933 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']))
7934 if(flinkinfo
['ftype'] == 2):
7935 os
.symlink(flinkinfo
['flinkname'], PrependPath(
7936 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']))
7937 if(flinkinfo
['ftype'] == 5):
7938 if(preservepermissions
):
7939 os
.mkdir(PrependPath(
7940 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode'])
7942 os
.mkdir(PrependPath(
7943 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']))
7944 if(hasattr(os
, "chown") and funame
== flinkinfo
['funame'] and fgname
== flinkinfo
['fgname'] and preservepermissions
):
7945 os
.chown(PrependPath(
7946 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid'])
7947 if(preservepermissions
):
7948 os
.chmod(PrependPath(
7949 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode'])
7951 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (
7952 flinkinfo
['fatime'], flinkinfo
['fmtime']))
7953 if(flinkinfo
['ftype'] == 6 and hasattr(os
, "mkfifo")):
7954 os
.mkfifo(PrependPath(
7955 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode'])
7957 os
.symlink(listcatfiles
['ffilelist'][lcfi
]['flinkname'], PrependPath(
7958 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']))
7959 if(listcatfiles
['ffilelist'][lcfi
]['ftype'] == 5):
7960 if(preservepermissions
):
7961 os
.mkdir(PrependPath(
7962 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode'])
7964 os
.mkdir(PrependPath(
7965 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']))
7966 if(hasattr(os
, "chown") and funame
== listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
== listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
7967 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']),
7968 listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid'])
7969 if(preservepermissions
):
7970 os
.chmod(PrependPath(
7971 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode'])
7973 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (
7974 listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']))
7975 if(listcatfiles
['ffilelist'][lcfi
]['ftype'] == 6 and hasattr(os
, "mkfifo")):
7976 os
.mkfifo(PrependPath(
7977 outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode'])
7980 return listcatfiles
['ffilelist']['catfp']
7985 create_alias_function("UnPack", __file_format_name__
, "", UnPackArchiveFile
)
7987 if(hasattr(shutil
, "register_unpack_format")):
7988 def UnPackArchiveFileFunc(archive_name
, extract_dir
=None, **kwargs
):
7989 return UnPackArchiveFile(archive_name
, extract_dir
, False, 0, 0, False, __file_format_dict__
['format_delimiter'], False, False)
7990 create_alias_function("UnPack", __file_format_name__
,
7991 "Func", UnPackArchiveFileFunc
)
7994 def UnPackArchiveFileString(catstr
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
7995 formatspecs
= FormatSpecsListToDict(formatspecs
)
7996 catfp
= BytesIO(catstr
)
7997 listcatfiles
= UnPackArchiveFile(
7998 catfp
, outdir
, followlink
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
)
8002 create_alias_function("UnPack", __file_format_name__
,
8003 "String", UnPackArchiveFileString
)
8006 def ArchiveFileListFiles(infile
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
8007 formatspecs
= FormatSpecsListToDict(formatspecs
)
8008 logging
.basicConfig(format
="%(message)s",
8009 stream
=sys
.stdout
, level
=logging
.DEBUG
)
8010 if(isinstance(infile
, dict)):
8011 listcatfiles
= infile
8013 if(infile
!= "-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
8014 infile
= RemoveWindowsPath(infile
)
8015 listcatfiles
= ArchiveFileToArray(
8016 infile
, seekstart
, seekend
, True, False, skipchecksum
, formatspecs
, returnfp
)
8017 if(not listcatfiles
):
8019 lenlist
= len(listcatfiles
['ffilelist'])
8020 fnumfiles
= int(listcatfiles
['fnumfiles'])
8022 lcfx
= int(listcatfiles
['fnumfiles'])
8023 if(lenlist
> listcatfiles
['fnumfiles'] or lenlist
< listcatfiles
['fnumfiles']):
8026 lcfx
= int(listcatfiles
['fnumfiles'])
8029 returnval
.update({lcfi
: listcatfiles
['ffilelist'][lcfi
]['fname']})
8031 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname'])
8033 permissions
= {'access': {'0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': (
8034 'r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx')}, 'roles': {0: 'owner', 1: 'group', 2: 'other'}}
8035 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname']
8036 if(listcatfiles
['ffilelist'][lcfi
]['ftype'] == 1):
8037 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + \
8038 " link to " + listcatfiles
['ffilelist'][lcfi
]['flinkname']
8039 if(listcatfiles
['ffilelist'][lcfi
]['ftype'] == 2):
8040 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + \
8041 " -> " + listcatfiles
['ffilelist'][lcfi
]['flinkname']
8042 fuprint
= listcatfiles
['ffilelist'][lcfi
]['funame']
8043 if(len(fuprint
) <= 0):
8044 fuprint
= listcatfiles
['ffilelist'][lcfi
]['fuid']
8045 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgname']
8046 if(len(fgprint
) <= 0):
8047 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgid']
8048 VerbosePrintOut(PrintPermissionString(listcatfiles
['ffilelist'][lcfi
]['fmode'], listcatfiles
['ffilelist'][lcfi
]['ftype']) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(
8049 listcatfiles
['ffilelist'][lcfi
]['fsize']).rjust(15) + " " + datetime
.datetime
.utcfromtimestamp(listcatfiles
['ffilelist'][lcfi
]['fmtime']).strftime('%Y-%m-%d %H:%M') + " " + printfname
))
8052 return listcatfiles
['catfp']
8057 create_alias_function("", __file_format_name__
,
8058 "ListFiles", ArchiveFileListFiles
)
8061 def ArchiveFileStringListFiles(catstr
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
8062 formatspecs
= FormatSpecsListToDict(formatspecs
)
8063 catfp
= BytesIO(catstr
)
8064 listcatfiles
= ArchiveFileListFiles(
8065 catstr
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
)
8069 create_alias_function("", __file_format_name__
,
8070 "StringListFiles", ArchiveFileStringListFiles
)
8073 def TarFileListFiles(infile
, verbose
=False, returnfp
=False):
8074 logging
.basicConfig(format
="%(message)s",
8075 stream
=sys
.stdout
, level
=logging
.DEBUG
)
8078 if(hasattr(sys
.stdin
, "buffer")):
8079 shutil
.copyfileobj(sys
.stdin
.buffer, infile
)
8081 shutil
.copyfileobj(sys
.stdin
, infile
)
8086 elif(re
.findall("^(http|https|ftp|ftps|sftp):\\/\\/", str(infile
))):
8087 infile
= download_file_from_internet_file(infile
)
8092 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
8094 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
8096 if(not tarfile
.TarFileCheck(infile
)):
8098 except AttributeError:
8099 if(not TarFileCheck(infile
)):
8102 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
8103 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r")
8105 tarfp
= tarfile
.open(infile
, "r")
8106 except FileNotFoundError
:
8110 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
8111 returnval
.update({lcfi
: member
.name
})
8112 fpremode
= member
.mode
8113 ffullmode
= member
.mode
8117 ffullmode
= member
.mode
+ stat
.S_IFREG
8119 elif(member
.isdev()):
8120 ffullmode
= member
.mode
8122 elif(member
.islnk()):
8123 ffullmode
= member
.mode
+ stat
.S_IFREG
8125 elif(member
.issym()):
8126 ffullmode
= member
.mode
+ stat
.S_IFLNK
8128 elif(member
.ischr()):
8129 ffullmode
= member
.mode
+ stat
.S_IFCHR
8131 elif(member
.isblk()):
8132 ffullmode
= member
.mode
+ stat
.S_IFBLK
8134 elif(member
.isdir()):
8135 ffullmode
= member
.mode
+ stat
.S_IFDIR
8137 elif(member
.isfifo()):
8138 ffullmode
= member
.mode
+ stat
.S_IFIFO
8140 elif(member
.issparse()):
8141 ffullmode
= member
.mode
8144 VerbosePrintOut(member
.name
)
8146 permissions
= {'access': {'0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': (
8147 'r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx')}, 'roles': {0: 'owner', 1: 'group', 2: 'other'}}
8148 printfname
= member
.name
8150 printfname
= member
.name
+ " link to " + member
.linkname
8151 elif(member
.issym()):
8152 printfname
= member
.name
+ " -> " + member
.linkname
8153 fuprint
= member
.uname
8154 if(len(fuprint
) <= 0):
8155 fuprint
= member
.uid
8156 fgprint
= member
.gname
8157 if(len(fgprint
) <= 0):
8158 fgprint
= member
.gid
8159 VerbosePrintOut(PrintPermissionString(ffullmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(
8160 member
.size
).rjust(15) + " " + datetime
.datetime
.utcfromtimestamp(member
.mtime
).strftime('%Y-%m-%d %H:%M') + " " + printfname
))
8163 return listcatfiles
['catfp']
8168 def ZipFileListFiles(infile
, verbose
=False, returnfp
=False):
8169 logging
.basicConfig(format
="%(message)s",
8170 stream
=sys
.stdout
, level
=logging
.DEBUG
)
8173 if(hasattr(sys
.stdin
, "buffer")):
8174 shutil
.copyfileobj(sys
.stdin
.buffer, infile
)
8176 shutil
.copyfileobj(sys
.stdin
, infile
)
8181 elif(re
.findall("^(http|https|ftp|ftps|sftp):\\/\\/", str(infile
))):
8182 infile
= download_file_from_internet_file(infile
)
8187 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
8189 if(not zipfile
.is_zipfile(infile
)):
8192 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True)
8193 except FileNotFoundError
:
8198 ziptest
= zipfp
.testzip()
8200 VerbosePrintOut("Bad file found!")
8201 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
8202 if(zipinfo
.create_system
== 0 or zipinfo
.create_system
== 10):
8203 fwinattributes
= int(zipinfo
.external_attr
)
8204 if(not member
.is_dir()):
8205 fmode
= int(stat
.S_IFREG
+ 438)
8206 fchmode
= int(stat
.S_IMODE(fmode
))
8207 ftypemod
= int(stat
.S_IFMT(fmode
))
8208 elif(member
.is_dir()):
8209 fmode
= int(stat
.S_IFDIR
+ 511)
8210 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)))
8211 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)))
8212 elif(zipinfo
.create_system
== 3):
8213 fwinattributes
= int(0)
8215 fmode
= int(zipinfo
.external_attr
)
8216 fchmode
= stat
.S_IMODE(fmode
)
8217 ftypemod
= stat
.S_IFMT(fmode
)
8218 except OverflowError:
8219 fmode
= int(zipinfo
.external_attr
>> 16)
8220 fchmode
= stat
.S_IMODE(fmode
)
8221 ftypemod
= stat
.S_IFMT(fmode
)
8223 fwinattributes
= int(0)
8224 if(not member
.is_dir()):
8225 fmode
= int(stat
.S_IFREG
+ 438)
8226 fchmode
= int(stat
.S_IMODE(fmode
))
8227 ftypemod
= int(stat
.S_IFMT(fmode
))
8228 elif(member
.is_dir()):
8229 fmode
= int(stat
.S_IFDIR
+ 511)
8230 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)))
8231 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)))
8232 returnval
.update({lcfi
: member
.filename
})
8234 VerbosePrintOut(member
.filename
)
8236 permissions
= {'access': {'0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': (
8237 'r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx')}, 'roles': {0: 'owner', 1: 'group', 2: 'other'}}
8239 for fmodval
in str(oct(fmode
))[-3:]:
8240 permissionstr
= permissionstr
+ \
8241 permissions
['access'].get(fmodval
, '---')
8242 if(not member
.is_dir()):
8244 permissionstr
= "-" + permissionstr
8245 elif(member
.is_dir()):
8247 permissionstr
= "d" + permissionstr
8248 printfname
= member
.filename
8250 fuid
= int(os
.getuid())
8251 except AttributeError:
8256 fgid
= int(os
.getgid())
8257 except AttributeError:
8264 userinfo
= pwd
.getpwuid(os
.getuid())
8265 funame
= userinfo
.pw_name
8268 except AttributeError:
8276 groupinfo
= grp
.getgrgid(os
.getgid())
8277 fgname
= groupinfo
.gr_name
8280 except AttributeError:
8285 if(len(fuprint
) <= 0):
8288 if(len(fgprint
) <= 0):
8290 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(member
.file_size
).rjust(
8291 15) + " " + datetime
.datetime
.utcfromtimestamp(int(time
.mktime(member
.date_time
+ (0, 0, -1)))).strftime('%Y-%m-%d %H:%M') + " " + printfname
))
8294 return listcatfiles
['catfp']
8299 if(not rarfile_support
):
8300 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
8303 if(rarfile_support
):
8304 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
8305 logging
.basicConfig(format
="%(message)s",
8306 stream
=sys
.stdout
, level
=logging
.DEBUG
)
8307 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
8309 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
8313 rarfp
= rarfile
.RarFile(infile
, "r")
8314 rartest
= rarfp
.testrar()
8316 VerbosePrintOut("Bad file found!")
8317 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
8320 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
8323 member
.external_attr
8325 except AttributeError:
8327 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
8330 member
.external_attr
8332 except AttributeError:
8337 if(is_unix
and member
.external_attr
!= 0):
8338 fpremode
= int(member
.external_attr
)
8339 elif(member
.is_file()):
8340 fpremode
= int(stat
.S_IFREG
+ 438)
8341 elif(member
.is_symlink()):
8342 fpremode
= int(stat
.S_IFLNK
+ 438)
8343 elif(member
.is_dir()):
8344 fpremode
= int(stat
.S_IFDIR
+ 511)
8345 if(is_windows
and member
.external_attr
!= 0):
8346 fwinattributes
= int(member
.external_attr
)
8348 fwinattributes
= int(0)
8349 if(is_unix
and member
.external_attr
!= 0):
8350 fmode
= int(member
.external_attr
)
8351 fchmode
= int(stat
.S_IMODE(member
.external_attr
))
8352 ftypemod
= int(stat
.S_IFMT(member
.external_attr
))
8353 elif(member
.is_file()):
8354 fmode
= int(stat
.S_IFREG
+ 438)
8355 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)))
8356 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)))
8357 elif(member
.is_symlink()):
8358 fmode
= int(stat
.S_IFLNK
+ 438)
8359 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFLNK
+ 438)))
8360 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFLNK
+ 438)))
8361 elif(member
.is_dir()):
8362 fmode
= int(stat
.S_IFDIR
+ 511)
8363 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)))
8364 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)))
8365 returnval
.update({lcfi
: member
.filename
})
8367 VerbosePrintOut(member
.filename
)
8369 permissions
= {'access': {'0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': (
8370 'r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx')}, 'roles': {0: 'owner', 1: 'group', 2: 'other'}}
8372 for fmodval
in str(oct(fmode
))[-3:]:
8373 permissionstr
= permissionstr
+ \
8374 permissions
['access'].get(fmodval
, '---')
8375 if(member
.is_file()):
8377 permissionstr
= "-" + permissionstr
8378 printfname
= member
.filename
8379 elif(member
.is_symlink()):
8381 permissionstr
= "l" + permissionstr
8382 printfname
= member
.name
+ " -> " + member
.read().decode("UTF-8")
8383 elif(member
.is_dir()):
8385 permissionstr
= "d" + permissionstr
8386 printfname
= member
.filename
8388 fuid
= int(os
.getuid())
8389 except AttributeError:
8394 fgid
= int(os
.getgid())
8395 except AttributeError:
8402 userinfo
= pwd
.getpwuid(os
.getuid())
8403 funame
= userinfo
.pw_name
8406 except AttributeError:
8414 groupinfo
= grp
.getgrgid(os
.getgid())
8415 fgname
= groupinfo
.gr_name
8418 except AttributeError:
8423 if(len(fuprint
) <= 0):
8426 if(len(fgprint
) <= 0):
8428 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(
8429 member
.file_size
).rjust(15) + " " + member
.mtime
.strftime('%Y-%m-%d %H:%M') + " " + printfname
))
8432 return listcatfiles
['catfp']
8436 if(not py7zr_support
):
8437 def SevenZipFileListFiles(infile
, verbose
=False, returnfp
=False):
8441 def SevenZipFileListFiles(infile
, verbose
=False, returnfp
=False):
8442 logging
.basicConfig(format
="%(message)s",
8443 stream
=sys
.stdout
, level
=logging
.DEBUG
)
8444 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
8448 szpfp
= py7zr
.SevenZipFile(infile
, mode
="r")
8449 file_content
= szpfp
.readall()
8450 #sztest = szpfp.testzip();
8451 sztestalt
= szpfp
.test()
8453 VerbosePrintOut("Bad file found!")
8454 for member
in sorted(szpfp
.list(), key
=lambda x
: x
.filename
):
8455 if(re
.findall("^[.|/]", member
.filename
)):
8456 fname
= member
.filename
8458 fname
= "./"+member
.filename
8459 if(not member
.is_directory
):
8460 fpremode
= int(stat
.S_IFREG
+ 438)
8461 elif(member
.is_directory
):
8462 fpremode
= int(stat
.S_IFDIR
+ 511)
8463 fwinattributes
= int(0)
8464 if(member
.is_directory
):
8465 fmode
= int(stat
.S_IFDIR
+ 511)
8466 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)))
8467 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)))
8469 fmode
= int(stat
.S_IFLNK
+ 438)
8470 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFLNK
+ 438)))
8471 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFLNK
+ 438)))
8472 returnval
.update({lcfi
: member
.filename
})
8474 VerbosePrintOut(member
.filename
)
8476 permissions
= {'access': {'0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': (
8477 'r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx')}, 'roles': {0: 'owner', 1: 'group', 2: 'other'}}
8479 for fmodval
in str(oct(fmode
))[-3:]:
8480 permissionstr
= permissionstr
+ \
8481 permissions
['access'].get(fmodval
, '---')
8483 if(not member
.is_directory
):
8485 permissionstr
= "-" + permissionstr
8486 printfname
= member
.filename
8487 elif(member
.is_directory
):
8489 permissionstr
= "d" + permissionstr
8490 printfname
= member
.filename
8492 fsize
= len(file_content
[member
.filename
].read())
8493 file_content
[member
.filename
].close()
8495 fuid
= int(os
.getuid())
8496 except AttributeError:
8501 fgid
= int(os
.getgid())
8502 except AttributeError:
8509 userinfo
= pwd
.getpwuid(os
.getuid())
8510 funame
= userinfo
.pw_name
8513 except AttributeError:
8521 groupinfo
= grp
.getgrgid(os
.getgid())
8522 fgname
= groupinfo
.gr_name
8525 except AttributeError:
8530 if(len(fuprint
) <= 0):
8533 if(len(fgprint
) <= 0):
8535 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(
8536 fsize
).rjust(15) + " " + member
.creationtime
.strftime('%Y-%m-%d %H:%M') + " " + printfname
))
8539 return listcatfiles
['catfp']
8544 def InFileListFiles(infile
, verbose
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
8545 formatspecs
= FormatSpecsListToDict(formatspecs
)
8546 logging
.basicConfig(format
="%(message)s",
8547 stream
=sys
.stdout
, level
=logging
.DEBUG
)
8548 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True)
8549 if(checkcompressfile
== "tarfile" and TarFileCheck(infile
)):
8550 return TarFileListFiles(infile
, verbose
, returnfp
)
8551 elif(checkcompressfile
== "zipfile" and zipfile
.is_zipfile(infile
)):
8552 return ZipFileListFiles(infile
, verbose
, returnfp
)
8553 elif(rarfile_support
and checkcompressfile
== "rarfile" and (rarfile
.is_rarfile(infile
) or rarfile
.is_rarfile_sfx(infile
))):
8554 return RarFileListFiles(infile
, verbose
, returnfp
)
8555 elif(py7zr_support
and checkcompressfile
== "7zipfile" and py7zr
.is_7zfile(infile
)):
8556 return SevenZipFileListFiles(infile
, verbose
, returnfp
)
8557 elif(checkcompressfile
== "catfile"):
8558 return ArchiveFileListFiles(infile
, 0, 0, False, formatspecs
, verbose
, returnfp
)
8564 def ListDirListFiles(infiles
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, checksumtype
="crc32", formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
8565 formatspecs
= FormatSpecsListToDict(formatspecs
)
8566 outarray
= BytesIO()
8567 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compresswholefile
,
8568 compressionlevel
, followlink
, checksumtype
, formatspecs
, False, True)
8569 listcatfiles
= ArchiveFileListFiles(
8570 outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
)
8574 def ListDirListFilesAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=False, contentasfile
=True, seekstart
=0, seekend
=0, skipchecksum
=False, checksumtype
="crc32", formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
8575 formatspecs
= FormatSpecsListToDict(formatspecs
)
8576 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
,
8577 listonly
, contentasfile
, checksumtype
, formatspecs
, verbose
)
8578 listcatfiles
= ArchiveFileListFiles(
8579 outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
)
8583 def PackArchiveFileFromListDirAlt(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, skipchecksum
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
8584 formatspecs
= FormatSpecsListToDict(formatspecs
)
8585 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
,
8586 False, True, checksumtype
, extradata
, formatspecs
, False)
8587 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compresswholefile
, compressionlevel
,
8588 followlink
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
)
8592 create_alias_function("Pack", __file_format_name__
,
8593 "FromListDirAlt", PackArchiveFileFromListDirAlt
)
8596 def PackArchiveFileFromTarFileAlt(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
8597 formatspecs
= FormatSpecsListToDict(formatspecs
)
8598 outarray
= TarFileToArrayAlt(
8599 infile
, False, True, checksumtype
, extradata
, formatspecs
, False)
8600 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compresswholefile
,
8601 compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
)
8605 create_alias_function("Pack", __file_format_name__
,
8606 "FromTarFileAlt", PackArchiveFileFromTarFileAlt
)
8609 def PackArchiveFileFromZipFileAlt(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
8610 formatspecs
= FormatSpecsListToDict(formatspecs
)
8611 outarray
= ZipFileToArrayAlt(
8612 infile
, False, True, checksumtype
, extradata
, formatspecs
, False)
8613 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compresswholefile
,
8614 compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
)
8618 create_alias_function("Pack", __file_format_name__
,
8619 "FromZipFileAlt", PackArchiveFileFromZipFileAlt
)
8621 if(not rarfile_support
):
8622 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
8625 if(rarfile_support
):
8626 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
8627 formatspecs
= FormatSpecsListToDict(formatspecs
)
8628 outarray
= RarFileToArrayAlt(
8629 infile
, False, True, checksumtype
, extradata
, formatspecs
, False)
8630 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compresswholefile
,
8631 compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
)
8634 create_alias_function("Pack", __file_format_name__
,
8635 "FromRarFileAlt", PackArchiveFileFromRarFileAlt
)
8637 if(not py7zr_support
):
8638 def PackArchiveFileFromSevenZipFileAlt(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
8642 def PackArchiveFileFromSevenZipFileAlt(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
8643 formatspecs
= FormatSpecsListToDict(formatspecs
)
8644 outarray
= SevenZipFileToArrayAlt(
8645 infile
, False, True, checksumtype
, extradata
, formatspecs
, False)
8646 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compresswholefile
,
8647 compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
)
8650 create_alias_function("Pack", __file_format_name__
,
8651 "FromSevenZipFileAlt", PackArchiveFileFromSevenZipFileAlt
)
8654 def download_file_from_ftp_file(url
):
8655 urlparts
= urlparse(url
)
8656 file_name
= os
.path
.basename(urlparts
.path
)
8657 file_dir
= os
.path
.dirname(urlparts
.path
)
8658 if(urlparts
.username
is not None):
8659 ftp_username
= urlparts
.username
8661 ftp_username
= "anonymous"
8662 if(urlparts
.password
is not None):
8663 ftp_password
= urlparts
.password
8664 elif(urlparts
.password
is None and urlparts
.username
== "anonymous"):
8665 ftp_password
= "anonymous"
8668 if(urlparts
.scheme
== "ftp"):
8670 elif(urlparts
.scheme
== "ftps" and ftpssl
):
8674 if(urlparts
.scheme
== "sftp"):
8676 return download_file_from_pysftp_file(url
)
8678 return download_file_from_sftp_file(url
)
8679 elif(urlparts
.scheme
== "http" or urlparts
.scheme
== "https"):
8680 return download_file_from_http_file(url
)
8681 ftp_port
= urlparts
.port
8682 if(urlparts
.port
is None):
8685 ftp
.connect(urlparts
.hostname
, ftp_port
)
8686 except socket
.gaierror
:
8687 log
.info("Error With URL "+url
)
8689 except socket
.timeout
:
8690 log
.info("Error With URL "+url
)
8692 ftp
.login(urlparts
.username
, urlparts
.password
)
8693 if(urlparts
.scheme
== "ftps"):
8696 ftp
.retrbinary("RETR "+urlparts
.path
, ftpfile
.write
)
8697 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
8703 def download_file_from_ftp_string(url
):
8704 ftpfile
= download_file_from_ftp_file(url
)
8705 return ftpfile
.read()
8708 def upload_file_to_ftp_file(ftpfile
, url
):
8709 urlparts
= urlparse(url
)
8710 file_name
= os
.path
.basename(urlparts
.path
)
8711 file_dir
= os
.path
.dirname(urlparts
.path
)
8712 if(urlparts
.username
is not None):
8713 ftp_username
= urlparts
.username
8715 ftp_username
= "anonymous"
8716 if(urlparts
.password
is not None):
8717 ftp_password
= urlparts
.password
8718 elif(urlparts
.password
is None and urlparts
.username
== "anonymous"):
8719 ftp_password
= "anonymous"
8722 if(urlparts
.scheme
== "ftp"):
8724 elif(urlparts
.scheme
== "ftps" and ftpssl
):
8728 if(urlparts
.scheme
== "sftp"):
8730 return upload_file_to_pysftp_file(url
)
8732 return upload_file_to_sftp_file(url
)
8733 elif(urlparts
.scheme
== "http" or urlparts
.scheme
== "https"):
8735 ftp_port
= urlparts
.port
8736 if(urlparts
.port
is None):
8739 ftp
.connect(urlparts
.hostname
, ftp_port
)
8740 except socket
.gaierror
:
8741 log
.info("Error With URL "+url
)
8743 except socket
.timeout
:
8744 log
.info("Error With URL "+url
)
8746 ftp
.login(urlparts
.username
, urlparts
.password
)
8747 if(urlparts
.scheme
== "ftps"):
8749 ftp
.storbinary("STOR "+urlparts
.path
, ftpfile
)
8755 def upload_file_to_ftp_string(ftpstring
, url
):
8756 ftpfileo
= BytesIO(ftpstring
)
8757 ftpfile
= upload_file_to_ftp_file(ftpfileo
, url
)
8762 class RawIteratorWrapper
:
8763 def __init__(self
, iterator
):
8764 self
.iterator
= iterator
8766 self
._iterator
_exhausted
= False
8768 def read(self
, size
=-1):
8769 if self
._iterator
_exhausted
:
8771 while size
< 0 or len(self
.buffer) < size
:
8773 chunk
= next(self
.iterator
)
8774 self
.buffer += chunk
8775 except StopIteration:
8776 self
._iterator
_exhausted
= True
8779 size
= len(self
.buffer)
8780 result
, self
.buffer = self
.buffer[:size
], self
.buffer[size
:]
8784 def download_file_from_http_file(url
, headers
=None, usehttp
=__use_http_lib__
):
8787 # Parse the URL to extract username and password if present
8788 urlparts
= urlparse(url
)
8789 username
= urlparts
.username
8790 password
= urlparts
.password
8791 # Rebuild the URL without the username and password
8792 netloc
= urlparts
.hostname
8793 if urlparts
.scheme
== "sftp":
8795 return download_file_from_pysftp_file(url
)
8797 return download_file_from_sftp_file(url
)
8798 elif urlparts
.scheme
== "ftp" or urlparts
.scheme
== "ftps":
8799 return download_file_from_ftp_file(url
)
8801 netloc
+= ':' + str(urlparts
.port
)
8802 rebuilt_url
= urlunparse((urlparts
.scheme
, netloc
, urlparts
.path
,
8803 urlparts
.params
, urlparts
.query
, urlparts
.fragment
))
8804 # Create a temporary file object
8805 httpfile
= BytesIO()
8806 if usehttp
== 'requests' and haverequests
:
8807 # Use the requests library if selected and available
8808 if username
and password
:
8809 response
= requests
.get(rebuilt_url
, headers
=headers
, auth
=(
8810 username
, password
), stream
=True)
8812 response
= requests
.get(rebuilt_url
, headers
=headers
, stream
=True)
8813 response
.raw
.decode_content
= True
8814 shutil
.copyfileobj(response
.raw
, httpfile
)
8815 elif usehttp
== 'httpx' and havehttpx
:
8816 # Use httpx if selected and available
8817 with httpx
.Client(follow_redirects
=True) as client
:
8818 if username
and password
:
8819 response
= client
.get(
8820 rebuilt_url
, headers
=headers
, auth
=(username
, password
))
8822 response
= client
.get(rebuilt_url
, headers
=headers
)
8823 raw_wrapper
= RawIteratorWrapper(response
.iter_bytes())
8824 shutil
.copyfileobj(raw_wrapper
, httpfile
)
8826 # Use urllib as a fallback
8827 # Build a Request object for urllib
8828 request
= Request(rebuilt_url
, headers
=headers
)
8829 # Create an opener object for handling URLs
8830 if username
and password
:
8831 # Create a password manager
8832 password_mgr
= HTTPPasswordMgrWithDefaultRealm()
8833 # Add the username and password
8834 password_mgr
.add_password(None, rebuilt_url
, username
, password
)
8835 # Create an authentication handler using the password manager
8836 auth_handler
= HTTPBasicAuthHandler(password_mgr
)
8837 # Build the opener with the authentication handler
8838 opener
= build_opener(auth_handler
)
8840 opener
= build_opener()
8841 response
= opener
.open(request
)
8842 shutil
.copyfileobj(response
, httpfile
)
8843 # Reset file pointer to the start
8845 # Return the temporary file object
8849 def download_file_from_http_string(url
, headers
=geturls_headers_pycatfile_python_alt
, usehttp
=__use_http_lib__
):
8850 httpfile
= download_file_from_http_file(url
, headers
, usehttp
)
8851 return httpfile
.read()
8855 def download_file_from_sftp_file(url
):
8856 urlparts
= urlparse(url
)
8857 file_name
= os
.path
.basename(urlparts
.path
)
8858 file_dir
= os
.path
.dirname(urlparts
.path
)
8859 sftp_port
= urlparts
.port
8860 if(urlparts
.port
is None):
8863 sftp_port
= urlparts
.port
8864 if(urlparts
.username
is not None):
8865 sftp_username
= urlparts
.username
8867 sftp_username
= "anonymous"
8868 if(urlparts
.password
is not None):
8869 sftp_password
= urlparts
.password
8870 elif(urlparts
.password
is None and urlparts
.username
== "anonymous"):
8871 sftp_password
= "anonymous"
8874 if(urlparts
.scheme
== "ftp"):
8875 return download_file_from_ftp_file(url
)
8876 elif(urlparts
.scheme
== "http" or urlparts
.scheme
== "https"):
8877 return download_file_from_http_file(url
)
8878 if(urlparts
.scheme
!= "sftp"):
8880 ssh
= paramiko
.SSHClient()
8881 ssh
.load_system_host_keys()
8882 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy())
8884 ssh
.connect(urlparts
.hostname
, port
=sftp_port
,
8885 username
=urlparts
.username
, password
=urlparts
.password
)
8886 except paramiko
.ssh_exception
.SSHException
:
8888 except socket
.gaierror
:
8889 log
.info("Error With URL "+url
)
8891 except socket
.timeout
:
8892 log
.info("Error With URL "+url
)
8894 sftp
= ssh
.open_sftp()
8895 sftpfile
= BytesIO()
8896 sftp
.getfo(urlparts
.path
, sftpfile
)
8902 def download_file_from_sftp_file(url
):
8906 def download_file_from_sftp_string(url
):
8907 sftpfile
= download_file_from_sftp_file(url
)
8908 return sftpfile
.read()
8910 def download_file_from_sftp_string(url
):
8914 def upload_file_to_sftp_file(sftpfile
, url
):
8915 urlparts
= urlparse(url
)
8916 file_name
= os
.path
.basename(urlparts
.path
)
8917 file_dir
= os
.path
.dirname(urlparts
.path
)
8918 sftp_port
= urlparts
.port
8919 if(urlparts
.port
is None):
8922 sftp_port
= urlparts
.port
8923 if(urlparts
.username
is not None):
8924 sftp_username
= urlparts
.username
8926 sftp_username
= "anonymous"
8927 if(urlparts
.password
is not None):
8928 sftp_password
= urlparts
.password
8929 elif(urlparts
.password
is None and urlparts
.username
== "anonymous"):
8930 sftp_password
= "anonymous"
8933 if(urlparts
.scheme
== "ftp"):
8934 return upload_file_to_ftp_file(url
)
8935 elif(urlparts
.scheme
== "http" or urlparts
.scheme
== "https"):
8937 if(urlparts
.scheme
!= "sftp"):
8939 ssh
= paramiko
.SSHClient()
8940 ssh
.load_system_host_keys()
8941 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy())
8943 ssh
.connect(urlparts
.hostname
, port
=sftp_port
,
8944 username
=urlparts
.username
, password
=urlparts
.password
)
8945 except paramiko
.ssh_exception
.SSHException
:
8947 except socket
.gaierror
:
8948 log
.info("Error With URL "+url
)
8950 except socket
.timeout
:
8951 log
.info("Error With URL "+url
)
8953 sftp
= ssh
.open_sftp()
8954 sftp
.putfo(sftpfile
, urlparts
.path
)
8960 def upload_file_to_sftp_file(sftpfile
, url
):
8964 def upload_file_to_sftp_string(sftpstring
, url
):
8965 sftpfileo
= BytesIO(sftpstring
)
8966 sftpfile
= upload_file_to_sftp_files(ftpfileo
, url
)
8970 def upload_file_to_sftp_string(url
):
8974 def download_file_from_pysftp_file(url
):
8975 urlparts
= urlparse(url
)
8976 file_name
= os
.path
.basename(urlparts
.path
)
8977 file_dir
= os
.path
.dirname(urlparts
.path
)
8978 sftp_port
= urlparts
.port
8979 if(urlparts
.port
is None):
8982 sftp_port
= urlparts
.port
8983 if(urlparts
.username
is not None):
8984 sftp_username
= urlparts
.username
8986 sftp_username
= "anonymous"
8987 if(urlparts
.password
is not None):
8988 sftp_password
= urlparts
.password
8989 elif(urlparts
.password
is None and urlparts
.username
== "anonymous"):
8990 sftp_password
= "anonymous"
8993 if(urlparts
.scheme
== "ftp"):
8994 return download_file_from_ftp_file(url
)
8995 elif(urlparts
.scheme
== "http" or urlparts
.scheme
== "https"):
8996 return download_file_from_http_file(url
)
8997 if(urlparts
.scheme
!= "sftp"):
9000 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
,
9001 username
=urlparts
.username
, password
=urlparts
.password
)
9002 except paramiko
.ssh_exception
.SSHException
:
9004 except socket
.gaierror
:
9005 log
.info("Error With URL "+url
)
9007 except socket
.timeout
:
9008 log
.info("Error With URL "+url
)
9010 sftp
= ssh
.open_sftp()
9011 sftpfile
= BytesIO()
9012 sftp
.getfo(urlparts
.path
, sftpfile
)
9018 def download_file_from_pysftp_file(url
):
9022 def download_file_from_pysftp_string(url
):
9023 sftpfile
= download_file_from_pysftp_file(url
)
9024 return sftpfile
.read()
9026 def download_file_from_pyftp_string(url
):
9030 def upload_file_to_pysftp_file(sftpfile
, url
):
9031 urlparts
= urlparse(url
)
9032 file_name
= os
.path
.basename(urlparts
.path
)
9033 file_dir
= os
.path
.dirname(urlparts
.path
)
9034 sftp_port
= urlparts
.port
9035 if(urlparts
.port
is None):
9038 sftp_port
= urlparts
.port
9039 if(urlparts
.username
is not None):
9040 sftp_username
= urlparts
.username
9042 sftp_username
= "anonymous"
9043 if(urlparts
.password
is not None):
9044 sftp_password
= urlparts
.password
9045 elif(urlparts
.password
is None and urlparts
.username
== "anonymous"):
9046 sftp_password
= "anonymous"
9049 if(urlparts
.scheme
== "ftp"):
9050 return upload_file_to_ftp_file(url
)
9051 elif(urlparts
.scheme
== "http" or urlparts
.scheme
== "https"):
9053 if(urlparts
.scheme
!= "sftp"):
9056 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
,
9057 username
=urlparts
.username
, password
=urlparts
.password
)
9058 except paramiko
.ssh_exception
.SSHException
:
9060 except socket
.gaierror
:
9061 log
.info("Error With URL "+url
)
9063 except socket
.timeout
:
9064 log
.info("Error With URL "+url
)
9066 sftp
= ssh
.open_sftp()
9067 sftp
.putfo(sftpfile
, urlparts
.path
)
9073 def upload_file_to_pysftp_file(sftpfile
, url
):
9077 def upload_file_to_pysftp_string(sftpstring
, url
):
9078 sftpfileo
= BytesIO(sftpstring
)
9079 sftpfile
= upload_file_to_pysftp_files(ftpfileo
, url
)
9083 def upload_file_to_pysftp_string(url
):
9087 def download_file_from_internet_file(url
, headers
=geturls_headers_pycatfile_python_alt
, usehttp
=__use_http_lib__
):
9088 urlparts
= urlparse(url
)
9089 if(urlparts
.scheme
== "http" or urlparts
.scheme
== "https"):
9090 return download_file_from_http_file(url
, headers
, usehttp
)
9091 elif(urlparts
.scheme
== "ftp" or urlparts
.scheme
== "ftps"):
9092 return download_file_from_ftp_file(url
)
9093 elif(urlparts
.scheme
== "sftp"):
9094 if(__use_pysftp__
and havepysftp
):
9095 return download_file_from_pysftp_file(url
)
9097 return download_file_from_sftp_file(url
)
9103 def download_file_from_internet_uncompress_file(url
, headers
=geturls_headers_pycatfile_python_alt
, formatspecs
=__file_format_dict__
):
9104 formatspecs
= FormatSpecsListToDict(formatspecs
)
9105 fp
= download_file_from_internet_file(url
)
9106 fp
= UncompressArchiveFile(fp
, formatspecs
)
9113 def download_file_from_internet_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
9114 urlparts
= urlparse(url
)
9115 if(urlparts
.scheme
== "http" or urlparts
.scheme
== "https"):
9116 return download_file_from_http_string(url
, headers
)
9117 elif(urlparts
.scheme
== "ftp" or urlparts
.scheme
== "ftps"):
9118 return download_file_from_ftp_string(url
)
9119 elif(urlparts
.scheme
== "sftp"):
9120 if(__use_pysftp__
and havepysftp
):
9121 return download_file_from_pysftp_string(url
)
9123 return download_file_from_sftp_string(url
)
9129 def download_file_from_internet_uncompress_string(url
, headers
=geturls_headers_pycatfile_python_alt
, formatspecs
=__file_format_dict__
):
9130 formatspecs
= FormatSpecsListToDict(formatspecs
)
9131 fp
= download_file_from_internet_string(url
)
9132 fp
= UncompressArchiveFile(fp
, formatspecs
)
9139 def upload_file_to_internet_file(ifp
, url
):
9140 urlparts
= urlparse(url
)
9141 if(urlparts
.scheme
== "http" or urlparts
.scheme
== "https"):
9143 elif(urlparts
.scheme
== "ftp" or urlparts
.scheme
== "ftps"):
9144 return upload_file_to_ftp_file(ifp
, url
)
9145 elif(urlparts
.scheme
== "sftp"):
9146 if(__use_pysftp__
and havepysftp
):
9147 return upload_file_to_pysftp_file(ifp
, url
)
9149 return upload_file_to_sftp_file(ifp
, url
)
9155 def upload_file_to_internet_compress_file(ifp
, url
, compression
="auto", compressionlevel
=None, formatspecs
=__file_format_dict__
):
9156 formatspecs
= FormatSpecsListToDict(formatspecs
)
9157 catfp
= CompressArchiveFile(
9158 catfp
, compression
, compressionlevel
, formatspecs
)
9162 upload_file_to_internet_file(catfp
, outfile
)
9166 def upload_file_to_internet_string(ifp
, url
):
9167 urlparts
= urlparse(url
)
9168 if(urlparts
.scheme
== "http" or urlparts
.scheme
== "https"):
9170 elif(urlparts
.scheme
== "ftp" or urlparts
.scheme
== "ftps"):
9171 return upload_file_to_ftp_string(ifp
, url
)
9172 elif(urlparts
.scheme
== "sftp"):
9173 if(__use_pysftp__
and havepysftp
):
9174 return upload_file_to_pysftp_string(ifp
, url
)
9176 return upload_file_to_sftp_string(ifp
, url
)
9182 def upload_file_to_internet_compress_string(ifp
, url
, compression
="auto", compressionlevel
=None, formatspecs
=__file_format_dict__
):
9183 formatspecs
= FormatSpecsListToDict(formatspecs
)
9184 catfp
= CompressArchiveFile(
9185 BytesIO(ifp
), compression
, compressionlevel
, formatspecs
)
9189 upload_file_to_internet_file(catfp
, outfile
)
9194 if(hasattr(shutil
, "register_archive_format")):
9195 # Register the packing format
9196 shutil
.register_archive_format(
9197 __file_format_name__
, PackArchiveFileFunc
, description
='Pack concatenated files')
9198 except shutil
.RegistryError
:
9202 if(hasattr(shutil
, "register_unpack_format")):
9203 # Register the unpacking format
9204 shutil
.register_unpack_format(__file_format_name__
, archivefile_extensions
,
9205 UnPackArchiveFileFunc
, description
='UnPack concatenated files')
9206 except shutil
.RegistryError
: