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: 3/29/2024 Ver. 0.7.0 RC 1 - Author: cooldude2k $
20 from __future__
import absolute_import
, division
, print_function
, unicode_literals
;
21 import io
, os
, re
, sys
, time
, stat
, zlib
, base64
, shutil
, socket
, hashlib
, datetime
, logging
, binascii
, tempfile
, zipfile
, platform
;
22 from ftplib
import FTP
, FTP_TLS
;
23 if(sys
.version
[0]=="2"):
24 from urlparse
import urlparse
, urlunparse
;
25 elif(sys
.version
[0]>="3"):
26 from urllib
.parse
import urlunparse
;
27 from urllib
.parse
import urlparse
;
29 if os
.name
== 'nt': # Only modify if on Windows
30 if sys
.version
[0] == "2":
32 sys
.stdout
= codecs
.getwriter('utf-8')(sys
.stdout
);
33 sys
.stderr
= codecs
.getwriter('utf-8')(sys
.stderr
);
36 sys
.stdout
= io
.TextIOWrapper(sys
.stdout
.buffer, encoding
='utf-8', errors
='replace', line_buffering
=True);
37 sys
.stderr
= io
.TextIOWrapper(sys
.stderr
.buffer, encoding
='utf-8', errors
='replace', line_buffering
=True);
39 hashlib_guaranteed
= False;
40 os
.environ
["PYTHONIOENCODING"] = "UTF-8";
41 os
.environ
["LC_CTYPE"] = "UTF-8";
45 sys
.setdefaultencoding('UTF-8');
48 except AttributeError:
52 except AttributeError:
56 from zlib
import crc32
;
58 from binascii
import crc32
;
60 rarfile_support
= False;
63 rarfile_support
= True;
65 rarfile_support
= False;
68 from safetar
import is_tarfile
;
71 from xtarfile
import is_tarfile
;
73 from tarfile
import is_tarfile
;
76 import safetar
as tarfile
;
79 import xtarfile
as tarfile
;
102 haverequests
= False;
106 logging
.getLogger("urllib3").setLevel(logging
.WARNING
);
110 from urllib
.request
import Request
, build_opener
, HTTPBasicAuthHandler
;
111 from urllib
.parse
import urlparse
;
114 from urllib2
import Request
, build_opener
, HTTPBasicAuthHandler
;
115 from urlparse
import urlparse
;
117 if(sys
.version
[0]=="2"):
119 from io
import StringIO
, BytesIO
;
122 from cStringIO
import StringIO
;
123 from cStringIO
import StringIO
as BytesIO
;
125 from StringIO
import StringIO
;
126 from StringIO
import StringIO
as BytesIO
;
127 elif(sys
.version
[0]>="3"):
128 from io
import StringIO
, BytesIO
;
133 from cStringIO
import StringIO
as BytesIO
;
139 from StringIO
import StringIO
as BytesIO
;
145 from io
import BytesIO
;
150 __use_pysftp__
= False;
152 __use_pysftp__
= False;
153 __file_format_name__
= "CatFile";
154 __program_name__
= "Py"+__file_format_name__
;
155 __file_format_lower__
= __file_format_name__
.lower();
156 __file_format_magic__
= __file_format_name__
;
157 __file_format_len__
= len(__file_format_magic__
);
158 __file_format_hex__
= binascii
.hexlify(__file_format_magic__
.encode("UTF-8")).decode("UTF-8");
159 __file_format_delimiter__
= "\x00";
160 __file_format_ver__
= "001";
161 __use_new_style__
= True;
162 __file_format_list__
= [__file_format_name__
, __file_format_magic__
, __file_format_lower__
, __file_format_len__
, __file_format_hex__
, __file_format_delimiter__
, __file_format_ver__
, __use_new_style__
];
163 __project__
= __program_name__
;
164 __project_url__
= "https://github.com/GameMaker2k/PyCatFile";
165 __version_info__
= (0, 7, 0, "RC 1", 1);
166 __version_date_info__
= (2024, 3, 27, "RC 1", 1);
167 __version_date__
= str(__version_date_info__
[0]) + "." + str(__version_date_info__
[1]).zfill(2) + "." + str(__version_date_info__
[2]).zfill(2);
168 __revision__
= __version_info__
[3];
169 __revision_id__
= "$Id$";
170 if(__version_info__
[4] is not None):
171 __version_date_plusrc__
= __version_date__
+ "-" + str(__version_date_info__
[4]);
172 if(__version_info__
[4] is None):
173 __version_date_plusrc__
= __version_date__
;
174 if(__version_info__
[3] is not None):
175 __version__
= str(__version_info__
[0]) + "." + str(__version_info__
[1]) + "." + str(__version_info__
[2]) + " " + str(__version_info__
[3]);
176 if(__version_info__
[3] is None):
177 __version__
= str(__version_info__
[0]) + "." + str(__version_info__
[1]) + "." + str(__version_info__
[2]);
179 PyBitness
= platform
.architecture();
180 if(PyBitness
=="32bit" or PyBitness
=="32"):
182 elif(PyBitness
=="64bit" or PyBitness
=="64"):
187 geturls_ua_pycatfile_python
= "Mozilla/5.0 (compatible; {proname}/{prover}; +{prourl})".format(proname
=__project__
, prover
=__version__
, prourl
=__project_url__
);
188 if(platform
.python_implementation()!=""):
189 py_implementation
= platform
.python_implementation();
190 if(platform
.python_implementation()==""):
191 py_implementation
= "Python";
192 geturls_ua_pycatfile_python_alt
= "Mozilla/5.0 ({osver}; {archtype}; +{prourl}) {pyimp}/{pyver} (KHTML, like Gecko) {proname}/{prover}".format(osver
=platform
.system()+" "+platform
.release(), archtype
=platform
.machine(), prourl
=__project_url__
, pyimp
=py_implementation
, pyver
=platform
.python_version(), proname
=__project__
, prover
=__version__
);
193 geturls_ua_googlebot_google
= "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
194 geturls_ua_googlebot_google_old
= "Googlebot/2.1 (+http://www.google.com/bot.html)";
195 geturls_headers_pycatfile_python
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_pycatfile_python
, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close", 'SEC-CH-UA': "\""+__project__
+"\";v=\""+str(__version__
)+"\", \"Not;A=Brand\";v=\"8\", \""+py_implementation
+"\";v=\""+str(platform
.release())+"\"", 'SEC-CH-UA-FULL-VERSION': str(__version__
), 'SEC-CH-UA-PLATFORM': ""+py_implementation
+"", 'SEC-CH-UA-ARCH': ""+platform
.machine()+"", 'SEC-CH-UA-PLATFORM': str(__version__
), 'SEC-CH-UA-BITNESS': str(PyBitness
)};
196 geturls_headers_pycatfile_python_alt
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_pycatfile_python_alt
, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close", 'SEC-CH-UA': "\""+__project__
+"\";v=\""+str(__version__
)+"\", \"Not;A=Brand\";v=\"8\", \""+py_implementation
+"\";v=\""+str(platform
.release())+"\"", 'SEC-CH-UA-FULL-VERSION': str(__version__
), 'SEC-CH-UA-PLATFORM': ""+py_implementation
+"", 'SEC-CH-UA-ARCH': ""+platform
.machine()+"", 'SEC-CH-UA-PLATFORM': str(__version__
), 'SEC-CH-UA-BITNESS': str(PyBitness
)};
197 geturls_headers_googlebot_google
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_googlebot_google
, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close"};
198 geturls_headers_googlebot_google_old
= {'Referer': "http://google.com/", 'User-Agent': geturls_ua_googlebot_google_old
, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close"};
200 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'xz', 'lz4', 'lzo', 'lzma'];
201 outextlist
= ['gz', 'bz2', 'zst', 'xz', 'lz4', 'lzo', 'lzma'];
202 outextlistwd
= ['.gz', '.bz2', '.zst', '.xz', '.lz4', '.lzo', '.lzma'];
204 tarfile_mimetype
= "application/tar";
205 tarfile_tar_mimetype
= tarfile_mimetype
;
206 zipfile_mimetype
= "application/zip";
207 zipfile_zip_mimetype
= zipfile_mimetype
;
208 rarfile_mimetype
= "application/rar";
209 rarfile_rar_mimetype
= rarfile_mimetype
;
210 archivefile_mimetype
= "application/x-"+__file_format_list__
[1]+"";
211 archivefile_cat_mimetype
= archivefile_mimetype
;
212 archivefile_gzip_mimetype
= "application/x-"+__file_format_list__
[1]+"+gzip";
213 archivefile_gz_mimetype
= archivefile_gzip_mimetype
;
214 archivefile_bzip2_mimetype
= "application/x-"+__file_format_list__
[1]+"+bzip2";
215 archivefile_bz2_mimetype
= archivefile_bzip2_mimetype
;
216 archivefile_lz4_mimetype
= "application/x-"+__file_format_list__
[1]+"+lz4";
217 archivefile_lzop_mimetype
= "application/x-"+__file_format_list__
[1]+"+lzop";
218 archivefile_lzo_mimetype
= archivefile_lzop_mimetype
;
219 archivefile_zstandard_mimetype
= "application/x-"+__file_format_list__
[1]+"+zstandard";
220 archivefile_zstd_mimetype
= archivefile_zstandard_mimetype
;
221 archivefile_lzma_mimetype
= "application/x-"+__file_format_list__
[1]+"+lzma";
222 archivefile_xz_mimetype
= "application/x-"+__file_format_list__
[1]+"+xz";
223 archivefile_extensions
= ['.cat', '.cat.gz', '.cat.bz2', '.cat.zst', '.cat.lz4', '.cat.lzo', '.cat.lzop', '.cat.lzma', '.cat.xz'];
225 if __name__
== "__main__":
227 curscrpath
= os
.path
.dirname(sys
.argv
[0]);
231 curscrpath
= curscrpath
.replace(os
.sep
, "/");
232 curscrpath
= curscrpath
+ "/";
233 scrfile
= curscrpath
+ "catfile.py";
234 if(os
.path
.exists(scrfile
) and os
.path
.isfile(scrfile
)):
235 scrcmd
= subprocess
.Popen([sys
.executable
, scrfile
] + sys
.argv
[1:]);
238 def VerbosePrintOut(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
244 "warning": logging
.warning
,
245 "error": logging
.error
,
246 "critical": logging
.critical
,
247 "exception": logging
.exception
,
248 "logalt": lambda x
: logging
.log(dgblevel
, x
),
249 "debug": logging
.debug
251 log_function
= log_functions
.get(outtype
);
253 log_function(dbgtxt
);
257 def VerbosePrintOutReturn(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
258 VerbosePrintOut(dbgtxt
, outtype
, dbgenable
, dgblevel
);
261 def RemoveWindowsPath(dpath
):
265 dpath
= dpath
.replace(os
.path
.sep
, "/");
266 dpath
= dpath
.rstrip("/");
267 if(dpath
=="." or dpath
==".."):
271 def NormalizeRelativePath(inpath
):
272 inpath
= RemoveWindowsPath(inpath
);
273 if(os
.path
.isabs(inpath
)):
276 if(inpath
.startswith("./") or inpath
.startswith("../")):
279 outpath
= "./" + inpath
;
282 def PrependPath(base_dir
, child_path
):
283 # Check if base_dir is None or empty, if so, return child_path as is
286 # Ensure base_dir ends with exactly one slash
287 if not base_dir
.endswith('/'):
289 # Check if child_path starts with ./ or ../ (indicating a relative path)
290 if child_path
.startswith('./') or child_path
.startswith('../'):
291 # For relative paths, we don't alter the child_path
292 return base_dir
+ child_path
;
294 # For non-relative paths, ensure there's no starting slash on child_path to avoid double slashes
295 return base_dir
+ child_path
.lstrip('/');
297 def ListDir(dirpath
, followlink
=False, duplicates
=False):
298 if(isinstance(dirpath
, (list, tuple, ))):
299 dirpath
= list(filter(None, dirpath
));
300 elif(isinstance(dirpath
, (str, ))):
301 dirpath
= list(filter(None, [dirpath
]));
303 for mydirfile
in dirpath
:
304 if(not os
.path
.exists(mydirfile
)):
306 mydirfile
= NormalizeRelativePath(mydirfile
);
307 if(os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
)):
308 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
));
309 if(os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
)):
310 for root
, dirs
, filenames
in os
.walk(mydirfile
):
312 dpath
= RemoveWindowsPath(dpath
);
313 if(dpath
not in retlist
and not duplicates
):
314 retlist
.append(dpath
);
316 retlist
.append(dpath
);
317 for file in filenames
:
318 fpath
= os
.path
.join(root
, file);
319 fpath
= RemoveWindowsPath(fpath
);
320 if(fpath
not in retlist
and not duplicates
):
321 retlist
.append(fpath
);
323 retlist
.append(fpath
);
325 retlist
.append(RemoveWindowsPath(mydirfile
));
328 def ListDirAdvanced(dirpath
, followlink
=False, duplicates
=False):
329 if isinstance(dirpath
, (list, tuple)):
330 dirpath
= list(filter(None, dirpath
));
331 elif isinstance(dirpath
, str):
332 dirpath
= list(filter(None, [dirpath
]));
334 for mydirfile
in dirpath
:
335 if not os
.path
.exists(mydirfile
):
337 mydirfile
= NormalizeRelativePath(mydirfile
);
338 if os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
) and followlink
:
339 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
))
340 if os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
):
341 for root
, dirs
, filenames
in os
.walk(mydirfile
):
342 # Sort dirs and filenames alphabetically in place
343 dirs
.sort(key
=lambda x
: x
.lower());
344 filenames
.sort(key
=lambda x
: x
.lower());
345 dpath
= RemoveWindowsPath(root
);
346 if not duplicates
and dpath
not in retlist
:
347 retlist
.append(dpath
);
349 retlist
.append(dpath
);
350 for file in filenames
:
351 fpath
= os
.path
.join(root
, file);
352 fpath
= RemoveWindowsPath(fpath
);
353 if not duplicates
and fpath
not in retlist
:
354 retlist
.append(fpath
);
356 retlist
.append(fpath
);
358 retlist
.append(RemoveWindowsPath(mydirfile
));
361 def create_alias_function(prefix
, base_name
, suffix
, target_function
):
362 # Define a new function that wraps the target function
363 def alias_function(*args
, **kwargs
):
364 return target_function(*args
, **kwargs
);
366 # Create the function name by combining the prefix, base name, and the suffix
367 function_name
= "{}{}{}".format(prefix
, base_name
, suffix
);
369 # Add the new function to the global namespace
370 globals()[function_name
] = alias_function
;
372 def create_alias_function_alt(prefix
, base_name
, suffix
, target_function
):
373 # Create the function name by combining the prefix, base name, and the suffix
374 # Use the format method for string formatting, compatible with Python 2 and 3
375 function_name
= "{}{}{}".format(prefix
, base_name
, suffix
)
376 # Add the new function (alias of the target_function) to the global namespace
377 # This line is compatible as-is with both Python 2 and 3
378 globals()[function_name
] = target_function
380 # initial_value can be 0xFFFF or 0x0000
381 def crc16_ansi(msg
, initial_value
=0xFFFF):
382 # CRC-16-IBM / CRC-16-ANSI polynomial and initial value
383 poly
= 0x8005; # Polynomial for CRC-16-IBM / CRC-16-ANSI
384 crc
= initial_value
; # Initial value
386 crc ^
= b
<< 8; # XOR byte into CRC top byte
387 for _
in range(8): # Process each bit
388 if crc
& 0x8000: # If the top bit is set
389 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
391 crc
= crc
<< 1; # Just shift left
392 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
395 # initial_value can be 0xFFFF or 0x0000
396 def crc16_ibm(msg
, initial_value
=0xFFFF):
397 return crc16_ansi(msg
, initial_value
);
399 # initial_value is 0xFFFF
401 return crc16_ansi(msg
, 0xFFFF);
403 # initial_value can be 0xFFFF, 0x1D0F or 0x0000
404 def crc16_ccitt(msg
, initial_value
=0xFFFF):
405 # CRC-16-CCITT polynomial
406 poly
= 0x1021; # Polynomial for CRC-16-CCITT
407 # Use the specified initial value
410 crc ^
= b
<< 8; # XOR byte into CRC top byte
411 for _
in range(8): # Process each bit
412 if crc
& 0x8000: # If the top bit is set
413 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
415 crc
= crc
<< 1; # Just shift left
416 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
419 # initial_value can be 0x42F0E1EBA9EA3693 or 0x0000000000000000
420 def crc64_ecma(msg
, initial_value
=0x0000000000000000):
421 # CRC-64-ECMA polynomial and initial value
422 poly
= 0x42F0E1EBA9EA3693;
423 crc
= initial_value
; # Initial value for CRC-64-ECMA
425 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
426 for _
in range(8): # Process each bit
427 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
428 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
430 crc
<<= 1; # Just shift left if the MSB is 0
431 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
434 # initial_value can be 0x000000000000001B or 0xFFFFFFFFFFFFFFFF
435 def crc64_iso(msg
, initial_value
=0xFFFFFFFFFFFFFFFF):
436 # CRC-64-ISO polynomial and initial value
437 poly
= 0x000000000000001B;
438 crc
= initial_value
; # Common initial value for CRC-64-ISO
440 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
441 for _
in range(8): # Process each bit
442 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
443 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
445 crc
<<= 1; # Just shift left if the MSB is 0
446 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
449 def GetDataFromArray(data
, path
, default
=None):
453 element
= element
[key
];
455 except (KeyError, TypeError, IndexError):
458 def GetDataFromArrayAlt(structure
, path
, default
=None):
461 if isinstance(element
, dict) and key
in element
:
462 element
= element
[key
];
463 elif isinstance(element
, list) and isinstance(key
, int) and -len(element
) <= key
< len(element
):
464 element
= element
[key
];
469 def GetHeaderChecksum(inlist
=[], checksumtype
="crc32", encodedata
=True, formatspecs
=__file_format_list__
):
470 if isinstance(inlist
, list):
471 fileheader
= AppendNullBytes(inlist
, formatspecs
[5]);
473 fileheader
= AppendNullByte(inlist
, formatspecs
[5]);
475 fileheader
= fileheader
.encode('UTF-8');
476 if(checksumtype
=="none" or checksumtype
==""):
477 catfileheadercshex
= format(0, 'x').lower();
478 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
479 catfileheadercshex
= format(crc16(fileheader
) & 0xffff, '04x').lower();
480 elif(checksumtype
=="crc16_ccitt"):
481 catfileheadercshex
= format(crc16_ccitt(fileheader
) & 0xffff, '04x').lower();
482 elif(checksumtype
=="adler32"):
483 catfileheadercshex
= format(zlib
.adler32(fileheader
) & 0xffffffff, '08x').lower();
484 elif(checksumtype
=="crc32"):
485 catfileheadercshex
= format(crc32(fileheader
) & 0xffffffff, '08x').lower();
486 elif(checksumtype
=="crc64_ecma"):
487 catfileheadercshex
= format(crc64_ecma(fileheader
) & 0xffffffffffffffff, '016x').lower();
488 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
489 catfileheadercshex
= format(crc64_iso(fileheader
) & 0xffffffffffffffff, '016x').lower();
490 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
491 checksumoutstr
= hashlib
.new(checksumtype
);
492 checksumoutstr
.update(fileheader
);
493 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
495 catfileheadercshex
= format(0, 'x').lower();
496 return catfileheadercshex
;
498 def GetFileChecksum(instr
, checksumtype
="crc32", encodedata
=True, formatspecs
=__file_format_list__
):
500 instr
= instr
.encode('UTF-8');
501 if(checksumtype
=="none" or checksumtype
==""):
502 catinstrcshex
= format(0, 'x').lower();
503 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
504 catinstrcshex
= format(crc16(instr
) & 0xffff, '04x').lower();
505 elif(checksumtype
=="crc16_ccitt"):
506 catinstrcshex
= format(crc16_ccitt(instr
) & 0xffff, '04x').lower();
507 elif(checksumtype
=="adler32"):
508 catinstrcshex
= format(zlib
.adler32(instr
) & 0xffffffff, '08x').lower();
509 elif(checksumtype
=="crc32"):
510 catinstrcshex
= format(crc32(instr
) & 0xffffffff, '08x').lower();
511 elif(checksumtype
=="crc64_ecma"):
512 catinstrcshex
= format(crc64_ecma(instr
) & 0xffffffffffffffff, '016x').lower();
513 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
514 catinstrcshex
= format(crc64_iso(instr
) & 0xffffffffffffffff, '016x').lower();
515 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
516 checksumoutstr
= hashlib
.new(checksumtype
);
517 checksumoutstr
.update(instr
);
518 catinstrcshex
= checksumoutstr
.hexdigest().lower();
520 catinstrcshex
= format(0, 'x').lower();
521 return catinstrcshex
;
523 def ValidateHeaderChecksum(inlist
=[], checksumtype
="crc32", inchecksum
="0", formatspecs
=__file_format_list__
):
524 catfileheadercshex
= GetHeaderChecksum(inlist
, checksumtype
, True, formatspecs
);
525 inchecksum
= inchecksum
.lower();
526 catfileheadercshex
= catfileheadercshex
.lower();
527 if(inchecksum
==catfileheadercshex
):
532 def ValidateFileChecksum(infile
, checksumtype
="crc32", inchecksum
="0", formatspecs
=__file_format_list__
):
533 catinfilecshex
= GetFileChecksum(infile
, checksumtype
, True, formatspecs
);
534 inchecksum
= inchecksum
.lower();
535 catinfilecshex
= catinfilecshex
.lower();
536 if(inchecksum
==catinfilecshex
):
541 def ReadTillNullByte(fp
, delimiter
=__file_format_delimiter__
):
544 nullbyte
= delimiter
.encode("UTF-8");
546 curbyte
= fp
.read(1);
547 if(curbyte
==nullbyte
or not curbyte
):
549 curfullbyte
= curfullbyte
+ curbyte
;
550 return curfullbyte
.decode('UTF-8');
552 def ReadUntilNullByte(fp
, delimiter
=__file_format_delimiter__
):
553 return ReadTillNullByte(fp
, delimiter
);
555 def SeekToEndOfFile(fp
):
559 if(lasttell
==fp
.tell()):
561 lasttell
= fp
.tell();
564 def ReadFileHeaderData(fp
, rounds
=0, delimiter
=__file_format_delimiter__
):
568 while(rocount
<roend
):
569 HeaderOut
.append(ReadTillNullByte(fp
, delimiter
));
570 rocount
= rocount
+ 1;
573 def ReadFileHeaderDataBySize(fp
, delimiter
=__file_format_delimiter__
):
574 headerpresize
= ReadTillNullByte(fp
, delimiter
);
575 headersize
= int(headerpresize
, 16);
576 headercontent
= str(fp
.read(headersize
).decode('UTF-8')).split(delimiter
);
579 roend
= int(len(headercontent
));
580 HeaderOut
= [headerpresize
];
581 while(rocount
<roend
):
582 HeaderOut
.append(headercontent
[rocount
]);
583 rocount
= rocount
+ 1;
586 def ReadFileHeaderDataWoSize(fp
, delimiter
=__file_format_delimiter__
):
587 preheaderdata
= ReadFileHeaderData(fp
, 2, delimiter
);
588 headersize
= int(preheaderdata
[0], 16);
589 headernumfields
= int(preheaderdata
[1], 16);
590 headerdata
= ReadFileHeaderData(fp
, headernumfields
, delimiter
);
591 HeaderOut
= preheaderdata
+ headerdata
;
594 def ReadFileHeaderDataBySizeWithContent(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
595 delimiter
= formatspecs
[5];
596 fheaderstart
= fp
.tell();
597 HeaderOut
= ReadFileHeaderDataBySize(fp
, delimiter
);
598 if(re
.findall("^[.|/]", HeaderOut
[3])):
599 fname
= HeaderOut
[3];
601 fname
= "./"+HeaderOut
[3];
602 fchecksumtype
= HeaderOut
[-3].lower();
603 fcs
= HeaderOut
[-2].lower();
604 fccs
= HeaderOut
[-1].lower();
605 fsize
= int(HeaderOut
[5], 16);
606 newfcs
= GetHeaderChecksum(HeaderOut
[:-2], HeaderOut
[-3].lower(), True, formatspecs
);
607 if(fcs
!=newfcs
and not skipchecksum
):
608 VerbosePrintOut("File Header Checksum Error with file " + fname
+ " at offset " + str(fheaderstart
));
610 fhend
= fp
.tell() - 1;
611 fcontentstart
= fp
.tell();
612 fcontents
= "".encode('UTF-8');
613 if(fsize
>0 and not listonly
):
614 fcontents
= fp
.read(fsize
);
615 elif(fsize
>0 and listonly
):
617 newfccs
= GetFileChecksum(fcontents
, HeaderOut
[-3].lower(), False, formatspecs
);
618 if(fccs
!=newfccs
and not skipchecksum
and not listonly
):
619 VerbosePrintOut("File Content Checksum Error with file " + fname
+ " at offset " + str(fcontentstart
));
622 fcontentend
= fp
.tell() - 1;
623 HeaderOut
.append(fcontents
);
626 def ReadFileHeaderDataBySizeWithContentToArray(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
627 delimiter
= formatspecs
[5];
628 fheaderstart
= fp
.tell();
630 HeaderOut
= ReadFileHeaderDataBySize(fp
, delimiter
);
632 HeaderOut
= ReadFileHeaderDataWoSize(fp
, delimiter
);
633 fheadsize
= int(HeaderOut
[0], 16);
634 fnumfields
= int(HeaderOut
[1], 16);
635 ftype
= int(HeaderOut
[2], 16);
636 if(re
.findall("^[.|/]", HeaderOut
[3])):
637 fname
= HeaderOut
[3];
639 fname
= "./"+HeaderOut
[3];
640 fbasedir
= os
.path
.dirname(fname
);
641 flinkname
= HeaderOut
[4];
642 fsize
= int(HeaderOut
[5], 16);
643 fatime
= int(HeaderOut
[6], 16);
644 fmtime
= int(HeaderOut
[7], 16);
645 fctime
= int(HeaderOut
[8], 16);
646 fbtime
= int(HeaderOut
[9], 16);
647 fmode
= int(HeaderOut
[10], 16);
648 fchmode
= stat
.S_IMODE(fmode
);
649 ftypemod
= stat
.S_IFMT(fmode
);
650 fwinattributes
= int(HeaderOut
[11], 16);
651 fuid
= int(HeaderOut
[12], 16);
652 funame
= HeaderOut
[13];
653 fgid
= int(HeaderOut
[14], 16);
654 fgname
= HeaderOut
[15];
655 fid
= int(HeaderOut
[16], 16);
656 finode
= int(HeaderOut
[17], 16);
657 flinkcount
= int(HeaderOut
[18], 16);
658 fdev_minor
= int(HeaderOut
[19], 16);
659 fdev_major
= int(HeaderOut
[20], 16);
660 frdev_minor
= int(HeaderOut
[21], 16);
661 frdev_major
= int(HeaderOut
[22], 16);
662 fextrasize
= int(HeaderOut
[23], 16);
663 fextrafields
= int(HeaderOut
[24], 16);
664 extrafieldslist
= [];
666 extraend
= extrastart
+ fextrafields
;
667 extrafieldslist
= [];
668 if(extrastart
<extraend
):
669 extrafieldslist
.append(HeaderOut
[extrastart
]);
670 extrastart
= extrastart
+ 1;
671 fchecksumtype
= HeaderOut
[extrastart
].lower();
672 fcs
= HeaderOut
[extrastart
+ 1].lower();
673 fccs
= HeaderOut
[extrastart
+ 2].lower();
674 newfcs
= GetHeaderChecksum(HeaderOut
[:-2], HeaderOut
[-3].lower(), True, formatspecs
);
675 if(fcs
!=newfcs
and not skipchecksum
):
676 VerbosePrintOut("File Header Checksum Error with file " + fname
+ " at offset " + str(fheaderstart
));
678 fhend
= fp
.tell() - 1;
679 fcontentstart
= fp
.tell();
680 fcontents
= "".encode('UTF-8');
681 pyhascontents
= False;
682 if(fsize
>0 and not listonly
):
683 fcontents
= fp
.read(fsize
);
684 pyhascontents
= True;
685 elif(fsize
>0 and listonly
):
687 pyhascontents
= False;
688 newfccs
= GetFileChecksum(fcontents
, HeaderOut
[-3].lower(), False, formatspecs
);
689 if(fccs
!=newfccs
and not skipchecksum
and not listonly
):
690 VerbosePrintOut("File Content Checksum Error with file " + fname
+ " at offset " + str(fcontentstart
));
693 fcontentend
= fp
.tell() - 1;
694 catlist
= {'fheadersize': fheadsize
, 'fhstart': fheaderstart
, 'fhend': fhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fwinattributes': fwinattributes
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': fchecksumtype
, 'fnumfields': fnumfields
+ 2, 'frawheader': HeaderOut
, 'fextrafields': fextrafields
, 'fextrafieldsize': fextrasize
, 'fextralist': extrafieldslist
, 'fheaderchecksum': fcs
, 'fcontentchecksum': fccs
, 'fhascontents': pyhascontents
, 'fcontentstart': fcontentstart
, 'fcontentend': fcontentend
, 'fcontents': fcontents
}
697 def ReadFileDataBySizeWithContent(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
698 delimiter
= formatspecs
[5];
699 catheader
= ReadFileHeaderData(fp
, 4, delimiter
);
700 headercheck
= ValidateHeaderChecksum(catheader
[:-1], catheader
[2], catheader
[3], formatspecs
);
701 if(not headercheck
and not skipchecksum
):
702 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
704 fnumfiles
= int(catheader
[1], 16);
707 while(countnum
< fnumfiles
):
708 flist
.append(ReadFileHeaderDataBySizeWithContent(fp
, listonly
, skipchecksum
, formatspecs
));
709 countnum
= countnum
+ 1;
712 def ReadFileDataBySizeWithContentToArray(fp
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
713 delimiter
= formatspecs
[5];
714 catheader
= ReadFileHeaderData(fp
, 4, delimiter
);
715 headercheck
= ValidateHeaderChecksum(catheader
[:-1], catheader
[2], catheader
[3], formatspecs
);
716 if(not headercheck
and not skipchecksum
):
717 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
719 catstring
= catheader
[0];
720 catversion
= re
.findall(r
"([\d]+)$", catstring
);
721 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
722 fprenumfiles
= catheader
[1];
723 fnumfiles
= int(fprenumfiles
, 16);
724 fprechecksumtype
= catheader
[2];
725 fprechecksum
= catheader
[3];
726 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
727 if(seekstart
<0 and seekstart
>fnumfiles
):
729 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
731 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
732 seekend
= fnumfiles
- abs(seekend
);
735 while(il
< seekstart
):
736 prefhstart
= fp
.tell();
737 preheaderdata
= ReadFileHeaderDataBySize(fp
, formatspecs
[5]);
738 prefsize
= int(preheaderdata
[5], 16);
739 prenewfcs
= GetHeaderChecksum(preheaderdata
[:-2], preheaderdata
[-3].lower(), True, formatspecs
);
740 prefcs
= preheaderdata
[-2];
741 if(prefcs
!=prenewfcs
and not skipchecksum
):
742 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
744 valid_archive
= False;
745 invalid_archive
= True;
746 prefhend
= fp
.tell() - 1;
747 prefcontentstart
= fp
.tell();
749 pyhascontents
= False;
751 prefcontents
= fp
.read(prefsize
);
752 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
753 prefccs
= preheaderdata
[-1];
754 pyhascontents
= True;
755 if(prefccs
!=prenewfccs
and not skipchecksum
):
756 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
761 countnum
= seekstart
;
762 while(countnum
< seekend
):
763 catlist
['ffilelist'].update({realidnum
: {'fid': realidnum
, 'fidalt': realidnum
}});
764 catlist
['ffilelist'][realidnum
].update(ReadFileHeaderDataBySizeWithContentToArray(fp
, listonly
, skipchecksum
, formatspecs
));
765 countnum
= countnum
+ 1;
766 realidnum
= realidnum
+ 1;
769 def ReadFileDataBySizeWithContentToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
770 if(isinstance(infile
, dict)):
771 listcatfiles
= infile
;
773 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
775 listcatfiles
= ReadFileDataBySizeWithContentToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
);
776 if(not listcatfiles
):
778 catarray
= {'list': listcatfiles
, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
779 lenlist
= len(listcatfiles
['ffilelist']);
781 lcfx
= int(listcatfiles
['fnumfiles']);
782 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
785 lcfx
= int(listcatfiles
['fnumfiles']);
787 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
788 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
789 catarray
['filetoid'].update(filetoidarray
);
790 catarray
['idtofile'].update(idtofilearray
);
791 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
792 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
793 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
794 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
795 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
796 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
797 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
798 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
799 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
800 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
801 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
802 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
803 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
804 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
805 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
806 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
807 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
808 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
809 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
810 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
811 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
812 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
813 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
814 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
815 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
816 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
817 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
818 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
819 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
820 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
821 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
825 def ReadInFileBySizeWithContent(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
826 delimiter
= formatspecs
[5];
827 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
830 fp
= UncompressArchiveFile(fp
, formatspecs
);
831 checkcompressfile
= CheckCompressionSubType(fp
, formatspecs
);
832 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
839 if(hasattr(sys
.stdin
, "buffer")):
840 shutil
.copyfileobj(sys
.stdin
.buffer, fp
);
842 shutil
.copyfileobj(sys
.stdin
, fp
);
844 fp
= UncompressArchiveFile(fp
, formatspecs
);
848 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
849 fp
= download_file_from_internet_file(infile
);
850 fp
= UncompressArchiveFile(fp
, formatspecs
);
856 infile
= RemoveWindowsPath(infile
);
857 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
858 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
860 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
861 if(not compresscheck
):
862 fextname
= os
.path
.splitext(infile
)[1];
864 compresscheck
= "gzip";
865 elif(fextname
==".bz2"):
866 compresscheck
= "bzip2";
867 elif(fextname
==".zst"):
868 compresscheck
= "zstd";
869 elif(fextname
==".lz4" or fextname
==".clz4"):
870 compresscheck
= "lz4";
871 elif(fextname
==".lzo" or fextname
==".lzop"):
872 compresscheck
= "lzo";
873 elif(fextname
==".lzma" or fextname
==".xz"):
874 compresscheck
= "lzma";
877 if(not compresscheck
):
879 fp
= UncompressFile(infile
, formatspecs
, "rb");
880 return ReadFileDataBySizeWithContentToArray(fp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
);
882 def ReadInFileBySizeWithContentToArray(infile
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
883 delimiter
= formatspecs
[5];
884 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
887 fp
= UncompressArchiveFile(fp
, formatspecs
);
888 checkcompressfile
= CheckCompressionSubType(fp
, formatspecs
);
889 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
896 if(hasattr(sys
.stdin
, "buffer")):
897 shutil
.copyfileobj(sys
.stdin
.buffer, fp
);
899 shutil
.copyfileobj(sys
.stdin
, fp
);
901 fp
= UncompressArchiveFile(fp
, formatspecs
);
905 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
906 fp
= download_file_from_internet_file(infile
);
907 fp
= UncompressArchiveFile(fp
, formatspecs
);
913 infile
= RemoveWindowsPath(infile
);
914 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
915 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
917 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
918 if(not compresscheck
):
919 fextname
= os
.path
.splitext(infile
)[1];
921 compresscheck
= "gzip";
922 elif(fextname
==".bz2"):
923 compresscheck
= "bzip2";
924 elif(fextname
==".zst"):
925 compresscheck
= "zstd";
926 elif(fextname
==".lz4" or fextname
==".clz4"):
927 compresscheck
= "lz4";
928 elif(fextname
==".lzo" or fextname
==".lzop"):
929 compresscheck
= "lzo";
930 elif(fextname
==".lzma" or fextname
==".xz"):
931 compresscheck
= "lzma";
934 if(not compresscheck
):
936 fp
= UncompressFile(infile
, formatspecs
, "rb");
937 return ReadFileDataBySizeWithContentToArray(fp
, listonly
, skipchecksum
, formatspecs
);
939 def ReadInFileBySizeWithContentToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
940 if(isinstance(infile
, dict)):
941 listcatfiles
= infile
;
943 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
944 infile
= RemoveWindowsPath(infile
);
945 listcatfiles
= ReadInFileBySizeWithContentToArray(infile
, listonly
, skipchecksum
, formatspecs
);
946 if(not listcatfiles
):
948 catarray
= {'list': listcatfiles
, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
949 lenlist
= len(listcatfiles
['ffilelist']);
951 lcfx
= int(listcatfiles
['fnumfiles']);
952 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
955 lcfx
= int(listcatfiles
['fnumfiles']);
957 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
958 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
959 catarray
['filetoid'].update(filetoidarray
);
960 catarray
['idtofile'].update(idtofilearray
);
961 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
962 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
963 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
964 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
965 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
966 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
967 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
968 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
969 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
970 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
971 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
972 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
973 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
974 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
975 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
976 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
977 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
978 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
979 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
980 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
981 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
982 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
983 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
984 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
985 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
986 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
987 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
988 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
989 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
990 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
991 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
995 def AppendNullByte(indata
, delimiter
=__file_format_delimiter__
):
996 outdata
= str(indata
) + delimiter
;
999 def AppendNullBytes(indata
=[], delimiter
=__file_format_delimiter__
):
1004 outdata
= outdata
+ AppendNullByte(indata
[inum
], delimiter
);
1008 def AppendFileHeader(fp
, numfiles
, checksumtype
="crc32", formatspecs
=__file_format_list__
):
1009 delimiter
= formatspecs
[5];
1010 catver
= formatspecs
[6];
1011 fileheaderver
= str(int(catver
.replace(".", "")));
1012 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
1013 fp
.write(fileheader
.encode('UTF-8'));
1014 fnumfiles
= format(int(numfiles
), 'x').lower();
1015 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
1016 catfileheadercshex
= GetFileChecksum(fileheader
+ fnumfilesa
, checksumtype
, True, formatspecs
);
1017 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
1018 fp
.write(fnumfilesa
.encode('UTF-8'));
1021 os
.fsync(fp
.fileno());
1022 except io
.UnsupportedOperation
:
1024 except AttributeError:
1028 def AppendFileHeaderWithContent(fp
, filevalues
=[], extradata
=[], filecontent
="", checksumtype
="crc32", formatspecs
=__file_format_list__
):
1029 extrafields
= format(len(extradata
), 'x').lower();
1030 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
1031 if(len(extradata
)>0):
1032 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
1033 extrasizelen
= format(len(extrasizestr
), 'x').lower();
1034 catoutlen
= len(filevalues
) + len(extradata
) + 5;
1035 catoutlenhex
= format(catoutlen
, 'x').lower();
1036 catoutlist
= filevalues
;
1037 catoutlist
.insert(0, catoutlenhex
);
1038 catoutlist
.append(extrasizelen
);
1039 catoutlist
.append(extrafields
);
1040 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
1041 if(len(extradata
)>0):
1042 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
1043 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
1044 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
1045 catfilecontentcshex
= GetFileChecksum(filecontent
, checksumtype
, False, formatspecs
);
1046 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1047 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
1048 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
1049 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
1050 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1051 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
1052 nullstrecd
= formatspecs
[5].encode('UTF-8');
1053 catfileout
= catfileoutstrecd
+ filecontent
+ nullstrecd
;
1054 fp
.write(catfileout
);
1057 os
.fsync(fp
.fileno());
1058 except io
.UnsupportedOperation
:
1060 except AttributeError:
1064 def AppendFilesWithContent(infiles
, fp
, dirlistfromtxt
=False, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
):
1065 advancedlist
= True;
1067 for line
in sys
.stdin
:
1068 infilelist
.append(line
.strip());
1069 infilelist
= list(filter(None, infilelist
));
1070 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
1071 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
1073 with
UncompressFile(infiles
, formatspecs
, "r") as finfile
:
1074 for line
in finfile
:
1075 infilelist
.append(line
.strip());
1076 infilelist
= list(filter(None, infilelist
));
1078 if(isinstance(infiles
, (list, tuple, ))):
1079 infilelist
= list(filter(None, infiles
));
1080 elif(isinstance(infiles
, (str, ))):
1081 infilelist
= list(filter(None, [infiles
]));
1083 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
1085 GetDirList
= ListDir(infilelist
, followlink
, False);
1093 inodetocatinode
= {};
1094 fnumfiles
= int(len(GetDirList
));
1095 AppendFileHeader(fp
, fnumfiles
, checksumtype
, formatspecs
);
1096 fnumfiles
= format(fnumfiles
, 'x').lower();
1097 for curfname
in GetDirList
:
1098 catfhstart
= fp
.tell();
1099 if(re
.findall("^[.|/]", curfname
)):
1102 fname
= "./"+curfname
;
1103 if(not followlink
or followlink
is None):
1104 fstatinfo
= os
.lstat(fname
);
1106 fstatinfo
= os
.stat(fname
);
1107 fpremode
= fstatinfo
.st_mode
;
1108 finode
= fstatinfo
.st_ino
;
1109 flinkcount
= fstatinfo
.st_nlink
;
1111 if(stat
.S_ISREG(fpremode
)):
1113 elif(stat
.S_ISLNK(fpremode
)):
1115 elif(stat
.S_ISCHR(fpremode
)):
1117 elif(stat
.S_ISBLK(fpremode
)):
1119 elif(stat
.S_ISDIR(fpremode
)):
1121 elif(stat
.S_ISFIFO(fpremode
)):
1123 elif(stat
.S_ISSOCK(fpremode
)):
1125 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
1127 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
1129 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
1134 fcurfid
= format(int(curfid
), 'x').lower();
1135 if(not followlink
and finode
!=0):
1137 if(finode
in inodelist
):
1139 flinkname
= inodetofile
[finode
];
1140 fcurinode
= format(int(inodetocatinode
[finode
]), 'x').lower();
1141 if(finode
not in inodelist
):
1142 inodelist
.append(finode
);
1143 inodetofile
.update({finode
: fname
});
1144 inodetocatinode
.update({finode
: curinode
});
1145 fcurinode
= format(int(curinode
), 'x').lower();
1146 curinode
= curinode
+ 1;
1148 fcurinode
= format(int(curinode
), 'x').lower();
1149 curinode
= curinode
+ 1;
1150 curfid
= curfid
+ 1;
1152 flinkname
= os
.readlink(fname
);
1153 fdev
= fstatinfo
.st_dev
;
1154 getfdev
= GetDevMajorMinor(fdev
);
1155 fdev_minor
= getfdev
[0];
1156 fdev_major
= getfdev
[1];
1157 frdev
= fstatinfo
.st_dev
;
1158 if(hasattr(fstatinfo
, "st_rdev")):
1159 frdev
= fstatinfo
.st_rdev
;
1161 frdev
= fstatinfo
.st_dev
;
1162 getfrdev
= GetDevMajorMinor(frdev
);
1163 frdev_minor
= getfrdev
[0];
1164 frdev_major
= getfrdev
[1];
1165 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
1166 fsize
= format(int("0"), 'x').lower();
1167 elif(ftype
==0 or ftype
==7):
1168 fsize
= format(int(fstatinfo
.st_size
), 'x').lower();
1170 fsize
= format(int(fstatinfo
.st_size
)).lower();
1171 fatime
= format(int(fstatinfo
.st_atime
), 'x').lower();
1172 fmtime
= format(int(fstatinfo
.st_mtime
), 'x').lower();
1173 fctime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1174 if(hasattr(fstatinfo
, "st_birthtime")):
1175 fbtime
= format(int(fstatinfo
.st_birthtime
), 'x').lower();
1177 fbtime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1178 fmode
= format(int(fstatinfo
.st_mode
), 'x').lower();
1179 fchmode
= format(int(stat
.S_IMODE(fstatinfo
.st_mode
)), 'x').lower();
1180 ftypemod
= format(int(stat
.S_IFMT(fstatinfo
.st_mode
)), 'x').lower();
1181 fuid
= format(int(fstatinfo
.st_uid
), 'x').lower();
1182 fgid
= format(int(fstatinfo
.st_gid
), 'x').lower();
1187 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
1188 funame
= userinfo
.pw_name
;
1197 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
1198 fgname
= groupinfo
.gr_name
;
1203 fdev_minor
= format(int(fdev_minor
), 'x').lower();
1204 fdev_major
= format(int(fdev_major
), 'x').lower();
1205 frdev_minor
= format(int(frdev_minor
), 'x').lower();
1206 frdev_major
= format(int(frdev_major
), 'x').lower();
1207 finode
= format(int(finode
), 'x').lower();
1208 flinkcount
= format(int(flinkcount
), 'x').lower();
1209 if(hasattr(fstatinfo
, "st_file_attributes")):
1210 fwinattributes
= format(int(fstatinfo
.st_file_attributes
), 'x').lower();
1212 fwinattributes
= format(int(0), 'x').lower();
1213 fcontents
= "".encode('UTF-8');
1215 if(ftype
== 0 or ftype
== 7):
1216 with
open(fname
, "rb") as fpc
:
1218 chunk
= fpc
.read(chunk_size
);
1222 if(followlink
and (ftype
== 1 or ftype
== 2)):
1223 flstatinfo
= os
.stat(flinkname
);
1224 with
open(flinkname
, "rb") as fpc
:
1226 chunk
= fpc
.read(chunk_size
);
1230 ftypehex
= format(ftype
, 'x').lower();
1231 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
];
1232 fp
= AppendFileHeaderWithContent(fp
, catoutlist
, extradata
, fcontents
, checksumtype
, formatspecs
);
1236 def AppendFilesWithContentToOutFile(infile
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, returnfp
=False):
1237 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1238 if(os
.path
.exists(outfile
)):
1242 catfpfp
= BytesIO();
1243 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1245 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1248 fbasename
= os
.path
.splitext(outfile
)[0];
1249 fextname
= os
.path
.splitext(outfile
)[1];
1250 catfp
= CompressOpenFile(outfile
, compressionlevel
);
1251 catfp
= AppendFilesWithContent(infile
, catfp
, dirlistfromtxt
, filevalues
, extradata
, followlink
, checksumtype
, formatspecs
);
1252 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1253 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1256 os
.fsync(catfp
.fileno());
1257 except io
.UnsupportedOperation
:
1259 except AttributeError:
1263 if(hasattr(sys
.stdout
, "buffer")):
1264 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1266 shutil
.copyfileobj(catfp
, sys
.stdout
);
1267 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1268 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1270 upload_file_to_internet_file(catfp
, outfile
);
1278 def PrintPermissionString(fchmode
, ftype
):
1279 permissions
= { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
1281 for fmodval
in str(oct(fchmode
))[-3:]:
1282 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
1283 if(ftype
==0 or ftype
==7):
1284 permissionstr
= "-" + permissionstr
;
1286 permissionstr
= "h" + permissionstr
;
1288 permissionstr
= "l" + permissionstr
;
1290 permissionstr
= "c" + permissionstr
;
1292 permissionstr
= "b" + permissionstr
;
1294 permissionstr
= "d" + permissionstr
;
1296 permissionstr
= "f" + permissionstr
;
1298 permissionstr
= "D" + permissionstr
;
1300 permissionstr
= "p" + permissionstr
;
1302 permissionstr
= "w" + permissionstr
;
1304 permissionoutstr
= stat
.filemode(fchmode
);
1305 except AttributeError:
1306 permissionoutstr
= permissionstr
;
1308 permissionoutstr
= permissionstr
;
1309 return permissionoutstr
;
1311 def PrintPermissionStringAlt(fchmode
, ftype
):
1313 '0': '---', '1': '--x', '2': '-w-', '3': '-wx',
1314 '4': 'r--', '5': 'r-x', '6': 'rw-', '7': 'rwx'
1316 # Translate file mode into permission string
1317 permissionstr
= ''.join([permissions
[i
] for i
in str(oct(fchmode
))[-3:]]);
1318 # Append file type indicator
1320 0: '-', 1: 'h', 2: 'l', 3: 'c', 4: 'b',
1321 5: 'd', 6: 'f', 8: 'D', 9: 'p', 10: 'w'
1323 file_type
= type_indicators
.get(ftype
, '-');
1324 permissionstr
= file_type
+ permissionstr
;
1326 permissionoutstr
= stat
.filemode(fchmode
);
1327 except AttributeError:
1328 permissionoutstr
= permissionstr
;
1329 return permissionoutstr
;
1331 def CompressionSupport():
1332 compression_list
= [];
1335 compression_list
.append("gz");
1336 compression_list
.append("gzip");
1341 compression_list
.append("bz2");
1342 compression_list
.append("bzip2");
1347 compression_list
.append("lz4");
1352 compression_list
.append("lzo");
1353 compression_list
.append("lzop");
1358 compression_list
.append("zstd");
1359 compression_list
.append("zstandard");
1364 compression_list
.append("lzma");
1365 compression_list
.append("xz");
1368 return compression_list
;
1370 def CheckCompressionType(infile
, formatspecs
=__file_format_list__
, closefp
=True):
1371 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1375 catfp
= open(infile
, "rb");
1376 except FileNotFoundError
:
1379 prefp
= catfp
.read(2);
1381 if(prefp
==binascii
.unhexlify("1f8b")):
1384 prefp
= catfp
.read(3);
1385 if(prefp
==binascii
.unhexlify("425a68")):
1387 if(prefp
==binascii
.unhexlify("5d0000")):
1390 prefp
= catfp
.read(4);
1391 if(prefp
==binascii
.unhexlify("28b52ffd")):
1393 if(prefp
==binascii
.unhexlify("04224d18")):
1395 if(prefp
==binascii
.unhexlify("504B0304")):
1396 filetype
= "zipfile";
1398 prefp
= catfp
.read(5);
1399 if(prefp
==binascii
.unhexlify("7573746172")):
1400 filetype
= "tarfile";
1402 prefp
= catfp
.read(6);
1403 if(prefp
==binascii
.unhexlify("fd377a585a00")):
1406 prefp
= catfp
.read(7);
1407 if(prefp
==binascii
.unhexlify("526172211a0700")):
1408 filetype
= "rarfile";
1409 if(prefp
==binascii
.unhexlify("43617446696c65")):
1410 filetype
= "catfile";
1412 prefp
= catfp
.read(8);
1413 if(prefp
==binascii
.unhexlify("526172211a070100")):
1414 filetype
= "rarfile";
1416 prefp
= catfp
.read(formatspecs
[3]);
1417 if(prefp
==binascii
.unhexlify(formatspecs
[4])):
1418 filetype
= formatspecs
[2];
1420 prefp
= catfp
.read(9);
1421 if(prefp
==binascii
.unhexlify("894c5a4f000d0a1a0a")):
1424 prefp
= catfp
.read(10);
1425 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
1426 filetype
= "tarfile";
1432 def CheckCompressionTypeFromString(instring
, formatspecs
=__file_format_list__
, closefp
=True):
1434 instringsfile
= BytesIO(instring
);
1436 instringsfile
= BytesIO(instring
.encode("UTF-8"));
1437 return CheckCompressionType(instringsfile
, formatspecs
, closefp
);
1439 def GetCompressionMimeType(infile
, formatspecs
=__file_format_list__
):
1440 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
1441 if(compresscheck
=="gzip" or compresscheck
=="gz"):
1442 return archivefile_gzip_mimetype
;
1443 if(compresscheck
=="bzip2" or compresscheck
=="bz2"):
1444 return archivefile_bzip2_mimetype
;
1445 if(compresscheck
=="zstd" or compresscheck
=="zstandard"):
1446 return archivefile_zstandard_mimetype
;
1447 if(compresscheck
=="lz4"):
1448 return archivefile_lz4_mimetype
;
1449 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
1450 return archivefile_lzop_mimetype
;
1451 if(compresscheck
=="lzma"):
1452 return archivefile_lzma_mimetype
;
1453 if(compresscheck
=="xz"):
1454 return archivefile_xz_mimetype
;
1455 if(compresscheck
=="catfile" or compresscheck
=="cat" or compresscheck
==formatspecs
[2]):
1456 return archivefile_cat_mimetype
;
1457 if(not compresscheck
):
1461 def UncompressArchiveFile(fp
, formatspecs
=__file_format_list__
):
1462 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
1464 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
1465 if(compresscheck
=="gzip"):
1470 catfp
= gzip
.GzipFile(fileobj
=fp
, mode
="rb");
1471 if(compresscheck
=="bzip2"):
1477 catfp
.write(bz2
.decompress(fp
.read()));
1478 if(compresscheck
=="zstd"):
1484 catfp
.write(zstandard
.decompress(fp
.read()));
1485 if(compresscheck
=="lz4"):
1491 catfp
.write(lz4
.frame
.decompress(fp
.read()));
1492 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
1498 catfp
.write(lzo
.decompress(fp
.read()));
1499 if(compresscheck
=="lzma" or compresscheck
=="xz"):
1505 catfp
.write(lzma
.decompress(fp
.read()));
1506 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[2]):
1508 if(not compresscheck
):
1514 with fp
as fpcontent
:
1516 catfp
.write(lzma
.decompress(fp
.read()));
1517 except lzma
.LZMAError
:
1521 create_alias_function("Uncompress", __file_format_name__
, "", UncompressArchiveFile
);
1523 def UncompressFile(infile
, formatspecs
=__file_format_list__
, mode
="rb"):
1524 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
1525 if(sys
.version_info
[0]==2 and compresscheck
):
1531 if(compresscheck
=="gzip"):
1537 filefp
= gzip
.open(infile
, mode
, encoding
="UTF-8");
1538 except (ValueError, TypeError) as e
:
1539 filefp
= gzip
.open(infile
, mode
);
1540 if(compresscheck
=="bzip2"):
1546 filefp
= bz2
.open(infile
, mode
, encoding
="UTF-8");
1547 except (ValueError, TypeError) as e
:
1548 filefp
= bz2
.open(infile
, mode
);
1549 if(compresscheck
=="zstd"):
1555 filefp
= zstandard
.open(infile
, mode
, encoding
="UTF-8");
1556 except (ValueError, TypeError) as e
:
1557 filefp
= zstandard
.open(infile
, mode
);
1558 if(compresscheck
=="lz4"):
1564 filefp
= lz4
.frame
.open(infile
, mode
, encoding
="UTF-8");
1565 except (ValueError, TypeError) as e
:
1566 filefp
= lz4
.frame
.open(infile
, mode
);
1567 if(compresscheck
=="lzo"):
1573 filefp
= lzo
.open(infile
, mode
, encoding
="UTF-8");
1574 except (ValueError, TypeError) as e
:
1575 filefp
= lzo
.open(infile
, mode
);
1576 if(compresscheck
=="lzma"):
1582 filefp
= lzma
.open(infile
, mode
, encoding
="UTF-8");
1583 except (ValueError, TypeError) as e
:
1584 filefp
= lzma
.open(infile
, mode
);
1585 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[2]):
1587 filefp
= open(infile
, mode
, encoding
="UTF-8");
1588 except (ValueError, TypeError) as e
:
1589 filefp
= open(infile
, mode
);
1590 if(not compresscheck
):
1592 filefp
= open(infile
, mode
, encoding
="UTF-8");
1593 except (ValueError, TypeError) as e
:
1594 filefp
= open(infile
, mode
);
1595 except FileNotFoundError
:
1599 def UncompressString(infile
):
1600 compresscheck
= CheckCompressionTypeFromString(infile
, formatspecs
, False);
1601 if(compresscheck
=="gzip"):
1606 fileuz
= gzip
.decompress(infile
);
1607 if(compresscheck
=="bzip2"):
1612 fileuz
= bz2
.decompress(infile
);
1613 if(compresscheck
=="zstd"):
1618 fileuz
= zstandard
.decompress(infile
);
1619 if(compresscheck
=="lz4"):
1624 fileuz
= lz4
.frame
.decompress(infile
);
1625 if(compresscheck
=="lzo"):
1630 fileuz
= lzo
.decompress(infile
);
1631 if(compresscheck
=="lzma"):
1636 fileuz
= lzma
.decompress(infile
);
1637 if(not compresscheck
):
1639 if(hasattr(fileuz
, 'decode')):
1640 fileuz
= fileuz
.decode("UTF-8");
1643 def UncompressStringAlt(infile
):
1644 filefp
= StringIO();
1645 outstring
= UncompressString(infile
);
1646 filefp
.write(outstring
);
1650 def CheckCompressionSubType(infile
, formatspecs
=__file_format_list__
):
1651 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
1652 if(not compresscheck
):
1653 fextname
= os
.path
.splitext(infile
)[1];
1654 if(fextname
==".gz"):
1655 compresscheck
= "gzip";
1656 elif(fextname
==".bz2"):
1657 compresscheck
= "bzip2";
1658 elif(fextname
==".zst"):
1659 compresscheck
= "zstd";
1660 elif(fextname
==".lz4"):
1661 compresscheck
= "lz4";
1662 elif(fextname
==".lzo" or fextname
==".lzop"):
1663 compresscheck
= "lzo";
1664 elif(fextname
==".lzma" or fextname
==".xz"):
1665 compresscheck
= "lzma";
1668 if(not compresscheck
):
1670 if(compresscheck
=="catfile"):
1672 if(compresscheck
==formatspecs
[2]):
1673 return formatspecs
[2];
1674 if(compresscheck
=="tarfile"):
1676 if(compresscheck
=="zipfile"):
1678 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1679 catfp
= UncompressArchiveFile(infile
, formatspecs
[2]);
1682 if(compresscheck
=="gzip"):
1687 catfp
= gzip
.GzipFile(infile
, "rb");
1688 if(compresscheck
=="bzip2"):
1693 catfp
= bz2
.BZ2File(infile
, "rb");
1694 if(compresscheck
=="lz4"):
1699 catfp
= lz4
.frame
.open(infile
, "rb");
1700 if(compresscheck
=="zstd"):
1705 catfp
= zstandard
.open(infile
, "rb");
1706 if(compresscheck
=="lzma" or compresscheck
=="xz"):
1711 catfp
= lzma
.open(infile
, "rb");
1712 except FileNotFoundError
:
1715 prefp
= catfp
.read(5);
1716 if(prefp
==binascii
.unhexlify("7573746172")):
1717 filetype
= "tarfile";
1719 prefp
= catfp
.read(7);
1720 if(prefp
==binascii
.unhexlify("43617446696c65")):
1721 filetype
= "catfile";
1723 prefp
= catfp
.read(formatspecs
[3]);
1724 if(prefp
==binascii
.unhexlify(formatspecs
[4])):
1725 filetype
= formatspecs
[2];
1727 prefp
= catfp
.read(10);
1728 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
1729 filetype
= "tarfile";
1734 def GZipCompress(data
, compresslevel
=9):
1739 tmpfp
= tempfile
.NamedTemporaryFile("wb", delete
=False);
1741 tmpfp
= gzip
.GzipFile(tmpfp
.name
, mode
="wb", compresslevel
=compresslevel
);
1745 catfp
= open(tmpfp
.name
, "rb");
1746 except FileNotFoundError
:
1748 catdata
= catfp
.read();
1752 def CompressArchiveFile(fp
, compression
="auto", compressionlevel
=None, formatspecs
=__file_format_list__
):
1753 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1754 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
1757 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
1759 if(compression
not in compressionlist
and compression
is None):
1760 compression
= "auto";
1761 if(compression
=="gzip"):
1767 if(compressionlevel
is None):
1768 compressionlevel
= 9;
1770 compressionlevel
= int(compressionlevel
);
1771 catfp
.write(gzip
.compress(fp
.read(), compresslevel
=compressionlevel
));
1772 if(compression
=="bzip2"):
1778 if(compressionlevel
is None):
1779 compressionlevel
= 9;
1781 compressionlevel
= int(compressionlevel
);
1782 catfp
.write(bz2
.compress(fp
.read(), compresslevel
=compressionlevel
));
1783 if(compression
=="lz4"):
1789 if(compressionlevel
is None):
1790 compressionlevel
= 9;
1792 compressionlevel
= int(compressionlevel
);
1793 catfp
.write(lz4
.frame
.compress(fp
.read(), compression_level
=compressionlevel
));
1794 if(compression
=="lzo" or compression
=="lzop"):
1800 if(compressionlevel
is None):
1801 compressionlevel
= 9;
1803 compressionlevel
= int(compressionlevel
);
1804 catfp
.write(lzo
.compress(fp
.read(), compresslevel
=compressionlevel
));
1805 if(compression
=="zstd"):
1811 if(compressionlevel
is None):
1812 compressionlevel
= 10;
1814 compressionlevel
= int(compressionlevel
);
1815 catfp
.write(zstandard
.compress(fp
.read(), level
=compressionlevel
));
1816 if(compression
=="lzma"):
1822 if(compressionlevel
is None):
1823 compressionlevel
= 9;
1825 compressionlevel
= int(compressionlevel
);
1826 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]));
1827 if(compression
=="xz"):
1833 if(compressionlevel
is None):
1834 compressionlevel
= 9;
1836 compressionlevel
= int(compressionlevel
);
1837 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]));
1838 if(compression
=="auto" or compression
is None):
1843 create_alias_function("Compress", __file_format_name__
, "", CompressArchiveFile
);
1845 def CompressOpenFile(outfile
, compressionlevel
=None):
1846 if(outfile
is None):
1848 fbasename
= os
.path
.splitext(outfile
)[0];
1849 fextname
= os
.path
.splitext(outfile
)[1];
1850 if(compressionlevel
is None and fextname
!=".zst"):
1851 compressionlevel
= 9;
1852 elif(compressionlevel
is None and fextname
==".zst"):
1853 compressionlevel
= 10;
1855 compressionlevel
= int(compressionlevel
);
1856 if(sys
.version_info
[0]==2):
1861 if(fextname
not in outextlistwd
):
1863 outfp
= open(outfile
, "wb", encoding
="UTF-8");
1864 except (ValueError, TypeError) as e
:
1865 outfp
= open(outfile
, "wb");
1866 elif(fextname
==".gz"):
1872 outfp
= gzip
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
1873 except (ValueError, TypeError) as e
:
1874 outfp
= gzip
.open(outfile
, mode
, compressionlevel
);
1875 elif(fextname
==".bz2"):
1881 outfp
= bz2
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
1882 except (ValueError, TypeError) as e
:
1883 outfp
= bz2
.open(outfile
, mode
, compressionlevel
);
1884 elif(fextname
==".zst"):
1890 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
), encoding
="UTF-8");
1891 except (ValueError, TypeError) as e
:
1892 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
));
1893 elif(fextname
==".xz"):
1899 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}], encoding
="UTF-8");
1900 except (ValueError, TypeError) as e
:
1901 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]);
1902 elif(fextname
==".lz4"):
1908 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
, encoding
="UTF-8");
1909 except (ValueError, TypeError) as e
:
1910 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
);
1911 elif(fextname
==".lzo"):
1917 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
, encoding
="UTF-8");
1918 except (ValueError, TypeError) as e
:
1919 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
);
1920 elif(fextname
==".lzma"):
1926 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}], encoding
="UTF-8");
1927 except (ValueError, TypeError) as e
:
1928 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]);
1929 except FileNotFoundError
:
1933 def GetDevMajorMinor(fdev
):
1935 if(hasattr(os
, "minor")):
1936 retdev
.append(os
.minor(fdev
));
1939 if(hasattr(os
, "major")):
1940 retdev
.append(os
.major(fdev
));
1945 def CheckSumSupport(checkfor
, guaranteed
=True):
1947 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
1949 hash_list
= sorted(list(hashlib
.algorithms_available
));
1950 checklistout
= sorted(hash_list
+ ['adler32', 'crc16', 'crc16_ansi', 'crc16_ibm', 'crc16_ccitt', 'crc32', 'crc64', 'crc64_ecma', 'crc64_iso', 'none']);
1951 if(checkfor
in checklistout
):
1956 def CheckSumSupportAlt(checkfor
, guaranteed
=True):
1958 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
1960 hash_list
= sorted(list(hashlib
.algorithms_available
));
1961 checklistout
= hash_list
;
1962 if(checkfor
in checklistout
):
1967 def PackArchiveFile(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1968 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1969 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1970 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1971 advancedlist
= True;
1972 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1973 outfile
= RemoveWindowsPath(outfile
);
1974 checksumtype
= checksumtype
.lower();
1975 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
1976 checksumtype
="crc32";
1977 if(checksumtype
=="none"):
1979 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
1981 if(compression
not in compressionlist
and compression
is None):
1982 compression
= "auto";
1984 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1985 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1986 if(os
.path
.exists(outfile
)):
1991 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1993 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1996 fbasename
= os
.path
.splitext(outfile
)[0];
1997 fextname
= os
.path
.splitext(outfile
)[1];
1998 catfp
= CompressOpenFile(outfile
, compressionlevel
);
1999 catver
= formatspecs
[6];
2000 fileheaderver
= str(int(catver
.replace(".", "")));
2001 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
2002 catfp
.write(fileheader
.encode('UTF-8'));
2005 for line
in sys
.stdin
:
2006 infilelist
.append(line
.strip());
2007 infilelist
= list(filter(None, infilelist
));
2008 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
2009 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
2011 with
UncompressFile(infiles
, formatspecs
, "r") as finfile
:
2012 for line
in finfile
:
2013 infilelist
.append(line
.strip());
2014 infilelist
= list(filter(None, infilelist
));
2016 if(isinstance(infiles
, (list, tuple, ))):
2017 infilelist
= list(filter(None, infiles
));
2018 elif(isinstance(infiles
, (str, ))):
2019 infilelist
= list(filter(None, [infiles
]));
2021 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
2023 GetDirList
= ListDir(infilelist
, followlink
, False);
2031 inodetocatinode
= {};
2032 fnumfiles
= format(int(len(GetDirList
)), 'x').lower();
2033 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2034 if(checksumtype
=="none" or checksumtype
==""):
2035 catfileheadercshex
= format(0, 'x').lower();
2036 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2037 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2038 elif(checksumtype
=="crc16_ccitt"):
2039 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2040 elif(checksumtype
=="adler32"):
2041 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2042 elif(checksumtype
=="crc32"):
2043 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2044 elif(checksumtype
=="crc64_ecma"):
2045 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2046 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2047 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2048 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2049 checksumoutstr
= hashlib
.new(checksumtype
);
2050 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
2051 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2053 catfileheadercshex
= format(0, 'x').lower
2054 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2055 catfp
.write(fnumfilesa
.encode('UTF-8'));
2058 os
.fsync(catfp
.fileno());
2059 except io
.UnsupportedOperation
:
2061 except AttributeError:
2063 for curfname
in GetDirList
:
2064 catfhstart
= catfp
.tell();
2065 if(re
.findall("^[.|/]", curfname
)):
2068 fname
= "./"+curfname
;
2070 VerbosePrintOut(fname
);
2071 if(not followlink
or followlink
is None):
2072 fstatinfo
= os
.lstat(fname
);
2074 fstatinfo
= os
.stat(fname
);
2075 fpremode
= fstatinfo
.st_mode
;
2076 finode
= fstatinfo
.st_ino
;
2077 flinkcount
= fstatinfo
.st_nlink
;
2079 if(stat
.S_ISREG(fpremode
)):
2081 elif(stat
.S_ISLNK(fpremode
)):
2083 elif(stat
.S_ISCHR(fpremode
)):
2085 elif(stat
.S_ISBLK(fpremode
)):
2087 elif(stat
.S_ISDIR(fpremode
)):
2089 elif(stat
.S_ISFIFO(fpremode
)):
2091 elif(stat
.S_ISSOCK(fpremode
)):
2093 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
2095 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
2097 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
2102 fcurfid
= format(int(curfid
), 'x').lower();
2103 if(not followlink
and finode
!=0):
2105 if(finode
in inodelist
):
2107 flinkname
= inodetofile
[finode
];
2108 fcurinode
= format(int(inodetocatinode
[finode
]), 'x').lower();
2109 if(finode
not in inodelist
):
2110 inodelist
.append(finode
);
2111 inodetofile
.update({finode
: fname
});
2112 inodetocatinode
.update({finode
: curinode
});
2113 fcurinode
= format(int(curinode
), 'x').lower();
2114 curinode
= curinode
+ 1;
2116 fcurinode
= format(int(curinode
), 'x').lower();
2117 curinode
= curinode
+ 1;
2118 curfid
= curfid
+ 1;
2120 flinkname
= os
.readlink(fname
);
2121 fdev
= fstatinfo
.st_dev
;
2122 getfdev
= GetDevMajorMinor(fdev
);
2123 fdev_minor
= getfdev
[0];
2124 fdev_major
= getfdev
[1];
2125 frdev
= fstatinfo
.st_dev
;
2126 if(hasattr(fstatinfo
, "st_rdev")):
2127 frdev
= fstatinfo
.st_rdev
;
2129 frdev
= fstatinfo
.st_dev
;
2130 getfrdev
= GetDevMajorMinor(frdev
);
2131 frdev_minor
= getfrdev
[0];
2132 frdev_major
= getfrdev
[1];
2133 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
2134 fsize
= format(int("0"), 'x').lower();
2135 elif(ftype
==0 or ftype
==7):
2136 fsize
= format(int(fstatinfo
.st_size
), 'x').lower();
2138 fsize
= format(int(fstatinfo
.st_size
)).lower();
2139 fatime
= format(int(fstatinfo
.st_atime
), 'x').lower();
2140 fmtime
= format(int(fstatinfo
.st_mtime
), 'x').lower();
2141 fctime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
2142 if(hasattr(fstatinfo
, "st_birthtime")):
2143 fbtime
= format(int(fstatinfo
.st_birthtime
), 'x').lower();
2145 fbtime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
2146 fmode
= format(int(fstatinfo
.st_mode
), 'x').lower();
2147 fchmode
= format(int(stat
.S_IMODE(fstatinfo
.st_mode
)), 'x').lower();
2148 ftypemod
= format(int(stat
.S_IFMT(fstatinfo
.st_mode
)), 'x').lower();
2149 fuid
= format(int(fstatinfo
.st_uid
), 'x').lower();
2150 fgid
= format(int(fstatinfo
.st_gid
), 'x').lower();
2155 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
2156 funame
= userinfo
.pw_name
;
2165 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
2166 fgname
= groupinfo
.gr_name
;
2171 fdev_minor
= format(int(fdev_minor
), 'x').lower();
2172 fdev_major
= format(int(fdev_major
), 'x').lower();
2173 frdev_minor
= format(int(frdev_minor
), 'x').lower();
2174 frdev_major
= format(int(frdev_major
), 'x').lower();
2175 finode
= format(int(finode
), 'x').lower();
2176 flinkcount
= format(int(flinkcount
), 'x').lower();
2177 if(hasattr(fstatinfo
, "st_file_attributes")):
2178 fwinattributes
= format(int(fstatinfo
.st_file_attributes
), 'x').lower();
2180 fwinattributes
= format(int(0), 'x').lower();
2181 fcontents
= "".encode('UTF-8');
2183 if(ftype
== 0 or ftype
== 7):
2184 with
open(fname
, "rb") as fpc
:
2186 chunk
= fpc
.read(chunk_size
);
2190 if(followlink
and (ftype
== 1 or ftype
== 2)):
2191 flstatinfo
= os
.stat(flinkname
);
2192 with
open(flinkname
, "rb") as fpc
:
2194 chunk
= fpc
.read(chunk_size
);
2198 ftypehex
= format(ftype
, 'x').lower();
2199 extrafields
= format(len(extradata
), 'x').lower();
2200 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
2201 if(len(extradata
)>0):
2202 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2203 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2204 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
];
2205 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
2206 catoutlenhex
= format(catoutlen
, 'x').lower();
2207 catoutlist
.insert(0, catoutlenhex
);
2208 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
2209 if(len(extradata
)>0):
2210 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2211 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
2212 if(checksumtype
=="none" or checksumtype
==""):
2213 catfileheadercshex
= format(0, 'x').lower();
2214 catfilecontentcshex
= format(0, 'x').lower();
2215 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2216 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2217 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
2218 elif(checksumtype
=="crc16_ccitt"):
2219 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2220 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
2221 elif(checksumtype
=="adler32"):
2222 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2223 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
2224 elif(checksumtype
=="crc32"):
2225 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2226 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
2227 elif(checksumtype
=="crc64_ecma"):
2228 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2229 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
2230 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2231 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2232 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
2233 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2234 checksumoutstr
= hashlib
.new(checksumtype
);
2235 checksumoutstr
.update("".encode('UTF-8'));
2236 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2237 checksumoutstr
= hashlib
.new(checksumtype
);
2238 checksumoutstr
.update(fcontents
);
2239 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
2241 catfileheadercshex
= format(0, 'x').lower();
2242 catfilecontentcshex
= format(0, 'x').lower();
2243 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2244 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2245 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
2246 if(checksumtype
=="none" or checksumtype
==""):
2247 catfileheadercshex
= format(0, 'x').lower();
2248 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2249 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2250 elif(checksumtype
=="crc16_ccitt"):
2251 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2252 elif(checksumtype
=="adler32"):
2253 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2254 elif(checksumtype
=="crc32"):
2255 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2256 elif(checksumtype
=="crc64_ecma"):
2257 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2258 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2259 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2260 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2261 checksumoutstr
= hashlib
.new(checksumtype
);
2262 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
2263 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2265 catfileheadercshex
= format(0, 'x').lower();
2266 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2267 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
2268 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
2269 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
2270 nullstrecd
= formatspecs
[5].encode('UTF-8');
2271 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
2272 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
2273 catfp
.write(catfileout
);
2276 os
.fsync(catfp
.fileno());
2277 except io
.UnsupportedOperation
:
2279 except AttributeError:
2281 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2282 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2285 os
.fsync(catfp
.fileno());
2286 except io
.UnsupportedOperation
:
2288 except AttributeError:
2292 if(hasattr(sys
.stdout
, "buffer")):
2293 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2295 shutil
.copyfileobj(catfp
, sys
.stdout
);
2296 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2297 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2299 upload_file_to_internet_file(catfp
, outfile
);
2307 create_alias_function("Pack", __file_format_name__
, "", PackArchiveFile
);
2309 if(hasattr(shutil
, "register_archive_format")):
2310 def PackArchiveFileFunc(archive_name
, source_dir
, **kwargs
):
2311 return PackArchiveFile(source_dir
, archive_name
, False, "auto", None, False, "crc32", [], __file_format_delimiter__
, False, False);
2312 create_alias_function("Pack", __file_format_name__
, "Func", PackArchiveFileFunc
);
2314 def PackArchiveFileFromDirList(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2315 return PackArchiveFile(infiles
, outfile
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
2317 def PackArchiveFileFromTarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2318 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2319 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2320 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
2321 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2322 outfile
= RemoveWindowsPath(outfile
);
2323 checksumtype
= checksumtype
.lower();
2324 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2325 checksumtype
="crc32";
2326 if(checksumtype
=="none"):
2328 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2330 if(compression
not in compressionlist
and compression
is None):
2331 compression
= "auto";
2333 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2334 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2335 if(os
.path
.exists(outfile
)):
2340 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2342 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2345 fbasename
= os
.path
.splitext(outfile
)[0];
2346 fextname
= os
.path
.splitext(outfile
)[1];
2347 catfp
= CompressOpenFile(outfile
, compressionlevel
);
2348 catver
= formatspecs
[6];
2349 fileheaderver
= str(int(catver
.replace(".", "")));
2350 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
2351 catfp
.write(fileheader
.encode('UTF-8'));
2357 inodetocatinode
= {};
2360 if(hasattr(sys
.stdin
, "buffer")):
2361 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
2363 shutil
.copyfileobj(sys
.stdin
, infile
);
2368 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
2369 infile
= download_file_from_internet_file(infile
);
2374 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2376 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
2378 if(not tarfile
.is_tarfile(infile
)):
2380 except AttributeError:
2381 if(not is_tarfile(infile
)):
2386 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2387 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
2389 tarfp
= tarfile
.open(infile
, "r");
2390 except FileNotFoundError
:
2392 fnumfiles
= format(int(len(tarfp
.getmembers())), 'x').lower();
2393 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2394 if(checksumtype
=="none" or checksumtype
==""):
2395 catfileheadercshex
= format(0, 'x').lower();
2396 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2397 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2398 elif(checksumtype
=="crc16_ccitt"):
2399 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2400 elif(checksumtype
=="adler32"):
2401 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2402 elif(checksumtype
=="crc32"):
2403 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2404 elif(checksumtype
=="crc64_ecma"):
2405 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2406 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2407 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2408 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2409 checksumoutstr
= hashlib
.new(checksumtype
);
2410 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
2411 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2413 catfileheadercshex
= format(0, 'x').lower();
2414 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2415 catfp
.write(fnumfilesa
.encode('UTF-8'));
2418 os
.fsync(catfp
.fileno());
2419 except io
.UnsupportedOperation
:
2421 except AttributeError:
2423 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
2424 catfhstart
= catfp
.tell();
2425 if(re
.findall("^[.|/]", member
.name
)):
2426 fname
= member
.name
;
2428 fname
= "./"+member
.name
;
2430 VerbosePrintOut(fname
);
2431 fpremode
= member
.mode
;
2432 ffullmode
= member
.mode
;
2436 ffullmode
= member
.mode
+ stat
.S_IFREG
;
2438 elif(member
.isdev()):
2439 ffullmode
= member
.mode
;
2441 elif(member
.islnk()):
2442 ffullmode
= member
.mode
+ stat
.S_IFREG
;
2444 elif(member
.issym()):
2445 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
2447 elif(member
.ischr()):
2448 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
2450 elif(member
.isblk()):
2451 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
2453 elif(member
.isdir()):
2454 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
2456 elif(member
.isfifo()):
2457 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
2459 elif(member
.issparse()):
2460 ffullmode
= member
.mode
;
2463 ffullmode
= member
.mode
;
2466 fcurfid
= format(int(curfid
), 'x').lower();
2467 fcurinode
= format(int(0), 'x').lower();
2468 curfid
= curfid
+ 1;
2470 flinkname
= member
.linkname
;
2471 fdev_minor
= format(int(member
.devminor
), 'x').lower();
2472 fdev_major
= format(int(member
.devmajor
), 'x').lower();
2473 frdev_minor
= format(int(member
.devminor
), 'x').lower();
2474 frdev_major
= format(int(member
.devmajor
), 'x').lower();
2475 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
2476 fsize
= format(int("0"), 'x').lower();
2477 elif(ftype
==0 or ftype
==7):
2478 fsize
= format(int(member
.size
), 'x').lower();
2480 fsize
= format(int(member
.size
), 'x').lower();
2481 fatime
= format(int(member
.mtime
), 'x').lower();
2482 fmtime
= format(int(member
.mtime
), 'x').lower();
2483 fctime
= format(int(member
.mtime
), 'x').lower();
2484 fbtime
= format(int(member
.mtime
), 'x').lower();
2485 fmode
= format(int(ffullmode
), 'x').lower();
2486 fchmode
= format(int(stat
.S_IMODE(ffullmode
)), 'x').lower();
2487 ftypemod
= format(int(stat
.S_IFMT(ffullmode
)), 'x').lower();
2488 fuid
= format(int(member
.uid
), 'x').lower();
2489 fgid
= format(int(member
.gid
), 'x').lower();
2490 funame
= member
.uname
;
2491 fgname
= member
.gname
;
2492 flinkcount
= format(int(flinkcount
), 'x').lower();
2493 fwinattributes
= format(int(0), 'x').lower();
2494 fcontents
= "".encode('UTF-8');
2496 if(ftype
== 0 or ftype
== 7):
2497 with tarfp
.extractfile(member
) as fpc
:
2499 chunk
= fpc
.read(chunk_size
);
2503 ftypehex
= format(ftype
, 'x').lower();
2504 extrafields
= format(len(extradata
), 'x').lower();
2505 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
2506 if(len(extradata
)>0):
2507 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2508 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2509 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
];
2510 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
2511 catoutlenhex
= format(catoutlen
, 'x').lower();
2512 catoutlist
.insert(0, catoutlenhex
);
2513 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
2514 if(len(extradata
)>0):
2515 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2516 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
2517 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
2518 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
2519 if(checksumtype
=="none" or checksumtype
==""):
2520 catfileheadercshex
= format(0, 'x').lower();
2521 catfilecontentcshex
= format(0, 'x').lower();
2522 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2523 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2524 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
2525 elif(checksumtype
=="crc16_ccitt"):
2526 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2527 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
2528 elif(checksumtype
=="adler32"):
2529 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2530 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
2531 elif(checksumtype
=="crc32"):
2532 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2533 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
2534 elif(checksumtype
=="crc64_ecma"):
2535 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2536 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
2537 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2538 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2539 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
2540 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2541 checksumoutstr
= hashlib
.new(checksumtype
);
2542 checksumoutstr
.update("".encode('UTF-8'));
2543 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2544 checksumoutstr
= hashlib
.new(checksumtype
);
2545 checksumoutstr
.update(fcontents
);
2546 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
2548 catfileheadercshex
= format(0, 'x').lower();
2549 catfilecontentcshex
= format(0, 'x').lower();
2550 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2551 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2552 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
2553 if(checksumtype
=="none" or checksumtype
==""):
2554 catfileheadercshex
= format(0, 'x').lower();
2555 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2556 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2557 elif(checksumtype
=="crc16_ccitt"):
2558 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2559 elif(checksumtype
=="adler32"):
2560 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2561 elif(checksumtype
=="crc32"):
2562 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2563 elif(checksumtype
=="crc64_ecma"):
2564 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2565 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2566 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2567 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2568 checksumoutstr
= hashlib
.new(checksumtype
);
2569 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
2570 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2572 catfileheadercshex
= format(0, 'x').lower();
2573 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2574 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
2575 nullstrecd
= formatspecs
[5].encode('UTF-8');
2576 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
2577 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
2578 catfp
.write(catfileout
);
2581 os
.fsync(catfp
.fileno());
2582 except io
.UnsupportedOperation
:
2584 except AttributeError:
2586 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2587 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2590 os
.fsync(catfp
.fileno());
2591 except io
.UnsupportedOperation
:
2593 except AttributeError:
2597 if(hasattr(sys
.stdout
, "buffer")):
2598 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2600 shutil
.copyfileobj(catfp
, sys
.stdout
);
2601 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2602 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2604 upload_file_from_internet_file(catfp
, outfile
);
2612 create_alias_function("Pack", __file_format_name__
, "FromTarFile", PackArchiveFileFromTarFile
);
2614 def PackArchiveFileFromZipFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2615 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2616 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2617 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
2618 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2619 outfile
= RemoveWindowsPath(outfile
);
2620 checksumtype
= checksumtype
.lower();
2621 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2622 checksumtype
="crc32";
2623 if(checksumtype
=="none"):
2625 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2627 if(compression
not in compressionlist
and compression
is None):
2628 compression
= "auto";
2630 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2631 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2632 if(os
.path
.exists(outfile
)):
2637 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2639 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2642 fbasename
= os
.path
.splitext(outfile
)[0];
2643 fextname
= os
.path
.splitext(outfile
)[1];
2644 catfp
= CompressOpenFile(outfile
, compressionlevel
);
2645 catver
= formatspecs
[6];
2646 fileheaderver
= str(int(catver
.replace(".", "")));
2647 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
2648 catfp
.write(fileheader
.encode('UTF-8'));
2654 inodetocatinode
= {};
2657 if(hasattr(sys
.stdin
, "buffer")):
2658 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
2660 shutil
.copyfileobj(sys
.stdin
, infile
);
2665 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
2666 infile
= download_file_from_internet_file(infile
);
2671 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2675 if(not zipfile
.is_zipfile(infile
)):
2678 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
2679 except FileNotFoundError
:
2681 ziptest
= zipfp
.testzip();
2683 VerbosePrintOut("Bad file found!");
2684 fnumfiles
= format(int(len(zipfp
.infolist())), 'x').lower();
2685 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2686 if(checksumtype
=="none" or checksumtype
==""):
2687 catfileheadercshex
= format(0, 'x').lower();
2688 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2689 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2690 elif(checksumtype
=="crc16_ccitt"):
2691 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2692 elif(checksumtype
=="adler32"):
2693 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2694 elif(checksumtype
=="crc32"):
2695 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2696 elif(checksumtype
=="crc64_ecma"):
2697 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2698 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2699 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2700 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2701 checksumoutstr
= hashlib
.new(checksumtype
);
2702 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
2703 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2705 catfileheadercshex
= format(0, 'x').lower();
2706 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2707 catfp
.write(fnumfilesa
.encode('UTF-8'));
2710 os
.fsync(catfp
.fileno());
2711 except io
.UnsupportedOperation
:
2713 except AttributeError:
2715 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
2716 catfhstart
= catfp
.tell();
2717 if(re
.findall("^[.|/]", member
.filename
)):
2718 fname
= member
.filename
;
2720 fname
= "./"+member
.filename
;
2721 zipinfo
= zipfp
.getinfo(member
.filename
);
2723 VerbosePrintOut(fname
);
2724 if(not member
.is_dir()):
2725 fpremode
= int(stat
.S_IFREG
+ 438);
2726 elif(member
.is_dir()):
2727 fpremode
= int(stat
.S_IFDIR
+ 511);
2730 if(not member
.is_dir()):
2732 elif(member
.is_dir()):
2735 fcurfid
= format(int(curfid
), 'x').lower();
2736 fcurinode
= format(int(0), 'x').lower();
2737 curfid
= curfid
+ 1;
2738 fdev_minor
= format(int(0), 'x').lower();
2739 fdev_major
= format(int(0), 'x').lower();
2740 frdev_minor
= format(int(0), 'x').lower();
2741 frdev_major
= format(int(0), 'x').lower();
2743 fsize
= format(int("0"), 'x').lower();
2745 fsize
= format(int(member
.file_size
), 'x').lower();
2747 fsize
= format(int(member
.file_size
), 'x').lower();
2748 fatime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2749 fmtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2750 fctime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2751 fbtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2752 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
2753 fwinattributes
= format(int(zipinfo
.external_attr
), 'x').lower();
2754 if(not member
.is_dir()):
2755 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
2756 fchmode
= stat
.S_IMODE(fmode
);
2757 ftypemod
= stat
.S_IFMT(fmode
);
2758 elif(member
.is_dir()):
2759 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
2760 fchmode
= stat
.S_IMODE(fmode
);
2761 ftypemod
= stat
.S_IFMT(fmode
);
2762 elif(zipinfo
.create_system
==3):
2763 fwinattributes
= format(int(0), 'x').lower();
2764 fmode
= format(int(zipinfo
.external_attr
), 'x').lower();
2765 fchmode
= stat
.S_IMODE(fmode
);
2766 ftypemod
= stat
.S_IFMT(fmode
);
2768 fwinattributes
= format(int(0), 'x').lower();
2769 if(not member
.is_dir()):
2770 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
2771 fchmode
= stat
.S_IMODE(fmode
);
2772 ftypemod
= stat
.S_IFMT(fmode
);
2773 elif(member
.is_dir()):
2774 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
2775 fchmode
= stat
.S_IMODE(fmode
);
2776 ftypemod
= stat
.S_IFMT(fmode
);
2778 fuid
= format(int(os
.getuid()), 'x').lower();
2779 except AttributeError:
2780 fuid
= format(int(0), 'x').lower();
2782 fuid
= format(int(0), 'x').lower();
2784 fgid
= format(int(os
.getgid()), 'x').lower();
2785 except AttributeError:
2786 fgid
= format(int(0), 'x').lower();
2788 fgid
= format(int(0), 'x').lower();
2792 userinfo
= pwd
.getpwuid(os
.getuid());
2793 funame
= userinfo
.pw_name
;
2796 except AttributeError:
2804 groupinfo
= grp
.getgrgid(os
.getgid());
2805 fgname
= groupinfo
.gr_name
;
2808 except AttributeError:
2812 fcontents
= "".encode('UTF-8');
2814 fcontents
= zipfp
.read(member
.filename
);
2815 ftypehex
= format(ftype
, 'x').lower();
2816 extrafields
= format(len(extradata
), 'x').lower();
2817 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
2818 if(len(extradata
)>0):
2819 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2820 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2821 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
];
2822 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
2823 catoutlenhex
= format(catoutlen
, 'x').lower();
2824 catoutlist
.insert(0, catoutlenhex
);
2825 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
2826 if(len(extradata
)>0):
2827 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2828 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
2829 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
2830 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
2831 if(checksumtype
=="none" or checksumtype
==""):
2832 catfileheadercshex
= format(0, 'x').lower();
2833 catfilecontentcshex
= format(0, 'x').lower();
2834 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2835 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2836 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
2837 elif(checksumtype
=="crc16_ccitt"):
2838 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2839 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
2840 elif(checksumtype
=="adler32"):
2841 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2842 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
2843 elif(checksumtype
=="crc32"):
2844 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2845 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
2846 elif(checksumtype
=="crc64_ecma"):
2847 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2848 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
2849 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2850 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2851 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
2852 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2853 checksumoutstr
= hashlib
.new(checksumtype
);
2854 checksumoutstr
.update("".encode('UTF-8'));
2855 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2856 checksumoutstr
= hashlib
.new(checksumtype
);
2857 checksumoutstr
.update(fcontents
);
2858 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
2860 catfileheadercshex
= format(0, 'x').lower();
2861 catfilecontentcshex
= format(0, 'x').lower();
2862 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2863 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2864 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
2865 if(checksumtype
=="none" or checksumtype
==""):
2866 catfileheadercshex
= format(0, 'x').lower();
2867 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2868 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2869 elif(checksumtype
=="crc16_ccitt"):
2870 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2871 elif(checksumtype
=="adler32"):
2872 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2873 elif(checksumtype
=="crc32"):
2874 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2875 elif(checksumtype
=="crc64_ecma"):
2876 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2877 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2878 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2879 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2880 checksumoutstr
= hashlib
.new(checksumtype
);
2881 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
2882 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2884 catfileheadercshex
= format(0, 'x').lower();
2885 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2886 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
2887 nullstrecd
= formatspecs
[5].encode('UTF-8');
2888 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
2889 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
2890 catfp
.write(catfileout
);
2893 os
.fsync(catfp
.fileno());
2894 except io
.UnsupportedOperation
:
2896 except AttributeError:
2898 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2899 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2902 os
.fsync(catfp
.fileno());
2903 except io
.UnsupportedOperation
:
2905 except AttributeError:
2909 if(hasattr(sys
.stdout
, "buffer")):
2910 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2912 shutil
.copyfileobj(catfp
, sys
.stdout
);
2913 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2914 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2916 upload_file_to_internet_file(catfp
, outfile
);
2924 create_alias_function("Pack", __file_format_name__
, "FromZipFile", PackArchiveFileFromZipFile
);
2926 if(not rarfile_support
):
2927 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2930 if(rarfile_support
):
2931 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2932 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2933 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2934 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
2935 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2936 outfile
= RemoveWindowsPath(outfile
);
2937 checksumtype
= checksumtype
.lower();
2938 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2939 checksumtype
="crc32";
2940 if(checksumtype
=="none"):
2942 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2944 if(compression
not in compressionlist
and compression
is None):
2945 compression
= "auto";
2947 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2948 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2949 if(os
.path
.exists(outfile
)):
2954 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2956 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2959 fbasename
= os
.path
.splitext(outfile
)[0];
2960 fextname
= os
.path
.splitext(outfile
)[1];
2961 catfp
= CompressOpenFile(outfile
, compressionlevel
);
2962 catver
= formatspecs
[6];
2963 fileheaderver
= str(int(catver
.replace(".", "")));
2964 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
2965 catfp
.write(fileheader
.encode('UTF-8'));
2971 inodetocatinode
= {};
2972 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2974 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
2976 rarfp
= rarfile
.RarFile(infile
, "r");
2977 rartest
= rarfp
.testrar();
2979 VerbosePrintOut("Bad file found!");
2980 fnumfiles
= format(int(len(rarfp
.infolist())), 'x').lower();
2981 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2982 if(checksumtype
=="none" or checksumtype
==""):
2983 catfileheadercshex
= format(0, 'x').lower();
2984 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2985 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2986 elif(checksumtype
=="crc16_ccitt"):
2987 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2988 elif(checksumtype
=="adler32"):
2989 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2990 elif(checksumtype
=="crc32"):
2991 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2992 elif(checksumtype
=="crc64_ecma"):
2993 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2994 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2995 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2996 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2997 checksumoutstr
= hashlib
.new(checksumtype
);
2998 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
2999 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3001 catfileheadercshex
= format(0, 'x').lower();
3002 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3003 catfp
.write(fnumfilesa
.encode('UTF-8'));
3006 os
.fsync(catfp
.fileno());
3007 except io
.UnsupportedOperation
:
3009 except AttributeError:
3011 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
3014 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
3017 member
.external_attr
3019 except AttributeError:
3021 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
3024 member
.external_attr
3026 except AttributeError:
3031 catfhstart
= catfp
.tell();
3032 if(re
.findall("^[.|/]", member
.filename
)):
3033 fname
= member
.filename
;
3035 fname
= "./"+member
.filename
;
3036 rarinfo
= rarfp
.getinfo(member
.filename
);
3038 VerbosePrintOut(fname
);
3039 if(is_unix
and member
.external_attr
!=0):
3040 fpremode
= int(member
.external_attr
);
3041 elif(member
.is_file()):
3042 fpremode
= int(stat
.S_IFREG
+ 438);
3043 elif(member
.is_symlink()):
3044 fpremode
= int(stat
.S_IFLNK
+ 438);
3045 elif(member
.is_dir()):
3046 fpremode
= int(stat
.S_IFDIR
+ 511);
3047 if(is_windows
and member
.external_attr
!=0):
3048 fwinattributes
= format(int(member
.external_attr
), 'x').lower();
3050 fwinattributes
= format(int(0), 'x').lower();
3053 if(member
.is_file()):
3055 elif(member
.is_symlink()):
3057 elif(member
.is_dir()):
3061 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
3062 fcurfid
= format(int(curfid
), 'x').lower();
3063 fcurinode
= format(int(0), 'x').lower();
3064 curfid
= curfid
+ 1;
3065 fdev_minor
= format(int(0), 'x').lower();
3066 fdev_major
= format(int(0), 'x').lower();
3067 frdev_minor
= format(int(0), 'x').lower();
3068 frdev_major
= format(int(0), 'x').lower();
3070 fsize
= format(int("0"), 'x').lower();
3072 fsize
= format(int(member
.file_size
), 'x').lower();
3074 fsize
= format(int(member
.file_size
), 'x').lower();
3077 fatime
= format(int(member
.atime
.timestamp()), 'x').lower();
3079 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3080 except AttributeError:
3081 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3082 fmtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3085 fctime
= format(int(member
.ctime
.timestamp()), 'x').lower();
3087 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3088 except AttributeError:
3089 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3090 fbtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3091 if(is_unix
and member
.external_attr
!=0):
3092 fmode
= format(int(member
.external_attr
), 'x').lower();
3093 fchmode
= format(int(stat
.S_IMODE(member
.external_attr
)), 'x').lower();
3094 ftypemod
= format(int(stat
.S_IFMT(member
.external_attr
)), 'x').lower();
3095 elif(member
.is_file()):
3096 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
3097 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
3098 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
3099 elif(member
.is_symlink()):
3100 fmode
= format(int(stat
.S_IFLNK
+ 438), 'x').lower();
3101 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
3102 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
3103 elif(member
.is_dir()):
3104 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
3105 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3106 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3108 fuid
= format(int(os
.getuid()), 'x').lower();
3109 except AttributeError:
3110 fuid
= format(int(0), 'x').lower();
3112 fuid
= format(int(0), 'x').lower();
3114 fgid
= format(int(os
.getgid()), 'x').lower();
3115 except AttributeError:
3116 fgid
= format(int(0), 'x').lower();
3118 fgid
= format(int(0), 'x').lower();
3122 userinfo
= pwd
.getpwuid(os
.getuid());
3123 funame
= userinfo
.pw_name
;
3126 except AttributeError:
3134 groupinfo
= grp
.getgrgid(os
.getgid());
3135 fgname
= groupinfo
.gr_name
;
3138 except AttributeError:
3142 fcontents
= "".encode('UTF-8');
3144 fcontents
= rarfp
.read(member
.filename
);
3145 ftypehex
= format(ftype
, 'x').lower();
3146 extrafields
= format(len(extradata
), 'x').lower();
3147 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
3148 if(len(extradata
)>0):
3149 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3150 extrasizelen
= format(len(extrasizestr
), 'x').lower();
3151 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
];
3152 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
3153 catoutlenhex
= format(catoutlen
, 'x').lower();
3154 catoutlist
.insert(0, catoutlenhex
);
3155 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
3156 if(len(extradata
)>0):
3157 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3158 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
3159 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
3160 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
3161 if(checksumtype
=="none" or checksumtype
==""):
3162 catfileheadercshex
= format(0, 'x').lower();
3163 catfilecontentcshex
= format(0, 'x').lower();
3164 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3165 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3166 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
3167 elif(checksumtype
=="crc16_ccitt"):
3168 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3169 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
3170 elif(checksumtype
=="adler32"):
3171 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3172 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
3173 elif(checksumtype
=="crc32"):
3174 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3175 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
3176 elif(checksumtype
=="crc64_ecma"):
3177 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3178 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
3179 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3180 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3181 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
3182 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3183 checksumoutstr
= hashlib
.new(checksumtype
);
3184 checksumoutstr
.update("".encode('UTF-8'));
3185 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3186 checksumoutstr
= hashlib
.new(checksumtype
);
3187 checksumoutstr
.update(fcontents
);
3188 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
3190 catfileheadercshex
= format(0, 'x').lower();
3191 catfilecontentcshex
= format(0, 'x').lower();
3192 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
3193 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
3194 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
3195 if(checksumtype
=="none" or checksumtype
==""):
3196 catfileheadercshex
= format(0, 'x').lower()
3197 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3198 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower()
3199 elif(checksumtype
=="crc16_ccitt"):
3200 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower()
3201 elif(checksumtype
=="adler32"):
3202 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower()
3203 elif(checksumtype
=="crc32"):
3204 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower()
3205 elif(checksumtype
=="crc64_ecma"):
3206 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
3207 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3208 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
3209 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3210 checksumoutstr
= hashlib
.new(checksumtype
)
3211 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'))
3212 catfileheadercshex
= checksumoutstr
.hexdigest().lower()
3214 catfileheadercshex
= format(0, 'x').lower()
3215 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5])
3216 catfileoutstrecd
= catfileoutstr
.encode('UTF-8')
3217 nullstrecd
= formatspecs
[5].encode('UTF-8')
3218 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
3219 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
)
3220 catfp
.write(catfileout
)
3223 os
.fsync(catfp
.fileno())
3224 except io
.UnsupportedOperation
:
3226 except AttributeError:
3228 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3229 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
)
3232 os
.fsync(catfp
.fileno())
3233 except io
.UnsupportedOperation
:
3235 except AttributeError:
3239 if(hasattr(sys
.stdout
, "buffer")):
3240 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
3242 shutil
.copyfileobj(catfp
, sys
.stdout
);
3243 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3244 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
3246 upload_file_to_internet_file(catfp
, outfile
);
3254 create_alias_function("Pack", __file_format_name__
, "FromRarFile", PackArchiveFileFromRarFile
);
3256 def ArchiveFileSeekToFileNum(infile
, seekto
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3257 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3260 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3261 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
3262 if(checkcompressfile
=="tarfile"):
3263 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3264 if(checkcompressfile
=="zipfile"):
3265 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3266 if(rarfile_support
and checkcompressfile
=="rarfile"):
3267 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3268 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3275 if(hasattr(sys
.stdin
, "buffer")):
3276 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
3278 shutil
.copyfileobj(sys
.stdin
, catfp
);
3280 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3284 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
3285 catfp
= download_file_from_internet_file(infile
);
3287 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3292 infile
= RemoveWindowsPath(infile
);
3293 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
3294 if(checkcompressfile
=="tarfile"):
3295 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3296 if(checkcompressfile
=="zipfile"):
3297 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3298 if(rarfile_support
and checkcompressfile
=="rarfile"):
3299 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3300 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3302 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
3303 if(not compresscheck
):
3304 fextname
= os
.path
.splitext(infile
)[1];
3305 if(fextname
==".gz"):
3306 compresscheck
= "gzip";
3307 elif(fextname
==".bz2"):
3308 compresscheck
= "bzip2";
3309 elif(fextname
==".zst"):
3310 compresscheck
= "zstd";
3311 elif(fextname
==".lz4" or fextname
==".clz4"):
3312 compresscheck
= "lz4";
3313 elif(fextname
==".lzo" or fextname
==".lzop"):
3314 compresscheck
= "lzo";
3315 elif(fextname
==".lzma" or fextname
==".xz"):
3316 compresscheck
= "lzma";
3319 if(not compresscheck
):
3321 catfp
= UncompressFile(infile
, formatspecs
, "rb");
3326 SeekToEndOfFile(catfp);
3328 SeekToEndOfFile(catfp);
3329 CatSize = catfp.tell();
3330 CatSizeEnd = CatSize;
3338 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
3339 catstring
= catheader
[0];
3340 catversion
= re
.findall(r
"([\d]+)$", catstring
);
3341 fprenumfiles
= catheader
[1];
3342 fnumfiles
= int(fprenumfiles
, 16);
3343 fprechecksumtype
= catheader
[2];
3344 fprechecksum
= catheader
[3];
3345 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
3346 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3347 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
3348 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
3349 catfileheadercshex
= format(0, 'x').lower();
3350 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
3351 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3352 elif(fprechecksumtype
=="crc16_ccitt"):
3353 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3354 elif(fprechecksumtype
=="adler32"):
3355 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3356 elif(fprechecksumtype
=="crc32"):
3357 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3358 elif(fprechecksumtype
=="crc64_ecma"):
3359 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3360 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
3361 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3362 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
3363 checksumoutstr
= hashlib
.new(fprechecksumtype
);
3364 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3365 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3367 catfileheadercshex
= format(0, 'x').lower();
3368 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3369 fheadtell
= len(fileheader
);
3370 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
3371 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
3373 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
3374 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
3375 if(seekto
>=fnumfiles
):
3376 seekto
= fnumfiles
- 1;
3382 prefhstart
= catfp
.tell();
3384 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3386 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
3387 prefheadsize
= int(preheaderdata
[0], 16);
3388 prefnumfields
= int(preheaderdata
[1], 16);
3389 preftype
= int(preheaderdata
[2], 16);
3390 if(re
.findall("^[.|/]", preheaderdata
[3])):
3391 prefname
= preheaderdata
[3];
3393 prefname
= "./"+preheaderdata
[3];
3394 prefbasedir
= os
.path
.dirname(prefname
);
3395 preflinkname
= preheaderdata
[4];
3396 prefsize
= int(preheaderdata
[5], 16);
3397 prefatime
= int(preheaderdata
[6], 16);
3398 prefmtime
= int(preheaderdata
[7], 16);
3399 prefctime
= int(preheaderdata
[8], 16);
3400 prefbtime
= int(preheaderdata
[9], 16);
3401 prefmode
= int(preheaderdata
[10], 16);
3402 prefchmode
= stat
.S_IMODE(prefmode
);
3403 preftypemod
= stat
.S_IFMT(prefmode
);
3404 prefwinattributes
= int(preheaderdata
[11], 16);
3405 prefuid
= int(preheaderdata
[12], 16);
3406 prefuname
= preheaderdata
[13];
3407 prefgid
= int(preheaderdata
[14], 16);
3408 prefgname
= preheaderdata
[15];
3409 fid
= int(preheaderdata
[16], 16);
3410 finode
= int(preheaderdata
[17], 16);
3411 flinkcount
= int(preheaderdata
[18], 16);
3412 prefdev_minor
= int(preheaderdata
[19], 16);
3413 prefdev_major
= int(preheaderdata
[20], 16);
3414 prefrdev_minor
= int(preheaderdata
[22], 16);
3415 prefrdev_major
= int(preheaderdata
[23], 16);
3416 prefextrasize
= int(preheaderdata
[24], 16);
3417 prefextrafields
= int(preheaderdata
[25], 16);
3418 extrafieldslist
= [];
3420 extraend
= extrastart
+ prefextrafields
;
3421 extrafieldslist
= [];
3422 if(extrastart
<extraend
):
3423 extrafieldslist
.append(preheaderdata
[extrastart
]);
3424 extrastart
= extrastart
+ 1;
3425 prefchecksumtype
= preheaderdata
[extrastart
].lower();
3426 prefcs
= preheaderdata
[extrastart
+ 1].lower();
3427 prefccs
= preheaderdata
[extrastart
+ 2].lower();
3429 hcmax
= len(preheaderdata
) - 2;
3432 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
3434 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
3436 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
3437 prenewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
3438 elif(prefchecksumtype
=="adler32"):
3439 prenewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3440 elif(prefchecksumtype
=="crc32"):
3441 prenewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3442 elif(prefchecksumtype
=="crc64_ecma"):
3443 prenewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3444 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
3445 prenewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3446 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
3447 checksumoutstr
= hashlib
.new(prefchecksumtype
);
3448 checksumoutstr
.update(hout
.encode('UTF-8'));
3449 prenewfcs
= checksumoutstr
.hexdigest().lower();
3450 if(prefcs
!=prenewfcs
and not skipchecksum
):
3451 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
3453 valid_archive
= False;
3454 invalid_archive
= True;
3455 prefhend
= catfp
.tell() - 1;
3456 prefcontentstart
= catfp
.tell();
3458 pyhascontents
= False;
3460 prefcontents
= catfp
.read(prefsize
);
3461 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
3463 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
3464 prenewfccs
= format(crc16(prefcontents
) & 0xffff, '04x').lower();
3465 elif(prefchecksumtype
=="crc16_ccitt"):
3466 prenewfcs
= format(crc16_ccitt(prefcontents
) & 0xffff, '04x').lower();
3467 elif(prefchecksumtype
=="adler32"):
3468 prenewfccs
= format(zlib
.adler32(prefcontents
) & 0xffffffff, '08x').lower();
3469 elif(prefchecksumtype
=="crc32"):
3470 prenewfccs
= format(crc32(prefcontents
) & 0xffffffff, '08x').lower();
3471 elif(prefchecksumtype
=="crc64_ecma"):
3472 prenewfcs
= format(crc64_ecma(prefcontents
) & 0xffffffffffffffff, '016x').lower();
3473 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
3474 prenewfcs
= format(crc64_iso(prefcontents
) & 0xffffffffffffffff, '016x').lower();
3475 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
3476 checksumoutstr
= hashlib
.new(prefchecksumtype
);
3477 checksumoutstr
.update(prefcontents
);
3478 prenewfccs
= checksumoutstr
.hexdigest().lower();
3479 pyhascontents
= True;
3480 if(prefccs
!=prenewfccs
and not skipchecksum
):
3481 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
3485 catfp
.seek(seekstart
, 0);
3487 catfheadsize
= int(preheaderdata
[0], 16);
3488 catfnumfields
= int(preheaderdata
[1], 16);
3489 catftype
= int(preheaderdata
[2], 16);
3490 if(re
.findall("^[.|/]", preheaderdata
[3])):
3491 catfname
= preheaderdata
[3];
3493 catfname
= "./"+preheaderdata
[3];
3494 catflinkname
= preheaderdata
[4];
3495 catfsize
= int(preheaderdata
[5], 16);
3496 catfbasedir
= os
.path
.dirname(catfname
);
3497 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
3499 catlist
.update({'catfp': catfp
});
3504 create_alias_function("", __file_format_name__
, "SeekToFileNum", ArchiveFileSeekToFileNum
);
3506 def ArchiveFileSeekToFileName(infile
, seekfile
=None, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3507 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3510 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3511 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
3512 if(checkcompressfile
=="tarfile"):
3513 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3514 if(checkcompressfile
=="zipfile"):
3515 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3516 if(rarfile_support
and checkcompressfile
=="rarfile"):
3517 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3518 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3525 if(hasattr(sys
.stdin
, "buffer")):
3526 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
3528 shutil
.copyfileobj(sys
.stdin
, catfp
);
3530 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3534 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
3535 catfp
= download_file_from_internet_file(infile
);
3536 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3542 infile
= RemoveWindowsPath(infile
);
3543 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
3544 if(checkcompressfile
=="tarfile"):
3545 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3546 if(checkcompressfile
=="zipfile"):
3547 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3548 if(rarfile_support
and checkcompressfile
=="rarfile"):
3549 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3550 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3552 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
3553 if(not compresscheck
):
3554 fextname
= os
.path
.splitext(infile
)[1];
3555 if(fextname
==".gz"):
3556 compresscheck
= "gzip";
3557 elif(fextname
==".bz2"):
3558 compresscheck
= "bzip2";
3559 elif(fextname
==".zst"):
3560 compresscheck
= "zstd";
3561 elif(fextname
==".lz4" or fextname
==".clz4"):
3562 compresscheck
= "lz4";
3563 elif(fextname
==".lzo" or fextname
==".lzop"):
3564 compresscheck
= "lzo";
3565 elif(fextname
==".lzma" or fextname
==".xz"):
3566 compresscheck
= "lzma";
3569 if(not compresscheck
):
3571 catfp
= UncompressFile(infile
, formatspecs
, "rb");
3576 SeekToEndOfFile(catfp);
3578 SeekToEndOfFile(catfp);
3579 CatSize = catfp.tell();
3580 CatSizeEnd = CatSize;
3588 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
3589 catstring
= catheader
[0];
3590 catversion
= re
.findall(r
"([\d]+)$", catstring
);
3591 fprenumfiles
= catheader
[1];
3592 fnumfiles
= int(fprenumfiles
, 16);
3593 fprechecksumtype
= catheader
[2];
3594 fprechecksum
= catheader
[3];
3595 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
3596 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3597 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
3598 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
3599 catfileheadercshex
= format(0, 'x').lower();
3600 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
3601 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3602 elif(fprechecksumtype
=="crc16_ccitt"):
3603 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3604 elif(fprechecksumtype
=="adler32"):
3605 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3606 elif(fprechecksumtype
=="crc32"):
3607 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3608 elif(fprechecksumtype
=="crc64_ecma"):
3609 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3610 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
3611 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3612 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
3613 checksumoutstr
= hashlib
.new(fprechecksumtype
);
3614 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3615 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3617 catfileheadercshex
= format(0, 'x').lower();
3618 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3619 fheadtell
= len(fileheader
);
3620 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
3621 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
3623 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
3624 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
3625 seekto
= fnumfiles
- 1
3630 prefhstart
= catfp
.tell();
3632 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3634 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
3635 prefheadsize
= int(preheaderdata
[0], 16);
3636 prefnumfields
= int(preheaderdata
[1], 16);
3637 preftype
= int(preheaderdata
[2], 16);
3638 if(re
.findall("^[.|/]", preheaderdata
[3])):
3639 prefname
= preheaderdata
[3];
3641 prefname
= "./"+preheaderdata
[3];
3642 prefbasedir
= os
.path
.dirname(prefname
);
3643 preflinkname
= preheaderdata
[4];
3644 prefsize
= int(preheaderdata
[5], 16);
3645 prefatime
= int(preheaderdata
[6], 16);
3646 prefmtime
= int(preheaderdata
[7], 16);
3647 prefctime
= int(preheaderdata
[8], 16);
3648 prefbtime
= int(preheaderdata
[9], 16);
3649 prefmode
= int(preheaderdata
[10], 16);
3650 prefchmode
= stat
.S_IMODE(prefmode
);
3651 preftypemod
= stat
.S_IFMT(prefmode
);
3652 prefwinattributes
= int(preheaderdata
[11], 16);
3653 prefuid
= int(preheaderdata
[12], 16);
3654 prefuname
= preheaderdata
[13];
3655 prefgid
= int(preheaderdata
[14], 16);
3656 prefgname
= preheaderdata
[15];
3657 fid
= int(preheaderdata
[16], 16);
3658 finode
= int(preheaderdata
[17], 16);
3659 flinkcount
= int(preheaderdata
[18], 16);
3660 prefdev_minor
= int(preheaderdata
[19], 16);
3661 prefdev_major
= int(preheaderdata
[20], 16);
3662 prefrdev_minor
= int(preheaderdata
[22], 16);
3663 prefrdev_major
= int(preheaderdata
[23], 16);
3664 prefextrasize
= int(preheaderdata
[24], 16);
3665 prefextrafields
= int(preheaderdata
[25], 16);
3666 extrafieldslist
= [];
3668 extraend
= extrastart
+ prefextrafields
;
3669 extrafieldslist
= [];
3670 if(extrastart
<extraend
):
3671 extrafieldslist
.append(preheaderdata
[extrastart
]);
3672 extrastart
= extrastart
+ 1;
3673 prefchecksumtype
= preheaderdata
[extrastart
].lower();
3674 prefcs
= preheaderdata
[extrastart
+ 1].lower();
3675 prefccs
= preheaderdata
[extrastart
+ 2].lower();
3677 hcmax
= len(preheaderdata
) - 2;
3680 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
3682 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
3684 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
3685 prenewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
3686 elif(prefchecksumtype
=="adler32"):
3687 prenewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3688 elif(prefchecksumtype
=="crc32"):
3689 prenewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3690 elif(prefchecksumtype
=="crc64_ecma"):
3691 prenewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3692 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
3693 prenewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3694 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
3695 checksumoutstr
= hashlib
.new(prefchecksumtype
);
3696 checksumoutstr
.update(hout
.encode('UTF-8'));
3697 prenewfcs
= checksumoutstr
.hexdigest().lower();
3698 if(prefcs
!=prenewfcs
and not skipchecksum
):
3699 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
3701 valid_archive
= False;
3702 invalid_archive
= True;
3703 prefhend
= catfp
.tell() - 1;
3704 prefcontentstart
= catfp
.tell();
3706 pyhascontents
= False;
3708 prefcontents
= catfp
.read(prefsize
);
3709 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
3711 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
3712 prenewfccs
= format(crc16(prefcontents
) & 0xffff, '04x').lower();
3713 elif(prefchecksumtype
=="crc16_ccitt"):
3714 prenewfcs
= format(crc16_ccitt(prefcontents
) & 0xffff, '04x').lower();
3715 elif(prefchecksumtype
=="adler32"):
3716 prenewfccs
= format(zlib
.adler32(prefcontents
) & 0xffffffff, '08x').lower();
3717 elif(prefchecksumtype
=="crc32"):
3718 prenewfccs
= format(crc32(prefcontents
) & 0xffffffff, '08x').lower();
3719 elif(prefchecksumtype
=="crc64_ecma"):
3720 prenewfcs
= format(crc64_ecma(prefcontents
) & 0xffffffffffffffff, '016x').lower();
3721 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
3722 prenewfcs
= format(crc64_iso(prefcontents
) & 0xffffffffffffffff, '016x').lower();
3723 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
3724 checksumoutstr
= hashlib
.new(prefchecksumtype
);
3725 checksumoutstr
.update(prefcontents
);
3726 prenewfccs
= checksumoutstr
.hexdigest().lower();
3727 pyhascontents
= True;
3728 if(prefccs
!=prenewfccs
and not skipchecksum
):
3729 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
3734 prefname
= preheaderdata
[2];
3735 if(re
.findall("^[.|/]", preheaderdata
[2])):
3736 prefname
= preheaderdata
[2];
3738 prefname
= "./"+preheaderdata
[2];
3739 if(prefname
==seekfile
):
3742 catfp
.seek(seekstart
, 0);
3744 catfheadsize
= int(preheaderdata
[0], 16);
3745 catfnumfields
= int(preheaderdata
[1], 16);
3746 catftype
= int(preheaderdata
[2], 16);
3747 if(re
.findall("^[.|/]", preheaderdata
[3])):
3748 catfname
= preheaderdata
[3];
3750 catfname
= "./"+preheaderdata
[3];
3751 catflinkname
= preheaderdata
[4];
3752 catfsize
= int(preheaderdata
[5], 16);
3753 catfbasedir
= os
.path
.dirname(catfname
);
3755 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
3759 catlist
.update({'catfp': catfp
});
3764 create_alias_function("", __file_format_name__
, "SeekToFileName", ArchiveFileSeekToFileName
);
3766 def ArchiveFileValidate(infile
, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
3768 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
3769 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3772 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3773 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
3774 if(checkcompressfile
=="tarfile"):
3775 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3776 if(checkcompressfile
=="zipfile"):
3777 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3778 if(rarfile_support
and checkcompressfile
=="rarfile"):
3779 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3780 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3787 if(hasattr(sys
.stdin
, "buffer")):
3788 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
3790 shutil
.copyfileobj(sys
.stdin
, catfp
);
3792 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3796 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
3797 catfp
= download_file_from_internet_file(infile
);
3798 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3804 infile
= RemoveWindowsPath(infile
);
3805 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
3806 if(checkcompressfile
=="tarfile"):
3807 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3808 if(checkcompressfile
=="zipfile"):
3809 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3810 if(rarfile_support
and checkcompressfile
=="rarfile"):
3811 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3812 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3814 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
3815 if(not compresscheck
):
3816 fextname
= os
.path
.splitext(infile
)[1];
3817 if(fextname
==".gz"):
3818 compresscheck
= "gzip";
3819 elif(fextname
==".bz2"):
3820 compresscheck
= "bzip2";
3821 elif(fextname
==".zst"):
3822 compresscheck
= "zstd";
3823 elif(fextname
==".lz4" or fextname
==".clz4"):
3824 compresscheck
= "lz4";
3825 elif(fextname
==".lzo" or fextname
==".lzop"):
3826 compresscheck
= "lzo";
3827 elif(fextname
==".lzma" or fextname
==".xz"):
3828 compresscheck
= "lzma";
3831 if(not compresscheck
):
3833 catfp
= UncompressFile(infile
, formatspecs
, "rb");
3838 SeekToEndOfFile(catfp);
3840 SeekToEndOfFile(catfp);
3841 CatSize = catfp.tell();
3842 CatSizeEnd = CatSize;
3850 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
3851 catstring
= catheader
[0];
3852 catversion
= re
.findall(r
"([\d]+)$", catstring
);
3853 fprenumfiles
= catheader
[1];
3854 fnumfiles
= int(fprenumfiles
, 16);
3855 fprechecksumtype
= catheader
[2];
3856 fprechecksum
= catheader
[3];
3858 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
3859 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3860 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
3861 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
3862 catfileheadercshex
= format(0, 'x').lower();
3863 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
3864 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3865 elif(fprechecksumtype
=="crc16_ccitt"):
3866 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3867 elif(fprechecksumtype
=="adler32"):
3868 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3869 elif(fprechecksumtype
=="crc32"):
3870 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3871 elif(fprechecksumtype
=="crc64_ecma"):
3872 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3873 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
3874 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3875 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
3876 checksumoutstr
= hashlib
.new(fprechecksumtype
);
3877 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3878 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3880 catfileheadercshex
= format(0, 'x').lower();
3881 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3882 valid_archive
= True;
3883 invalid_archive
= False;
3885 VerbosePrintOut("Checking File Header Checksum of file at offset " + str(0));
3886 VerbosePrintOut(infile
);
3887 if(fprechecksum
==catfileheadercshex
):
3889 VerbosePrintOut("File Header Checksum Passed");
3892 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
3893 valid_archive
= False;
3894 invalid_archive
= True;
3895 while(il
<fnumfiles
):
3896 catfhstart
= catfp
.tell();
3898 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3900 catheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
3901 catfheadsize
= int(catheaderdata
[0], 16);
3902 catfnumfields
= int(catheaderdata
[1], 16);
3903 catftype
= int(catheaderdata
[2], 16);
3904 if(re
.findall("^[.|/]", catheaderdata
[3])):
3905 catfname
= catheaderdata
[3];
3907 catfname
= "./"+catheaderdata
[3];
3908 catfbasedir
= os
.path
.dirname(catfname
);
3909 catflinkname
= catheaderdata
[4];
3910 catfsize
= int(catheaderdata
[5], 16);
3911 catfatime
= int(catheaderdata
[6], 16);
3912 catfmtime
= int(catheaderdata
[7], 16);
3913 catfctime
= int(catheaderdata
[8], 16);
3914 catfbtime
= int(catheaderdata
[9], 16);
3915 catfmode
= int(catheaderdata
[10], 16);
3916 catfchmode
= stat
.S_IMODE(catfmode
);
3917 catftypemod
= stat
.S_IFMT(catfmode
);
3918 prefwinattributes
= int(catheaderdata
[11], 16);
3919 catfuid
= int(catheaderdata
[12], 16);
3920 catfuname
= catheaderdata
[13];
3921 catfgid
= int(catheaderdata
[14], 16);
3922 catfgname
= catheaderdata
[15];
3923 fid
= int(catheaderdata
[16], 16);
3924 finode
= int(catheaderdata
[17], 16);
3925 flinkcount
= int(catheaderdata
[18], 16);
3926 catfdev_minor
= int(catheaderdata
[19], 16);
3927 catfdev_major
= int(catheaderdata
[20], 16);
3928 catfrdev_minor
= int(catheaderdata
[21], 16);
3929 catfrdev_major
= int(catheaderdata
[22], 16);
3930 catfextrasize
= int(catheaderdata
[23], 16);
3931 catfextrafields
= int(catheaderdata
[24], 16);
3932 extrafieldslist
= [];
3934 extraend
= extrastart
+ catfextrafields
;
3935 extrafieldslist
= [];
3936 if(extrastart
<extraend
):
3937 extrafieldslist
.append(catheaderdata
[extrastart
]);
3938 extrastart
= extrastart
+ 1;
3939 catfchecksumtype
= catheaderdata
[extrastart
].lower();
3940 catfcs
= catheaderdata
[extrastart
+ 1].lower();
3941 catfccs
= catheaderdata
[extrastart
+ 2].lower();
3943 hcmax
= len(catheaderdata
) - 2;
3946 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[5]);
3948 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
3950 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
3951 catnewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
3952 elif(catfchecksumtype
=="adler32"):
3953 catnewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3954 elif(catfchecksumtype
=="crc32"):
3955 catnewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3956 elif(catfchecksumtype
=="crc64_ecma"):
3957 catnewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3958 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
3959 catnewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3960 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
3961 checksumoutstr
= hashlib
.new(catfchecksumtype
);
3962 checksumoutstr
.update(hout
.encode('UTF-8'));
3963 catnewfcs
= checksumoutstr
.hexdigest().lower();
3965 VerbosePrintOut("Checking File Header Checksum of file at offset " + str(catfhstart
));
3966 VerbosePrintOut(catfname
);
3967 if(catfcs
==catnewfcs
):
3969 VerbosePrintOut("File Header Checksum Passed");
3972 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfhstart
));
3973 valid_archive
= False;
3974 invalid_archive
= True;
3975 catfhend
= catfp
.tell() - 1;
3976 catfcontentstart
= catfp
.tell();
3978 pyhascontents
= False;
3980 catfcontents
= catfp
.read(catfsize
);
3981 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
3983 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
3984 catnewfccs
= format(crc16(catfcontents
) & 0xffff, '04x').lower();
3985 elif(catfchecksumtype
=="crc16_ccitt"):
3986 catnewfcs
= format(crc16_ccitt(catfcontents
) & 0xffff, '04x').lower();
3987 elif(catfchecksumtype
=="adler32"):
3988 catnewfccs
= format(zlib
.adler32(catfcontents
) & 0xffffffff, '08x').lower();
3989 elif(catfchecksumtype
=="crc32"):
3990 catnewfccs
= format(crc32(catfcontents
) & 0xffffffff, '08x').lower();
3991 elif(catfchecksumtype
=="crc64_ecma"):
3992 catnewfcs
= format(crc64_ecma(catfcontents
) & 0xffffffffffffffff, '016x').lower();
3993 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
3994 catnewfcs
= format(crc64_iso(catfcontents
) & 0xffffffffffffffff, '016x').lower();
3995 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
3996 checksumoutstr
= hashlib
.new(catfchecksumtype
);
3997 checksumoutstr
.update(catfcontents
);
3998 catnewfccs
= checksumoutstr
.hexdigest().lower();
3999 pyhascontents
= True;
4001 VerbosePrintOut("Checking File Content Checksum of file at offset " + str(catfcontentstart
));
4002 VerbosePrintOut(catfname
);
4003 if(catfccs
==catnewfccs
):
4005 VerbosePrintOut("File Content Checksum Passed");
4008 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfcontentstart
));
4009 valid_archive
= False;
4010 invalid_archive
= True;
4012 VerbosePrintOut("");
4025 create_alias_function("", __file_format_name__
, "Validate", ArchiveFileValidate
);
4027 def ArchiveFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4028 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4031 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4032 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
4033 if(checkcompressfile
=="tarfile"):
4034 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4035 if(checkcompressfile
=="zipfile"):
4036 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4037 if(rarfile_support
and checkcompressfile
=="rarfile"):
4038 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4039 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4046 if(hasattr(sys
.stdin
, "buffer")):
4047 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
4049 shutil
.copyfileobj(sys
.stdin
, catfp
);
4051 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4055 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4056 catfp
= download_file_from_internet_file(infile
);
4057 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4063 infile
= RemoveWindowsPath(infile
);
4064 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
4065 if(checkcompressfile
=="tarfile"):
4066 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4067 if(checkcompressfile
=="zipfile"):
4068 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4069 if(rarfile_support
and checkcompressfile
=="rarfile"):
4070 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4071 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4073 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
4074 if(not compresscheck
):
4075 fextname
= os
.path
.splitext(infile
)[1];
4076 if(fextname
==".gz"):
4077 compresscheck
= "gzip";
4078 elif(fextname
==".bz2"):
4079 compresscheck
= "bzip2";
4080 elif(fextname
==".zst"):
4081 compresscheck
= "zstd";
4082 elif(fextname
==".lz4" or fextname
==".clz4"):
4083 compresscheck
= "lz4";
4084 elif(fextname
==".lzo" or fextname
==".lzop"):
4085 compresscheck
= "lzo";
4086 elif(fextname
==".lzma" or fextname
==".xz"):
4087 compresscheck
= "lzma";
4090 if(not compresscheck
):
4092 catfp
= UncompressFile(infile
, formatspecs
, "rb");
4097 SeekToEndOfFile(catfp);
4099 SeekToEndOfFile(catfp);
4100 CatSize = catfp.tell();
4101 CatSizeEnd = CatSize;
4109 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
4110 catstring
= catheader
[0];
4111 catversion
= re
.findall(r
"([\d]+)$", catstring
);
4112 fprenumfiles
= catheader
[1];
4113 fnumfiles
= int(fprenumfiles
, 16);
4114 fprechecksumtype
= catheader
[2];
4115 fprechecksum
= catheader
[3];
4116 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
4117 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4118 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
4119 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
4120 catfileheadercshex
= format(0, 'x').lower();
4121 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
4122 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4123 elif(fprechecksumtype
=="crc16_ccitt"):
4124 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4125 elif(fprechecksumtype
=="adler32"):
4126 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4127 elif(fprechecksumtype
=="crc32"):
4128 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4129 elif(fprechecksumtype
=="crc64_ecma"):
4130 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4131 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
4132 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4133 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
4134 checksumoutstr
= hashlib
.new(fprechecksumtype
);
4135 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4136 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4138 catfileheadercshex
= format(0, 'x').lower();
4139 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4140 fheadtell
= len(fileheader
);
4141 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
4142 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
4144 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4145 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
4146 if(seekstart
<0 and seekstart
>fnumfiles
):
4148 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
4149 seekend
= fnumfiles
;
4150 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
4151 seekend
= fnumfiles
- abs(seekend
);
4154 while(il
< seekstart
):
4155 prefhstart
= catfp
.tell();
4157 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
4159 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
4160 prefheadsize
= int(preheaderdata
[0], 16);
4161 prefnumfields
= int(preheaderdata
[1], 16);
4162 if(re
.findall("^[.|/]", preheaderdata
[3])):
4163 prefname
= preheaderdata
[3];
4165 prefname
= "./"+preheaderdata
[3];
4166 prefsize
= int(preheaderdata
[5], 16);
4167 prefextrasize
= int(preheaderdata
[23], 16);
4168 prefextrafields
= int(preheaderdata
[24], 16);
4169 extrafieldslist
= [];
4171 extraend
= extrastart
+ prefextrafields
;
4172 extrafieldslist
= [];
4173 if(extrastart
<extraend
):
4174 extrafieldslist
.append(preheaderdata
[extrastart
]);
4175 extrastart
= extrastart
+ 1;
4176 prefchecksumtype
= preheaderdata
[extrastart
].lower();
4177 prefcs
= preheaderdata
[extrastart
+ 1].lower();
4178 prefccs
= preheaderdata
[extrastart
+ 2].lower();
4180 hcmax
= len(preheaderdata
) - 2;
4183 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
4185 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
4187 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
4188 prenewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
4189 elif(prefchecksumtype
=="adler32"):
4190 prenewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4191 elif(prefchecksumtype
=="crc32"):
4192 prenewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4193 elif(prefchecksumtype
=="crc64_ecma"):
4194 prenewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4195 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
4196 prenewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4197 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
4198 checksumoutstr
= hashlib
.new(prefchecksumtype
);
4199 checksumoutstr
.update(hout
.encode('UTF-8'));
4200 prenewfcs
= checksumoutstr
.hexdigest().lower();
4201 if(prefcs
!=prenewfcs
and not skipchecksum
):
4202 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
4204 valid_archive
= False;
4205 invalid_archive
= True;
4206 prefhend
= catfp
.tell() - 1;
4207 prefcontentstart
= catfp
.tell();
4209 pyhascontents
= False;
4211 prefcontents
= catfp
.read(prefsize
);
4212 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
4214 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
4215 prenewfccs
= format(crc16(prefcontents
) & 0xffff, '04x').lower();
4216 elif(prefchecksumtype
=="crc16_ccitt"):
4217 prenewfcs
= format(crc16_ccitt(prefcontents
) & 0xffff, '04x').lower();
4218 elif(prefchecksumtype
=="adler32"):
4219 prenewfccs
= format(zlib
.adler32(prefcontents
) & 0xffffffff, '08x').lower();
4220 elif(prefchecksumtype
=="crc32"):
4221 prenewfccs
= format(crc32(prefcontents
) & 0xffffffff, '08x').lower();
4222 elif(prefchecksumtype
=="crc64_ecma"):
4223 prenewfcs
= format(crc64_ecma(prefcontents
) & 0xffffffffffffffff, '016x').lower();
4224 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
4225 prenewfcs
= format(crc64_iso(prefcontents
) & 0xffffffffffffffff, '016x').lower();
4226 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
4227 checksumoutstr
= hashlib
.new(prefchecksumtype
);
4228 checksumoutstr
.update(prefcontents
);
4229 prenewfccs
= checksumoutstr
.hexdigest().lower();
4230 pyhascontents
= True;
4231 if(prefccs
!=prenewfccs
and not skipchecksum
):
4232 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
4236 fileidnum
= seekstart
;
4238 while(fileidnum
<seekend
):
4239 catfhstart
= catfp
.tell();
4241 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
4243 catheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
4244 catfheadsize
= int(catheaderdata
[0], 16);
4245 catfnumfields
= int(catheaderdata
[1], 16);
4246 catftype
= int(catheaderdata
[2], 16);
4247 if(re
.findall("^[.|/]", catheaderdata
[3])):
4248 catfname
= catheaderdata
[3];
4250 catfname
= "./"+catheaderdata
[3];
4251 catfbasedir
= os
.path
.dirname(catfname
);
4252 catflinkname
= catheaderdata
[4];
4253 catfsize
= int(catheaderdata
[5], 16);
4254 catfatime
= int(catheaderdata
[6], 16);
4255 catfmtime
= int(catheaderdata
[7], 16);
4256 catfctime
= int(catheaderdata
[8], 16);
4257 catfbtime
= int(catheaderdata
[9], 16);
4258 catfmode
= int(catheaderdata
[10], 16);
4259 catfchmode
= stat
.S_IMODE(catfmode
);
4260 catftypemod
= stat
.S_IFMT(catfmode
);
4261 catfwinattributes
= int(catheaderdata
[11], 16);
4262 catfuid
= int(catheaderdata
[12], 16);
4263 catfuname
= catheaderdata
[13];
4264 catfgid
= int(catheaderdata
[14], 16);
4265 catfgname
= catheaderdata
[15];
4266 fid
= int(catheaderdata
[16], 16);
4267 finode
= int(catheaderdata
[17], 16);
4268 flinkcount
= int(catheaderdata
[18], 16);
4269 catfdev_minor
= int(catheaderdata
[19], 16);
4270 catfdev_major
= int(catheaderdata
[20], 16);
4271 catfrdev_minor
= int(catheaderdata
[21], 16);
4272 catfrdev_major
= int(catheaderdata
[22], 16);
4273 catfextrasize
= int(catheaderdata
[23], 16);
4274 catfextrafields
= int(catheaderdata
[24], 16);
4275 extrafieldslist
= [];
4277 extraend
= extrastart
+ catfextrafields
;
4278 extrafieldslist
= [];
4279 if(extrastart
<extraend
):
4280 extrafieldslist
.append(catheaderdata
[extrastart
]);
4281 extrastart
= extrastart
+ 1;
4282 catfchecksumtype
= catheaderdata
[extrastart
].lower();
4283 catfcs
= catheaderdata
[extrastart
+ 1].lower();
4284 catfccs
= catheaderdata
[extrastart
+ 2].lower();
4286 hcmax
= len(catheaderdata
) - 2;
4289 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[5]);
4291 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
4293 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
4294 catnewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
4295 elif(catfchecksumtype
=="adler32"):
4296 catnewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4297 elif(catfchecksumtype
=="crc32"):
4298 catnewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4299 elif(catfchecksumtype
=="crc64_ecma"):
4300 catnewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4301 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
4302 catnewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4303 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
4304 checksumoutstr
= hashlib
.new(catfchecksumtype
);
4305 checksumoutstr
.update(hout
.encode('UTF-8'));
4306 catnewfcs
= checksumoutstr
.hexdigest().lower();
4307 if(catfcs
!=catnewfcs
and not skipchecksum
):
4308 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfhstart
));
4310 catfhend
= catfp
.tell() - 1;
4311 catfcontentstart
= catfp
.tell();
4313 pyhascontents
= False;
4314 if(catfsize
>0 and not listonly
):
4315 catfcontents
= catfp
.read(catfsize
);
4316 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
4318 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
4319 catnewfccs
= format(crc16(catfcontents
) & 0xffff, '04x').lower();
4320 elif(catfchecksumtype
=="crc16_ccitt"):
4321 catnewfcs
= format(crc16_ccitt(catfcontents
) & 0xffff, '04x').lower();
4322 elif(catfchecksumtype
=="adler32"):
4323 catnewfccs
= format(zlib
.adler32(catfcontents
) & 0xffffffff, '08x').lower();
4324 elif(catfchecksumtype
=="crc32"):
4325 catnewfccs
= format(crc32(catfcontents
) & 0xffffffff, '08x').lower();
4326 elif(catfchecksumtype
=="crc64_ecma"):
4327 catnewfcs
= format(crc64_ecma(catfcontents
) & 0xffffffffffffffff, '016x').lower();
4328 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
4329 catnewfcs
= format(crc64_iso(catfcontents
) & 0xffffffffffffffff, '016x').lower();
4330 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
4331 checksumoutstr
= hashlib
.new(catfchecksumtype
);
4332 checksumoutstr
.update(catfcontents
);
4333 catnewfccs
= checksumoutstr
.hexdigest().lower();
4334 pyhascontents
= True;
4335 if(catfccs
!=catnewfccs
and skipchecksum
):
4336 VerbosePrintOut("File Content Checksum Error with file " + catfname
+ " at offset " + str(catfcontentstart
));
4338 if(catfsize
>0 and listonly
):
4339 catfp
.seek(catfsize
, 1);
4340 pyhascontents
= False;
4342 catfcontentend
= catfp
.tell() - 1;
4343 catlist
['ffilelist'].update({realidnum
: {'fid': realidnum
, 'fidalt': fileidnum
, 'fheadersize': catfheadsize
, 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
, 'fatime': catfatime
, 'fmtime': catfmtime
, 'fctime': catfctime
, 'fbtime': catfbtime
, 'fmode': catfmode
, 'fchmode': catfchmode
, 'ftypemod': catftypemod
, 'fwinattributes': catfwinattributes
, 'fuid': catfuid
, 'funame': catfuname
, 'fgid': catfgid
, 'fgname': catfgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': catfdev_minor
, 'fmajor': catfdev_major
, 'frminor': catfrdev_minor
, 'frmajor': catfrdev_major
, 'fchecksumtype': catfchecksumtype
, 'fnumfields': catfnumfields
+ 2, 'frawheader': catheaderdata
, 'fextrafields': catfextrafields
, 'fextrafieldsize': catfextrasize
, 'fextralist': extrafieldslist
, 'fheaderchecksum': catfcs
, 'fcontentchecksum': catfccs
, 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': catfcontents
} });
4344 fileidnum
= fileidnum
+ 1;
4345 realidnum
= realidnum
+ 1;
4347 catlist
.update({'catfp': catfp
});
4352 create_alias_function("", __file_format_name__
, "ToArray", ArchiveFileToArray
);
4354 def ArchiveFileStringToArray(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4355 catfp
= BytesIO(catstr
);
4356 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4357 return listcatfiles
;
4359 create_alias_function("", __file_format_name__
, "StringToArray", ArchiveFileStringToArray
);
4361 def TarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4363 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4364 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4365 return listcatfiles
;
4367 def ZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4369 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4370 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4371 return listcatfiles
;
4373 if(not rarfile_support
):
4374 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4377 if(rarfile_support
):
4378 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4380 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4381 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4382 return listcatfiles
;
4384 def ListDirToArrayAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4385 catver
= formatspecs
[6];
4386 fileheaderver
= str(int(catver
.replace(".", "")));
4387 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4388 advancedlist
= True;
4391 for line
in sys
.stdin
:
4392 infilelist
.append(line
.strip());
4393 infilelist
= list(filter(None, infilelist
));
4394 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
4395 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
4397 with
UncompressFile(infiles
, formatspecs
, "r") as finfile
:
4398 for line
in finfile
:
4399 infilelist
.append(line
.strip());
4400 infilelist
= list(filter(None, infilelist
));
4402 if(isinstance(infiles
, (list, tuple, ))):
4403 infilelist
= list(filter(None, infiles
));
4404 elif(isinstance(infiles
, (str, ))):
4405 infilelist
= list(filter(None, [infiles
]));
4407 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
4409 GetDirList
= ListDir(infilelist
, followlink
, False);
4417 inodetocatinode
= {};
4419 fnumfiles
= int(len(GetDirList
));
4420 catver
= formatspecs
[6];
4421 fileheaderver
= str(int(catver
.replace(".", "")));
4422 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4423 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4424 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4425 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4426 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4427 if(checksumtype
=="none" or checksumtype
==""):
4428 catfileheadercshex
= format(0, 'x').lower();
4429 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4430 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4431 elif(checksumtype
=="crc16_ccitt"):
4432 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4433 elif(checksumtype
=="adler32"):
4434 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4435 elif(checksumtype
=="crc32"):
4436 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4437 elif(checksumtype
=="crc64_ecma"):
4438 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4439 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4440 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4441 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4442 checksumoutstr
= hashlib
.new(checksumtype
);
4443 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4444 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4446 catfileheadercshex
= format(0, 'x').lower();
4447 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4448 fheadtell
= len(fileheader
);
4449 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4450 for curfname
in GetDirList
:
4451 if(re
.findall("^[.|/]", curfname
)):
4454 fname
= "./"+curfname
;
4456 VerbosePrintOut(fname
);
4457 if(not followlink
or followlink
is None):
4458 fstatinfo
= os
.lstat(fname
);
4460 fstatinfo
= os
.stat(fname
);
4461 fpremode
= fstatinfo
.st_mode
;
4462 finode
= fstatinfo
.st_ino
;
4463 flinkcount
= fstatinfo
.st_nlink
;
4465 if(stat
.S_ISREG(fpremode
)):
4467 elif(stat
.S_ISLNK(fpremode
)):
4469 elif(stat
.S_ISCHR(fpremode
)):
4471 elif(stat
.S_ISBLK(fpremode
)):
4473 elif(stat
.S_ISDIR(fpremode
)):
4475 elif(stat
.S_ISFIFO(fpremode
)):
4477 elif(stat
.S_ISSOCK(fpremode
)):
4479 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
4481 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
4483 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
4488 fbasedir
= os
.path
.dirname(fname
);
4490 if(not followlink
and finode
!=0):
4492 if(finode
in inodelist
):
4494 flinkname
= inodetofile
[finode
];
4495 fcurinode
= inodetocatinode
[finode
];
4496 if(finode
not in inodelist
):
4497 inodelist
.append(finode
);
4498 inodetofile
.update({finode
: fname
});
4499 inodetocatinode
.update({finode
: curinode
});
4500 fcurinode
= curinode
;
4501 curinode
= curinode
+ 1;
4503 fcurinode
= curinode
;
4504 curinode
= curinode
+ 1;
4505 curfid
= curfid
+ 1;
4507 flinkname
= os
.readlink(fname
);
4508 fdev
= fstatinfo
.st_dev
;
4509 getfdev
= GetDevMajorMinor(fdev
);
4510 fdev_minor
= getfdev
[0];
4511 fdev_major
= getfdev
[1];
4512 frdev
= fstatinfo
.st_dev
;
4513 if(hasattr(fstatinfo
, "st_rdev")):
4514 frdev
= fstatinfo
.st_rdev
;
4516 frdev
= fstatinfo
.st_dev
;
4517 getfrdev
= GetDevMajorMinor(frdev
);
4518 frdev_minor
= getfrdev
[0];
4519 frdev_major
= getfrdev
[1];
4520 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
4522 if(ftype
==0 or ftype
==7):
4523 fsize
= fstatinfo
.st_size
;
4524 fatime
= fstatinfo
.st_atime
;
4525 fmtime
= fstatinfo
.st_mtime
;
4526 fctime
= fstatinfo
.st_ctime
;
4527 if(hasattr(fstatinfo
, "st_birthtime")):
4528 fbtime
= fstatinfo
.st_birthtime
;
4530 fbtime
= fstatinfo
.st_ctime
;
4531 fmode
= fstatinfo
.st_mode
;
4532 fchmode
= stat
.S_IMODE(fstatinfo
.st_mode
);
4533 ftypemod
= stat
.S_IFMT(fstatinfo
.st_mode
);
4534 fuid
= fstatinfo
.st_uid
;
4535 fgid
= fstatinfo
.st_gid
;
4540 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
4541 funame
= userinfo
.pw_name
;
4550 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
4551 fgname
= groupinfo
.gr_name
;
4556 fdev_minor
= fdev_minor
;
4557 fdev_major
= fdev_major
;
4558 frdev_minor
= frdev_minor
;
4559 frdev_major
= frdev_major
;
4561 flinkcount
= flinkcount
;
4562 if(hasattr(fstatinfo
, "st_file_attributes")):
4563 fwinattributes
= fstatinfo
.st_file_attributes
;
4566 fcontents
= "".encode('UTF-8');
4568 if(ftype
== 0 or ftype
== 7):
4569 with
open(fname
, "rb") as fpc
:
4571 chunk
= fpc
.read(chunk_size
);
4575 if(followlink
and (ftype
== 1 or ftype
== 2)):
4576 flstatinfo
= os
.stat(flinkname
);
4577 with
open(flinkname
, "rb") as fpc
:
4579 chunk
= fpc
.read(chunk_size
);
4583 ftypehex
= format(ftype
, 'x').lower();
4584 extrafields
= len(extradata
);
4585 extrafieldslist
= extradata
;
4586 catfextrafields
= extrafields
;
4587 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
4588 if(len(extradata
)>0):
4589 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4590 extrasizelen
= len(extrasizestr
);
4591 extrasizelenhex
= format(extrasizelen
, 'x').lower();
4592 catoutlist
= [ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fwinattributes
), 'x').lower(), format(int(fuid
), 'x').lower(), funame
, format(int(fgid
), 'x').lower(), fgname
, format(int(fcurfid
), 'x').lower(), format(int(fcurinode
), 'x').lower(), format(int(flinkcount
), 'x').lower(), format(int(fdev_minor
), 'x').lower(), format(int(fdev_major
), 'x').lower(), format(int(frdev_minor
), 'x').lower(), format(int(frdev_major
), 'x').lower(), extrasizelenhex
, format(catfextrafields
, 'x').lower()];
4593 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
4594 catoutlenhex
= format(catoutlen
, 'x').lower();
4595 catoutlist
.insert(0, catoutlenhex
);
4596 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
4597 if(len(extradata
)>0):
4598 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4599 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
4600 catfnumfields
= catoutlen
;
4601 if(checksumtype
=="none" or checksumtype
==""):
4602 catfileheadercshex
= format(0, 'x').lower();
4603 catfilecontentcshex
= format(0, 'x').lower();
4604 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4605 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
4606 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
4607 elif(checksumtype
=="crc16_ccitt"):
4608 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
4609 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
4610 elif(checksumtype
=="adler32"):
4611 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4612 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
4613 elif(checksumtype
=="crc32"):
4614 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4615 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
4616 elif(checksumtype
=="crc64_ecma"):
4617 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4618 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
4619 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4620 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4621 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
4622 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4623 checksumoutstr
= hashlib
.new(checksumtype
);
4624 checksumoutstr
.update("".encode('UTF-8'));
4625 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4626 checksumoutstr
= hashlib
.new(checksumtype
);
4627 checksumoutstr
.update(fcontents
);
4628 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
4630 catfileheadercshex
= format(0, 'x').lower();
4631 catfilecontentcshex
= format(0, 'x').lower();
4632 catfhstart
= fheadtell
;
4633 fheadtell
+= len(catfileoutstr
);
4634 catfhend
= fheadtell
- 1;
4635 catfcontentstart
= fheadtell
;
4636 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4637 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
4638 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
4639 catheaderdata
= [catheaersize
] + catoutlist
+ extradata
+ [checksumtype
, catfileheadercshex
, catfilecontentcshex
]
4640 if(checksumtype
=="none" or checksumtype
==""):
4641 catfileheadercshex
= format(0, 'x').lower();
4642 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4643 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4644 elif(checksumtype
=="crc16_ccitt"):
4645 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4646 elif(checksumtype
=="adler32"):
4647 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4648 elif(checksumtype
=="crc32"):
4649 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4650 elif(checksumtype
=="crc64_ecma"):
4651 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4652 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4653 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4654 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4655 checksumoutstr
= hashlib
.new(checksumtype
);
4656 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
4657 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4659 catfileheadercshex
= format(0, 'x').lower();
4660 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4661 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
4662 nullstrecd
= formatspecs
[5].encode('UTF-8');
4663 fheadtell
+= len(catfileoutstr
) + 1;
4664 catfcontentend
= fheadtell
- 1;
4665 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
4666 pyhascontents
= False;
4667 if(int(fsize
)>0 and not listonly
):
4668 pyhascontents
= True;
4669 if(int(fsize
)>0 and listonly
):
4671 pyhascontents
= False;
4672 catlist
['ffilelist'].update({fileidnum
: {'fid': fileidnum
, 'fidalt': fileidnum
, 'fheadersize': int(catheaersize
, 16), 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fwinattributes': fwinattributes
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': checksumtype
, 'fnumfields': catfnumfields
+ 2, 'frawheader': catheaderdata
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
4673 fileidnum
= fileidnum
+ 1;
4676 def TarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4682 inodetocatinode
= {};
4686 if(hasattr(sys
.stdin
, "buffer")):
4687 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
4689 shutil
.copyfileobj(sys
.stdin
, infile
);
4694 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4695 infile
= download_file_from_internet_file(infile
);
4700 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
4702 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
4704 if(not tarfile
.is_tarfile(infile
)):
4706 except AttributeError:
4707 if(not is_tarfile(infile
)):
4712 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4713 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
4715 tarfp
= tarfile
.open(infile
, "r");
4716 except FileNotFoundError
:
4718 fnumfiles
= int(len(tarfp
.getmembers()));
4719 catver
= formatspecs
[6];
4720 fileheaderver
= str(int(catver
.replace(".", "")));
4721 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4722 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4723 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4724 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4725 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4726 if(checksumtype
=="none" or checksumtype
==""):
4727 catfileheadercshex
= format(0, 'x').lower();
4728 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4729 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4730 elif(checksumtype
=="crc16_ccitt"):
4731 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4732 elif(checksumtype
=="adler32"):
4733 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4734 elif(checksumtype
=="crc32"):
4735 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4736 elif(checksumtype
=="crc64_ecma"):
4737 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4738 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4739 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4740 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4741 checksumoutstr
= hashlib
.new(checksumtype
);
4742 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4743 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4745 catfileheadercshex
= format(0, 'x').lower();
4746 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4747 fheadtell
= len(fileheader
);
4748 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4749 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
4750 if(re
.findall("^[.|/]", member
.name
)):
4751 fname
= member
.name
;
4753 fname
= "./"+member
.name
;
4755 VerbosePrintOut(fname
);
4756 fpremode
= member
.mode
;
4757 ffullmode
= member
.mode
;
4761 ffullmode
= member
.mode
+ stat
.S_IFREG
;
4763 elif(member
.isdev()):
4764 ffullmode
= member
.mode
;
4766 elif(member
.islnk()):
4767 ffullmode
= member
.mode
+ stat
.S_IFREG
;
4769 elif(member
.issym()):
4770 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
4772 elif(member
.ischr()):
4773 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
4775 elif(member
.isblk()):
4776 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
4778 elif(member
.isdir()):
4779 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
4781 elif(member
.isfifo()):
4782 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
4784 elif(member
.issparse()):
4785 ffullmode
= member
.mode
;
4788 ffullmode
= member
.mode
;
4791 fbasedir
= os
.path
.dirname(fname
);
4795 curfid
= curfid
+ 1;
4797 flinkname
= member
.linkname
;
4798 fdev_minor
= member
.devminor
;
4799 fdev_major
= member
.devmajor
;
4800 frdev_minor
= member
.devminor
;
4801 frdev_major
= member
.devmajor
;
4802 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
4804 elif(ftype
==0 or ftype
==7):
4805 fsize
= member
.size
;
4807 fsize
= member
.size
;
4808 fatime
= member
.mtime
;
4809 fmtime
= member
.mtime
;
4810 fctime
= member
.mtime
;
4811 fbtime
= member
.mtime
;
4813 fchmode
= stat
.S_IMODE(ffullmode
);
4814 ftypemod
= stat
.S_IFMT(ffullmode
);
4817 funame
= member
.uname
;
4818 fgname
= member
.gname
;
4819 flinkcount
= flinkcount
;
4820 fwinattributes
= int(0);
4821 fcontents
= "".encode('UTF-8');
4823 if(ftype
== 0 or ftype
== 7):
4824 with tarfp
.extractfile(member
) as fpc
:
4826 chunk
= fpc
.read(chunk_size
);
4830 ftypehex
= format(ftype
, 'x').lower();
4831 extrafields
= len(extradata
);
4832 extrafieldslist
= extradata
;
4833 catfextrafields
= extrafields
;
4834 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
4835 if(len(extradata
)>0):
4836 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4837 extrasizelen
= len(extrasizestr
);
4838 extrasizelenhex
= format(extrasizelen
, 'x').lower();
4839 catoutlist
= [ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fwinattributes
), 'x').lower(), format(int(fuid
), 'x').lower(), funame
, format(int(fgid
), 'x').lower(), fgname
, format(int(fcurfid
), 'x').lower(), format(int(fcurinode
), 'x').lower(), format(int(flinkcount
), 'x').lower(), format(int(fdev_minor
), 'x').lower(), format(int(fdev_major
), 'x').lower(), format(int(frdev_minor
), 'x').lower(), format(int(frdev_major
), 'x').lower(), extrasizelenhex
, format(catfextrafields
, 'x').lower()];
4840 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
4841 catoutlenhex
= format(catoutlen
, 'x').lower();
4842 catoutlist
.insert(0, catoutlenhex
);
4843 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
4844 if(len(extradata
)>0):
4845 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4846 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
4847 catfnumfields
= catoutlen
;
4848 if(checksumtype
=="none" or checksumtype
==""):
4849 catfileheadercshex
= format(0, 'x').lower();
4850 catfilecontentcshex
= format(0, 'x').lower();
4851 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4852 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
4853 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
4854 elif(checksumtype
=="crc16_ccitt"):
4855 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
4856 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
4857 elif(checksumtype
=="adler32"):
4858 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4859 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
4860 elif(checksumtype
=="crc32"):
4861 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4862 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
4863 elif(checksumtype
=="crc64_ecma"):
4864 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4865 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
4866 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4867 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4868 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
4869 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4870 checksumoutstr
= hashlib
.new(checksumtype
);
4871 checksumoutstr
.update("".encode('UTF-8'));
4872 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4873 checksumoutstr
= hashlib
.new(checksumtype
);
4874 checksumoutstr
.update(fcontents
);
4875 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
4877 catfileheadercshex
= format(0, 'x').lower();
4878 catfilecontentcshex
= format(0, 'x').lower();
4879 catfhstart
= fheadtell
;
4880 fheadtell
+= len(catfileoutstr
);
4881 catfhend
= fheadtell
- 1;
4882 catfcontentstart
= fheadtell
;
4883 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4884 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
4885 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
4886 catheaderdata
= [catheaersize
] + catoutlist
+ extradata
+ [checksumtype
, catfileheadercshex
, catfilecontentcshex
]
4887 if(checksumtype
=="none" or checksumtype
==""):
4888 catfileheadercshex
= format(0, 'x').lower();
4889 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4890 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4891 elif(checksumtype
=="crc16_ccitt"):
4892 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4893 elif(checksumtype
=="adler32"):
4894 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4895 elif(checksumtype
=="crc32"):
4896 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4897 elif(checksumtype
=="crc64_ecma"):
4898 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4899 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4900 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4901 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4902 checksumoutstr
= hashlib
.new(checksumtype
);
4903 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
4904 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4906 catfileheadercshex
= format(0, 'x').lower();
4907 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4908 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
4909 nullstrecd
= formatspecs
[5].encode('UTF-8');
4910 fheadtell
+= len(catfileoutstr
) + 1;
4911 catfcontentend
= fheadtell
- 1;
4912 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
4913 pyhascontents
= False;
4914 if(int(fsize
)>0 and not listonly
):
4915 pyhascontents
= True;
4916 if(int(fsize
)>0 and listonly
):
4918 pyhascontents
= False;
4919 catlist
['ffilelist'].update({fileidnum
: {'fid': fileidnum
, 'fidalt': fileidnum
, 'fheadersize': int(catheaersize
, 16), 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fwinattributes': fwinattributes
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': checksumtype
, 'fnumfields': catfnumfields
+ 2, 'frawheader': catheaderdata
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
4920 fileidnum
= fileidnum
+ 1;
4923 def ZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4924 advancedlist
= True;
4930 inodetocatinode
= {};
4934 if(hasattr(sys
.stdin
, "buffer")):
4935 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
4937 shutil
.copyfileobj(sys
.stdin
, infile
);
4942 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4943 infile
= download_file_from_internet_file(infile
);
4948 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
4952 if(not zipfile
.is_zipfile(infile
)):
4955 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
4956 except FileNotFoundError
:
4958 ziptest
= zipfp
.testzip();
4960 VerbosePrintOut("Bad file found!");
4961 fnumfiles
= int(len(zipfp
.infolist()));
4962 catver
= formatspecs
[6];
4963 fileheaderver
= str(int(catver
.replace(".", "")));
4964 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4965 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4966 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4967 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4968 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4969 if(checksumtype
=="none" or checksumtype
==""):
4970 catfileheadercshex
= format(0, 'x').lower();
4971 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4972 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4973 elif(checksumtype
=="crc16_ccitt"):
4974 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4975 elif(checksumtype
=="adler32"):
4976 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4977 elif(checksumtype
=="crc32"):
4978 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4979 elif(checksumtype
=="crc64_ecma"):
4980 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4981 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4982 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4983 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4984 checksumoutstr
= hashlib
.new(checksumtype
);
4985 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4986 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4988 catfileheadercshex
= format(0, 'x').lower();
4989 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4990 fheadtell
= len(fileheader
);
4991 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4992 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
4993 if(re
.findall("^[.|/]", member
.filename
)):
4994 fname
= member
.filename
;
4996 fname
= "./"+member
.filename
;
4997 zipinfo
= zipfp
.getinfo(member
.filename
);
4999 VerbosePrintOut(fname
);
5000 if(not member
.is_dir()):
5001 fpremode
= stat
.S_IFREG
+ 438;
5002 elif(member
.is_dir()):
5003 fpremode
= stat
.S_IFDIR
+ 511;
5006 if(not member
.is_dir()):
5008 elif(member
.is_dir()):
5011 fbasedir
= os
.path
.dirname(fname
);
5015 curfid
= curfid
+ 1;
5023 fsize
= member
.file_size
;
5025 fsize
= member
.file_size
;
5026 fatime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5027 fmtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5028 fctime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5029 fbtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5030 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
5031 fwinattributes
= format(int(zipinfo
.external_attr
), 'x').lower();
5032 if(not member
.is_dir()):
5033 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
5034 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
5035 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
5036 elif(member
.is_dir()):
5037 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
5038 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
5039 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
5040 elif(zipinfo
.create_system
==3):
5041 fwinattributes
= format(int(0), 'x').lower();
5042 fmode
= format(int(zipinfo
.external_attr
), 'x').lower();
5044 fwinattributes
= format(int(0), 'x').lower();
5045 if(not member
.is_dir()):
5046 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
5047 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
5048 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
5049 elif(member
.is_dir()):
5050 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
5051 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
5052 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
5055 except AttributeError:
5061 except AttributeError:
5068 userinfo
= pwd
.getpwuid(os
.getuid());
5069 funame
= userinfo
.pw_name
;
5072 except AttributeError:
5080 groupinfo
= grp
.getgrgid(os
.getgid());
5081 fgname
= groupinfo
.gr_name
;
5084 except AttributeError:
5088 fcontents
= "".encode('UTF-8');
5090 fcontents
= zipfp
.read(member
.filename
);
5091 ftypehex
= format(ftype
, 'x').lower();
5092 extrafields
= len(extradata
);
5093 extrafieldslist
= extradata
;
5094 catfextrafields
= extrafields
;
5095 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5096 if(len(extradata
)>0):
5097 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5098 extrasizelen
= len(extrasizestr
);
5099 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5100 catoutlist
= [ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fwinattributes
), 'x').lower(), format(int(fuid
), 'x').lower(), funame
, format(int(fgid
), 'x').lower(), fgname
, format(int(fcurfid
), 'x').lower(), format(int(fcurinode
), 'x').lower(), format(int(flinkcount
), 'x').lower(), format(int(fdev_minor
), 'x').lower(), format(int(fdev_major
), 'x').lower(), format(int(frdev_minor
), 'x').lower(), format(int(frdev_major
), 'x').lower(), extrasizelenhex
, format(catfextrafields
, 'x').lower()];
5101 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5102 catoutlenhex
= format(catoutlen
, 'x').lower();
5103 catoutlist
.insert(0, catoutlenhex
);
5104 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5105 if(len(extradata
)>0):
5106 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5107 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5108 catfnumfields
= catoutlen
;
5109 if(checksumtype
=="none" or checksumtype
==""):
5110 catfileheadercshex
= format(0, 'x').lower();
5111 catfilecontentcshex
= format(0, 'x').lower();
5112 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5113 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5114 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
5115 elif(checksumtype
=="crc16_ccitt"):
5116 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5117 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
5118 elif(checksumtype
=="adler32"):
5119 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5120 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
5121 elif(checksumtype
=="crc32"):
5122 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5123 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
5124 elif(checksumtype
=="crc64_ecma"):
5125 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5126 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
5127 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5128 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5129 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
5130 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5131 checksumoutstr
= hashlib
.new(checksumtype
);
5132 checksumoutstr
.update("".encode('UTF-8'));
5133 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5134 checksumoutstr
= hashlib
.new(checksumtype
);
5135 checksumoutstr
.update(fcontents
);
5136 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
5138 catfileheadercshex
= format(0, 'x').lower();
5139 catfilecontentcshex
= format(0, 'x').lower();
5140 catfhstart
= fheadtell
;
5141 fheadtell
+= len(catfileoutstr
);
5142 catfhend
= fheadtell
- 1;
5143 catfcontentstart
= fheadtell
;
5144 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5145 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
5146 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5147 catheaderdata
= [catheaersize
] + catoutlist
+ extradata
+ [checksumtype
, catfileheadercshex
, catfilecontentcshex
];
5148 if(checksumtype
=="none" or checksumtype
==""):
5149 catfileheadercshex
= format(0, 'x').lower();
5150 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5151 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5152 elif(checksumtype
=="crc16_ccitt"):
5153 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5154 elif(checksumtype
=="adler32"):
5155 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5156 elif(checksumtype
=="crc32"):
5157 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5158 elif(checksumtype
=="crc64_ecma"):
5159 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5160 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5161 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5162 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5163 checksumoutstr
= hashlib
.new(checksumtype
);
5164 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5165 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5167 catfileheadercshex
= format(0, 'x').lower();
5168 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5169 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5170 nullstrecd
= formatspecs
[5].encode('UTF-8');
5171 fheadtell
+= len(catfileoutstr
) + 1;
5172 catfcontentend
= fheadtell
- 1;
5173 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
5174 pyhascontents
= False;
5175 if(int(fsize
)>0 and not listonly
):
5176 pyhascontents
= True;
5177 if(int(fsize
)>0 and listonly
):
5179 pyhascontents
= False;
5180 catlist
['ffilelist'].update({fileidnum
: {'fid': fileidnum
, 'fidalt': fileidnum
, 'fheadersize': int(catheaersize
, 16), 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fwinattributes': fwinattributes
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': checksumtype
, 'fnumfields': catfnumfields
+ 2, 'frawheader': catheaderdata
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
5181 fileidnum
= fileidnum
+ 1;
5184 if(not rarfile_support
):
5185 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5188 if(rarfile_support
):
5189 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5190 advancedlist
= True;
5196 inodetocatinode
= {};
5198 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
5200 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
5202 rarfp
= rarfile
.RarFile(infile
, "r");
5203 rartest
= rarfp
.testrar();
5205 VerbosePrintOut("Bad file found!");
5206 fnumfiles
= int(len(rarfp
.infolist()));
5207 catver
= formatspecs
[6];
5208 fileheaderver
= str(int(catver
.replace(".", "")));
5209 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5210 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5211 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5212 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5213 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
5214 if(checksumtype
=="none" or checksumtype
==""):
5215 catfileheadercshex
= format(0, 'x').lower();
5216 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5217 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
5218 elif(checksumtype
=="crc16_ccitt"):
5219 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
5220 elif(checksumtype
=="adler32"):
5221 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5222 elif(checksumtype
=="crc32"):
5223 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5224 elif(checksumtype
=="crc64_ecma"):
5225 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5226 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5227 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5228 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5229 checksumoutstr
= hashlib
.new(checksumtype
);
5230 checksumoutstr
.update(fileheader
.encode('UTF-8'));
5231 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5233 catfileheadercshex
= format(0, 'x').lower();
5234 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
5235 fheadtell
= len(fileheader
);
5236 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5237 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
5240 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
5243 member
.external_attr
5245 except AttributeError:
5247 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
5250 member
.external_attr
5252 except AttributeError:
5257 if(re
.findall("^[.|/]", member
.filename
)):
5258 fname
= member
.filename
;
5260 fname
= "./"+member
.filename
;
5261 rarinfo
= rarfp
.getinfo(member
.filename
);
5263 VerbosePrintOut(fname
);
5264 if(is_unix
and member
.external_attr
!=0):
5265 fpremode
= int(member
.external_attr
);
5266 elif(member
.is_file()):
5267 fpremode
= stat
.S_IFREG
+ 438;
5268 elif(member
.is_symlink()):
5269 fpremode
= stat
.S_IFLNK
+ 438;
5270 elif(member
.is_dir()):
5271 fpremode
= stat
.S_IFDIR
+ 511;
5272 if(is_windows
and member
.external_attr
!=0):
5273 fwinattributes
= int(member
.external_attr
);
5275 fwinattributes
= int(0);
5278 if(member
.is_file()):
5280 elif(member
.is_symlink()):
5282 elif(member
.is_dir()):
5286 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
5287 fbasedir
= os
.path
.dirname(fname
);
5291 curfid
= curfid
+ 1;
5299 fsize
= member
.file_size
;
5302 fatime
= int(member
.atime
.timestamp());
5304 fatime
= int(member
.mtime
.timestamp());
5305 except AttributeError:
5306 fatime
= int(member
.mtime
.timestamp());
5307 fmtime
= int(member
.mtime
.timestamp());
5310 fctime
= int(member
.ctime
.timestamp());
5312 fctime
= int(member
.mtime
.timestamp());
5313 except AttributeError:
5314 fctime
= int(member
.mtime
.timestamp());
5315 fbtime
= int(member
.mtime
.timestamp());
5316 if(is_unix
and member
.external_attr
!=0):
5317 fmode
= format(int(member
.external_attr
), 'x').lower();
5318 fchmode
= format(int(stat
.S_IMODE(member
.external_attr
)), 'x').lower();
5319 ftypemod
= format(int(stat
.S_IFMT(member
.external_attr
)), 'x').lower();
5320 elif(member
.is_file()):
5321 fmode
= int(stat
.S_IFREG
+ 438)
5322 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
5323 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
5324 elif(member
.is_symlink()):
5325 fmode
= int(stat
.S_IFLNK
+ 438)
5326 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
5327 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
5328 elif(member
.is_dir()):
5329 fmode
= int(stat
.S_IFDIR
+ 511)
5330 fchmode
= int(stat
.S_IMODE(stat
.S_IFDIR
+ 511));
5331 ftypemod
= int(stat
.S_IFMT(stat
.S_IFDIR
+ 511));
5334 except AttributeError:
5340 except AttributeError:
5347 userinfo
= pwd
.getpwuid(os
.getuid());
5348 funame
= userinfo
.pw_name
;
5351 except AttributeError:
5359 groupinfo
= grp
.getgrgid(os
.getgid());
5360 fgname
= groupinfo
.gr_name
;
5363 except AttributeError:
5367 fcontents
= "".encode('UTF-8');
5369 fcontents
= rarfp
.read(member
.filename
);
5370 ftypehex
= format(ftype
, 'x').lower();
5371 extrafields
= len(extradata
);
5372 extrafieldslist
= extradata
;
5373 catfextrafields
= extrafields
;
5374 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5375 if(len(extradata
)>0):
5376 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5377 extrasizelen
= len(extrasizestr
);
5378 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5379 catoutlist
= [ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fwinattributes
), 'x').lower(), format(int(fuid
), 'x').lower(), funame
, format(int(fgid
), 'x').lower(), fgname
, format(int(fcurfid
), 'x').lower(), format(int(fcurinode
), 'x').lower(), format(int(flinkcount
), 'x').lower(), format(int(fdev_minor
), 'x').lower(), format(int(fdev_major
), 'x').lower(), format(int(frdev_minor
), 'x').lower(), format(int(frdev_major
), 'x').lower(), extrasizelenhex
, format(catfextrafields
, 'x').lower()];
5380 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5381 catoutlenhex
= format(catoutlen
, 'x').lower();
5382 catoutlist
.insert(0, catoutlenhex
);
5383 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5384 if(len(extradata
)>0):
5385 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5386 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5387 catfnumfields
= 24 + catfextrafields
;
5388 if(checksumtype
=="none" or checksumtype
==""):
5389 catfileheadercshex
= format(0, 'x').lower();
5390 catfilecontentcshex
= format(0, 'x').lower();
5391 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5392 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5393 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
5394 elif(checksumtype
=="crc16_ccitt"):
5395 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5396 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
5397 elif(checksumtype
=="adler32"):
5398 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5399 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
5400 elif(checksumtype
=="crc32"):
5401 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5402 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
5403 elif(checksumtype
=="crc64_ecma"):
5404 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5405 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
5406 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5407 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5408 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
5409 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5410 checksumoutstr
= hashlib
.new(checksumtype
);
5411 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5412 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5414 catfileheadercshex
= format(0, 'x').lower();
5415 catfilecontentcshex
= format(0, 'x').lower();
5416 catfhstart
= fheadtell
;
5417 fheadtell
+= len(catfileoutstr
);
5418 catfhend
= fheadtell
- 1;
5419 catfcontentstart
= fheadtell
;
5420 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5421 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
5422 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5423 catheaderdata
= [catheaersize
] + catoutlist
+ extradata
+ [checksumtype
, catfileheadercshex
, catfilecontentcshex
];
5424 if(checksumtype
=="none" or checksumtype
==""):
5425 catfileheadercshex
= format(0, 'x').lower();
5426 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5427 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5428 elif(checksumtype
=="crc16_ccitt"):
5429 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5430 elif(checksumtype
=="adler32"):
5431 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5432 elif(checksumtype
=="crc32"):
5433 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5434 elif(checksumtype
=="crc64_ecma"):
5435 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5436 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5437 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5438 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5439 checksumoutstr
= hashlib
.new(checksumtype
);
5440 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5441 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5443 catfileheadercshex
= format(0, 'x').lower();
5444 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5445 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5446 nullstrecd
= formatspecs
[5].encode('UTF-8');
5447 fheadtell
+= len(catfileoutstr
) + 1;
5448 catfcontentend
= fheadtell
- 1;
5449 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
5450 pyhascontents
= False;
5451 if(int(fsize
)>0 and not listonly
):
5452 pyhascontents
= True;
5453 if(int(fsize
)>0 and listonly
):
5455 pyhascontents
= False;
5456 catlist
['ffilelist'].update({fileidnum
: {'fid': fileidnum
, 'fidalt': fileidnum
, 'fheadersize': int(catheaersize
, 16), 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fwinattributes': fwinattributes
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': checksumtype
, 'fnumfields': catfnumfields
+ 2, 'frawheader': catheaderdata
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
5457 fileidnum
= fileidnum
+ 1;
5460 def ListDirToArray(infiles
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5461 outarray
= BytesIO();
5462 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
5463 listcatfiles
= ArchiveFileToArray(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5464 return listcatfiles
;
5466 def ArchiveFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5467 if(isinstance(infile
, dict)):
5468 listcatfiles
= infile
;
5470 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5471 infile
= RemoveWindowsPath(infile
);
5472 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5473 if(not listcatfiles
):
5475 catarray
= {'list': listcatfiles
, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
5477 catarray
.update({'catfp': listcatfiles
['catfp']});
5478 lenlist
= len(listcatfiles
['ffilelist']);
5480 lcfx
= int(listcatfiles
['fnumfiles']);
5481 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5482 lcfx
= int(lenlist
);
5484 lcfx
= int(listcatfiles
['fnumfiles']);
5486 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5487 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5488 catarray
['filetoid'].update(filetoidarray
);
5489 catarray
['idtofile'].update(idtofilearray
);
5490 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5491 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5492 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5493 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5494 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5495 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5496 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5497 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5498 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5499 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5500 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5501 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5502 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5503 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5504 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5505 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5506 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5507 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5508 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5509 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5510 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5511 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5512 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5513 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5514 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5515 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5516 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5517 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5518 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5519 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5520 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5524 create_alias_function("", __file_format_name__
, "ToArrayIndex", ArchiveFileToArrayIndex
);
5526 def ListDirToArrayIndexAlt(infiles
, dirlistfromtxt
=False, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5527 listcatfiles
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5528 if(not listcatfiles
):
5530 catarray
= {'list': listcatfiles
, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
5531 lenlist
= len(listcatfiles
['ffilelist']);
5533 lcfx
= int(listcatfiles
['fnumfiles']);
5534 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5535 lcfx
= int(lenlist
);
5537 lcfx
= int(listcatfiles
['fnumfiles']);
5539 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5540 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5541 catarray
['filetoid'].update(filetoidarray
);
5542 catarray
['idtofile'].update(idtofilearray
);
5543 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5544 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5545 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5546 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5547 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5548 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5549 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5550 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5551 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5552 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5553 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5554 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5555 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5556 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5557 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5558 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5559 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5560 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5561 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5562 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5563 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5564 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5565 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5566 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5567 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5568 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5569 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5570 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5571 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5572 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5573 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5577 def TarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5578 listcatfiles
= TarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5579 if(not listcatfiles
):
5581 catarray
= {'list': listcatfiles
, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
5582 lenlist
= len(listcatfiles
['ffilelist']);
5584 lcfx
= int(listcatfiles
['fnumfiles']);
5585 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5586 lcfx
= int(lenlist
);
5588 lcfx
= int(listcatfiles
['fnumfiles']);
5590 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5591 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5592 catarray
['filetoid'].update(filetoidarray
);
5593 catarray
['idtofile'].update(idtofilearray
);
5594 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5595 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5596 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5597 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5598 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5599 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5600 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5601 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5602 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5603 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5604 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5605 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5606 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5607 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5608 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5609 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5610 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5611 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5612 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5613 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5614 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5615 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5616 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5617 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5618 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5619 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5620 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5621 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5622 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5623 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5624 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5628 def ZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5629 listcatfiles
= ZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5630 if(not listcatfiles
):
5632 catarray
= {'list': listcatfiles
, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
5633 lenlist
= len(listcatfiles
['ffilelist']);
5635 lcfx
= int(listcatfiles
['fnumfiles']);
5636 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5637 lcfx
= int(lenlist
);
5639 lcfx
= int(listcatfiles
['fnumfiles']);
5641 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5642 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5643 catarray
['filetoid'].update(filetoidarray
);
5644 catarray
['idtofile'].update(idtofilearray
);
5645 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5646 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5647 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5648 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5649 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5650 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5651 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5652 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5653 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5654 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5655 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5656 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5657 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5658 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5659 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5660 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5661 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5662 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5663 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5664 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5665 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5666 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5667 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5668 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5669 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5670 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5671 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5672 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5673 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5674 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5675 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5679 if(not rarfile_support
):
5680 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5683 if(rarfile_support
):
5684 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5685 listcatfiles
= RarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5686 if(not listcatfiles
):
5688 catarray
= {'list': listcatfiles
, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
5689 lenlist
= len(listcatfiles
['ffilelist']);
5691 lcfx
= int(listcatfiles
['fnumfiles']);
5692 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5693 lcfx
= int(lenlist
);
5695 lcfx
= int(listcatfiles
['fnumfiles']);
5697 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5698 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5699 catarray
['filetoid'].update(filetoidarray
);
5700 catarray
['idtofile'].update(idtofilearray
);
5701 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5702 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5703 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5704 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5705 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5706 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5707 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5708 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5709 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5710 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5711 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5712 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5713 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5714 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5715 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5716 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5717 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5718 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5719 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5720 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5721 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5722 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5723 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5724 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5725 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5726 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5727 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5728 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5729 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5730 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5731 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5735 def ArchiveFileStringToArrayIndex(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5736 catfp
= BytesIO(catstr
);
5737 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5738 return listcatfiles
;
5740 create_alias_function("", __file_format_name__
, "StringToArrayIndex", ArchiveFileStringToArrayIndex
);
5742 def TarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5744 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5745 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5746 return listcatfiles
;
5748 def ZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5750 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5751 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5752 return listcatfiles
;
5754 if(not rarfile_support
):
5755 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5758 if(rarfile_support
):
5759 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5761 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5762 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5763 return listcatfiles
;
5765 def ListDirToArrayIndex(infiles
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5766 outarray
= BytesIO();
5767 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, verbose
, True);
5768 listcatfiles
= ArchiveFileToArrayIndex(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
)
5769 return listcatfiles
;
5771 def RePackArchiveFile(infile
, outfile
, compression
="auto", compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5772 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
5773 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzop', 'lzo', 'lzma', 'xz'];
5774 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', 'lzop', '.lzo', '.lzma', '.xz'];
5775 if(isinstance(infile
, dict)):
5776 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5777 listcatfiles
= prelistcatfiles
['list'];
5779 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5780 infile
= RemoveWindowsPath(infile
);
5782 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5783 listcatfiles
= prelistcatfiles
['list'];
5785 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5786 if(outfile
!="-" and not hasattr(infile
, "read") and not hasattr(outfile
, "write")):
5787 outfile
= RemoveWindowsPath(outfile
);
5788 checksumtype
= checksumtype
.lower();
5789 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
5790 checksumtype
="crc32";
5791 if(checksumtype
=="none"):
5793 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
5795 if(compression
not in compressionlist
and compression
is None):
5796 compression
= "auto";
5798 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5799 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
5800 if(os
.path
.exists(outfile
)):
5802 if(not listcatfiles
):
5807 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
5809 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
5812 fbasename
= os
.path
.splitext(outfile
)[0];
5813 fextname
= os
.path
.splitext(outfile
)[1];
5814 catfp
= CompressOpenFile(outfile
, compressionlevel
);
5815 catver
= formatspecs
[6];
5816 fileheaderver
= str(int(catver
.replace(".", "")));
5817 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5818 catfp
.write(fileheader
.encode('UTF-8'));
5819 lenlist
= len(listcatfiles
['ffilelist']);
5820 fnumfiles
= int(listcatfiles
['fnumfiles']);
5821 if(lenlist
>fnumfiles
or lenlist
<fnumfiles
):
5822 fnumfiles
= lenlist
;
5823 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5824 fnumfilesa
= AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
5825 if(checksumtype
=="none" or checksumtype
==""):
5826 catfileheadercshex
= format(0, 'x').lower();
5827 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5828 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
5829 elif(checksumtype
=="crc16_ccitt"):
5830 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
5831 elif(checksumtype
=="adler32"):
5832 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
5833 elif(checksumtype
=="crc32"):
5834 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
5835 elif(checksumtype
=="crc64_ecma"):
5836 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5837 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5838 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5839 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5840 checksumoutstr
= hashlib
.new(checksumtype
);
5841 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
5842 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5844 catfileheadercshex
= format(0, 'x').lower();
5845 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
5846 catfp
.write(fnumfilesa
.encode('UTF-8'));
5849 os
.fsync(catfp
.fileno());
5850 except io
.UnsupportedOperation
:
5852 except AttributeError:
5854 lenlist
= len(listcatfiles
['ffilelist']);
5855 fnumfiles
= int(listcatfiles
['fnumfiles']);
5857 lcfx
= int(listcatfiles
['fnumfiles']);
5858 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5859 lcfx
= int(lenlist
);
5861 lcfx
= int(listcatfiles
['fnumfiles']);
5869 catfhstart
= catfp
.tell();
5870 if(re
.findall("^[.|/]", listcatfiles
['ffilelist'][reallcfi
]['fname'])):
5871 fname
= listcatfiles
['ffilelist'][reallcfi
]['fname'];
5873 fname
= "./"+listcatfiles
['ffilelist'][reallcfi
]['fname'];
5875 VerbosePrintOut(fname
);
5876 fheadersize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fheadersize']), 'x').lower();
5877 fsize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fsize']), 'x').lower();
5878 flinkname
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
5879 fatime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fatime']), 'x').lower();
5880 fmtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmtime']), 'x').lower();
5881 fctime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fctime']), 'x').lower();
5882 fbtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fbtime']), 'x').lower();
5883 fmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmode']), 'x').lower();
5884 fchmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fchmode']), 'x').lower();
5885 fuid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fuid']), 'x').lower();
5886 funame
= listcatfiles
['ffilelist'][reallcfi
]['funame'];
5887 fgid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fgid']), 'x').lower();
5888 fgname
= listcatfiles
['ffilelist'][reallcfi
]['fgname'];
5889 finode
= listcatfiles
['ffilelist'][reallcfi
]['finode'];
5890 flinkcount
= listcatfiles
['ffilelist'][reallcfi
]['flinkcount'];
5891 fwinattributes
= flinkinfo
['ffilelist'][reallcfi
]['fwinattributes'];
5892 fdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fminor']), 'x').lower();
5893 fdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmajor']), 'x').lower();
5894 frdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frminor']), 'x').lower();
5895 frdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frmajor']), 'x').lower();
5896 if(len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] and len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>0):
5897 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
5898 if(len(extradata
) > 0):
5899 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(extradata
);
5900 listcatfiles
['ffilelist'][reallcfi
]['fextralist'] = extradata
;
5901 extrafields
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']), 'x').lower();
5902 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5903 if(len(extradata
)>0):
5904 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5905 extrasizelen
= format(len(extrasizestr
), 'x').lower();
5906 fcontents
= listcatfiles
['ffilelist'][reallcfi
]['fcontents'];
5908 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']==1 or listcatfiles
['ffilelist'][reallcfi
]['ftype']==2):
5909 getflinkpath
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
5910 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
5911 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
5912 fheadersize
= format(int(flinkinfo
['fheadersize']), 'x').lower();
5913 fsize
= format(int(flinkinfo
['fsize']), 'x').lower();
5914 flinkname
= flinkinfo
['flinkname'];
5915 fatime
= format(int(flinkinfo
['fatime']), 'x').lower();
5916 fmtime
= format(int(flinkinfo
['fmtime']), 'x').lower();
5917 fctime
= format(int(flinkinfo
['fctime']), 'x').lower();
5918 fbtime
= format(int(flinkinfo
['fbtime']), 'x').lower();
5919 fmode
= format(int(flinkinfo
['fmode']), 'x').lower();
5920 fchmode
= format(int(flinkinfo
['fchmode']), 'x').lower();
5921 fuid
= format(int(flinkinfo
['fuid']), 'x').lower();
5922 funame
= flinkinfo
['funame'];
5923 fgid
= format(int(flinkinfo
['fgid']), 'x').lower();
5924 fgname
= flinkinfo
['fgname'];
5925 finode
= flinkinfo
['finode'];
5926 flinkcount
= flinkinfo
['flinkcount'];
5927 fwinattributes
= flinkinfo
['fwinattributes'];
5928 fdev_minor
= format(int(flinkinfo
['fminor']), 'x').lower();
5929 fdev_major
= format(int(flinkinfo
['fmajor']), 'x').lower();
5930 frdev_minor
= format(int(flinkinfo
['frminor']), 'x').lower();
5931 frdev_major
= format(int(flinkinfo
['frmajor']), 'x').lower();
5932 if(len(flinkinfo
['fextralist'])>flinkinfo
['fextrafields'] and len(flinkinfo
['fextralist'])>0):
5933 flinkinfo
['fextrafields'] = len(flinkinfo
['fextralist']);
5934 if(len(extradata
) > 0):
5935 flinkinfo
['fextrafields'] = len(extradata
);
5936 flinkinfo
['fextralist'] = extradata
;
5937 extrafields
= format(int(flinkinfo
['fextrafields']), 'x').lower();
5938 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5939 if(len(extradata
)>0):
5940 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5941 extrasizelen
= format(len(extrasizestr
), 'x').lower();
5942 fcontents
= flinkinfo
['fcontents'];
5943 if(flinkinfo
['ftype']!=0 and flinkinfo
['ftype']!=7):
5945 fcontents
= fcontents
.encode('UTF-8');
5946 except AttributeError:
5948 ftypehex
= format(flinkinfo
['ftype'], 'x').lower();
5950 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=0 and listcatfiles
['ffilelist'][reallcfi
]['ftype']!=7):
5952 fcontents
= fcontents
.encode('UTF-8');
5953 except AttributeError:
5955 ftypehex
= format(listcatfiles
['ffilelist'][reallcfi
]['ftype'], 'x').lower();
5956 fcurfid
= format(curfid
, 'x').lower();
5957 if(not followlink
and finode
!=0):
5958 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=1):
5959 fcurinode
= format(int(curinode
), 'x').lower();
5960 inodetofile
.update({curinode
: fname
});
5961 filetoinode
.update({fname
: curinode
});
5962 curinode
= curinode
+ 1;
5964 fcurinode
= format(int(filetoinode
[flinkname
]), 'x').lower();
5966 fcurinode
= format(int(curinode
), 'x').lower();
5967 curinode
= curinode
+ 1;
5968 curfid
= curfid
+ 1;
5969 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
];
5970 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5971 catoutlenhex
= format(catoutlen
, 'x').lower();
5972 catoutlist
.insert(0, catoutlenhex
);
5973 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5974 if(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']>0):
5975 extrafieldslist
= [];
5977 exil
= listcatfiles
['ffilelist'][reallcfi
]['fextrafields'];
5979 extrafieldslist
.append(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
5981 catfileoutstr
+= AppendNullBytes([extrafieldslist
], formatspecs
[5]);
5982 catfileoutstr
+= AppendNullBytes([checksumtype
], formatspecs
[5]);
5983 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
5984 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
5985 if(checksumtype
=="none" or checksumtype
==""):
5986 catfileheadercshex
= format(0, 'x').lower();
5987 catfilecontentcshex
= format(0, 'x').lower();
5988 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5989 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5990 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
5991 elif(checksumtype
=="crc16_ccitt"):
5992 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5993 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
5994 elif(checksumtype
=="adler32"):
5995 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5996 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
5997 elif(checksumtype
=="crc32"):
5998 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5999 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
6000 elif(checksumtype
=="crc64_ecma"):
6001 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6002 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
6003 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
6004 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6005 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
6006 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
6007 checksumoutstr
= hashlib
.new(checksumtype
);
6008 checksumoutstr
.update("".encode('UTF-8'));
6009 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
6010 checksumoutstr
= hashlib
.new(checksumtype
);
6011 checksumoutstr
.update(fcontents
);
6012 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
6014 catfileheadercshex
= format(0, 'x').lower();
6015 catfilecontentcshex
= format(0, 'x').lower();
6016 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
6017 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
6018 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
6019 if(checksumtype
=="none" or checksumtype
==""):
6020 catfileheadercshex
= format(0, 'x').lower();
6021 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
6022 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
6023 elif(checksumtype
=="crc16_ccitt"):
6024 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
6025 elif(checksumtype
=="adler32"):
6026 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
6027 elif(checksumtype
=="crc32"):
6028 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
6029 elif(checksumtype
=="crc64_ecma"):
6030 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6031 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
6032 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6033 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
6034 checksumoutstr
= hashlib
.new(checksumtype
);
6035 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
6036 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
6038 catfileheadercshex
= format(0, 'x').lower();
6039 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
6040 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
6041 nullstrecd
= formatspecs
[5].encode('UTF-8');
6042 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
6043 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
6044 catfp
.write(catfileout
);
6047 os
.fsync(catfp
.fileno());
6048 except io
.UnsupportedOperation
:
6050 except AttributeError:
6053 reallcfi
= reallcfi
+ 1;
6054 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
6055 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
6058 os
.fsync(catfp
.fileno());
6059 except io
.UnsupportedOperation
:
6061 except AttributeError:
6065 if(hasattr(sys
.stdout
, "buffer")):
6066 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
6068 shutil
.copyfileobj(catfp
, sys
.stdout
);
6069 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
6070 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
6072 upload_file_to_internet_file(catfp
, outfile
);
6080 create_alias_function("RePack", __file_format_name__
, "", RePackArchiveFile
);
6082 def RePackArchiveFileFromString(catstr
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6083 catfp
= BytesIO(catstr
);
6084 listcatfiles
= RePackArchiveFile(catfp
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
6085 return listcatfiles
;
6087 create_alias_function("RePack", __file_format_name__
, "FromString", RePackArchiveFileFromString
);
6089 def PackArchiveFileFromListDir(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, skipchecksum
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6090 outarray
= BytesIO();
6091 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
6092 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
6093 return listcatfiles
;
6095 create_alias_function("Pack", __file_format_name__
, "FromListDir", PackArchiveFileFromListDir
);
6097 def ArchiveFileArrayBase64Encode(infile
, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6099 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6100 if(isinstance(infile
, dict)):
6101 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6102 listcatfiles
= prelistcatfiles
['list'];
6104 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6105 infile
= RemoveWindowsPath(infile
);
6107 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6108 listcatfiles
= prelistcatfiles
['list'];
6110 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6111 if(not listcatfiles
):
6113 lenlist
= len(listcatfiles
['ffilelist']);
6114 fnumfiles
= int(listcatfiles
['fnumfiles']);
6116 lcfx
= int(listcatfiles
['fnumfiles']);
6117 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6118 lcfx
= int(lenlist
);
6120 lcfx
= int(listcatfiles
['fnumfiles']);
6122 if(listcatfiles
['ffilelist'][lcfi
]['fhascontents']):
6123 listcatfiles
['ffilelist'][lcfi
]['fcontents'] = base64
.b64encode(listcatfiles
['ffilelist'][lcfi
]['fcontents']).decode("UTF-8");
6125 return listcatfiles
;
6127 create_alias_function("", __file_format_name__
, "ArrayBase64Encode", ArchiveFileArrayBase64Encode
);
6129 def ArchiveFileArrayBase64Decode(infile
, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6131 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6132 if(isinstance(infile
, dict)):
6133 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6134 listcatfiles
= prelistcatfiles
['list'];
6136 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6137 infile
= RemoveWindowsPath(infile
);
6139 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6140 listcatfiles
= prelistcatfiles
['list'];
6142 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6143 if(not listcatfiles
):
6145 lenlist
= len(listcatfiles
['ffilelist']);
6146 fnumfiles
= int(listcatfiles
['fnumfiles']);
6148 lcfx
= int(listcatfiles
['fnumfiles']);
6149 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6150 lcfx
= int(lenlist
);
6152 lcfx
= int(listcatfiles
['fnumfiles']);
6154 if(listcatfiles
['ffilelist'][lcfi
]['fhascontents']):
6155 listcatfiles
['ffilelist'][lcfi
]['fcontents'] = base64
.b64decode(listcatfiles
['ffilelist'][lcfi
]['fcontents'].encode("UTF-8"));
6157 return listcatfiles
;
6159 create_alias_function("", __file_format_name__
, "ArrayBase64Decode", ArchiveFileArrayBase64Decode
);
6161 def UnPackArchiveFile(infile
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, preservepermissions
=True, preservetime
=True, verbose
=False, returnfp
=False):
6162 if(outdir
is not None):
6163 outdir
= RemoveWindowsPath(outdir
);
6165 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6166 if(isinstance(infile
, dict)):
6167 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6168 listcatfiles
= prelistcatfiles
['list'];
6170 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6171 infile
= RemoveWindowsPath(infile
);
6173 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6174 listcatfiles
= prelistcatfiles
['list'];
6176 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6177 if(not listcatfiles
):
6179 lenlist
= len(listcatfiles
['ffilelist']);
6180 fnumfiles
= int(listcatfiles
['fnumfiles']);
6182 lcfx
= int(listcatfiles
['fnumfiles']);
6183 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6184 lcfx
= int(lenlist
);
6186 lcfx
= int(listcatfiles
['fnumfiles']);
6192 userinfo
= pwd
.getpwuid(listcatfiles
['ffilelist'][lcfi
]['fuid']);
6193 funame
= userinfo
.pw_name
;
6202 groupinfo
= grp
.getgrgid(listcatfiles
['ffilelist'][lcfi
]['fgid']);
6203 fgname
= groupinfo
.gr_name
;
6209 VerbosePrintOut(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6210 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6211 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
6212 fpc
.write(listcatfiles
['ffilelist'][lcfi
]['fcontents'])
6215 os
.fsync(fpc
.fileno())
6216 except io
.UnsupportedOperation
:
6218 except AttributeError:
6220 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
6221 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
6222 if(preservepermissions
):
6223 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6225 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
6226 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6228 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6229 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
6230 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
6235 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
6236 funame
= userinfo
.pw_name
;
6245 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
6246 fgname
= groupinfo
.gr_name
;
6251 if(flinkinfo
['ftype'] == 0 or flinkinfo
['ftype'] == 7):
6252 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
6253 fpc
.write(flinkinfo
['fcontents'])
6256 os
.fsync(fpc
.fileno())
6257 except io
.UnsupportedOperation
:
6259 except AttributeError:
6261 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6262 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6263 if(preservepermissions
):
6264 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6266 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6267 if(flinkinfo
['ftype']==1):
6268 os
.link(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6269 if(flinkinfo
['ftype']==2):
6270 os
.symlink(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6271 if(flinkinfo
['ftype']==5):
6272 if(preservepermissions
):
6273 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6275 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6276 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6277 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6278 if(preservepermissions
):
6279 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6281 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6282 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
6283 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6285 os
.link(listcatfiles
['ffilelist'][lcfi
]['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6286 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6288 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6289 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
6290 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
6295 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
6296 funame
= userinfo
.pw_name
;
6305 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
6306 fgname
= groupinfo
.gr_name
;
6311 if(flinkinfo
['ftype']==0 or flinkinfo
['ftype']==7):
6312 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
6313 fpc
.write(flinkinfo
['fcontents'])
6316 os
.fsync(fpc
.fileno())
6317 except io
.UnsupportedOperation
:
6319 except AttributeError:
6321 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6322 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6323 if(preservepermissions
):
6324 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6326 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6327 if(flinkinfo
['ftype']==1):
6328 os
.link(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6329 if(flinkinfo
['ftype']==2):
6330 os
.symlink(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6331 if(flinkinfo
['ftype']==5):
6332 if(preservepermissions
):
6333 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6335 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6336 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6337 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6338 if(preservepermissions
):
6339 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6341 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6342 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
6343 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6345 os
.symlink(listcatfiles
['ffilelist'][lcfi
]['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6346 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6347 if(preservepermissions
):
6348 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6350 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6351 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
6352 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
6353 if(preservepermissions
):
6354 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6356 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
6357 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6 and hasattr(os
, "mkfifo")):
6358 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6361 return listcatfiles
['ffilelist']['catfp'];
6365 create_alias_function("UnPack", __file_format_name__
, "", UnPackArchiveFile
);
6367 if(hasattr(shutil
, "register_unpack_format")):
6368 def UnPackArchiveFileFunc(archive_name
, extract_dir
=None, **kwargs
):
6369 return UnPackArchiveFile(archive_name
, extract_dir
, False, 0, 0, False, __file_format_delimiter__
, False, False);
6370 create_alias_function("UnPack", __file_format_name__
, "Func", UnPackArchiveFileFunc
);
6372 def UnPackArchiveFileString(catstr
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6373 catfp
= BytesIO(catstr
);
6374 listcatfiles
= UnPackArchiveFile(catfp
, outdir
, followlink
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6375 return listcatfiles
;
6377 create_alias_function("UnPack", __file_format_name__
, "String", UnPackArchiveFileString
);
6379 def ArchiveFileListFiles(infile
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6380 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6381 if(isinstance(infile
, dict)):
6382 listcatfiles
= infile
;
6384 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6385 infile
= RemoveWindowsPath(infile
);
6386 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, True, skipchecksum
, formatspecs
, returnfp
);
6387 if(not listcatfiles
):
6389 lenlist
= len(listcatfiles
['ffilelist']);
6390 fnumfiles
= int(listcatfiles
['fnumfiles']);
6392 lcfx
= int(listcatfiles
['fnumfiles']);
6393 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6394 lcfx
= int(lenlist
);
6396 lcfx
= int(listcatfiles
['fnumfiles']);
6399 returnval
.update({lcfi
: listcatfiles
['ffilelist'][lcfi
]['fname']});
6401 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
6403 permissions
= { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
6404 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'];
6405 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6406 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " link to " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6407 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6408 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " -> " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6409 fuprint
= listcatfiles
['ffilelist'][lcfi
]['funame'];
6410 if(len(fuprint
)<=0):
6411 fuprint
= listcatfiles
['ffilelist'][lcfi
]['fuid'];
6412 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgname'];
6413 if(len(fgprint
)<=0):
6414 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgid'];
6415 VerbosePrintOut(PrintPermissionString(listcatfiles
['ffilelist'][lcfi
]['fmode'], listcatfiles
['ffilelist'][lcfi
]['ftype']) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(listcatfiles
['ffilelist'][lcfi
]['fsize']).rjust(15) + " " + datetime
.datetime
.utcfromtimestamp(listcatfiles
['ffilelist'][lcfi
]['fmtime']).strftime('%Y-%m-%d %H:%M') + " " + printfname
));
6418 return listcatfiles
['catfp'];
6422 create_alias_function("", __file_format_name__
, "ListFiles", ArchiveFileListFiles
);
6424 def ArchiveFileStringListFiles(catstr
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6425 catfp
= BytesIO(catstr
);
6426 listcatfiles
= ArchiveFileListFiles(catstr
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6427 return listcatfiles
;
6429 create_alias_function("", __file_format_name__
, "StringListFiles", ArchiveFileListFiles
);
6431 def ArchiveFileListFilesAlt(infile
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6432 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6433 if(isinstance(infile
, dict)):
6434 listcatfiles
= infile
;
6436 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6437 infile
= RemoveWindowsPath(infile
);
6438 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, True, skipchecksum
, formatspecs
, returnfp
);
6439 if(not listcatfiles
):
6441 lenlist
= len(listcatfiles
['ffilelist']);
6442 fnumfiles
= int(listcatfiles
['fnumfiles']);
6444 lcfx
= int(listcatfiles
['fnumfiles']);
6445 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6446 lcfx
= int(lenlist
);
6448 lcfx
= int(listcatfiles
['fnumfiles']);
6451 returnval
.update({lcfi
: listcatfiles
['ffilelist'][lcfi
]['fname']});
6453 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
6455 permissions
= { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
6456 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'];
6457 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6458 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " link to " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6459 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6460 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " -> " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6461 fuprint
= listcatfiles
['ffilelist'][lcfi
]['funame'];
6462 if(len(fuprint
)<=0):
6463 fuprint
= listcatfiles
['ffilelist'][lcfi
]['fuid'];
6464 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgname'];
6465 if(len(fgprint
)<=0):
6466 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgid'];
6467 VerbosePrintOut(PrintPermissionString(listcatfiles
['ffilelist'][lcfi
]['fmode'], listcatfiles
['ffilelist'][lcfi
]['ftype']) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(listcatfiles
['ffilelist'][lcfi
]['fsize']).rjust(15) + " " + datetime
.datetime
.utcfromtimestamp(listcatfiles
['ffilelist'][lcfi
]['fmtime']).strftime('%Y-%m-%d %H:%M') + " " + printfname
));
6470 return listcatfiles
['catfp'];
6474 create_alias_function("", __file_format_name__
, "ListFilesAlt", ArchiveFileListFilesAlt
);
6476 def ArchiveFileStringListFilesAlt(catstr
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6477 catfp
= BytesIO(catstr
);
6478 listcatfiles
= ArchiveFileListFilesAlt(catstr
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6479 return listcatfiles
;
6481 create_alias_function("", __file_format_name__
, "StringListFilesAlt", ArchiveFileListFilesAlt
);
6483 def TarFileListFiles(infile
, verbose
=False, returnfp
=False):
6484 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6487 if(hasattr(sys
.stdin
, "buffer")):
6488 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
6490 shutil
.copyfileobj(sys
.stdin
, infile
);
6495 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
6496 infile
= download_file_from_internet_file(infile
);
6501 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6503 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
6505 if(not tarfile
.is_tarfile(infile
)):
6507 except AttributeError:
6508 if(not is_tarfile(infile
)):
6513 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
6514 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
6516 tarfp
= tarfile
.open(infile
, "r");
6517 except FileNotFoundError
:
6519 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
6520 returnval
.update({lcfi
: member
.name
});
6521 fpremode
= member
.mode
;
6522 ffullmode
= member
.mode
;
6526 ffullmode
= member
.mode
+ stat
.S_IFREG
;
6528 elif(member
.isdev()):
6529 ffullmode
= member
.mode
;
6531 elif(member
.islnk()):
6532 ffullmode
= member
.mode
+ stat
.S_IFREG
;
6534 elif(member
.issym()):
6535 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
6537 elif(member
.ischr()):
6538 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
6540 elif(member
.isblk()):
6541 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
6543 elif(member
.isdir()):
6544 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
6546 elif(member
.isfifo()):
6547 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
6549 elif(member
.issparse()):
6550 ffullmode
= member
.mode
;
6553 VerbosePrintOut(member
.name
);
6555 permissions
= { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
6556 printfname
= member
.name
;
6558 printfname
= member
.name
+ " link to " + member
.linkname
;
6559 elif(member
.issym()):
6560 printfname
= member
.name
+ " -> " + member
.linkname
;
6561 fuprint
= member
.uname
;
6562 if(len(fuprint
)<=0):
6563 fuprint
= member
.uid
;
6564 fgprint
= member
.gname
;
6565 if(len(fgprint
)<=0):
6566 fgprint
= member
.gid
;
6567 VerbosePrintOut(PrintPermissionString(ffullmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(member
.size
).rjust(15) + " " + datetime
.datetime
.utcfromtimestamp(member
.mtime
).strftime('%Y-%m-%d %H:%M') + " " + printfname
));
6570 return listcatfiles
['catfp'];
6574 def ZipFileListFiles(infile
, verbose
=False, returnfp
=False):
6575 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6578 if(hasattr(sys
.stdin
, "buffer")):
6579 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
6581 shutil
.copyfileobj(sys
.stdin
, infile
);
6586 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
6587 infile
= download_file_from_internet_file(infile
);
6592 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6596 if(not zipfile
.is_zipfile(infile
)):
6599 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
6600 except FileNotFoundError
:
6604 ziptest
= zipfp
.testzip();
6606 VerbosePrintOut("Bad file found!");
6607 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
6608 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
6609 fwinattributes
= int(zipinfo
.external_attr
);
6610 if(not member
.is_dir()):
6611 fmode
= int(stat
.S_IFREG
+ 438);
6612 fchmode
= int(stat
.S_IMODE(fmode
));
6613 ftypemod
= int(stat
.S_IFMT(fmode
));
6614 elif(member
.is_dir()):
6615 fmode
= int(stat
.S_IFDIR
+ 511);
6616 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
6617 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
6618 elif(zipinfo
.create_system
==3):
6619 fwinattributes
=int(0);
6620 fmode
= int(zipinfo
.external_attr
);
6621 fchmode
= int(stat
.S_IMODE(fmode
));
6622 ftypemod
= int(stat
.S_IFMT(fmode
));
6624 fwinattributes
= int(0);
6625 if(not member
.is_dir()):
6626 fmode
= int(stat
.S_IFREG
+ 438);
6627 fchmode
= int(stat
.S_IMODE(fmode
));
6628 ftypemod
= int(stat
.S_IFMT(fmode
));
6629 elif(member
.is_dir()):
6630 fmode
= int(stat
.S_IFDIR
+ 511);
6631 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
6632 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
6633 returnval
.update({lcfi
: member
.filename
});
6635 VerbosePrintOut(member
.filename
);
6637 permissions
= { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
6639 for fmodval
in str(oct(fmode
))[-3:]:
6640 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
6641 if(not member
.is_dir()):
6643 permissionstr
= "-" + permissionstr
;
6644 elif(member
.is_dir()):
6646 permissionstr
= "d" + permissionstr
;
6647 printfname
= member
.filename
;
6649 fuid
= int(os
.getuid());
6650 except AttributeError:
6655 fgid
= int(os
.getgid());
6656 except AttributeError:
6663 userinfo
= pwd
.getpwuid(os
.getuid());
6664 funame
= userinfo
.pw_name
;
6667 except AttributeError:
6675 groupinfo
= grp
.getgrgid(os
.getgid());
6676 fgname
= groupinfo
.gr_name
;
6679 except AttributeError:
6684 if(len(fuprint
)<=0):
6685 fuprint
= str(fuid
);
6687 if(len(fgprint
)<=0):
6688 fgprint
= str(fgid
);
6689 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(member
.file_size
).rjust(15) + " " + datetime
.datetime
.utcfromtimestamp(int(time
.mktime(member
.date_time
+ (0, 0, -1)))).strftime('%Y-%m-%d %H:%M') + " " + printfname
));
6692 return listcatfiles
['catfp'];
6696 if(not rarfile_support
):
6697 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
6698 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6699 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6702 if(rarfile_support
):
6703 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
6704 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6705 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6707 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
6711 rarfp
= rarfile
.RarFile(infile
, "r");
6712 rartest
= rarfp
.testrar();
6714 VerbosePrintOut("Bad file found!");
6715 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
6718 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
6721 member
.external_attr
6723 except AttributeError:
6725 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
6728 member
.external_attr
6730 except AttributeError:
6735 if(is_unix
and member
.external_attr
!=0):
6736 fpremode
= int(member
.external_attr
);
6737 elif(member
.is_file()):
6738 fpremode
= int(stat
.S_IFREG
+ 438);
6739 elif(member
.is_symlink()):
6740 fpremode
= int(stat
.S_IFLNK
+ 438);
6741 elif(member
.is_dir()):
6742 fpremode
= int(stat
.S_IFDIR
+ 511);
6743 if(is_windows
and member
.external_attr
!=0):
6744 fwinattributes
= int(member
.external_attr
);
6746 fwinattributes
= int(0);
6747 if(is_unix
and member
.external_attr
!=0):
6748 fmode
= int(member
.external_attr
);
6749 fchmode
= int(stat
.S_IMODE(member
.external_attr
));
6750 ftypemod
= int(stat
.S_IFMT(member
.external_attr
));
6751 elif(member
.is_file()):
6752 fmode
= int(stat
.S_IFREG
+ 438);
6753 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
6754 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
6755 elif(member
.is_symlink()):
6756 fmode
= int(stat
.S_IFLNK
+ 438);
6757 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFLNK
+ 438)));
6758 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFLNK
+ 438)));
6759 elif(member
.is_dir()):
6760 fmode
= int(stat
.S_IFDIR
+ 511);
6761 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
6762 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
6763 returnval
.update({lcfi
: member
.filename
});
6765 VerbosePrintOut(member
.filename
);
6767 permissions
= { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
6769 for fmodval
in str(oct(fmode
))[-3:]:
6770 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
6771 if(member
.is_file()):
6773 permissionstr
= "-" + permissionstr
;
6774 printfname
= member
.filename
;
6775 elif(member
.is_symlink()):
6777 permissionstr
= "l" + permissionstr
;
6778 printfname
= member
.name
+ " -> " + member
.read().decode("UTF-8");
6779 elif(member
.is_dir()):
6781 permissionstr
= "d" + permissionstr
;
6782 printfname
= member
.filename
;
6784 fuid
= int(os
.getuid());
6785 except AttributeError:
6790 fgid
= int(os
.getgid());
6791 except AttributeError:
6798 userinfo
= pwd
.getpwuid(os
.getuid());
6799 funame
= userinfo
.pw_name
;
6802 except AttributeError:
6810 groupinfo
= grp
.getgrgid(os
.getgid());
6811 fgname
= groupinfo
.gr_name
;
6814 except AttributeError:
6819 if(len(fuprint
)<=0):
6820 fuprint
= str(fuid
);
6822 if(len(fgprint
)<=0):
6823 fgprint
= str(fgid
);
6824 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(member
.file_size
).rjust(15) + " " + member
.mtime
.strftime('%Y-%m-%d %H:%M') + " " + printfname
));
6827 return listcatfiles
['catfp'];
6831 def ListDirListFiles(infiles
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6832 outarray
= BytesIO();
6833 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, False, True);
6834 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6835 return listcatfiles
;
6837 def ListDirListFilesAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=True, seekstart
=0, seekend
=0, skipchecksum
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6838 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, formatspecs
, verbose
);
6839 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6840 return listcatfiles
;
6842 def PackArchiveFileFromListDirAlt(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, skipchecksum
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6843 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, False, checksumtype
, extradata
, formatspecs
, False);
6844 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, followlink
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
6845 return listcatfiles
;
6847 create_alias_function("Pack", __file_format_name__
, "FromListDirAlt", PackArchiveFileFromListDirAlt
);
6849 def PackArchiveFileFromTarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6850 outarray
= TarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
6851 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
6852 return listcatfiles
;
6854 create_alias_function("Pack", __file_format_name__
, "FromTarFileAlt", PackArchiveFileFromTarFileAlt
);
6856 def PackArchiveFileFromZipFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6857 outarray
= ZipFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
6858 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
6859 return listcatfiles
;
6861 create_alias_function("Pack", __file_format_name__
, "FromZipFileAlt", PackArchiveFileFromZipFileAlt
);
6863 if(not rarfile_support
):
6864 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6867 if(rarfile_support
):
6868 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6869 outarray
= RarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
6870 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
6871 return listcatfiles
;
6873 create_alias_function("Pack", __file_format_name__
, "FromRarFileAlt", PackArchiveFileFromRarFileAlt
);
6875 def download_file_from_ftp_file(url
):
6876 urlparts
= urlparse(url
);
6877 file_name
= os
.path
.basename(urlparts
.path
);
6878 file_dir
= os
.path
.dirname(urlparts
.path
);
6879 if(urlparts
.username
is not None):
6880 ftp_username
= urlparts
.username
;
6882 ftp_username
= "anonymous";
6883 if(urlparts
.password
is not None):
6884 ftp_password
= urlparts
.password
;
6885 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6886 ftp_password
= "anonymous";
6889 if(urlparts
.scheme
=="ftp"):
6891 elif(urlparts
.scheme
=="ftps"):
6895 if(urlparts
.scheme
=="sftp"):
6897 return download_file_from_pysftp_file(url
);
6899 return download_file_from_sftp_file(url
);
6900 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6901 return download_file_from_http_file(url
);
6902 ftp_port
= urlparts
.port
;
6903 if(urlparts
.port
is None):
6906 ftp
.connect(urlparts
.hostname
, ftp_port
);
6907 except socket
.gaierror
:
6908 log
.info("Error With URL "+url
);
6910 except socket
.timeout
:
6911 log
.info("Error With URL "+url
);
6913 ftp
.login(urlparts
.username
, urlparts
.password
);
6914 if(urlparts
.scheme
=="ftps"):
6916 ftpfile
= BytesIO();
6917 ftp
.retrbinary("RETR "+urlparts
.path
, ftpfile
.write
);
6918 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
6923 def download_file_from_ftp_string(url
):
6924 ftpfile
= download_file_from_ftp_file(url
);
6925 return ftpfile
.read();
6927 def upload_file_to_ftp_file(ftpfile
, url
):
6928 urlparts
= urlparse(url
);
6929 file_name
= os
.path
.basename(urlparts
.path
);
6930 file_dir
= os
.path
.dirname(urlparts
.path
);
6931 if(urlparts
.username
is not None):
6932 ftp_username
= urlparts
.username
;
6934 ftp_username
= "anonymous";
6935 if(urlparts
.password
is not None):
6936 ftp_password
= urlparts
.password
;
6937 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6938 ftp_password
= "anonymous";
6941 if(urlparts
.scheme
=="ftp"):
6943 elif(urlparts
.scheme
=="ftps"):
6947 if(urlparts
.scheme
=="sftp"):
6949 return upload_file_from_pysftp_file(url
);
6951 return upload_file_from_sftp_file(url
);
6952 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6954 ftp_port
= urlparts
.port
;
6955 if(urlparts
.port
is None):
6958 ftp
.connect(urlparts
.hostname
, ftp_port
);
6959 except socket
.gaierror
:
6960 log
.info("Error With URL "+url
);
6962 except socket
.timeout
:
6963 log
.info("Error With URL "+url
);
6965 ftp
.login(urlparts
.username
, urlparts
.password
);
6966 if(urlparts
.scheme
=="ftps"):
6968 ftp
.storbinary("STOR "+urlparts
.path
, ftpfile
);
6973 def upload_file_to_ftp_string(ftpstring
, url
):
6974 ftpfileo
= BytesIO(ftpstring
);
6975 ftpfile
= upload_file_to_ftp_file(ftpfileo
, url
);
6979 def download_file_from_http_file(url
, headers
=geturls_headers_pycatfile_python_alt
):
6980 # Parse the URL to extract username and password if present
6981 urlparts
= urlparse(url
);
6982 username
= urlparts
.username
;
6983 password
= urlparts
.password
;
6984 # Rebuild the URL without the username and password
6985 netloc
= urlparts
.hostname
;
6986 if(urlparts
.scheme
=="sftp"):
6988 return download_file_from_pysftp_file(url
);
6990 return download_file_from_sftp_file(url
);
6991 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
6992 return download_file_from_ftp_file(url
);
6994 netloc
+= ':' + str(urlparts
.port
);
6995 rebuilt_url
= urlunparse((urlparts
.scheme
, netloc
, urlparts
.path
, urlparts
.params
, urlparts
.query
, urlparts
.fragment
));
6996 # Create a temporary file object
6997 httpfile
= BytesIO();
6999 # Use the requests library if available
7000 if username
and password
:
7001 response
= requests
.get(rebuilt_url
, headers
=headers
, auth
=(username
, password
), stream
=True);
7003 response
= requests
.get(rebuilt_url
, headers
=headers
, stream
=True);
7004 response
.raw
.decode_content
= True
7005 shutil
.copyfileobj(response
.raw
, httpfile
);
7007 # Build a Request object for urllib
7008 request
= Request(rebuilt_url
, headers
=headers
);
7009 # Create an opener object for handling URLs
7010 if username
and password
:
7011 # Create a password manager
7012 password_mgr
= HTTPPasswordMgrWithDefaultRealm();
7013 # Add the username and password
7014 password_mgr
.add_password(None, rebuilt_url
, username
, password
);
7015 # Create an authentication handler using the password manager
7016 auth_handler
= HTTPBasicAuthHandler(password_mgr
);
7017 # Build the opener with the authentication handler
7018 opener
= build_opener(auth_handler
);
7020 opener
= build_opener();
7021 with opener
.open(request
) as response
:
7022 shutil
.copyfileobj(response
, httpfile
);
7023 # Reset file pointer to the start
7024 httpfile
.seek(0, 0);
7025 # Return the temporary file object
7028 def download_file_from_http_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
7029 httpfile
= download_file_from_http_file(url
, headers
);
7030 return ftpfile
.read();
7033 def download_file_from_sftp_file(url
):
7034 urlparts
= urlparse(url
);
7035 file_name
= os
.path
.basename(urlparts
.path
);
7036 file_dir
= os
.path
.dirname(urlparts
.path
);
7037 sftp_port
= urlparts
.port
;
7038 if(urlparts
.port
is None):
7041 sftp_port
= urlparts
.port
;
7042 if(urlparts
.username
is not None):
7043 sftp_username
= urlparts
.username
;
7045 sftp_username
= "anonymous";
7046 if(urlparts
.password
is not None):
7047 sftp_password
= urlparts
.password
;
7048 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7049 sftp_password
= "anonymous";
7052 if(urlparts
.scheme
=="ftp"):
7053 return download_file_from_ftp_file(url
);
7054 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7055 return download_file_from_http_file(url
);
7056 if(urlparts
.scheme
!="sftp"):
7058 ssh
= paramiko
.SSHClient();
7059 ssh
.load_system_host_keys();
7060 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
7062 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7063 except paramiko
.ssh_exception
.SSHException
:
7065 except socket
.gaierror
:
7066 log
.info("Error With URL "+url
);
7068 except socket
.timeout
:
7069 log
.info("Error With URL "+url
);
7071 sftp
= ssh
.open_sftp();
7072 sftpfile
= BytesIO();
7073 sftp
.getfo(urlparts
.path
, sftpfile
);
7076 sftpfile
.seek(0, 0);
7079 def download_file_from_sftp_file(url
):
7083 def download_file_from_sftp_string(url
):
7084 sftpfile
= download_file_from_sftp_file(url
);
7085 return sftpfile
.read();
7087 def download_file_from_ftp_string(url
):
7091 def upload_file_to_sftp_file(sftpfile
, url
):
7092 urlparts
= urlparse(url
);
7093 file_name
= os
.path
.basename(urlparts
.path
);
7094 file_dir
= os
.path
.dirname(urlparts
.path
);
7095 sftp_port
= urlparts
.port
;
7096 if(urlparts
.port
is None):
7099 sftp_port
= urlparts
.port
;
7100 if(urlparts
.username
is not None):
7101 sftp_username
= urlparts
.username
;
7103 sftp_username
= "anonymous";
7104 if(urlparts
.password
is not None):
7105 sftp_password
= urlparts
.password
;
7106 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7107 sftp_password
= "anonymous";
7110 if(urlparts
.scheme
=="ftp"):
7111 return upload_file_from_ftp_file(url
);
7112 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7114 if(urlparts
.scheme
!="sftp"):
7116 ssh
= paramiko
.SSHClient();
7117 ssh
.load_system_host_keys();
7118 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
7120 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7121 except paramiko
.ssh_exception
.SSHException
:
7123 except socket
.gaierror
:
7124 log
.info("Error With URL "+url
);
7126 except socket
.timeout
:
7127 log
.info("Error With URL "+url
);
7129 sftp
= ssh
.open_sftp();
7130 sftp
.putfo(sftpfile
, urlparts
.path
);
7133 sftpfile
.seek(0, 0);
7136 def upload_file_to_sftp_file(sftpfile
, url
):
7140 def upload_file_to_sftp_string(sftpstring
, url
):
7141 sftpfileo
= BytesIO(sftpstring
);
7142 sftpfile
= upload_file_to_sftp_files(ftpfileo
, url
);
7146 def upload_file_to_sftp_string(url
):
7150 def download_file_from_pysftp_file(url
):
7151 urlparts
= urlparse(url
);
7152 file_name
= os
.path
.basename(urlparts
.path
);
7153 file_dir
= os
.path
.dirname(urlparts
.path
);
7154 sftp_port
= urlparts
.port
;
7155 if(urlparts
.port
is None):
7158 sftp_port
= urlparts
.port
;
7159 if(urlparts
.username
is not None):
7160 sftp_username
= urlparts
.username
;
7162 sftp_username
= "anonymous";
7163 if(urlparts
.password
is not None):
7164 sftp_password
= urlparts
.password
;
7165 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7166 sftp_password
= "anonymous";
7169 if(urlparts
.scheme
=="ftp"):
7170 return download_file_from_ftp_file(url
);
7171 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7172 return download_file_from_http_file(url
);
7173 if(urlparts
.scheme
!="sftp"):
7176 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7177 except paramiko
.ssh_exception
.SSHException
:
7179 except socket
.gaierror
:
7180 log
.info("Error With URL "+url
);
7182 except socket
.timeout
:
7183 log
.info("Error With URL "+url
);
7185 sftp
= ssh
.open_sftp();
7186 sftpfile
= BytesIO();
7187 sftp
.getfo(urlparts
.path
, sftpfile
);
7190 sftpfile
.seek(0, 0);
7193 def download_file_from_pysftp_file(url
):
7197 def download_file_from_pysftp_string(url
):
7198 sftpfile
= download_file_from_pysftp_file(url
);
7199 return sftpfile
.read();
7201 def download_file_from_ftp_string(url
):
7205 def upload_file_to_pysftp_file(sftpfile
, url
):
7206 urlparts
= urlparse(url
);
7207 file_name
= os
.path
.basename(urlparts
.path
);
7208 file_dir
= os
.path
.dirname(urlparts
.path
);
7209 sftp_port
= urlparts
.port
;
7210 if(urlparts
.port
is None):
7213 sftp_port
= urlparts
.port
;
7214 if(urlparts
.username
is not None):
7215 sftp_username
= urlparts
.username
;
7217 sftp_username
= "anonymous";
7218 if(urlparts
.password
is not None):
7219 sftp_password
= urlparts
.password
;
7220 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7221 sftp_password
= "anonymous";
7224 if(urlparts
.scheme
=="ftp"):
7225 return upload_file_from_ftp_file(url
);
7226 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7228 if(urlparts
.scheme
!="sftp"):
7231 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7232 except paramiko
.ssh_exception
.SSHException
:
7234 except socket
.gaierror
:
7235 log
.info("Error With URL "+url
);
7237 except socket
.timeout
:
7238 log
.info("Error With URL "+url
);
7240 sftp
= ssh
.open_sftp();
7241 sftp
.putfo(sftpfile
, urlparts
.path
);
7244 sftpfile
.seek(0, 0);
7247 def upload_file_to_pysftp_file(sftpfile
, url
):
7251 def upload_file_to_pysftp_string(sftpstring
, url
):
7252 sftpfileo
= BytesIO(sftpstring
);
7253 sftpfile
= upload_file_to_pysftp_files(ftpfileo
, url
);
7257 def upload_file_to_pysftp_string(url
):
7260 def download_file_from_internet_file(url
, headers
=geturls_headers_pycatfile_python_alt
):
7261 urlparts
= urlparse(url
);
7262 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7263 return download_file_from_http_file(url
, headers
);
7264 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7265 return download_file_from_ftp_file(url
);
7266 elif(urlparts
.scheme
=="sftp"):
7267 if(__use_pysftp__
and havepysftp
):
7268 return download_file_from_pysftp_file(url
);
7270 return download_file_from_sftp_file(url
);
7275 def download_file_from_internet_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
7276 urlparts
= urlparse(url
);
7277 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7278 return download_file_from_http_string(url
, headers
);
7279 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7280 return download_file_from_ftp_string(url
);
7281 elif(urlparts
.scheme
=="sftp"):
7282 if(__use_pysftp__
and havepysftp
):
7283 return download_file_from_pysftp_string(url
);
7285 return download_file_from_sftp_string(url
);
7290 def upload_file_to_internet_file(ifp
, url
):
7291 urlparts
= urlparse(url
);
7292 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7294 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7295 return upload_file_from_ftp_file(ifp
, url
);
7296 elif(urlparts
.scheme
=="sftp"):
7297 if(__use_pysftp__
and havepysftp
):
7298 return upload_file_from_pysftp_file(ifp
, url
);
7300 return download_file_from_sftp_file(ifp
, url
);
7305 def upload_file_to_internet_string(ifp
, url
):
7306 urlparts
= urlparse(url
);
7307 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7309 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7310 return upload_file_from_ftp_string(ifp
, url
);
7311 elif(urlparts
.scheme
=="sftp"):
7312 if(__use_pysftp__
and havepysftp
):
7313 return upload_file_from_pysftp_string(ifp
, url
);
7315 return download_file_from_sftp_string(ifp
, url
);
7321 if(hasattr(shutil
, "register_archive_format")):
7322 # Register the packing format
7323 shutil
.register_archive_format(__file_format_name__
, PackArchiveFileFunc
, description
='Pack concatenated files');
7324 except shutil
.RegistryError
:
7328 if(hasattr(shutil
, "register_unpack_format")):
7329 # Register the unpacking format
7330 shutil
.register_unpack_format(__file_format_name__
, archivefile_extensions
, UnPackArchiveFileFunc
, description
='UnPack concatenated files');
7331 except shutil
.RegistryError
: