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/27/2024 Ver. 0.6.8 RC 1 - Author: cooldude2k $
20 from __future__
import absolute_import
, division
, print_function
, unicode_literals
;
21 import io
, os
, re
, sys
, time
, stat
, zlib
, base64
, shutil
, socket
, hashlib
, datetime
, logging
, binascii
, tempfile
, zipfile
, platform
;
22 from ftplib
import FTP
, FTP_TLS
;
23 if(sys
.version
[0]=="2"):
24 from urlparse
import urlparse
, urlunparse
;
25 elif(sys
.version
[0]>="3"):
26 from urllib
.parse
import urlunparse
;
27 from urllib
.parse
import urlparse
;
29 if os
.name
== 'nt': # Only modify if on Windows
30 if sys
.version
[0] == "2":
32 sys
.stdout
= codecs
.getwriter('utf-8')(sys
.stdout
);
33 sys
.stderr
= codecs
.getwriter('utf-8')(sys
.stderr
);
36 sys
.stdout
= io
.TextIOWrapper(sys
.stdout
.buffer, encoding
='utf-8', errors
='replace', line_buffering
=True);
37 sys
.stderr
= io
.TextIOWrapper(sys
.stderr
.buffer, encoding
='utf-8', errors
='replace', line_buffering
=True);
39 hashlib_guaranteed
= False;
40 os
.environ
["PYTHONIOENCODING"] = "UTF-8";
41 os
.environ
["LC_CTYPE"] = "UTF-8";
45 sys
.setdefaultencoding('UTF-8');
48 except AttributeError:
52 except AttributeError:
56 from zlib
import crc32
;
58 from binascii
import crc32
;
60 rarfile_support
= False;
63 rarfile_support
= True;
65 rarfile_support
= False;
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, 6, 8, "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
) + 3;
1035 catoutlenhex
= format(catoutlen
, 'x').lower();
1036 catoutlist
= filevalues
;
1037 catoutlist
.insert(0, catoutlenhex
);
1038 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
1039 if(len(extradata
)>0):
1040 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
1041 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
1042 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
1043 catfilecontentcshex
= GetFileChecksum(filecontent
, checksumtype
, False, formatspecs
);
1044 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1045 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
1046 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
1047 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1048 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
1049 nullstrecd
= formatspecs
[5].encode('UTF-8');
1050 catfileout
= catfileoutstrecd
+ filecontent
+ nullstrecd
;
1051 fp
.write(catfileout
);
1054 os
.fsync(fp
.fileno());
1055 except io
.UnsupportedOperation
:
1057 except AttributeError:
1061 def AppendFiles(infiles
, fp
, dirlistfromtxt
=False, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
):
1062 advancedlist
= True;
1064 for line
in sys
.stdin
:
1065 infilelist
.append(line
.strip());
1066 infilelist
= list(filter(None, infilelist
));
1067 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
1068 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
1070 with
open(infiles
, "r") as finfile
:
1071 for line
in finfile
:
1072 infilelist
.append(line
.strip());
1073 infilelist
= list(filter(None, infilelist
));
1075 if(isinstance(infiles
, (list, tuple, ))):
1076 infilelist
= list(filter(None, infiles
));
1077 elif(isinstance(infiles
, (str, ))):
1078 infilelist
= list(filter(None, [infiles
]));
1080 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
1082 GetDirList
= ListDir(infilelist
, followlink
, False);
1090 inodetocatinode
= {};
1091 fnumfiles
= int(len(GetDirList
));
1092 AppendFileHeader(fp
, fnumfiles
, checksumtype
, formatspecs
);
1093 fnumfiles
= format(fnumfiles
, 'x').lower();
1094 for curfname
in GetDirList
:
1095 catfhstart
= fp
.tell();
1096 if(re
.findall("^[.|/]", curfname
)):
1099 fname
= "./"+curfname
;
1100 if(not followlink
or followlink
is None):
1101 fstatinfo
= os
.lstat(fname
);
1103 fstatinfo
= os
.stat(fname
);
1104 fpremode
= fstatinfo
.st_mode
;
1105 finode
= fstatinfo
.st_ino
;
1106 flinkcount
= fstatinfo
.st_nlink
;
1108 if(stat
.S_ISREG(fpremode
)):
1110 elif(stat
.S_ISLNK(fpremode
)):
1112 elif(stat
.S_ISCHR(fpremode
)):
1114 elif(stat
.S_ISBLK(fpremode
)):
1116 elif(stat
.S_ISDIR(fpremode
)):
1118 elif(stat
.S_ISFIFO(fpremode
)):
1120 elif(stat
.S_ISSOCK(fpremode
)):
1122 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
1124 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
1126 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
1131 fcurfid
= format(int(curfid
), 'x').lower();
1132 if(not followlink
and finode
!=0):
1134 if(finode
in inodelist
):
1136 flinkname
= inodetofile
[finode
];
1137 fcurinode
= format(int(inodetocatinode
[finode
]), 'x').lower();
1138 if(finode
not in inodelist
):
1139 inodelist
.append(finode
);
1140 inodetofile
.update({finode
: fname
});
1141 inodetocatinode
.update({finode
: curinode
});
1142 fcurinode
= format(int(curinode
), 'x').lower();
1143 curinode
= curinode
+ 1;
1145 fcurinode
= format(int(curinode
), 'x').lower();
1146 curinode
= curinode
+ 1;
1147 curfid
= curfid
+ 1;
1149 flinkname
= os
.readlink(fname
);
1150 fdev
= fstatinfo
.st_dev
;
1151 getfdev
= GetDevMajorMinor(fdev
);
1152 fdev_minor
= getfdev
[0];
1153 fdev_major
= getfdev
[1];
1154 frdev
= fstatinfo
.st_dev
;
1155 if(hasattr(fstatinfo
, "st_rdev")):
1156 frdev
= fstatinfo
.st_rdev
;
1158 frdev
= fstatinfo
.st_dev
;
1159 getfrdev
= GetDevMajorMinor(frdev
);
1160 frdev_minor
= getfrdev
[0];
1161 frdev_major
= getfrdev
[1];
1162 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
1163 fsize
= format(int("0"), 'x').lower();
1164 elif(ftype
==0 or ftype
==7):
1165 fsize
= format(int(fstatinfo
.st_size
), 'x').lower();
1167 fsize
= format(int(fstatinfo
.st_size
)).lower();
1168 fatime
= format(int(fstatinfo
.st_atime
), 'x').lower();
1169 fmtime
= format(int(fstatinfo
.st_mtime
), 'x').lower();
1170 fctime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1171 if(hasattr(fstatinfo
, "st_birthtime")):
1172 fbtime
= format(int(fstatinfo
.st_birthtime
), 'x').lower();
1174 fbtime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1175 fmode
= format(int(fstatinfo
.st_mode
), 'x').lower();
1176 fchmode
= format(int(stat
.S_IMODE(fstatinfo
.st_mode
)), 'x').lower();
1177 ftypemod
= format(int(stat
.S_IFMT(fstatinfo
.st_mode
)), 'x').lower();
1178 fuid
= format(int(fstatinfo
.st_uid
), 'x').lower();
1179 fgid
= format(int(fstatinfo
.st_gid
), 'x').lower();
1184 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
1185 funame
= userinfo
.pw_name
;
1194 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
1195 fgname
= groupinfo
.gr_name
;
1200 fdev_minor
= format(int(fdev_minor
), 'x').lower();
1201 fdev_major
= format(int(fdev_major
), 'x').lower();
1202 frdev_minor
= format(int(frdev_minor
), 'x').lower();
1203 frdev_major
= format(int(frdev_major
), 'x').lower();
1204 finode
= format(int(finode
), 'x').lower();
1205 flinkcount
= format(int(flinkcount
), 'x').lower();
1206 if(hasattr(fstatinfo
, "st_file_attributes")):
1207 fwinattributes
= format(int(fstatinfo
.st_file_attributes
), 'x').lower();
1209 fwinattributes
= format(int(0), 'x').lower();
1210 fcontents
= "".encode('UTF-8');
1212 if(ftype
== 0 or ftype
== 7):
1213 with
open(fname
, "rb") as fpc
:
1215 chunk
= fpc
.read(chunk_size
);
1219 if(followlink
and (ftype
== 1 or ftype
== 2)):
1220 flstatinfo
= os
.stat(flinkname
);
1221 with
open(flinkname
, "rb") as fpc
:
1223 chunk
= fpc
.read(chunk_size
);
1227 ftypehex
= format(ftype
, 'x').lower();
1228 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
];
1229 fp
= AppendFileHeaderWithContent(fp
, catoutlist
, extradata
, fcontents
, checksumtype
, formatspecs
);
1233 def AppendFilesToOutFile(infile
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, returnfp
=False):
1234 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1235 if(os
.path
.exists(outfile
)):
1239 catfpfp
= BytesIO();
1240 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1242 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1245 fbasename
= os
.path
.splitext(outfile
)[0];
1246 fextname
= os
.path
.splitext(outfile
)[1];
1247 catfp
= CompressOpenFile(outfile
, compressionlevel
);
1248 catfp
= AppendFiles(infile
, catfp
, dirlistfromtxt
, filevalues
, extradata
, followlink
, checksumtype
, formatspecs
);
1249 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1250 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1253 os
.fsync(catfp
.fileno());
1254 except io
.UnsupportedOperation
:
1256 except AttributeError:
1260 if(hasattr(sys
.stdout
, "buffer")):
1261 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1263 shutil
.copyfileobj(catfp
, sys
.stdout
);
1264 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1265 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1267 upload_file_to_internet_file(catfp
, outfile
);
1275 def PrintPermissionString(fchmode
, ftype
):
1276 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' } };
1278 for fmodval
in str(oct(fchmode
))[-3:]:
1279 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
1280 if(ftype
==0 or ftype
==7):
1281 permissionstr
= "-" + permissionstr
;
1283 permissionstr
= "h" + permissionstr
;
1285 permissionstr
= "l" + permissionstr
;
1287 permissionstr
= "c" + permissionstr
;
1289 permissionstr
= "b" + permissionstr
;
1291 permissionstr
= "d" + permissionstr
;
1293 permissionstr
= "f" + permissionstr
;
1295 permissionstr
= "D" + permissionstr
;
1297 permissionstr
= "p" + permissionstr
;
1299 permissionstr
= "w" + permissionstr
;
1301 permissionoutstr
= stat
.filemode(fchmode
);
1302 except AttributeError:
1303 permissionoutstr
= permissionstr
;
1305 permissionoutstr
= permissionstr
;
1306 return permissionoutstr
;
1308 def PrintPermissionStringAlt(fchmode
, ftype
):
1310 '0': '---', '1': '--x', '2': '-w-', '3': '-wx',
1311 '4': 'r--', '5': 'r-x', '6': 'rw-', '7': 'rwx'
1313 # Translate file mode into permission string
1314 permissionstr
= ''.join([permissions
[i
] for i
in str(oct(fchmode
))[-3:]]);
1315 # Append file type indicator
1317 0: '-', 1: 'h', 2: 'l', 3: 'c', 4: 'b',
1318 5: 'd', 6: 'f', 8: 'D', 9: 'p', 10: 'w'
1320 file_type
= type_indicators
.get(ftype
, '-');
1321 permissionstr
= file_type
+ permissionstr
;
1323 permissionoutstr
= stat
.filemode(fchmode
);
1324 except AttributeError:
1325 permissionoutstr
= permissionstr
;
1326 return permissionoutstr
;
1328 def CompressionSupport():
1329 compression_list
= [];
1332 compression_list
.append("gz");
1333 compression_list
.append("gzip");
1338 compression_list
.append("bz2");
1339 compression_list
.append("bzip2");
1344 compression_list
.append("lz4");
1349 compression_list
.append("lzo");
1350 compression_list
.append("lzop");
1355 compression_list
.append("zstd");
1356 compression_list
.append("zstandard");
1361 compression_list
.append("lzma");
1362 compression_list
.append("xz");
1365 return compression_list
;
1367 def CheckCompressionType(infile
, formatspecs
=__file_format_list__
, closefp
=True):
1368 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1372 catfp
= open(infile
, "rb");
1373 except FileNotFoundError
:
1376 prefp
= catfp
.read(2);
1378 if(prefp
==binascii
.unhexlify("1f8b")):
1381 prefp
= catfp
.read(3);
1382 if(prefp
==binascii
.unhexlify("425a68")):
1384 if(prefp
==binascii
.unhexlify("5d0000")):
1387 prefp
= catfp
.read(4);
1388 if(prefp
==binascii
.unhexlify("28b52ffd")):
1390 if(prefp
==binascii
.unhexlify("04224d18")):
1392 if(prefp
==binascii
.unhexlify("504B0304")):
1393 filetype
= "zipfile";
1395 prefp
= catfp
.read(5);
1396 if(prefp
==binascii
.unhexlify("7573746172")):
1397 filetype
= "tarfile";
1399 prefp
= catfp
.read(6);
1400 if(prefp
==binascii
.unhexlify("fd377a585a00")):
1403 prefp
= catfp
.read(7);
1404 if(prefp
==binascii
.unhexlify("526172211a0700")):
1405 filetype
= "rarfile";
1406 if(prefp
==binascii
.unhexlify("43617446696c65")):
1407 filetype
= "catfile";
1409 prefp
= catfp
.read(8);
1410 if(prefp
==binascii
.unhexlify("526172211a070100")):
1411 filetype
= "rarfile";
1413 prefp
= catfp
.read(formatspecs
[3]);
1414 if(prefp
==binascii
.unhexlify(formatspecs
[4])):
1415 filetype
= formatspecs
[2];
1417 prefp
= catfp
.read(9);
1418 if(prefp
==binascii
.unhexlify("894c5a4f000d0a1a0a")):
1421 prefp
= catfp
.read(10);
1422 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
1423 filetype
= "tarfile";
1429 def CheckCompressionTypeFromString(instring
, formatspecs
=__file_format_list__
, closefp
=True):
1431 instringsfile
= BytesIO(instring
);
1433 instringsfile
= BytesIO(instring
.encode("UTF-8"));
1434 return CheckCompressionType(instringsfile
, formatspecs
, closefp
);
1436 def GetCompressionMimeType(infile
, formatspecs
=__file_format_list__
):
1437 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
1438 if(compresscheck
=="gzip" or compresscheck
=="gz"):
1439 return archivefile_gzip_mimetype
;
1440 if(compresscheck
=="bzip2" or compresscheck
=="bz2"):
1441 return archivefile_bzip2_mimetype
;
1442 if(compresscheck
=="zstd" or compresscheck
=="zstandard"):
1443 return archivefile_zstandard_mimetype
;
1444 if(compresscheck
=="lz4"):
1445 return archivefile_lz4_mimetype
;
1446 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
1447 return archivefile_lzop_mimetype
;
1448 if(compresscheck
=="lzma"):
1449 return archivefile_lzma_mimetype
;
1450 if(compresscheck
=="xz"):
1451 return archivefile_xz_mimetype
;
1452 if(compresscheck
=="catfile" or compresscheck
=="cat" or compresscheck
==formatspecs
[2]):
1453 return archivefile_cat_mimetype
;
1454 if(not compresscheck
):
1458 def UncompressArchiveFile(fp
, formatspecs
=__file_format_list__
):
1459 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
1461 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
1462 if(compresscheck
=="gzip"):
1467 catfp
= gzip
.GzipFile(fileobj
=fp
, mode
="rb");
1468 if(compresscheck
=="bzip2"):
1474 catfp
.write(bz2
.decompress(fp
.read()));
1475 if(compresscheck
=="zstd"):
1481 catfp
.write(zstandard
.decompress(fp
.read()));
1482 if(compresscheck
=="lz4"):
1488 catfp
.write(lz4
.frame
.decompress(fp
.read()));
1489 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
1495 catfp
.write(lzo
.decompress(fp
.read()));
1496 if(compresscheck
=="lzma" or compresscheck
=="xz"):
1502 catfp
.write(lzma
.decompress(fp
.read()));
1503 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[2]):
1505 if(not compresscheck
):
1511 with fp
as fpcontent
:
1513 catfp
.write(lzma
.decompress(fp
.read()));
1514 except lzma
.LZMAError
:
1518 create_alias_function("Uncompress", __file_format_name__
, "", UncompressArchiveFile
);
1520 def UncompressFile(infile
, formatspecs
=__file_format_list__
, mode
="rb"):
1521 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
1522 if(sys
.version_info
[0]==2 and compresscheck
):
1528 if(compresscheck
=="gzip"):
1534 filefp
= gzip
.open(infile
, mode
, encoding
="UTF-8");
1535 except (ValueError, TypeError) as e
:
1536 filefp
= gzip
.open(infile
, mode
);
1537 if(compresscheck
=="bzip2"):
1543 filefp
= bz2
.open(infile
, mode
, encoding
="UTF-8");
1544 except (ValueError, TypeError) as e
:
1545 filefp
= bz2
.open(infile
, mode
);
1546 if(compresscheck
=="zstd"):
1552 filefp
= zstandard
.open(infile
, mode
, encoding
="UTF-8");
1553 except (ValueError, TypeError) as e
:
1554 filefp
= zstandard
.open(infile
, mode
);
1555 if(compresscheck
=="lz4"):
1561 filefp
= lz4
.frame
.open(infile
, mode
, encoding
="UTF-8");
1562 except (ValueError, TypeError) as e
:
1563 filefp
= lz4
.frame
.open(infile
, mode
);
1564 if(compresscheck
=="lzo"):
1570 filefp
= lzo
.open(infile
, mode
, encoding
="UTF-8");
1571 except (ValueError, TypeError) as e
:
1572 filefp
= lzo
.open(infile
, mode
);
1573 if(compresscheck
=="lzma"):
1579 filefp
= lzma
.open(infile
, mode
, encoding
="UTF-8");
1580 except (ValueError, TypeError) as e
:
1581 filefp
= lzma
.open(infile
, mode
);
1582 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[2]):
1584 filefp
= open(infile
, mode
, encoding
="UTF-8");
1585 except (ValueError, TypeError) as e
:
1586 filefp
= open(infile
, mode
);
1587 if(not compresscheck
):
1589 filefp
= open(infile
, mode
, encoding
="UTF-8");
1590 except (ValueError, TypeError) as e
:
1591 filefp
= open(infile
, mode
);
1592 except FileNotFoundError
:
1596 def UncompressString(infile
):
1597 compresscheck
= CheckCompressionTypeFromString(infile
, formatspecs
, False);
1598 if(compresscheck
=="gzip"):
1603 fileuz
= gzip
.decompress(infile
);
1604 if(compresscheck
=="bzip2"):
1609 fileuz
= bz2
.decompress(infile
);
1610 if(compresscheck
=="zstd"):
1615 fileuz
= zstandard
.decompress(infile
);
1616 if(compresscheck
=="lz4"):
1621 fileuz
= lz4
.frame
.decompress(infile
);
1622 if(compresscheck
=="lzo"):
1627 fileuz
= lzo
.decompress(infile
);
1628 if(compresscheck
=="lzma"):
1633 fileuz
= lzma
.decompress(infile
);
1634 if(not compresscheck
):
1636 if(hasattr(fileuz
, 'decode')):
1637 fileuz
= fileuz
.decode("UTF-8");
1640 def UncompressStringAlt(infile
):
1641 filefp
= StringIO();
1642 outstring
= UncompressString(infile
);
1643 filefp
.write(outstring
);
1647 def CheckCompressionSubType(infile
, formatspecs
=__file_format_list__
):
1648 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
1649 if(not compresscheck
):
1650 fextname
= os
.path
.splitext(infile
)[1];
1651 if(fextname
==".gz"):
1652 compresscheck
= "gzip";
1653 elif(fextname
==".bz2"):
1654 compresscheck
= "bzip2";
1655 elif(fextname
==".zst"):
1656 compresscheck
= "zstd";
1657 elif(fextname
==".lz4"):
1658 compresscheck
= "lz4";
1659 elif(fextname
==".lzo" or fextname
==".lzop"):
1660 compresscheck
= "lzo";
1661 elif(fextname
==".lzma" or fextname
==".xz"):
1662 compresscheck
= "lzma";
1665 if(not compresscheck
):
1667 if(compresscheck
=="catfile"):
1669 if(compresscheck
==formatspecs
[2]):
1670 return formatspecs
[2];
1671 if(compresscheck
=="tarfile"):
1673 if(compresscheck
=="zipfile"):
1675 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1676 catfp
= UncompressArchiveFile(infile
, formatspecs
[2]);
1679 if(compresscheck
=="gzip"):
1684 catfp
= gzip
.GzipFile(infile
, "rb");
1685 if(compresscheck
=="bzip2"):
1690 catfp
= bz2
.BZ2File(infile
, "rb");
1691 if(compresscheck
=="lz4"):
1696 catfp
= lz4
.frame
.open(infile
, "rb");
1697 if(compresscheck
=="zstd"):
1702 catfp
= zstandard
.open(infile
, "rb");
1703 if(compresscheck
=="lzma" or compresscheck
=="xz"):
1708 catfp
= lzma
.open(infile
, "rb");
1709 except FileNotFoundError
:
1712 prefp
= catfp
.read(5);
1713 if(prefp
==binascii
.unhexlify("7573746172")):
1714 filetype
= "tarfile";
1716 prefp
= catfp
.read(7);
1717 if(prefp
==binascii
.unhexlify("43617446696c65")):
1718 filetype
= "catfile";
1720 prefp
= catfp
.read(formatspecs
[3]);
1721 if(prefp
==binascii
.unhexlify(formatspecs
[4])):
1722 filetype
= formatspecs
[2];
1724 prefp
= catfp
.read(10);
1725 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
1726 filetype
= "tarfile";
1731 def GZipCompress(data
, compresslevel
=9):
1736 tmpfp
= tempfile
.NamedTemporaryFile("wb", delete
=False);
1738 tmpfp
= gzip
.GzipFile(tmpfp
.name
, mode
="wb", compresslevel
=compresslevel
);
1742 catfp
= open(tmpfp
.name
, "rb");
1743 except FileNotFoundError
:
1745 catdata
= catfp
.read();
1749 def CompressArchiveFile(fp
, compression
="auto", compressionlevel
=None, formatspecs
=__file_format_list__
):
1750 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1751 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
1754 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
1756 if(compression
not in compressionlist
and compression
is None):
1757 compression
= "auto";
1758 if(compression
=="gzip"):
1764 if(compressionlevel
is None):
1765 compressionlevel
= 9;
1767 compressionlevel
= int(compressionlevel
);
1768 catfp
.write(gzip
.compress(fp
.read(), compresslevel
=compressionlevel
));
1769 if(compression
=="bzip2"):
1775 if(compressionlevel
is None):
1776 compressionlevel
= 9;
1778 compressionlevel
= int(compressionlevel
);
1779 catfp
.write(bz2
.compress(fp
.read(), compresslevel
=compressionlevel
));
1780 if(compression
=="lz4"):
1786 if(compressionlevel
is None):
1787 compressionlevel
= 9;
1789 compressionlevel
= int(compressionlevel
);
1790 catfp
.write(lz4
.frame
.compress(fp
.read(), compression_level
=compressionlevel
));
1791 if(compression
=="lzo" or compression
=="lzop"):
1797 if(compressionlevel
is None):
1798 compressionlevel
= 9;
1800 compressionlevel
= int(compressionlevel
);
1801 catfp
.write(lzo
.compress(fp
.read(), compresslevel
=compressionlevel
));
1802 if(compression
=="zstd"):
1808 if(compressionlevel
is None):
1809 compressionlevel
= 10;
1811 compressionlevel
= int(compressionlevel
);
1812 catfp
.write(zstandard
.compress(fp
.read(), level
=compressionlevel
));
1813 if(compression
=="lzma"):
1819 if(compressionlevel
is None):
1820 compressionlevel
= 9;
1822 compressionlevel
= int(compressionlevel
);
1823 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]));
1824 if(compression
=="xz"):
1830 if(compressionlevel
is None):
1831 compressionlevel
= 9;
1833 compressionlevel
= int(compressionlevel
);
1834 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]));
1835 if(compression
=="auto" or compression
is None):
1840 create_alias_function("Compress", __file_format_name__
, "", CompressArchiveFile
);
1842 def CompressOpenFile(outfile
, compressionlevel
=None):
1843 if(outfile
is None):
1845 fbasename
= os
.path
.splitext(outfile
)[0];
1846 fextname
= os
.path
.splitext(outfile
)[1];
1847 if(compressionlevel
is None and fextname
!=".zst"):
1848 compressionlevel
= 9;
1849 elif(compressionlevel
is None and fextname
==".zst"):
1850 compressionlevel
= 10;
1852 compressionlevel
= int(compressionlevel
);
1853 if(sys
.version_info
[0]==2):
1858 if(fextname
not in outextlistwd
):
1860 outfp
= open(outfile
, "wb", encoding
="UTF-8");
1861 except (ValueError, TypeError) as e
:
1862 outfp
= open(outfile
, "wb");
1863 elif(fextname
==".gz"):
1869 outfp
= gzip
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
1870 except (ValueError, TypeError) as e
:
1871 outfp
= gzip
.open(outfile
, mode
, compressionlevel
);
1872 elif(fextname
==".bz2"):
1878 outfp
= bz2
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
1879 except (ValueError, TypeError) as e
:
1880 outfp
= bz2
.open(outfile
, mode
, compressionlevel
);
1881 elif(fextname
==".zst"):
1887 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
), encoding
="UTF-8");
1888 except (ValueError, TypeError) as e
:
1889 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
));
1890 elif(fextname
==".xz"):
1896 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}], encoding
="UTF-8");
1897 except (ValueError, TypeError) as e
:
1898 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]);
1899 elif(fextname
==".lz4"):
1905 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
, encoding
="UTF-8");
1906 except (ValueError, TypeError) as e
:
1907 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
);
1908 elif(fextname
==".lzo"):
1914 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
, encoding
="UTF-8");
1915 except (ValueError, TypeError) as e
:
1916 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
);
1917 elif(fextname
==".lzma"):
1923 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}], encoding
="UTF-8");
1924 except (ValueError, TypeError) as e
:
1925 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]);
1926 except FileNotFoundError
:
1930 def GetDevMajorMinor(fdev
):
1932 if(hasattr(os
, "minor")):
1933 retdev
.append(os
.minor(fdev
));
1936 if(hasattr(os
, "major")):
1937 retdev
.append(os
.major(fdev
));
1942 def CheckSumSupport(checkfor
, guaranteed
=True):
1944 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
1946 hash_list
= sorted(list(hashlib
.algorithms_available
));
1947 checklistout
= sorted(hash_list
+ ['adler32', 'crc16', 'crc16_ansi', 'crc16_ibm', 'crc16_ccitt', 'crc32', 'crc64', 'crc64_ecma', 'crc64_iso', 'none']);
1948 if(checkfor
in checklistout
):
1953 def CheckSumSupportAlt(checkfor
, guaranteed
=True):
1955 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
1957 hash_list
= sorted(list(hashlib
.algorithms_available
));
1958 checklistout
= hash_list
;
1959 if(checkfor
in checklistout
):
1964 def PackArchiveFile(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1965 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1966 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1967 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1968 advancedlist
= True;
1969 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1970 outfile
= RemoveWindowsPath(outfile
);
1971 checksumtype
= checksumtype
.lower();
1972 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
1973 checksumtype
="crc32";
1974 if(checksumtype
=="none"):
1976 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
1978 if(compression
not in compressionlist
and compression
is None):
1979 compression
= "auto";
1981 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1982 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1983 if(os
.path
.exists(outfile
)):
1988 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1990 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1993 fbasename
= os
.path
.splitext(outfile
)[0];
1994 fextname
= os
.path
.splitext(outfile
)[1];
1995 catfp
= CompressOpenFile(outfile
, compressionlevel
);
1996 catver
= formatspecs
[6];
1997 fileheaderver
= str(int(catver
.replace(".", "")));
1998 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
1999 catfp
.write(fileheader
.encode('UTF-8'));
2002 for line
in sys
.stdin
:
2003 infilelist
.append(line
.strip());
2004 infilelist
= list(filter(None, infilelist
));
2005 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
2006 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
2008 with
open(infiles
, "r") as finfile
:
2009 for line
in finfile
:
2010 infilelist
.append(line
.strip());
2011 infilelist
= list(filter(None, infilelist
));
2013 if(isinstance(infiles
, (list, tuple, ))):
2014 infilelist
= list(filter(None, infiles
));
2015 elif(isinstance(infiles
, (str, ))):
2016 infilelist
= list(filter(None, [infiles
]));
2018 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
2020 GetDirList
= ListDir(infilelist
, followlink
, False);
2028 inodetocatinode
= {};
2029 fnumfiles
= format(int(len(GetDirList
)), 'x').lower();
2030 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2031 if(checksumtype
=="none" or checksumtype
==""):
2032 catfileheadercshex
= format(0, 'x').lower();
2033 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2034 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2035 elif(checksumtype
=="crc16_ccitt"):
2036 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2037 elif(checksumtype
=="adler32"):
2038 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2039 elif(checksumtype
=="crc32"):
2040 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2041 elif(checksumtype
=="crc64_ecma"):
2042 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2043 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2044 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2045 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2046 checksumoutstr
= hashlib
.new(checksumtype
);
2047 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
2048 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2050 catfileheadercshex
= format(0, 'x').lower
2051 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2052 catfp
.write(fnumfilesa
.encode('UTF-8'));
2055 os
.fsync(catfp
.fileno());
2056 except io
.UnsupportedOperation
:
2058 except AttributeError:
2060 for curfname
in GetDirList
:
2061 catfhstart
= catfp
.tell();
2062 if(re
.findall("^[.|/]", curfname
)):
2065 fname
= "./"+curfname
;
2067 VerbosePrintOut(fname
);
2068 if(not followlink
or followlink
is None):
2069 fstatinfo
= os
.lstat(fname
);
2071 fstatinfo
= os
.stat(fname
);
2072 fpremode
= fstatinfo
.st_mode
;
2073 finode
= fstatinfo
.st_ino
;
2074 flinkcount
= fstatinfo
.st_nlink
;
2076 if(stat
.S_ISREG(fpremode
)):
2078 elif(stat
.S_ISLNK(fpremode
)):
2080 elif(stat
.S_ISCHR(fpremode
)):
2082 elif(stat
.S_ISBLK(fpremode
)):
2084 elif(stat
.S_ISDIR(fpremode
)):
2086 elif(stat
.S_ISFIFO(fpremode
)):
2088 elif(stat
.S_ISSOCK(fpremode
)):
2090 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
2092 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
2094 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
2099 fcurfid
= format(int(curfid
), 'x').lower();
2100 if(not followlink
and finode
!=0):
2102 if(finode
in inodelist
):
2104 flinkname
= inodetofile
[finode
];
2105 fcurinode
= format(int(inodetocatinode
[finode
]), 'x').lower();
2106 if(finode
not in inodelist
):
2107 inodelist
.append(finode
);
2108 inodetofile
.update({finode
: fname
});
2109 inodetocatinode
.update({finode
: curinode
});
2110 fcurinode
= format(int(curinode
), 'x').lower();
2111 curinode
= curinode
+ 1;
2113 fcurinode
= format(int(curinode
), 'x').lower();
2114 curinode
= curinode
+ 1;
2115 curfid
= curfid
+ 1;
2117 flinkname
= os
.readlink(fname
);
2118 fdev
= fstatinfo
.st_dev
;
2119 getfdev
= GetDevMajorMinor(fdev
);
2120 fdev_minor
= getfdev
[0];
2121 fdev_major
= getfdev
[1];
2122 frdev
= fstatinfo
.st_dev
;
2123 if(hasattr(fstatinfo
, "st_rdev")):
2124 frdev
= fstatinfo
.st_rdev
;
2126 frdev
= fstatinfo
.st_dev
;
2127 getfrdev
= GetDevMajorMinor(frdev
);
2128 frdev_minor
= getfrdev
[0];
2129 frdev_major
= getfrdev
[1];
2130 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
2131 fsize
= format(int("0"), 'x').lower();
2132 elif(ftype
==0 or ftype
==7):
2133 fsize
= format(int(fstatinfo
.st_size
), 'x').lower();
2135 fsize
= format(int(fstatinfo
.st_size
)).lower();
2136 fatime
= format(int(fstatinfo
.st_atime
), 'x').lower();
2137 fmtime
= format(int(fstatinfo
.st_mtime
), 'x').lower();
2138 fctime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
2139 if(hasattr(fstatinfo
, "st_birthtime")):
2140 fbtime
= format(int(fstatinfo
.st_birthtime
), 'x').lower();
2142 fbtime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
2143 fmode
= format(int(fstatinfo
.st_mode
), 'x').lower();
2144 fchmode
= format(int(stat
.S_IMODE(fstatinfo
.st_mode
)), 'x').lower();
2145 ftypemod
= format(int(stat
.S_IFMT(fstatinfo
.st_mode
)), 'x').lower();
2146 fuid
= format(int(fstatinfo
.st_uid
), 'x').lower();
2147 fgid
= format(int(fstatinfo
.st_gid
), 'x').lower();
2152 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
2153 funame
= userinfo
.pw_name
;
2162 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
2163 fgname
= groupinfo
.gr_name
;
2168 fdev_minor
= format(int(fdev_minor
), 'x').lower();
2169 fdev_major
= format(int(fdev_major
), 'x').lower();
2170 frdev_minor
= format(int(frdev_minor
), 'x').lower();
2171 frdev_major
= format(int(frdev_major
), 'x').lower();
2172 finode
= format(int(finode
), 'x').lower();
2173 flinkcount
= format(int(flinkcount
), 'x').lower();
2174 if(hasattr(fstatinfo
, "st_file_attributes")):
2175 fwinattributes
= format(int(fstatinfo
.st_file_attributes
), 'x').lower();
2177 fwinattributes
= format(int(0), 'x').lower();
2178 fcontents
= "".encode('UTF-8');
2180 if(ftype
== 0 or ftype
== 7):
2181 with
open(fname
, "rb") as fpc
:
2183 chunk
= fpc
.read(chunk_size
);
2187 if(followlink
and (ftype
== 1 or ftype
== 2)):
2188 flstatinfo
= os
.stat(flinkname
);
2189 with
open(flinkname
, "rb") as fpc
:
2191 chunk
= fpc
.read(chunk_size
);
2195 ftypehex
= format(ftype
, 'x').lower();
2196 extrafields
= format(len(extradata
), 'x').lower();
2197 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
2198 if(len(extradata
)>0):
2199 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2200 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2201 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
];
2202 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
2203 catoutlenhex
= format(catoutlen
, 'x').lower();
2204 catoutlist
.insert(0, catoutlenhex
);
2205 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
2206 if(len(extradata
)>0):
2207 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2208 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
2209 if(checksumtype
=="none" or checksumtype
==""):
2210 catfileheadercshex
= format(0, 'x').lower();
2211 catfilecontentcshex
= format(0, 'x').lower();
2212 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2213 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2214 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
2215 elif(checksumtype
=="crc16_ccitt"):
2216 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2217 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
2218 elif(checksumtype
=="adler32"):
2219 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2220 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
2221 elif(checksumtype
=="crc32"):
2222 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2223 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
2224 elif(checksumtype
=="crc64_ecma"):
2225 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2226 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
2227 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2228 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2229 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
2230 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2231 checksumoutstr
= hashlib
.new(checksumtype
);
2232 checksumoutstr
.update("".encode('UTF-8'));
2233 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2234 checksumoutstr
= hashlib
.new(checksumtype
);
2235 checksumoutstr
.update(fcontents
);
2236 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
2238 catfileheadercshex
= format(0, 'x').lower();
2239 catfilecontentcshex
= format(0, 'x').lower();
2240 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2241 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2242 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
2243 if(checksumtype
=="none" or checksumtype
==""):
2244 catfileheadercshex
= format(0, 'x').lower();
2245 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2246 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2247 elif(checksumtype
=="crc16_ccitt"):
2248 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2249 elif(checksumtype
=="adler32"):
2250 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2251 elif(checksumtype
=="crc32"):
2252 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2253 elif(checksumtype
=="crc64_ecma"):
2254 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2255 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2256 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2257 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2258 checksumoutstr
= hashlib
.new(checksumtype
);
2259 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
2260 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2262 catfileheadercshex
= format(0, 'x').lower();
2263 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2264 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
2265 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
2266 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
2267 nullstrecd
= formatspecs
[5].encode('UTF-8');
2268 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
2269 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
2270 catfp
.write(catfileout
);
2273 os
.fsync(catfp
.fileno());
2274 except io
.UnsupportedOperation
:
2276 except AttributeError:
2278 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2279 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2282 os
.fsync(catfp
.fileno());
2283 except io
.UnsupportedOperation
:
2285 except AttributeError:
2289 if(hasattr(sys
.stdout
, "buffer")):
2290 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2292 shutil
.copyfileobj(catfp
, sys
.stdout
);
2293 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2294 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2296 upload_file_to_internet_file(catfp
, outfile
);
2304 create_alias_function("Pack", __file_format_name__
, "", PackArchiveFile
);
2306 if(hasattr(shutil
, "register_archive_format")):
2307 def PackArchiveFileFunc(archive_name
, source_dir
, **kwargs
):
2308 return PackArchiveFile(source_dir
, archive_name
, False, "auto", None, False, "crc32", [], __file_format_delimiter__
, False, False);
2309 create_alias_function("Pack", __file_format_name__
, "Func", PackArchiveFileFunc
);
2311 def PackArchiveFileFromDirList(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2312 return PackArchiveFile(infiles
, outfile
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
2314 def PackArchiveFileFromTarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2315 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2316 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2317 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
2318 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2319 outfile
= RemoveWindowsPath(outfile
);
2320 checksumtype
= checksumtype
.lower();
2321 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2322 checksumtype
="crc32";
2323 if(checksumtype
=="none"):
2325 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2327 if(compression
not in compressionlist
and compression
is None):
2328 compression
= "auto";
2330 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2331 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2332 if(os
.path
.exists(outfile
)):
2337 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2339 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2342 fbasename
= os
.path
.splitext(outfile
)[0];
2343 fextname
= os
.path
.splitext(outfile
)[1];
2344 catfp
= CompressOpenFile(outfile
, compressionlevel
);
2345 catver
= formatspecs
[6];
2346 fileheaderver
= str(int(catver
.replace(".", "")));
2347 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
2348 catfp
.write(fileheader
.encode('UTF-8'));
2354 inodetocatinode
= {};
2357 if(hasattr(sys
.stdin
, "buffer")):
2358 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
2360 shutil
.copyfileobj(sys
.stdin
, infile
);
2365 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
2366 infile
= download_file_from_internet_file(infile
);
2371 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2373 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
2375 if(not tarfile
.is_tarfile(infile
)):
2377 except AttributeError:
2378 if(not is_tarfile(infile
)):
2383 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2384 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
2386 tarfp
= tarfile
.open(infile
, "r");
2387 except FileNotFoundError
:
2389 fnumfiles
= format(int(len(tarfp
.getmembers())), 'x').lower();
2390 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2391 if(checksumtype
=="none" or checksumtype
==""):
2392 catfileheadercshex
= format(0, 'x').lower();
2393 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2394 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2395 elif(checksumtype
=="crc16_ccitt"):
2396 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2397 elif(checksumtype
=="adler32"):
2398 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2399 elif(checksumtype
=="crc32"):
2400 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2401 elif(checksumtype
=="crc64_ecma"):
2402 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2403 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2404 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2405 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2406 checksumoutstr
= hashlib
.new(checksumtype
);
2407 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
2408 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2410 catfileheadercshex
= format(0, 'x').lower();
2411 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2412 catfp
.write(fnumfilesa
.encode('UTF-8'));
2415 os
.fsync(catfp
.fileno());
2416 except io
.UnsupportedOperation
:
2418 except AttributeError:
2420 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
2421 catfhstart
= catfp
.tell();
2422 if(re
.findall("^[.|/]", member
.name
)):
2423 fname
= member
.name
;
2425 fname
= "./"+member
.name
;
2427 VerbosePrintOut(fname
);
2428 fpremode
= member
.mode
;
2429 ffullmode
= member
.mode
;
2433 ffullmode
= member
.mode
+ stat
.S_IFREG
;
2435 elif(member
.isdev()):
2436 ffullmode
= member
.mode
;
2438 elif(member
.islnk()):
2439 ffullmode
= member
.mode
+ stat
.S_IFREG
;
2441 elif(member
.issym()):
2442 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
2444 elif(member
.ischr()):
2445 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
2447 elif(member
.isblk()):
2448 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
2450 elif(member
.isdir()):
2451 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
2453 elif(member
.isfifo()):
2454 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
2456 elif(member
.issparse()):
2457 ffullmode
= member
.mode
;
2460 ffullmode
= member
.mode
;
2463 fcurfid
= format(int(curfid
), 'x').lower();
2464 fcurinode
= format(int(0), 'x').lower();
2465 curfid
= curfid
+ 1;
2467 flinkname
= member
.linkname
;
2468 fdev_minor
= format(int(member
.devminor
), 'x').lower();
2469 fdev_major
= format(int(member
.devmajor
), 'x').lower();
2470 frdev_minor
= format(int(member
.devminor
), 'x').lower();
2471 frdev_major
= format(int(member
.devmajor
), 'x').lower();
2472 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
2473 fsize
= format(int("0"), 'x').lower();
2474 elif(ftype
==0 or ftype
==7):
2475 fsize
= format(int(member
.size
), 'x').lower();
2477 fsize
= format(int(member
.size
), 'x').lower();
2478 fatime
= format(int(member
.mtime
), 'x').lower();
2479 fmtime
= format(int(member
.mtime
), 'x').lower();
2480 fctime
= format(int(member
.mtime
), 'x').lower();
2481 fbtime
= format(int(member
.mtime
), 'x').lower();
2482 fmode
= format(int(ffullmode
), 'x').lower();
2483 fchmode
= format(int(stat
.S_IMODE(ffullmode
)), 'x').lower();
2484 ftypemod
= format(int(stat
.S_IFMT(ffullmode
)), 'x').lower();
2485 fuid
= format(int(member
.uid
), 'x').lower();
2486 fgid
= format(int(member
.gid
), 'x').lower();
2487 funame
= member
.uname
;
2488 fgname
= member
.gname
;
2489 flinkcount
= format(int(flinkcount
), 'x').lower();
2490 fwinattributes
= format(int(0), 'x').lower();
2491 fcontents
= "".encode('UTF-8');
2493 if(ftype
== 0 or ftype
== 7):
2494 with tarfp
.extractfile(member
) as fpc
:
2496 chunk
= fpc
.read(chunk_size
);
2500 ftypehex
= format(ftype
, 'x').lower();
2501 extrafields
= format(len(extradata
), 'x').lower();
2502 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
2503 if(len(extradata
)>0):
2504 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2505 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2506 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
];
2507 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
2508 catoutlenhex
= format(catoutlen
, 'x').lower();
2509 catoutlist
.insert(0, catoutlenhex
);
2510 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
2511 if(len(extradata
)>0):
2512 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2513 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
2514 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
2515 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
2516 if(checksumtype
=="none" or checksumtype
==""):
2517 catfileheadercshex
= format(0, 'x').lower();
2518 catfilecontentcshex
= format(0, 'x').lower();
2519 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2520 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2521 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
2522 elif(checksumtype
=="crc16_ccitt"):
2523 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2524 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
2525 elif(checksumtype
=="adler32"):
2526 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2527 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
2528 elif(checksumtype
=="crc32"):
2529 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2530 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
2531 elif(checksumtype
=="crc64_ecma"):
2532 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2533 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
2534 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2535 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2536 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
2537 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2538 checksumoutstr
= hashlib
.new(checksumtype
);
2539 checksumoutstr
.update("".encode('UTF-8'));
2540 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2541 checksumoutstr
= hashlib
.new(checksumtype
);
2542 checksumoutstr
.update(fcontents
);
2543 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
2545 catfileheadercshex
= format(0, 'x').lower();
2546 catfilecontentcshex
= format(0, 'x').lower();
2547 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2548 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2549 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
2550 if(checksumtype
=="none" or checksumtype
==""):
2551 catfileheadercshex
= format(0, 'x').lower();
2552 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2553 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2554 elif(checksumtype
=="crc16_ccitt"):
2555 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2556 elif(checksumtype
=="adler32"):
2557 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2558 elif(checksumtype
=="crc32"):
2559 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2560 elif(checksumtype
=="crc64_ecma"):
2561 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2562 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2563 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2564 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2565 checksumoutstr
= hashlib
.new(checksumtype
);
2566 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
2567 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2569 catfileheadercshex
= format(0, 'x').lower();
2570 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2571 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
2572 nullstrecd
= formatspecs
[5].encode('UTF-8');
2573 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
2574 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
2575 catfp
.write(catfileout
);
2578 os
.fsync(catfp
.fileno());
2579 except io
.UnsupportedOperation
:
2581 except AttributeError:
2583 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2584 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2587 os
.fsync(catfp
.fileno());
2588 except io
.UnsupportedOperation
:
2590 except AttributeError:
2594 if(hasattr(sys
.stdout
, "buffer")):
2595 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2597 shutil
.copyfileobj(catfp
, sys
.stdout
);
2598 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2599 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2601 upload_file_from_internet_file(catfp
, outfile
);
2609 create_alias_function("Pack", __file_format_name__
, "FromTarFile", PackArchiveFileFromTarFile
);
2611 def PackArchiveFileFromZipFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2612 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2613 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2614 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
2615 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2616 outfile
= RemoveWindowsPath(outfile
);
2617 checksumtype
= checksumtype
.lower();
2618 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2619 checksumtype
="crc32";
2620 if(checksumtype
=="none"):
2622 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2624 if(compression
not in compressionlist
and compression
is None):
2625 compression
= "auto";
2627 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2628 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2629 if(os
.path
.exists(outfile
)):
2634 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2636 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2639 fbasename
= os
.path
.splitext(outfile
)[0];
2640 fextname
= os
.path
.splitext(outfile
)[1];
2641 catfp
= CompressOpenFile(outfile
, compressionlevel
);
2642 catver
= formatspecs
[6];
2643 fileheaderver
= str(int(catver
.replace(".", "")));
2644 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
2645 catfp
.write(fileheader
.encode('UTF-8'));
2651 inodetocatinode
= {};
2654 if(hasattr(sys
.stdin
, "buffer")):
2655 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
2657 shutil
.copyfileobj(sys
.stdin
, infile
);
2662 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
2663 infile
= download_file_from_internet_file(infile
);
2668 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2672 if(not zipfile
.is_zipfile(infile
)):
2675 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
2676 except FileNotFoundError
:
2678 ziptest
= zipfp
.testzip();
2680 VerbosePrintOut("Bad file found!");
2681 fnumfiles
= format(int(len(zipfp
.infolist())), 'x').lower();
2682 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2683 if(checksumtype
=="none" or checksumtype
==""):
2684 catfileheadercshex
= format(0, 'x').lower();
2685 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2686 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2687 elif(checksumtype
=="crc16_ccitt"):
2688 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2689 elif(checksumtype
=="adler32"):
2690 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2691 elif(checksumtype
=="crc32"):
2692 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2693 elif(checksumtype
=="crc64_ecma"):
2694 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2695 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2696 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2697 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2698 checksumoutstr
= hashlib
.new(checksumtype
);
2699 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
2700 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2702 catfileheadercshex
= format(0, 'x').lower();
2703 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2704 catfp
.write(fnumfilesa
.encode('UTF-8'));
2707 os
.fsync(catfp
.fileno());
2708 except io
.UnsupportedOperation
:
2710 except AttributeError:
2712 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
2713 catfhstart
= catfp
.tell();
2714 if(re
.findall("^[.|/]", member
.filename
)):
2715 fname
= member
.filename
;
2717 fname
= "./"+member
.filename
;
2718 zipinfo
= zipfp
.getinfo(member
.filename
);
2720 VerbosePrintOut(fname
);
2721 if(not member
.is_dir()):
2722 fpremode
= int(stat
.S_IFREG
+ 438);
2723 elif(member
.is_dir()):
2724 fpremode
= int(stat
.S_IFDIR
+ 511);
2727 if(not member
.is_dir()):
2729 elif(member
.is_dir()):
2732 fcurfid
= format(int(curfid
), 'x').lower();
2733 fcurinode
= format(int(0), 'x').lower();
2734 curfid
= curfid
+ 1;
2735 fdev_minor
= format(int(0), 'x').lower();
2736 fdev_major
= format(int(0), 'x').lower();
2737 frdev_minor
= format(int(0), 'x').lower();
2738 frdev_major
= format(int(0), 'x').lower();
2740 fsize
= format(int("0"), 'x').lower();
2742 fsize
= format(int(member
.file_size
), 'x').lower();
2744 fsize
= format(int(member
.file_size
), 'x').lower();
2745 fatime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2746 fmtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2747 fctime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2748 fbtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2749 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
2750 fwinattributes
= format(int(zipinfo
.external_attr
), 'x').lower();
2751 if(not member
.is_dir()):
2752 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
2753 fchmode
= stat
.S_IMODE(fmode
);
2754 ftypemod
= stat
.S_IFMT(fmode
);
2755 elif(member
.is_dir()):
2756 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
2757 fchmode
= stat
.S_IMODE(fmode
);
2758 ftypemod
= stat
.S_IFMT(fmode
);
2759 elif(zipinfo
.create_system
==3):
2760 fwinattributes
= format(int(0), 'x').lower();
2761 fmode
= format(int(zipinfo
.external_attr
), 'x').lower();
2762 fchmode
= stat
.S_IMODE(fmode
);
2763 ftypemod
= stat
.S_IFMT(fmode
);
2765 fwinattributes
= format(int(0), 'x').lower();
2766 if(not member
.is_dir()):
2767 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
2768 fchmode
= stat
.S_IMODE(fmode
);
2769 ftypemod
= stat
.S_IFMT(fmode
);
2770 elif(member
.is_dir()):
2771 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
2772 fchmode
= stat
.S_IMODE(fmode
);
2773 ftypemod
= stat
.S_IFMT(fmode
);
2775 fuid
= format(int(os
.getuid()), 'x').lower();
2776 except AttributeError:
2777 fuid
= format(int(0), 'x').lower();
2779 fuid
= format(int(0), 'x').lower();
2781 fgid
= format(int(os
.getgid()), 'x').lower();
2782 except AttributeError:
2783 fgid
= format(int(0), 'x').lower();
2785 fgid
= format(int(0), 'x').lower();
2789 userinfo
= pwd
.getpwuid(os
.getuid());
2790 funame
= userinfo
.pw_name
;
2793 except AttributeError:
2801 groupinfo
= grp
.getgrgid(os
.getgid());
2802 fgname
= groupinfo
.gr_name
;
2805 except AttributeError:
2809 fcontents
= "".encode('UTF-8');
2811 fcontents
= zipfp
.read(member
.filename
);
2812 ftypehex
= format(ftype
, 'x').lower();
2813 extrafields
= format(len(extradata
), 'x').lower();
2814 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
2815 if(len(extradata
)>0):
2816 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2817 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2818 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
];
2819 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
2820 catoutlenhex
= format(catoutlen
, 'x').lower();
2821 catoutlist
.insert(0, catoutlenhex
);
2822 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
2823 if(len(extradata
)>0):
2824 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2825 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
2826 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
2827 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
2828 if(checksumtype
=="none" or checksumtype
==""):
2829 catfileheadercshex
= format(0, 'x').lower();
2830 catfilecontentcshex
= format(0, 'x').lower();
2831 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2832 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2833 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
2834 elif(checksumtype
=="crc16_ccitt"):
2835 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2836 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
2837 elif(checksumtype
=="adler32"):
2838 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2839 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
2840 elif(checksumtype
=="crc32"):
2841 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2842 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
2843 elif(checksumtype
=="crc64_ecma"):
2844 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2845 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
2846 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2847 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2848 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
2849 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2850 checksumoutstr
= hashlib
.new(checksumtype
);
2851 checksumoutstr
.update("".encode('UTF-8'));
2852 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2853 checksumoutstr
= hashlib
.new(checksumtype
);
2854 checksumoutstr
.update(fcontents
);
2855 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
2857 catfileheadercshex
= format(0, 'x').lower();
2858 catfilecontentcshex
= format(0, 'x').lower();
2859 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2860 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2861 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
2862 if(checksumtype
=="none" or checksumtype
==""):
2863 catfileheadercshex
= format(0, 'x').lower();
2864 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2865 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2866 elif(checksumtype
=="crc16_ccitt"):
2867 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2868 elif(checksumtype
=="adler32"):
2869 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2870 elif(checksumtype
=="crc32"):
2871 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2872 elif(checksumtype
=="crc64_ecma"):
2873 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2874 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2875 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2876 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2877 checksumoutstr
= hashlib
.new(checksumtype
);
2878 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
2879 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2881 catfileheadercshex
= format(0, 'x').lower();
2882 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2883 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
2884 nullstrecd
= formatspecs
[5].encode('UTF-8');
2885 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
2886 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
2887 catfp
.write(catfileout
);
2890 os
.fsync(catfp
.fileno());
2891 except io
.UnsupportedOperation
:
2893 except AttributeError:
2895 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2896 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2899 os
.fsync(catfp
.fileno());
2900 except io
.UnsupportedOperation
:
2902 except AttributeError:
2906 if(hasattr(sys
.stdout
, "buffer")):
2907 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2909 shutil
.copyfileobj(catfp
, sys
.stdout
);
2910 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2911 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2913 upload_file_to_internet_file(catfp
, outfile
);
2921 create_alias_function("Pack", __file_format_name__
, "FromZipFile", PackArchiveFileFromZipFile
);
2923 if(not rarfile_support
):
2924 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2927 if(rarfile_support
):
2928 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2929 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2930 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2931 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
2932 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2933 outfile
= RemoveWindowsPath(outfile
);
2934 checksumtype
= checksumtype
.lower();
2935 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2936 checksumtype
="crc32";
2937 if(checksumtype
=="none"):
2939 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2941 if(compression
not in compressionlist
and compression
is None):
2942 compression
= "auto";
2944 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2945 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2946 if(os
.path
.exists(outfile
)):
2951 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2953 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2956 fbasename
= os
.path
.splitext(outfile
)[0];
2957 fextname
= os
.path
.splitext(outfile
)[1];
2958 catfp
= CompressOpenFile(outfile
, compressionlevel
);
2959 catver
= formatspecs
[6];
2960 fileheaderver
= str(int(catver
.replace(".", "")));
2961 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
2962 catfp
.write(fileheader
.encode('UTF-8'));
2968 inodetocatinode
= {};
2969 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2971 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
2973 rarfp
= rarfile
.RarFile(infile
, "r");
2974 rartest
= rarfp
.testrar();
2976 VerbosePrintOut("Bad file found!");
2977 fnumfiles
= format(int(len(rarfp
.infolist())), 'x').lower();
2978 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2979 if(checksumtype
=="none" or checksumtype
==""):
2980 catfileheadercshex
= format(0, 'x').lower();
2981 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2982 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2983 elif(checksumtype
=="crc16_ccitt"):
2984 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2985 elif(checksumtype
=="adler32"):
2986 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2987 elif(checksumtype
=="crc32"):
2988 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2989 elif(checksumtype
=="crc64_ecma"):
2990 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2991 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2992 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2993 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2994 checksumoutstr
= hashlib
.new(checksumtype
);
2995 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
2996 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2998 catfileheadercshex
= format(0, 'x').lower();
2999 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3000 catfp
.write(fnumfilesa
.encode('UTF-8'));
3003 os
.fsync(catfp
.fileno());
3004 except io
.UnsupportedOperation
:
3006 except AttributeError:
3008 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
3011 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
3014 member
.external_attr
3016 except AttributeError:
3018 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
3021 member
.external_attr
3023 except AttributeError:
3028 catfhstart
= catfp
.tell();
3029 if(re
.findall("^[.|/]", member
.filename
)):
3030 fname
= member
.filename
;
3032 fname
= "./"+member
.filename
;
3033 rarinfo
= rarfp
.getinfo(member
.filename
);
3035 VerbosePrintOut(fname
);
3036 if(is_unix
and member
.external_attr
!=0):
3037 fpremode
= int(member
.external_attr
);
3038 elif(member
.is_file()):
3039 fpremode
= int(stat
.S_IFREG
+ 438);
3040 elif(member
.is_symlink()):
3041 fpremode
= int(stat
.S_IFLNK
+ 438);
3042 elif(member
.is_dir()):
3043 fpremode
= int(stat
.S_IFDIR
+ 511);
3044 if(is_windows
and member
.external_attr
!=0):
3045 fwinattributes
= format(int(member
.external_attr
), 'x').lower();
3047 fwinattributes
= format(int(0), 'x').lower();
3050 if(member
.is_file()):
3052 elif(member
.is_symlink()):
3054 elif(member
.is_dir()):
3058 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
3059 fcurfid
= format(int(curfid
), 'x').lower();
3060 fcurinode
= format(int(0), 'x').lower();
3061 curfid
= curfid
+ 1;
3062 fdev_minor
= format(int(0), 'x').lower();
3063 fdev_major
= format(int(0), 'x').lower();
3064 frdev_minor
= format(int(0), 'x').lower();
3065 frdev_major
= format(int(0), 'x').lower();
3067 fsize
= format(int("0"), 'x').lower();
3069 fsize
= format(int(member
.file_size
), 'x').lower();
3071 fsize
= format(int(member
.file_size
), 'x').lower();
3074 fatime
= format(int(member
.atime
.timestamp()), 'x').lower();
3076 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3077 except AttributeError:
3078 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3079 fmtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3082 fctime
= format(int(member
.ctime
.timestamp()), 'x').lower();
3084 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3085 except AttributeError:
3086 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3087 fbtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3088 if(is_unix
and member
.external_attr
!=0):
3089 fmode
= format(int(member
.external_attr
), 'x').lower();
3090 fchmode
= format(int(stat
.S_IMODE(member
.external_attr
)), 'x').lower();
3091 ftypemod
= format(int(stat
.S_IFMT(member
.external_attr
)), 'x').lower();
3092 elif(member
.is_file()):
3093 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
3094 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
3095 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
3096 elif(member
.is_symlink()):
3097 fmode
= format(int(stat
.S_IFLNK
+ 438), 'x').lower();
3098 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
3099 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
3100 elif(member
.is_dir()):
3101 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
3102 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3103 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3105 fuid
= format(int(os
.getuid()), 'x').lower();
3106 except AttributeError:
3107 fuid
= format(int(0), 'x').lower();
3109 fuid
= format(int(0), 'x').lower();
3111 fgid
= format(int(os
.getgid()), 'x').lower();
3112 except AttributeError:
3113 fgid
= format(int(0), 'x').lower();
3115 fgid
= format(int(0), 'x').lower();
3119 userinfo
= pwd
.getpwuid(os
.getuid());
3120 funame
= userinfo
.pw_name
;
3123 except AttributeError:
3131 groupinfo
= grp
.getgrgid(os
.getgid());
3132 fgname
= groupinfo
.gr_name
;
3135 except AttributeError:
3139 fcontents
= "".encode('UTF-8');
3141 fcontents
= rarfp
.read(member
.filename
);
3142 ftypehex
= format(ftype
, 'x').lower();
3143 extrafields
= format(len(extradata
), 'x').lower();
3144 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
3145 if(len(extradata
)>0):
3146 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3147 extrasizelen
= format(len(extrasizestr
), 'x').lower();
3148 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
];
3149 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
3150 catoutlenhex
= format(catoutlen
, 'x').lower();
3151 catoutlist
.insert(0, catoutlenhex
);
3152 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
3153 if(len(extradata
)>0):
3154 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3155 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
3156 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
3157 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
3158 if(checksumtype
=="none" or checksumtype
==""):
3159 catfileheadercshex
= format(0, 'x').lower();
3160 catfilecontentcshex
= format(0, 'x').lower();
3161 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3162 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3163 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
3164 elif(checksumtype
=="crc16_ccitt"):
3165 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3166 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
3167 elif(checksumtype
=="adler32"):
3168 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3169 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
3170 elif(checksumtype
=="crc32"):
3171 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3172 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
3173 elif(checksumtype
=="crc64_ecma"):
3174 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3175 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
3176 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3177 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3178 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
3179 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3180 checksumoutstr
= hashlib
.new(checksumtype
);
3181 checksumoutstr
.update("".encode('UTF-8'));
3182 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3183 checksumoutstr
= hashlib
.new(checksumtype
);
3184 checksumoutstr
.update(fcontents
);
3185 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
3187 catfileheadercshex
= format(0, 'x').lower();
3188 catfilecontentcshex
= format(0, 'x').lower();
3189 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
3190 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
3191 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
3192 if(checksumtype
=="none" or checksumtype
==""):
3193 catfileheadercshex
= format(0, 'x').lower()
3194 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3195 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower()
3196 elif(checksumtype
=="crc16_ccitt"):
3197 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower()
3198 elif(checksumtype
=="adler32"):
3199 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower()
3200 elif(checksumtype
=="crc32"):
3201 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower()
3202 elif(checksumtype
=="crc64_ecma"):
3203 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
3204 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3205 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
3206 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3207 checksumoutstr
= hashlib
.new(checksumtype
)
3208 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'))
3209 catfileheadercshex
= checksumoutstr
.hexdigest().lower()
3211 catfileheadercshex
= format(0, 'x').lower()
3212 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5])
3213 catfileoutstrecd
= catfileoutstr
.encode('UTF-8')
3214 nullstrecd
= formatspecs
[5].encode('UTF-8')
3215 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
3216 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
)
3217 catfp
.write(catfileout
)
3220 os
.fsync(catfp
.fileno())
3221 except io
.UnsupportedOperation
:
3223 except AttributeError:
3225 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3226 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
)
3229 os
.fsync(catfp
.fileno())
3230 except io
.UnsupportedOperation
:
3232 except AttributeError:
3236 if(hasattr(sys
.stdout
, "buffer")):
3237 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
3239 shutil
.copyfileobj(catfp
, sys
.stdout
);
3240 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3241 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
3243 upload_file_to_internet_file(catfp
, outfile
);
3251 create_alias_function("Pack", __file_format_name__
, "FromRarFile", PackArchiveFileFromRarFile
);
3253 def ArchiveFileSeekToFileNum(infile
, seekto
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3254 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3257 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3258 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
3259 if(checkcompressfile
=="tarfile"):
3260 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3261 if(checkcompressfile
=="zipfile"):
3262 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3263 if(rarfile_support
and checkcompressfile
=="rarfile"):
3264 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3265 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3272 if(hasattr(sys
.stdin
, "buffer")):
3273 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
3275 shutil
.copyfileobj(sys
.stdin
, catfp
);
3277 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3281 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
3282 catfp
= download_file_from_internet_file(infile
);
3284 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3289 infile
= RemoveWindowsPath(infile
);
3290 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
3291 if(checkcompressfile
=="tarfile"):
3292 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3293 if(checkcompressfile
=="zipfile"):
3294 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3295 if(rarfile_support
and checkcompressfile
=="rarfile"):
3296 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3297 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3299 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
3300 if(not compresscheck
):
3301 fextname
= os
.path
.splitext(infile
)[1];
3302 if(fextname
==".gz"):
3303 compresscheck
= "gzip";
3304 elif(fextname
==".bz2"):
3305 compresscheck
= "bzip2";
3306 elif(fextname
==".zst"):
3307 compresscheck
= "zstd";
3308 elif(fextname
==".lz4" or fextname
==".clz4"):
3309 compresscheck
= "lz4";
3310 elif(fextname
==".lzo" or fextname
==".lzop"):
3311 compresscheck
= "lzo";
3312 elif(fextname
==".lzma" or fextname
==".xz"):
3313 compresscheck
= "lzma";
3316 if(not compresscheck
):
3318 catfp
= UncompressFile(infile
, formatspecs
, "rb");
3323 SeekToEndOfFile(catfp);
3325 SeekToEndOfFile(catfp);
3326 CatSize = catfp.tell();
3327 CatSizeEnd = CatSize;
3335 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
3336 catstring
= catheader
[0];
3337 catversion
= re
.findall(r
"([\d]+)$", catstring
);
3338 fprenumfiles
= catheader
[1];
3339 fnumfiles
= int(fprenumfiles
, 16);
3340 fprechecksumtype
= catheader
[2];
3341 fprechecksum
= catheader
[3];
3342 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
3343 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3344 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
3345 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
3346 catfileheadercshex
= format(0, 'x').lower();
3347 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
3348 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3349 elif(fprechecksumtype
=="crc16_ccitt"):
3350 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3351 elif(fprechecksumtype
=="adler32"):
3352 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3353 elif(fprechecksumtype
=="crc32"):
3354 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3355 elif(fprechecksumtype
=="crc64_ecma"):
3356 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3357 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
3358 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3359 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
3360 checksumoutstr
= hashlib
.new(fprechecksumtype
);
3361 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3362 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3364 catfileheadercshex
= format(0, 'x').lower();
3365 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3366 fheadtell
= len(fileheader
);
3367 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
3368 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
3370 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
3371 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
3372 if(seekto
>=fnumfiles
):
3373 seekto
= fnumfiles
- 1;
3379 prefhstart
= catfp
.tell();
3381 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3383 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
3384 prefheadsize
= int(preheaderdata
[0], 16);
3385 prefnumfields
= int(preheaderdata
[1], 16);
3386 preftype
= int(preheaderdata
[2], 16);
3387 if(re
.findall("^[.|/]", preheaderdata
[3])):
3388 prefname
= preheaderdata
[3];
3390 prefname
= "./"+preheaderdata
[3];
3391 prefbasedir
= os
.path
.dirname(prefname
);
3392 preflinkname
= preheaderdata
[4];
3393 prefsize
= int(preheaderdata
[5], 16);
3394 prefatime
= int(preheaderdata
[6], 16);
3395 prefmtime
= int(preheaderdata
[7], 16);
3396 prefctime
= int(preheaderdata
[8], 16);
3397 prefbtime
= int(preheaderdata
[9], 16);
3398 prefmode
= int(preheaderdata
[10], 16);
3399 prefchmode
= stat
.S_IMODE(prefmode
);
3400 preftypemod
= stat
.S_IFMT(prefmode
);
3401 prefwinattributes
= int(preheaderdata
[11], 16);
3402 prefuid
= int(preheaderdata
[12], 16);
3403 prefuname
= preheaderdata
[13];
3404 prefgid
= int(preheaderdata
[14], 16);
3405 prefgname
= preheaderdata
[15];
3406 fid
= int(preheaderdata
[16], 16);
3407 finode
= int(preheaderdata
[17], 16);
3408 flinkcount
= int(preheaderdata
[18], 16);
3409 prefdev_minor
= int(preheaderdata
[19], 16);
3410 prefdev_major
= int(preheaderdata
[20], 16);
3411 prefrdev_minor
= int(preheaderdata
[22], 16);
3412 prefrdev_major
= int(preheaderdata
[23], 16);
3413 prefextrasize
= int(preheaderdata
[24], 16);
3414 prefextrafields
= int(preheaderdata
[25], 16);
3415 extrafieldslist
= [];
3417 extraend
= extrastart
+ prefextrafields
;
3418 extrafieldslist
= [];
3419 if(extrastart
<extraend
):
3420 extrafieldslist
.append(preheaderdata
[extrastart
]);
3421 extrastart
= extrastart
+ 1;
3422 prefchecksumtype
= preheaderdata
[extrastart
].lower();
3423 prefcs
= preheaderdata
[extrastart
+ 1].lower();
3424 prefccs
= preheaderdata
[extrastart
+ 2].lower();
3426 hcmax
= len(preheaderdata
) - 2;
3429 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
3431 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
3433 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
3434 prenewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
3435 elif(prefchecksumtype
=="adler32"):
3436 prenewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3437 elif(prefchecksumtype
=="crc32"):
3438 prenewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3439 elif(prefchecksumtype
=="crc64_ecma"):
3440 prenewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3441 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
3442 prenewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3443 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
3444 checksumoutstr
= hashlib
.new(prefchecksumtype
);
3445 checksumoutstr
.update(hout
.encode('UTF-8'));
3446 prenewfcs
= checksumoutstr
.hexdigest().lower();
3447 if(prefcs
!=prenewfcs
and not skipchecksum
):
3448 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
3450 valid_archive
= False;
3451 invalid_archive
= True;
3452 prefhend
= catfp
.tell() - 1;
3453 prefcontentstart
= catfp
.tell();
3455 pyhascontents
= False;
3457 prefcontents
= catfp
.read(prefsize
);
3458 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
3460 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
3461 prenewfccs
= format(crc16(prefcontents
) & 0xffff, '04x').lower();
3462 elif(prefchecksumtype
=="crc16_ccitt"):
3463 prenewfcs
= format(crc16_ccitt(prefcontents
) & 0xffff, '04x').lower();
3464 elif(prefchecksumtype
=="adler32"):
3465 prenewfccs
= format(zlib
.adler32(prefcontents
) & 0xffffffff, '08x').lower();
3466 elif(prefchecksumtype
=="crc32"):
3467 prenewfccs
= format(crc32(prefcontents
) & 0xffffffff, '08x').lower();
3468 elif(prefchecksumtype
=="crc64_ecma"):
3469 prenewfcs
= format(crc64_ecma(prefcontents
) & 0xffffffffffffffff, '016x').lower();
3470 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
3471 prenewfcs
= format(crc64_iso(prefcontents
) & 0xffffffffffffffff, '016x').lower();
3472 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
3473 checksumoutstr
= hashlib
.new(prefchecksumtype
);
3474 checksumoutstr
.update(prefcontents
);
3475 prenewfccs
= checksumoutstr
.hexdigest().lower();
3476 pyhascontents
= True;
3477 if(prefccs
!=prenewfccs
and not skipchecksum
):
3478 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
3482 catfp
.seek(seekstart
, 0);
3484 catfheadsize
= int(preheaderdata
[0], 16);
3485 catfnumfields
= int(preheaderdata
[1], 16);
3486 catftype
= int(preheaderdata
[2], 16);
3487 if(re
.findall("^[.|/]", preheaderdata
[3])):
3488 catfname
= preheaderdata
[3];
3490 catfname
= "./"+preheaderdata
[3];
3491 catflinkname
= preheaderdata
[4];
3492 catfsize
= int(preheaderdata
[5], 16);
3493 catfbasedir
= os
.path
.dirname(catfname
);
3494 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
3496 catlist
.update({'catfp': catfp
});
3501 create_alias_function("", __file_format_name__
, "SeekToFileNum", ArchiveFileSeekToFileNum
);
3503 def ArchiveFileSeekToFileName(infile
, seekfile
=None, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3504 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3507 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3508 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
3509 if(checkcompressfile
=="tarfile"):
3510 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3511 if(checkcompressfile
=="zipfile"):
3512 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3513 if(rarfile_support
and checkcompressfile
=="rarfile"):
3514 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3515 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3522 if(hasattr(sys
.stdin
, "buffer")):
3523 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
3525 shutil
.copyfileobj(sys
.stdin
, catfp
);
3527 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3531 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
3532 catfp
= download_file_from_internet_file(infile
);
3533 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3539 infile
= RemoveWindowsPath(infile
);
3540 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
3541 if(checkcompressfile
=="tarfile"):
3542 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3543 if(checkcompressfile
=="zipfile"):
3544 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3545 if(rarfile_support
and checkcompressfile
=="rarfile"):
3546 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3547 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3549 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
3550 if(not compresscheck
):
3551 fextname
= os
.path
.splitext(infile
)[1];
3552 if(fextname
==".gz"):
3553 compresscheck
= "gzip";
3554 elif(fextname
==".bz2"):
3555 compresscheck
= "bzip2";
3556 elif(fextname
==".zst"):
3557 compresscheck
= "zstd";
3558 elif(fextname
==".lz4" or fextname
==".clz4"):
3559 compresscheck
= "lz4";
3560 elif(fextname
==".lzo" or fextname
==".lzop"):
3561 compresscheck
= "lzo";
3562 elif(fextname
==".lzma" or fextname
==".xz"):
3563 compresscheck
= "lzma";
3566 if(not compresscheck
):
3568 catfp
= UncompressFile(infile
, formatspecs
, "rb");
3573 SeekToEndOfFile(catfp);
3575 SeekToEndOfFile(catfp);
3576 CatSize = catfp.tell();
3577 CatSizeEnd = CatSize;
3585 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
3586 catstring
= catheader
[0];
3587 catversion
= re
.findall(r
"([\d]+)$", catstring
);
3588 fprenumfiles
= catheader
[1];
3589 fnumfiles
= int(fprenumfiles
, 16);
3590 fprechecksumtype
= catheader
[2];
3591 fprechecksum
= catheader
[3];
3592 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
3593 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3594 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
3595 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
3596 catfileheadercshex
= format(0, 'x').lower();
3597 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
3598 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3599 elif(fprechecksumtype
=="crc16_ccitt"):
3600 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3601 elif(fprechecksumtype
=="adler32"):
3602 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3603 elif(fprechecksumtype
=="crc32"):
3604 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3605 elif(fprechecksumtype
=="crc64_ecma"):
3606 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3607 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
3608 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3609 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
3610 checksumoutstr
= hashlib
.new(fprechecksumtype
);
3611 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3612 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3614 catfileheadercshex
= format(0, 'x').lower();
3615 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3616 fheadtell
= len(fileheader
);
3617 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
3618 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
3620 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
3621 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
3622 seekto
= fnumfiles
- 1
3627 prefhstart
= catfp
.tell();
3629 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3631 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
3632 prefheadsize
= int(preheaderdata
[0], 16);
3633 prefnumfields
= int(preheaderdata
[1], 16);
3634 preftype
= int(preheaderdata
[2], 16);
3635 if(re
.findall("^[.|/]", preheaderdata
[3])):
3636 prefname
= preheaderdata
[3];
3638 prefname
= "./"+preheaderdata
[3];
3639 prefbasedir
= os
.path
.dirname(prefname
);
3640 preflinkname
= preheaderdata
[4];
3641 prefsize
= int(preheaderdata
[5], 16);
3642 prefatime
= int(preheaderdata
[6], 16);
3643 prefmtime
= int(preheaderdata
[7], 16);
3644 prefctime
= int(preheaderdata
[8], 16);
3645 prefbtime
= int(preheaderdata
[9], 16);
3646 prefmode
= int(preheaderdata
[10], 16);
3647 prefchmode
= stat
.S_IMODE(prefmode
);
3648 preftypemod
= stat
.S_IFMT(prefmode
);
3649 prefwinattributes
= int(preheaderdata
[11], 16);
3650 prefuid
= int(preheaderdata
[12], 16);
3651 prefuname
= preheaderdata
[13];
3652 prefgid
= int(preheaderdata
[14], 16);
3653 prefgname
= preheaderdata
[15];
3654 fid
= int(preheaderdata
[16], 16);
3655 finode
= int(preheaderdata
[17], 16);
3656 flinkcount
= int(preheaderdata
[18], 16);
3657 prefdev_minor
= int(preheaderdata
[19], 16);
3658 prefdev_major
= int(preheaderdata
[20], 16);
3659 prefrdev_minor
= int(preheaderdata
[22], 16);
3660 prefrdev_major
= int(preheaderdata
[23], 16);
3661 prefextrasize
= int(preheaderdata
[24], 16);
3662 prefextrafields
= int(preheaderdata
[25], 16);
3663 extrafieldslist
= [];
3665 extraend
= extrastart
+ prefextrafields
;
3666 extrafieldslist
= [];
3667 if(extrastart
<extraend
):
3668 extrafieldslist
.append(preheaderdata
[extrastart
]);
3669 extrastart
= extrastart
+ 1;
3670 prefchecksumtype
= preheaderdata
[extrastart
].lower();
3671 prefcs
= preheaderdata
[extrastart
+ 1].lower();
3672 prefccs
= preheaderdata
[extrastart
+ 2].lower();
3674 hcmax
= len(preheaderdata
) - 2;
3677 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
3679 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
3681 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
3682 prenewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
3683 elif(prefchecksumtype
=="adler32"):
3684 prenewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3685 elif(prefchecksumtype
=="crc32"):
3686 prenewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3687 elif(prefchecksumtype
=="crc64_ecma"):
3688 prenewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3689 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
3690 prenewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3691 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
3692 checksumoutstr
= hashlib
.new(prefchecksumtype
);
3693 checksumoutstr
.update(hout
.encode('UTF-8'));
3694 prenewfcs
= checksumoutstr
.hexdigest().lower();
3695 if(prefcs
!=prenewfcs
and not skipchecksum
):
3696 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
3698 valid_archive
= False;
3699 invalid_archive
= True;
3700 prefhend
= catfp
.tell() - 1;
3701 prefcontentstart
= catfp
.tell();
3703 pyhascontents
= False;
3705 prefcontents
= catfp
.read(prefsize
);
3706 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
3708 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
3709 prenewfccs
= format(crc16(prefcontents
) & 0xffff, '04x').lower();
3710 elif(prefchecksumtype
=="crc16_ccitt"):
3711 prenewfcs
= format(crc16_ccitt(prefcontents
) & 0xffff, '04x').lower();
3712 elif(prefchecksumtype
=="adler32"):
3713 prenewfccs
= format(zlib
.adler32(prefcontents
) & 0xffffffff, '08x').lower();
3714 elif(prefchecksumtype
=="crc32"):
3715 prenewfccs
= format(crc32(prefcontents
) & 0xffffffff, '08x').lower();
3716 elif(prefchecksumtype
=="crc64_ecma"):
3717 prenewfcs
= format(crc64_ecma(prefcontents
) & 0xffffffffffffffff, '016x').lower();
3718 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
3719 prenewfcs
= format(crc64_iso(prefcontents
) & 0xffffffffffffffff, '016x').lower();
3720 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
3721 checksumoutstr
= hashlib
.new(prefchecksumtype
);
3722 checksumoutstr
.update(prefcontents
);
3723 prenewfccs
= checksumoutstr
.hexdigest().lower();
3724 pyhascontents
= True;
3725 if(prefccs
!=prenewfccs
and not skipchecksum
):
3726 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
3731 prefname
= preheaderdata
[2];
3732 if(re
.findall("^[.|/]", preheaderdata
[2])):
3733 prefname
= preheaderdata
[2];
3735 prefname
= "./"+preheaderdata
[2];
3736 if(prefname
==seekfile
):
3739 catfp
.seek(seekstart
, 0);
3741 catfheadsize
= int(preheaderdata
[0], 16);
3742 catfnumfields
= int(preheaderdata
[1], 16);
3743 catftype
= int(preheaderdata
[2], 16);
3744 if(re
.findall("^[.|/]", preheaderdata
[3])):
3745 catfname
= preheaderdata
[3];
3747 catfname
= "./"+preheaderdata
[3];
3748 catflinkname
= preheaderdata
[4];
3749 catfsize
= int(preheaderdata
[5], 16);
3750 catfbasedir
= os
.path
.dirname(catfname
);
3752 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
3756 catlist
.update({'catfp': catfp
});
3761 create_alias_function("", __file_format_name__
, "SeekToFileName", ArchiveFileSeekToFileName
);
3763 def ArchiveFileValidate(infile
, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
3765 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
3766 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3769 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3770 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
3771 if(checkcompressfile
=="tarfile"):
3772 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3773 if(checkcompressfile
=="zipfile"):
3774 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3775 if(rarfile_support
and checkcompressfile
=="rarfile"):
3776 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3777 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3784 if(hasattr(sys
.stdin
, "buffer")):
3785 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
3787 shutil
.copyfileobj(sys
.stdin
, catfp
);
3789 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3793 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
3794 catfp
= download_file_from_internet_file(infile
);
3795 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3801 infile
= RemoveWindowsPath(infile
);
3802 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
3803 if(checkcompressfile
=="tarfile"):
3804 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3805 if(checkcompressfile
=="zipfile"):
3806 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3807 if(rarfile_support
and checkcompressfile
=="rarfile"):
3808 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3809 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3811 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
3812 if(not compresscheck
):
3813 fextname
= os
.path
.splitext(infile
)[1];
3814 if(fextname
==".gz"):
3815 compresscheck
= "gzip";
3816 elif(fextname
==".bz2"):
3817 compresscheck
= "bzip2";
3818 elif(fextname
==".zst"):
3819 compresscheck
= "zstd";
3820 elif(fextname
==".lz4" or fextname
==".clz4"):
3821 compresscheck
= "lz4";
3822 elif(fextname
==".lzo" or fextname
==".lzop"):
3823 compresscheck
= "lzo";
3824 elif(fextname
==".lzma" or fextname
==".xz"):
3825 compresscheck
= "lzma";
3828 if(not compresscheck
):
3830 catfp
= UncompressFile(infile
, formatspecs
, "rb");
3835 SeekToEndOfFile(catfp);
3837 SeekToEndOfFile(catfp);
3838 CatSize = catfp.tell();
3839 CatSizeEnd = CatSize;
3847 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
3848 catstring
= catheader
[0];
3849 catversion
= re
.findall(r
"([\d]+)$", catstring
);
3850 fprenumfiles
= catheader
[1];
3851 fnumfiles
= int(fprenumfiles
, 16);
3852 fprechecksumtype
= catheader
[2];
3853 fprechecksum
= catheader
[3];
3855 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
3856 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3857 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
3858 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
3859 catfileheadercshex
= format(0, 'x').lower();
3860 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
3861 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3862 elif(fprechecksumtype
=="crc16_ccitt"):
3863 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3864 elif(fprechecksumtype
=="adler32"):
3865 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3866 elif(fprechecksumtype
=="crc32"):
3867 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3868 elif(fprechecksumtype
=="crc64_ecma"):
3869 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3870 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
3871 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3872 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
3873 checksumoutstr
= hashlib
.new(fprechecksumtype
);
3874 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3875 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3877 catfileheadercshex
= format(0, 'x').lower();
3878 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3879 valid_archive
= True;
3880 invalid_archive
= False;
3882 VerbosePrintOut("Checking File Header Checksum of file at offset " + str(0));
3883 VerbosePrintOut(infile
);
3884 if(fprechecksum
==catfileheadercshex
):
3886 VerbosePrintOut("File Header Checksum Passed");
3889 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
3890 valid_archive
= False;
3891 invalid_archive
= True;
3892 while(il
<fnumfiles
):
3893 catfhstart
= catfp
.tell();
3895 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3897 catheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
3898 catfheadsize
= int(catheaderdata
[0], 16);
3899 catfnumfields
= int(catheaderdata
[1], 16);
3900 catftype
= int(catheaderdata
[2], 16);
3901 if(re
.findall("^[.|/]", catheaderdata
[3])):
3902 catfname
= catheaderdata
[3];
3904 catfname
= "./"+catheaderdata
[3];
3905 catfbasedir
= os
.path
.dirname(catfname
);
3906 catflinkname
= catheaderdata
[4];
3907 catfsize
= int(catheaderdata
[5], 16);
3908 catfatime
= int(catheaderdata
[6], 16);
3909 catfmtime
= int(catheaderdata
[7], 16);
3910 catfctime
= int(catheaderdata
[8], 16);
3911 catfbtime
= int(catheaderdata
[9], 16);
3912 catfmode
= int(catheaderdata
[10], 16);
3913 catfchmode
= stat
.S_IMODE(catfmode
);
3914 catftypemod
= stat
.S_IFMT(catfmode
);
3915 prefwinattributes
= int(catheaderdata
[11], 16);
3916 catfuid
= int(catheaderdata
[12], 16);
3917 catfuname
= catheaderdata
[13];
3918 catfgid
= int(catheaderdata
[14], 16);
3919 catfgname
= catheaderdata
[15];
3920 fid
= int(catheaderdata
[16], 16);
3921 finode
= int(catheaderdata
[17], 16);
3922 flinkcount
= int(catheaderdata
[18], 16);
3923 catfdev_minor
= int(catheaderdata
[19], 16);
3924 catfdev_major
= int(catheaderdata
[20], 16);
3925 catfrdev_minor
= int(catheaderdata
[21], 16);
3926 catfrdev_major
= int(catheaderdata
[22], 16);
3927 catfextrasize
= int(catheaderdata
[23], 16);
3928 catfextrafields
= int(catheaderdata
[24], 16);
3929 extrafieldslist
= [];
3931 extraend
= extrastart
+ catfextrafields
;
3932 extrafieldslist
= [];
3933 if(extrastart
<extraend
):
3934 extrafieldslist
.append(catheaderdata
[extrastart
]);
3935 extrastart
= extrastart
+ 1;
3936 catfchecksumtype
= catheaderdata
[extrastart
].lower();
3937 catfcs
= catheaderdata
[extrastart
+ 1].lower();
3938 catfccs
= catheaderdata
[extrastart
+ 2].lower();
3940 hcmax
= len(catheaderdata
) - 2;
3943 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[5]);
3945 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
3947 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
3948 catnewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
3949 elif(catfchecksumtype
=="adler32"):
3950 catnewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3951 elif(catfchecksumtype
=="crc32"):
3952 catnewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3953 elif(catfchecksumtype
=="crc64_ecma"):
3954 catnewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3955 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
3956 catnewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3957 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
3958 checksumoutstr
= hashlib
.new(catfchecksumtype
);
3959 checksumoutstr
.update(hout
.encode('UTF-8'));
3960 catnewfcs
= checksumoutstr
.hexdigest().lower();
3962 VerbosePrintOut("Checking File Header Checksum of file at offset " + str(catfhstart
));
3963 VerbosePrintOut(catfname
);
3964 if(catfcs
==catnewfcs
):
3966 VerbosePrintOut("File Header Checksum Passed");
3969 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfhstart
));
3970 valid_archive
= False;
3971 invalid_archive
= True;
3972 catfhend
= catfp
.tell() - 1;
3973 catfcontentstart
= catfp
.tell();
3975 pyhascontents
= False;
3977 catfcontents
= catfp
.read(catfsize
);
3978 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
3980 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
3981 catnewfccs
= format(crc16(catfcontents
) & 0xffff, '04x').lower();
3982 elif(catfchecksumtype
=="crc16_ccitt"):
3983 catnewfcs
= format(crc16_ccitt(catfcontents
) & 0xffff, '04x').lower();
3984 elif(catfchecksumtype
=="adler32"):
3985 catnewfccs
= format(zlib
.adler32(catfcontents
) & 0xffffffff, '08x').lower();
3986 elif(catfchecksumtype
=="crc32"):
3987 catnewfccs
= format(crc32(catfcontents
) & 0xffffffff, '08x').lower();
3988 elif(catfchecksumtype
=="crc64_ecma"):
3989 catnewfcs
= format(crc64_ecma(catfcontents
) & 0xffffffffffffffff, '016x').lower();
3990 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
3991 catnewfcs
= format(crc64_iso(catfcontents
) & 0xffffffffffffffff, '016x').lower();
3992 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
3993 checksumoutstr
= hashlib
.new(catfchecksumtype
);
3994 checksumoutstr
.update(catfcontents
);
3995 catnewfccs
= checksumoutstr
.hexdigest().lower();
3996 pyhascontents
= True;
3998 VerbosePrintOut("Checking File Content Checksum of file at offset " + str(catfcontentstart
));
3999 VerbosePrintOut(catfname
);
4000 if(catfccs
==catnewfccs
):
4002 VerbosePrintOut("File Content Checksum Passed");
4005 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfcontentstart
));
4006 valid_archive
= False;
4007 invalid_archive
= True;
4009 VerbosePrintOut("");
4022 create_alias_function("", __file_format_name__
, "Validate", ArchiveFileValidate
);
4024 def ArchiveFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4025 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4028 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4029 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
4030 if(checkcompressfile
=="tarfile"):
4031 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4032 if(checkcompressfile
=="zipfile"):
4033 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4034 if(rarfile_support
and checkcompressfile
=="rarfile"):
4035 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4036 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4043 if(hasattr(sys
.stdin
, "buffer")):
4044 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
4046 shutil
.copyfileobj(sys
.stdin
, catfp
);
4048 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4052 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4053 catfp
= download_file_from_internet_file(infile
);
4054 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4060 infile
= RemoveWindowsPath(infile
);
4061 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
4062 if(checkcompressfile
=="tarfile"):
4063 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4064 if(checkcompressfile
=="zipfile"):
4065 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4066 if(rarfile_support
and checkcompressfile
=="rarfile"):
4067 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4068 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4070 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
4071 if(not compresscheck
):
4072 fextname
= os
.path
.splitext(infile
)[1];
4073 if(fextname
==".gz"):
4074 compresscheck
= "gzip";
4075 elif(fextname
==".bz2"):
4076 compresscheck
= "bzip2";
4077 elif(fextname
==".zst"):
4078 compresscheck
= "zstd";
4079 elif(fextname
==".lz4" or fextname
==".clz4"):
4080 compresscheck
= "lz4";
4081 elif(fextname
==".lzo" or fextname
==".lzop"):
4082 compresscheck
= "lzo";
4083 elif(fextname
==".lzma" or fextname
==".xz"):
4084 compresscheck
= "lzma";
4087 if(not compresscheck
):
4089 catfp
= UncompressFile(infile
, formatspecs
, "rb");
4094 SeekToEndOfFile(catfp);
4096 SeekToEndOfFile(catfp);
4097 CatSize = catfp.tell();
4098 CatSizeEnd = CatSize;
4106 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
4107 catstring
= catheader
[0];
4108 catversion
= re
.findall(r
"([\d]+)$", catstring
);
4109 fprenumfiles
= catheader
[1];
4110 fnumfiles
= int(fprenumfiles
, 16);
4111 fprechecksumtype
= catheader
[2];
4112 fprechecksum
= catheader
[3];
4113 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
4114 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4115 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
4116 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
4117 catfileheadercshex
= format(0, 'x').lower();
4118 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
4119 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4120 elif(fprechecksumtype
=="crc16_ccitt"):
4121 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4122 elif(fprechecksumtype
=="adler32"):
4123 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4124 elif(fprechecksumtype
=="crc32"):
4125 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4126 elif(fprechecksumtype
=="crc64_ecma"):
4127 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4128 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
4129 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4130 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
4131 checksumoutstr
= hashlib
.new(fprechecksumtype
);
4132 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4133 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4135 catfileheadercshex
= format(0, 'x').lower();
4136 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4137 fheadtell
= len(fileheader
);
4138 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
4139 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
4141 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4142 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
4143 if(seekstart
<0 and seekstart
>fnumfiles
):
4145 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
4146 seekend
= fnumfiles
;
4147 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
4148 seekend
= fnumfiles
- abs(seekend
);
4151 while(il
< seekstart
):
4152 prefhstart
= catfp
.tell();
4154 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
4156 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
4157 prefheadsize
= int(preheaderdata
[0], 16);
4158 prefnumfields
= int(preheaderdata
[1], 16);
4159 if(re
.findall("^[.|/]", preheaderdata
[3])):
4160 prefname
= preheaderdata
[3];
4162 prefname
= "./"+preheaderdata
[3];
4163 prefsize
= int(preheaderdata
[5], 16);
4164 prefextrasize
= int(preheaderdata
[23], 16);
4165 prefextrafields
= int(preheaderdata
[24], 16);
4166 extrafieldslist
= [];
4168 extraend
= extrastart
+ prefextrafields
;
4169 extrafieldslist
= [];
4170 if(extrastart
<extraend
):
4171 extrafieldslist
.append(preheaderdata
[extrastart
]);
4172 extrastart
= extrastart
+ 1;
4173 prefchecksumtype
= preheaderdata
[extrastart
].lower();
4174 prefcs
= preheaderdata
[extrastart
+ 1].lower();
4175 prefccs
= preheaderdata
[extrastart
+ 2].lower();
4177 hcmax
= len(preheaderdata
) - 2;
4180 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
4182 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
4184 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
4185 prenewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
4186 elif(prefchecksumtype
=="adler32"):
4187 prenewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4188 elif(prefchecksumtype
=="crc32"):
4189 prenewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4190 elif(prefchecksumtype
=="crc64_ecma"):
4191 prenewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4192 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
4193 prenewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4194 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
4195 checksumoutstr
= hashlib
.new(prefchecksumtype
);
4196 checksumoutstr
.update(hout
.encode('UTF-8'));
4197 prenewfcs
= checksumoutstr
.hexdigest().lower();
4198 if(prefcs
!=prenewfcs
and not skipchecksum
):
4199 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
4201 valid_archive
= False;
4202 invalid_archive
= True;
4203 prefhend
= catfp
.tell() - 1;
4204 prefcontentstart
= catfp
.tell();
4206 pyhascontents
= False;
4208 prefcontents
= catfp
.read(prefsize
);
4209 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
4211 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
4212 prenewfccs
= format(crc16(prefcontents
) & 0xffff, '04x').lower();
4213 elif(prefchecksumtype
=="crc16_ccitt"):
4214 prenewfcs
= format(crc16_ccitt(prefcontents
) & 0xffff, '04x').lower();
4215 elif(prefchecksumtype
=="adler32"):
4216 prenewfccs
= format(zlib
.adler32(prefcontents
) & 0xffffffff, '08x').lower();
4217 elif(prefchecksumtype
=="crc32"):
4218 prenewfccs
= format(crc32(prefcontents
) & 0xffffffff, '08x').lower();
4219 elif(prefchecksumtype
=="crc64_ecma"):
4220 prenewfcs
= format(crc64_ecma(prefcontents
) & 0xffffffffffffffff, '016x').lower();
4221 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
4222 prenewfcs
= format(crc64_iso(prefcontents
) & 0xffffffffffffffff, '016x').lower();
4223 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
4224 checksumoutstr
= hashlib
.new(prefchecksumtype
);
4225 checksumoutstr
.update(prefcontents
);
4226 prenewfccs
= checksumoutstr
.hexdigest().lower();
4227 pyhascontents
= True;
4228 if(prefccs
!=prenewfccs
and not skipchecksum
):
4229 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
4233 fileidnum
= seekstart
;
4235 while(fileidnum
<seekend
):
4236 catfhstart
= catfp
.tell();
4238 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
4240 catheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
4241 catfheadsize
= int(catheaderdata
[0], 16);
4242 catfnumfields
= int(catheaderdata
[1], 16);
4243 catftype
= int(catheaderdata
[2], 16);
4244 if(re
.findall("^[.|/]", catheaderdata
[3])):
4245 catfname
= catheaderdata
[3];
4247 catfname
= "./"+catheaderdata
[3];
4248 catfbasedir
= os
.path
.dirname(catfname
);
4249 catflinkname
= catheaderdata
[4];
4250 catfsize
= int(catheaderdata
[5], 16);
4251 catfatime
= int(catheaderdata
[6], 16);
4252 catfmtime
= int(catheaderdata
[7], 16);
4253 catfctime
= int(catheaderdata
[8], 16);
4254 catfbtime
= int(catheaderdata
[9], 16);
4255 catfmode
= int(catheaderdata
[10], 16);
4256 catfchmode
= stat
.S_IMODE(catfmode
);
4257 catftypemod
= stat
.S_IFMT(catfmode
);
4258 catfwinattributes
= int(catheaderdata
[11], 16);
4259 catfuid
= int(catheaderdata
[12], 16);
4260 catfuname
= catheaderdata
[13];
4261 catfgid
= int(catheaderdata
[14], 16);
4262 catfgname
= catheaderdata
[15];
4263 fid
= int(catheaderdata
[16], 16);
4264 finode
= int(catheaderdata
[17], 16);
4265 flinkcount
= int(catheaderdata
[18], 16);
4266 catfdev_minor
= int(catheaderdata
[19], 16);
4267 catfdev_major
= int(catheaderdata
[20], 16);
4268 catfrdev_minor
= int(catheaderdata
[21], 16);
4269 catfrdev_major
= int(catheaderdata
[22], 16);
4270 catfextrasize
= int(catheaderdata
[23], 16);
4271 catfextrafields
= int(catheaderdata
[24], 16);
4272 extrafieldslist
= [];
4274 extraend
= extrastart
+ catfextrafields
;
4275 extrafieldslist
= [];
4276 if(extrastart
<extraend
):
4277 extrafieldslist
.append(catheaderdata
[extrastart
]);
4278 extrastart
= extrastart
+ 1;
4279 catfchecksumtype
= catheaderdata
[extrastart
].lower();
4280 catfcs
= catheaderdata
[extrastart
+ 1].lower();
4281 catfccs
= catheaderdata
[extrastart
+ 2].lower();
4283 hcmax
= len(catheaderdata
) - 2;
4286 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[5]);
4288 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
4290 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
4291 catnewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
4292 elif(catfchecksumtype
=="adler32"):
4293 catnewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4294 elif(catfchecksumtype
=="crc32"):
4295 catnewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4296 elif(catfchecksumtype
=="crc64_ecma"):
4297 catnewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4298 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
4299 catnewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4300 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
4301 checksumoutstr
= hashlib
.new(catfchecksumtype
);
4302 checksumoutstr
.update(hout
.encode('UTF-8'));
4303 catnewfcs
= checksumoutstr
.hexdigest().lower();
4304 if(catfcs
!=catnewfcs
and not skipchecksum
):
4305 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfhstart
));
4307 catfhend
= catfp
.tell() - 1;
4308 catfcontentstart
= catfp
.tell();
4310 pyhascontents
= False;
4311 if(catfsize
>0 and not listonly
):
4312 catfcontents
= catfp
.read(catfsize
);
4313 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
4315 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
4316 catnewfccs
= format(crc16(catfcontents
) & 0xffff, '04x').lower();
4317 elif(catfchecksumtype
=="crc16_ccitt"):
4318 catnewfcs
= format(crc16_ccitt(catfcontents
) & 0xffff, '04x').lower();
4319 elif(catfchecksumtype
=="adler32"):
4320 catnewfccs
= format(zlib
.adler32(catfcontents
) & 0xffffffff, '08x').lower();
4321 elif(catfchecksumtype
=="crc32"):
4322 catnewfccs
= format(crc32(catfcontents
) & 0xffffffff, '08x').lower();
4323 elif(catfchecksumtype
=="crc64_ecma"):
4324 catnewfcs
= format(crc64_ecma(catfcontents
) & 0xffffffffffffffff, '016x').lower();
4325 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
4326 catnewfcs
= format(crc64_iso(catfcontents
) & 0xffffffffffffffff, '016x').lower();
4327 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
4328 checksumoutstr
= hashlib
.new(catfchecksumtype
);
4329 checksumoutstr
.update(catfcontents
);
4330 catnewfccs
= checksumoutstr
.hexdigest().lower();
4331 pyhascontents
= True;
4332 if(catfccs
!=catnewfccs
and skipchecksum
):
4333 VerbosePrintOut("File Content Checksum Error with file " + catfname
+ " at offset " + str(catfcontentstart
));
4335 if(catfsize
>0 and listonly
):
4336 catfp
.seek(catfsize
, 1);
4337 pyhascontents
= False;
4339 catfcontentend
= catfp
.tell() - 1;
4340 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
} });
4341 fileidnum
= fileidnum
+ 1;
4342 realidnum
= realidnum
+ 1;
4344 catlist
.update({'catfp': catfp
});
4349 create_alias_function("", __file_format_name__
, "ToArray", ArchiveFileToArray
);
4351 def ArchiveFileStringToArray(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4352 catfp
= BytesIO(catstr
);
4353 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4354 return listcatfiles
;
4356 create_alias_function("", __file_format_name__
, "StringToArray", ArchiveFileStringToArray
);
4358 def TarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4360 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4361 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4362 return listcatfiles
;
4364 def ZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4366 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4367 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4368 return listcatfiles
;
4370 if(not rarfile_support
):
4371 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4374 if(rarfile_support
):
4375 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4377 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4378 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4379 return listcatfiles
;
4381 def ListDirToArrayAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4382 catver
= formatspecs
[6];
4383 fileheaderver
= str(int(catver
.replace(".", "")));
4384 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4385 advancedlist
= True;
4388 for line
in sys
.stdin
:
4389 infilelist
.append(line
.strip());
4390 infilelist
= list(filter(None, infilelist
));
4391 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
4392 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
4394 with
open(infiles
, "r") as finfile
:
4395 for line
in finfile
:
4396 infilelist
.append(line
.strip());
4397 infilelist
= list(filter(None, infilelist
));
4399 if(isinstance(infiles
, (list, tuple, ))):
4400 infilelist
= list(filter(None, infiles
));
4401 elif(isinstance(infiles
, (str, ))):
4402 infilelist
= list(filter(None, [infiles
]));
4404 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
4406 GetDirList
= ListDir(infilelist
, followlink
, False);
4414 inodetocatinode
= {};
4416 fnumfiles
= int(len(GetDirList
));
4417 catver
= formatspecs
[6];
4418 fileheaderver
= str(int(catver
.replace(".", "")));
4419 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4420 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4421 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4422 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4423 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4424 if(checksumtype
=="none" or checksumtype
==""):
4425 catfileheadercshex
= format(0, 'x').lower();
4426 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4427 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4428 elif(checksumtype
=="crc16_ccitt"):
4429 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4430 elif(checksumtype
=="adler32"):
4431 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4432 elif(checksumtype
=="crc32"):
4433 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4434 elif(checksumtype
=="crc64_ecma"):
4435 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4436 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4437 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4438 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4439 checksumoutstr
= hashlib
.new(checksumtype
);
4440 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4441 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4443 catfileheadercshex
= format(0, 'x').lower();
4444 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4445 fheadtell
= len(fileheader
);
4446 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4447 for curfname
in GetDirList
:
4448 if(re
.findall("^[.|/]", curfname
)):
4451 fname
= "./"+curfname
;
4453 VerbosePrintOut(fname
);
4454 if(not followlink
or followlink
is None):
4455 fstatinfo
= os
.lstat(fname
);
4457 fstatinfo
= os
.stat(fname
);
4458 fpremode
= fstatinfo
.st_mode
;
4459 finode
= fstatinfo
.st_ino
;
4460 flinkcount
= fstatinfo
.st_nlink
;
4462 if(stat
.S_ISREG(fpremode
)):
4464 elif(stat
.S_ISLNK(fpremode
)):
4466 elif(stat
.S_ISCHR(fpremode
)):
4468 elif(stat
.S_ISBLK(fpremode
)):
4470 elif(stat
.S_ISDIR(fpremode
)):
4472 elif(stat
.S_ISFIFO(fpremode
)):
4474 elif(stat
.S_ISSOCK(fpremode
)):
4476 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
4478 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
4480 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
4485 fbasedir
= os
.path
.dirname(fname
);
4487 if(not followlink
and finode
!=0):
4489 if(finode
in inodelist
):
4491 flinkname
= inodetofile
[finode
];
4492 fcurinode
= inodetocatinode
[finode
];
4493 if(finode
not in inodelist
):
4494 inodelist
.append(finode
);
4495 inodetofile
.update({finode
: fname
});
4496 inodetocatinode
.update({finode
: curinode
});
4497 fcurinode
= curinode
;
4498 curinode
= curinode
+ 1;
4500 fcurinode
= curinode
;
4501 curinode
= curinode
+ 1;
4502 curfid
= curfid
+ 1;
4504 flinkname
= os
.readlink(fname
);
4505 fdev
= fstatinfo
.st_dev
;
4506 getfdev
= GetDevMajorMinor(fdev
);
4507 fdev_minor
= getfdev
[0];
4508 fdev_major
= getfdev
[1];
4509 frdev
= fstatinfo
.st_dev
;
4510 if(hasattr(fstatinfo
, "st_rdev")):
4511 frdev
= fstatinfo
.st_rdev
;
4513 frdev
= fstatinfo
.st_dev
;
4514 getfrdev
= GetDevMajorMinor(frdev
);
4515 frdev_minor
= getfrdev
[0];
4516 frdev_major
= getfrdev
[1];
4517 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
4519 if(ftype
==0 or ftype
==7):
4520 fsize
= fstatinfo
.st_size
;
4521 fatime
= fstatinfo
.st_atime
;
4522 fmtime
= fstatinfo
.st_mtime
;
4523 fctime
= fstatinfo
.st_ctime
;
4524 if(hasattr(fstatinfo
, "st_birthtime")):
4525 fbtime
= fstatinfo
.st_birthtime
;
4527 fbtime
= fstatinfo
.st_ctime
;
4528 fmode
= fstatinfo
.st_mode
;
4529 fchmode
= stat
.S_IMODE(fstatinfo
.st_mode
);
4530 ftypemod
= stat
.S_IFMT(fstatinfo
.st_mode
);
4531 fuid
= fstatinfo
.st_uid
;
4532 fgid
= fstatinfo
.st_gid
;
4537 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
4538 funame
= userinfo
.pw_name
;
4547 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
4548 fgname
= groupinfo
.gr_name
;
4553 fdev_minor
= fdev_minor
;
4554 fdev_major
= fdev_major
;
4555 frdev_minor
= frdev_minor
;
4556 frdev_major
= frdev_major
;
4558 flinkcount
= flinkcount
;
4559 if(hasattr(fstatinfo
, "st_file_attributes")):
4560 fwinattributes
= fstatinfo
.st_file_attributes
;
4563 fcontents
= "".encode('UTF-8');
4565 if(ftype
== 0 or ftype
== 7):
4566 with
open(fname
, "rb") as fpc
:
4568 chunk
= fpc
.read(chunk_size
);
4572 if(followlink
and (ftype
== 1 or ftype
== 2)):
4573 flstatinfo
= os
.stat(flinkname
);
4574 with
open(flinkname
, "rb") as fpc
:
4576 chunk
= fpc
.read(chunk_size
);
4580 ftypehex
= format(ftype
, 'x').lower();
4581 extrafields
= len(extradata
);
4582 extrafieldslist
= extradata
;
4583 catfextrafields
= extrafields
;
4584 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
4585 if(len(extradata
)>0):
4586 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4587 extrasizelen
= len(extrasizestr
);
4588 extrasizelenhex
= format(extrasizelen
, 'x').lower();
4589 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()];
4590 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
4591 catoutlenhex
= format(catoutlen
, 'x').lower();
4592 catoutlist
.insert(0, catoutlenhex
);
4593 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
4594 if(len(extradata
)>0):
4595 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4596 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
4597 catfnumfields
= catoutlen
;
4598 if(checksumtype
=="none" or checksumtype
==""):
4599 catfileheadercshex
= format(0, 'x').lower();
4600 catfilecontentcshex
= format(0, 'x').lower();
4601 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4602 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
4603 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
4604 elif(checksumtype
=="crc16_ccitt"):
4605 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
4606 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
4607 elif(checksumtype
=="adler32"):
4608 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4609 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
4610 elif(checksumtype
=="crc32"):
4611 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4612 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
4613 elif(checksumtype
=="crc64_ecma"):
4614 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4615 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
4616 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4617 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4618 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
4619 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4620 checksumoutstr
= hashlib
.new(checksumtype
);
4621 checksumoutstr
.update("".encode('UTF-8'));
4622 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4623 checksumoutstr
= hashlib
.new(checksumtype
);
4624 checksumoutstr
.update(fcontents
);
4625 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
4627 catfileheadercshex
= format(0, 'x').lower();
4628 catfilecontentcshex
= format(0, 'x').lower();
4629 catfhstart
= fheadtell
;
4630 fheadtell
+= len(catfileoutstr
);
4631 catfhend
= fheadtell
- 1;
4632 catfcontentstart
= fheadtell
;
4633 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4634 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
4635 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
4636 catheaderdata
= [catheaersize
] + catoutlist
+ extradata
+ [checksumtype
, catfileheadercshex
, catfilecontentcshex
]
4637 if(checksumtype
=="none" or checksumtype
==""):
4638 catfileheadercshex
= format(0, 'x').lower();
4639 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4640 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4641 elif(checksumtype
=="crc16_ccitt"):
4642 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4643 elif(checksumtype
=="adler32"):
4644 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4645 elif(checksumtype
=="crc32"):
4646 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4647 elif(checksumtype
=="crc64_ecma"):
4648 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4649 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4650 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4651 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4652 checksumoutstr
= hashlib
.new(checksumtype
);
4653 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
4654 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4656 catfileheadercshex
= format(0, 'x').lower();
4657 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4658 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
4659 nullstrecd
= formatspecs
[5].encode('UTF-8');
4660 fheadtell
+= len(catfileoutstr
) + 1;
4661 catfcontentend
= fheadtell
- 1;
4662 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
4663 pyhascontents
= False;
4664 if(int(fsize
)>0 and not listonly
):
4665 pyhascontents
= True;
4666 if(int(fsize
)>0 and listonly
):
4668 pyhascontents
= False;
4669 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
} });
4670 fileidnum
= fileidnum
+ 1;
4673 def TarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4679 inodetocatinode
= {};
4683 if(hasattr(sys
.stdin
, "buffer")):
4684 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
4686 shutil
.copyfileobj(sys
.stdin
, infile
);
4691 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4692 infile
= download_file_from_internet_file(infile
);
4697 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
4699 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
4701 if(not tarfile
.is_tarfile(infile
)):
4703 except AttributeError:
4704 if(not is_tarfile(infile
)):
4709 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4710 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
4712 tarfp
= tarfile
.open(infile
, "r");
4713 except FileNotFoundError
:
4715 fnumfiles
= int(len(tarfp
.getmembers()));
4716 catver
= formatspecs
[6];
4717 fileheaderver
= str(int(catver
.replace(".", "")));
4718 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4719 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4720 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4721 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4722 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4723 if(checksumtype
=="none" or checksumtype
==""):
4724 catfileheadercshex
= format(0, 'x').lower();
4725 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4726 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4727 elif(checksumtype
=="crc16_ccitt"):
4728 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4729 elif(checksumtype
=="adler32"):
4730 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4731 elif(checksumtype
=="crc32"):
4732 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4733 elif(checksumtype
=="crc64_ecma"):
4734 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4735 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4736 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4737 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4738 checksumoutstr
= hashlib
.new(checksumtype
);
4739 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4740 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4742 catfileheadercshex
= format(0, 'x').lower();
4743 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4744 fheadtell
= len(fileheader
);
4745 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4746 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
4747 if(re
.findall("^[.|/]", member
.name
)):
4748 fname
= member
.name
;
4750 fname
= "./"+member
.name
;
4752 VerbosePrintOut(fname
);
4753 fpremode
= member
.mode
;
4754 ffullmode
= member
.mode
;
4758 ffullmode
= member
.mode
+ stat
.S_IFREG
;
4760 elif(member
.isdev()):
4761 ffullmode
= member
.mode
;
4763 elif(member
.islnk()):
4764 ffullmode
= member
.mode
+ stat
.S_IFREG
;
4766 elif(member
.issym()):
4767 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
4769 elif(member
.ischr()):
4770 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
4772 elif(member
.isblk()):
4773 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
4775 elif(member
.isdir()):
4776 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
4778 elif(member
.isfifo()):
4779 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
4781 elif(member
.issparse()):
4782 ffullmode
= member
.mode
;
4785 ffullmode
= member
.mode
;
4788 fbasedir
= os
.path
.dirname(fname
);
4792 curfid
= curfid
+ 1;
4794 flinkname
= member
.linkname
;
4795 fdev_minor
= member
.devminor
;
4796 fdev_major
= member
.devmajor
;
4797 frdev_minor
= member
.devminor
;
4798 frdev_major
= member
.devmajor
;
4799 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
4801 elif(ftype
==0 or ftype
==7):
4802 fsize
= member
.size
;
4804 fsize
= member
.size
;
4805 fatime
= member
.mtime
;
4806 fmtime
= member
.mtime
;
4807 fctime
= member
.mtime
;
4808 fbtime
= member
.mtime
;
4810 fchmode
= stat
.S_IMODE(ffullmode
);
4811 ftypemod
= stat
.S_IFMT(ffullmode
);
4814 funame
= member
.uname
;
4815 fgname
= member
.gname
;
4816 flinkcount
= flinkcount
;
4817 fwinattributes
= int(0);
4818 fcontents
= "".encode('UTF-8');
4820 if(ftype
== 0 or ftype
== 7):
4821 with tarfp
.extractfile(member
) as fpc
:
4823 chunk
= fpc
.read(chunk_size
);
4827 ftypehex
= format(ftype
, 'x').lower();
4828 extrafields
= len(extradata
);
4829 extrafieldslist
= extradata
;
4830 catfextrafields
= extrafields
;
4831 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
4832 if(len(extradata
)>0):
4833 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4834 extrasizelen
= len(extrasizestr
);
4835 extrasizelenhex
= format(extrasizelen
, 'x').lower();
4836 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()];
4837 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
4838 catoutlenhex
= format(catoutlen
, 'x').lower();
4839 catoutlist
.insert(0, catoutlenhex
);
4840 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
4841 if(len(extradata
)>0):
4842 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4843 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
4844 catfnumfields
= catoutlen
;
4845 if(checksumtype
=="none" or checksumtype
==""):
4846 catfileheadercshex
= format(0, 'x').lower();
4847 catfilecontentcshex
= format(0, 'x').lower();
4848 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4849 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
4850 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
4851 elif(checksumtype
=="crc16_ccitt"):
4852 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
4853 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
4854 elif(checksumtype
=="adler32"):
4855 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4856 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
4857 elif(checksumtype
=="crc32"):
4858 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4859 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
4860 elif(checksumtype
=="crc64_ecma"):
4861 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4862 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
4863 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4864 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4865 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
4866 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4867 checksumoutstr
= hashlib
.new(checksumtype
);
4868 checksumoutstr
.update("".encode('UTF-8'));
4869 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4870 checksumoutstr
= hashlib
.new(checksumtype
);
4871 checksumoutstr
.update(fcontents
);
4872 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
4874 catfileheadercshex
= format(0, 'x').lower();
4875 catfilecontentcshex
= format(0, 'x').lower();
4876 catfhstart
= fheadtell
;
4877 fheadtell
+= len(catfileoutstr
);
4878 catfhend
= fheadtell
- 1;
4879 catfcontentstart
= fheadtell
;
4880 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4881 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
4882 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
4883 catheaderdata
= [catheaersize
] + catoutlist
+ extradata
+ [checksumtype
, catfileheadercshex
, catfilecontentcshex
]
4884 if(checksumtype
=="none" or checksumtype
==""):
4885 catfileheadercshex
= format(0, 'x').lower();
4886 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4887 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4888 elif(checksumtype
=="crc16_ccitt"):
4889 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4890 elif(checksumtype
=="adler32"):
4891 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4892 elif(checksumtype
=="crc32"):
4893 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4894 elif(checksumtype
=="crc64_ecma"):
4895 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4896 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4897 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4898 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4899 checksumoutstr
= hashlib
.new(checksumtype
);
4900 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
4901 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4903 catfileheadercshex
= format(0, 'x').lower();
4904 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4905 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
4906 nullstrecd
= formatspecs
[5].encode('UTF-8');
4907 fheadtell
+= len(catfileoutstr
) + 1;
4908 catfcontentend
= fheadtell
- 1;
4909 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
4910 pyhascontents
= False;
4911 if(int(fsize
)>0 and not listonly
):
4912 pyhascontents
= True;
4913 if(int(fsize
)>0 and listonly
):
4915 pyhascontents
= False;
4916 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
} });
4917 fileidnum
= fileidnum
+ 1;
4920 def ZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4921 advancedlist
= True;
4927 inodetocatinode
= {};
4931 if(hasattr(sys
.stdin
, "buffer")):
4932 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
4934 shutil
.copyfileobj(sys
.stdin
, infile
);
4939 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4940 infile
= download_file_from_internet_file(infile
);
4945 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
4949 if(not zipfile
.is_zipfile(infile
)):
4952 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
4953 except FileNotFoundError
:
4955 ziptest
= zipfp
.testzip();
4957 VerbosePrintOut("Bad file found!");
4958 fnumfiles
= int(len(zipfp
.infolist()));
4959 catver
= formatspecs
[6];
4960 fileheaderver
= str(int(catver
.replace(".", "")));
4961 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4962 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4963 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4964 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4965 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4966 if(checksumtype
=="none" or checksumtype
==""):
4967 catfileheadercshex
= format(0, 'x').lower();
4968 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4969 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4970 elif(checksumtype
=="crc16_ccitt"):
4971 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4972 elif(checksumtype
=="adler32"):
4973 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4974 elif(checksumtype
=="crc32"):
4975 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4976 elif(checksumtype
=="crc64_ecma"):
4977 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4978 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4979 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4980 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4981 checksumoutstr
= hashlib
.new(checksumtype
);
4982 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4983 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4985 catfileheadercshex
= format(0, 'x').lower();
4986 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4987 fheadtell
= len(fileheader
);
4988 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4989 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
4990 if(re
.findall("^[.|/]", member
.filename
)):
4991 fname
= member
.filename
;
4993 fname
= "./"+member
.filename
;
4994 zipinfo
= zipfp
.getinfo(member
.filename
);
4996 VerbosePrintOut(fname
);
4997 if(not member
.is_dir()):
4998 fpremode
= stat
.S_IFREG
+ 438;
4999 elif(member
.is_dir()):
5000 fpremode
= stat
.S_IFDIR
+ 511;
5003 if(not member
.is_dir()):
5005 elif(member
.is_dir()):
5008 fbasedir
= os
.path
.dirname(fname
);
5012 curfid
= curfid
+ 1;
5020 fsize
= member
.file_size
;
5022 fsize
= member
.file_size
;
5023 fatime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5024 fmtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5025 fctime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5026 fbtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5027 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
5028 fwinattributes
= format(int(zipinfo
.external_attr
), 'x').lower();
5029 if(not member
.is_dir()):
5030 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
5031 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
5032 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
5033 elif(member
.is_dir()):
5034 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
5035 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
5036 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
5037 elif(zipinfo
.create_system
==3):
5038 fwinattributes
= format(int(0), 'x').lower();
5039 fmode
= format(int(zipinfo
.external_attr
), 'x').lower();
5041 fwinattributes
= format(int(0), 'x').lower();
5042 if(not member
.is_dir()):
5043 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
5044 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
5045 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
5046 elif(member
.is_dir()):
5047 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
5048 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
5049 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
5052 except AttributeError:
5058 except AttributeError:
5065 userinfo
= pwd
.getpwuid(os
.getuid());
5066 funame
= userinfo
.pw_name
;
5069 except AttributeError:
5077 groupinfo
= grp
.getgrgid(os
.getgid());
5078 fgname
= groupinfo
.gr_name
;
5081 except AttributeError:
5085 fcontents
= "".encode('UTF-8');
5087 fcontents
= zipfp
.read(member
.filename
);
5088 ftypehex
= format(ftype
, 'x').lower();
5089 extrafields
= len(extradata
);
5090 extrafieldslist
= extradata
;
5091 catfextrafields
= extrafields
;
5092 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5093 if(len(extradata
)>0):
5094 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5095 extrasizelen
= len(extrasizestr
);
5096 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5097 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()];
5098 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5099 catoutlenhex
= format(catoutlen
, 'x').lower();
5100 catoutlist
.insert(0, catoutlenhex
);
5101 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5102 if(len(extradata
)>0):
5103 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5104 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5105 catfnumfields
= catoutlen
;
5106 if(checksumtype
=="none" or checksumtype
==""):
5107 catfileheadercshex
= format(0, 'x').lower();
5108 catfilecontentcshex
= format(0, 'x').lower();
5109 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5110 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5111 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
5112 elif(checksumtype
=="crc16_ccitt"):
5113 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5114 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
5115 elif(checksumtype
=="adler32"):
5116 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5117 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
5118 elif(checksumtype
=="crc32"):
5119 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5120 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
5121 elif(checksumtype
=="crc64_ecma"):
5122 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5123 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
5124 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5125 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5126 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
5127 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5128 checksumoutstr
= hashlib
.new(checksumtype
);
5129 checksumoutstr
.update("".encode('UTF-8'));
5130 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5131 checksumoutstr
= hashlib
.new(checksumtype
);
5132 checksumoutstr
.update(fcontents
);
5133 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
5135 catfileheadercshex
= format(0, 'x').lower();
5136 catfilecontentcshex
= format(0, 'x').lower();
5137 catfhstart
= fheadtell
;
5138 fheadtell
+= len(catfileoutstr
);
5139 catfhend
= fheadtell
- 1;
5140 catfcontentstart
= fheadtell
;
5141 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5142 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
5143 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5144 catheaderdata
= [catheaersize
] + catoutlist
+ extradata
+ [checksumtype
, catfileheadercshex
, catfilecontentcshex
];
5145 if(checksumtype
=="none" or checksumtype
==""):
5146 catfileheadercshex
= format(0, 'x').lower();
5147 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5148 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5149 elif(checksumtype
=="crc16_ccitt"):
5150 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5151 elif(checksumtype
=="adler32"):
5152 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5153 elif(checksumtype
=="crc32"):
5154 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5155 elif(checksumtype
=="crc64_ecma"):
5156 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5157 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5158 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5159 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5160 checksumoutstr
= hashlib
.new(checksumtype
);
5161 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5162 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5164 catfileheadercshex
= format(0, 'x').lower();
5165 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5166 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5167 nullstrecd
= formatspecs
[5].encode('UTF-8');
5168 fheadtell
+= len(catfileoutstr
) + 1;
5169 catfcontentend
= fheadtell
- 1;
5170 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
5171 pyhascontents
= False;
5172 if(int(fsize
)>0 and not listonly
):
5173 pyhascontents
= True;
5174 if(int(fsize
)>0 and listonly
):
5176 pyhascontents
= False;
5177 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
} });
5178 fileidnum
= fileidnum
+ 1;
5181 if(not rarfile_support
):
5182 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5185 if(rarfile_support
):
5186 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5187 advancedlist
= True;
5193 inodetocatinode
= {};
5195 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
5197 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
5199 rarfp
= rarfile
.RarFile(infile
, "r");
5200 rartest
= rarfp
.testrar();
5202 VerbosePrintOut("Bad file found!");
5203 fnumfiles
= int(len(rarfp
.infolist()));
5204 catver
= formatspecs
[6];
5205 fileheaderver
= str(int(catver
.replace(".", "")));
5206 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5207 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5208 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5209 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5210 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
5211 if(checksumtype
=="none" or checksumtype
==""):
5212 catfileheadercshex
= format(0, 'x').lower();
5213 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5214 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
5215 elif(checksumtype
=="crc16_ccitt"):
5216 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
5217 elif(checksumtype
=="adler32"):
5218 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5219 elif(checksumtype
=="crc32"):
5220 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5221 elif(checksumtype
=="crc64_ecma"):
5222 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5223 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5224 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5225 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5226 checksumoutstr
= hashlib
.new(checksumtype
);
5227 checksumoutstr
.update(fileheader
.encode('UTF-8'));
5228 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5230 catfileheadercshex
= format(0, 'x').lower();
5231 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
5232 fheadtell
= len(fileheader
);
5233 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5234 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
5237 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
5240 member
.external_attr
5242 except AttributeError:
5244 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
5247 member
.external_attr
5249 except AttributeError:
5254 if(re
.findall("^[.|/]", member
.filename
)):
5255 fname
= member
.filename
;
5257 fname
= "./"+member
.filename
;
5258 rarinfo
= rarfp
.getinfo(member
.filename
);
5260 VerbosePrintOut(fname
);
5261 if(is_unix
and member
.external_attr
!=0):
5262 fpremode
= int(member
.external_attr
);
5263 elif(member
.is_file()):
5264 fpremode
= stat
.S_IFREG
+ 438;
5265 elif(member
.is_symlink()):
5266 fpremode
= stat
.S_IFLNK
+ 438;
5267 elif(member
.is_dir()):
5268 fpremode
= stat
.S_IFDIR
+ 511;
5269 if(is_windows
and member
.external_attr
!=0):
5270 fwinattributes
= int(member
.external_attr
);
5272 fwinattributes
= int(0);
5275 if(member
.is_file()):
5277 elif(member
.is_symlink()):
5279 elif(member
.is_dir()):
5283 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
5284 fbasedir
= os
.path
.dirname(fname
);
5288 curfid
= curfid
+ 1;
5296 fsize
= member
.file_size
;
5299 fatime
= int(member
.atime
.timestamp());
5301 fatime
= int(member
.mtime
.timestamp());
5302 except AttributeError:
5303 fatime
= int(member
.mtime
.timestamp());
5304 fmtime
= int(member
.mtime
.timestamp());
5307 fctime
= int(member
.ctime
.timestamp());
5309 fctime
= int(member
.mtime
.timestamp());
5310 except AttributeError:
5311 fctime
= int(member
.mtime
.timestamp());
5312 fbtime
= int(member
.mtime
.timestamp());
5313 if(is_unix
and member
.external_attr
!=0):
5314 fmode
= format(int(member
.external_attr
), 'x').lower();
5315 fchmode
= format(int(stat
.S_IMODE(member
.external_attr
)), 'x').lower();
5316 ftypemod
= format(int(stat
.S_IFMT(member
.external_attr
)), 'x').lower();
5317 elif(member
.is_file()):
5318 fmode
= int(stat
.S_IFREG
+ 438)
5319 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
5320 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
5321 elif(member
.is_symlink()):
5322 fmode
= int(stat
.S_IFLNK
+ 438)
5323 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
5324 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
5325 elif(member
.is_dir()):
5326 fmode
= int(stat
.S_IFDIR
+ 511)
5327 fchmode
= int(stat
.S_IMODE(stat
.S_IFDIR
+ 511));
5328 ftypemod
= int(stat
.S_IFMT(stat
.S_IFDIR
+ 511));
5331 except AttributeError:
5337 except AttributeError:
5344 userinfo
= pwd
.getpwuid(os
.getuid());
5345 funame
= userinfo
.pw_name
;
5348 except AttributeError:
5356 groupinfo
= grp
.getgrgid(os
.getgid());
5357 fgname
= groupinfo
.gr_name
;
5360 except AttributeError:
5364 fcontents
= "".encode('UTF-8');
5366 fcontents
= rarfp
.read(member
.filename
);
5367 ftypehex
= format(ftype
, 'x').lower();
5368 extrafields
= len(extradata
);
5369 extrafieldslist
= extradata
;
5370 catfextrafields
= extrafields
;
5371 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5372 if(len(extradata
)>0):
5373 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5374 extrasizelen
= len(extrasizestr
);
5375 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5376 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()];
5377 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5378 catoutlenhex
= format(catoutlen
, 'x').lower();
5379 catoutlist
.insert(0, catoutlenhex
);
5380 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5381 if(len(extradata
)>0):
5382 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5383 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5384 catfnumfields
= 24 + catfextrafields
;
5385 if(checksumtype
=="none" or checksumtype
==""):
5386 catfileheadercshex
= format(0, 'x').lower();
5387 catfilecontentcshex
= format(0, 'x').lower();
5388 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5389 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5390 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
5391 elif(checksumtype
=="crc16_ccitt"):
5392 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5393 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
5394 elif(checksumtype
=="adler32"):
5395 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5396 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
5397 elif(checksumtype
=="crc32"):
5398 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5399 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
5400 elif(checksumtype
=="crc64_ecma"):
5401 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5402 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
5403 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5404 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5405 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
5406 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5407 checksumoutstr
= hashlib
.new(checksumtype
);
5408 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5409 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5411 catfileheadercshex
= format(0, 'x').lower();
5412 catfilecontentcshex
= format(0, 'x').lower();
5413 catfhstart
= fheadtell
;
5414 fheadtell
+= len(catfileoutstr
);
5415 catfhend
= fheadtell
- 1;
5416 catfcontentstart
= fheadtell
;
5417 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5418 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
5419 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5420 catheaderdata
= [catheaersize
] + catoutlist
+ extradata
+ [checksumtype
, catfileheadercshex
, catfilecontentcshex
];
5421 if(checksumtype
=="none" or checksumtype
==""):
5422 catfileheadercshex
= format(0, 'x').lower();
5423 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5424 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5425 elif(checksumtype
=="crc16_ccitt"):
5426 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5427 elif(checksumtype
=="adler32"):
5428 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5429 elif(checksumtype
=="crc32"):
5430 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5431 elif(checksumtype
=="crc64_ecma"):
5432 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5433 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5434 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5435 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5436 checksumoutstr
= hashlib
.new(checksumtype
);
5437 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5438 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5440 catfileheadercshex
= format(0, 'x').lower();
5441 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5442 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5443 nullstrecd
= formatspecs
[5].encode('UTF-8');
5444 fheadtell
+= len(catfileoutstr
) + 1;
5445 catfcontentend
= fheadtell
- 1;
5446 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
5447 pyhascontents
= False;
5448 if(int(fsize
)>0 and not listonly
):
5449 pyhascontents
= True;
5450 if(int(fsize
)>0 and listonly
):
5452 pyhascontents
= False;
5453 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
} });
5454 fileidnum
= fileidnum
+ 1;
5457 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):
5458 outarray
= BytesIO();
5459 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
5460 listcatfiles
= ArchiveFileToArray(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5461 return listcatfiles
;
5463 def ArchiveFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5464 if(isinstance(infile
, dict)):
5465 listcatfiles
= infile
;
5467 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5468 infile
= RemoveWindowsPath(infile
);
5469 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5470 if(not listcatfiles
):
5472 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': {}}}};
5474 catarray
.update({'catfp': listcatfiles
['catfp']});
5475 lenlist
= len(listcatfiles
['ffilelist']);
5477 lcfx
= int(listcatfiles
['fnumfiles']);
5478 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5479 lcfx
= int(lenlist
);
5481 lcfx
= int(listcatfiles
['fnumfiles']);
5483 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5484 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5485 catarray
['filetoid'].update(filetoidarray
);
5486 catarray
['idtofile'].update(idtofilearray
);
5487 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5488 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5489 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5490 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5491 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5492 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5493 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5494 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5495 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5496 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5497 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5498 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5499 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5500 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5501 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5502 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5503 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5504 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5505 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5506 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5507 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5508 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5509 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5510 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5511 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5512 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5513 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5514 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5515 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5516 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5517 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5521 create_alias_function("", __file_format_name__
, "ToArrayIndex", ArchiveFileToArrayIndex
);
5523 def ListDirToArrayIndexAlt(infiles
, dirlistfromtxt
=False, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5524 listcatfiles
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5525 if(not listcatfiles
):
5527 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': {}}}};
5528 lenlist
= len(listcatfiles
['ffilelist']);
5530 lcfx
= int(listcatfiles
['fnumfiles']);
5531 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5532 lcfx
= int(lenlist
);
5534 lcfx
= int(listcatfiles
['fnumfiles']);
5536 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5537 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5538 catarray
['filetoid'].update(filetoidarray
);
5539 catarray
['idtofile'].update(idtofilearray
);
5540 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5541 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5542 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5543 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5544 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5545 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5546 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5547 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5548 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5549 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5550 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5551 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5552 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5553 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5554 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5555 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5556 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5557 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5558 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5559 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5560 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5561 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5562 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5563 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5564 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5565 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5566 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5567 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5568 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5569 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5570 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5574 def TarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5575 listcatfiles
= TarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5576 if(not listcatfiles
):
5578 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': {}}}};
5579 lenlist
= len(listcatfiles
['ffilelist']);
5581 lcfx
= int(listcatfiles
['fnumfiles']);
5582 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5583 lcfx
= int(lenlist
);
5585 lcfx
= int(listcatfiles
['fnumfiles']);
5587 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5588 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5589 catarray
['filetoid'].update(filetoidarray
);
5590 catarray
['idtofile'].update(idtofilearray
);
5591 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5592 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5593 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5594 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5595 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5596 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5597 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5598 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5599 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5600 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5601 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5602 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5603 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5604 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5605 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5606 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5607 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5608 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5609 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5610 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5611 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5612 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5613 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5614 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5615 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5616 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5617 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5618 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5619 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5620 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5621 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5625 def ZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5626 listcatfiles
= ZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5627 if(not listcatfiles
):
5629 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': {}}}};
5630 lenlist
= len(listcatfiles
['ffilelist']);
5632 lcfx
= int(listcatfiles
['fnumfiles']);
5633 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5634 lcfx
= int(lenlist
);
5636 lcfx
= int(listcatfiles
['fnumfiles']);
5638 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5639 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5640 catarray
['filetoid'].update(filetoidarray
);
5641 catarray
['idtofile'].update(idtofilearray
);
5642 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5643 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5644 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5645 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5646 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5647 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5648 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5649 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5650 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5651 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5652 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5653 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5654 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5655 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5656 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5657 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5658 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5659 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5660 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5661 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5662 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5663 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5664 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5665 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5666 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5667 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5668 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5669 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5670 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5671 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5672 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5676 if(not rarfile_support
):
5677 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5680 if(rarfile_support
):
5681 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5682 listcatfiles
= RarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5683 if(not listcatfiles
):
5685 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': {}}}};
5686 lenlist
= len(listcatfiles
['ffilelist']);
5688 lcfx
= int(listcatfiles
['fnumfiles']);
5689 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5690 lcfx
= int(lenlist
);
5692 lcfx
= int(listcatfiles
['fnumfiles']);
5694 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5695 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5696 catarray
['filetoid'].update(filetoidarray
);
5697 catarray
['idtofile'].update(idtofilearray
);
5698 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5699 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5700 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5701 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5702 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5703 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5704 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5705 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5706 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5707 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5708 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5709 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5710 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5711 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5712 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5713 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5714 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5715 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5716 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5717 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5718 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5719 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5720 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5721 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5722 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5723 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5724 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5725 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5726 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5727 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5728 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5732 def ArchiveFileStringToArrayIndex(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5733 catfp
= BytesIO(catstr
);
5734 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5735 return listcatfiles
;
5737 create_alias_function("", __file_format_name__
, "StringToArrayIndex", ArchiveFileStringToArrayIndex
);
5739 def TarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5741 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5742 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5743 return listcatfiles
;
5745 def ZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5747 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5748 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5749 return listcatfiles
;
5751 if(not rarfile_support
):
5752 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5755 if(rarfile_support
):
5756 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5758 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5759 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5760 return listcatfiles
;
5762 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):
5763 outarray
= BytesIO();
5764 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, verbose
, True);
5765 listcatfiles
= ArchiveFileToArrayIndex(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
)
5766 return listcatfiles
;
5768 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):
5769 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
5770 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzop', 'lzo', 'lzma', 'xz'];
5771 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', 'lzop', '.lzo', '.lzma', '.xz'];
5772 if(isinstance(infile
, dict)):
5773 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5774 listcatfiles
= prelistcatfiles
['list'];
5776 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5777 infile
= RemoveWindowsPath(infile
);
5779 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5780 listcatfiles
= prelistcatfiles
['list'];
5782 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5783 if(outfile
!="-" and not hasattr(infile
, "read") and not hasattr(outfile
, "write")):
5784 outfile
= RemoveWindowsPath(outfile
);
5785 checksumtype
= checksumtype
.lower();
5786 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
5787 checksumtype
="crc32";
5788 if(checksumtype
=="none"):
5790 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
5792 if(compression
not in compressionlist
and compression
is None):
5793 compression
= "auto";
5795 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5796 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
5797 if(os
.path
.exists(outfile
)):
5799 if(not listcatfiles
):
5804 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
5806 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
5809 fbasename
= os
.path
.splitext(outfile
)[0];
5810 fextname
= os
.path
.splitext(outfile
)[1];
5811 catfp
= CompressOpenFile(outfile
, compressionlevel
);
5812 catver
= formatspecs
[6];
5813 fileheaderver
= str(int(catver
.replace(".", "")));
5814 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5815 catfp
.write(fileheader
.encode('UTF-8'));
5816 lenlist
= len(listcatfiles
['ffilelist']);
5817 fnumfiles
= int(listcatfiles
['fnumfiles']);
5818 if(lenlist
>fnumfiles
or lenlist
<fnumfiles
):
5819 fnumfiles
= lenlist
;
5820 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5821 fnumfilesa
= AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
5822 if(checksumtype
=="none" or checksumtype
==""):
5823 catfileheadercshex
= format(0, 'x').lower();
5824 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5825 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
5826 elif(checksumtype
=="crc16_ccitt"):
5827 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
5828 elif(checksumtype
=="adler32"):
5829 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
5830 elif(checksumtype
=="crc32"):
5831 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
5832 elif(checksumtype
=="crc64_ecma"):
5833 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5834 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5835 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5836 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5837 checksumoutstr
= hashlib
.new(checksumtype
);
5838 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
5839 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5841 catfileheadercshex
= format(0, 'x').lower();
5842 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
5843 catfp
.write(fnumfilesa
.encode('UTF-8'));
5846 os
.fsync(catfp
.fileno());
5847 except io
.UnsupportedOperation
:
5849 except AttributeError:
5851 lenlist
= len(listcatfiles
['ffilelist']);
5852 fnumfiles
= int(listcatfiles
['fnumfiles']);
5854 lcfx
= int(listcatfiles
['fnumfiles']);
5855 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5856 lcfx
= int(lenlist
);
5858 lcfx
= int(listcatfiles
['fnumfiles']);
5866 catfhstart
= catfp
.tell();
5867 if(re
.findall("^[.|/]", listcatfiles
['ffilelist'][reallcfi
]['fname'])):
5868 fname
= listcatfiles
['ffilelist'][reallcfi
]['fname'];
5870 fname
= "./"+listcatfiles
['ffilelist'][reallcfi
]['fname'];
5872 VerbosePrintOut(fname
);
5873 fheadersize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fheadersize']), 'x').lower();
5874 fsize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fsize']), 'x').lower();
5875 flinkname
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
5876 fatime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fatime']), 'x').lower();
5877 fmtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmtime']), 'x').lower();
5878 fctime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fctime']), 'x').lower();
5879 fbtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fbtime']), 'x').lower();
5880 fmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmode']), 'x').lower();
5881 fchmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fchmode']), 'x').lower();
5882 fuid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fuid']), 'x').lower();
5883 funame
= listcatfiles
['ffilelist'][reallcfi
]['funame'];
5884 fgid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fgid']), 'x').lower();
5885 fgname
= listcatfiles
['ffilelist'][reallcfi
]['fgname'];
5886 finode
= listcatfiles
['ffilelist'][reallcfi
]['finode'];
5887 flinkcount
= listcatfiles
['ffilelist'][reallcfi
]['flinkcount'];
5888 fwinattributes
= flinkinfo
['ffilelist'][reallcfi
]['fwinattributes'];
5889 fdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fminor']), 'x').lower();
5890 fdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmajor']), 'x').lower();
5891 frdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frminor']), 'x').lower();
5892 frdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frmajor']), 'x').lower();
5893 if(len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] and len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>0):
5894 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
5895 if(len(extradata
) > 0):
5896 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(extradata
);
5897 listcatfiles
['ffilelist'][reallcfi
]['fextralist'] = extradata
;
5898 extrafields
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']), 'x').lower();
5899 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5900 if(len(extradata
)>0):
5901 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5902 extrasizelen
= format(len(extrasizestr
), 'x').lower();
5903 fcontents
= listcatfiles
['ffilelist'][reallcfi
]['fcontents'];
5905 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']==1 or listcatfiles
['ffilelist'][reallcfi
]['ftype']==2):
5906 getflinkpath
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
5907 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
5908 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
5909 fheadersize
= format(int(flinkinfo
['fheadersize']), 'x').lower();
5910 fsize
= format(int(flinkinfo
['fsize']), 'x').lower();
5911 flinkname
= flinkinfo
['flinkname'];
5912 fatime
= format(int(flinkinfo
['fatime']), 'x').lower();
5913 fmtime
= format(int(flinkinfo
['fmtime']), 'x').lower();
5914 fctime
= format(int(flinkinfo
['fctime']), 'x').lower();
5915 fbtime
= format(int(flinkinfo
['fbtime']), 'x').lower();
5916 fmode
= format(int(flinkinfo
['fmode']), 'x').lower();
5917 fchmode
= format(int(flinkinfo
['fchmode']), 'x').lower();
5918 fuid
= format(int(flinkinfo
['fuid']), 'x').lower();
5919 funame
= flinkinfo
['funame'];
5920 fgid
= format(int(flinkinfo
['fgid']), 'x').lower();
5921 fgname
= flinkinfo
['fgname'];
5922 finode
= flinkinfo
['finode'];
5923 flinkcount
= flinkinfo
['flinkcount'];
5924 fwinattributes
= flinkinfo
['fwinattributes'];
5925 fdev_minor
= format(int(flinkinfo
['fminor']), 'x').lower();
5926 fdev_major
= format(int(flinkinfo
['fmajor']), 'x').lower();
5927 frdev_minor
= format(int(flinkinfo
['frminor']), 'x').lower();
5928 frdev_major
= format(int(flinkinfo
['frmajor']), 'x').lower();
5929 if(len(flinkinfo
['fextralist'])>flinkinfo
['fextrafields'] and len(flinkinfo
['fextralist'])>0):
5930 flinkinfo
['fextrafields'] = len(flinkinfo
['fextralist']);
5931 if(len(extradata
) > 0):
5932 flinkinfo
['fextrafields'] = len(extradata
);
5933 flinkinfo
['fextralist'] = extradata
;
5934 extrafields
= format(int(flinkinfo
['fextrafields']), 'x').lower();
5935 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5936 if(len(extradata
)>0):
5937 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5938 extrasizelen
= format(len(extrasizestr
), 'x').lower();
5939 fcontents
= flinkinfo
['fcontents'];
5940 if(flinkinfo
['ftype']!=0 and flinkinfo
['ftype']!=7):
5942 fcontents
= fcontents
.encode('UTF-8');
5943 except AttributeError:
5945 ftypehex
= format(flinkinfo
['ftype'], 'x').lower();
5947 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=0 and listcatfiles
['ffilelist'][reallcfi
]['ftype']!=7):
5949 fcontents
= fcontents
.encode('UTF-8');
5950 except AttributeError:
5952 ftypehex
= format(listcatfiles
['ffilelist'][reallcfi
]['ftype'], 'x').lower();
5953 fcurfid
= format(curfid
, 'x').lower();
5954 if(not followlink
and finode
!=0):
5955 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=1):
5956 fcurinode
= format(int(curinode
), 'x').lower();
5957 inodetofile
.update({curinode
: fname
});
5958 filetoinode
.update({fname
: curinode
});
5959 curinode
= curinode
+ 1;
5961 fcurinode
= format(int(filetoinode
[flinkname
]), 'x').lower();
5963 fcurinode
= format(int(curinode
), 'x').lower();
5964 curinode
= curinode
+ 1;
5965 curfid
= curfid
+ 1;
5966 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
];
5967 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5968 catoutlenhex
= format(catoutlen
, 'x').lower();
5969 catoutlist
.insert(0, catoutlenhex
);
5970 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5971 if(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']>0):
5972 extrafieldslist
= [];
5974 exil
= listcatfiles
['ffilelist'][reallcfi
]['fextrafields'];
5976 extrafieldslist
.append(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
5978 catfileoutstr
+= AppendNullBytes([extrafieldslist
], formatspecs
[5]);
5979 catfileoutstr
+= AppendNullBytes([checksumtype
], formatspecs
[5]);
5980 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
5981 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
5982 if(checksumtype
=="none" or checksumtype
==""):
5983 catfileheadercshex
= format(0, 'x').lower();
5984 catfilecontentcshex
= format(0, 'x').lower();
5985 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5986 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5987 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
5988 elif(checksumtype
=="crc16_ccitt"):
5989 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5990 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
5991 elif(checksumtype
=="adler32"):
5992 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5993 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
5994 elif(checksumtype
=="crc32"):
5995 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5996 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
5997 elif(checksumtype
=="crc64_ecma"):
5998 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5999 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
6000 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
6001 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6002 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
6003 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
6004 checksumoutstr
= hashlib
.new(checksumtype
);
6005 checksumoutstr
.update("".encode('UTF-8'));
6006 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
6007 checksumoutstr
= hashlib
.new(checksumtype
);
6008 checksumoutstr
.update(fcontents
);
6009 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
6011 catfileheadercshex
= format(0, 'x').lower();
6012 catfilecontentcshex
= format(0, 'x').lower();
6013 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
6014 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
6015 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
6016 if(checksumtype
=="none" or checksumtype
==""):
6017 catfileheadercshex
= format(0, 'x').lower();
6018 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
6019 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
6020 elif(checksumtype
=="crc16_ccitt"):
6021 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
6022 elif(checksumtype
=="adler32"):
6023 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
6024 elif(checksumtype
=="crc32"):
6025 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
6026 elif(checksumtype
=="crc64_ecma"):
6027 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6028 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
6029 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
6030 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
6031 checksumoutstr
= hashlib
.new(checksumtype
);
6032 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
6033 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
6035 catfileheadercshex
= format(0, 'x').lower();
6036 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
6037 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
6038 nullstrecd
= formatspecs
[5].encode('UTF-8');
6039 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
6040 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
6041 catfp
.write(catfileout
);
6044 os
.fsync(catfp
.fileno());
6045 except io
.UnsupportedOperation
:
6047 except AttributeError:
6050 reallcfi
= reallcfi
+ 1;
6051 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
6052 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
6055 os
.fsync(catfp
.fileno());
6056 except io
.UnsupportedOperation
:
6058 except AttributeError:
6062 if(hasattr(sys
.stdout
, "buffer")):
6063 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
6065 shutil
.copyfileobj(catfp
, sys
.stdout
);
6066 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
6067 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
6069 upload_file_to_internet_file(catfp
, outfile
);
6077 create_alias_function("RePack", __file_format_name__
, "", RePackArchiveFile
);
6079 def RePackArchiveFileFromString(catstr
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6080 catfp
= BytesIO(catstr
);
6081 listcatfiles
= RePackArchiveFile(catfp
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
6082 return listcatfiles
;
6084 create_alias_function("RePack", __file_format_name__
, "FromString", RePackArchiveFileFromString
);
6086 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):
6087 outarray
= BytesIO();
6088 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
6089 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
6090 return listcatfiles
;
6092 create_alias_function("Pack", __file_format_name__
, "FromListDir", PackArchiveFileFromListDir
);
6094 def ArchiveFileArrayBase64Encode(infile
, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6096 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6097 if(isinstance(infile
, dict)):
6098 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6099 listcatfiles
= prelistcatfiles
['list'];
6101 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6102 infile
= RemoveWindowsPath(infile
);
6104 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6105 listcatfiles
= prelistcatfiles
['list'];
6107 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6108 if(not listcatfiles
):
6110 lenlist
= len(listcatfiles
['ffilelist']);
6111 fnumfiles
= int(listcatfiles
['fnumfiles']);
6113 lcfx
= int(listcatfiles
['fnumfiles']);
6114 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6115 lcfx
= int(lenlist
);
6117 lcfx
= int(listcatfiles
['fnumfiles']);
6119 if(listcatfiles
['ffilelist'][lcfi
]['fhascontents']):
6120 listcatfiles
['ffilelist'][lcfi
]['fcontents'] = base64
.b64encode(listcatfiles
['ffilelist'][lcfi
]['fcontents']).decode("UTF-8");
6122 return listcatfiles
;
6124 create_alias_function("", __file_format_name__
, "ArrayBase64Encode", ArchiveFileArrayBase64Encode
);
6126 def ArchiveFileArrayBase64Decode(infile
, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6128 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6129 if(isinstance(infile
, dict)):
6130 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6131 listcatfiles
= prelistcatfiles
['list'];
6133 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6134 infile
= RemoveWindowsPath(infile
);
6136 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6137 listcatfiles
= prelistcatfiles
['list'];
6139 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6140 if(not listcatfiles
):
6142 lenlist
= len(listcatfiles
['ffilelist']);
6143 fnumfiles
= int(listcatfiles
['fnumfiles']);
6145 lcfx
= int(listcatfiles
['fnumfiles']);
6146 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6147 lcfx
= int(lenlist
);
6149 lcfx
= int(listcatfiles
['fnumfiles']);
6151 if(listcatfiles
['ffilelist'][lcfi
]['fhascontents']):
6152 listcatfiles
['ffilelist'][lcfi
]['fcontents'] = base64
.b64decode(listcatfiles
['ffilelist'][lcfi
]['fcontents'].encode("UTF-8"));
6154 return listcatfiles
;
6156 create_alias_function("", __file_format_name__
, "ArrayBase64Decode", ArchiveFileArrayBase64Decode
);
6158 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):
6159 if(outdir
is not None):
6160 outdir
= RemoveWindowsPath(outdir
);
6162 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6163 if(isinstance(infile
, dict)):
6164 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6165 listcatfiles
= prelistcatfiles
['list'];
6167 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6168 infile
= RemoveWindowsPath(infile
);
6170 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6171 listcatfiles
= prelistcatfiles
['list'];
6173 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6174 if(not listcatfiles
):
6176 lenlist
= len(listcatfiles
['ffilelist']);
6177 fnumfiles
= int(listcatfiles
['fnumfiles']);
6179 lcfx
= int(listcatfiles
['fnumfiles']);
6180 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6181 lcfx
= int(lenlist
);
6183 lcfx
= int(listcatfiles
['fnumfiles']);
6189 userinfo
= pwd
.getpwuid(listcatfiles
['ffilelist'][lcfi
]['fuid']);
6190 funame
= userinfo
.pw_name
;
6199 groupinfo
= grp
.getgrgid(listcatfiles
['ffilelist'][lcfi
]['fgid']);
6200 fgname
= groupinfo
.gr_name
;
6206 VerbosePrintOut(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6207 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6208 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
6209 fpc
.write(listcatfiles
['ffilelist'][lcfi
]['fcontents'])
6212 os
.fsync(fpc
.fileno())
6213 except io
.UnsupportedOperation
:
6215 except AttributeError:
6217 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
6218 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
6219 if(preservepermissions
):
6220 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6222 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
6223 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6225 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6226 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
6227 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
6232 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
6233 funame
= userinfo
.pw_name
;
6242 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
6243 fgname
= groupinfo
.gr_name
;
6248 if(flinkinfo
['ftype'] == 0 or flinkinfo
['ftype'] == 7):
6249 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
6250 fpc
.write(flinkinfo
['fcontents'])
6253 os
.fsync(fpc
.fileno())
6254 except io
.UnsupportedOperation
:
6256 except AttributeError:
6258 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6259 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6260 if(preservepermissions
):
6261 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6263 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6264 if(flinkinfo
['ftype']==1):
6265 os
.link(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6266 if(flinkinfo
['ftype']==2):
6267 os
.symlink(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6268 if(flinkinfo
['ftype']==5):
6269 if(preservepermissions
):
6270 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6272 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6273 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6274 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6275 if(preservepermissions
):
6276 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6278 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6279 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
6280 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6282 os
.link(listcatfiles
['ffilelist'][lcfi
]['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6283 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6285 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6286 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
6287 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
6292 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
6293 funame
= userinfo
.pw_name
;
6302 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
6303 fgname
= groupinfo
.gr_name
;
6308 if(flinkinfo
['ftype']==0 or flinkinfo
['ftype']==7):
6309 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
6310 fpc
.write(flinkinfo
['fcontents'])
6313 os
.fsync(fpc
.fileno())
6314 except io
.UnsupportedOperation
:
6316 except AttributeError:
6318 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6319 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6320 if(preservepermissions
):
6321 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6323 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6324 if(flinkinfo
['ftype']==1):
6325 os
.link(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6326 if(flinkinfo
['ftype']==2):
6327 os
.symlink(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6328 if(flinkinfo
['ftype']==5):
6329 if(preservepermissions
):
6330 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6332 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6333 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6334 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6335 if(preservepermissions
):
6336 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6338 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6339 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
6340 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6342 os
.symlink(listcatfiles
['ffilelist'][lcfi
]['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6343 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6344 if(preservepermissions
):
6345 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6347 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6348 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
6349 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
6350 if(preservepermissions
):
6351 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6353 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
6354 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6 and hasattr(os
, "mkfifo")):
6355 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6358 return listcatfiles
['ffilelist']['catfp'];
6362 create_alias_function("UnPack", __file_format_name__
, "", UnPackArchiveFile
);
6364 if(hasattr(shutil
, "register_unpack_format")):
6365 def UnPackArchiveFileFunc(archive_name
, extract_dir
=None, **kwargs
):
6366 return UnPackArchiveFile(archive_name
, extract_dir
, False, 0, 0, False, __file_format_delimiter__
, False, False);
6367 create_alias_function("UnPack", __file_format_name__
, "Func", UnPackArchiveFileFunc
);
6369 def UnPackArchiveFileString(catstr
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6370 catfp
= BytesIO(catstr
);
6371 listcatfiles
= UnPackArchiveFile(catfp
, outdir
, followlink
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6372 return listcatfiles
;
6374 create_alias_function("UnPack", __file_format_name__
, "String", UnPackArchiveFileString
);
6376 def ArchiveFileListFiles(infile
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6377 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6378 if(isinstance(infile
, dict)):
6379 listcatfiles
= infile
;
6381 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6382 infile
= RemoveWindowsPath(infile
);
6383 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, True, skipchecksum
, formatspecs
, returnfp
);
6384 if(not listcatfiles
):
6386 lenlist
= len(listcatfiles
['ffilelist']);
6387 fnumfiles
= int(listcatfiles
['fnumfiles']);
6389 lcfx
= int(listcatfiles
['fnumfiles']);
6390 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6391 lcfx
= int(lenlist
);
6393 lcfx
= int(listcatfiles
['fnumfiles']);
6396 returnval
.update({lcfi
: listcatfiles
['ffilelist'][lcfi
]['fname']});
6398 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
6400 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' } };
6401 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'];
6402 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6403 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " link to " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6404 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6405 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " -> " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6406 fuprint
= listcatfiles
['ffilelist'][lcfi
]['funame'];
6407 if(len(fuprint
)<=0):
6408 fuprint
= listcatfiles
['ffilelist'][lcfi
]['fuid'];
6409 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgname'];
6410 if(len(fgprint
)<=0):
6411 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgid'];
6412 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
));
6415 return listcatfiles
['catfp'];
6419 create_alias_function("", __file_format_name__
, "ListFiles", ArchiveFileListFiles
);
6421 def ArchiveFileStringListFiles(catstr
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6422 catfp
= BytesIO(catstr
);
6423 listcatfiles
= ArchiveFileListFiles(catstr
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6424 return listcatfiles
;
6426 create_alias_function("", __file_format_name__
, "StringListFiles", ArchiveFileListFiles
);
6428 def ArchiveFileListFilesAlt(infile
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6429 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6430 if(isinstance(infile
, dict)):
6431 listcatfiles
= infile
;
6433 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6434 infile
= RemoveWindowsPath(infile
);
6435 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, True, skipchecksum
, formatspecs
, returnfp
);
6436 if(not listcatfiles
):
6438 lenlist
= len(listcatfiles
['ffilelist']);
6439 fnumfiles
= int(listcatfiles
['fnumfiles']);
6441 lcfx
= int(listcatfiles
['fnumfiles']);
6442 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6443 lcfx
= int(lenlist
);
6445 lcfx
= int(listcatfiles
['fnumfiles']);
6448 returnval
.update({lcfi
: listcatfiles
['ffilelist'][lcfi
]['fname']});
6450 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
6452 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' } };
6453 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'];
6454 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6455 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " link to " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6456 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6457 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " -> " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6458 fuprint
= listcatfiles
['ffilelist'][lcfi
]['funame'];
6459 if(len(fuprint
)<=0):
6460 fuprint
= listcatfiles
['ffilelist'][lcfi
]['fuid'];
6461 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgname'];
6462 if(len(fgprint
)<=0):
6463 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgid'];
6464 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
));
6467 return listcatfiles
['catfp'];
6471 create_alias_function("", __file_format_name__
, "ListFilesAlt", ArchiveFileListFilesAlt
);
6473 def ArchiveFileStringListFilesAlt(catstr
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6474 catfp
= BytesIO(catstr
);
6475 listcatfiles
= ArchiveFileListFilesAlt(catstr
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6476 return listcatfiles
;
6478 create_alias_function("", __file_format_name__
, "StringListFilesAlt", ArchiveFileListFilesAlt
);
6480 def TarFileListFiles(infile
, verbose
=False, returnfp
=False):
6481 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6484 if(hasattr(sys
.stdin
, "buffer")):
6485 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
6487 shutil
.copyfileobj(sys
.stdin
, infile
);
6492 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
6493 infile
= download_file_from_internet_file(infile
);
6498 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6500 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
6502 if(not tarfile
.is_tarfile(infile
)):
6504 except AttributeError:
6505 if(not is_tarfile(infile
)):
6510 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
6511 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
6513 tarfp
= tarfile
.open(infile
, "r");
6514 except FileNotFoundError
:
6516 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
6517 returnval
.update({lcfi
: member
.name
});
6518 fpremode
= member
.mode
;
6519 ffullmode
= member
.mode
;
6523 ffullmode
= member
.mode
+ stat
.S_IFREG
;
6525 elif(member
.isdev()):
6526 ffullmode
= member
.mode
;
6528 elif(member
.islnk()):
6529 ffullmode
= member
.mode
+ stat
.S_IFREG
;
6531 elif(member
.issym()):
6532 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
6534 elif(member
.ischr()):
6535 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
6537 elif(member
.isblk()):
6538 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
6540 elif(member
.isdir()):
6541 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
6543 elif(member
.isfifo()):
6544 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
6546 elif(member
.issparse()):
6547 ffullmode
= member
.mode
;
6550 VerbosePrintOut(member
.name
);
6552 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' } };
6553 printfname
= member
.name
;
6555 printfname
= member
.name
+ " link to " + member
.linkname
;
6556 elif(member
.issym()):
6557 printfname
= member
.name
+ " -> " + member
.linkname
;
6558 fuprint
= member
.uname
;
6559 if(len(fuprint
)<=0):
6560 fuprint
= member
.uid
;
6561 fgprint
= member
.gname
;
6562 if(len(fgprint
)<=0):
6563 fgprint
= member
.gid
;
6564 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
));
6567 return listcatfiles
['catfp'];
6571 def ZipFileListFiles(infile
, verbose
=False, returnfp
=False):
6572 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6575 if(hasattr(sys
.stdin
, "buffer")):
6576 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
6578 shutil
.copyfileobj(sys
.stdin
, infile
);
6583 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
6584 infile
= download_file_from_internet_file(infile
);
6589 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6593 if(not zipfile
.is_zipfile(infile
)):
6596 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
6597 except FileNotFoundError
:
6601 ziptest
= zipfp
.testzip();
6603 VerbosePrintOut("Bad file found!");
6604 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
6605 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
6606 fwinattributes
= int(zipinfo
.external_attr
);
6607 if(not member
.is_dir()):
6608 fmode
= int(stat
.S_IFREG
+ 438);
6609 fchmode
= int(stat
.S_IMODE(fmode
));
6610 ftypemod
= int(stat
.S_IFMT(fmode
));
6611 elif(member
.is_dir()):
6612 fmode
= int(stat
.S_IFDIR
+ 511);
6613 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
6614 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
6615 elif(zipinfo
.create_system
==3):
6616 fwinattributes
=int(0);
6617 fmode
= int(zipinfo
.external_attr
);
6618 fchmode
= int(stat
.S_IMODE(fmode
));
6619 ftypemod
= int(stat
.S_IFMT(fmode
));
6621 fwinattributes
= int(0);
6622 if(not member
.is_dir()):
6623 fmode
= int(stat
.S_IFREG
+ 438);
6624 fchmode
= int(stat
.S_IMODE(fmode
));
6625 ftypemod
= int(stat
.S_IFMT(fmode
));
6626 elif(member
.is_dir()):
6627 fmode
= int(stat
.S_IFDIR
+ 511);
6628 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
6629 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
6630 returnval
.update({lcfi
: member
.filename
});
6632 VerbosePrintOut(member
.filename
);
6634 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' } };
6636 for fmodval
in str(oct(fmode
))[-3:]:
6637 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
6638 if(not member
.is_dir()):
6640 permissionstr
= "-" + permissionstr
;
6641 elif(member
.is_dir()):
6643 permissionstr
= "d" + permissionstr
;
6644 printfname
= member
.filename
;
6646 fuid
= int(os
.getuid());
6647 except AttributeError:
6652 fgid
= int(os
.getgid());
6653 except AttributeError:
6660 userinfo
= pwd
.getpwuid(os
.getuid());
6661 funame
= userinfo
.pw_name
;
6664 except AttributeError:
6672 groupinfo
= grp
.getgrgid(os
.getgid());
6673 fgname
= groupinfo
.gr_name
;
6676 except AttributeError:
6681 if(len(fuprint
)<=0):
6682 fuprint
= str(fuid
);
6684 if(len(fgprint
)<=0):
6685 fgprint
= str(fgid
);
6686 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
));
6689 return listcatfiles
['catfp'];
6693 if(not rarfile_support
):
6694 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
6695 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6696 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6699 if(rarfile_support
):
6700 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
6701 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6702 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6704 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
6708 rarfp
= rarfile
.RarFile(infile
, "r");
6709 rartest
= rarfp
.testrar();
6711 VerbosePrintOut("Bad file found!");
6712 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
6715 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
6718 member
.external_attr
6720 except AttributeError:
6722 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
6725 member
.external_attr
6727 except AttributeError:
6732 if(is_unix
and member
.external_attr
!=0):
6733 fpremode
= int(member
.external_attr
);
6734 elif(member
.is_file()):
6735 fpremode
= int(stat
.S_IFREG
+ 438);
6736 elif(member
.is_symlink()):
6737 fpremode
= int(stat
.S_IFLNK
+ 438);
6738 elif(member
.is_dir()):
6739 fpremode
= int(stat
.S_IFDIR
+ 511);
6740 if(is_windows
and member
.external_attr
!=0):
6741 fwinattributes
= int(member
.external_attr
);
6743 fwinattributes
= int(0);
6744 if(is_unix
and member
.external_attr
!=0):
6745 fmode
= int(member
.external_attr
);
6746 fchmode
= int(stat
.S_IMODE(member
.external_attr
));
6747 ftypemod
= int(stat
.S_IFMT(member
.external_attr
));
6748 elif(member
.is_file()):
6749 fmode
= int(stat
.S_IFREG
+ 438);
6750 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
6751 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
6752 elif(member
.is_symlink()):
6753 fmode
= int(stat
.S_IFLNK
+ 438);
6754 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFLNK
+ 438)));
6755 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFLNK
+ 438)));
6756 elif(member
.is_dir()):
6757 fmode
= int(stat
.S_IFDIR
+ 511);
6758 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
6759 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
6760 returnval
.update({lcfi
: member
.filename
});
6762 VerbosePrintOut(member
.filename
);
6764 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' } };
6766 for fmodval
in str(oct(fmode
))[-3:]:
6767 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
6768 if(member
.is_file()):
6770 permissionstr
= "-" + permissionstr
;
6771 printfname
= member
.filename
;
6772 elif(member
.is_symlink()):
6774 permissionstr
= "l" + permissionstr
;
6775 printfname
= member
.name
+ " -> " + member
.read().decode("UTF-8");
6776 elif(member
.is_dir()):
6778 permissionstr
= "d" + permissionstr
;
6779 printfname
= member
.filename
;
6781 fuid
= int(os
.getuid());
6782 except AttributeError:
6787 fgid
= int(os
.getgid());
6788 except AttributeError:
6795 userinfo
= pwd
.getpwuid(os
.getuid());
6796 funame
= userinfo
.pw_name
;
6799 except AttributeError:
6807 groupinfo
= grp
.getgrgid(os
.getgid());
6808 fgname
= groupinfo
.gr_name
;
6811 except AttributeError:
6816 if(len(fuprint
)<=0):
6817 fuprint
= str(fuid
);
6819 if(len(fgprint
)<=0):
6820 fgprint
= str(fgid
);
6821 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(member
.file_size
).rjust(15) + " " + member
.mtime
.strftime('%Y-%m-%d %H:%M') + " " + printfname
));
6824 return listcatfiles
['catfp'];
6828 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):
6829 outarray
= BytesIO();
6830 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, False, True);
6831 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6832 return listcatfiles
;
6834 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):
6835 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, formatspecs
, verbose
);
6836 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6837 return listcatfiles
;
6839 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):
6840 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, False, checksumtype
, extradata
, formatspecs
, False);
6841 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, followlink
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
6842 return listcatfiles
;
6844 create_alias_function("Pack", __file_format_name__
, "FromListDirAlt", PackArchiveFileFromListDirAlt
);
6846 def PackArchiveFileFromTarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6847 outarray
= TarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
6848 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
6849 return listcatfiles
;
6851 create_alias_function("Pack", __file_format_name__
, "FromTarFileAlt", PackArchiveFileFromTarFileAlt
);
6853 def PackArchiveFileFromZipFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6854 outarray
= ZipFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
6855 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
6856 return listcatfiles
;
6858 create_alias_function("Pack", __file_format_name__
, "FromZipFileAlt", PackArchiveFileFromZipFileAlt
);
6860 if(not rarfile_support
):
6861 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6864 if(rarfile_support
):
6865 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6866 outarray
= RarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
6867 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
6868 return listcatfiles
;
6870 create_alias_function("Pack", __file_format_name__
, "FromRarFileAlt", PackArchiveFileFromRarFileAlt
);
6872 def download_file_from_ftp_file(url
):
6873 urlparts
= urlparse(url
);
6874 file_name
= os
.path
.basename(urlparts
.path
);
6875 file_dir
= os
.path
.dirname(urlparts
.path
);
6876 if(urlparts
.username
is not None):
6877 ftp_username
= urlparts
.username
;
6879 ftp_username
= "anonymous";
6880 if(urlparts
.password
is not None):
6881 ftp_password
= urlparts
.password
;
6882 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6883 ftp_password
= "anonymous";
6886 if(urlparts
.scheme
=="ftp"):
6888 elif(urlparts
.scheme
=="ftps"):
6892 if(urlparts
.scheme
=="sftp"):
6894 return download_file_from_pysftp_file(url
);
6896 return download_file_from_sftp_file(url
);
6897 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6898 return download_file_from_http_file(url
);
6899 ftp_port
= urlparts
.port
;
6900 if(urlparts
.port
is None):
6903 ftp
.connect(urlparts
.hostname
, ftp_port
);
6904 except socket
.gaierror
:
6905 log
.info("Error With URL "+url
);
6907 except socket
.timeout
:
6908 log
.info("Error With URL "+url
);
6910 ftp
.login(urlparts
.username
, urlparts
.password
);
6911 if(urlparts
.scheme
=="ftps"):
6913 ftpfile
= BytesIO();
6914 ftp
.retrbinary("RETR "+urlparts
.path
, ftpfile
.write
);
6915 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
6920 def download_file_from_ftp_string(url
):
6921 ftpfile
= download_file_from_ftp_file(url
);
6922 return ftpfile
.read();
6924 def upload_file_to_ftp_file(ftpfile
, url
):
6925 urlparts
= urlparse(url
);
6926 file_name
= os
.path
.basename(urlparts
.path
);
6927 file_dir
= os
.path
.dirname(urlparts
.path
);
6928 if(urlparts
.username
is not None):
6929 ftp_username
= urlparts
.username
;
6931 ftp_username
= "anonymous";
6932 if(urlparts
.password
is not None):
6933 ftp_password
= urlparts
.password
;
6934 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6935 ftp_password
= "anonymous";
6938 if(urlparts
.scheme
=="ftp"):
6940 elif(urlparts
.scheme
=="ftps"):
6944 if(urlparts
.scheme
=="sftp"):
6946 return upload_file_from_pysftp_file(url
);
6948 return upload_file_from_sftp_file(url
);
6949 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6951 ftp_port
= urlparts
.port
;
6952 if(urlparts
.port
is None):
6955 ftp
.connect(urlparts
.hostname
, ftp_port
);
6956 except socket
.gaierror
:
6957 log
.info("Error With URL "+url
);
6959 except socket
.timeout
:
6960 log
.info("Error With URL "+url
);
6962 ftp
.login(urlparts
.username
, urlparts
.password
);
6963 if(urlparts
.scheme
=="ftps"):
6965 ftp
.storbinary("STOR "+urlparts
.path
, ftpfile
);
6970 def upload_file_to_ftp_string(ftpstring
, url
):
6971 ftpfileo
= BytesIO(ftpstring
);
6972 ftpfile
= upload_file_to_ftp_file(ftpfileo
, url
);
6976 def download_file_from_http_file(url
, headers
=geturls_headers_pycatfile_python_alt
):
6977 # Parse the URL to extract username and password if present
6978 urlparts
= urlparse(url
);
6979 username
= urlparts
.username
;
6980 password
= urlparts
.password
;
6981 # Rebuild the URL without the username and password
6982 netloc
= urlparts
.hostname
;
6983 if(urlparts
.scheme
=="sftp"):
6985 return download_file_from_pysftp_file(url
);
6987 return download_file_from_sftp_file(url
);
6988 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
6989 return download_file_from_ftp_file(url
);
6991 netloc
+= ':' + str(urlparts
.port
);
6992 rebuilt_url
= urlunparse((urlparts
.scheme
, netloc
, urlparts
.path
, urlparts
.params
, urlparts
.query
, urlparts
.fragment
));
6993 # Create a temporary file object
6994 httpfile
= BytesIO();
6996 # Use the requests library if available
6997 if username
and password
:
6998 response
= requests
.get(rebuilt_url
, headers
=headers
, auth
=(username
, password
), stream
=True);
7000 response
= requests
.get(rebuilt_url
, headers
=headers
, stream
=True);
7001 response
.raw
.decode_content
= True
7002 shutil
.copyfileobj(response
.raw
, httpfile
);
7004 # Build a Request object for urllib
7005 request
= Request(rebuilt_url
, headers
=headers
);
7006 # Create an opener object for handling URLs
7007 if username
and password
:
7008 # Create a password manager
7009 password_mgr
= HTTPPasswordMgrWithDefaultRealm();
7010 # Add the username and password
7011 password_mgr
.add_password(None, rebuilt_url
, username
, password
);
7012 # Create an authentication handler using the password manager
7013 auth_handler
= HTTPBasicAuthHandler(password_mgr
);
7014 # Build the opener with the authentication handler
7015 opener
= build_opener(auth_handler
);
7017 opener
= build_opener();
7018 with opener
.open(request
) as response
:
7019 shutil
.copyfileobj(response
, httpfile
);
7020 # Reset file pointer to the start
7021 httpfile
.seek(0, 0);
7022 # Return the temporary file object
7025 def download_file_from_http_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
7026 httpfile
= download_file_from_http_file(url
, headers
);
7027 return ftpfile
.read();
7030 def download_file_from_sftp_file(url
):
7031 urlparts
= urlparse(url
);
7032 file_name
= os
.path
.basename(urlparts
.path
);
7033 file_dir
= os
.path
.dirname(urlparts
.path
);
7034 sftp_port
= urlparts
.port
;
7035 if(urlparts
.port
is None):
7038 sftp_port
= urlparts
.port
;
7039 if(urlparts
.username
is not None):
7040 sftp_username
= urlparts
.username
;
7042 sftp_username
= "anonymous";
7043 if(urlparts
.password
is not None):
7044 sftp_password
= urlparts
.password
;
7045 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7046 sftp_password
= "anonymous";
7049 if(urlparts
.scheme
=="ftp"):
7050 return download_file_from_ftp_file(url
);
7051 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7052 return download_file_from_http_file(url
);
7053 if(urlparts
.scheme
!="sftp"):
7055 ssh
= paramiko
.SSHClient();
7056 ssh
.load_system_host_keys();
7057 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
7059 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7060 except paramiko
.ssh_exception
.SSHException
:
7062 except socket
.gaierror
:
7063 log
.info("Error With URL "+url
);
7065 except socket
.timeout
:
7066 log
.info("Error With URL "+url
);
7068 sftp
= ssh
.open_sftp();
7069 sftpfile
= BytesIO();
7070 sftp
.getfo(urlparts
.path
, sftpfile
);
7073 sftpfile
.seek(0, 0);
7076 def download_file_from_sftp_file(url
):
7080 def download_file_from_sftp_string(url
):
7081 sftpfile
= download_file_from_sftp_file(url
);
7082 return sftpfile
.read();
7084 def download_file_from_ftp_string(url
):
7088 def upload_file_to_sftp_file(sftpfile
, url
):
7089 urlparts
= urlparse(url
);
7090 file_name
= os
.path
.basename(urlparts
.path
);
7091 file_dir
= os
.path
.dirname(urlparts
.path
);
7092 sftp_port
= urlparts
.port
;
7093 if(urlparts
.port
is None):
7096 sftp_port
= urlparts
.port
;
7097 if(urlparts
.username
is not None):
7098 sftp_username
= urlparts
.username
;
7100 sftp_username
= "anonymous";
7101 if(urlparts
.password
is not None):
7102 sftp_password
= urlparts
.password
;
7103 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7104 sftp_password
= "anonymous";
7107 if(urlparts
.scheme
=="ftp"):
7108 return upload_file_from_ftp_file(url
);
7109 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7111 if(urlparts
.scheme
!="sftp"):
7113 ssh
= paramiko
.SSHClient();
7114 ssh
.load_system_host_keys();
7115 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
7117 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7118 except paramiko
.ssh_exception
.SSHException
:
7120 except socket
.gaierror
:
7121 log
.info("Error With URL "+url
);
7123 except socket
.timeout
:
7124 log
.info("Error With URL "+url
);
7126 sftp
= ssh
.open_sftp();
7127 sftp
.putfo(sftpfile
, urlparts
.path
);
7130 sftpfile
.seek(0, 0);
7133 def upload_file_to_sftp_file(sftpfile
, url
):
7137 def upload_file_to_sftp_string(sftpstring
, url
):
7138 sftpfileo
= BytesIO(sftpstring
);
7139 sftpfile
= upload_file_to_sftp_files(ftpfileo
, url
);
7143 def upload_file_to_sftp_string(url
):
7147 def download_file_from_pysftp_file(url
):
7148 urlparts
= urlparse(url
);
7149 file_name
= os
.path
.basename(urlparts
.path
);
7150 file_dir
= os
.path
.dirname(urlparts
.path
);
7151 sftp_port
= urlparts
.port
;
7152 if(urlparts
.port
is None):
7155 sftp_port
= urlparts
.port
;
7156 if(urlparts
.username
is not None):
7157 sftp_username
= urlparts
.username
;
7159 sftp_username
= "anonymous";
7160 if(urlparts
.password
is not None):
7161 sftp_password
= urlparts
.password
;
7162 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7163 sftp_password
= "anonymous";
7166 if(urlparts
.scheme
=="ftp"):
7167 return download_file_from_ftp_file(url
);
7168 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7169 return download_file_from_http_file(url
);
7170 if(urlparts
.scheme
!="sftp"):
7173 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7174 except paramiko
.ssh_exception
.SSHException
:
7176 except socket
.gaierror
:
7177 log
.info("Error With URL "+url
);
7179 except socket
.timeout
:
7180 log
.info("Error With URL "+url
);
7182 sftp
= ssh
.open_sftp();
7183 sftpfile
= BytesIO();
7184 sftp
.getfo(urlparts
.path
, sftpfile
);
7187 sftpfile
.seek(0, 0);
7190 def download_file_from_pysftp_file(url
):
7194 def download_file_from_pysftp_string(url
):
7195 sftpfile
= download_file_from_pysftp_file(url
);
7196 return sftpfile
.read();
7198 def download_file_from_ftp_string(url
):
7202 def upload_file_to_pysftp_file(sftpfile
, url
):
7203 urlparts
= urlparse(url
);
7204 file_name
= os
.path
.basename(urlparts
.path
);
7205 file_dir
= os
.path
.dirname(urlparts
.path
);
7206 sftp_port
= urlparts
.port
;
7207 if(urlparts
.port
is None):
7210 sftp_port
= urlparts
.port
;
7211 if(urlparts
.username
is not None):
7212 sftp_username
= urlparts
.username
;
7214 sftp_username
= "anonymous";
7215 if(urlparts
.password
is not None):
7216 sftp_password
= urlparts
.password
;
7217 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7218 sftp_password
= "anonymous";
7221 if(urlparts
.scheme
=="ftp"):
7222 return upload_file_from_ftp_file(url
);
7223 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7225 if(urlparts
.scheme
!="sftp"):
7228 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7229 except paramiko
.ssh_exception
.SSHException
:
7231 except socket
.gaierror
:
7232 log
.info("Error With URL "+url
);
7234 except socket
.timeout
:
7235 log
.info("Error With URL "+url
);
7237 sftp
= ssh
.open_sftp();
7238 sftp
.putfo(sftpfile
, urlparts
.path
);
7241 sftpfile
.seek(0, 0);
7244 def upload_file_to_pysftp_file(sftpfile
, url
):
7248 def upload_file_to_pysftp_string(sftpstring
, url
):
7249 sftpfileo
= BytesIO(sftpstring
);
7250 sftpfile
= upload_file_to_pysftp_files(ftpfileo
, url
);
7254 def upload_file_to_pysftp_string(url
):
7257 def download_file_from_internet_file(url
, headers
=geturls_headers_pycatfile_python_alt
):
7258 urlparts
= urlparse(url
);
7259 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7260 return download_file_from_http_file(url
, headers
);
7261 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7262 return download_file_from_ftp_file(url
);
7263 elif(urlparts
.scheme
=="sftp"):
7264 if(__use_pysftp__
and havepysftp
):
7265 return download_file_from_pysftp_file(url
);
7267 return download_file_from_sftp_file(url
);
7272 def download_file_from_internet_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
7273 urlparts
= urlparse(url
);
7274 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7275 return download_file_from_http_string(url
, headers
);
7276 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7277 return download_file_from_ftp_string(url
);
7278 elif(urlparts
.scheme
=="sftp"):
7279 if(__use_pysftp__
and havepysftp
):
7280 return download_file_from_pysftp_string(url
);
7282 return download_file_from_sftp_string(url
);
7287 def upload_file_to_internet_file(ifp
, url
):
7288 urlparts
= urlparse(url
);
7289 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7291 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7292 return upload_file_from_ftp_file(ifp
, url
);
7293 elif(urlparts
.scheme
=="sftp"):
7294 if(__use_pysftp__
and havepysftp
):
7295 return upload_file_from_pysftp_file(ifp
, url
);
7297 return download_file_from_sftp_file(ifp
, url
);
7302 def upload_file_to_internet_string(ifp
, url
):
7303 urlparts
= urlparse(url
);
7304 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7306 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7307 return upload_file_from_ftp_string(ifp
, url
);
7308 elif(urlparts
.scheme
=="sftp"):
7309 if(__use_pysftp__
and havepysftp
):
7310 return upload_file_from_pysftp_string(ifp
, url
);
7312 return download_file_from_sftp_string(ifp
, url
);
7318 if(hasattr(shutil
, "register_archive_format")):
7319 # Register the packing format
7320 shutil
.register_archive_format(__file_format_name__
, PackArchiveFileFunc
, description
='Pack concatenated files');
7321 except shutil
.RegistryError
:
7325 if(hasattr(shutil
, "register_unpack_format")):
7326 # Register the unpacking format
7327 shutil
.register_unpack_format(__file_format_name__
, archivefile_extensions
, UnPackArchiveFileFunc
, description
='UnPack concatenated files');
7328 except shutil
.RegistryError
: