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/15/2024 Ver. 0.5.0 RC 1 - Author: cooldude2k $
20 from __future__
import absolute_import
, division
, print_function
, unicode_literals
;
21 import io
, os
, re
, sys
, time
, stat
, zlib
, base64
, shutil
, socket
, hashlib
, datetime
, logging
, binascii
, tempfile
, zipfile
, platform
;
22 from ftplib
import FTP
, FTP_TLS
;
23 if(sys
.version
[0]=="2"):
24 from urlparse
import urlparse
, urlunparse
;
25 elif(sys
.version
[0]>="3"):
26 from urllib
.parse
import urlunparse
;
27 from urllib
.parse
import urlparse
;
29 if os
.name
== 'nt': # Only modify if on Windows
30 if sys
.version
[0] == "2":
32 sys
.stdout
= codecs
.getwriter('utf-8')(sys
.stdout
);
33 sys
.stderr
= codecs
.getwriter('utf-8')(sys
.stderr
);
36 sys
.stdout
= io
.TextIOWrapper(sys
.stdout
.buffer, encoding
='utf-8', errors
='replace', line_buffering
=True);
37 sys
.stderr
= io
.TextIOWrapper(sys
.stderr
.buffer, encoding
='utf-8', errors
='replace', line_buffering
=True);
39 hashlib_guaranteed
= False;
40 os
.environ
["PYTHONIOENCODING"] = "UTF-8";
41 os
.environ
["LC_CTYPE"] = "UTF-8";
45 sys
.setdefaultencoding('UTF-8');
48 except AttributeError:
52 except AttributeError:
56 from zlib
import crc32
;
58 from binascii
import crc32
;
60 rarfile_support
= False;
63 rarfile_support
= True;
65 rarfile_support
= False;
68 from safetar
import is_tarfile
;
71 from xtarfile
import is_tarfile
;
73 from tarfile
import is_tarfile
;
76 import safetar
as tarfile
;
79 import xtarfile
as tarfile
;
102 haverequests
= False;
106 logging
.getLogger("urllib3").setLevel(logging
.WARNING
);
110 from urllib
.request
import Request
, build_opener
, HTTPBasicAuthHandler
;
111 from urllib
.parse
import urlparse
;
114 from urllib2
import Request
, build_opener
, HTTPBasicAuthHandler
;
115 from urlparse
import urlparse
;
117 if(sys
.version
[0]=="2"):
119 from io
import StringIO
, BytesIO
;
122 from cStringIO
import StringIO
;
123 from cStringIO
import StringIO
as BytesIO
;
125 from StringIO
import StringIO
;
126 from StringIO
import StringIO
as BytesIO
;
127 elif(sys
.version
[0]>="3"):
128 from io
import StringIO
, BytesIO
;
133 from cStringIO
import StringIO
as BytesIO
;
139 from StringIO
import StringIO
as BytesIO
;
145 from io
import BytesIO
;
150 __use_pysftp__
= False;
152 __use_pysftp__
= False;
153 __file_format_name__
= "CatFile";
154 __program_name__
= "Py"+__file_format_name__
;
155 __file_format_lower__
= __file_format_name__
.lower();
156 __file_format_magic__
= __file_format_name__
;
157 __file_format_len__
= len(__file_format_magic__
);
158 __file_format_hex__
= binascii
.hexlify(__file_format_magic__
.encode("UTF-8")).decode("UTF-8");
159 __file_format_delimiter__
= "\x00";
160 __file_format_ver__
= "001";
161 __use_new_style__
= True;
162 __file_format_list__
= [__file_format_name__
, __file_format_magic__
, __file_format_lower__
, __file_format_len__
, __file_format_hex__
, __file_format_delimiter__
, __file_format_ver__
, __use_new_style__
];
163 __project__
= __program_name__
;
164 __project_url__
= "https://github.com/GameMaker2k/PyCatFile";
165 __version_info__
= (0, 6, 0, "RC 1", 1);
166 __version_date_info__
= (2024, 3, 21, "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 ListDir(dirpath
, followlink
=False, duplicates
=False):
283 if(isinstance(dirpath
, (list, tuple, ))):
284 dirpath
= list(filter(None, dirpath
));
285 elif(isinstance(dirpath
, (str, ))):
286 dirpath
= list(filter(None, [dirpath
]));
288 for mydirfile
in dirpath
:
289 if(not os
.path
.exists(mydirfile
)):
291 mydirfile
= NormalizeRelativePath(mydirfile
);
292 if(os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
)):
293 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
));
294 if(os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
)):
295 for root
, dirs
, filenames
in os
.walk(mydirfile
):
297 dpath
= RemoveWindowsPath(dpath
);
298 if(dpath
not in retlist
and not duplicates
):
299 retlist
.append(dpath
);
301 retlist
.append(dpath
);
302 for file in filenames
:
303 fpath
= os
.path
.join(root
, file);
304 fpath
= RemoveWindowsPath(fpath
);
305 if(fpath
not in retlist
and not duplicates
):
306 retlist
.append(fpath
);
308 retlist
.append(fpath
);
310 retlist
.append(RemoveWindowsPath(mydirfile
));
313 def ListDirAdvanced(dirpath
, followlink
=False, duplicates
=False):
314 if isinstance(dirpath
, (list, tuple)):
315 dirpath
= list(filter(None, dirpath
));
316 elif isinstance(dirpath
, str):
317 dirpath
= list(filter(None, [dirpath
]));
319 for mydirfile
in dirpath
:
320 if not os
.path
.exists(mydirfile
):
322 mydirfile
= NormalizeRelativePath(mydirfile
);
323 if os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
) and followlink
:
324 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
))
325 if os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
):
326 for root
, dirs
, filenames
in os
.walk(mydirfile
):
327 # Sort dirs and filenames alphabetically in place
328 dirs
.sort(key
=lambda x
: x
.lower());
329 filenames
.sort(key
=lambda x
: x
.lower());
330 dpath
= RemoveWindowsPath(root
);
331 if not duplicates
and dpath
not in retlist
:
332 retlist
.append(dpath
);
334 retlist
.append(dpath
);
335 for file in filenames
:
336 fpath
= os
.path
.join(root
, file);
337 fpath
= RemoveWindowsPath(fpath
);
338 if not duplicates
and fpath
not in retlist
:
339 retlist
.append(fpath
);
341 retlist
.append(fpath
);
343 retlist
.append(RemoveWindowsPath(mydirfile
));
346 def create_alias_function(prefix
, base_name
, suffix
, target_function
):
347 # Define a new function that wraps the target function
348 def alias_function(*args
, **kwargs
):
349 return target_function(*args
, **kwargs
);
351 # Create the function name by combining the prefix, base name, and the suffix
352 function_name
= "{}{}{}".format(prefix
, base_name
, suffix
);
354 # Add the new function to the global namespace
355 globals()[function_name
] = alias_function
;
357 # initial_value can be 0xFFFF or 0x0000
358 def crc16_ansi(msg
, initial_value
=0xFFFF):
359 # CRC-16-IBM / CRC-16-ANSI polynomial and initial value
360 poly
= 0x8005; # Polynomial for CRC-16-IBM / CRC-16-ANSI
361 crc
= initial_value
; # Initial value
363 crc ^
= b
<< 8; # XOR byte into CRC top byte
364 for _
in range(8): # Process each bit
365 if crc
& 0x8000: # If the top bit is set
366 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
368 crc
= crc
<< 1; # Just shift left
369 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
372 # initial_value can be 0xFFFF or 0x0000
373 def crc16_ibm(msg
, initial_value
=0xFFFF):
374 return crc16_ansi(msg
, initial_value
);
376 # initial_value is 0xFFFF
378 return crc16_ansi(msg
, 0xFFFF);
380 # initial_value can be 0xFFFF, 0x1D0F or 0x0000
381 def crc16_ccitt(msg
, initial_value
=0xFFFF):
382 # CRC-16-CCITT polynomial
383 poly
= 0x1021; # Polynomial for CRC-16-CCITT
384 # Use the specified initial value
387 crc ^
= b
<< 8; # XOR byte into CRC top byte
388 for _
in range(8): # Process each bit
389 if crc
& 0x8000: # If the top bit is set
390 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
392 crc
= crc
<< 1; # Just shift left
393 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
396 # initial_value can be 0x42F0E1EBA9EA3693 or 0x0000000000000000
397 def crc64_ecma(msg
, initial_value
=0x0000000000000000):
398 # CRC-64-ECMA polynomial and initial value
399 poly
= 0x42F0E1EBA9EA3693;
400 crc
= initial_value
; # Initial value for CRC-64-ECMA
402 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
403 for _
in range(8): # Process each bit
404 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
405 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
407 crc
<<= 1; # Just shift left if the MSB is 0
408 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
411 # initial_value can be 0x000000000000001B or 0xFFFFFFFFFFFFFFFF
412 def crc64_iso(msg
, initial_value
=0xFFFFFFFFFFFFFFFF):
413 # CRC-64-ISO polynomial and initial value
414 poly
= 0x000000000000001B;
415 crc
= initial_value
; # Common initial value for CRC-64-ISO
417 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
418 for _
in range(8): # Process each bit
419 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
420 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
422 crc
<<= 1; # Just shift left if the MSB is 0
423 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
426 def GetDataFromArray(data
, path
, default
=None):
430 element
= element
[key
];
432 except (KeyError, TypeError, IndexError):
435 def GetDataFromArrayAlt(structure
, path
, default
=None):
438 if isinstance(element
, dict) and key
in element
:
439 element
= element
[key
];
440 elif isinstance(element
, list) and isinstance(key
, int) and -len(element
) <= key
< len(element
):
441 element
= element
[key
];
446 def GetHeaderChecksum(inlist
=[], checksumtype
="crc32", encodedata
=True, formatspecs
=__file_format_list__
):
447 if isinstance(inlist
, list):
448 fileheader
= AppendNullBytes(inlist
, formatspecs
[5]);
450 fileheader
= AppendNullByte(inlist
, formatspecs
[5]);
452 fileheader
= fileheader
.encode('UTF-8');
453 if(checksumtype
=="none" or checksumtype
==""):
454 catfileheadercshex
= format(0, 'x').lower();
455 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
456 catfileheadercshex
= format(crc16(fileheader
) & 0xffff, '04x').lower();
457 elif(checksumtype
=="crc16_ccitt"):
458 catfileheadercshex
= format(crc16_ccitt(fileheader
) & 0xffff, '04x').lower();
459 elif(checksumtype
=="adler32"):
460 catfileheadercshex
= format(zlib
.adler32(fileheader
) & 0xffffffff, '08x').lower();
461 elif(checksumtype
=="crc32"):
462 catfileheadercshex
= format(crc32(fileheader
) & 0xffffffff, '08x').lower();
463 elif(checksumtype
=="crc64_ecma"):
464 catfileheadercshex
= format(crc64_ecma(fileheader
) & 0xffffffffffffffff, '016x').lower();
465 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
466 catfileheadercshex
= format(crc64_iso(fileheader
) & 0xffffffffffffffff, '016x').lower();
467 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
468 checksumoutstr
= hashlib
.new(checksumtype
);
469 checksumoutstr
.update(fileheader
);
470 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
472 catfileheadercshex
= format(0, 'x').lower();
473 return catfileheadercshex
;
475 def GetFileChecksum(infile
, checksumtype
="crc32", encodedata
=True, formatspecs
=__file_format_list__
):
477 infile
= infile
.encode('UTF-8');
478 if(checksumtype
=="none" or checksumtype
==""):
479 catinfilecshex
= format(0, 'x').lower();
480 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
481 catinfilecshex
= format(crc16(infile
) & 0xffff, '04x').lower();
482 elif(checksumtype
=="crc16_ccitt"):
483 catinfilecshex
= format(crc16_ccitt(infile
) & 0xffff, '04x').lower();
484 elif(checksumtype
=="adler32"):
485 catinfilecshex
= format(zlib
.adler32(infile
) & 0xffffffff, '08x').lower();
486 elif(checksumtype
=="crc32"):
487 catinfilecshex
= format(crc32(infile
) & 0xffffffff, '08x').lower();
488 elif(checksumtype
=="crc64_ecma"):
489 catinfilecshex
= format(crc64_ecma(infile
) & 0xffffffffffffffff, '016x').lower();
490 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
491 catinfilecshex
= format(crc64_iso(infile
) & 0xffffffffffffffff, '016x').lower();
492 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
493 checksumoutstr
= hashlib
.new(checksumtype
);
494 checksumoutstr
.update(infile
);
495 catinfilecshex
= checksumoutstr
.hexdigest().lower();
497 catinfilecshex
= format(0, 'x').lower();
498 return catinfilecshex
;
500 def ValidateHeaderChecksum(inlist
=[], checksumtype
="crc32", inchecksum
="0", formatspecs
=__file_format_list__
):
501 catfileheadercshex
= GetHeaderChecksum(inlist
, checksumtype
, True, formatspecs
);
502 inchecksum
= inchecksum
.lower();
503 catfileheadercshex
= catfileheadercshex
.lower();
504 if(inchecksum
==catfileheadercshex
):
509 def ValidateFileChecksum(infile
, checksumtype
="crc32", inchecksum
="0", formatspecs
=__file_format_list__
):
510 catinfilecshex
= GetFileChecksum(infile
, checksumtype
, True, formatspecs
);
511 inchecksum
= inchecksum
.lower();
512 catinfilecshex
= catinfilecshex
.lower();
513 if(inchecksum
==catinfilecshex
):
518 def ReadTillNullByte(fp
, delimiter
=__file_format_delimiter__
):
521 nullbyte
= delimiter
.encode("UTF-8");
523 curbyte
= fp
.read(1);
524 if(curbyte
==nullbyte
or not curbyte
):
526 curfullbyte
= curfullbyte
+ curbyte
;
527 return curfullbyte
.decode('UTF-8');
529 def ReadUntilNullByte(fp
, delimiter
=__file_format_delimiter__
):
530 return ReadTillNullByte(fp
, delimiter
);
532 def SeekToEndOfFile(fp
):
536 if(lasttell
==fp
.tell()):
538 lasttell
= fp
.tell();
541 def ReadFileHeaderData(fp
, rounds
=0, delimiter
=__file_format_delimiter__
):
545 while(rocount
<roend
):
546 HeaderOut
.append(ReadTillNullByte(fp
, delimiter
));
547 rocount
= rocount
+ 1;
550 def ReadFileHeaderDataBySize(fp
, delimiter
=__file_format_delimiter__
):
551 headerpresize
= ReadTillNullByte(fp
, delimiter
);
552 headersize
= int(headerpresize
, 16);
553 headercontent
= str(fp
.read(headersize
).decode('UTF-8')).split(delimiter
);
556 roend
= int(len(headercontent
));
557 HeaderOut
= [headerpresize
];
558 while(rocount
<roend
):
559 HeaderOut
.append(headercontent
[rocount
]);
560 rocount
= rocount
+ 1;
563 def ReadFileHeaderDataBySizeWithContent(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
564 delimiter
= formatspecs
[5];
565 fheaderstart
= fp
.tell();
566 HeaderOut
= ReadFileHeaderDataBySize(fp
, delimiter
);
567 if(re
.findall("^[.|/]", HeaderOut
[2])):
568 fname
= HeaderOut
[2];
570 fname
= "./"+HeaderOut
[2];
571 fchecksumtype
= HeaderOut
[-3].lower();
572 fcs
= HeaderOut
[-2].lower();
573 fccs
= HeaderOut
[-1].lower();
574 fsize
= int(HeaderOut
[4], 16);
575 newfcs
= GetHeaderChecksum(HeaderOut
[:-2], HeaderOut
[-3].lower(), True, formatspecs
);
576 if(fcs
!=newfcs
and not skipchecksum
):
577 VerbosePrintOut("File Header Checksum Error with file " + fname
+ " at offset " + str(fheaderstart
));
579 fhend
= fp
.tell() - 1;
580 fcontentstart
= fp
.tell();
581 fcontents
= "".encode('UTF-8');
582 if(fsize
>0 and not listonly
):
583 fcontents
= fp
.read(fsize
);
584 elif(fsize
>0 and listonly
):
586 newfccs
= GetFileChecksum(fcontents
, HeaderOut
[-3].lower(), False, formatspecs
);
587 if(fccs
!=newfccs
and not skipchecksum
and not listonly
):
588 VerbosePrintOut("File Content Checksum Error with file " + fname
+ " at offset " + str(fcontentstart
));
591 fcontentend
= fp
.tell() - 1;
592 HeaderOut
.append(fcontents
);
595 def ReadFileHeaderDataBySizeWithContentToArray(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
596 delimiter
= formatspecs
[5];
597 fheaderstart
= fp
.tell();
598 HeaderOut
= ReadFileHeaderDataBySize(fp
, delimiter
);
599 fheadsize
= int(HeaderOut
[0], 16);
600 ftype
= int(HeaderOut
[1], 16);
601 if(re
.findall("^[.|/]", HeaderOut
[2])):
602 fname
= HeaderOut
[2];
604 fname
= "./"+HeaderOut
[2];
605 fbasedir
= os
.path
.dirname(fname
);
606 flinkname
= HeaderOut
[3];
607 fsize
= int(HeaderOut
[4], 16);
608 fatime
= int(HeaderOut
[5], 16);
609 fmtime
= int(HeaderOut
[6], 16);
610 fctime
= int(HeaderOut
[7], 16);
611 fbtime
= int(HeaderOut
[8], 16);
612 fmode
= int(HeaderOut
[9], 16);
613 fchmode
= stat
.S_IMODE(fmode
);
614 ftypemod
= stat
.S_IFMT(fmode
);
615 fuid
= int(HeaderOut
[10], 16);
616 funame
= HeaderOut
[11];
617 fgid
= int(HeaderOut
[12], 16);
618 fgname
= HeaderOut
[13];
619 fid
= int(HeaderOut
[14], 16);
620 finode
= int(HeaderOut
[15], 16);
621 flinkcount
= int(HeaderOut
[16], 16);
622 fdev_minor
= int(HeaderOut
[17], 16);
623 fdev_major
= int(HeaderOut
[18], 16);
624 frdev_minor
= int(HeaderOut
[19], 16);
625 frdev_major
= int(HeaderOut
[20], 16);
626 fextrasize
= int(HeaderOut
[21], 16);
627 fextrafields
= int(HeaderOut
[22], 16);
628 extrafieldslist
= [];
630 extraend
= extrastart
+ fextrafields
;
631 extrafieldslist
= [];
632 if(extrastart
<extraend
):
633 extrafieldslist
.append(HeaderOut
[extrastart
]);
634 extrastart
= extrastart
+ 1;
635 fnumfields
= 24 + fextrafields
;
636 fchecksumtype
= HeaderOut
[extrastart
].lower();
637 fcs
= HeaderOut
[extrastart
+ 1].lower();
638 fccs
= HeaderOut
[extrastart
+ 2].lower();
639 newfcs
= GetHeaderChecksum(HeaderOut
[:-2], HeaderOut
[-3].lower(), True, formatspecs
);
640 if(fcs
!=newfcs
and not skipchecksum
):
641 VerbosePrintOut("File Header Checksum Error with file " + fname
+ " at offset " + str(fheaderstart
));
643 fhend
= fp
.tell() - 1;
644 fcontentstart
= fp
.tell();
645 fcontents
= "".encode('UTF-8');
646 pyhascontents
= False;
647 if(fsize
>0 and not listonly
):
648 fcontents
= fp
.read(fsize
);
649 pyhascontents
= True;
650 elif(fsize
>0 and listonly
):
652 pyhascontents
= False;
653 newfccs
= GetFileChecksum(fcontents
, HeaderOut
[-3].lower(), False, formatspecs
);
654 if(fccs
!=newfccs
and not skipchecksum
and not listonly
):
655 VerbosePrintOut("File Content Checksum Error with file " + fname
+ " at offset " + str(fcontentstart
));
658 fcontentend
= fp
.tell() - 1;
659 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
, '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
, 'fextrafields': fextrafields
, 'fextrafieldsize': fextrasize
, 'fextralist': extrafieldslist
, 'fheaderchecksum': fcs
, 'fcontentchecksum': fccs
, 'fhascontents': pyhascontents
, 'fcontentstart': fcontentstart
, 'fcontentend': fcontentend
, 'fcontents': fcontents
}
662 def ReadFileDataBySizeWithContent(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
663 delimiter
= formatspecs
[5];
664 catheader
= ReadFileHeaderData(fp
, 4, delimiter
);
665 headercheck
= ValidateHeaderChecksum(catheader
[:-1], catheader
[2], catheader
[3], formatspecs
);
666 if(not headercheck
and not skipchecksum
):
667 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
669 fnumfiles
= int(catheader
[1], 16);
672 while(countnum
< fnumfiles
):
673 flist
.append(ReadFileHeaderDataBySizeWithContent(fp
, listonly
, skipchecksum
, formatspecs
));
674 countnum
= countnum
+ 1;
677 def ReadFileDataBySizeWithContentToArray(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
678 delimiter
= formatspecs
[5];
679 catheader
= ReadFileHeaderData(fp
, 4, delimiter
);
680 headercheck
= ValidateHeaderChecksum(catheader
[:-1], catheader
[2], catheader
[3], formatspecs
);
681 if(not headercheck
and not skipchecksum
):
682 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
684 catstring
= catheader
[0];
685 catversion
= re
.findall(r
"([\d]+)$", catstring
);
686 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
687 fprenumfiles
= catheader
[1];
688 fnumfiles
= int(fprenumfiles
, 16);
689 fprechecksumtype
= catheader
[2];
690 fprechecksum
= catheader
[3];
691 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
693 while(countnum
< fnumfiles
):
694 catlist
['ffilelist'].update({countnum
: {'fid': countnum
, 'fidalt': countnum
}});
695 catlist
['ffilelist'][countnum
].update(ReadFileHeaderDataBySizeWithContentToArray(fp
, listonly
, skipchecksum
, formatspecs
));
696 countnum
= countnum
+ 1;
699 def ReadFileDataBySizeWithContentToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
700 if(isinstance(infile
, dict)):
701 listcatfiles
= infile
;
703 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
705 listcatfiles
= ReadFileDataBySizeWithContentToArray(infile
, listonly
, skipchecksum
, formatspecs
);
706 if(not listcatfiles
):
708 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': {}}}};
709 lenlist
= len(listcatfiles
['ffilelist']);
714 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
717 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
718 lcfx
= listcatfiles
['fnumfiles'];
720 lcfx
= int(listcatfiles
['fnumfiles']);
722 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
723 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
724 catarray
['filetoid'].update(filetoidarray
);
725 catarray
['idtofile'].update(idtofilearray
);
726 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
727 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
728 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
729 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
730 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
731 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
732 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
733 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
734 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
735 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
736 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
737 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
738 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
739 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
740 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
741 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
742 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
743 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
744 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
745 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
746 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
747 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
748 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
749 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
750 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
751 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
752 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
753 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
754 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
755 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
756 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
760 def ReadInFileBySizeWithContent(infile
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
761 delimiter
= formatspecs
[5];
762 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
765 fp
= UncompressArchiveFile(fp
, formatspecs
);
766 checkcompressfile
= CheckCompressionSubType(fp
, formatspecs
);
767 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
774 if(hasattr(sys
.stdin
, "buffer")):
775 shutil
.copyfileobj(sys
.stdin
.buffer, fp
);
777 shutil
.copyfileobj(sys
.stdin
, fp
);
779 fp
= UncompressArchiveFile(fp
, formatspecs
);
783 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
784 fp
= download_file_from_internet_file(infile
);
785 fp
= UncompressArchiveFile(fp
, formatspecs
);
791 infile
= RemoveWindowsPath(infile
);
792 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
793 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
795 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
796 if(not compresscheck
):
797 fextname
= os
.path
.splitext(infile
)[1];
799 compresscheck
= "gzip";
800 elif(fextname
==".bz2"):
801 compresscheck
= "bzip2";
802 elif(fextname
==".zst"):
803 compresscheck
= "zstd";
804 elif(fextname
==".lz4" or fextname
==".clz4"):
805 compresscheck
= "lz4";
806 elif(fextname
==".lzo" or fextname
==".lzop"):
807 compresscheck
= "lzo";
808 elif(fextname
==".lzma" or fextname
==".xz"):
809 compresscheck
= "lzma";
812 if(not compresscheck
):
814 fp
= UncompressFile(infile
, formatspecs
, "rb");
815 return ReadFileDataBySizeWithContent(fp
, listonly
, skipchecksum
, formatspecs
);
817 def ReadInFileBySizeWithContentToArray(infile
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
818 delimiter
= formatspecs
[5];
819 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
822 fp
= UncompressArchiveFile(fp
, formatspecs
);
823 checkcompressfile
= CheckCompressionSubType(fp
, formatspecs
);
824 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
831 if(hasattr(sys
.stdin
, "buffer")):
832 shutil
.copyfileobj(sys
.stdin
.buffer, fp
);
834 shutil
.copyfileobj(sys
.stdin
, fp
);
836 fp
= UncompressArchiveFile(fp
, formatspecs
);
840 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
841 fp
= download_file_from_internet_file(infile
);
842 fp
= UncompressArchiveFile(fp
, formatspecs
);
848 infile
= RemoveWindowsPath(infile
);
849 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
850 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
852 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
853 if(not compresscheck
):
854 fextname
= os
.path
.splitext(infile
)[1];
856 compresscheck
= "gzip";
857 elif(fextname
==".bz2"):
858 compresscheck
= "bzip2";
859 elif(fextname
==".zst"):
860 compresscheck
= "zstd";
861 elif(fextname
==".lz4" or fextname
==".clz4"):
862 compresscheck
= "lz4";
863 elif(fextname
==".lzo" or fextname
==".lzop"):
864 compresscheck
= "lzo";
865 elif(fextname
==".lzma" or fextname
==".xz"):
866 compresscheck
= "lzma";
869 if(not compresscheck
):
871 fp
= UncompressFile(infile
, formatspecs
, "rb");
872 return ReadFileDataBySizeWithContentToArray(fp
, listonly
, skipchecksum
, formatspecs
);
874 def ReadInFileBySizeWithContentToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
875 if(isinstance(infile
, dict)):
876 listcatfiles
= infile
;
878 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
879 infile
= RemoveWindowsPath(infile
);
880 listcatfiles
= ReadInFileBySizeWithContentToArray(infile
, listonly
, skipchecksum
, formatspecs
);
881 if(not listcatfiles
):
883 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': {}}}};
884 lenlist
= len(listcatfiles
['ffilelist']);
889 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
892 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
893 lcfx
= listcatfiles
['fnumfiles'];
895 lcfx
= int(listcatfiles
['fnumfiles']);
897 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
898 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
899 catarray
['filetoid'].update(filetoidarray
);
900 catarray
['idtofile'].update(idtofilearray
);
901 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
902 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
903 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
904 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
905 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
906 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
907 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
908 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
909 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
910 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
911 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
912 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
913 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
914 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
915 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
916 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
917 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
918 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
919 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
920 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
921 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
922 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
923 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
924 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
925 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
926 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
927 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
928 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
929 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
930 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
931 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
935 def AppendNullByte(indata
, delimiter
=__file_format_delimiter__
):
936 outdata
= str(indata
) + delimiter
;
939 def AppendNullBytes(indata
=[], delimiter
=__file_format_delimiter__
):
944 outdata
= outdata
+ AppendNullByte(indata
[inum
], delimiter
);
948 def PrintPermissionString(fchmode
, ftype
):
949 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' } };
951 for fmodval
in str(oct(fchmode
))[-3:]:
952 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
953 if(ftype
==0 or ftype
==7):
954 permissionstr
= "-" + permissionstr
;
956 permissionstr
= "h" + permissionstr
;
958 permissionstr
= "l" + permissionstr
;
960 permissionstr
= "c" + permissionstr
;
962 permissionstr
= "b" + permissionstr
;
964 permissionstr
= "d" + permissionstr
;
966 permissionstr
= "f" + permissionstr
;
968 permissionstr
= "D" + permissionstr
;
970 permissionstr
= "p" + permissionstr
;
972 permissionstr
= "w" + permissionstr
;
974 permissionoutstr
= stat
.filemode(fchmode
);
975 except AttributeError:
976 permissionoutstr
= permissionstr
;
978 permissionoutstr
= permissionstr
;
979 return permissionoutstr
;
981 def PrintPermissionStringAlt(fchmode
, ftype
):
983 '0': '---', '1': '--x', '2': '-w-', '3': '-wx',
984 '4': 'r--', '5': 'r-x', '6': 'rw-', '7': 'rwx'
986 # Translate file mode into permission string
987 permissionstr
= ''.join([permissions
[i
] for i
in str(oct(fchmode
))[-3:]]);
988 # Append file type indicator
990 0: '-', 1: 'h', 2: 'l', 3: 'c', 4: 'b',
991 5: 'd', 6: 'f', 8: 'D', 9: 'p', 10: 'w'
993 file_type
= type_indicators
.get(ftype
, '-');
994 permissionstr
= file_type
+ permissionstr
;
996 permissionoutstr
= stat
.filemode(fchmode
);
997 except AttributeError:
998 permissionoutstr
= permissionstr
;
999 return permissionoutstr
;
1001 def CompressionSupport():
1002 compression_list
= [];
1005 compression_list
.append("gz");
1006 compression_list
.append("gzip");
1011 compression_list
.append("bz2");
1012 compression_list
.append("bzip2");
1017 compression_list
.append("lz4");
1022 compression_list
.append("lzo");
1023 compression_list
.append("lzop");
1028 compression_list
.append("zstd");
1029 compression_list
.append("zstandard");
1034 compression_list
.append("lzma");
1035 compression_list
.append("xz");
1038 return compression_list
;
1040 def CheckCompressionType(infile
, formatspecs
=__file_format_list__
, closefp
=True):
1041 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1045 catfp
= open(infile
, "rb");
1046 except FileNotFoundError
:
1049 prefp
= catfp
.read(2);
1051 if(prefp
==binascii
.unhexlify("1f8b")):
1054 prefp
= catfp
.read(3);
1055 if(prefp
==binascii
.unhexlify("425a68")):
1057 if(prefp
==binascii
.unhexlify("5d0000")):
1060 prefp
= catfp
.read(4);
1061 if(prefp
==binascii
.unhexlify("28b52ffd")):
1063 if(prefp
==binascii
.unhexlify("04224d18")):
1065 if(prefp
==binascii
.unhexlify("504B0304")):
1066 filetype
= "zipfile";
1068 prefp
= catfp
.read(5);
1069 if(prefp
==binascii
.unhexlify("7573746172")):
1070 filetype
= "tarfile";
1072 prefp
= catfp
.read(6);
1073 if(prefp
==binascii
.unhexlify("fd377a585a00")):
1076 prefp
= catfp
.read(7);
1077 if(prefp
==binascii
.unhexlify("526172211a0700")):
1078 filetype
= "rarfile";
1079 if(prefp
==binascii
.unhexlify("43617446696c65")):
1080 filetype
= "catfile";
1082 prefp
= catfp
.read(8);
1083 if(prefp
==binascii
.unhexlify("526172211a070100")):
1084 filetype
= "rarfile";
1086 prefp
= catfp
.read(formatspecs
[3]);
1087 if(prefp
==binascii
.unhexlify(formatspecs
[4])):
1088 filetype
= formatspecs
[2];
1090 prefp
= catfp
.read(9);
1091 if(prefp
==binascii
.unhexlify("894c5a4f000d0a1a0a")):
1094 prefp
= catfp
.read(10);
1095 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
1096 filetype
= "tarfile";
1102 def CheckCompressionTypeFromString(instring
, formatspecs
=__file_format_list__
, closefp
=True):
1104 instringsfile
= BytesIO(instring
);
1106 instringsfile
= BytesIO(instring
.encode("UTF-8"));
1107 return CheckCompressionType(instringsfile
, formatspecs
, closefp
);
1109 def GetCompressionMimeType(infile
, formatspecs
=__file_format_list__
):
1110 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
1111 if(compresscheck
=="gzip" or compresscheck
=="gz"):
1112 return archivefile_gzip_mimetype
;
1113 if(compresscheck
=="bzip2" or compresscheck
=="bz2"):
1114 return archivefile_bzip2_mimetype
;
1115 if(compresscheck
=="zstd" or compresscheck
=="zstandard"):
1116 return archivefile_zstandard_mimetype
;
1117 if(compresscheck
=="lz4"):
1118 return archivefile_lz4_mimetype
;
1119 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
1120 return archivefile_lzop_mimetype
;
1121 if(compresscheck
=="lzma"):
1122 return archivefile_lzma_mimetype
;
1123 if(compresscheck
=="xz"):
1124 return archivefile_xz_mimetype
;
1125 if(compresscheck
=="catfile" or compresscheck
=="cat" or compresscheck
==formatspecs
[2]):
1126 return archivefile_cat_mimetype
;
1127 if(not compresscheck
):
1131 def UncompressArchiveFile(fp
, formatspecs
=__file_format_list__
):
1132 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
1134 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
1135 if(compresscheck
=="gzip"):
1140 catfp
= gzip
.GzipFile(fileobj
=fp
, mode
="rb");
1141 if(compresscheck
=="bzip2"):
1147 catfp
.write(bz2
.decompress(fp
.read()));
1148 if(compresscheck
=="zstd"):
1154 catfp
.write(zstandard
.decompress(fp
.read()));
1155 if(compresscheck
=="lz4"):
1161 catfp
.write(lz4
.frame
.decompress(fp
.read()));
1162 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
1168 catfp
.write(lzo
.decompress(fp
.read()));
1169 if(compresscheck
=="lzma" or compresscheck
=="xz"):
1175 catfp
.write(lzma
.decompress(fp
.read()));
1176 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[2]):
1178 if(not compresscheck
):
1184 with fp
as fpcontent
:
1186 catfp
.write(lzma
.decompress(fp
.read()));
1187 except lzma
.LZMAError
:
1191 create_alias_function("Uncompress", __file_format_name__
, "", UncompressArchiveFile
);
1193 def UncompressFile(infile
, formatspecs
=__file_format_list__
, mode
="rb"):
1194 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
1195 if(sys
.version_info
[0]==2 and compresscheck
):
1201 if(compresscheck
=="gzip"):
1207 filefp
= gzip
.open(infile
, mode
, encoding
="UTF-8");
1208 except (ValueError, TypeError) as e
:
1209 filefp
= gzip
.open(infile
, mode
);
1210 if(compresscheck
=="bzip2"):
1216 filefp
= bz2
.open(infile
, mode
, encoding
="UTF-8");
1217 except (ValueError, TypeError) as e
:
1218 filefp
= bz2
.open(infile
, mode
);
1219 if(compresscheck
=="zstd"):
1225 filefp
= zstandard
.open(infile
, mode
, encoding
="UTF-8");
1226 except (ValueError, TypeError) as e
:
1227 filefp
= zstandard
.open(infile
, mode
);
1228 if(compresscheck
=="lz4"):
1234 filefp
= lz4
.frame
.open(infile
, mode
, encoding
="UTF-8");
1235 except (ValueError, TypeError) as e
:
1236 filefp
= lz4
.frame
.open(infile
, mode
);
1237 if(compresscheck
=="lzo"):
1243 filefp
= lzo
.open(infile
, mode
, encoding
="UTF-8");
1244 except (ValueError, TypeError) as e
:
1245 filefp
= lzo
.open(infile
, mode
);
1246 if(compresscheck
=="lzma"):
1252 filefp
= lzma
.open(infile
, mode
, encoding
="UTF-8");
1253 except (ValueError, TypeError) as e
:
1254 filefp
= lzma
.open(infile
, mode
);
1255 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[2]):
1257 filefp
= open(infile
, mode
, encoding
="UTF-8");
1258 except (ValueError, TypeError) as e
:
1259 filefp
= open(infile
, mode
);
1260 if(not compresscheck
):
1262 filefp
= open(infile
, mode
, encoding
="UTF-8");
1263 except (ValueError, TypeError) as e
:
1264 filefp
= open(infile
, mode
);
1265 except FileNotFoundError
:
1269 def UncompressString(infile
):
1270 compresscheck
= CheckCompressionTypeFromString(infile
, formatspecs
, False);
1271 if(compresscheck
=="gzip"):
1276 fileuz
= gzip
.decompress(infile
);
1277 if(compresscheck
=="bzip2"):
1282 fileuz
= bz2
.decompress(infile
);
1283 if(compresscheck
=="zstd"):
1288 fileuz
= zstandard
.decompress(infile
);
1289 if(compresscheck
=="lz4"):
1294 fileuz
= lz4
.frame
.decompress(infile
);
1295 if(compresscheck
=="lzo"):
1300 fileuz
= lzo
.decompress(infile
);
1301 if(compresscheck
=="lzma"):
1306 fileuz
= lzma
.decompress(infile
);
1307 if(not compresscheck
):
1309 if(hasattr(fileuz
, 'decode')):
1310 fileuz
= fileuz
.decode("UTF-8");
1313 def UncompressStringAlt(infile
):
1314 filefp
= StringIO();
1315 outstring
= UncompressString(infile
);
1316 filefp
.write(outstring
);
1320 def CheckCompressionSubType(infile
, formatspecs
=__file_format_list__
):
1321 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
1322 if(not compresscheck
):
1323 fextname
= os
.path
.splitext(infile
)[1];
1324 if(fextname
==".gz"):
1325 compresscheck
= "gzip";
1326 elif(fextname
==".bz2"):
1327 compresscheck
= "bzip2";
1328 elif(fextname
==".zst"):
1329 compresscheck
= "zstd";
1330 elif(fextname
==".lz4"):
1331 compresscheck
= "lz4";
1332 elif(fextname
==".lzo" or fextname
==".lzop"):
1333 compresscheck
= "lzo";
1334 elif(fextname
==".lzma" or fextname
==".xz"):
1335 compresscheck
= "lzma";
1338 if(not compresscheck
):
1340 if(compresscheck
=="catfile"):
1342 if(compresscheck
==formatspecs
[2]):
1343 return formatspecs
[2];
1344 if(compresscheck
=="tarfile"):
1346 if(compresscheck
=="zipfile"):
1348 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1349 catfp
= UncompressArchiveFile(infile
, formatspecs
[2]);
1352 if(compresscheck
=="gzip"):
1357 catfp
= gzip
.GzipFile(infile
, "rb");
1358 if(compresscheck
=="bzip2"):
1363 catfp
= bz2
.BZ2File(infile
, "rb");
1364 if(compresscheck
=="lz4"):
1369 catfp
= lz4
.frame
.open(infile
, "rb");
1370 if(compresscheck
=="zstd"):
1375 catfp
= zstandard
.open(infile
, "rb");
1376 if(compresscheck
=="lzma" or compresscheck
=="xz"):
1381 catfp
= lzma
.open(infile
, "rb");
1382 except FileNotFoundError
:
1385 prefp
= catfp
.read(5);
1386 if(prefp
==binascii
.unhexlify("7573746172")):
1387 filetype
= "tarfile";
1389 prefp
= catfp
.read(7);
1390 if(prefp
==binascii
.unhexlify("43617446696c65")):
1391 filetype
= "catfile";
1393 prefp
= catfp
.read(formatspecs
[3]);
1394 if(prefp
==binascii
.unhexlify(formatspecs
[4])):
1395 filetype
= formatspecs
[2];
1397 prefp
= catfp
.read(10);
1398 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
1399 filetype
= "tarfile";
1404 def GZipCompress(data
, compresslevel
=9):
1409 tmpfp
= tempfile
.NamedTemporaryFile("wb", delete
=False);
1411 tmpfp
= gzip
.GzipFile(tmpfp
.name
, mode
="wb", compresslevel
=compresslevel
);
1415 catfp
= open(tmpfp
.name
, "rb");
1416 except FileNotFoundError
:
1418 catdata
= catfp
.read();
1422 def CompressArchiveFile(fp
, compression
="auto", compressionlevel
=None, formatspecs
=__file_format_list__
):
1423 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1424 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
1427 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
1429 if(compression
not in compressionlist
and compression
is None):
1430 compression
= "auto";
1431 if(compression
=="gzip"):
1437 if(compressionlevel
is None):
1438 compressionlevel
= 9;
1440 compressionlevel
= int(compressionlevel
);
1441 catfp
.write(gzip
.compress(fp
.read(), compresslevel
=compressionlevel
));
1442 if(compression
=="bzip2"):
1448 if(compressionlevel
is None):
1449 compressionlevel
= 9;
1451 compressionlevel
= int(compressionlevel
);
1452 catfp
.write(bz2
.compress(fp
.read(), compresslevel
=compressionlevel
));
1453 if(compression
=="lz4"):
1459 if(compressionlevel
is None):
1460 compressionlevel
= 9;
1462 compressionlevel
= int(compressionlevel
);
1463 catfp
.write(lz4
.frame
.compress(fp
.read(), compression_level
=compressionlevel
));
1464 if(compression
=="lzo" or compression
=="lzop"):
1470 if(compressionlevel
is None):
1471 compressionlevel
= 9;
1473 compressionlevel
= int(compressionlevel
);
1474 catfp
.write(lzo
.compress(fp
.read(), compresslevel
=compressionlevel
));
1475 if(compression
=="zstd"):
1481 if(compressionlevel
is None):
1482 compressionlevel
= 10;
1484 compressionlevel
= int(compressionlevel
);
1485 catfp
.write(zstandard
.compress(fp
.read(), level
=compressionlevel
));
1486 if(compression
=="lzma"):
1492 if(compressionlevel
is None):
1493 compressionlevel
= 9;
1495 compressionlevel
= int(compressionlevel
);
1496 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]));
1497 if(compression
=="xz"):
1503 if(compressionlevel
is None):
1504 compressionlevel
= 9;
1506 compressionlevel
= int(compressionlevel
);
1507 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]));
1508 if(compression
=="auto" or compression
is None):
1513 create_alias_function("Compress", __file_format_name__
, "", CompressArchiveFile
);
1515 def CompressOpenFile(outfile
, compressionlevel
=None):
1516 if(outfile
is None):
1518 fbasename
= os
.path
.splitext(outfile
)[0];
1519 fextname
= os
.path
.splitext(outfile
)[1];
1520 if(compressionlevel
is None and fextname
!=".zst"):
1521 compressionlevel
= 9;
1522 elif(compressionlevel
is None and fextname
==".zst"):
1523 compressionlevel
= 10;
1525 compressionlevel
= int(compressionlevel
);
1526 if(sys
.version_info
[0]==2):
1531 if(fextname
not in outextlistwd
):
1533 outfp
= open(outfile
, "wb", encoding
="UTF-8");
1534 except (ValueError, TypeError) as e
:
1535 outfp
= open(outfile
, "wb");
1536 elif(fextname
==".gz"):
1542 outfp
= gzip
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
1543 except (ValueError, TypeError) as e
:
1544 outfp
= gzip
.open(outfile
, mode
, compressionlevel
);
1545 elif(fextname
==".bz2"):
1551 outfp
= bz2
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
1552 except (ValueError, TypeError) as e
:
1553 outfp
= bz2
.open(outfile
, mode
, compressionlevel
);
1554 elif(fextname
==".zst"):
1560 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
), encoding
="UTF-8");
1561 except (ValueError, TypeError) as e
:
1562 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
));
1563 elif(fextname
==".xz"):
1569 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}], encoding
="UTF-8");
1570 except (ValueError, TypeError) as e
:
1571 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]);
1572 elif(fextname
==".lz4"):
1578 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
, encoding
="UTF-8");
1579 except (ValueError, TypeError) as e
:
1580 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
);
1581 elif(fextname
==".lzo"):
1587 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
, encoding
="UTF-8");
1588 except (ValueError, TypeError) as e
:
1589 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
);
1590 elif(fextname
==".lzma"):
1596 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}], encoding
="UTF-8");
1597 except (ValueError, TypeError) as e
:
1598 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]);
1599 except FileNotFoundError
:
1603 def GetDevMajorMinor(fdev
):
1605 if(hasattr(os
, "minor")):
1606 retdev
.append(os
.minor(fdev
));
1609 if(hasattr(os
, "major")):
1610 retdev
.append(os
.major(fdev
));
1615 def CheckSumSupport(checkfor
, guaranteed
=True):
1617 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
1619 hash_list
= sorted(list(hashlib
.algorithms_available
));
1620 checklistout
= sorted(hash_list
+ ['adler32', 'crc16', 'crc16_ansi', 'crc16_ibm', 'crc16_ccitt', 'crc32', 'crc64', 'crc64_ecma', 'crc64_iso', 'none']);
1621 if(checkfor
in checklistout
):
1626 def CheckSumSupportAlt(checkfor
, guaranteed
=True):
1628 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
1630 hash_list
= sorted(list(hashlib
.algorithms_available
));
1631 checklistout
= hash_list
;
1632 if(checkfor
in checklistout
):
1637 def PackArchiveFile(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1638 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1639 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1640 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1641 advancedlist
= True;
1642 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1643 outfile
= RemoveWindowsPath(outfile
);
1644 checksumtype
= checksumtype
.lower();
1645 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
1646 checksumtype
="crc32";
1647 if(checksumtype
=="none"):
1649 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
1651 if(compression
not in compressionlist
and compression
is None):
1652 compression
= "auto";
1654 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1655 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1656 if(os
.path
.exists(outfile
)):
1661 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1663 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
1666 fbasename
= os
.path
.splitext(outfile
)[0];
1667 fextname
= os
.path
.splitext(outfile
)[1];
1668 catfp
= CompressOpenFile(outfile
, compressionlevel
);
1669 catver
= formatspecs
[6];
1670 fileheaderver
= str(int(catver
.replace(".", "")));
1671 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
1672 catfp
.write(fileheader
.encode('UTF-8'));
1675 for line
in sys
.stdin
:
1676 infilelist
.append(line
.strip());
1677 infilelist
= list(filter(None, infilelist
));
1678 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
1679 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
1681 with
open(infiles
, "r") as finfile
:
1682 for line
in finfile
:
1683 infilelist
.append(line
.strip());
1684 infilelist
= list(filter(None, infilelist
));
1686 if(isinstance(infiles
, (list, tuple, ))):
1687 infilelist
= list(filter(None, infiles
));
1688 elif(isinstance(infiles
, (str, ))):
1689 infilelist
= list(filter(None, [infiles
]));
1691 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
1693 GetDirList
= ListDir(infilelist
, followlink
, False);
1701 inodetocatinode
= {};
1702 fnumfiles
= format(int(len(GetDirList
)), 'x').lower();
1703 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
1704 if(checksumtype
=="none" or checksumtype
==""):
1705 catfileheadercshex
= format(0, 'x').lower();
1706 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1707 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1708 elif(checksumtype
=="crc16_ccitt"):
1709 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1710 elif(checksumtype
=="adler32"):
1711 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1712 elif(checksumtype
=="crc32"):
1713 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1714 elif(checksumtype
=="crc64_ecma"):
1715 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1716 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1717 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1718 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1719 checksumoutstr
= hashlib
.new(checksumtype
);
1720 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
1721 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1723 catfileheadercshex
= format(0, 'x').lower
1724 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
1725 catfp
.write(fnumfilesa
.encode('UTF-8'));
1728 os
.fsync(catfp
.fileno());
1729 except io
.UnsupportedOperation
:
1731 except AttributeError:
1733 for curfname
in GetDirList
:
1734 catfhstart
= catfp
.tell();
1735 if(re
.findall("^[.|/]", curfname
)):
1738 fname
= "./"+curfname
;
1740 VerbosePrintOut(fname
);
1741 if(not followlink
or followlink
is None):
1742 fstatinfo
= os
.lstat(fname
);
1744 fstatinfo
= os
.stat(fname
);
1745 fpremode
= fstatinfo
.st_mode
;
1746 finode
= fstatinfo
.st_ino
;
1747 flinkcount
= fstatinfo
.st_nlink
;
1749 if(stat
.S_ISREG(fpremode
)):
1751 elif(stat
.S_ISLNK(fpremode
)):
1753 elif(stat
.S_ISCHR(fpremode
)):
1755 elif(stat
.S_ISBLK(fpremode
)):
1757 elif(stat
.S_ISDIR(fpremode
)):
1759 elif(stat
.S_ISFIFO(fpremode
)):
1761 elif(stat
.S_ISSOCK(fpremode
)):
1763 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
1765 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
1767 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
1772 fcurfid
= format(int(curfid
), 'x').lower();
1773 if(not followlink
and finode
!=0):
1775 if(finode
in inodelist
):
1777 flinkname
= inodetofile
[finode
];
1778 fcurinode
= format(int(inodetocatinode
[finode
]), 'x').lower();
1779 if(finode
not in inodelist
):
1780 inodelist
.append(finode
);
1781 inodetofile
.update({finode
: fname
});
1782 inodetocatinode
.update({finode
: curinode
});
1783 fcurinode
= format(int(curinode
), 'x').lower();
1784 curinode
= curinode
+ 1;
1786 fcurinode
= format(int(curinode
), 'x').lower();
1787 curinode
= curinode
+ 1;
1788 curfid
= curfid
+ 1;
1790 flinkname
= os
.readlink(fname
);
1791 fdev
= fstatinfo
.st_dev
;
1792 getfdev
= GetDevMajorMinor(fdev
);
1793 fdev_minor
= getfdev
[0];
1794 fdev_major
= getfdev
[1];
1795 frdev
= fstatinfo
.st_dev
;
1796 if(hasattr(fstatinfo
, "st_rdev")):
1797 frdev
= fstatinfo
.st_rdev
;
1799 frdev
= fstatinfo
.st_dev
;
1800 getfrdev
= GetDevMajorMinor(frdev
);
1801 frdev_minor
= getfrdev
[0];
1802 frdev_major
= getfrdev
[1];
1803 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
1804 fsize
= format(int("0"), 'x').lower();
1805 elif(ftype
==0 or ftype
==7):
1806 fsize
= format(int(fstatinfo
.st_size
), 'x').lower();
1808 fsize
= format(int(fstatinfo
.st_size
)).lower();
1809 fatime
= format(int(fstatinfo
.st_atime
), 'x').lower();
1810 fmtime
= format(int(fstatinfo
.st_mtime
), 'x').lower();
1811 fctime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1812 if(hasattr(fstatinfo
, "st_birthtime")):
1813 fbtime
= format(int(fstatinfo
.st_birthtime
), 'x').lower();
1815 fbtime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1816 fmode
= format(int(fstatinfo
.st_mode
), 'x').lower();
1817 fchmode
= format(int(stat
.S_IMODE(fstatinfo
.st_mode
)), 'x').lower();
1818 ftypemod
= format(int(stat
.S_IFMT(fstatinfo
.st_mode
)), 'x').lower();
1819 fuid
= format(int(fstatinfo
.st_uid
), 'x').lower();
1820 fgid
= format(int(fstatinfo
.st_gid
), 'x').lower();
1825 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
1826 funame
= userinfo
.pw_name
;
1835 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
1836 fgname
= groupinfo
.gr_name
;
1841 fdev_minor
= format(int(fdev_minor
), 'x').lower();
1842 fdev_major
= format(int(fdev_major
), 'x').lower();
1843 frdev_minor
= format(int(frdev_minor
), 'x').lower();
1844 frdev_major
= format(int(frdev_major
), 'x').lower();
1845 finode
= format(int(finode
), 'x').lower();
1846 flinkcount
= format(int(flinkcount
), 'x').lower();
1847 if(hasattr(fstatinfo
, "st_file_attributes")):
1848 fwinattributes
= format(int(fstatinfo
.st_file_attributes
), 'x').lower();
1850 fwinattributes
= format(int(0), 'x').lower();
1851 fcontents
= "".encode('UTF-8');
1853 if(ftype
== 0 or ftype
== 7):
1854 with
open(fname
, "rb") as fpc
:
1856 chunk
= fpc
.read(chunk_size
);
1860 if(followlink
and (ftype
== 1 or ftype
== 2)):
1861 flstatinfo
= os
.stat(flinkname
);
1862 with
open(flinkname
, "rb") as fpc
:
1864 chunk
= fpc
.read(chunk_size
);
1868 ftypehex
= format(ftype
, 'x').lower();
1869 extrafields
= format(len(extradata
), 'x').lower();
1870 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
1871 if(len(extradata
)>0):
1872 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
1873 extrasizelen
= format(len(extrasizestr
), 'x').lower();
1874 catfileoutstr
= AppendNullBytes([ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
], formatspecs
[5]);
1875 if(len(extradata
)>0):
1876 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
1877 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
1878 if(checksumtype
=="none" or checksumtype
==""):
1879 catfileheadercshex
= format(0, 'x').lower();
1880 catfilecontentcshex
= format(0, 'x').lower();
1881 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1882 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
1883 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
1884 elif(checksumtype
=="crc16_ccitt"):
1885 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
1886 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
1887 elif(checksumtype
=="adler32"):
1888 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1889 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
1890 elif(checksumtype
=="crc32"):
1891 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1892 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
1893 elif(checksumtype
=="crc64_ecma"):
1894 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1895 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
1896 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1897 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1898 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
1899 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1900 checksumoutstr
= hashlib
.new(checksumtype
);
1901 checksumoutstr
.update("".encode('UTF-8'));
1902 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1903 checksumoutstr
= hashlib
.new(checksumtype
);
1904 checksumoutstr
.update(fcontents
);
1905 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
1907 catfileheadercshex
= format(0, 'x').lower();
1908 catfilecontentcshex
= format(0, 'x').lower();
1909 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1910 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
1911 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
1912 if(checksumtype
=="none" or checksumtype
==""):
1913 catfileheadercshex
= format(0, 'x').lower();
1914 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1915 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
1916 elif(checksumtype
=="crc16_ccitt"):
1917 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
1918 elif(checksumtype
=="adler32"):
1919 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
1920 elif(checksumtype
=="crc32"):
1921 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
1922 elif(checksumtype
=="crc64_ecma"):
1923 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1924 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1925 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1926 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1927 checksumoutstr
= hashlib
.new(checksumtype
);
1928 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
1929 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1931 catfileheadercshex
= format(0, 'x').lower();
1932 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1933 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
1934 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
1935 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
1936 nullstrecd
= formatspecs
[5].encode('UTF-8');
1937 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
1938 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
1939 catfp
.write(catfileout
);
1942 os
.fsync(catfp
.fileno());
1943 except io
.UnsupportedOperation
:
1945 except AttributeError:
1947 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1948 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1951 os
.fsync(catfp
.fileno());
1952 except io
.UnsupportedOperation
:
1954 except AttributeError:
1958 if(hasattr(sys
.stdout
, "buffer")):
1959 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1961 shutil
.copyfileobj(catfp
, sys
.stdout
);
1962 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
1963 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1965 upload_file_to_internet_file(catfp
, outfile
);
1973 create_alias_function("Pack", __file_format_name__
, "", PackArchiveFile
);
1975 if(hasattr(shutil
, "register_archive_format")):
1976 def PackArchiveFileFunc(archive_name
, source_dir
, **kwargs
):
1977 return PackArchiveFile(source_dir
, archive_name
, False, "auto", None, False, "crc32", [], __file_format_delimiter__
, False, False);
1978 create_alias_function("Pack", __file_format_name__
, "Func", PackArchiveFileFunc
);
1980 def PackArchiveFileFromDirList(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1981 return PackArchiveFile(infiles
, outfile
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
1983 def PackArchiveFileFromTarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1984 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1985 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1986 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1987 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1988 outfile
= RemoveWindowsPath(outfile
);
1989 checksumtype
= checksumtype
.lower();
1990 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
1991 checksumtype
="crc32";
1992 if(checksumtype
=="none"):
1994 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
1996 if(compression
not in compressionlist
and compression
is None):
1997 compression
= "auto";
1999 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2000 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2001 if(os
.path
.exists(outfile
)):
2006 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2008 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
2011 fbasename
= os
.path
.splitext(outfile
)[0];
2012 fextname
= os
.path
.splitext(outfile
)[1];
2013 catfp
= CompressOpenFile(outfile
, compressionlevel
);
2014 catver
= formatspecs
[6];
2015 fileheaderver
= str(int(catver
.replace(".", "")));
2016 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
2017 catfp
.write(fileheader
.encode('UTF-8'));
2023 inodetocatinode
= {};
2026 if(hasattr(sys
.stdin
, "buffer")):
2027 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
2029 shutil
.copyfileobj(sys
.stdin
, infile
);
2034 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
2035 infile
= download_file_from_internet_file(infile
);
2040 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2042 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
2044 if(not tarfile
.is_tarfile(infile
)):
2046 except AttributeError:
2047 if(not is_tarfile(infile
)):
2052 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2053 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
2055 tarfp
= tarfile
.open(infile
, "r");
2056 except FileNotFoundError
:
2058 fnumfiles
= format(int(len(tarfp
.getmembers())), 'x').lower();
2059 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2060 if(checksumtype
=="none" or checksumtype
==""):
2061 catfileheadercshex
= format(0, 'x').lower();
2062 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2063 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2064 elif(checksumtype
=="crc16_ccitt"):
2065 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2066 elif(checksumtype
=="adler32"):
2067 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2068 elif(checksumtype
=="crc32"):
2069 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2070 elif(checksumtype
=="crc64_ecma"):
2071 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2072 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2073 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2074 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2075 checksumoutstr
= hashlib
.new(checksumtype
);
2076 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
2077 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2079 catfileheadercshex
= format(0, 'x').lower();
2080 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2081 catfp
.write(fnumfilesa
.encode('UTF-8'));
2084 os
.fsync(catfp
.fileno());
2085 except io
.UnsupportedOperation
:
2087 except AttributeError:
2089 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
2090 catfhstart
= catfp
.tell();
2091 if(re
.findall("^[.|/]", member
.name
)):
2092 fname
= member
.name
;
2094 fname
= "./"+member
.name
;
2096 VerbosePrintOut(fname
);
2097 fpremode
= member
.mode
;
2098 ffullmode
= member
.mode
;
2102 ffullmode
= member
.mode
+ stat
.S_IFREG
;
2104 elif(member
.isdev()):
2105 ffullmode
= member
.mode
;
2107 elif(member
.islnk()):
2108 ffullmode
= member
.mode
+ stat
.S_IFREG
;
2110 elif(member
.issym()):
2111 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
2113 elif(member
.ischr()):
2114 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
2116 elif(member
.isblk()):
2117 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
2119 elif(member
.isdir()):
2120 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
2122 elif(member
.isfifo()):
2123 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
2125 elif(member
.issparse()):
2126 ffullmode
= member
.mode
;
2129 ffullmode
= member
.mode
;
2132 fcurfid
= format(int(curfid
), 'x').lower();
2133 fcurinode
= format(int(0), 'x').lower();
2134 curfid
= curfid
+ 1;
2136 flinkname
= member
.linkname
;
2137 fdev_minor
= format(int(member
.devminor
), 'x').lower();
2138 fdev_major
= format(int(member
.devmajor
), 'x').lower();
2139 frdev_minor
= format(int(member
.devminor
), 'x').lower();
2140 frdev_major
= format(int(member
.devmajor
), 'x').lower();
2141 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
2142 fsize
= format(int("0"), 'x').lower();
2143 elif(ftype
==0 or ftype
==7):
2144 fsize
= format(int(member
.size
), 'x').lower();
2146 fsize
= format(int(member
.size
), 'x').lower();
2147 fatime
= format(int(member
.mtime
), 'x').lower();
2148 fmtime
= format(int(member
.mtime
), 'x').lower();
2149 fctime
= format(int(member
.mtime
), 'x').lower();
2150 fbtime
= format(int(member
.mtime
), 'x').lower();
2151 fmode
= format(int(ffullmode
), 'x').lower();
2152 fchmode
= format(int(stat
.S_IMODE(ffullmode
)), 'x').lower();
2153 ftypemod
= format(int(stat
.S_IFMT(ffullmode
)), 'x').lower();
2154 fuid
= format(int(member
.uid
), 'x').lower();
2155 fgid
= format(int(member
.gid
), 'x').lower();
2156 funame
= member
.uname
;
2157 fgname
= member
.gname
;
2158 flinkcount
= format(int(flinkcount
), 'x').lower();
2159 fcontents
= "".encode('UTF-8');
2161 if(ftype
== 0 or ftype
== 7):
2162 with tarfp
.extractfile(member
) as fpc
:
2164 chunk
= fpc
.read(chunk_size
);
2168 ftypehex
= format(ftype
, 'x').lower();
2169 extrafields
= format(len(extradata
), 'x').lower();
2170 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
2171 if(len(extradata
)>0):
2172 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2173 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2174 catfileoutstr
= AppendNullBytes([ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
], formatspecs
[5]);
2175 if(len(extradata
)>0):
2176 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2177 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
2178 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
2179 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
2180 if(checksumtype
=="none" or checksumtype
==""):
2181 catfileheadercshex
= format(0, 'x').lower();
2182 catfilecontentcshex
= format(0, 'x').lower();
2183 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2184 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2185 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
2186 elif(checksumtype
=="crc16_ccitt"):
2187 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2188 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
2189 elif(checksumtype
=="adler32"):
2190 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2191 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
2192 elif(checksumtype
=="crc32"):
2193 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2194 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
2195 elif(checksumtype
=="crc64_ecma"):
2196 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2197 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
2198 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2199 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2200 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
2201 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2202 checksumoutstr
= hashlib
.new(checksumtype
);
2203 checksumoutstr
.update("".encode('UTF-8'));
2204 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2205 checksumoutstr
= hashlib
.new(checksumtype
);
2206 checksumoutstr
.update(fcontents
);
2207 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
2209 catfileheadercshex
= format(0, 'x').lower();
2210 catfilecontentcshex
= format(0, 'x').lower();
2211 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2212 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2213 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
2214 if(checksumtype
=="none" or checksumtype
==""):
2215 catfileheadercshex
= format(0, 'x').lower();
2216 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2217 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2218 elif(checksumtype
=="crc16_ccitt"):
2219 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2220 elif(checksumtype
=="adler32"):
2221 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2222 elif(checksumtype
=="crc32"):
2223 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2224 elif(checksumtype
=="crc64_ecma"):
2225 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2226 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2227 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2228 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2229 checksumoutstr
= hashlib
.new(checksumtype
);
2230 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
2231 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2233 catfileheadercshex
= format(0, 'x').lower();
2234 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2235 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
2236 nullstrecd
= formatspecs
[5].encode('UTF-8');
2237 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
2238 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
2239 catfp
.write(catfileout
);
2242 os
.fsync(catfp
.fileno());
2243 except io
.UnsupportedOperation
:
2245 except AttributeError:
2247 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2248 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2251 os
.fsync(catfp
.fileno());
2252 except io
.UnsupportedOperation
:
2254 except AttributeError:
2258 if(hasattr(sys
.stdout
, "buffer")):
2259 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2261 shutil
.copyfileobj(catfp
, sys
.stdout
);
2262 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
2263 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2265 upload_file_from_internet_file(catfp
, outfile
);
2273 create_alias_function("Pack", __file_format_name__
, "FromTarFile", PackArchiveFileFromTarFile
);
2275 def PackArchiveFileFromZipFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2276 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2277 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2278 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
2279 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2280 outfile
= RemoveWindowsPath(outfile
);
2281 checksumtype
= checksumtype
.lower();
2282 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2283 checksumtype
="crc32";
2284 if(checksumtype
=="none"):
2286 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2288 if(compression
not in compressionlist
and compression
is None):
2289 compression
= "auto";
2291 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2292 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2293 if(os
.path
.exists(outfile
)):
2298 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2300 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
2303 fbasename
= os
.path
.splitext(outfile
)[0];
2304 fextname
= os
.path
.splitext(outfile
)[1];
2305 catfp
= CompressOpenFile(outfile
, compressionlevel
);
2306 catver
= formatspecs
[6];
2307 fileheaderver
= str(int(catver
.replace(".", "")));
2308 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
2309 catfp
.write(fileheader
.encode('UTF-8'));
2315 inodetocatinode
= {};
2318 if(hasattr(sys
.stdin
, "buffer")):
2319 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
2321 shutil
.copyfileobj(sys
.stdin
, infile
);
2326 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
2327 infile
= download_file_from_internet_file(infile
);
2332 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2336 if(not zipfile
.is_zipfile(infile
)):
2339 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
2340 except FileNotFoundError
:
2342 ziptest
= zipfp
.testzip();
2344 VerbosePrintOut("Bad file found!");
2345 fnumfiles
= format(int(len(zipfp
.infolist())), 'x').lower();
2346 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2347 if(checksumtype
=="none" or checksumtype
==""):
2348 catfileheadercshex
= format(0, 'x').lower();
2349 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2350 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2351 elif(checksumtype
=="crc16_ccitt"):
2352 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2353 elif(checksumtype
=="adler32"):
2354 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2355 elif(checksumtype
=="crc32"):
2356 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2357 elif(checksumtype
=="crc64_ecma"):
2358 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2359 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2360 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2361 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2362 checksumoutstr
= hashlib
.new(checksumtype
);
2363 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
2364 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2366 catfileheadercshex
= format(0, 'x').lower();
2367 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2368 catfp
.write(fnumfilesa
.encode('UTF-8'));
2371 os
.fsync(catfp
.fileno());
2372 except io
.UnsupportedOperation
:
2374 except AttributeError:
2376 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
2377 catfhstart
= catfp
.tell();
2378 if(re
.findall("^[.|/]", member
.filename
)):
2379 fname
= member
.filename
;
2381 fname
= "./"+member
.filename
;
2382 zipinfo
= zipfp
.getinfo(member
.filename
);
2384 VerbosePrintOut(fname
);
2385 if(not member
.is_dir()):
2386 fpremode
= int(stat
.S_IFREG
+ 438);
2387 elif(member
.is_dir()):
2388 fpremode
= int(stat
.S_IFDIR
+ 511);
2391 if(not member
.is_dir()):
2393 elif(member
.is_dir()):
2396 fcurfid
= format(int(curfid
), 'x').lower();
2397 fcurinode
= format(int(0), 'x').lower();
2398 curfid
= curfid
+ 1;
2399 fdev_minor
= format(int(0), 'x').lower();
2400 fdev_major
= format(int(0), 'x').lower();
2401 frdev_minor
= format(int(0), 'x').lower();
2402 frdev_major
= format(int(0), 'x').lower();
2404 fsize
= format(int("0"), 'x').lower();
2406 fsize
= format(int(member
.file_size
), 'x').lower();
2408 fsize
= format(int(member
.file_size
), 'x').lower();
2409 fatime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2410 fmtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2411 fctime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2412 fbtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2413 if(not member
.is_dir()):
2414 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
2415 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
2416 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
2417 if(member
.is_dir()):
2418 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
2419 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
2420 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
2422 fuid
= format(int(os
.getuid()), 'x').lower();
2423 except AttributeError:
2424 fuid
= format(int(0), 'x').lower();
2426 fuid
= format(int(0), 'x').lower();
2428 fgid
= format(int(os
.getgid()), 'x').lower();
2429 except AttributeError:
2430 fgid
= format(int(0), 'x').lower();
2432 fgid
= format(int(0), 'x').lower();
2436 userinfo
= pwd
.getpwuid(os
.getuid());
2437 funame
= userinfo
.pw_name
;
2440 except AttributeError:
2448 groupinfo
= grp
.getgrgid(os
.getgid());
2449 fgname
= groupinfo
.gr_name
;
2452 except AttributeError:
2456 fcontents
= "".encode('UTF-8');
2458 fcontents
= zipfp
.read(member
.filename
);
2459 ftypehex
= format(ftype
, 'x').lower();
2460 extrafields
= format(len(extradata
), 'x').lower();
2461 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
2462 if(len(extradata
)>0):
2463 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2464 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2465 catfileoutstr
= AppendNullBytes([ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
], formatspecs
[5]);
2466 if(len(extradata
)>0):
2467 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2468 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
2469 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
2470 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
2471 if(checksumtype
=="none" or checksumtype
==""):
2472 catfileheadercshex
= format(0, 'x').lower();
2473 catfilecontentcshex
= format(0, 'x').lower();
2474 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2475 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2476 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
2477 elif(checksumtype
=="crc16_ccitt"):
2478 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2479 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
2480 elif(checksumtype
=="adler32"):
2481 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2482 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
2483 elif(checksumtype
=="crc32"):
2484 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2485 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
2486 elif(checksumtype
=="crc64_ecma"):
2487 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2488 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
2489 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2490 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2491 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
2492 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2493 checksumoutstr
= hashlib
.new(checksumtype
);
2494 checksumoutstr
.update("".encode('UTF-8'));
2495 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2496 checksumoutstr
= hashlib
.new(checksumtype
);
2497 checksumoutstr
.update(fcontents
);
2498 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
2500 catfileheadercshex
= format(0, 'x').lower();
2501 catfilecontentcshex
= format(0, 'x').lower();
2502 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2503 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2504 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
2505 if(checksumtype
=="none" or checksumtype
==""):
2506 catfileheadercshex
= format(0, 'x').lower();
2507 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2508 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2509 elif(checksumtype
=="crc16_ccitt"):
2510 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2511 elif(checksumtype
=="adler32"):
2512 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2513 elif(checksumtype
=="crc32"):
2514 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2515 elif(checksumtype
=="crc64_ecma"):
2516 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2517 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2518 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2519 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2520 checksumoutstr
= hashlib
.new(checksumtype
);
2521 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
2522 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2524 catfileheadercshex
= format(0, 'x').lower();
2525 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2526 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
2527 nullstrecd
= formatspecs
[5].encode('UTF-8');
2528 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
2529 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
2530 catfp
.write(catfileout
);
2533 os
.fsync(catfp
.fileno());
2534 except io
.UnsupportedOperation
:
2536 except AttributeError:
2538 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2539 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2542 os
.fsync(catfp
.fileno());
2543 except io
.UnsupportedOperation
:
2545 except AttributeError:
2549 if(hasattr(sys
.stdout
, "buffer")):
2550 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2552 shutil
.copyfileobj(catfp
, sys
.stdout
);
2553 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
2554 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2556 upload_file_to_internet_file(catfp
, outfile
);
2564 create_alias_function("Pack", __file_format_name__
, "FromZipFile", PackArchiveFileFromZipFile
);
2566 if(not rarfile_support
):
2567 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2570 if(rarfile_support
):
2571 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2572 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2573 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2574 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
2575 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2576 outfile
= RemoveWindowsPath(outfile
);
2577 checksumtype
= checksumtype
.lower();
2578 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2579 checksumtype
="crc32";
2580 if(checksumtype
=="none"):
2582 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2584 if(compression
not in compressionlist
and compression
is None):
2585 compression
= "auto";
2587 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2588 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2589 if(os
.path
.exists(outfile
)):
2594 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2596 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
2599 fbasename
= os
.path
.splitext(outfile
)[0];
2600 fextname
= os
.path
.splitext(outfile
)[1];
2601 catfp
= CompressOpenFile(outfile
, compressionlevel
);
2602 catver
= formatspecs
[6];
2603 fileheaderver
= str(int(catver
.replace(".", "")));
2604 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
2605 catfp
.write(fileheader
.encode('UTF-8'));
2611 inodetocatinode
= {};
2612 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2614 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
2616 rarfp
= rarfile
.RarFile(infile
, "r");
2617 rartest
= rarfp
.testrar();
2619 VerbosePrintOut("Bad file found!");
2620 fnumfiles
= format(int(len(rarfp
.infolist())), 'x').lower();
2621 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2622 if(checksumtype
=="none" or checksumtype
==""):
2623 catfileheadercshex
= format(0, 'x').lower();
2624 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2625 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2626 elif(checksumtype
=="crc16_ccitt"):
2627 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2628 elif(checksumtype
=="adler32"):
2629 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2630 elif(checksumtype
=="crc32"):
2631 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2632 elif(checksumtype
=="crc64_ecma"):
2633 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2634 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2635 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2636 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2637 checksumoutstr
= hashlib
.new(checksumtype
);
2638 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
2639 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2641 catfileheadercshex
= format(0, 'x').lower();
2642 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2643 catfp
.write(fnumfilesa
.encode('UTF-8'));
2646 os
.fsync(catfp
.fileno());
2647 except io
.UnsupportedOperation
:
2649 except AttributeError:
2651 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
2654 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
2657 member
.external_attr
2659 except AttributeError:
2661 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
2664 member
.external_attr
2666 except AttributeError:
2671 catfhstart
= catfp
.tell();
2672 if(re
.findall("^[.|/]", member
.filename
)):
2673 fname
= member
.filename
;
2675 fname
= "./"+member
.filename
;
2676 rarinfo
= rarfp
.getinfo(member
.filename
);
2678 VerbosePrintOut(fname
);
2679 if(is_unix
and member
.external_attr
!=0):
2680 fpremode
= int(member
.external_attr
);
2681 elif(member
.is_file()):
2682 fpremode
= int(stat
.S_IFREG
+ 438);
2683 elif(member
.is_symlink()):
2684 fpremode
= int(stat
.S_IFLNK
+ 438);
2685 elif(member
.is_dir()):
2686 fpremode
= int(stat
.S_IFDIR
+ 511);
2687 if(is_windows
and member
.external_attr
!=0):
2688 fwinattributes
= format(int(member
.external_attr
), 'x').lower();
2690 fwinattributes
= format(int(0), 'x').lower();
2693 if(member
.is_file()):
2695 elif(member
.is_symlink()):
2697 elif(member
.is_dir()):
2701 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
2702 fcurfid
= format(int(curfid
), 'x').lower();
2703 fcurinode
= format(int(0), 'x').lower();
2704 curfid
= curfid
+ 1;
2705 fdev_minor
= format(int(0), 'x').lower();
2706 fdev_major
= format(int(0), 'x').lower();
2707 frdev_minor
= format(int(0), 'x').lower();
2708 frdev_major
= format(int(0), 'x').lower();
2710 fsize
= format(int("0"), 'x').lower();
2712 fsize
= format(int(member
.file_size
), 'x').lower();
2714 fsize
= format(int(member
.file_size
), 'x').lower();
2717 fatime
= format(int(member
.atime
.timestamp()), 'x').lower();
2719 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2720 except AttributeError:
2721 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2722 fmtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2725 fctime
= format(int(member
.ctime
.timestamp()), 'x').lower();
2727 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2728 except AttributeError:
2729 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2730 fbtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2731 if(is_unix
and member
.external_attr
!=0):
2732 fmode
= format(int(member
.external_attr
), 'x').lower();
2733 fchmode
= format(int(stat
.S_IMODE(member
.external_attr
)), 'x').lower();
2734 ftypemod
= format(int(stat
.S_IFMT(member
.external_attr
)), 'x').lower();
2735 elif(member
.is_file()):
2736 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
2737 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
2738 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
2739 elif(member
.is_symlink()):
2740 fmode
= format(int(stat
.S_IFLNK
+ 438), 'x').lower();
2741 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
2742 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
2743 elif(member
.is_dir()):
2744 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
2745 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
2746 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
2748 fuid
= format(int(os
.getuid()), 'x').lower();
2749 except AttributeError:
2750 fuid
= format(int(0), 'x').lower();
2752 fuid
= format(int(0), 'x').lower();
2754 fgid
= format(int(os
.getgid()), 'x').lower();
2755 except AttributeError:
2756 fgid
= format(int(0), 'x').lower();
2758 fgid
= format(int(0), 'x').lower();
2762 userinfo
= pwd
.getpwuid(os
.getuid());
2763 funame
= userinfo
.pw_name
;
2766 except AttributeError:
2774 groupinfo
= grp
.getgrgid(os
.getgid());
2775 fgname
= groupinfo
.gr_name
;
2778 except AttributeError:
2782 fcontents
= "".encode('UTF-8');
2784 fcontents
= rarfp
.read(member
.filename
);
2785 ftypehex
= format(ftype
, 'x').lower();
2786 extrafields
= format(len(extradata
), 'x').lower();
2787 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
2788 if(len(extradata
)>0):
2789 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2790 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2791 catfileoutstr
= AppendNullBytes([ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
], formatspecs
[5]);
2792 if(len(extradata
)>0):
2793 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2794 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
2795 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
2796 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
2797 if(checksumtype
=="none" or checksumtype
==""):
2798 catfileheadercshex
= format(0, 'x').lower();
2799 catfilecontentcshex
= format(0, 'x').lower();
2800 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2801 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2802 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
2803 elif(checksumtype
=="crc16_ccitt"):
2804 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2805 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
2806 elif(checksumtype
=="adler32"):
2807 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2808 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
2809 elif(checksumtype
=="crc32"):
2810 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2811 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
2812 elif(checksumtype
=="crc64_ecma"):
2813 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2814 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
2815 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2816 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2817 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
2818 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2819 checksumoutstr
= hashlib
.new(checksumtype
);
2820 checksumoutstr
.update("".encode('UTF-8'));
2821 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2822 checksumoutstr
= hashlib
.new(checksumtype
);
2823 checksumoutstr
.update(fcontents
);
2824 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
2826 catfileheadercshex
= format(0, 'x').lower();
2827 catfilecontentcshex
= format(0, 'x').lower();
2828 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2829 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2830 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
2831 if(checksumtype
=="none" or checksumtype
==""):
2832 catfileheadercshex
= format(0, 'x').lower()
2833 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2834 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower()
2835 elif(checksumtype
=="crc16_ccitt"):
2836 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower()
2837 elif(checksumtype
=="adler32"):
2838 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower()
2839 elif(checksumtype
=="crc32"):
2840 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower()
2841 elif(checksumtype
=="crc64_ecma"):
2842 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
2843 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2844 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
2845 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2846 checksumoutstr
= hashlib
.new(checksumtype
)
2847 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'))
2848 catfileheadercshex
= checksumoutstr
.hexdigest().lower()
2850 catfileheadercshex
= format(0, 'x').lower()
2851 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5])
2852 catfileoutstrecd
= catfileoutstr
.encode('UTF-8')
2853 nullstrecd
= formatspecs
[5].encode('UTF-8')
2854 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
2855 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
)
2856 catfp
.write(catfileout
)
2859 os
.fsync(catfp
.fileno())
2860 except io
.UnsupportedOperation
:
2862 except AttributeError:
2864 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2865 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
)
2868 os
.fsync(catfp
.fileno())
2869 except io
.UnsupportedOperation
:
2871 except AttributeError:
2875 if(hasattr(sys
.stdout
, "buffer")):
2876 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2878 shutil
.copyfileobj(catfp
, sys
.stdout
);
2879 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
2880 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2882 upload_file_to_internet_file(catfp
, outfile
);
2890 create_alias_function("Pack", __file_format_name__
, "FromRarFile", PackArchiveFileFromRarFile
);
2892 def ArchiveFileSeekToFileNum(infile
, seekto
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
2893 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2896 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2897 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
2898 if(checkcompressfile
=="tarfile"):
2899 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2900 if(checkcompressfile
=="zipfile"):
2901 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2902 if(rarfile_support
and checkcompressfile
=="rarfile"):
2903 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2904 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
2911 if(hasattr(sys
.stdin
, "buffer")):
2912 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
2914 shutil
.copyfileobj(sys
.stdin
, catfp
);
2916 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2920 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
2921 catfp
= download_file_from_internet_file(infile
);
2923 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2928 infile
= RemoveWindowsPath(infile
);
2929 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
2930 if(checkcompressfile
=="tarfile"):
2931 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2932 if(checkcompressfile
=="zipfile"):
2933 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2934 if(rarfile_support
and checkcompressfile
=="rarfile"):
2935 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2936 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
2938 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
2939 if(not compresscheck
):
2940 fextname
= os
.path
.splitext(infile
)[1];
2941 if(fextname
==".gz"):
2942 compresscheck
= "gzip";
2943 elif(fextname
==".bz2"):
2944 compresscheck
= "bzip2";
2945 elif(fextname
==".zst"):
2946 compresscheck
= "zstd";
2947 elif(fextname
==".lz4" or fextname
==".clz4"):
2948 compresscheck
= "lz4";
2949 elif(fextname
==".lzo" or fextname
==".lzop"):
2950 compresscheck
= "lzo";
2951 elif(fextname
==".lzma" or fextname
==".xz"):
2952 compresscheck
= "lzma";
2955 if(not compresscheck
):
2957 catfp
= UncompressFile(infile
, formatspecs
, "rb");
2962 SeekToEndOfFile(catfp);
2964 SeekToEndOfFile(catfp);
2965 CatSize = catfp.tell();
2966 CatSizeEnd = CatSize;
2974 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
2975 catstring
= catheader
[0];
2976 catversion
= re
.findall(r
"([\d]+)$", catstring
);
2977 fprenumfiles
= catheader
[1];
2978 fnumfiles
= int(fprenumfiles
, 16);
2979 fprechecksumtype
= catheader
[2];
2980 fprechecksum
= catheader
[3];
2981 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
2982 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
2983 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
2984 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
2985 catfileheadercshex
= format(0, 'x').lower();
2986 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
2987 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2988 elif(fprechecksumtype
=="crc16_ccitt"):
2989 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2990 elif(fprechecksumtype
=="adler32"):
2991 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2992 elif(fprechecksumtype
=="crc32"):
2993 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2994 elif(fprechecksumtype
=="crc64_ecma"):
2995 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2996 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
2997 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2998 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
2999 checksumoutstr
= hashlib
.new(fprechecksumtype
);
3000 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3001 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3003 catfileheadercshex
= format(0, 'x').lower();
3004 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3005 fheadtell
= len(fileheader
);
3006 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
3007 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
3009 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
3010 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
3011 if(seekto
>=fnumfiles
):
3012 seekto
= fnumfiles
- 1;
3018 prefhstart
= catfp
.tell();
3020 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3022 preheaderdata
= ReadFileHeaderData(catfp
, 23, formatspecs
[5]);
3023 prefheadsize
= int(preheaderdata
[0], 16);
3024 preftype
= int(preheaderdata
[1], 16);
3025 if(re
.findall("^[.|/]", preheaderdata
[2])):
3026 prefname
= preheaderdata
[2];
3028 prefname
= "./"+preheaderdata
[2];
3029 prefbasedir
= os
.path
.dirname(prefname
);
3030 preflinkname
= preheaderdata
[3];
3031 prefsize
= int(preheaderdata
[4], 16);
3032 prefatime
= int(preheaderdata
[5], 16);
3033 prefmtime
= int(preheaderdata
[6], 16);
3034 prefctime
= int(preheaderdata
[7], 16);
3035 prefbtime
= int(preheaderdata
[8], 16);
3036 prefmode
= int(preheaderdata
[9], 16);
3037 prefchmode
= stat
.S_IMODE(prefmode
);
3038 preftypemod
= stat
.S_IFMT(prefmode
);
3039 prefuid
= int(preheaderdata
[10], 16);
3040 prefuname
= preheaderdata
[11];
3041 prefgid
= int(preheaderdata
[12], 16);
3042 prefgname
= preheaderdata
[13];
3043 fid
= int(preheaderdata
[14], 16);
3044 finode
= int(preheaderdata
[15], 16);
3045 flinkcount
= int(preheaderdata
[16], 16);
3046 prefdev_minor
= int(preheaderdata
[17], 16);
3047 prefdev_major
= int(preheaderdata
[18], 16);
3048 prefrdev_minor
= int(preheaderdata
[19], 16);
3049 prefrdev_major
= int(preheaderdata
[20], 16);
3050 prefextrasize
= int(preheaderdata
[21], 16);
3051 prefextrafields
= int(preheaderdata
[22], 16);
3052 extrafieldslist
= [];
3055 extraend
= extrastart
+ prefextrafields
;
3056 extrafieldslist
= [];
3057 if(extrastart
<extraend
):
3058 extrafieldslist
.append(preheaderdata
[extrastart
]);
3059 extrastart
= extrastart
+ 1;
3060 prefchecksumtype
= preheaderdata
[extrastart
].lower();
3061 prefcs
= preheaderdata
[extrastart
+ 1].lower();
3062 prefccs
= preheaderdata
[extrastart
+ 2].lower();
3064 extrafieldslist
= [];
3066 extrafieldslist
= ReadFileHeaderData(catfp
, prefextrafields
, formatspecs
[5]);
3067 checksumsval
= ReadFileHeaderData(catfp
, 3, formatspecs
[5]);
3068 prefchecksumtype
= checksumsval
[0].lower();
3069 prefcs
= checksumsval
[1].lower();
3070 prefccs
= checksumsval
[2].lower();
3073 hcmax
= len(preheaderdata
) - 2;
3075 hcmax
= len(preheaderdata
);
3078 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
3080 prefnumfields
= 24 + prefextrafields
;
3081 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
3083 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
3084 prenewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
3085 elif(prefchecksumtype
=="adler32"):
3086 prenewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3087 elif(prefchecksumtype
=="crc32"):
3088 prenewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3089 elif(prefchecksumtype
=="crc64_ecma"):
3090 prenewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3091 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
3092 prenewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3093 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
3094 checksumoutstr
= hashlib
.new(prefchecksumtype
);
3095 checksumoutstr
.update(hout
.encode('UTF-8'));
3096 prenewfcs
= checksumoutstr
.hexdigest().lower();
3097 if(prefcs
!=prenewfcs
and not skipchecksum
):
3098 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
3100 valid_archive
= False;
3101 invalid_archive
= True;
3102 prefhend
= catfp
.tell() - 1;
3103 prefcontentstart
= catfp
.tell();
3105 pyhascontents
= False;
3107 prefcontents
= catfp
.read(prefsize
);
3108 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
3110 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
3111 prenewfccs
= format(crc16(prefcontents
) & 0xffff, '04x').lower();
3112 elif(prefchecksumtype
=="crc16_ccitt"):
3113 prenewfcs
= format(crc16_ccitt(prefcontents
) & 0xffff, '04x').lower();
3114 elif(prefchecksumtype
=="adler32"):
3115 prenewfccs
= format(zlib
.adler32(prefcontents
) & 0xffffffff, '08x').lower();
3116 elif(prefchecksumtype
=="crc32"):
3117 prenewfccs
= format(crc32(prefcontents
) & 0xffffffff, '08x').lower();
3118 elif(prefchecksumtype
=="crc64_ecma"):
3119 prenewfcs
= format(crc64_ecma(prefcontents
) & 0xffffffffffffffff, '016x').lower();
3120 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
3121 prenewfcs
= format(crc64_iso(prefcontents
) & 0xffffffffffffffff, '016x').lower();
3122 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
3123 checksumoutstr
= hashlib
.new(prefchecksumtype
);
3124 checksumoutstr
.update(prefcontents
);
3125 prenewfccs
= checksumoutstr
.hexdigest().lower();
3126 pyhascontents
= True;
3127 if(prefccs
!=prenewfccs
and not skipchecksum
):
3128 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
3132 catfp
.seek(seekstart
, 0);
3134 catfheadsize
= int(preheaderdata
[0], 16);
3135 catftype
= int(preheaderdata
[1], 16);
3136 if(re
.findall("^[.|/]", preheaderdata
[2])):
3137 catfname
= preheaderdata
[2];
3139 catfname
= "./"+preheaderdata
[2];
3140 catflinkname
= preheaderdata
[3];
3141 catfsize
= int(preheaderdata
[4], 16);
3142 catfbasedir
= os
.path
.dirname(catfname
);
3143 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
3145 catlist
.update({'catfp': catfp
});
3150 create_alias_function("", __file_format_name__
, "SeekToFileNum", ArchiveFileSeekToFileNum
);
3152 def ArchiveFileSeekToFileName(infile
, seekfile
=None, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3153 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3156 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3157 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
3158 if(checkcompressfile
=="tarfile"):
3159 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3160 if(checkcompressfile
=="zipfile"):
3161 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3162 if(rarfile_support
and checkcompressfile
=="rarfile"):
3163 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3164 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3171 if(hasattr(sys
.stdin
, "buffer")):
3172 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
3174 shutil
.copyfileobj(sys
.stdin
, catfp
);
3176 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3180 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
3181 catfp
= download_file_from_internet_file(infile
);
3182 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3188 infile
= RemoveWindowsPath(infile
);
3189 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
3190 if(checkcompressfile
=="tarfile"):
3191 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3192 if(checkcompressfile
=="zipfile"):
3193 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3194 if(rarfile_support
and checkcompressfile
=="rarfile"):
3195 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3196 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3198 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
3199 if(not compresscheck
):
3200 fextname
= os
.path
.splitext(infile
)[1];
3201 if(fextname
==".gz"):
3202 compresscheck
= "gzip";
3203 elif(fextname
==".bz2"):
3204 compresscheck
= "bzip2";
3205 elif(fextname
==".zst"):
3206 compresscheck
= "zstd";
3207 elif(fextname
==".lz4" or fextname
==".clz4"):
3208 compresscheck
= "lz4";
3209 elif(fextname
==".lzo" or fextname
==".lzop"):
3210 compresscheck
= "lzo";
3211 elif(fextname
==".lzma" or fextname
==".xz"):
3212 compresscheck
= "lzma";
3215 if(not compresscheck
):
3217 catfp
= UncompressFile(infile
, formatspecs
, "rb");
3222 SeekToEndOfFile(catfp);
3224 SeekToEndOfFile(catfp);
3225 CatSize = catfp.tell();
3226 CatSizeEnd = CatSize;
3234 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
3235 catstring
= catheader
[0];
3236 catversion
= re
.findall(r
"([\d]+)$", catstring
);
3237 fprenumfiles
= catheader
[1];
3238 fnumfiles
= int(fprenumfiles
, 16);
3239 fprechecksumtype
= catheader
[2];
3240 fprechecksum
= catheader
[3];
3241 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
3242 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3243 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
3244 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
3245 catfileheadercshex
= format(0, 'x').lower();
3246 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
3247 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3248 elif(fprechecksumtype
=="crc16_ccitt"):
3249 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3250 elif(fprechecksumtype
=="adler32"):
3251 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3252 elif(fprechecksumtype
=="crc32"):
3253 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3254 elif(fprechecksumtype
=="crc64_ecma"):
3255 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3256 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
3257 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3258 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
3259 checksumoutstr
= hashlib
.new(fprechecksumtype
);
3260 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3261 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3263 catfileheadercshex
= format(0, 'x').lower();
3264 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3265 fheadtell
= len(fileheader
);
3266 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
3267 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
3269 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
3270 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
3271 seekto
= fnumfiles
- 1
3276 prefhstart
= catfp
.tell();
3278 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3280 preheaderdata
= ReadFileHeaderData(catfp
, 23, formatspecs
[5]);
3281 prefheadsize
= int(preheaderdata
[0], 16);
3282 preftype
= int(preheaderdata
[1], 16);
3283 if(re
.findall("^[.|/]", preheaderdata
[2])):
3284 prefname
= preheaderdata
[2];
3286 prefname
= "./"+preheaderdata
[2];
3287 prefbasedir
= os
.path
.dirname(prefname
);
3288 preflinkname
= preheaderdata
[3];
3289 prefsize
= int(preheaderdata
[4], 16);
3290 prefatime
= int(preheaderdata
[5], 16);
3291 prefmtime
= int(preheaderdata
[6], 16);
3292 prefctime
= int(preheaderdata
[7], 16);
3293 prefbtime
= int(preheaderdata
[8], 16);
3294 prefmode
= int(preheaderdata
[9], 16);
3295 prefchmode
= stat
.S_IMODE(prefmode
);
3296 preftypemod
= stat
.S_IFMT(prefmode
);
3297 prefuid
= int(preheaderdata
[10], 16);
3298 prefuname
= preheaderdata
[11];
3299 prefgid
= int(preheaderdata
[12], 16);
3300 prefgname
= preheaderdata
[13];
3301 fid
= int(preheaderdata
[14], 16);
3302 finode
= int(preheaderdata
[15], 16);
3303 flinkcount
= int(preheaderdata
[16], 16);
3304 prefdev_minor
= int(preheaderdata
[17], 16);
3305 prefdev_major
= int(preheaderdata
[18], 16);
3306 prefrdev_minor
= int(preheaderdata
[19], 16);
3307 prefrdev_major
= int(preheaderdata
[20], 16);
3308 prefextrasize
= int(preheaderdata
[21], 16);
3309 prefextrafields
= int(preheaderdata
[22], 16);
3310 extrafieldslist
= [];
3313 extraend
= extrastart
+ prefextrafields
;
3314 extrafieldslist
= [];
3315 if(extrastart
<extraend
):
3316 extrafieldslist
.append(preheaderdata
[extrastart
]);
3317 extrastart
= extrastart
+ 1;
3318 prefchecksumtype
= preheaderdata
[extrastart
].lower();
3319 prefcs
= preheaderdata
[extrastart
+ 1].lower();
3320 prefccs
= preheaderdata
[extrastart
+ 2].lower();
3322 extrafieldslist
= [];
3324 extrafieldslist
= ReadFileHeaderData(catfp
, prefextrafields
, formatspecs
[5]);
3325 checksumsval
= ReadFileHeaderData(catfp
, 3, formatspecs
[5]);
3326 prefchecksumtype
= checksumsval
[0].lower();
3327 prefcs
= checksumsval
[1].lower();
3328 prefccs
= checksumsval
[2].lower();
3331 hcmax
= len(preheaderdata
) - 2;
3333 hcmax
= len(preheaderdata
);
3336 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
3338 prefnumfields
= 24 + prefextrafields
;
3339 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
3341 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
3342 prenewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
3343 elif(prefchecksumtype
=="adler32"):
3344 prenewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3345 elif(prefchecksumtype
=="crc32"):
3346 prenewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3347 elif(prefchecksumtype
=="crc64_ecma"):
3348 prenewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3349 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
3350 prenewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3351 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
3352 checksumoutstr
= hashlib
.new(prefchecksumtype
);
3353 checksumoutstr
.update(hout
.encode('UTF-8'));
3354 prenewfcs
= checksumoutstr
.hexdigest().lower();
3355 if(prefcs
!=prenewfcs
and not skipchecksum
):
3356 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
3358 valid_archive
= False;
3359 invalid_archive
= True;
3360 prefhend
= catfp
.tell() - 1;
3361 prefcontentstart
= catfp
.tell();
3363 pyhascontents
= False;
3365 prefcontents
= catfp
.read(prefsize
);
3366 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
3368 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
3369 prenewfccs
= format(crc16(prefcontents
) & 0xffff, '04x').lower();
3370 elif(prefchecksumtype
=="crc16_ccitt"):
3371 prenewfcs
= format(crc16_ccitt(prefcontents
) & 0xffff, '04x').lower();
3372 elif(prefchecksumtype
=="adler32"):
3373 prenewfccs
= format(zlib
.adler32(prefcontents
) & 0xffffffff, '08x').lower();
3374 elif(prefchecksumtype
=="crc32"):
3375 prenewfccs
= format(crc32(prefcontents
) & 0xffffffff, '08x').lower();
3376 elif(prefchecksumtype
=="crc64_ecma"):
3377 prenewfcs
= format(crc64_ecma(prefcontents
) & 0xffffffffffffffff, '016x').lower();
3378 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
3379 prenewfcs
= format(crc64_iso(prefcontents
) & 0xffffffffffffffff, '016x').lower();
3380 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
3381 checksumoutstr
= hashlib
.new(prefchecksumtype
);
3382 checksumoutstr
.update(prefcontents
);
3383 prenewfccs
= checksumoutstr
.hexdigest().lower();
3384 pyhascontents
= True;
3385 if(prefccs
!=prenewfccs
and not skipchecksum
):
3386 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
3391 prefname
= preheaderdata
[2];
3392 if(re
.findall("^[.|/]", preheaderdata
[2])):
3393 prefname
= preheaderdata
[2];
3395 prefname
= "./"+preheaderdata
[2];
3396 if(prefname
==seekfile
):
3399 catfp
.seek(seekstart
, 0);
3401 catfheadsize
= int(preheaderdata
[0], 16);
3402 catftype
= int(preheaderdata
[1], 16);
3403 if(re
.findall("^[.|/]", preheaderdata
[2])):
3404 catfname
= preheaderdata
[2];
3406 catfname
= "./"+preheaderdata
[2];
3407 catflinkname
= preheaderdata
[3];
3408 catfsize
= int(preheaderdata
[4], 16);
3409 catfbasedir
= os
.path
.dirname(catfname
);
3411 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
3415 catlist
.update({'catfp': catfp
});
3420 create_alias_function("", __file_format_name__
, "SeekToFileName", ArchiveFileSeekToFileName
);
3422 def ArchiveFileValidate(infile
, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
3424 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
3425 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3428 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3429 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
3430 if(checkcompressfile
=="tarfile"):
3431 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3432 if(checkcompressfile
=="zipfile"):
3433 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3434 if(rarfile_support
and checkcompressfile
=="rarfile"):
3435 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3436 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3443 if(hasattr(sys
.stdin
, "buffer")):
3444 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
3446 shutil
.copyfileobj(sys
.stdin
, catfp
);
3448 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3452 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
3453 catfp
= download_file_from_internet_file(infile
);
3454 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3460 infile
= RemoveWindowsPath(infile
);
3461 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
3462 if(checkcompressfile
=="tarfile"):
3463 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3464 if(checkcompressfile
=="zipfile"):
3465 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3466 if(rarfile_support
and checkcompressfile
=="rarfile"):
3467 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3468 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3470 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
3471 if(not compresscheck
):
3472 fextname
= os
.path
.splitext(infile
)[1];
3473 if(fextname
==".gz"):
3474 compresscheck
= "gzip";
3475 elif(fextname
==".bz2"):
3476 compresscheck
= "bzip2";
3477 elif(fextname
==".zst"):
3478 compresscheck
= "zstd";
3479 elif(fextname
==".lz4" or fextname
==".clz4"):
3480 compresscheck
= "lz4";
3481 elif(fextname
==".lzo" or fextname
==".lzop"):
3482 compresscheck
= "lzo";
3483 elif(fextname
==".lzma" or fextname
==".xz"):
3484 compresscheck
= "lzma";
3487 if(not compresscheck
):
3489 catfp
= UncompressFile(infile
, formatspecs
, "rb");
3494 SeekToEndOfFile(catfp);
3496 SeekToEndOfFile(catfp);
3497 CatSize = catfp.tell();
3498 CatSizeEnd = CatSize;
3506 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
3507 catstring
= catheader
[0];
3508 catversion
= re
.findall(r
"([\d]+)$", catstring
);
3509 fprenumfiles
= catheader
[1];
3510 fnumfiles
= int(fprenumfiles
, 16);
3511 fprechecksumtype
= catheader
[2];
3512 fprechecksum
= catheader
[3];
3514 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
3515 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3516 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
3517 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
3518 catfileheadercshex
= format(0, 'x').lower();
3519 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
3520 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3521 elif(fprechecksumtype
=="crc16_ccitt"):
3522 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3523 elif(fprechecksumtype
=="adler32"):
3524 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3525 elif(fprechecksumtype
=="crc32"):
3526 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3527 elif(fprechecksumtype
=="crc64_ecma"):
3528 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3529 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
3530 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3531 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
3532 checksumoutstr
= hashlib
.new(fprechecksumtype
);
3533 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3534 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3536 catfileheadercshex
= format(0, 'x').lower();
3537 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3538 valid_archive
= True;
3539 invalid_archive
= False;
3541 VerbosePrintOut("Checking File Header Checksum of file at offset " + str(0));
3542 VerbosePrintOut(infile
);
3543 if(fprechecksum
==catfileheadercshex
):
3545 VerbosePrintOut("File Header Checksum Passed");
3548 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
3549 valid_archive
= False;
3550 invalid_archive
= True;
3551 while(il
<fnumfiles
):
3552 catfhstart
= catfp
.tell();
3554 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3556 catheaderdata
= ReadFileHeaderData(catfp
, 23, formatspecs
[5]);
3557 catfheadsize
= int(catheaderdata
[0], 16);
3558 catftype
= int(catheaderdata
[1], 16);
3559 if(re
.findall("^[.|/]", catheaderdata
[2])):
3560 catfname
= catheaderdata
[2];
3562 catfname
= "./"+catheaderdata
[2];
3563 catfbasedir
= os
.path
.dirname(catfname
);
3564 catflinkname
= catheaderdata
[3];
3565 catfsize
= int(catheaderdata
[4], 16);
3566 catfatime
= int(catheaderdata
[5], 16);
3567 catfmtime
= int(catheaderdata
[6], 16);
3568 catfctime
= int(catheaderdata
[7], 16);
3569 catfbtime
= int(catheaderdata
[8], 16);
3570 catfmode
= int(catheaderdata
[9], 16);
3571 catfchmode
= stat
.S_IMODE(catfmode
);
3572 catftypemod
= stat
.S_IFMT(catfmode
);
3573 catfuid
= int(catheaderdata
[10], 16);
3574 catfuname
= catheaderdata
[11];
3575 catfgid
= int(catheaderdata
[12], 16);
3576 catfgname
= catheaderdata
[13];
3577 fid
= int(catheaderdata
[14], 16);
3578 finode
= int(catheaderdata
[15], 16);
3579 flinkcount
= int(catheaderdata
[16], 16);
3580 catfdev_minor
= int(catheaderdata
[17], 16);
3581 catfdev_major
= int(catheaderdata
[18], 16);
3582 catfrdev_minor
= int(catheaderdata
[19], 16);
3583 catfrdev_major
= int(catheaderdata
[20], 16);
3584 catfextrasize
= int(catheaderdata
[21], 16);
3585 catfextrafields
= int(catheaderdata
[22], 16);
3586 extrafieldslist
= [];
3589 extraend
= extrastart
+ catfextrafields
;
3590 extrafieldslist
= [];
3591 if(extrastart
<extraend
):
3592 extrafieldslist
.append(catheaderdata
[extrastart
]);
3593 extrastart
= extrastart
+ 1;
3594 catfchecksumtype
= catheaderdata
[extrastart
].lower();
3595 catfcs
= catheaderdata
[extrastart
+ 1].lower();
3596 catfccs
= catheaderdata
[extrastart
+ 2].lower();
3598 extrafieldslist
= [];
3600 extrafieldslist
= ReadFileHeaderData(catfp
, catfextrafields
, formatspecs
[5]);
3601 checksumsval
= ReadFileHeaderData(catfp
, 3, formatspecs
[5]);
3602 catfchecksumtype
= checksumsval
[0].lower();
3603 catfcs
= checksumsval
[1].lower();
3604 catfccs
= checksumsval
[2].lower();
3607 hcmax
= len(catheaderdata
) - 2;
3609 hcmax
= len(catheaderdata
);
3612 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[5]);
3614 catfnumfields
= 24 + catfextrafields
;
3615 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
3617 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
3618 catnewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
3619 elif(catfchecksumtype
=="adler32"):
3620 catnewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3621 elif(catfchecksumtype
=="crc32"):
3622 catnewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3623 elif(catfchecksumtype
=="crc64_ecma"):
3624 catnewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3625 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
3626 catnewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3627 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
3628 checksumoutstr
= hashlib
.new(catfchecksumtype
);
3629 checksumoutstr
.update(hout
.encode('UTF-8'));
3630 catnewfcs
= checksumoutstr
.hexdigest().lower();
3632 VerbosePrintOut("Checking File Header Checksum of file at offset " + str(catfhstart
));
3633 VerbosePrintOut(catfname
);
3634 if(catfcs
==catnewfcs
):
3636 VerbosePrintOut("File Header Checksum Passed");
3639 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfhstart
));
3640 valid_archive
= False;
3641 invalid_archive
= True;
3642 catfhend
= catfp
.tell() - 1;
3643 catfcontentstart
= catfp
.tell();
3645 pyhascontents
= False;
3647 catfcontents
= catfp
.read(catfsize
);
3648 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
3650 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
3651 catnewfccs
= format(crc16(catfcontents
) & 0xffff, '04x').lower();
3652 elif(catfchecksumtype
=="crc16_ccitt"):
3653 catnewfcs
= format(crc16_ccitt(catfcontents
) & 0xffff, '04x').lower();
3654 elif(catfchecksumtype
=="adler32"):
3655 catnewfccs
= format(zlib
.adler32(catfcontents
) & 0xffffffff, '08x').lower();
3656 elif(catfchecksumtype
=="crc32"):
3657 catnewfccs
= format(crc32(catfcontents
) & 0xffffffff, '08x').lower();
3658 elif(catfchecksumtype
=="crc64_ecma"):
3659 catnewfcs
= format(crc64_ecma(catfcontents
) & 0xffffffffffffffff, '016x').lower();
3660 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
3661 catnewfcs
= format(crc64_iso(catfcontents
) & 0xffffffffffffffff, '016x').lower();
3662 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
3663 checksumoutstr
= hashlib
.new(catfchecksumtype
);
3664 checksumoutstr
.update(catfcontents
);
3665 catnewfccs
= checksumoutstr
.hexdigest().lower();
3666 pyhascontents
= True;
3668 VerbosePrintOut("Checking File Content Checksum of file at offset " + str(catfcontentstart
));
3669 VerbosePrintOut(catfname
);
3670 if(catfccs
==catnewfccs
):
3672 VerbosePrintOut("File Content Checksum Passed");
3675 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfcontentstart
));
3676 valid_archive
= False;
3677 invalid_archive
= True;
3679 VerbosePrintOut("");
3692 create_alias_function("", __file_format_name__
, "Validate", ArchiveFileValidate
);
3694 def ArchiveFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3695 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3698 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3699 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
3700 if(checkcompressfile
=="tarfile"):
3701 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3702 if(checkcompressfile
=="zipfile"):
3703 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3704 if(rarfile_support
and checkcompressfile
=="rarfile"):
3705 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3706 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3713 if(hasattr(sys
.stdin
, "buffer")):
3714 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
3716 shutil
.copyfileobj(sys
.stdin
, catfp
);
3718 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3722 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
3723 catfp
= download_file_from_internet_file(infile
);
3724 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3730 infile
= RemoveWindowsPath(infile
);
3731 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
3732 if(checkcompressfile
=="tarfile"):
3733 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3734 if(checkcompressfile
=="zipfile"):
3735 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3736 if(rarfile_support
and checkcompressfile
=="rarfile"):
3737 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3738 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3740 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
3741 if(not compresscheck
):
3742 fextname
= os
.path
.splitext(infile
)[1];
3743 if(fextname
==".gz"):
3744 compresscheck
= "gzip";
3745 elif(fextname
==".bz2"):
3746 compresscheck
= "bzip2";
3747 elif(fextname
==".zst"):
3748 compresscheck
= "zstd";
3749 elif(fextname
==".lz4" or fextname
==".clz4"):
3750 compresscheck
= "lz4";
3751 elif(fextname
==".lzo" or fextname
==".lzop"):
3752 compresscheck
= "lzo";
3753 elif(fextname
==".lzma" or fextname
==".xz"):
3754 compresscheck
= "lzma";
3757 if(not compresscheck
):
3759 catfp
= UncompressFile(infile
, formatspecs
, "rb");
3764 SeekToEndOfFile(catfp);
3766 SeekToEndOfFile(catfp);
3767 CatSize = catfp.tell();
3768 CatSizeEnd = CatSize;
3776 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
3777 catstring
= catheader
[0];
3778 catversion
= re
.findall(r
"([\d]+)$", catstring
);
3779 fprenumfiles
= catheader
[1];
3780 fnumfiles
= int(fprenumfiles
, 16);
3781 fprechecksumtype
= catheader
[2];
3782 fprechecksum
= catheader
[3];
3783 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
3784 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3785 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
3786 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
3787 catfileheadercshex
= format(0, 'x').lower();
3788 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
3789 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3790 elif(fprechecksumtype
=="crc16_ccitt"):
3791 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3792 elif(fprechecksumtype
=="adler32"):
3793 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3794 elif(fprechecksumtype
=="crc32"):
3795 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3796 elif(fprechecksumtype
=="crc64_ecma"):
3797 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3798 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
3799 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3800 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
3801 checksumoutstr
= hashlib
.new(fprechecksumtype
);
3802 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3803 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3805 catfileheadercshex
= format(0, 'x').lower();
3806 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3807 fheadtell
= len(fileheader
);
3808 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
3809 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
3811 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
3812 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
3813 if(seekstart
<0 and seekstart
>fnumfiles
):
3815 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
3816 seekend
= fnumfiles
;
3817 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
3818 seekend
= fnumfiles
- abs(seekend
);
3821 while(il
< seekstart
):
3822 prefhstart
= catfp
.tell();
3824 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3826 preheaderdata
= ReadFileHeaderData(catfp
, 23, formatspecs
[5]);
3827 prefheadsize
= int(preheaderdata
[0], 16);
3828 preftype
= int(preheaderdata
[1], 16);
3829 if(re
.findall("^[.|/]", preheaderdata
[2])):
3830 prefname
= preheaderdata
[2];
3832 prefname
= "./"+preheaderdata
[2];
3833 prefbasedir
= os
.path
.dirname(prefname
);
3834 preflinkname
= preheaderdata
[3];
3835 prefsize
= int(preheaderdata
[4], 16);
3836 prefatime
= int(preheaderdata
[5], 16);
3837 prefmtime
= int(preheaderdata
[6], 16);
3838 prefctime
= int(preheaderdata
[7], 16);
3839 prefbtime
= int(preheaderdata
[8], 16);
3840 prefmode
= int(preheaderdata
[9], 16);
3841 prefchmode
= stat
.S_IMODE(prefmode
);
3842 preftypemod
= stat
.S_IFMT(prefmode
);
3843 prefuid
= int(preheaderdata
[10], 16);
3844 prefuname
= preheaderdata
[11];
3845 prefgid
= int(preheaderdata
[12], 16);
3846 prefgname
= preheaderdata
[13];
3847 fid
= int(preheaderdata
[14], 16);
3848 finode
= int(preheaderdata
[15], 16);
3849 flinkcount
= int(preheaderdata
[16], 16);
3850 prefdev_minor
= int(preheaderdata
[17], 16);
3851 prefdev_major
= int(preheaderdata
[18], 16);
3852 prefrdev_minor
= int(preheaderdata
[19], 16);
3853 prefrdev_major
= int(preheaderdata
[20], 16);
3854 prefextrasize
= int(preheaderdata
[21], 16);
3855 prefextrafields
= int(preheaderdata
[22], 16);
3856 extrafieldslist
= [];
3859 extraend
= extrastart
+ prefextrafields
;
3860 extrafieldslist
= [];
3861 if(extrastart
<extraend
):
3862 extrafieldslist
.append(preheaderdata
[extrastart
]);
3863 extrastart
= extrastart
+ 1;
3864 prefchecksumtype
= preheaderdata
[extrastart
].lower();
3865 prefcs
= preheaderdata
[extrastart
+ 1].lower();
3866 prefccs
= preheaderdata
[extrastart
+ 2].lower();
3868 extrafieldslist
= [];
3870 extrafieldslist
= ReadFileHeaderData(catfp
, prefextrafields
, formatspecs
[5]);
3871 checksumsval
= ReadFileHeaderData(catfp
, 3, formatspecs
[5]);
3872 prefchecksumtype
= checksumsval
[0].lower();
3873 prefcs
= checksumsval
[1].lower();
3874 prefccs
= checksumsval
[2].lower();
3877 hcmax
= len(preheaderdata
) - 2;
3879 hcmax
= len(preheaderdata
);
3882 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
3884 prefnumfields
= 24 + prefextrafields
;
3885 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
3887 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
3888 prenewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
3889 elif(prefchecksumtype
=="adler32"):
3890 prenewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3891 elif(prefchecksumtype
=="crc32"):
3892 prenewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3893 elif(prefchecksumtype
=="crc64_ecma"):
3894 prenewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3895 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
3896 prenewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3897 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
3898 checksumoutstr
= hashlib
.new(prefchecksumtype
);
3899 checksumoutstr
.update(hout
.encode('UTF-8'));
3900 prenewfcs
= checksumoutstr
.hexdigest().lower();
3901 if(prefcs
!=prenewfcs
and not skipchecksum
):
3902 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
3904 valid_archive
= False;
3905 invalid_archive
= True;
3906 prefhend
= catfp
.tell() - 1;
3907 prefcontentstart
= catfp
.tell();
3909 pyhascontents
= False;
3911 prefcontents
= catfp
.read(prefsize
);
3912 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
3914 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
3915 prenewfccs
= format(crc16(prefcontents
) & 0xffff, '04x').lower();
3916 elif(prefchecksumtype
=="crc16_ccitt"):
3917 prenewfcs
= format(crc16_ccitt(prefcontents
) & 0xffff, '04x').lower();
3918 elif(prefchecksumtype
=="adler32"):
3919 prenewfccs
= format(zlib
.adler32(prefcontents
) & 0xffffffff, '08x').lower();
3920 elif(prefchecksumtype
=="crc32"):
3921 prenewfccs
= format(crc32(prefcontents
) & 0xffffffff, '08x').lower();
3922 elif(prefchecksumtype
=="crc64_ecma"):
3923 prenewfcs
= format(crc64_ecma(prefcontents
) & 0xffffffffffffffff, '016x').lower();
3924 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
3925 prenewfcs
= format(crc64_iso(prefcontents
) & 0xffffffffffffffff, '016x').lower();
3926 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
3927 checksumoutstr
= hashlib
.new(prefchecksumtype
);
3928 checksumoutstr
.update(prefcontents
);
3929 prenewfccs
= checksumoutstr
.hexdigest().lower();
3930 pyhascontents
= True;
3931 if(prefccs
!=prenewfccs
and not skipchecksum
):
3932 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
3936 fileidnum
= seekstart
;
3938 while(fileidnum
<seekend
):
3939 catfhstart
= catfp
.tell();
3941 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3943 catheaderdata
= ReadFileHeaderData(catfp
, 23, formatspecs
[5]);
3944 catfheadsize
= int(catheaderdata
[0], 16);
3945 catftype
= int(catheaderdata
[1], 16);
3946 if(re
.findall("^[.|/]", catheaderdata
[2])):
3947 catfname
= catheaderdata
[2];
3949 catfname
= "./"+catheaderdata
[2];
3950 catfbasedir
= os
.path
.dirname(catfname
);
3951 catflinkname
= catheaderdata
[3];
3952 catfsize
= int(catheaderdata
[4], 16);
3953 catfatime
= int(catheaderdata
[5], 16);
3954 catfmtime
= int(catheaderdata
[6], 16);
3955 catfctime
= int(catheaderdata
[7], 16);
3956 catfbtime
= int(catheaderdata
[8], 16);
3957 catfmode
= int(catheaderdata
[9], 16);
3958 catfchmode
= stat
.S_IMODE(catfmode
);
3959 catftypemod
= stat
.S_IFMT(catfmode
);
3960 catfuid
= int(catheaderdata
[10], 16);
3961 catfuname
= catheaderdata
[11];
3962 catfgid
= int(catheaderdata
[12], 16);
3963 catfgname
= catheaderdata
[13];
3964 fid
= int(catheaderdata
[14], 16);
3965 finode
= int(catheaderdata
[15], 16);
3966 flinkcount
= int(catheaderdata
[16], 16);
3967 catfdev_minor
= int(catheaderdata
[17], 16);
3968 catfdev_major
= int(catheaderdata
[18], 16);
3969 catfrdev_minor
= int(catheaderdata
[19], 16);
3970 catfrdev_major
= int(catheaderdata
[20], 16);
3971 catfextrasize
= int(catheaderdata
[21], 16);
3972 catfextrafields
= int(catheaderdata
[22], 16);
3973 extrafieldslist
= [];
3976 extraend
= extrastart
+ catfextrafields
;
3977 extrafieldslist
= [];
3978 if(extrastart
<extraend
):
3979 extrafieldslist
.append(catheaderdata
[extrastart
]);
3980 extrastart
= extrastart
+ 1;
3981 catfchecksumtype
= catheaderdata
[extrastart
].lower();
3982 catfcs
= catheaderdata
[extrastart
+ 1].lower();
3983 catfccs
= catheaderdata
[extrastart
+ 2].lower();
3985 extrafieldslist
= [];
3987 extrafieldslist
= ReadFileHeaderData(catfp
, catfextrafields
, formatspecs
[5]);
3988 checksumsval
= ReadFileHeaderData(catfp
, 3, formatspecs
[5]);
3989 catfchecksumtype
= checksumsval
[0].lower();
3990 catfcs
= checksumsval
[1].lower();
3991 catfccs
= checksumsval
[2].lower();
3994 hcmax
= len(catheaderdata
) - 2;
3996 hcmax
= len(catheaderdata
);
3999 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[5]);
4001 catfnumfields
= 24 + catfextrafields
;
4002 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
4004 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
4005 catnewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
4006 elif(catfchecksumtype
=="adler32"):
4007 catnewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4008 elif(catfchecksumtype
=="crc32"):
4009 catnewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4010 elif(catfchecksumtype
=="crc64_ecma"):
4011 catnewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4012 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
4013 catnewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4014 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
4015 checksumoutstr
= hashlib
.new(catfchecksumtype
);
4016 checksumoutstr
.update(hout
.encode('UTF-8'));
4017 catnewfcs
= checksumoutstr
.hexdigest().lower();
4018 if(catfcs
!=catnewfcs
and not skipchecksum
):
4019 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfhstart
));
4021 catfhend
= catfp
.tell() - 1;
4022 catfcontentstart
= catfp
.tell();
4024 pyhascontents
= False;
4025 if(catfsize
>0 and not listonly
):
4026 catfcontents
= catfp
.read(catfsize
);
4027 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
4029 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
4030 catnewfccs
= format(crc16(catfcontents
) & 0xffff, '04x').lower();
4031 elif(catfchecksumtype
=="crc16_ccitt"):
4032 catnewfcs
= format(crc16_ccitt(catfcontents
) & 0xffff, '04x').lower();
4033 elif(catfchecksumtype
=="adler32"):
4034 catnewfccs
= format(zlib
.adler32(catfcontents
) & 0xffffffff, '08x').lower();
4035 elif(catfchecksumtype
=="crc32"):
4036 catnewfccs
= format(crc32(catfcontents
) & 0xffffffff, '08x').lower();
4037 elif(catfchecksumtype
=="crc64_ecma"):
4038 catnewfcs
= format(crc64_ecma(catfcontents
) & 0xffffffffffffffff, '016x').lower();
4039 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
4040 catnewfcs
= format(crc64_iso(catfcontents
) & 0xffffffffffffffff, '016x').lower();
4041 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
4042 checksumoutstr
= hashlib
.new(catfchecksumtype
);
4043 checksumoutstr
.update(catfcontents
);
4044 catnewfccs
= checksumoutstr
.hexdigest().lower();
4045 pyhascontents
= True;
4046 if(catfccs
!=catnewfccs
and skipchecksum
):
4047 VerbosePrintOut("File Content Checksum Error with file " + catfname
+ " at offset " + str(catfcontentstart
));
4049 if(catfsize
>0 and listonly
):
4050 catfp
.seek(catfsize
, 1);
4051 pyhascontents
= False;
4053 catfcontentend
= catfp
.tell() - 1;
4054 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
, '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
, 'fextrafields': catfextrafields
, 'fextrafieldsize': catfextrasize
, 'fextralist': extrafieldslist
, 'fheaderchecksum': catfcs
, 'fcontentchecksum': catfccs
, 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': catfcontents
} });
4055 fileidnum
= fileidnum
+ 1;
4056 realidnum
= realidnum
+ 1;
4058 catlist
.update({'catfp': catfp
});
4063 create_alias_function("", __file_format_name__
, "ToArray", ArchiveFileToArray
);
4065 def ArchiveFileStringToArray(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4066 catfp
= BytesIO(catstr
);
4067 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4068 return listcatfiles
;
4070 create_alias_function("", __file_format_name__
, "StringToArray", ArchiveFileStringToArray
);
4072 def TarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4074 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4075 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4076 return listcatfiles
;
4078 def ZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4080 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4081 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4082 return listcatfiles
;
4084 if(not rarfile_support
):
4085 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4088 if(rarfile_support
):
4089 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4091 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4092 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4093 return listcatfiles
;
4095 def ListDirToArrayAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4096 catver
= formatspecs
[6];
4097 fileheaderver
= str(int(catver
.replace(".", "")));
4098 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4099 advancedlist
= True;
4102 for line
in sys
.stdin
:
4103 infilelist
.append(line
.strip());
4104 infilelist
= list(filter(None, infilelist
));
4105 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
4106 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
4108 with
open(infiles
, "r") as finfile
:
4109 for line
in finfile
:
4110 infilelist
.append(line
.strip());
4111 infilelist
= list(filter(None, infilelist
));
4113 if(isinstance(infiles
, (list, tuple, ))):
4114 infilelist
= list(filter(None, infiles
));
4115 elif(isinstance(infiles
, (str, ))):
4116 infilelist
= list(filter(None, [infiles
]));
4118 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
4120 GetDirList
= ListDir(infilelist
, followlink
, False);
4128 inodetocatinode
= {};
4130 fnumfiles
= int(len(GetDirList
));
4131 catver
= formatspecs
[6];
4132 fileheaderver
= str(int(catver
.replace(".", "")));
4133 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4134 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4135 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4136 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4137 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4138 if(checksumtype
=="none" or checksumtype
==""):
4139 catfileheadercshex
= format(0, 'x').lower();
4140 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4141 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4142 elif(checksumtype
=="crc16_ccitt"):
4143 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4144 elif(checksumtype
=="adler32"):
4145 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4146 elif(checksumtype
=="crc32"):
4147 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4148 elif(checksumtype
=="crc64_ecma"):
4149 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4150 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4151 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4152 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4153 checksumoutstr
= hashlib
.new(checksumtype
);
4154 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4155 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4157 catfileheadercshex
= format(0, 'x').lower();
4158 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4159 fheadtell
= len(fileheader
);
4160 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4161 for curfname
in GetDirList
:
4162 if(re
.findall("^[.|/]", curfname
)):
4165 fname
= "./"+curfname
;
4167 VerbosePrintOut(fname
);
4168 if(not followlink
or followlink
is None):
4169 fstatinfo
= os
.lstat(fname
);
4171 fstatinfo
= os
.stat(fname
);
4172 fpremode
= fstatinfo
.st_mode
;
4173 finode
= fstatinfo
.st_ino
;
4174 flinkcount
= fstatinfo
.st_nlink
;
4176 if(stat
.S_ISREG(fpremode
)):
4178 elif(stat
.S_ISLNK(fpremode
)):
4180 elif(stat
.S_ISCHR(fpremode
)):
4182 elif(stat
.S_ISBLK(fpremode
)):
4184 elif(stat
.S_ISDIR(fpremode
)):
4186 elif(stat
.S_ISFIFO(fpremode
)):
4188 elif(stat
.S_ISSOCK(fpremode
)):
4190 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
4192 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
4194 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
4199 fbasedir
= os
.path
.dirname(fname
);
4201 if(not followlink
and finode
!=0):
4203 if(finode
in inodelist
):
4205 flinkname
= inodetofile
[finode
];
4206 fcurinode
= inodetocatinode
[finode
];
4207 if(finode
not in inodelist
):
4208 inodelist
.append(finode
);
4209 inodetofile
.update({finode
: fname
});
4210 inodetocatinode
.update({finode
: curinode
});
4211 fcurinode
= curinode
;
4212 curinode
= curinode
+ 1;
4214 fcurinode
= curinode
;
4215 curinode
= curinode
+ 1;
4216 curfid
= curfid
+ 1;
4218 flinkname
= os
.readlink(fname
);
4219 fdev
= fstatinfo
.st_dev
;
4220 getfdev
= GetDevMajorMinor(fdev
);
4221 fdev_minor
= getfdev
[0];
4222 fdev_major
= getfdev
[1];
4223 frdev
= fstatinfo
.st_dev
;
4224 if(hasattr(fstatinfo
, "st_rdev")):
4225 frdev
= fstatinfo
.st_rdev
;
4227 frdev
= fstatinfo
.st_dev
;
4228 getfrdev
= GetDevMajorMinor(frdev
);
4229 frdev_minor
= getfrdev
[0];
4230 frdev_major
= getfrdev
[1];
4231 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
4233 if(ftype
==0 or ftype
==7):
4234 fsize
= fstatinfo
.st_size
;
4235 fatime
= fstatinfo
.st_atime
;
4236 fmtime
= fstatinfo
.st_mtime
;
4237 fctime
= fstatinfo
.st_ctime
;
4238 if(hasattr(fstatinfo
, "st_birthtime")):
4239 fbtime
= fstatinfo
.st_birthtime
;
4241 fbtime
= fstatinfo
.st_ctime
;
4242 fmode
= fstatinfo
.st_mode
;
4243 fchmode
= stat
.S_IMODE(fstatinfo
.st_mode
);
4244 ftypemod
= stat
.S_IFMT(fstatinfo
.st_mode
);
4245 fuid
= fstatinfo
.st_uid
;
4246 fgid
= fstatinfo
.st_gid
;
4251 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
4252 funame
= userinfo
.pw_name
;
4261 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
4262 fgname
= groupinfo
.gr_name
;
4267 fdev_minor
= fdev_minor
;
4268 fdev_major
= fdev_major
;
4269 frdev_minor
= frdev_minor
;
4270 frdev_major
= frdev_major
;
4272 flinkcount
= flinkcount
;
4273 if(hasattr(fstatinfo
, "st_file_attributes")):
4274 fwinattributes
= fstatinfo
.st_file_attributes
;
4277 fcontents
= "".encode('UTF-8');
4279 if(ftype
== 0 or ftype
== 7):
4280 with
open(fname
, "rb") as fpc
:
4282 chunk
= fpc
.read(chunk_size
);
4286 if(followlink
and (ftype
== 1 or ftype
== 2)):
4287 flstatinfo
= os
.stat(flinkname
);
4288 with
open(flinkname
, "rb") as fpc
:
4290 chunk
= fpc
.read(chunk_size
);
4294 ftypehex
= format(ftype
, 'x').lower();
4295 extrafields
= len(extradata
);
4296 extrafieldslist
= extradata
;
4297 catfextrafields
= extrafields
;
4298 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
4299 if(len(extradata
)>0):
4300 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4301 extrasizelen
= len(extrasizestr
);
4302 extrasizelenhex
= format(extrasizelen
, 'x').lower();
4303 catfileoutstr
= AppendNullBytes([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(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()], formatspecs
[5]);
4304 if(len(extradata
)>0):
4305 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4306 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
4307 catfnumfields
= 24 + catfextrafields
;
4308 if(checksumtype
=="none" or checksumtype
==""):
4309 catfileheadercshex
= format(0, 'x').lower();
4310 catfilecontentcshex
= format(0, 'x').lower();
4311 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4312 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
4313 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
4314 elif(checksumtype
=="crc16_ccitt"):
4315 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
4316 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
4317 elif(checksumtype
=="adler32"):
4318 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4319 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
4320 elif(checksumtype
=="crc32"):
4321 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4322 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
4323 elif(checksumtype
=="crc64_ecma"):
4324 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4325 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
4326 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4327 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4328 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
4329 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4330 checksumoutstr
= hashlib
.new(checksumtype
);
4331 checksumoutstr
.update("".encode('UTF-8'));
4332 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4333 checksumoutstr
= hashlib
.new(checksumtype
);
4334 checksumoutstr
.update(fcontents
);
4335 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
4337 catfileheadercshex
= format(0, 'x').lower();
4338 catfilecontentcshex
= format(0, 'x').lower();
4339 catfhstart
= fheadtell
;
4340 fheadtell
+= len(catfileoutstr
);
4341 catfhend
= fheadtell
- 1;
4342 catfcontentstart
= fheadtell
;
4343 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4344 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
4345 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
4346 if(checksumtype
=="none" or checksumtype
==""):
4347 catfileheadercshex
= format(0, 'x').lower();
4348 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4349 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4350 elif(checksumtype
=="crc16_ccitt"):
4351 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4352 elif(checksumtype
=="adler32"):
4353 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4354 elif(checksumtype
=="crc32"):
4355 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4356 elif(checksumtype
=="crc64_ecma"):
4357 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4358 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4359 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4360 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4361 checksumoutstr
= hashlib
.new(checksumtype
);
4362 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
4363 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4365 catfileheadercshex
= format(0, 'x').lower();
4366 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4367 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
4368 nullstrecd
= formatspecs
[5].encode('UTF-8');
4369 fheadtell
+= len(catfileoutstr
) + 1;
4370 catfcontentend
= fheadtell
- 1;
4371 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
4372 pyhascontents
= False;
4373 if(int(fsize
)>0 and not listonly
):
4374 pyhascontents
= True;
4375 if(int(fsize
)>0 and listonly
):
4377 pyhascontents
= False;
4378 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
, '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
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
4379 fileidnum
= fileidnum
+ 1;
4382 def TarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4388 inodetocatinode
= {};
4392 if(hasattr(sys
.stdin
, "buffer")):
4393 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
4395 shutil
.copyfileobj(sys
.stdin
, infile
);
4400 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
4401 infile
= download_file_from_internet_file(infile
);
4406 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
4408 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
4410 if(not tarfile
.is_tarfile(infile
)):
4412 except AttributeError:
4413 if(not is_tarfile(infile
)):
4418 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4419 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
4421 tarfp
= tarfile
.open(infile
, "r");
4422 except FileNotFoundError
:
4424 fnumfiles
= int(len(tarfp
.getmembers()));
4425 catver
= formatspecs
[6];
4426 fileheaderver
= str(int(catver
.replace(".", "")));
4427 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4428 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4429 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4430 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4431 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4432 if(checksumtype
=="none" or checksumtype
==""):
4433 catfileheadercshex
= format(0, 'x').lower();
4434 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4435 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4436 elif(checksumtype
=="crc16_ccitt"):
4437 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4438 elif(checksumtype
=="adler32"):
4439 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4440 elif(checksumtype
=="crc32"):
4441 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4442 elif(checksumtype
=="crc64_ecma"):
4443 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4444 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4445 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4446 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4447 checksumoutstr
= hashlib
.new(checksumtype
);
4448 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4449 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4451 catfileheadercshex
= format(0, 'x').lower();
4452 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4453 fheadtell
= len(fileheader
);
4454 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4455 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
4456 if(re
.findall("^[.|/]", member
.name
)):
4457 fname
= member
.name
;
4459 fname
= "./"+member
.name
;
4461 VerbosePrintOut(fname
);
4462 fpremode
= member
.mode
;
4463 ffullmode
= member
.mode
;
4467 ffullmode
= member
.mode
+ stat
.S_IFREG
;
4469 elif(member
.isdev()):
4470 ffullmode
= member
.mode
;
4472 elif(member
.islnk()):
4473 ffullmode
= member
.mode
+ stat
.S_IFREG
;
4475 elif(member
.issym()):
4476 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
4478 elif(member
.ischr()):
4479 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
4481 elif(member
.isblk()):
4482 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
4484 elif(member
.isdir()):
4485 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
4487 elif(member
.isfifo()):
4488 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
4490 elif(member
.issparse()):
4491 ffullmode
= member
.mode
;
4494 ffullmode
= member
.mode
;
4497 fbasedir
= os
.path
.dirname(fname
);
4501 curfid
= curfid
+ 1;
4503 flinkname
= member
.linkname
;
4504 fdev_minor
= member
.devminor
;
4505 fdev_major
= member
.devmajor
;
4506 frdev_minor
= member
.devminor
;
4507 frdev_major
= member
.devmajor
;
4508 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
4510 elif(ftype
==0 or ftype
==7):
4511 fsize
= member
.size
;
4513 fsize
= member
.size
;
4514 fatime
= member
.mtime
;
4515 fmtime
= member
.mtime
;
4516 fctime
= member
.mtime
;
4517 fbtime
= member
.mtime
;
4519 fchmode
= stat
.S_IMODE(ffullmode
);
4520 ftypemod
= stat
.S_IFMT(ffullmode
);
4523 funame
= member
.uname
;
4524 fgname
= member
.gname
;
4525 flinkcount
= flinkcount
;
4526 fcontents
= "".encode('UTF-8');
4528 if(ftype
== 0 or ftype
== 7):
4529 with tarfp
.extractfile(member
) as fpc
:
4531 chunk
= fpc
.read(chunk_size
);
4535 ftypehex
= format(ftype
, 'x').lower();
4536 extrafields
= len(extradata
);
4537 extrafieldslist
= extradata
;
4538 catfextrafields
= extrafields
;
4539 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
4540 if(len(extradata
)>0):
4541 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4542 extrasizelen
= len(extrasizestr
);
4543 extrasizelenhex
= format(extrasizelen
, 'x').lower();
4544 catfileoutstr
= AppendNullBytes([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(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()], formatspecs
[5]);
4545 if(len(extradata
)>0):
4546 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4547 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
4548 catfnumfields
= 24 + catfextrafields
;
4549 if(checksumtype
=="none" or checksumtype
==""):
4550 catfileheadercshex
= format(0, 'x').lower();
4551 catfilecontentcshex
= format(0, 'x').lower();
4552 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4553 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
4554 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
4555 elif(checksumtype
=="crc16_ccitt"):
4556 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
4557 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
4558 elif(checksumtype
=="adler32"):
4559 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4560 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
4561 elif(checksumtype
=="crc32"):
4562 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4563 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
4564 elif(checksumtype
=="crc64_ecma"):
4565 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4566 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
4567 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4568 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4569 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
4570 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4571 checksumoutstr
= hashlib
.new(checksumtype
);
4572 checksumoutstr
.update("".encode('UTF-8'));
4573 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4574 checksumoutstr
= hashlib
.new(checksumtype
);
4575 checksumoutstr
.update(fcontents
);
4576 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
4578 catfileheadercshex
= format(0, 'x').lower();
4579 catfilecontentcshex
= format(0, 'x').lower();
4580 catfhstart
= fheadtell
;
4581 fheadtell
+= len(catfileoutstr
);
4582 catfhend
= fheadtell
- 1;
4583 catfcontentstart
= fheadtell
;
4584 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4585 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
4586 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
4587 if(checksumtype
=="none" or checksumtype
==""):
4588 catfileheadercshex
= format(0, 'x').lower();
4589 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4590 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4591 elif(checksumtype
=="crc16_ccitt"):
4592 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4593 elif(checksumtype
=="adler32"):
4594 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4595 elif(checksumtype
=="crc32"):
4596 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4597 elif(checksumtype
=="crc64_ecma"):
4598 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4599 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4600 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4601 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4602 checksumoutstr
= hashlib
.new(checksumtype
);
4603 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
4604 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4606 catfileheadercshex
= format(0, 'x').lower();
4607 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4608 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
4609 nullstrecd
= formatspecs
[5].encode('UTF-8');
4610 fheadtell
+= len(catfileoutstr
) + 1;
4611 catfcontentend
= fheadtell
- 1;
4612 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
4613 pyhascontents
= False;
4614 if(int(fsize
)>0 and not listonly
):
4615 pyhascontents
= True;
4616 if(int(fsize
)>0 and listonly
):
4618 pyhascontents
= False;
4619 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
, '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
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
4620 fileidnum
= fileidnum
+ 1;
4623 def ZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4624 advancedlist
= True;
4630 inodetocatinode
= {};
4634 if(hasattr(sys
.stdin
, "buffer")):
4635 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
4637 shutil
.copyfileobj(sys
.stdin
, infile
);
4642 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
4643 infile
= download_file_from_internet_file(infile
);
4648 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
4652 if(not zipfile
.is_zipfile(infile
)):
4655 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
4656 except FileNotFoundError
:
4658 ziptest
= zipfp
.testzip();
4660 VerbosePrintOut("Bad file found!");
4661 fnumfiles
= int(len(zipfp
.infolist()));
4662 catver
= formatspecs
[6];
4663 fileheaderver
= str(int(catver
.replace(".", "")));
4664 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4665 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4666 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4667 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4668 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4669 if(checksumtype
=="none" or checksumtype
==""):
4670 catfileheadercshex
= format(0, 'x').lower();
4671 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4672 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4673 elif(checksumtype
=="crc16_ccitt"):
4674 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4675 elif(checksumtype
=="adler32"):
4676 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4677 elif(checksumtype
=="crc32"):
4678 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4679 elif(checksumtype
=="crc64_ecma"):
4680 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4681 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4682 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4683 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4684 checksumoutstr
= hashlib
.new(checksumtype
);
4685 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4686 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4688 catfileheadercshex
= format(0, 'x').lower();
4689 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4690 fheadtell
= len(fileheader
);
4691 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4692 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
4693 if(re
.findall("^[.|/]", member
.filename
)):
4694 fname
= member
.filename
;
4696 fname
= "./"+member
.filename
;
4697 zipinfo
= zipfp
.getinfo(member
.filename
);
4699 VerbosePrintOut(fname
);
4700 if(not member
.is_dir()):
4701 fpremode
= stat
.S_IFREG
+ 438;
4702 elif(member
.is_dir()):
4703 fpremode
= stat
.S_IFDIR
+ 511;
4706 if(not member
.is_dir()):
4708 elif(member
.is_dir()):
4711 fbasedir
= os
.path
.dirname(fname
);
4715 curfid
= curfid
+ 1;
4723 fsize
= member
.file_size
;
4725 fsize
= member
.file_size
;
4726 fatime
= time
.mktime(member
.date_time
+ (0, 0, -1));
4727 fmtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
4728 fctime
= time
.mktime(member
.date_time
+ (0, 0, -1));
4729 fbtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
4730 if(not member
.is_dir()):
4731 fmode
= stat
.S_IFREG
+ 438;
4732 fchmode
= stat
.S_IMODE(int(stat
.S_IFREG
+ 438));
4733 ftypemod
= stat
.S_IFMT(int(stat
.S_IFREG
+ 438));
4734 if(member
.is_dir()):
4735 fmode
= stat
.S_IFDIR
+ 511;
4736 fchmode
= stat
.S_IMODE(int(stat
.S_IFDIR
+ 511));
4737 ftypemod
= stat
.S_IFMT(int(stat
.S_IFDIR
+ 511));
4740 except AttributeError:
4746 except AttributeError:
4753 userinfo
= pwd
.getpwuid(os
.getuid());
4754 funame
= userinfo
.pw_name
;
4757 except AttributeError:
4765 groupinfo
= grp
.getgrgid(os
.getgid());
4766 fgname
= groupinfo
.gr_name
;
4769 except AttributeError:
4773 fcontents
= "".encode('UTF-8');
4775 fcontents
= zipfp
.read(member
.filename
);
4776 ftypehex
= format(ftype
, 'x').lower();
4777 extrafields
= len(extradata
);
4778 extrafieldslist
= extradata
;
4779 catfextrafields
= extrafields
;
4780 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
4781 if(len(extradata
)>0):
4782 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4783 extrasizelen
= len(extrasizestr
);
4784 extrasizelenhex
= format(extrasizelen
, 'x').lower();
4785 catfileoutstr
= AppendNullBytes([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(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()], formatspecs
[5]);
4786 if(len(extradata
)>0):
4787 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4788 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
4789 catfnumfields
= 24 + catfextrafields
;
4790 if(checksumtype
=="none" or checksumtype
==""):
4791 catfileheadercshex
= format(0, 'x').lower();
4792 catfilecontentcshex
= format(0, 'x').lower();
4793 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4794 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
4795 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
4796 elif(checksumtype
=="crc16_ccitt"):
4797 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
4798 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
4799 elif(checksumtype
=="adler32"):
4800 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4801 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
4802 elif(checksumtype
=="crc32"):
4803 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4804 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
4805 elif(checksumtype
=="crc64_ecma"):
4806 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4807 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
4808 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4809 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4810 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
4811 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4812 checksumoutstr
= hashlib
.new(checksumtype
);
4813 checksumoutstr
.update("".encode('UTF-8'));
4814 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4815 checksumoutstr
= hashlib
.new(checksumtype
);
4816 checksumoutstr
.update(fcontents
);
4817 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
4819 catfileheadercshex
= format(0, 'x').lower();
4820 catfilecontentcshex
= format(0, 'x').lower();
4821 catfhstart
= fheadtell
;
4822 fheadtell
+= len(catfileoutstr
);
4823 catfhend
= fheadtell
- 1;
4824 catfcontentstart
= fheadtell
;
4825 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4826 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
4827 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
4828 if(checksumtype
=="none" or checksumtype
==""):
4829 catfileheadercshex
= format(0, 'x').lower();
4830 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4831 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4832 elif(checksumtype
=="crc16_ccitt"):
4833 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4834 elif(checksumtype
=="adler32"):
4835 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4836 elif(checksumtype
=="crc32"):
4837 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4838 elif(checksumtype
=="crc64_ecma"):
4839 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4840 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4841 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4842 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4843 checksumoutstr
= hashlib
.new(checksumtype
);
4844 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
4845 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4847 catfileheadercshex
= format(0, 'x').lower();
4848 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4849 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
4850 nullstrecd
= formatspecs
[5].encode('UTF-8');
4851 fheadtell
+= len(catfileoutstr
) + 1;
4852 catfcontentend
= fheadtell
- 1;
4853 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
4854 pyhascontents
= False;
4855 if(int(fsize
)>0 and not listonly
):
4856 pyhascontents
= True;
4857 if(int(fsize
)>0 and listonly
):
4859 pyhascontents
= False;
4860 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
, '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
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
4861 fileidnum
= fileidnum
+ 1;
4864 if(not rarfile_support
):
4865 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4868 if(rarfile_support
):
4869 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4870 advancedlist
= True;
4876 inodetocatinode
= {};
4878 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
4880 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
4882 rarfp
= rarfile
.RarFile(infile
, "r");
4883 rartest
= rarfp
.testrar();
4885 VerbosePrintOut("Bad file found!");
4886 fnumfiles
= int(len(rarfp
.infolist()));
4887 catver
= formatspecs
[6];
4888 fileheaderver
= str(int(catver
.replace(".", "")));
4889 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4890 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4891 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4892 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4893 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4894 if(checksumtype
=="none" or checksumtype
==""):
4895 catfileheadercshex
= format(0, 'x').lower();
4896 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4897 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4898 elif(checksumtype
=="crc16_ccitt"):
4899 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4900 elif(checksumtype
=="adler32"):
4901 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4902 elif(checksumtype
=="crc32"):
4903 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4904 elif(checksumtype
=="crc64_ecma"):
4905 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4906 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4907 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4908 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4909 checksumoutstr
= hashlib
.new(checksumtype
);
4910 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4911 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4913 catfileheadercshex
= format(0, 'x').lower();
4914 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4915 fheadtell
= len(fileheader
);
4916 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4917 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
4920 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
4923 member
.external_attr
4925 except AttributeError:
4927 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
4930 member
.external_attr
4932 except AttributeError:
4937 if(re
.findall("^[.|/]", member
.filename
)):
4938 fname
= member
.filename
;
4940 fname
= "./"+member
.filename
;
4941 rarinfo
= rarfp
.getinfo(member
.filename
);
4943 VerbosePrintOut(fname
);
4944 if(is_unix
and member
.external_attr
!=0):
4945 fpremode
= int(member
.external_attr
);
4946 elif(member
.is_file()):
4947 fpremode
= stat
.S_IFREG
+ 438;
4948 elif(member
.is_symlink()):
4949 fpremode
= stat
.S_IFLNK
+ 438;
4950 elif(member
.is_dir()):
4951 fpremode
= stat
.S_IFDIR
+ 511;
4952 if(is_windows
and member
.external_attr
!=0):
4953 fwinattributes
= int(member
.external_attr
);
4955 fwinattributes
= int(0);
4958 if(member
.is_file()):
4960 elif(member
.is_symlink()):
4962 elif(member
.is_dir()):
4966 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
4967 fbasedir
= os
.path
.dirname(fname
);
4971 curfid
= curfid
+ 1;
4979 fsize
= member
.file_size
;
4982 fatime
= int(member
.atime
.timestamp());
4984 fatime
= int(member
.mtime
.timestamp());
4985 except AttributeError:
4986 fatime
= int(member
.mtime
.timestamp());
4987 fmtime
= int(member
.mtime
.timestamp());
4990 fctime
= int(member
.ctime
.timestamp());
4992 fctime
= int(member
.mtime
.timestamp());
4993 except AttributeError:
4994 fctime
= int(member
.mtime
.timestamp());
4995 fbtime
= int(member
.mtime
.timestamp());
4996 if(is_unix
and member
.external_attr
!=0):
4997 fmode
= format(int(member
.external_attr
), 'x').lower();
4998 fchmode
= format(int(stat
.S_IMODE(member
.external_attr
)), 'x').lower();
4999 ftypemod
= format(int(stat
.S_IFMT(member
.external_attr
)), 'x').lower();
5000 elif(member
.is_file()):
5001 fmode
= int(stat
.S_IFREG
+ 438)
5002 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
5003 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
5004 elif(member
.is_symlink()):
5005 fmode
= int(stat
.S_IFLNK
+ 438)
5006 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
5007 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
5008 elif(member
.is_dir()):
5009 fmode
= int(stat
.S_IFDIR
+ 511)
5010 fchmode
= int(stat
.S_IMODE(stat
.S_IFDIR
+ 511));
5011 ftypemod
= int(stat
.S_IFMT(stat
.S_IFDIR
+ 511));
5014 except AttributeError:
5020 except AttributeError:
5027 userinfo
= pwd
.getpwuid(os
.getuid());
5028 funame
= userinfo
.pw_name
;
5031 except AttributeError:
5039 groupinfo
= grp
.getgrgid(os
.getgid());
5040 fgname
= groupinfo
.gr_name
;
5043 except AttributeError:
5047 fcontents
= "".encode('UTF-8');
5049 fcontents
= rarfp
.read(member
.filename
);
5050 ftypehex
= format(ftype
, 'x').lower();
5051 extrafields
= len(extradata
);
5052 extrafieldslist
= extradata
;
5053 catfextrafields
= extrafields
;
5054 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5055 if(len(extradata
)>0):
5056 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5057 extrasizelen
= len(extrasizestr
);
5058 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5059 catfileoutstr
= AppendNullBytes([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(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()], formatspecs
[5]);
5060 if(len(extradata
)>0):
5061 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5062 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5063 catfnumfields
= 24 + catfextrafields
;
5064 if(checksumtype
=="none" or checksumtype
==""):
5065 catfileheadercshex
= format(0, 'x').lower();
5066 catfilecontentcshex
= format(0, 'x').lower();
5067 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5068 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5069 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
5070 elif(checksumtype
=="crc16_ccitt"):
5071 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5072 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
5073 elif(checksumtype
=="adler32"):
5074 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5075 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
5076 elif(checksumtype
=="crc32"):
5077 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5078 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
5079 elif(checksumtype
=="crc64_ecma"):
5080 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5081 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
5082 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5083 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5084 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
5085 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5086 checksumoutstr
= hashlib
.new(checksumtype
);
5087 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5088 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5090 catfileheadercshex
= format(0, 'x').lower();
5091 catfilecontentcshex
= format(0, 'x').lower();
5092 catfhstart
= fheadtell
;
5093 fheadtell
+= len(catfileoutstr
);
5094 catfhend
= fheadtell
- 1;
5095 catfcontentstart
= fheadtell
;
5096 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5097 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
5098 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5099 if(checksumtype
=="none" or checksumtype
==""):
5100 catfileheadercshex
= format(0, 'x').lower();
5101 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5102 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5103 elif(checksumtype
=="crc16_ccitt"):
5104 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5105 elif(checksumtype
=="adler32"):
5106 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5107 elif(checksumtype
=="crc32"):
5108 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5109 elif(checksumtype
=="crc64_ecma"):
5110 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5111 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5112 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5113 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5114 checksumoutstr
= hashlib
.new(checksumtype
);
5115 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5116 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5118 catfileheadercshex
= format(0, 'x').lower();
5119 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5120 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5121 nullstrecd
= formatspecs
[5].encode('UTF-8');
5122 fheadtell
+= len(catfileoutstr
) + 1;
5123 catfcontentend
= fheadtell
- 1;
5124 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
5125 pyhascontents
= False;
5126 if(int(fsize
)>0 and not listonly
):
5127 pyhascontents
= True;
5128 if(int(fsize
)>0 and listonly
):
5130 pyhascontents
= False;
5131 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
, '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
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
5132 fileidnum
= fileidnum
+ 1;
5135 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):
5136 outarray
= BytesIO();
5137 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
5138 listcatfiles
= ArchiveFileToArray(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5139 return listcatfiles
;
5141 def ArchiveFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5142 if(isinstance(infile
, dict)):
5143 listcatfiles
= infile
;
5145 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5146 infile
= RemoveWindowsPath(infile
);
5147 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5148 if(not listcatfiles
):
5150 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': {}}}};
5152 catarray
.update({'catfp': listcatfiles
['catfp']});
5153 lenlist
= len(listcatfiles
['ffilelist']);
5158 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
5161 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5162 lcfx
= listcatfiles
['fnumfiles'];
5164 lcfx
= int(listcatfiles
['fnumfiles']);
5166 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5167 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5168 catarray
['filetoid'].update(filetoidarray
);
5169 catarray
['idtofile'].update(idtofilearray
);
5170 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5171 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5172 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5173 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5174 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5175 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5176 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5177 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5178 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5179 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5180 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5181 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5182 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5183 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5184 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5185 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5186 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5187 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5188 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5189 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5190 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5191 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5192 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5193 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5194 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5195 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5196 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5197 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5198 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5199 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5200 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5204 create_alias_function("", __file_format_name__
, "ToArrayIndex", ArchiveFileToArrayIndex
);
5206 def ListDirToArrayIndexAlt(infiles
, dirlistfromtxt
=False, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5207 listcatfiles
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5208 if(not listcatfiles
):
5210 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': {}}}};
5211 lenlist
= len(listcatfiles
['ffilelist']);
5216 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
5219 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5220 lcfx
= listcatfiles
['fnumfiles'];
5222 lcfx
= int(listcatfiles
['fnumfiles']);
5224 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5225 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5226 catarray
['filetoid'].update(filetoidarray
);
5227 catarray
['idtofile'].update(idtofilearray
);
5228 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5229 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5230 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5231 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5232 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5233 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5234 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5235 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5236 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5237 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5238 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5239 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5240 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5241 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5242 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5243 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5244 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5245 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5246 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5247 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5248 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5249 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5250 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5251 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5252 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5253 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5254 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5255 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5256 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5257 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5258 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5262 def TarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5263 listcatfiles
= TarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5264 if(not listcatfiles
):
5266 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': {}}}};
5267 lenlist
= len(listcatfiles
['ffilelist']);
5272 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
5275 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5276 lcfx
= listcatfiles
['fnumfiles'];
5278 lcfx
= int(listcatfiles
['fnumfiles']);
5280 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5281 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5282 catarray
['filetoid'].update(filetoidarray
);
5283 catarray
['idtofile'].update(idtofilearray
);
5284 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5285 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5286 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5287 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5288 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5289 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5290 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5291 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5292 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5293 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5294 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5295 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5296 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5297 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5298 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5299 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5300 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5301 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5302 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5303 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5304 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5305 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5306 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5307 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5308 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5309 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5310 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5311 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5312 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5313 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5314 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5318 def ZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5319 listcatfiles
= ZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5320 if(not listcatfiles
):
5322 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': {}}}};
5323 lenlist
= len(listcatfiles
['ffilelist']);
5328 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
5331 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5332 lcfx
= listcatfiles
['fnumfiles'];
5334 lcfx
= int(listcatfiles
['fnumfiles']);
5336 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5337 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5338 catarray
['filetoid'].update(filetoidarray
);
5339 catarray
['idtofile'].update(idtofilearray
);
5340 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5341 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5342 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5343 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5344 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5345 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5346 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5347 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5348 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5349 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5350 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5351 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5352 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5353 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5354 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5355 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5356 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5357 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5358 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5359 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5360 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5361 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5362 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5363 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5364 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5365 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5366 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5367 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5368 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5369 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5370 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5374 if(not rarfile_support
):
5375 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5378 if(rarfile_support
):
5379 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5380 listcatfiles
= RarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5381 if(not listcatfiles
):
5383 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': {}}}};
5384 lenlist
= len(listcatfiles
['ffilelist']);
5389 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
5392 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5393 lcfx
= listcatfiles
['fnumfiles'];
5395 lcfx
= int(listcatfiles
['fnumfiles']);
5397 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5398 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5399 catarray
['filetoid'].update(filetoidarray
);
5400 catarray
['idtofile'].update(idtofilearray
);
5401 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5402 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5403 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5404 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5405 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5406 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5407 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5408 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5409 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5410 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5411 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5412 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5413 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5414 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5415 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5416 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5417 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5418 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5419 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5420 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5421 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5422 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5423 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5424 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5425 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5426 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5427 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5428 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5429 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5430 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5431 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5435 def ArchiveFileStringToArrayIndex(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5436 catfp
= BytesIO(catstr
);
5437 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5438 return listcatfiles
;
5440 create_alias_function("", __file_format_name__
, "StringToArrayIndex", ArchiveFileStringToArrayIndex
);
5442 def TarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5444 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5445 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5446 return listcatfiles
;
5448 def ZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5450 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5451 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5452 return listcatfiles
;
5454 if(not rarfile_support
):
5455 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5458 if(rarfile_support
):
5459 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5461 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5462 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5463 return listcatfiles
;
5465 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):
5466 outarray
= BytesIO();
5467 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, verbose
, True);
5468 listcatfiles
= ArchiveFileToArrayIndex(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
)
5469 return listcatfiles
;
5471 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):
5472 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
5473 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzop', 'lzo', 'lzma', 'xz'];
5474 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', 'lzop', '.lzo', '.lzma', '.xz'];
5475 if(isinstance(infile
, dict)):
5476 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5477 listcatfiles
= prelistcatfiles
['list'];
5479 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5480 infile
= RemoveWindowsPath(infile
);
5482 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5483 listcatfiles
= prelistcatfiles
['list'];
5485 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5486 if(outfile
!="-" and not hasattr(infile
, "read") and not hasattr(outfile
, "write")):
5487 outfile
= RemoveWindowsPath(outfile
);
5488 checksumtype
= checksumtype
.lower();
5489 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
5490 checksumtype
="crc32";
5491 if(checksumtype
=="none"):
5493 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
5495 if(compression
not in compressionlist
and compression
is None):
5496 compression
= "auto";
5498 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5499 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
5500 if(os
.path
.exists(outfile
)):
5502 if(not listcatfiles
):
5507 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
5509 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
5512 fbasename
= os
.path
.splitext(outfile
)[0];
5513 fextname
= os
.path
.splitext(outfile
)[1];
5514 catfp
= CompressOpenFile(outfile
, compressionlevel
);
5515 catver
= formatspecs
[6];
5516 fileheaderver
= str(int(catver
.replace(".", "")));
5517 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5518 catfp
.write(fileheader
.encode('UTF-8'));
5519 lenlist
= len(listcatfiles
['ffilelist']);
5520 fnumfiles
= int(listcatfiles
['fnumfiles']);
5521 if(lenlist
>fnumfiles
or lenlist
<fnumfiles
):
5522 fnumfiles
= lenlist
;
5523 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5524 fnumfilesa
= AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
5525 if(checksumtype
=="none" or checksumtype
==""):
5526 catfileheadercshex
= format(0, 'x').lower();
5527 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5528 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
5529 elif(checksumtype
=="crc16_ccitt"):
5530 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
5531 elif(checksumtype
=="adler32"):
5532 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
5533 elif(checksumtype
=="crc32"):
5534 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
5535 elif(checksumtype
=="crc64_ecma"):
5536 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5537 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5538 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5539 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5540 checksumoutstr
= hashlib
.new(checksumtype
);
5541 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
5542 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5544 catfileheadercshex
= format(0, 'x').lower();
5545 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
5546 catfp
.write(fnumfilesa
.encode('UTF-8'));
5549 os
.fsync(catfp
.fileno());
5550 except io
.UnsupportedOperation
:
5552 except AttributeError:
5558 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
5561 lcfx
= int(listcatfiles
['fnumfiles']);
5569 catfhstart
= catfp
.tell();
5570 if(re
.findall("^[.|/]", listcatfiles
['ffilelist'][reallcfi
]['fname'])):
5571 fname
= listcatfiles
['ffilelist'][reallcfi
]['fname'];
5573 fname
= "./"+listcatfiles
['ffilelist'][reallcfi
]['fname'];
5575 VerbosePrintOut(fname
);
5576 fheadersize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fheadersize']), 'x').lower();
5577 fsize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fsize']), 'x').lower();
5578 flinkname
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
5579 fatime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fatime']), 'x').lower();
5580 fmtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmtime']), 'x').lower();
5581 fctime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fctime']), 'x').lower();
5582 fbtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fbtime']), 'x').lower();
5583 fmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmode']), 'x').lower();
5584 fchmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fchmode']), 'x').lower();
5585 fuid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fuid']), 'x').lower();
5586 funame
= listcatfiles
['ffilelist'][reallcfi
]['funame'];
5587 fgid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fgid']), 'x').lower();
5588 fgname
= listcatfiles
['ffilelist'][reallcfi
]['fgname'];
5589 finode
= listcatfiles
['ffilelist'][reallcfi
]['finode'];
5590 flinkcount
= listcatfiles
['ffilelist'][reallcfi
]['flinkcount'];
5591 fdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fminor']), 'x').lower();
5592 fdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmajor']), 'x').lower();
5593 frdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frminor']), 'x').lower();
5594 frdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frmajor']), 'x').lower();
5595 if(len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] and len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>0):
5596 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
5597 if(len(extradata
) > 0):
5598 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(extradata
);
5599 listcatfiles
['ffilelist'][reallcfi
]['fextralist'] = extradata
;
5600 extrafields
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']), 'x').lower();
5601 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5602 if(len(extradata
)>0):
5603 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5604 extrasizelen
= format(len(extrasizestr
), 'x').lower();
5605 fcontents
= listcatfiles
['ffilelist'][reallcfi
]['fcontents'];
5607 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']==1 or listcatfiles
['ffilelist'][reallcfi
]['ftype']==2):
5608 getflinkpath
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
5609 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
5610 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
5611 fheadersize
= format(int(flinkinfo
['fheadersize']), 'x').lower();
5612 fsize
= format(int(flinkinfo
['fsize']), 'x').lower();
5613 flinkname
= flinkinfo
['flinkname'];
5614 fatime
= format(int(flinkinfo
['fatime']), 'x').lower();
5615 fmtime
= format(int(flinkinfo
['fmtime']), 'x').lower();
5616 fctime
= format(int(flinkinfo
['fctime']), 'x').lower();
5617 fbtime
= format(int(flinkinfo
['fbtime']), 'x').lower();
5618 fmode
= format(int(flinkinfo
['fmode']), 'x').lower();
5619 fchmode
= format(int(flinkinfo
['fchmode']), 'x').lower();
5620 fuid
= format(int(flinkinfo
['fuid']), 'x').lower();
5621 funame
= flinkinfo
['funame'];
5622 fgid
= format(int(flinkinfo
['fgid']), 'x').lower();
5623 fgname
= flinkinfo
['fgname'];
5624 finode
= flinkinfo
['finode'];
5625 flinkcount
= flinkinfo
['flinkcount'];
5626 fdev_minor
= format(int(flinkinfo
['fminor']), 'x').lower();
5627 fdev_major
= format(int(flinkinfo
['fmajor']), 'x').lower();
5628 frdev_minor
= format(int(flinkinfo
['frminor']), 'x').lower();
5629 frdev_major
= format(int(flinkinfo
['frmajor']), 'x').lower();
5630 if(len(flinkinfo
['fextralist'])>flinkinfo
['fextrafields'] and len(flinkinfo
['fextralist'])>0):
5631 flinkinfo
['fextrafields'] = len(flinkinfo
['fextralist']);
5632 if(len(extradata
) > 0):
5633 flinkinfo
['fextrafields'] = len(extradata
);
5634 flinkinfo
['fextralist'] = extradata
;
5635 extrafields
= format(int(flinkinfo
['fextrafields']), 'x').lower();
5636 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5637 if(len(extradata
)>0):
5638 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5639 extrasizelen
= format(len(extrasizestr
), 'x').lower();
5640 fcontents
= flinkinfo
['fcontents'];
5641 if(flinkinfo
['ftype']!=0 and flinkinfo
['ftype']!=7):
5643 fcontents
= fcontents
.encode('UTF-8');
5644 except AttributeError:
5646 ftypehex
= format(flinkinfo
['ftype'], 'x').lower();
5648 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=0 and listcatfiles
['ffilelist'][reallcfi
]['ftype']!=7):
5650 fcontents
= fcontents
.encode('UTF-8');
5651 except AttributeError:
5653 ftypehex
= format(listcatfiles
['ffilelist'][reallcfi
]['ftype'], 'x').lower();
5654 fcurfid
= format(curfid
, 'x').lower();
5655 if(not followlink
and finode
!=0):
5656 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=1):
5657 fcurinode
= format(int(curinode
), 'x').lower();
5658 inodetofile
.update({curinode
: fname
});
5659 filetoinode
.update({fname
: curinode
});
5660 curinode
= curinode
+ 1;
5662 fcurinode
= format(int(filetoinode
[flinkname
]), 'x').lower();
5664 fcurinode
= format(int(curinode
), 'x').lower();
5665 curinode
= curinode
+ 1;
5666 curfid
= curfid
+ 1;
5667 catfileoutstr
= AppendNullBytes([ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
], formatspecs
[5]);
5668 if(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']>0):
5669 extrafieldslist
= [];
5671 exil
= listcatfiles
['ffilelist'][reallcfi
]['fextrafields'];
5673 extrafieldslist
.append(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
5675 catfileoutstr
+= AppendNullBytes([extrafieldslist
], formatspecs
[5]);
5676 catfileoutstr
+= AppendNullBytes([checksumtype
], formatspecs
[5]);
5677 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
5678 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
5679 if(checksumtype
=="none" or checksumtype
==""):
5680 catfileheadercshex
= format(0, 'x').lower();
5681 catfilecontentcshex
= format(0, 'x').lower();
5682 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5683 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5684 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
5685 elif(checksumtype
=="crc16_ccitt"):
5686 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5687 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
5688 elif(checksumtype
=="adler32"):
5689 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5690 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
5691 elif(checksumtype
=="crc32"):
5692 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5693 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
5694 elif(checksumtype
=="crc64_ecma"):
5695 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5696 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
5697 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5698 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5699 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
5700 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5701 checksumoutstr
= hashlib
.new(checksumtype
);
5702 checksumoutstr
.update("".encode('UTF-8'));
5703 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5704 checksumoutstr
= hashlib
.new(checksumtype
);
5705 checksumoutstr
.update(fcontents
);
5706 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
5708 catfileheadercshex
= format(0, 'x').lower();
5709 catfilecontentcshex
= format(0, 'x').lower();
5710 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5711 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
5712 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5713 if(checksumtype
=="none" or checksumtype
==""):
5714 catfileheadercshex
= format(0, 'x').lower();
5715 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5716 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5717 elif(checksumtype
=="crc16_ccitt"):
5718 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5719 elif(checksumtype
=="adler32"):
5720 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5721 elif(checksumtype
=="crc32"):
5722 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5723 elif(checksumtype
=="crc64_ecma"):
5724 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5725 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5726 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5727 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5728 checksumoutstr
= hashlib
.new(checksumtype
);
5729 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5730 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5732 catfileheadercshex
= format(0, 'x').lower();
5733 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5734 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5735 nullstrecd
= formatspecs
[5].encode('UTF-8');
5736 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
5737 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
5738 catfp
.write(catfileout
);
5741 os
.fsync(catfp
.fileno());
5742 except io
.UnsupportedOperation
:
5744 except AttributeError:
5747 reallcfi
= reallcfi
+ 1;
5748 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
5749 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
5752 os
.fsync(catfp
.fileno());
5753 except io
.UnsupportedOperation
:
5755 except AttributeError:
5759 if(hasattr(sys
.stdout
, "buffer")):
5760 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
5762 shutil
.copyfileobj(catfp
, sys
.stdout
);
5763 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
5764 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
5766 upload_file_to_internet_file(catfp
, outfile
);
5774 create_alias_function("RePack", __file_format_name__
, "", RePackArchiveFile
);
5776 def RePackArchiveFileFromString(catstr
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5777 catfp
= BytesIO(catstr
);
5778 listcatfiles
= RePackArchiveFile(catfp
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
5779 return listcatfiles
;
5781 create_alias_function("RePack", __file_format_name__
, "FromString", RePackArchiveFileFromString
);
5783 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):
5784 outarray
= BytesIO();
5785 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
5786 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
5787 return listcatfiles
;
5789 create_alias_function("Pack", __file_format_name__
, "FromListDir", PackArchiveFileFromListDir
);
5791 def ArchiveFileArrayBase64Encode(infile
, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5793 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5794 if(isinstance(infile
, dict)):
5795 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5796 listcatfiles
= prelistcatfiles
['list'];
5798 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5799 infile
= RemoveWindowsPath(infile
);
5801 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5802 listcatfiles
= prelistcatfiles
['list'];
5804 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5805 if(not listcatfiles
):
5807 lenlist
= len(listcatfiles
['ffilelist']);
5812 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
5815 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5816 lcfx
= listcatfiles
['fnumfiles'];
5818 lcfx
= int(listcatfiles
['fnumfiles']);
5819 if(lenlist
>lcfx
or lenlist
<lcfx
):
5822 if(listcatfiles
['ffilelist'][lcfi
]['fhascontents']):
5823 listcatfiles
['ffilelist'][lcfi
]['fcontents'] = base64
.b64encode(listcatfiles
['ffilelist'][lcfi
]['fcontents']).decode("UTF-8");
5825 return listcatfiles
;
5827 create_alias_function("", __file_format_name__
, "ArrayBase64Encode", ArchiveFileArrayBase64Encode
);
5829 def ArchiveFileArrayBase64Decode(infile
, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5831 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5832 if(isinstance(infile
, dict)):
5833 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5834 listcatfiles
= prelistcatfiles
['list'];
5836 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5837 infile
= RemoveWindowsPath(infile
);
5839 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5840 listcatfiles
= prelistcatfiles
['list'];
5842 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5843 if(not listcatfiles
):
5845 lenlist
= len(listcatfiles
['ffilelist']);
5850 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
5853 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5854 lcfx
= listcatfiles
['fnumfiles'];
5856 lcfx
= int(listcatfiles
['fnumfiles']);
5857 if(lenlist
>lcfx
or lenlist
<lcfx
):
5860 if(listcatfiles
['ffilelist'][lcfi
]['fhascontents']):
5861 listcatfiles
['ffilelist'][lcfi
]['fcontents'] = base64
.b64decode(listcatfiles
['ffilelist'][lcfi
]['fcontents'].encode("UTF-8"));
5863 return listcatfiles
;
5865 create_alias_function("", __file_format_name__
, "ArrayBase64Decode", ArchiveFileArrayBase64Decode
);
5867 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):
5868 if(outdir
is not None):
5869 outdir
= RemoveWindowsPath(outdir
);
5871 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5872 if(isinstance(infile
, dict)):
5873 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5874 listcatfiles
= prelistcatfiles
['list'];
5876 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5877 infile
= RemoveWindowsPath(infile
);
5879 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5880 listcatfiles
= prelistcatfiles
['list'];
5882 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5883 if(not listcatfiles
):
5885 lenlist
= len(listcatfiles
['ffilelist']);
5890 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
5893 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5894 lcfx
= listcatfiles
['fnumfiles'];
5896 lcfx
= int(listcatfiles
['fnumfiles']);
5897 if(lenlist
>lcfx
or lenlist
<lcfx
):
5904 userinfo
= pwd
.getpwuid(listcatfiles
['ffilelist'][lcfi
]['fuid']);
5905 funame
= userinfo
.pw_name
;
5914 groupinfo
= grp
.getgrgid(listcatfiles
['ffilelist'][lcfi
]['fgid']);
5915 fgname
= groupinfo
.gr_name
;
5921 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
5922 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5923 with
open(listcatfiles
['ffilelist'][lcfi
]['fname'], "wb") as fpc
:
5924 fpc
.write(listcatfiles
['ffilelist'][lcfi
]['fcontents'])
5927 os
.fsync(fpc
.fileno())
5928 except io
.UnsupportedOperation
:
5930 except AttributeError:
5932 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
5933 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
5934 if(preservepermissions
):
5935 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fchmode']);
5937 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
5938 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5940 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
5941 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
5942 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
5947 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
5948 funame
= userinfo
.pw_name
;
5957 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
5958 fgname
= groupinfo
.gr_name
;
5963 if(flinkinfo
['ftype'] == 0 or flinkinfo
['ftype'] == 7):
5964 with
open(listcatfiles
['ffilelist'][lcfi
]['fname'], "wb") as fpc
:
5965 fpc
.write(flinkinfo
['fcontents'])
5968 os
.fsync(fpc
.fileno())
5969 except io
.UnsupportedOperation
:
5971 except AttributeError:
5973 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
5974 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
5975 if(preservepermissions
):
5976 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5978 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
5979 if(flinkinfo
['ftype']==1):
5980 os
.link(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
5981 if(flinkinfo
['ftype']==2):
5982 os
.symlink(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
5983 if(flinkinfo
['ftype']==5):
5984 if(preservepermissions
):
5985 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5987 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname']);
5988 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
5989 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
5990 if(preservepermissions
):
5991 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5993 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
5994 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
5995 os
.mkfifo(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5997 os
.link(listcatfiles
['ffilelist'][lcfi
]['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
5998 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6000 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6001 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
6002 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
6007 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
6008 funame
= userinfo
.pw_name
;
6017 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
6018 fgname
= groupinfo
.gr_name
;
6023 if(flinkinfo
['ftype']==0 or flinkinfo
['ftype']==7):
6024 with
open(listcatfiles
['ffilelist'][lcfi
]['fname'], "wb") as fpc
:
6025 fpc
.write(flinkinfo
['fcontents'])
6028 os
.fsync(fpc
.fileno())
6029 except io
.UnsupportedOperation
:
6031 except AttributeError:
6033 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6034 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
6035 if(preservepermissions
):
6036 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
6038 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6039 if(flinkinfo
['ftype']==1):
6040 os
.link(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
6041 if(flinkinfo
['ftype']==2):
6042 os
.symlink(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
6043 if(flinkinfo
['ftype']==5):
6044 if(preservepermissions
):
6045 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
6047 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname']);
6048 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6049 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
6050 if(preservepermissions
):
6051 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
6053 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6054 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
6055 os
.mkfifo(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
6057 os
.symlink(listcatfiles
['ffilelist'][lcfi
]['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
6058 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6059 if(preservepermissions
):
6060 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6062 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname']);
6063 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
6064 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
6065 if(preservepermissions
):
6066 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6068 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
6069 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6 and hasattr(os
, "mkfifo")):
6070 os
.mkfifo(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6073 return listcatfiles
['ffilelist']['catfp'];
6077 create_alias_function("UnPack", __file_format_name__
, "", UnPackArchiveFile
);
6079 if(hasattr(shutil
, "register_unpack_format")):
6080 def UnPackArchiveFileFunc(archive_name
, extract_dir
=None, **kwargs
):
6081 return UnPackArchiveFile(archive_name
, extract_dir
, False, 0, 0, False, __file_format_delimiter__
, False, False);
6082 create_alias_function("UnPack", __file_format_name__
, "Func", UnPackArchiveFileFunc
);
6084 def UnPackArchiveFileString(catstr
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6085 catfp
= BytesIO(catstr
);
6086 listcatfiles
= UnPackArchiveFile(catfp
, outdir
, followlink
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6087 return listcatfiles
;
6089 create_alias_function("UnPack", __file_format_name__
, "String", UnPackArchiveFileString
);
6091 def ArchiveFileListFiles(infile
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6092 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6093 if(isinstance(infile
, dict)):
6094 listcatfiles
= infile
;
6096 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6097 infile
= RemoveWindowsPath(infile
);
6098 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, True, skipchecksum
, formatspecs
, returnfp
);
6099 if(not listcatfiles
):
6101 lenlist
= len(listcatfiles
['ffilelist']);
6106 returnval
.update({lcfi
: listcatfiles
['ffilelist'][lcfi
]['fname']});
6108 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
6110 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' } };
6111 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'];
6112 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6113 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " link to " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6114 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6115 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " -> " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6116 fuprint
= listcatfiles
['ffilelist'][lcfi
]['funame'];
6117 if(len(fuprint
)<=0):
6118 fuprint
= listcatfiles
['ffilelist'][lcfi
]['fuid'];
6119 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgname'];
6120 if(len(fgprint
)<=0):
6121 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgid'];
6122 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
));
6125 return listcatfiles
['catfp'];
6129 create_alias_function("", __file_format_name__
, "ListFiles", ArchiveFileListFiles
);
6131 def ArchiveFileStringListFiles(catstr
, followlink
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6132 catfp
= BytesIO(catstr
);
6133 listcatfiles
= UnPackArchiveFile(catfp
, None, followlink
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6134 return listcatfiles
;
6136 create_alias_function("", __file_format_name__
, "StringListFiles", ArchiveFileListFiles
);
6138 def TarFileListFiles(infile
, verbose
=False, returnfp
=False):
6139 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6142 if(hasattr(sys
.stdin
, "buffer")):
6143 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
6145 shutil
.copyfileobj(sys
.stdin
, infile
);
6150 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
6151 infile
= download_file_from_internet_file(infile
);
6156 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6158 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
6160 if(not tarfile
.is_tarfile(infile
)):
6162 except AttributeError:
6163 if(not is_tarfile(infile
)):
6168 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
6169 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
6171 tarfp
= tarfile
.open(infile
, "r");
6172 except FileNotFoundError
:
6174 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
6175 returnval
.update({lcfi
: member
.name
});
6176 fpremode
= member
.mode
;
6177 ffullmode
= member
.mode
;
6181 ffullmode
= member
.mode
+ stat
.S_IFREG
;
6183 elif(member
.isdev()):
6184 ffullmode
= member
.mode
;
6186 elif(member
.islnk()):
6187 ffullmode
= member
.mode
+ stat
.S_IFREG
;
6189 elif(member
.issym()):
6190 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
6192 elif(member
.ischr()):
6193 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
6195 elif(member
.isblk()):
6196 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
6198 elif(member
.isdir()):
6199 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
6201 elif(member
.isfifo()):
6202 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
6204 elif(member
.issparse()):
6205 ffullmode
= member
.mode
;
6208 VerbosePrintOut(member
.name
);
6210 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' } };
6211 printfname
= member
.name
;
6213 printfname
= member
.name
+ " link to " + member
.linkname
;
6214 elif(member
.issym()):
6215 printfname
= member
.name
+ " -> " + member
.linkname
;
6216 fuprint
= member
.uname
;
6217 if(len(fuprint
)<=0):
6218 fuprint
= member
.uid
;
6219 fgprint
= member
.gname
;
6220 if(len(fgprint
)<=0):
6221 fgprint
= member
.gid
;
6222 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
));
6225 return listcatfiles
['catfp'];
6229 def ZipFileListFiles(infile
, verbose
=False, returnfp
=False):
6230 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6233 if(hasattr(sys
.stdin
, "buffer")):
6234 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
6236 shutil
.copyfileobj(sys
.stdin
, infile
);
6241 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
6242 infile
= download_file_from_internet_file(infile
);
6247 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6251 if(not zipfile
.is_zipfile(infile
)):
6254 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
6255 except FileNotFoundError
:
6259 ziptest
= zipfp
.testzip();
6261 VerbosePrintOut("Bad file found!");
6262 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
6263 if(not member
.is_dir()):
6264 fpremode
= int(stat
.S_IFREG
+ 438);
6265 elif(member
.is_dir()):
6266 fpremode
= int(stat
.S_IFDIR
+ 511);
6267 if(not member
.is_dir()):
6268 fmode
= int(stat
.S_IFREG
+ 438);
6269 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
6270 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
6271 elif(member
.is_dir()):
6272 fmode
= int(stat
.S_IFDIR
+ 511);
6273 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
6274 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
6275 returnval
.update({lcfi
: member
.filename
});
6277 VerbosePrintOut(member
.filename
);
6279 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' } };
6281 for fmodval
in str(oct(fmode
))[-3:]:
6282 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
6283 if(not member
.is_dir()):
6285 permissionstr
= "-" + permissionstr
;
6286 elif(member
.is_dir()):
6288 permissionstr
= "d" + permissionstr
;
6289 printfname
= member
.filename
;
6291 fuid
= int(os
.getuid());
6292 except AttributeError:
6297 fgid
= int(os
.getgid());
6298 except AttributeError:
6305 userinfo
= pwd
.getpwuid(os
.getuid());
6306 funame
= userinfo
.pw_name
;
6309 except AttributeError:
6317 groupinfo
= grp
.getgrgid(os
.getgid());
6318 fgname
= groupinfo
.gr_name
;
6321 except AttributeError:
6326 if(len(fuprint
)<=0):
6327 fuprint
= str(fuid
);
6329 if(len(fgprint
)<=0):
6330 fgprint
= str(fgid
);
6331 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
));
6334 return listcatfiles
['catfp'];
6338 if(not rarfile_support
):
6339 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
6340 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6341 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6344 if(rarfile_support
):
6345 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
6346 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6347 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6349 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
6353 rarfp
= rarfile
.RarFile(infile
, "r");
6354 rartest
= rarfp
.testrar();
6356 VerbosePrintOut("Bad file found!");
6357 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
6360 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
6363 member
.external_attr
6365 except AttributeError:
6367 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
6370 member
.external_attr
6372 except AttributeError:
6377 if(is_unix
and member
.external_attr
!=0):
6378 fpremode
= int(member
.external_attr
);
6379 elif(member
.is_file()):
6380 fpremode
= int(stat
.S_IFREG
+ 438);
6381 elif(member
.is_symlink()):
6382 fpremode
= int(stat
.S_IFLNK
+ 438);
6383 elif(member
.is_dir()):
6384 fpremode
= int(stat
.S_IFDIR
+ 511);
6385 if(is_windows
and member
.external_attr
!=0):
6386 fwinattributes
= int(member
.external_attr
);
6388 fwinattributes
= int(0);
6389 if(is_unix
and member
.external_attr
!=0):
6390 fmode
= int(member
.external_attr
);
6391 fchmode
= int(stat
.S_IMODE(member
.external_attr
));
6392 ftypemod
= int(stat
.S_IFMT(member
.external_attr
));
6393 elif(member
.is_file()):
6394 fmode
= int(stat
.S_IFREG
+ 438);
6395 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
6396 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
6397 elif(member
.is_symlink()):
6398 fmode
= int(stat
.S_IFLNK
+ 438);
6399 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFLNK
+ 438)));
6400 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFLNK
+ 438)));
6401 elif(member
.is_dir()):
6402 fmode
= int(stat
.S_IFDIR
+ 511);
6403 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
6404 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
6405 returnval
.update({lcfi
: member
.filename
});
6407 VerbosePrintOut(member
.filename
);
6409 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' } };
6411 for fmodval
in str(oct(fmode
))[-3:]:
6412 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
6413 if(member
.is_file()):
6415 permissionstr
= "-" + permissionstr
;
6416 printfname
= member
.filename
;
6417 elif(member
.is_symlink()):
6419 permissionstr
= "l" + permissionstr
;
6420 printfname
= member
.name
+ " -> " + member
.read().decode("UTF-8");
6421 elif(member
.is_dir()):
6423 permissionstr
= "d" + permissionstr
;
6424 printfname
= member
.filename
;
6426 fuid
= int(os
.getuid());
6427 except AttributeError:
6432 fgid
= int(os
.getgid());
6433 except AttributeError:
6440 userinfo
= pwd
.getpwuid(os
.getuid());
6441 funame
= userinfo
.pw_name
;
6444 except AttributeError:
6452 groupinfo
= grp
.getgrgid(os
.getgid());
6453 fgname
= groupinfo
.gr_name
;
6456 except AttributeError:
6461 if(len(fuprint
)<=0):
6462 fuprint
= str(fuid
);
6464 if(len(fgprint
)<=0):
6465 fgprint
= str(fgid
);
6466 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(member
.file_size
).rjust(15) + " " + member
.mtime
.strftime('%Y-%m-%d %H:%M') + " " + printfname
));
6469 return listcatfiles
['catfp'];
6473 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):
6474 outarray
= BytesIO();
6475 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, False, True);
6476 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6477 return listcatfiles
;
6479 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):
6480 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, formatspecs
, verbose
);
6481 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6482 return listcatfiles
;
6484 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):
6485 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, False, checksumtype
, extradata
, formatspecs
, False);
6486 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, followlink
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
6487 return listcatfiles
;
6489 create_alias_function("Pack", __file_format_name__
, "FromListDirAlt", PackArchiveFileFromListDirAlt
);
6491 def PackArchiveFileFromTarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6492 outarray
= TarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
6493 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
6494 return listcatfiles
;
6496 create_alias_function("Pack", __file_format_name__
, "FromTarFileAlt", PackArchiveFileFromTarFileAlt
);
6498 def PackArchiveFileFromZipFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6499 outarray
= ZipFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
6500 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
6501 return listcatfiles
;
6503 create_alias_function("Pack", __file_format_name__
, "FromZipFileAlt", PackArchiveFileFromZipFileAlt
);
6505 if(not rarfile_support
):
6506 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6509 if(rarfile_support
):
6510 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6511 outarray
= RarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
6512 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
6513 return listcatfiles
;
6515 create_alias_function("Pack", __file_format_name__
, "FromRarFileAlt", PackArchiveFileFromRarFileAlt
);
6517 def download_file_from_ftp_file(url
):
6518 urlparts
= urlparse(url
);
6519 file_name
= os
.path
.basename(urlparts
.path
);
6520 file_dir
= os
.path
.dirname(urlparts
.path
);
6521 if(urlparts
.username
is not None):
6522 ftp_username
= urlparts
.username
;
6524 ftp_username
= "anonymous";
6525 if(urlparts
.password
is not None):
6526 ftp_password
= urlparts
.password
;
6527 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6528 ftp_password
= "anonymous";
6531 if(urlparts
.scheme
=="ftp"):
6533 elif(urlparts
.scheme
=="ftps"):
6537 if(urlparts
.scheme
=="sftp"):
6539 return download_file_from_pysftp_file(url
);
6541 return download_file_from_sftp_file(url
);
6542 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6543 return download_file_from_http_file(url
);
6544 ftp_port
= urlparts
.port
;
6545 if(urlparts
.port
is None):
6548 ftp
.connect(urlparts
.hostname
, ftp_port
);
6549 except socket
.gaierror
:
6550 log
.info("Error With URL "+url
);
6552 except socket
.timeout
:
6553 log
.info("Error With URL "+url
);
6555 ftp
.login(urlparts
.username
, urlparts
.password
);
6556 if(urlparts
.scheme
=="ftps"):
6558 ftpfile
= BytesIO();
6559 ftp
.retrbinary("RETR "+urlparts
.path
, ftpfile
.write
);
6560 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
6565 def download_file_from_ftp_string(url
):
6566 ftpfile
= download_file_from_ftp_file(url
);
6567 return ftpfile
.read();
6569 def upload_file_to_ftp_file(ftpfile
, url
):
6570 urlparts
= urlparse(url
);
6571 file_name
= os
.path
.basename(urlparts
.path
);
6572 file_dir
= os
.path
.dirname(urlparts
.path
);
6573 if(urlparts
.username
is not None):
6574 ftp_username
= urlparts
.username
;
6576 ftp_username
= "anonymous";
6577 if(urlparts
.password
is not None):
6578 ftp_password
= urlparts
.password
;
6579 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6580 ftp_password
= "anonymous";
6583 if(urlparts
.scheme
=="ftp"):
6585 elif(urlparts
.scheme
=="ftps"):
6589 if(urlparts
.scheme
=="sftp"):
6591 return upload_file_from_pysftp_file(url
);
6593 return upload_file_from_sftp_file(url
);
6594 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6596 ftp_port
= urlparts
.port
;
6597 if(urlparts
.port
is None):
6600 ftp
.connect(urlparts
.hostname
, ftp_port
);
6601 except socket
.gaierror
:
6602 log
.info("Error With URL "+url
);
6604 except socket
.timeout
:
6605 log
.info("Error With URL "+url
);
6607 ftp
.login(urlparts
.username
, urlparts
.password
);
6608 if(urlparts
.scheme
=="ftps"):
6610 ftp
.storbinary("STOR "+urlparts
.path
, ftpfile
);
6615 def upload_file_to_ftp_string(ftpstring
, url
):
6616 ftpfileo
= BytesIO(ftpstring
);
6617 ftpfile
= upload_file_to_ftp_file(ftpfileo
, url
);
6621 def download_file_from_http_file(url
, headers
=geturls_headers_pycatfile_python_alt
):
6622 # Parse the URL to extract username and password if present
6623 urlparts
= urlparse(url
);
6624 username
= urlparts
.username
;
6625 password
= urlparts
.password
;
6626 # Rebuild the URL without the username and password
6627 netloc
= urlparts
.hostname
;
6628 if(urlparts
.scheme
=="sftp"):
6630 return download_file_from_pysftp_file(url
);
6632 return download_file_from_sftp_file(url
);
6633 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
6634 return download_file_from_ftp_file(url
);
6636 netloc
+= ':' + str(urlparts
.port
);
6637 rebuilt_url
= urlunparse((urlparts
.scheme
, netloc
, urlparts
.path
, urlparts
.params
, urlparts
.query
, urlparts
.fragment
));
6638 # Create a temporary file object
6639 temp_file
= BytesIO();
6641 # Use the requests library if available
6642 if username
and password
:
6643 response
= requests
.get(rebuilt_url
, headers
=headers
, auth
=(username
, password
), stream
=True);
6645 response
= requests
.get(rebuilt_url
, headers
=headers
, stream
=True);
6646 response
.raw
.decode_content
= True
6647 shutil
.copyfileobj(response
.raw
, temp_file
);
6649 # Build a Request object for urllib
6650 request
= Request(rebuilt_url
, headers
=headers
);
6651 # Create an opener object for handling URLs
6652 if username
and password
:
6653 # Create a password manager
6654 password_mgr
= HTTPPasswordMgrWithDefaultRealm();
6655 # Add the username and password
6656 password_mgr
.add_password(None, rebuilt_url
, username
, password
);
6657 # Create an authentication handler using the password manager
6658 auth_handler
= HTTPBasicAuthHandler(password_mgr
);
6659 # Build the opener with the authentication handler
6660 opener
= build_opener(auth_handler
);
6662 opener
= build_opener();
6663 with opener
.open(request
) as response
:
6664 shutil
.copyfileobj(response
, temp_file
);
6665 # Reset file pointer to the start
6667 # Return the temporary file object
6670 def download_file_from_http_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
6671 httpfile
= download_file_from_http_file(url
, headers
);
6672 return ftpfile
.read();
6675 def download_file_from_sftp_file(url
):
6676 urlparts
= urlparse(url
);
6677 file_name
= os
.path
.basename(urlparts
.path
);
6678 file_dir
= os
.path
.dirname(urlparts
.path
);
6679 sftp_port
= urlparts
.port
;
6680 if(urlparts
.port
is None):
6683 sftp_port
= urlparts
.port
;
6684 if(urlparts
.username
is not None):
6685 sftp_username
= urlparts
.username
;
6687 sftp_username
= "anonymous";
6688 if(urlparts
.password
is not None):
6689 sftp_password
= urlparts
.password
;
6690 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6691 sftp_password
= "anonymous";
6694 if(urlparts
.scheme
=="ftp"):
6695 return download_file_from_ftp_file(url
);
6696 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6697 return download_file_from_http_file(url
);
6698 if(urlparts
.scheme
!="sftp"):
6700 ssh
= paramiko
.SSHClient();
6701 ssh
.load_system_host_keys();
6702 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
6704 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
6705 except paramiko
.ssh_exception
.SSHException
:
6707 except socket
.gaierror
:
6708 log
.info("Error With URL "+url
);
6710 except socket
.timeout
:
6711 log
.info("Error With URL "+url
);
6713 sftp
= ssh
.open_sftp();
6714 sftpfile
= BytesIO();
6715 sftp
.getfo(urlparts
.path
, sftpfile
);
6718 sftpfile
.seek(0, 0);
6721 def download_file_from_sftp_file(url
):
6725 def download_file_from_sftp_string(url
):
6726 sftpfile
= download_file_from_sftp_file(url
);
6727 return sftpfile
.read();
6729 def download_file_from_ftp_string(url
):
6733 def upload_file_to_sftp_file(sftpfile
, url
):
6734 urlparts
= urlparse(url
);
6735 file_name
= os
.path
.basename(urlparts
.path
);
6736 file_dir
= os
.path
.dirname(urlparts
.path
);
6737 sftp_port
= urlparts
.port
;
6738 if(urlparts
.port
is None):
6741 sftp_port
= urlparts
.port
;
6742 if(urlparts
.username
is not None):
6743 sftp_username
= urlparts
.username
;
6745 sftp_username
= "anonymous";
6746 if(urlparts
.password
is not None):
6747 sftp_password
= urlparts
.password
;
6748 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6749 sftp_password
= "anonymous";
6752 if(urlparts
.scheme
=="ftp"):
6753 return upload_file_from_ftp_file(url
);
6754 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6756 if(urlparts
.scheme
!="sftp"):
6758 ssh
= paramiko
.SSHClient();
6759 ssh
.load_system_host_keys();
6760 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
6762 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
6763 except paramiko
.ssh_exception
.SSHException
:
6765 except socket
.gaierror
:
6766 log
.info("Error With URL "+url
);
6768 except socket
.timeout
:
6769 log
.info("Error With URL "+url
);
6771 sftp
= ssh
.open_sftp();
6772 sftp
.putfo(sftpfile
, urlparts
.path
);
6775 sftpfile
.seek(0, 0);
6778 def upload_file_to_sftp_file(sftpfile
, url
):
6782 def upload_file_to_sftp_string(sftpstring
, url
):
6783 sftpfileo
= BytesIO(sftpstring
);
6784 sftpfile
= upload_file_to_sftp_files(ftpfileo
, url
);
6788 def upload_file_to_sftp_string(url
):
6792 def download_file_from_pysftp_file(url
):
6793 urlparts
= urlparse(url
);
6794 file_name
= os
.path
.basename(urlparts
.path
);
6795 file_dir
= os
.path
.dirname(urlparts
.path
);
6796 sftp_port
= urlparts
.port
;
6797 if(urlparts
.port
is None):
6800 sftp_port
= urlparts
.port
;
6801 if(urlparts
.username
is not None):
6802 sftp_username
= urlparts
.username
;
6804 sftp_username
= "anonymous";
6805 if(urlparts
.password
is not None):
6806 sftp_password
= urlparts
.password
;
6807 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6808 sftp_password
= "anonymous";
6811 if(urlparts
.scheme
=="ftp"):
6812 return download_file_from_ftp_file(url
);
6813 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6814 return download_file_from_http_file(url
);
6815 if(urlparts
.scheme
!="sftp"):
6818 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
6819 except paramiko
.ssh_exception
.SSHException
:
6821 except socket
.gaierror
:
6822 log
.info("Error With URL "+url
);
6824 except socket
.timeout
:
6825 log
.info("Error With URL "+url
);
6827 sftp
= ssh
.open_sftp();
6828 sftpfile
= BytesIO();
6829 sftp
.getfo(urlparts
.path
, sftpfile
);
6832 sftpfile
.seek(0, 0);
6835 def download_file_from_pysftp_file(url
):
6839 def download_file_from_pysftp_string(url
):
6840 sftpfile
= download_file_from_pysftp_file(url
);
6841 return sftpfile
.read();
6843 def download_file_from_ftp_string(url
):
6847 def upload_file_to_pysftp_file(sftpfile
, url
):
6848 urlparts
= urlparse(url
);
6849 file_name
= os
.path
.basename(urlparts
.path
);
6850 file_dir
= os
.path
.dirname(urlparts
.path
);
6851 sftp_port
= urlparts
.port
;
6852 if(urlparts
.port
is None):
6855 sftp_port
= urlparts
.port
;
6856 if(urlparts
.username
is not None):
6857 sftp_username
= urlparts
.username
;
6859 sftp_username
= "anonymous";
6860 if(urlparts
.password
is not None):
6861 sftp_password
= urlparts
.password
;
6862 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6863 sftp_password
= "anonymous";
6866 if(urlparts
.scheme
=="ftp"):
6867 return upload_file_from_ftp_file(url
);
6868 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6870 if(urlparts
.scheme
!="sftp"):
6873 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
6874 except paramiko
.ssh_exception
.SSHException
:
6876 except socket
.gaierror
:
6877 log
.info("Error With URL "+url
);
6879 except socket
.timeout
:
6880 log
.info("Error With URL "+url
);
6882 sftp
= ssh
.open_sftp();
6883 sftp
.putfo(sftpfile
, urlparts
.path
);
6886 sftpfile
.seek(0, 0);
6889 def upload_file_to_pysftp_file(sftpfile
, url
):
6893 def upload_file_to_pysftp_string(sftpstring
, url
):
6894 sftpfileo
= BytesIO(sftpstring
);
6895 sftpfile
= upload_file_to_pysftp_files(ftpfileo
, url
);
6899 def upload_file_to_pysftp_string(url
):
6902 def download_file_from_internet_file(url
, headers
=geturls_headers_pycatfile_python_alt
):
6903 urlparts
= urlparse(url
);
6904 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6905 return download_file_from_http_file(url
, headers
);
6906 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
6907 return download_file_from_ftp_file(url
);
6908 elif(urlparts
.scheme
=="sftp"):
6909 if(__use_pysftp__
and havepysftp
):
6910 return download_file_from_pysftp_file(url
);
6912 return download_file_from_sftp_file(url
);
6917 def download_file_from_internet_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
6918 urlparts
= urlparse(url
);
6919 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6920 return download_file_from_http_string(url
, headers
);
6921 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
6922 return download_file_from_ftp_string(url
);
6923 elif(urlparts
.scheme
=="sftp"):
6924 if(__use_pysftp__
and havepysftp
):
6925 return download_file_from_pysftp_string(url
);
6927 return download_file_from_sftp_string(url
);
6932 def upload_file_to_internet_file(ifp
, url
):
6933 urlparts
= urlparse(url
);
6934 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6936 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
6937 return upload_file_from_ftp_file(ifp
, url
);
6938 elif(urlparts
.scheme
=="sftp"):
6939 if(__use_pysftp__
and havepysftp
):
6940 return upload_file_from_pysftp_file(ifp
, url
);
6942 return download_file_from_sftp_file(ifp
, url
);
6947 def upload_file_to_internet_string(ifp
, url
):
6948 urlparts
= urlparse(url
);
6949 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6951 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
6952 return upload_file_from_ftp_string(ifp
, url
);
6953 elif(urlparts
.scheme
=="sftp"):
6954 if(__use_pysftp__
and havepysftp
):
6955 return upload_file_from_pysftp_string(ifp
, url
);
6957 return download_file_from_sftp_string(ifp
, url
);
6963 if(hasattr(shutil
, "register_archive_format")):
6964 # Register the packing format
6965 shutil
.register_archive_format(__file_format_name__
, PackArchiveFileFunc
, description
='Pack concatenated files');
6966 except shutil
.RegistryError
:
6970 if(hasattr(shutil
, "register_unpack_format")):
6971 # Register the unpacking format
6972 shutil
.register_unpack_format(__file_format_name__
, archivefile_extensions
, UnPackArchiveFileFunc
, description
='UnPack concatenated files');
6973 except shutil
.RegistryError
: