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: 5/5/2024 Ver. 0.10.2 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 if(sys
.version_info
[0]==2):
61 FileNotFoundError
= IOError;
63 rarfile_support
= False;
66 rarfile_support
= True;
68 rarfile_support
= False;
70 py7zr_support
= False;
75 py7zr_support
= False;
78 from xtarfile
import is_tarfile
;
81 from safetar
import is_tarfile
;
83 from tarfile
import is_tarfile
;
86 import xtarfile
as tarfile
;
89 import safetar
as tarfile
;
107 haverequests
= False;
112 haverequests
= False;
116 logging
.getLogger("urllib3").setLevel(logging
.WARNING
);
120 from urllib
.request
import Request
, build_opener
, HTTPBasicAuthHandler
;
121 from urllib
.parse
import urlparse
;
124 from urllib2
import Request
, build_opener
, HTTPBasicAuthHandler
;
125 from urlparse
import urlparse
;
127 if(sys
.version
[0]=="2"):
129 from io
import StringIO
, BytesIO
;
132 from cStringIO
import StringIO
;
133 from cStringIO
import StringIO
as BytesIO
;
135 from StringIO
import StringIO
;
136 from StringIO
import StringIO
as BytesIO
;
137 elif(sys
.version
[0]>="3"):
138 from io
import StringIO
, BytesIO
;
143 from cStringIO
import StringIO
as BytesIO
;
149 from StringIO
import StringIO
as BytesIO
;
155 from io
import BytesIO
;
160 __use_pysftp__
= False;
162 __use_pysftp__
= False;
163 __file_format_name__
= "CatFile";
164 __program_name__
= "Py"+__file_format_name__
;
165 __file_format_lower__
= __file_format_name__
.lower();
166 __file_format_magic__
= __file_format_name__
;
167 __file_format_len__
= len(__file_format_magic__
);
168 __file_format_hex__
= binascii
.hexlify(__file_format_magic__
.encode("UTF-8")).decode("UTF-8");
169 __file_format_delimiter__
= "\x00";
170 __file_format_ver__
= "001";
171 __use_new_style__
= True;
172 __use_advanced_list__
= True;
173 __use_alt_inode__
= False;
174 __file_format_list__
= [__file_format_name__
, __file_format_magic__
, __file_format_lower__
, __file_format_len__
, __file_format_hex__
, __file_format_delimiter__
, __file_format_ver__
, __use_new_style__
, __use_advanced_list__
, __use_alt_inode__
];
175 __project__
= __program_name__
;
176 __project_url__
= "https://github.com/GameMaker2k/PyCatFile";
177 __version_info__
= (0, 10, 2, "RC 1", 1);
178 __version_date_info__
= (2024, 5, 5, "RC 1", 1);
179 __version_date__
= str(__version_date_info__
[0]) + "." + str(__version_date_info__
[1]).zfill(2) + "." + str(__version_date_info__
[2]).zfill(2);
180 __revision__
= __version_info__
[3];
181 __revision_id__
= "$Id$";
182 if(__version_info__
[4] is not None):
183 __version_date_plusrc__
= __version_date__
+ "-" + str(__version_date_info__
[4]);
184 if(__version_info__
[4] is None):
185 __version_date_plusrc__
= __version_date__
;
186 if(__version_info__
[3] is not None):
187 __version__
= str(__version_info__
[0]) + "." + str(__version_info__
[1]) + "." + str(__version_info__
[2]) + " " + str(__version_info__
[3]);
188 if(__version_info__
[3] is None):
189 __version__
= str(__version_info__
[0]) + "." + str(__version_info__
[1]) + "." + str(__version_info__
[2]);
191 PyBitness
= platform
.architecture();
192 if(PyBitness
=="32bit" or PyBitness
=="32"):
194 elif(PyBitness
=="64bit" or PyBitness
=="64"):
199 geturls_ua_pycatfile_python
= "Mozilla/5.0 (compatible; {proname}/{prover}; +{prourl})".format(proname
=__project__
, prover
=__version__
, prourl
=__project_url__
);
200 if(platform
.python_implementation()!=""):
201 py_implementation
= platform
.python_implementation();
202 if(platform
.python_implementation()==""):
203 py_implementation
= "Python";
204 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__
);
205 geturls_ua_googlebot_google
= "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
206 geturls_ua_googlebot_google_old
= "Googlebot/2.1 (+http://www.google.com/bot.html)";
207 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
)};
208 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
)};
209 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"};
210 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"};
212 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
213 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
214 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
216 tarfile_mimetype
= "application/tar";
217 tarfile_tar_mimetype
= tarfile_mimetype
;
218 zipfile_mimetype
= "application/zip";
219 zipfile_zip_mimetype
= zipfile_mimetype
;
220 rarfile_mimetype
= "application/rar";
221 rarfile_rar_mimetype
= rarfile_mimetype
;
222 archivefile_mimetype
= "application/x-"+__file_format_list__
[1]+"";
223 archivefile_cat_mimetype
= archivefile_mimetype
;
224 archivefile_gzip_mimetype
= "application/x-"+__file_format_list__
[1]+"+gzip";
225 archivefile_gz_mimetype
= archivefile_gzip_mimetype
;
226 archivefile_bzip2_mimetype
= "application/x-"+__file_format_list__
[1]+"+bzip2";
227 archivefile_bz2_mimetype
= archivefile_bzip2_mimetype
;
228 archivefile_lz4_mimetype
= "application/x-"+__file_format_list__
[1]+"+lz4";
229 archivefile_lzop_mimetype
= "application/x-"+__file_format_list__
[1]+"+lzop";
230 archivefile_lzo_mimetype
= archivefile_lzop_mimetype
;
231 archivefile_zstandard_mimetype
= "application/x-"+__file_format_list__
[1]+"+zstandard";
232 archivefile_zstd_mimetype
= archivefile_zstandard_mimetype
;
233 archivefile_lzma_mimetype
= "application/x-"+__file_format_list__
[1]+"+lzma";
234 archivefile_xz_mimetype
= "application/x-"+__file_format_list__
[1]+"+xz";
235 archivefile_extensions
= ['.cat', '.cat.gz', '.cat.bz2', '.cat.zst', '.cat.lz4', '.cat.lzo', '.cat.lzop', '.cat.lzma', '.cat.xz'];
237 if __name__
== "__main__":
239 curscrpath
= os
.path
.dirname(sys
.argv
[0]);
243 curscrpath
= curscrpath
.replace(os
.sep
, "/");
244 curscrpath
= curscrpath
+ "/";
245 scrfile
= curscrpath
+ "catfile.py";
246 if(os
.path
.exists(scrfile
) and os
.path
.isfile(scrfile
)):
247 scrcmd
= subprocess
.Popen([sys
.executable
, scrfile
] + sys
.argv
[1:]);
250 def VerbosePrintOut(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
256 "warning": logging
.warning
,
257 "error": logging
.error
,
258 "critical": logging
.critical
,
259 "exception": logging
.exception
,
260 "logalt": lambda x
: logging
.log(dgblevel
, x
),
261 "debug": logging
.debug
263 log_function
= log_functions
.get(outtype
);
265 log_function(dbgtxt
);
269 def VerbosePrintOutReturn(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
270 VerbosePrintOut(dbgtxt
, outtype
, dbgenable
, dgblevel
);
273 def RemoveWindowsPath(dpath
):
277 dpath
= dpath
.replace(os
.path
.sep
, "/");
278 dpath
= dpath
.rstrip("/");
279 if(dpath
=="." or dpath
==".."):
283 def NormalizeRelativePath(inpath
):
284 inpath
= RemoveWindowsPath(inpath
);
285 if(os
.path
.isabs(inpath
)):
288 if(inpath
.startswith("./") or inpath
.startswith("../")):
291 outpath
= "./" + inpath
;
294 def PrependPath(base_dir
, child_path
):
295 # Check if base_dir is None or empty, if so, return child_path as is
298 # Ensure base_dir ends with exactly one slash
299 if not base_dir
.endswith('/'):
301 # Check if child_path starts with ./ or ../ (indicating a relative path)
302 if child_path
.startswith('./') or child_path
.startswith('../'):
303 # For relative paths, we don't alter the child_path
304 return base_dir
+ child_path
;
306 # For non-relative paths, ensure there's no starting slash on child_path to avoid double slashes
307 return base_dir
+ child_path
.lstrip('/');
309 def ListDir(dirpath
, followlink
=False, duplicates
=False):
310 if(isinstance(dirpath
, (list, tuple, ))):
311 dirpath
= list(filter(None, dirpath
));
312 elif(isinstance(dirpath
, (str, ))):
313 dirpath
= list(filter(None, [dirpath
]));
315 for mydirfile
in dirpath
:
316 if(not os
.path
.exists(mydirfile
)):
318 mydirfile
= NormalizeRelativePath(mydirfile
);
319 if(os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
)):
320 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
));
321 if(os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
)):
322 for root
, dirs
, filenames
in os
.walk(mydirfile
):
324 dpath
= RemoveWindowsPath(dpath
);
325 if(dpath
not in retlist
and not duplicates
):
326 retlist
.append(dpath
);
328 retlist
.append(dpath
);
329 for file in filenames
:
330 fpath
= os
.path
.join(root
, file);
331 fpath
= RemoveWindowsPath(fpath
);
332 if(fpath
not in retlist
and not duplicates
):
333 retlist
.append(fpath
);
335 retlist
.append(fpath
);
337 retlist
.append(RemoveWindowsPath(mydirfile
));
340 def ListDirAdvanced(dirpath
, followlink
=False, duplicates
=False):
341 if isinstance(dirpath
, (list, tuple)):
342 dirpath
= list(filter(None, dirpath
));
343 elif isinstance(dirpath
, str):
344 dirpath
= list(filter(None, [dirpath
]));
346 for mydirfile
in dirpath
:
347 if not os
.path
.exists(mydirfile
):
349 mydirfile
= NormalizeRelativePath(mydirfile
);
350 if os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
) and followlink
:
351 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
))
352 if os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
):
353 for root
, dirs
, filenames
in os
.walk(mydirfile
):
354 # Sort dirs and filenames alphabetically in place
355 dirs
.sort(key
=lambda x
: x
.lower());
356 filenames
.sort(key
=lambda x
: x
.lower());
357 dpath
= RemoveWindowsPath(root
);
358 if not duplicates
and dpath
not in retlist
:
359 retlist
.append(dpath
);
361 retlist
.append(dpath
);
362 for file in filenames
:
363 fpath
= os
.path
.join(root
, file);
364 fpath
= RemoveWindowsPath(fpath
);
365 if not duplicates
and fpath
not in retlist
:
366 retlist
.append(fpath
);
368 retlist
.append(fpath
);
370 retlist
.append(RemoveWindowsPath(mydirfile
));
373 def create_alias_function(prefix
, base_name
, suffix
, target_function
):
374 # Define a new function that wraps the target function
375 def alias_function(*args
, **kwargs
):
376 return target_function(*args
, **kwargs
);
378 # Create the function name by combining the prefix, base name, and the suffix
379 function_name
= "{}{}{}".format(prefix
, base_name
, suffix
);
381 # Add the new function to the global namespace
382 globals()[function_name
] = alias_function
;
384 def create_alias_function_alt(prefix
, base_name
, suffix
, target_function
):
385 # Create the function name by combining the prefix, base name, and the suffix
386 # Use the format method for string formatting, compatible with Python 2 and 3
387 function_name
= "{}{}{}".format(prefix
, base_name
, suffix
)
388 # Add the new function (alias of the target_function) to the global namespace
389 # This line is compatible as-is with both Python 2 and 3
390 globals()[function_name
] = target_function
392 # initial_value can be 0xFFFF or 0x0000
393 def crc16_ansi(msg
, initial_value
=0xFFFF):
394 # CRC-16-IBM / CRC-16-ANSI polynomial and initial value
395 poly
= 0x8005; # Polynomial for CRC-16-IBM / CRC-16-ANSI
396 crc
= initial_value
; # Initial value
398 crc ^
= b
<< 8; # XOR byte into CRC top byte
399 for _
in range(8): # Process each bit
400 if crc
& 0x8000: # If the top bit is set
401 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
403 crc
= crc
<< 1; # Just shift left
404 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
407 # initial_value can be 0xFFFF or 0x0000
408 def crc16_ibm(msg
, initial_value
=0xFFFF):
409 return crc16_ansi(msg
, initial_value
);
411 # initial_value is 0xFFFF
413 return crc16_ansi(msg
, 0xFFFF);
415 # initial_value can be 0xFFFF, 0x1D0F or 0x0000
416 def crc16_ccitt(msg
, initial_value
=0xFFFF):
417 # CRC-16-CCITT polynomial
418 poly
= 0x1021; # Polynomial for CRC-16-CCITT
419 # Use the specified initial value
422 crc ^
= b
<< 8; # XOR byte into CRC top byte
423 for _
in range(8): # Process each bit
424 if crc
& 0x8000: # If the top bit is set
425 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
427 crc
= crc
<< 1; # Just shift left
428 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
431 # initial_value can be 0x42F0E1EBA9EA3693 or 0x0000000000000000
432 def crc64_ecma(msg
, initial_value
=0x0000000000000000):
433 # CRC-64-ECMA polynomial and initial value
434 poly
= 0x42F0E1EBA9EA3693;
435 crc
= initial_value
; # Initial value for CRC-64-ECMA
437 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
438 for _
in range(8): # Process each bit
439 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
440 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
442 crc
<<= 1; # Just shift left if the MSB is 0
443 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
446 # initial_value can be 0x000000000000001B or 0xFFFFFFFFFFFFFFFF
447 def crc64_iso(msg
, initial_value
=0xFFFFFFFFFFFFFFFF):
448 # CRC-64-ISO polynomial and initial value
449 poly
= 0x000000000000001B;
450 crc
= initial_value
; # Common initial value for CRC-64-ISO
452 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
453 for _
in range(8): # Process each bit
454 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
455 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
457 crc
<<= 1; # Just shift left if the MSB is 0
458 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
461 def GetDataFromArray(data
, path
, default
=None):
465 element
= element
[key
];
467 except (KeyError, TypeError, IndexError):
470 def GetDataFromArrayAlt(structure
, path
, default
=None):
473 if isinstance(element
, dict) and key
in element
:
474 element
= element
[key
];
475 elif isinstance(element
, list) and isinstance(key
, int) and -len(element
) <= key
< len(element
):
476 element
= element
[key
];
481 def GetHeaderChecksum(inlist
=[], checksumtype
="crc32", encodedata
=True, formatspecs
=__file_format_list__
):
482 if isinstance(inlist
, list):
483 fileheader
= AppendNullBytes(inlist
, formatspecs
[5]);
485 fileheader
= AppendNullByte(inlist
, formatspecs
[5]);
487 fileheader
= fileheader
.encode('UTF-8');
488 if(checksumtype
=="none" or checksumtype
==""):
489 catfileheadercshex
= format(0, 'x').lower();
490 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
491 catfileheadercshex
= format(crc16(fileheader
) & 0xffff, '04x').lower();
492 elif(checksumtype
=="crc16_ccitt"):
493 catfileheadercshex
= format(crc16_ccitt(fileheader
) & 0xffff, '04x').lower();
494 elif(checksumtype
=="adler32"):
495 catfileheadercshex
= format(zlib
.adler32(fileheader
) & 0xffffffff, '08x').lower();
496 elif(checksumtype
=="crc32"):
497 catfileheadercshex
= format(crc32(fileheader
) & 0xffffffff, '08x').lower();
498 elif(checksumtype
=="crc64_ecma"):
499 catfileheadercshex
= format(crc64_ecma(fileheader
) & 0xffffffffffffffff, '016x').lower();
500 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
501 catfileheadercshex
= format(crc64_iso(fileheader
) & 0xffffffffffffffff, '016x').lower();
502 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
503 checksumoutstr
= hashlib
.new(checksumtype
);
504 checksumoutstr
.update(fileheader
);
505 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
507 catfileheadercshex
= format(0, 'x').lower();
508 return catfileheadercshex
;
510 def GetFileChecksum(instr
, checksumtype
="crc32", encodedata
=True, formatspecs
=__file_format_list__
):
512 instr
= instr
.encode('UTF-8');
513 if(checksumtype
=="none" or checksumtype
==""):
514 catinstrcshex
= format(0, 'x').lower();
515 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
516 catinstrcshex
= format(crc16(instr
) & 0xffff, '04x').lower();
517 elif(checksumtype
=="crc16_ccitt"):
518 catinstrcshex
= format(crc16_ccitt(instr
) & 0xffff, '04x').lower();
519 elif(checksumtype
=="adler32"):
520 catinstrcshex
= format(zlib
.adler32(instr
) & 0xffffffff, '08x').lower();
521 elif(checksumtype
=="crc32"):
522 catinstrcshex
= format(crc32(instr
) & 0xffffffff, '08x').lower();
523 elif(checksumtype
=="crc64_ecma"):
524 catinstrcshex
= format(crc64_ecma(instr
) & 0xffffffffffffffff, '016x').lower();
525 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
526 catinstrcshex
= format(crc64_iso(instr
) & 0xffffffffffffffff, '016x').lower();
527 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
528 checksumoutstr
= hashlib
.new(checksumtype
);
529 checksumoutstr
.update(instr
);
530 catinstrcshex
= checksumoutstr
.hexdigest().lower();
532 catinstrcshex
= format(0, 'x').lower();
533 return catinstrcshex
;
535 def ValidateHeaderChecksum(inlist
=[], checksumtype
="crc32", inchecksum
="0", formatspecs
=__file_format_list__
):
536 catfileheadercshex
= GetHeaderChecksum(inlist
, checksumtype
, True, formatspecs
);
537 inchecksum
= inchecksum
.lower();
538 catfileheadercshex
= catfileheadercshex
.lower();
539 if(inchecksum
==catfileheadercshex
):
544 def ValidateFileChecksum(infile
, checksumtype
="crc32", inchecksum
="0", formatspecs
=__file_format_list__
):
545 catinfilecshex
= GetFileChecksum(infile
, checksumtype
, True, formatspecs
);
546 inchecksum
= inchecksum
.lower();
547 catinfilecshex
= catinfilecshex
.lower();
548 if(inchecksum
==catinfilecshex
):
553 def ReadTillNullByte(fp
, delimiter
=__file_format_delimiter__
):
556 nullbyte
= delimiter
.encode("UTF-8");
558 curbyte
= fp
.read(1);
559 if(curbyte
==nullbyte
or not curbyte
):
561 curfullbyte
= curfullbyte
+ curbyte
;
562 return curfullbyte
.decode('UTF-8');
564 def ReadUntilNullByte(fp
, delimiter
=__file_format_delimiter__
):
565 return ReadTillNullByte(fp
, delimiter
);
567 def SeekToEndOfFile(fp
):
571 if(lasttell
==fp
.tell()):
573 lasttell
= fp
.tell();
576 def ReadFileHeaderData(fp
, rounds
=0, delimiter
=__file_format_delimiter__
):
580 while(rocount
<roend
):
581 HeaderOut
.append(ReadTillNullByte(fp
, delimiter
));
582 rocount
= rocount
+ 1;
585 def ReadFileHeaderDataBySize(fp
, delimiter
=__file_format_delimiter__
):
586 headerpresize
= ReadTillNullByte(fp
, delimiter
);
587 headersize
= int(headerpresize
, 16);
590 headercontent
= str(fp
.read(headersize
).decode('UTF-8')).split(delimiter
);
593 roend
= int(len(headercontent
));
594 HeaderOut
= [headerpresize
];
595 while(rocount
<roend
):
596 HeaderOut
.append(headercontent
[rocount
]);
597 rocount
= rocount
+ 1;
600 def ReadFileHeaderDataWoSize(fp
, delimiter
=__file_format_delimiter__
):
601 preheaderdata
= ReadFileHeaderData(fp
, 2, delimiter
);
602 headersize
= int(preheaderdata
[0], 16);
603 headernumfields
= int(preheaderdata
[1], 16);
604 if(headersize
<=0 or headernumfields
<=0):
606 headerdata
= ReadFileHeaderData(fp
, headernumfields
, delimiter
);
607 HeaderOut
= preheaderdata
+ headerdata
;
610 def ReadFileHeaderDataBySizeWithContent(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
611 delimiter
= formatspecs
[5];
612 fheaderstart
= fp
.tell();
613 HeaderOut
= ReadFileHeaderDataBySize(fp
, delimiter
);
614 if(len(HeaderOut
)==0):
616 if(re
.findall("^[.|/]", HeaderOut
[3])):
617 fname
= HeaderOut
[3];
619 fname
= "./"+HeaderOut
[3];
620 fchecksumtype
= HeaderOut
[-3].lower();
621 fcs
= HeaderOut
[-2].lower();
622 fccs
= HeaderOut
[-1].lower();
623 fsize
= int(HeaderOut
[5], 16);
624 newfcs
= GetHeaderChecksum(HeaderOut
[:-2], HeaderOut
[-3].lower(), True, formatspecs
);
625 if(fcs
!=newfcs
and not skipchecksum
):
626 VerbosePrintOut("File Header Checksum Error with file " + fname
+ " at offset " + str(fheaderstart
));
627 VerbosePrintOut("'" + str(fcs
) + "' != " + "'" + str(newfcs
) + "'");
629 fhend
= fp
.tell() - 1;
630 fcontentstart
= fp
.tell();
631 fcontents
= BytesIO();
632 if(fsize
>0 and not listonly
):
633 fcontents
.write(fp
.read(fsize
));
634 elif(fsize
>0 and listonly
):
636 fcontents
.seek(0, 0);
637 newfccs
= GetFileChecksum(fcontents
.read(), HeaderOut
[-3].lower(), False, formatspecs
);
638 if(fccs
!=newfccs
and not skipchecksum
and not listonly
):
639 VerbosePrintOut("File Content Checksum Error with file " + fname
+ " at offset " + str(fcontentstart
));
640 VerbosePrintOut("'" + str(fccs
) + "' != " + "'" + str(newfccs
) + "'");
643 fcontentend
= fp
.tell() - 1;
644 HeaderOut
.append(fcontents
);
647 def ReadFileHeaderDataBySizeWithContentToArray(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
648 delimiter
= formatspecs
[5];
649 fheaderstart
= fp
.tell();
651 HeaderOut
= ReadFileHeaderDataBySize(fp
, delimiter
);
653 HeaderOut
= ReadFileHeaderDataWoSize(fp
, delimiter
);
654 if(len(HeaderOut
)==0):
656 fheadsize
= int(HeaderOut
[0], 16);
657 fnumfields
= int(HeaderOut
[1], 16);
658 ftype
= int(HeaderOut
[2], 16);
659 if(re
.findall("^[.|/]", HeaderOut
[3])):
660 fname
= HeaderOut
[3];
662 fname
= "./"+HeaderOut
[3];
663 fbasedir
= os
.path
.dirname(fname
);
664 flinkname
= HeaderOut
[4];
665 fsize
= int(HeaderOut
[5], 16);
666 fatime
= int(HeaderOut
[6], 16);
667 fmtime
= int(HeaderOut
[7], 16);
668 fctime
= int(HeaderOut
[8], 16);
669 fbtime
= int(HeaderOut
[9], 16);
670 fmode
= int(HeaderOut
[10], 16);
671 fchmode
= stat
.S_IMODE(fmode
);
672 ftypemod
= stat
.S_IFMT(fmode
);
673 fwinattributes
= int(HeaderOut
[11], 16);
674 fuid
= int(HeaderOut
[12], 16);
675 funame
= HeaderOut
[13];
676 fgid
= int(HeaderOut
[14], 16);
677 fgname
= HeaderOut
[15];
678 fid
= int(HeaderOut
[16], 16);
679 finode
= int(HeaderOut
[17], 16);
680 flinkcount
= int(HeaderOut
[18], 16);
681 fdev_minor
= int(HeaderOut
[19], 16);
682 fdev_major
= int(HeaderOut
[20], 16);
683 frdev_minor
= int(HeaderOut
[21], 16);
684 frdev_major
= int(HeaderOut
[22], 16);
685 fextrasize
= int(HeaderOut
[23], 16);
686 fextrafields
= int(HeaderOut
[24], 16);
687 extrafieldslist
= [];
689 extraend
= extrastart
+ fextrafields
;
690 extrafieldslist
= [];
691 if(extrastart
<extraend
):
692 extrafieldslist
.append(HeaderOut
[extrastart
]);
693 extrastart
= extrastart
+ 1;
694 fchecksumtype
= HeaderOut
[extrastart
].lower();
695 fcs
= HeaderOut
[extrastart
+ 1].lower();
696 fccs
= HeaderOut
[extrastart
+ 2].lower();
697 newfcs
= GetHeaderChecksum(HeaderOut
[:-2], HeaderOut
[-3].lower(), True, formatspecs
);
698 if(fcs
!=newfcs
and not skipchecksum
):
699 VerbosePrintOut("File Header Checksum Error with file " + fname
+ " at offset " + str(fheaderstart
));
700 VerbosePrintOut("'" + str(fcs
) + "' != " + "'" + str(newfcs
) + "'");
702 fhend
= fp
.tell() - 1;
703 fcontentstart
= fp
.tell();
704 fcontents
= BytesIO();
705 pyhascontents
= False;
706 if(fsize
>0 and not listonly
):
707 fcontents
.write(fp
.read(catfsize
));
708 pyhascontents
= True;
709 elif(fsize
>0 and listonly
):
711 pyhascontents
= False;
712 fcontents
.seek(0, 0);
713 newfccs
= GetFileChecksum(fcontents
.read(), HeaderOut
[-3].lower(), False, formatspecs
);
714 if(fccs
!=newfccs
and not skipchecksum
and not listonly
):
715 VerbosePrintOut("File Content Checksum Error with file " + fname
+ " at offset " + str(fcontentstart
));
716 VerbosePrintOut("'" + str(fccs
) + "' != " + "'" + str(newfccs
) + "'");
719 fcontentend
= fp
.tell() - 1;
720 fcontents
.seek(0, 0);
721 catlist
= {'fheadersize': fheadsize
, 'fhstart': fheaderstart
, 'fhend': fhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fwinattributes': fwinattributes
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': fchecksumtype
, 'fnumfields': fnumfields
+ 2, 'frawheader': HeaderOut
, 'fextrafields': fextrafields
, 'fextrafieldsize': fextrasize
, 'fextralist': extrafieldslist
, 'fheaderchecksum': fcs
, 'fcontentchecksum': fccs
, 'fhascontents': pyhascontents
, 'fcontentstart': fcontentstart
, 'fcontentend': fcontentend
, 'fcontents': fcontents
};
724 def ReadFileHeaderDataBySizeWithContentToList(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
725 delimiter
= formatspecs
[5];
726 fheaderstart
= fp
.tell();
728 HeaderOut
= ReadFileHeaderDataBySize(fp
, delimiter
);
730 HeaderOut
= ReadFileHeaderDataWoSize(fp
, delimiter
);
731 if(len(HeaderOut
)==0):
733 fheadsize
= int(HeaderOut
[0], 16);
734 fnumfields
= int(HeaderOut
[1], 16);
735 ftype
= int(HeaderOut
[2], 16);
736 if(re
.findall("^[.|/]", HeaderOut
[3])):
737 fname
= HeaderOut
[3];
739 fname
= "./"+HeaderOut
[3];
740 fbasedir
= os
.path
.dirname(fname
);
741 flinkname
= HeaderOut
[4];
742 fsize
= int(HeaderOut
[5], 16);
743 fatime
= int(HeaderOut
[6], 16);
744 fmtime
= int(HeaderOut
[7], 16);
745 fctime
= int(HeaderOut
[8], 16);
746 fbtime
= int(HeaderOut
[9], 16);
747 fmode
= int(HeaderOut
[10], 16);
748 fchmode
= stat
.S_IMODE(fmode
);
749 ftypemod
= stat
.S_IFMT(fmode
);
750 fwinattributes
= int(HeaderOut
[11], 16);
751 fuid
= int(HeaderOut
[12], 16);
752 funame
= HeaderOut
[13];
753 fgid
= int(HeaderOut
[14], 16);
754 fgname
= HeaderOut
[15];
755 fid
= int(HeaderOut
[16], 16);
756 finode
= int(HeaderOut
[17], 16);
757 flinkcount
= int(HeaderOut
[18], 16);
758 fdev_minor
= int(HeaderOut
[19], 16);
759 fdev_major
= int(HeaderOut
[20], 16);
760 frdev_minor
= int(HeaderOut
[21], 16);
761 frdev_major
= int(HeaderOut
[22], 16);
762 fextrasize
= int(HeaderOut
[23], 16);
763 fextrafields
= int(HeaderOut
[24], 16);
764 extrafieldslist
= [];
766 extraend
= extrastart
+ fextrafields
;
767 extrafieldslist
= [];
768 if(extrastart
<extraend
):
769 extrafieldslist
.append(HeaderOut
[extrastart
]);
770 extrastart
= extrastart
+ 1;
771 fchecksumtype
= HeaderOut
[extrastart
].lower();
772 fcs
= HeaderOut
[extrastart
+ 1].lower();
773 fccs
= HeaderOut
[extrastart
+ 2].lower();
774 newfcs
= GetHeaderChecksum(HeaderOut
[:-2], HeaderOut
[-3].lower(), True, formatspecs
);
775 if(fcs
!=newfcs
and not skipchecksum
):
776 VerbosePrintOut("File Header Checksum Error with file " + fname
+ " at offset " + str(fheaderstart
));
777 VerbosePrintOut("'" + str(fcs
) + "' != " + "'" + str(newfcs
) + "'");
779 fhend
= fp
.tell() - 1;
780 fcontentstart
= fp
.tell();
781 fcontents
= BytesIO();
782 pyhascontents
= False;
783 if(fsize
>0 and not listonly
):
784 fcontents
.write(fp
.read(fsize
));
785 pyhascontents
= True;
786 elif(fsize
>0 and listonly
):
788 pyhascontents
= False;
789 fcontents
.seek(0, 0);
790 newfccs
= GetFileChecksum(fcontents
.read(), HeaderOut
[-3].lower(), False, formatspecs
);
791 if(fccs
!=newfccs
and not skipchecksum
and not listonly
):
792 VerbosePrintOut("File Content Checksum Error with file " + fname
+ " at offset " + str(fcontentstart
));
793 VerbosePrintOut("'" + str(fccs
) + "' != " + "'" + str(newfccs
) + "'");
796 fcontentend
= fp
.tell() - 1;
797 catlist
= [ftype
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fid
, finode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrafieldslist
, fchecksumtype
, fcontents
];
800 def ReadFileDataBySizeWithContent(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
801 delimiter
= formatspecs
[5];
805 catheader
= ReadFileHeaderData(fp
, 4, delimiter
);
808 headercheck
= ValidateHeaderChecksum(catheader
[:-1], catheader
[2], catheader
[3], formatspecs
);
809 newfcs
= GetHeaderChecksum(catheader
[:-2], catheader
[2], True, formatspecs
);
810 if(not headercheck
and not skipchecksum
):
811 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
812 VerbosePrintOut("'" + str(newfcs
) + "' != " + "'" + str(catheader
[3]) + "'");
814 fnumfiles
= int(catheader
[1], 16);
817 while(countnum
< fnumfiles
):
818 HeaderOut
= ReadFileHeaderDataBySizeWithContent(fp
, listonly
, skipchecksum
, formatspecs
)
819 if(len(HeaderOut
)==0):
821 flist
.append(HeaderOut
);
822 countnum
= countnum
+ 1;
825 def ReadFileDataBySizeWithContentToArray(fp
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
826 delimiter
= formatspecs
[5];
830 catheader
= ReadFileHeaderData(fp
, 4, delimiter
);
833 headercheck
= ValidateHeaderChecksum(catheader
[:-1], catheader
[2], catheader
[3], formatspecs
);
834 newfcs
= GetHeaderChecksum(catheader
[:-2], catheader
[2], True, formatspecs
);
835 if(not headercheck
and not skipchecksum
):
836 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
837 VerbosePrintOut("'" + str(newfcs
) + "' != " + "'" + str(catheader
[3]) + "'");
839 catstring
= catheader
[0];
840 catversion
= re
.findall(r
"([\d]+)$", catstring
);
841 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
842 fprenumfiles
= catheader
[1];
843 fnumfiles
= int(fprenumfiles
, 16);
844 fprechecksumtype
= catheader
[2];
845 fprechecksum
= catheader
[3];
846 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
847 if(seekstart
<0 and seekstart
>fnumfiles
):
849 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
851 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
852 seekend
= fnumfiles
- abs(seekend
);
855 while(il
< seekstart
):
856 prefhstart
= fp
.tell();
857 preheaderdata
= ReadFileHeaderDataBySize(fp
, formatspecs
[5]);
858 if(len(preheaderdata
)==0):
860 prefsize
= int(preheaderdata
[5], 16);
861 prenewfcs
= GetHeaderChecksum(preheaderdata
[:-2], preheaderdata
[-3].lower(), True, formatspecs
);
862 prefcs
= preheaderdata
[-2];
863 if(prefcs
!=prenewfcs
and not skipchecksum
):
864 VVerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
865 VerbosePrintOut("'" + str(prefcs
) + "' != " + "'" + str(prenewfcs
) + "'");
867 valid_archive
= False;
868 invalid_archive
= True;
869 prefhend
= fp
.tell() - 1;
870 prefcontentstart
= fp
.tell();
871 prefcontents
= BytesIO();
872 pyhascontents
= False;
874 prefcontents
.write(fp
.read(prefsize
));
875 prefcontents
.seek(0, 0);
876 prenewfccs
= GetFileChecksum(prefcontents
.read(), preheaderdata
[-3].lower(), False, formatspecs
);
877 prefccs
= preheaderdata
[-1];
878 pyhascontents
= True;
879 if(prefccs
!=prenewfccs
and not skipchecksum
):
880 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
881 VerbosePrintOut("'" + str(prefccs
) + "' != " + "'" + str(prenewfccs
) + "'");
886 countnum
= seekstart
;
887 while(countnum
< seekend
):
888 catlist
['ffilelist'].update({realidnum
: {'fid': realidnum
, 'fidalt': realidnum
}});
889 HeaderOut
= ReadFileHeaderDataBySizeWithContentToArray(fp
, listonly
, skipchecksum
, formatspecs
);
892 catlist
['ffilelist'][realidnum
].update(HeaderOut
);
893 countnum
= countnum
+ 1;
894 realidnum
= realidnum
+ 1;
897 def ReadFileDataBySizeWithContentToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
898 if(isinstance(infile
, dict)):
899 listcatfiles
= infile
;
901 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
903 listcatfiles
= ReadFileDataBySizeWithContentToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
);
904 if(not listcatfiles
):
906 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': {}}}};
907 lenlist
= len(listcatfiles
['ffilelist']);
909 lcfx
= int(listcatfiles
['fnumfiles']);
910 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
913 lcfx
= int(listcatfiles
['fnumfiles']);
915 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
916 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
917 catarray
['filetoid'].update(filetoidarray
);
918 catarray
['idtofile'].update(idtofilearray
);
919 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
920 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
921 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
922 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
923 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
924 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
925 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
926 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
927 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
928 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
929 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
930 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
931 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
932 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
933 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
934 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
935 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
936 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
937 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
938 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
939 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
940 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
941 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
942 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
943 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
944 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
945 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
946 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
947 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
948 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
949 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
953 def ReadFileDataBySizeWithContentToList(fp
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
954 delimiter
= formatspecs
[5];
958 catheader
= ReadFileHeaderData(fp
, 4, delimiter
);
961 headercheck
= ValidateHeaderChecksum(catheader
[:-1], catheader
[2], catheader
[3], formatspecs
);
962 newfcs
= GetHeaderChecksum(catheader
[:-2], catheader
[2], True, formatspecs
);
963 if(not headercheck
and not skipchecksum
):
964 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
965 VerbosePrintOut("'" + str(newfcs
) + "' != " + "'" + str(catheader
[3]) + "'");
967 catstring
= catheader
[0];
968 catversion
= re
.findall(r
"([\d]+)$", catstring
);
969 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
970 fprenumfiles
= catheader
[1];
971 fnumfiles
= int(fprenumfiles
, 16);
972 fprechecksumtype
= catheader
[2];
973 fprechecksum
= catheader
[3];
975 if(seekstart
<0 and seekstart
>fnumfiles
):
977 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
979 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
980 seekend
= fnumfiles
- abs(seekend
);
983 while(il
< seekstart
):
984 prefhstart
= fp
.tell();
985 preheaderdata
= ReadFileHeaderDataBySize(fp
, formatspecs
[5]);
986 if(len(preheaderdata
)==0):
988 prefsize
= int(preheaderdata
[5], 16);
989 prenewfcs
= GetHeaderChecksum(preheaderdata
[:-2], preheaderdata
[-3].lower(), True, formatspecs
);
990 prefcs
= preheaderdata
[-2];
991 if(prefcs
!=prenewfcs
and not skipchecksum
):
992 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
993 VerbosePrintOut("'" + str(prefcs
) + "' != " + "'" + str(prenewfcs
) + "'");
995 valid_archive
= False;
996 invalid_archive
= True;
997 prefhend
= fp
.tell() - 1;
998 prefcontentstart
= fp
.tell();
1000 pyhascontents
= False;
1002 prefcontents
= fp
.read(prefsize
);
1003 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
1004 prefccs
= preheaderdata
[-1];
1005 pyhascontents
= True;
1006 if(prefccs
!=prenewfccs
and not skipchecksum
):
1007 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
1008 VerbosePrintOut("'" + str(prefccs
) + "' != " + "'" + str(prenewfccs
) + "'");
1013 countnum
= seekstart
;
1014 while(countnum
< seekend
):
1015 HeaderOut
= ReadFileHeaderDataBySizeWithContentToList(fp
, listonly
, skipchecksum
, formatspecs
);
1018 catlist
.append(HeaderOut
);
1019 countnum
= countnum
+ 1;
1020 realidnum
= realidnum
+ 1;
1023 def ReadInFileBySizeWithContentToArray(infile
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
1024 delimiter
= formatspecs
[5];
1025 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1028 fp
= UncompressArchiveFile(fp
, formatspecs
);
1029 checkcompressfile
= CheckCompressionSubType(fp
, formatspecs
, True);
1030 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
1037 if(hasattr(sys
.stdin
, "buffer")):
1038 shutil
.copyfileobj(sys
.stdin
.buffer, fp
);
1040 shutil
.copyfileobj(sys
.stdin
, fp
);
1042 fp
= UncompressArchiveFile(fp
, formatspecs
);
1046 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
1047 fp
= download_file_from_internet_file(infile
);
1048 fp
= UncompressArchiveFile(fp
, formatspecs
);
1054 infile
= RemoveWindowsPath(infile
);
1055 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
1056 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
1058 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
1059 if(not compresscheck
):
1060 fextname
= os
.path
.splitext(infile
)[1];
1061 if(fextname
==".gz"):
1062 compresscheck
= "gzip";
1063 elif(fextname
==".bz2"):
1064 compresscheck
= "bzip2";
1065 elif(fextname
==".zst"):
1066 compresscheck
= "zstd";
1067 elif(fextname
==".lz4" or fextname
==".clz4"):
1068 compresscheck
= "lz4";
1069 elif(fextname
==".lzo" or fextname
==".lzop"):
1070 compresscheck
= "lzo";
1071 elif(fextname
==".lzma" or fextname
==".xz"):
1072 compresscheck
= "lzma";
1075 if(not compresscheck
):
1077 fp
= UncompressFile(infile
, formatspecs
, "rb");
1078 return ReadFileDataBySizeWithContentToArray(fp
, listonly
, skipchecksum
, formatspecs
);
1080 def ReadInFileBySizeWithContentToList(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
1081 delimiter
= formatspecs
[5];
1082 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1085 fp
= UncompressArchiveFile(fp
, formatspecs
);
1086 checkcompressfile
= CheckCompressionSubType(fp
, formatspecs
, True);
1087 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
1094 if(hasattr(sys
.stdin
, "buffer")):
1095 shutil
.copyfileobj(sys
.stdin
.buffer, fp
);
1097 shutil
.copyfileobj(sys
.stdin
, fp
);
1099 fp
= UncompressArchiveFile(fp
, formatspecs
);
1103 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
1104 fp
= download_file_from_internet_file(infile
);
1105 fp
= UncompressArchiveFile(fp
, formatspecs
);
1111 infile
= RemoveWindowsPath(infile
);
1112 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
1113 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
1115 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
1116 if(not compresscheck
):
1117 fextname
= os
.path
.splitext(infile
)[1];
1118 if(fextname
==".gz"):
1119 compresscheck
= "gzip";
1120 elif(fextname
==".bz2"):
1121 compresscheck
= "bzip2";
1122 elif(fextname
==".zst"):
1123 compresscheck
= "zstd";
1124 elif(fextname
==".lz4" or fextname
==".clz4"):
1125 compresscheck
= "lz4";
1126 elif(fextname
==".lzo" or fextname
==".lzop"):
1127 compresscheck
= "lzo";
1128 elif(fextname
==".lzma" or fextname
==".xz"):
1129 compresscheck
= "lzma";
1132 if(not compresscheck
):
1134 fp
= UncompressFile(infile
, formatspecs
, "rb");
1135 return ReadFileDataBySizeWithContentToList(fp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
);
1137 def ReadInFileBySizeWithContentToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
1138 if(isinstance(infile
, dict)):
1139 listcatfiles
= infile
;
1141 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
1142 infile
= RemoveWindowsPath(infile
);
1143 listcatfiles
= ReadInFileBySizeWithContentToArray(infile
, listonly
, skipchecksum
, formatspecs
);
1144 if(not listcatfiles
):
1146 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': {}}}};
1147 lenlist
= len(listcatfiles
['ffilelist']);
1149 lcfx
= int(listcatfiles
['fnumfiles']);
1150 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
1151 lcfx
= int(lenlist
);
1153 lcfx
= int(listcatfiles
['fnumfiles']);
1155 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
1156 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
1157 catarray
['filetoid'].update(filetoidarray
);
1158 catarray
['idtofile'].update(idtofilearray
);
1159 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
1160 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
1161 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
1162 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
1163 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
1164 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
1165 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
1166 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
1167 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
1168 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
1169 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
1170 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
1171 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
1172 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
1173 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
1174 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
1175 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
1176 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
1177 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
1178 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
1179 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
1180 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
1181 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
1182 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
1183 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
1184 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
1185 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
1186 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
1187 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
1188 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
1189 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
1193 def AppendNullByte(indata
, delimiter
=__file_format_delimiter__
):
1194 outdata
= str(indata
) + delimiter
;
1197 def AppendNullBytes(indata
=[], delimiter
=__file_format_delimiter__
):
1202 outdata
= outdata
+ AppendNullByte(indata
[inum
], delimiter
);
1206 def AppendFileHeader(fp
, numfiles
, checksumtype
="crc32", formatspecs
=__file_format_list__
):
1207 delimiter
= formatspecs
[5];
1208 catver
= formatspecs
[6];
1209 fileheaderver
= str(int(catver
.replace(".", "")));
1210 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
1211 fp
.write(fileheader
.encode('UTF-8'));
1212 fnumfiles
= format(int(numfiles
), 'x').lower();
1213 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
1214 catfileheadercshex
= GetFileChecksum(fileheader
+ fnumfilesa
, checksumtype
, True, formatspecs
);
1215 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
1216 fp
.write(fnumfilesa
.encode('UTF-8'));
1219 os
.fsync(fp
.fileno());
1220 except io
.UnsupportedOperation
:
1222 except AttributeError:
1224 except OSError as e
:
1228 def MakeEmptyFilePointer(fp
, checksumtype
="crc32", formatspecs
=__file_format_list__
):
1229 AppendFileHeader(fp
, 0, checksumtype
, formatspecs
);
1232 def MakeEmptyFile(outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", formatspecs
=__file_format_list__
, returnfp
=False):
1233 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1234 if(os
.path
.exists(outfile
)):
1237 except OSError as e
:
1241 catfpfp
= BytesIO();
1242 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1244 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1247 fbasename
= os
.path
.splitext(outfile
)[0];
1248 fextname
= os
.path
.splitext(outfile
)[1];
1249 catfp
= CompressOpenFile(outfile
, True, compressionlevel
);
1250 catfp
= AppendFileHeader(catfp
, 0, checksumtype
, formatspecs
);
1251 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1252 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
1255 os
.fsync(catfp
.fileno());
1256 except io
.UnsupportedOperation
:
1258 except AttributeError:
1260 except OSError as e
:
1264 if(hasattr(sys
.stdout
, "buffer")):
1265 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1267 shutil
.copyfileobj(catfp
, sys
.stdout
);
1268 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1269 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
1271 upload_file_to_internet_file(catfp
, outfile
);
1279 def AppendFileHeaderWithContent(fp
, filevalues
=[], extradata
=[], filecontent
="", checksumtype
="crc32", formatspecs
=__file_format_list__
):
1280 extrafields
= format(len(extradata
), 'x').lower();
1281 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
1282 if(len(extradata
)>0):
1283 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
1284 extrasizelen
= format(len(extrasizestr
), 'x').lower();
1285 catoutlen
= len(filevalues
) + len(extradata
) + 5;
1286 catoutlenhex
= format(catoutlen
, 'x').lower();
1287 catoutlist
= filevalues
;
1288 catoutlist
.insert(0, catoutlenhex
);
1289 catoutlist
.append(extrasizelen
);
1290 catoutlist
.append(extrafields
);
1291 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
1292 if(len(extradata
)>0):
1293 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
1294 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
1295 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
1296 catfilecontentcshex
= GetFileChecksum(filecontent
, checksumtype
, False, formatspecs
);
1297 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1298 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
1299 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
1300 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
1301 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1302 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
1303 nullstrecd
= formatspecs
[5].encode('UTF-8');
1304 catfileout
= catfileoutstrecd
+ filecontent
+ nullstrecd
;
1305 fp
.write(catfileout
);
1308 os
.fsync(fp
.fileno());
1309 except io
.UnsupportedOperation
:
1311 except AttributeError:
1313 except OSError as e
:
1317 def AppendFilesWithContent(infiles
, fp
, dirlistfromtxt
=False, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False):
1318 advancedlist
= formatspecs
[8];
1319 altinode
= formatspecs
[9];
1321 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1323 for line
in sys
.stdin
:
1324 infilelist
.append(line
.strip());
1325 infilelist
= list(filter(None, infilelist
));
1326 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
1327 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
1329 with
UncompressFile(infiles
, formatspecs
, "r") as finfile
:
1330 for line
in finfile
:
1331 infilelist
.append(line
.strip());
1332 infilelist
= list(filter(None, infilelist
));
1334 if(isinstance(infiles
, (list, tuple, ))):
1335 infilelist
= list(filter(None, infiles
));
1336 elif(isinstance(infiles
, (str, ))):
1337 infilelist
= list(filter(None, [infiles
]));
1339 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
1341 GetDirList
= ListDir(infilelist
, followlink
, False);
1349 inodetocatinode
= {};
1350 numfiles
= int(len(GetDirList
));
1351 fnumfiles
= format(numfiles
, 'x').lower();
1352 AppendFileHeader(fp
, fnumfiles
, checksumtype
, formatspecs
);
1353 for curfname
in GetDirList
:
1354 if(re
.findall("^[.|/]", curfname
)):
1357 fname
= "./"+curfname
;
1359 VerbosePrintOut(fname
);
1360 if(not followlink
or followlink
is None):
1361 fstatinfo
= os
.lstat(fname
);
1363 fstatinfo
= os
.stat(fname
);
1364 fpremode
= fstatinfo
.st_mode
;
1365 finode
= fstatinfo
.st_ino
;
1366 flinkcount
= fstatinfo
.st_nlink
;
1368 if(stat
.S_ISREG(fpremode
)):
1370 elif(stat
.S_ISLNK(fpremode
)):
1372 elif(stat
.S_ISCHR(fpremode
)):
1374 elif(stat
.S_ISBLK(fpremode
)):
1376 elif(stat
.S_ISDIR(fpremode
)):
1378 elif(stat
.S_ISFIFO(fpremode
)):
1380 elif(stat
.S_ISSOCK(fpremode
)):
1382 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
1384 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
1386 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
1391 fcurfid
= format(int(curfid
), 'x').lower();
1392 if(not followlink
and finode
!=0):
1394 if(finode
in inodelist
):
1396 flinkname
= inodetofile
[finode
];
1398 fcurinode
= format(int(finode
), 'x').lower();
1400 fcurinode
= format(int(inodetocatinode
[finode
]), 'x').lower();
1401 if(finode
not in inodelist
):
1402 inodelist
.append(finode
);
1403 inodetofile
.update({finode
: fname
});
1404 inodetocatinode
.update({finode
: curinode
});
1406 fcurinode
= format(int(finode
), 'x').lower();
1408 fcurinode
= format(int(curinode
), 'x').lower();
1409 curinode
= curinode
+ 1;
1411 fcurinode
= format(int(curinode
), 'x').lower();
1412 curinode
= curinode
+ 1;
1413 curfid
= curfid
+ 1;
1415 flinkname
= os
.readlink(fname
);
1416 fdev
= fstatinfo
.st_dev
;
1417 getfdev
= GetDevMajorMinor(fdev
);
1418 fdev_minor
= getfdev
[0];
1419 fdev_major
= getfdev
[1];
1420 frdev
= fstatinfo
.st_dev
;
1421 if(hasattr(fstatinfo
, "st_rdev")):
1422 frdev
= fstatinfo
.st_rdev
;
1424 frdev
= fstatinfo
.st_dev
;
1425 getfrdev
= GetDevMajorMinor(frdev
);
1426 frdev_minor
= getfrdev
[0];
1427 frdev_major
= getfrdev
[1];
1428 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
1429 fsize
= format(int("0"), 'x').lower();
1430 elif(ftype
==0 or ftype
==7):
1431 fsize
= format(int(fstatinfo
.st_size
), 'x').lower();
1433 fsize
= format(int(fstatinfo
.st_size
)).lower();
1434 fatime
= format(int(fstatinfo
.st_atime
), 'x').lower();
1435 fmtime
= format(int(fstatinfo
.st_mtime
), 'x').lower();
1436 fctime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1437 if(hasattr(fstatinfo
, "st_birthtime")):
1438 fbtime
= format(int(fstatinfo
.st_birthtime
), 'x').lower();
1440 fbtime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1441 fmode
= format(int(fstatinfo
.st_mode
), 'x').lower();
1442 fchmode
= format(int(stat
.S_IMODE(fstatinfo
.st_mode
)), 'x').lower();
1443 ftypemod
= format(int(stat
.S_IFMT(fstatinfo
.st_mode
)), 'x').lower();
1444 fuid
= format(int(fstatinfo
.st_uid
), 'x').lower();
1445 fgid
= format(int(fstatinfo
.st_gid
), 'x').lower();
1450 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
1451 funame
= userinfo
.pw_name
;
1460 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
1461 fgname
= groupinfo
.gr_name
;
1466 fdev_minor
= format(int(fdev_minor
), 'x').lower();
1467 fdev_major
= format(int(fdev_major
), 'x').lower();
1468 frdev_minor
= format(int(frdev_minor
), 'x').lower();
1469 frdev_major
= format(int(frdev_major
), 'x').lower();
1470 finode
= format(int(finode
), 'x').lower();
1471 flinkcount
= format(int(flinkcount
), 'x').lower();
1472 if(hasattr(fstatinfo
, "st_file_attributes")):
1473 fwinattributes
= format(int(fstatinfo
.st_file_attributes
), 'x').lower();
1475 fwinattributes
= format(int(0), 'x').lower();
1476 fcontents
= BytesIO();
1478 if(ftype
==0 or ftype
==7):
1479 with
open(fname
, "rb") as fpc
:
1480 shutil
.copyfileobj(fpc
, fcontents
);
1481 if(followlink
and (ftype
==1 or ftype
==2)):
1482 flstatinfo
= os
.stat(flinkname
);
1483 with
open(flinkname
, "rb") as fpc
:
1484 shutil
.copyfileobj(fpc
, fcontents
);
1485 fcontents
.seek(0, 0);
1486 ftypehex
= format(ftype
, 'x').lower();
1487 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
];
1488 fp
= AppendFileHeaderWithContent(fp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
);
1490 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
1494 def AppendListsWithContent(inlist
, fp
, dirlistfromtxt
=False, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False):
1496 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1497 GetDirList
= inlist
;
1505 inodetocatinode
= {};
1506 numfiles
= int(len(GetDirList
));
1507 fnumfiles
= format(numfiles
, 'x').lower();
1508 AppendFileHeader(fp
, fnumfiles
, checksumtype
, formatspecs
);
1509 for curfname
in GetDirList
:
1510 ftype
= format(curfname
[0], 'x').lower();
1511 if(re
.findall("^[.|/]", curfname
[1])):
1512 fname
= curfname
[1];
1514 fname
= "./"+curfname
[1];
1515 fbasedir
= os
.path
.dirname(fname
);
1516 flinkname
= curfname
[2];
1517 fsize
= format(curfname
[3], 'x').lower();
1518 fatime
= format(curfname
[4], 'x').lower();
1519 fmtime
= format(curfname
[5], 'x').lower();
1520 fctime
= format(curfname
[6], 'x').lower();
1521 fbtime
= format(curfname
[7], 'x').lower();
1522 fmode
= format(curfname
[8], 'x').lower();
1523 fwinattributes
= format(curfname
[9], 'x').lower();
1524 fuid
= format(curfname
[10], 'x').lower();
1525 funame
= curfname
[11];
1526 fgid
= format(curfname
[12], 'x').lower();
1527 fgname
= curfname
[13];
1528 fid
= format(curfname
[14], 'x').lower();
1529 finode
= format(curfname
[15], 'x').lower();
1530 flinkcount
= format(curfname
[16], 'x').lower();
1531 fdev_minor
= format(curfname
[17], 'x').lower();
1532 fdev_major
= format(curfname
[18], 'x').lower();
1533 frdev_minor
= format(curfname
[19], 'x').lower();
1534 frdev_major
= format(curfname
[20], 'x').lower();
1535 extradata
= curfname
[21];
1536 fchecksumtype
= curfname
[22];
1537 fcontents
= curfname
[23];
1538 catoutlist
= [ftype
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fid
, finode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
];
1539 fp
= AppendFileHeaderWithContent(fp
, catoutlist
, extradata
, fcontents
, checksumtype
, formatspecs
);
1541 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
1545 def AppendInFileWithContent(infile
, fp
, dirlistfromtxt
=False, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False):
1546 inlist
= ReadInFileBySizeWithContentToList(infile
, 0, 0, False, False, formatspecs
);
1547 return AppendListsWithContent(inlist
, fp
, dirlistfromtxt
, filevalues
, extradata
, followlink
, checksumtype
, formatspecs
, verbose
);
1549 def AppendFilesWithContentToOutFile(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1550 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1551 if(os
.path
.exists(outfile
)):
1554 except OSError as e
:
1558 catfpfp
= BytesIO();
1559 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1561 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1564 fbasename
= os
.path
.splitext(outfile
)[0];
1565 fextname
= os
.path
.splitext(outfile
)[1];
1566 catfp
= CompressOpenFile(outfile
, True, compressionlevel
);
1567 catfp
= AppendFilesWithContent(infiles
, catfp
, dirlistfromtxt
, filevalues
, extradata
, followlink
, checksumtype
, formatspecs
, verbose
);
1568 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1569 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
1572 os
.fsync(catfp
.fileno());
1573 except io
.UnsupportedOperation
:
1575 except AttributeError:
1577 except OSError as e
:
1581 if(hasattr(sys
.stdout
, "buffer")):
1582 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1584 shutil
.copyfileobj(catfp
, sys
.stdout
);
1585 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1586 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
1588 upload_file_to_internet_file(catfp
, outfile
);
1596 def AppendListsWithContentToOutFile(inlist
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1597 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1598 if(os
.path
.exists(outfile
)):
1601 except OSError as e
:
1605 catfpfp
= BytesIO();
1606 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1608 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1611 fbasename
= os
.path
.splitext(outfile
)[0];
1612 fextname
= os
.path
.splitext(outfile
)[1];
1613 catfp
= CompressOpenFile(outfile
, True, compressionlevel
);
1614 catfp
= AppendListsWithContent(inlist
, catfp
, dirlistfromtxt
, filevalues
, extradata
, followlink
, checksumtype
, formatspecs
, verbose
);
1615 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1616 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
1619 os
.fsync(catfp
.fileno());
1620 except io
.UnsupportedOperation
:
1622 except AttributeError:
1624 except OSError as e
:
1628 if(hasattr(sys
.stdout
, "buffer")):
1629 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1631 shutil
.copyfileobj(catfp
, sys
.stdout
);
1632 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1633 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
1635 upload_file_to_internet_file(catfp
, outfile
);
1643 def AppendInFileWithContentToOutFile(infile
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1644 inlist
= ReadInFileBySizeWithContentToList(infile
, 0, 0, False, False, formatspecs
);
1645 return AppendListsWithContentToOutFile(inlist
, outfile
, dirlistfromtxt
, compression
, compressionlevel
, filevalues
, extradata
, followlink
, checksumtype
, formatspecs
, verbose
, returnfp
);
1647 def PrintPermissionString(fchmode
, ftype
):
1648 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' } };
1650 for fmodval
in str(oct(fchmode
))[-3:]:
1651 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
1652 if(ftype
==0 or ftype
==7):
1653 permissionstr
= "-" + permissionstr
;
1655 permissionstr
= "h" + permissionstr
;
1657 permissionstr
= "l" + permissionstr
;
1659 permissionstr
= "c" + permissionstr
;
1661 permissionstr
= "b" + permissionstr
;
1663 permissionstr
= "d" + permissionstr
;
1665 permissionstr
= "f" + permissionstr
;
1667 permissionstr
= "D" + permissionstr
;
1669 permissionstr
= "p" + permissionstr
;
1671 permissionstr
= "w" + permissionstr
;
1673 permissionoutstr
= stat
.filemode(fchmode
);
1674 except AttributeError:
1675 permissionoutstr
= permissionstr
;
1677 permissionoutstr
= permissionstr
;
1678 return permissionoutstr
;
1680 def PrintPermissionStringAlt(fchmode
, ftype
):
1682 '0': '---', '1': '--x', '2': '-w-', '3': '-wx',
1683 '4': 'r--', '5': 'r-x', '6': 'rw-', '7': 'rwx'
1685 # Translate file mode into permission string
1686 permissionstr
= ''.join([permissions
[i
] for i
in str(oct(fchmode
))[-3:]]);
1687 # Append file type indicator
1689 0: '-', 1: 'h', 2: 'l', 3: 'c', 4: 'b',
1690 5: 'd', 6: 'f', 8: 'D', 9: 'p', 10: 'w'
1692 file_type
= type_indicators
.get(ftype
, '-');
1693 permissionstr
= file_type
+ permissionstr
;
1695 permissionoutstr
= stat
.filemode(fchmode
);
1696 except AttributeError:
1697 permissionoutstr
= permissionstr
;
1698 return permissionoutstr
;
1700 def CompressionSupport():
1701 compression_list
= [];
1704 compression_list
.append("gz");
1705 compression_list
.append("gzip");
1710 compression_list
.append("bz2");
1711 compression_list
.append("bzip2");
1716 compression_list
.append("lz4");
1721 compression_list
.append("lzo");
1722 compression_list
.append("lzop");
1727 compression_list
.append("zstd");
1728 compression_list
.append("zstandard");
1733 compression_list
.append("lzma");
1734 compression_list
.append("xz");
1737 return compression_list
;
1739 def CheckCompressionType(infile
, formatspecs
=__file_format_list__
, closefp
=True):
1740 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1744 catfp
= open(infile
, "rb");
1745 except FileNotFoundError
:
1748 prefp
= catfp
.read(2);
1750 if(prefp
==binascii
.unhexlify("1f8b")):
1753 prefp
= catfp
.read(3);
1754 if(prefp
==binascii
.unhexlify("425a68")):
1756 if(prefp
==binascii
.unhexlify("5d0000")):
1759 prefp
= catfp
.read(4);
1760 if(prefp
==binascii
.unhexlify("28b52ffd")):
1762 if(prefp
==binascii
.unhexlify("04224d18")):
1764 if(prefp
==binascii
.unhexlify("504B0304")):
1765 filetype
= "zipfile";
1767 prefp
= catfp
.read(5);
1768 if(prefp
==binascii
.unhexlify("7573746172")):
1769 filetype
= "tarfile";
1771 prefp
= catfp
.read(6);
1772 if(prefp
==binascii
.unhexlify("fd377a585a00")):
1774 if(prefp
==binascii
.unhexlify("377abcaf271c")):
1775 filetype
= "7zipfile";
1777 prefp
= catfp
.read(7);
1778 if(prefp
==binascii
.unhexlify("526172211a0700")):
1779 filetype
= "rarfile";
1780 if(prefp
==binascii
.unhexlify("43617446696c65")):
1781 filetype
= "catfile";
1783 prefp
= catfp
.read(8);
1784 if(prefp
==binascii
.unhexlify("526172211a070100")):
1785 filetype
= "rarfile";
1787 prefp
= catfp
.read(formatspecs
[3]);
1788 if(prefp
==binascii
.unhexlify(formatspecs
[4])):
1789 filetype
= formatspecs
[2];
1791 prefp
= catfp
.read(9);
1792 if(prefp
==binascii
.unhexlify("894c5a4f000d0a1a0a")):
1795 prefp
= catfp
.read(10);
1796 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
1797 filetype
= "tarfile";
1799 if(filetype
=="gzip" or filetype
=="bzip2" or filetype
=="lzma" or filetype
=="zstd" or filetype
=="lz4"):
1800 if(is_tarfile(catfp
)):
1801 filetype
= "tarfile";
1803 if(is_tarfile(catfp
)):
1804 filetype
= "tarfile";
1805 elif(zipfile
.is_zipfile(catfp
)):
1806 filetype
= "zipfile";
1807 elif(rarfile
.is_rarfile(catfp
) or rarfile
.is_rarfile_sfx(catfp
)):
1808 filetype
= "rarile";
1816 def CheckCompressionTypeFromString(instring
, formatspecs
=__file_format_list__
, closefp
=True):
1818 instringsfile
= BytesIO(instring
);
1820 instringsfile
= BytesIO(instring
.encode("UTF-8"));
1821 return CheckCompressionType(instringsfile
, formatspecs
, closefp
);
1823 def GetCompressionMimeType(infile
, formatspecs
=__file_format_list__
):
1824 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
1825 if(compresscheck
=="gzip" or compresscheck
=="gz"):
1826 return archivefile_gzip_mimetype
;
1827 if(compresscheck
=="bzip2" or compresscheck
=="bz2"):
1828 return archivefile_bzip2_mimetype
;
1829 if(compresscheck
=="zstd" or compresscheck
=="zstandard"):
1830 return archivefile_zstandard_mimetype
;
1831 if(compresscheck
=="lz4"):
1832 return archivefile_lz4_mimetype
;
1833 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
1834 return archivefile_lzop_mimetype
;
1835 if(compresscheck
=="lzma"):
1836 return archivefile_lzma_mimetype
;
1837 if(compresscheck
=="xz"):
1838 return archivefile_xz_mimetype
;
1839 if(compresscheck
=="catfile" or compresscheck
=="cat" or compresscheck
==formatspecs
[2]):
1840 return archivefile_cat_mimetype
;
1841 if(not compresscheck
):
1845 def UncompressArchiveFile(fp
, formatspecs
=__file_format_list__
):
1846 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
1848 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
1849 if(compresscheck
=="gzip"):
1854 catfp
= gzip
.GzipFile(fileobj
=fp
, mode
="rb");
1855 if(compresscheck
=="bzip2"):
1861 catfp
.write(bz2
.decompress(fp
.read()));
1862 if(compresscheck
=="zstd"):
1868 catfp
.write(zstandard
.decompress(fp
.read()));
1869 if(compresscheck
=="lz4"):
1875 catfp
.write(lz4
.frame
.decompress(fp
.read()));
1876 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
1882 catfp
.write(lzo
.decompress(fp
.read()));
1883 if(compresscheck
=="lzma" or compresscheck
=="xz"):
1889 catfp
.write(lzma
.decompress(fp
.read()));
1890 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[2]):
1892 if(not compresscheck
):
1898 with fp
as fpcontent
:
1900 catfp
.write(lzma
.decompress(fp
.read()));
1901 except lzma
.LZMAError
:
1903 if(compresscheck
!="catfile" or compresscheck
!=formatspecs
[2]):
1907 create_alias_function("Uncompress", __file_format_name__
, "", UncompressArchiveFile
);
1909 def UncompressFile(infile
, formatspecs
=__file_format_list__
, mode
="rb"):
1910 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
1911 if(sys
.version_info
[0]==2 and compresscheck
):
1917 if(compresscheck
=="gzip"):
1923 filefp
= gzip
.open(infile
, mode
, encoding
="UTF-8");
1924 except (ValueError, TypeError) as e
:
1925 filefp
= gzip
.open(infile
, mode
);
1926 if(compresscheck
=="bzip2"):
1932 filefp
= bz2
.open(infile
, mode
, encoding
="UTF-8");
1933 except (ValueError, TypeError) as e
:
1934 filefp
= bz2
.open(infile
, mode
);
1935 if(compresscheck
=="zstd"):
1941 filefp
= zstandard
.open(infile
, mode
, encoding
="UTF-8");
1942 except (ValueError, TypeError) as e
:
1943 filefp
= zstandard
.open(infile
, mode
);
1944 if(compresscheck
=="lz4"):
1950 filefp
= lz4
.frame
.open(infile
, mode
, encoding
="UTF-8");
1951 except (ValueError, TypeError) as e
:
1952 filefp
= lz4
.frame
.open(infile
, mode
);
1953 if(compresscheck
=="lzo"):
1959 filefp
= lzo
.open(infile
, mode
, encoding
="UTF-8");
1960 except (ValueError, TypeError) as e
:
1961 filefp
= lzo
.open(infile
, mode
);
1962 if(compresscheck
=="lzma"):
1968 filefp
= lzma
.open(infile
, mode
, encoding
="UTF-8");
1969 except (ValueError, TypeError) as e
:
1970 filefp
= lzma
.open(infile
, mode
);
1971 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[2]):
1973 filefp
= open(infile
, mode
, encoding
="UTF-8");
1974 except (ValueError, TypeError) as e
:
1975 filefp
= open(infile
, mode
);
1976 if(not compresscheck
):
1978 filefp
= open(infile
, mode
, encoding
="UTF-8");
1979 except (ValueError, TypeError) as e
:
1980 filefp
= open(infile
, mode
);
1981 except FileNotFoundError
:
1985 def UncompressString(infile
):
1986 compresscheck
= CheckCompressionTypeFromString(infile
, formatspecs
, False);
1987 if(compresscheck
=="gzip"):
1992 fileuz
= gzip
.decompress(infile
);
1993 if(compresscheck
=="bzip2"):
1998 fileuz
= bz2
.decompress(infile
);
1999 if(compresscheck
=="zstd"):
2004 fileuz
= zstandard
.decompress(infile
);
2005 if(compresscheck
=="lz4"):
2010 fileuz
= lz4
.frame
.decompress(infile
);
2011 if(compresscheck
=="lzo"):
2016 fileuz
= lzo
.decompress(infile
);
2017 if(compresscheck
=="lzma"):
2022 fileuz
= lzma
.decompress(infile
);
2023 if(not compresscheck
):
2025 if(hasattr(fileuz
, 'decode')):
2026 fileuz
= fileuz
.decode("UTF-8");
2029 def UncompressStringAlt(infile
):
2030 filefp
= StringIO();
2031 outstring
= UncompressString(infile
);
2032 filefp
.write(outstring
);
2036 def CheckCompressionSubType(infile
, formatspecs
=__file_format_list__
, closefp
=True):
2037 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
2038 if(not compresscheck
):
2039 fextname
= os
.path
.splitext(infile
)[1];
2040 if(fextname
==".gz"):
2041 compresscheck
= "gzip";
2042 elif(fextname
==".bz2"):
2043 compresscheck
= "bzip2";
2044 elif(fextname
==".zst"):
2045 compresscheck
= "zstd";
2046 elif(fextname
==".lz4"):
2047 compresscheck
= "lz4";
2048 elif(fextname
==".lzo" or fextname
==".lzop"):
2049 compresscheck
= "lzo";
2050 elif(fextname
==".lzma" or fextname
==".xz"):
2051 compresscheck
= "lzma";
2054 if(compresscheck
=="gzip" or compresscheck
=="bzip2" or compresscheck
=="lzma" or compresscheck
=="zstd" or compresscheck
=="lz4"):
2055 if(is_tarfile(infile
)):
2056 filetype
= "tarfile";
2057 if(not compresscheck
):
2058 if(is_tarfile(infile
)):
2060 elif(zipfile
.is_zipfile(infile
)):
2062 elif(rarfile
.is_rarfile(infile
) or rarfile
.is_rarfile_sfx(infile
)):
2067 if(compresscheck
=="catfile"):
2069 if(compresscheck
==formatspecs
[2]):
2070 return formatspecs
[2];
2071 if(compresscheck
=="tarfile"):
2073 if(compresscheck
=="zipfile"):
2075 if(rarfile_support
and compresscheck
=="rarfile"):
2077 if(py7zr_support
and compresscheck
=="7zipfile"):
2079 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2080 catfp
= UncompressArchiveFile(infile
, formatspecs
[2]);
2083 if(compresscheck
=="gzip"):
2088 catfp
= gzip
.GzipFile(infile
, "rb");
2089 if(compresscheck
=="bzip2"):
2094 catfp
= bz2
.BZ2File(infile
, "rb");
2095 if(compresscheck
=="lz4"):
2100 catfp
= lz4
.frame
.open(infile
, "rb");
2101 if(compresscheck
=="zstd"):
2106 catfp
= zstandard
.open(infile
, "rb");
2107 if(compresscheck
=="lzma" or compresscheck
=="xz"):
2112 catfp
= lzma
.open(infile
, "rb");
2113 except FileNotFoundError
:
2116 prefp
= catfp
.read(5);
2117 if(prefp
==binascii
.unhexlify("7573746172")):
2118 filetype
= "tarfile";
2120 prefp
= catfp
.read(7);
2121 if(prefp
==binascii
.unhexlify("43617446696c65")):
2122 filetype
= "catfile";
2124 prefp
= catfp
.read(formatspecs
[3]);
2125 if(prefp
==binascii
.unhexlify(formatspecs
[4])):
2126 filetype
= formatspecs
[2];
2128 prefp
= catfp
.read(10);
2129 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
2130 filetype
= "tarfile";
2136 def GZipCompress(data
, compresslevel
=9):
2141 tmpfp
= tempfile
.NamedTemporaryFile("wb", delete
=False);
2143 tmpfp
= gzip
.GzipFile(tmpfp
.name
, mode
="wb", compresslevel
=compresslevel
);
2147 catfp
= open(tmpfp
.name
, "rb");
2148 except FileNotFoundError
:
2150 catdata
= catfp
.read();
2154 def CompressArchiveFile(fp
, compression
="auto", compressionlevel
=None, formatspecs
=__file_format_list__
):
2155 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
2158 if(not compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2159 compression
= "auto";
2160 if(compression
not in compressionlist
and compression
is None):
2161 compression
= "auto";
2162 if(compression
=="gzip"):
2168 if(compressionlevel
is None):
2169 compressionlevel
= 9;
2171 compressionlevel
= int(compressionlevel
);
2172 catfp
.write(gzip
.compress(fp
.read(), compresslevel
=compressionlevel
));
2173 if(compression
=="bzip2"):
2179 if(compressionlevel
is None):
2180 compressionlevel
= 9;
2182 compressionlevel
= int(compressionlevel
);
2183 catfp
.write(bz2
.compress(fp
.read(), compresslevel
=compressionlevel
));
2184 if(compression
=="lz4"):
2190 if(compressionlevel
is None):
2191 compressionlevel
= 9;
2193 compressionlevel
= int(compressionlevel
);
2194 catfp
.write(lz4
.frame
.compress(fp
.read(), compression_level
=compressionlevel
));
2195 if(compression
=="lzo" or compression
=="lzop"):
2201 if(compressionlevel
is None):
2202 compressionlevel
= 9;
2204 compressionlevel
= int(compressionlevel
);
2205 catfp
.write(lzo
.compress(fp
.read(), compresslevel
=compressionlevel
));
2206 if(compression
=="zstd"):
2212 if(compressionlevel
is None):
2213 compressionlevel
= 10;
2215 compressionlevel
= int(compressionlevel
);
2216 catfp
.write(zstandard
.compress(fp
.read(), level
=compressionlevel
));
2217 if(compression
=="lzma"):
2223 if(compressionlevel
is None):
2224 compressionlevel
= 9;
2226 compressionlevel
= int(compressionlevel
);
2227 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]));
2228 if(compression
=="xz"):
2234 if(compressionlevel
is None):
2235 compressionlevel
= 9;
2237 compressionlevel
= int(compressionlevel
);
2238 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]));
2239 if(compression
=="auto" or compression
is None):
2244 create_alias_function("Compress", __file_format_name__
, "", CompressArchiveFile
);
2246 def CompressOpenFile(outfile
, compressionenable
=True, compressionlevel
=None):
2247 if(outfile
is None):
2249 fbasename
= os
.path
.splitext(outfile
)[0];
2250 fextname
= os
.path
.splitext(outfile
)[1];
2251 if(compressionlevel
is None and fextname
!=".zst"):
2252 compressionlevel
= 9;
2253 elif(compressionlevel
is None and fextname
==".zst"):
2254 compressionlevel
= 10;
2256 compressionlevel
= int(compressionlevel
);
2257 if(sys
.version_info
[0]==2):
2262 if(fextname
not in outextlistwd
or not compressionenable
):
2264 outfp
= open(outfile
, "wb", encoding
="UTF-8");
2265 except (ValueError, TypeError) as e
:
2266 outfp
= open(outfile
, "wb");
2267 elif(fextname
==".gz"):
2273 outfp
= gzip
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
2274 except (ValueError, TypeError) as e
:
2275 outfp
= gzip
.open(outfile
, mode
, compressionlevel
);
2276 elif(fextname
==".bz2"):
2282 outfp
= bz2
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
2283 except (ValueError, TypeError) as e
:
2284 outfp
= bz2
.open(outfile
, mode
, compressionlevel
);
2285 elif(fextname
==".zst"):
2291 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
), encoding
="UTF-8");
2292 except (ValueError, TypeError) as e
:
2293 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
));
2294 elif(fextname
==".xz"):
2300 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}], encoding
="UTF-8");
2301 except (ValueError, TypeError) as e
:
2302 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]);
2303 elif(fextname
==".lz4"):
2309 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
, encoding
="UTF-8");
2310 except (ValueError, TypeError) as e
:
2311 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
);
2312 elif(fextname
==".lzo"):
2318 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
, encoding
="UTF-8");
2319 except (ValueError, TypeError) as e
:
2320 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
);
2321 elif(fextname
==".lzma"):
2327 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}], encoding
="UTF-8");
2328 except (ValueError, TypeError) as e
:
2329 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]);
2330 except FileNotFoundError
:
2334 def GetDevMajorMinor(fdev
):
2336 if(hasattr(os
, "minor")):
2337 retdev
.append(os
.minor(fdev
));
2340 if(hasattr(os
, "major")):
2341 retdev
.append(os
.major(fdev
));
2346 def CheckSumSupport(checkfor
, guaranteed
=True):
2348 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
2350 hash_list
= sorted(list(hashlib
.algorithms_available
));
2351 checklistout
= sorted(hash_list
+ ['adler32', 'crc16', 'crc16_ansi', 'crc16_ibm', 'crc16_ccitt', 'crc32', 'crc64', 'crc64_ecma', 'crc64_iso', 'none']);
2352 if(checkfor
in checklistout
):
2357 def CheckSumSupportAlt(checkfor
, guaranteed
=True):
2359 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
2361 hash_list
= sorted(list(hashlib
.algorithms_available
));
2362 checklistout
= hash_list
;
2363 if(checkfor
in checklistout
):
2368 def PackArchiveFile(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2369 advancedlist
= formatspecs
[8];
2370 altinode
= formatspecs
[9];
2371 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2372 outfile
= RemoveWindowsPath(outfile
);
2373 checksumtype
= checksumtype
.lower();
2374 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2375 checksumtype
="crc32";
2376 if(checksumtype
=="none"):
2378 if(not compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2379 compression
= "auto";
2380 if(compression
not in compressionlist
and compression
is None):
2381 compression
= "auto";
2383 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2384 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2385 if(os
.path
.exists(outfile
)):
2388 except OSError as e
:
2393 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2395 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2398 fbasename
= os
.path
.splitext(outfile
)[0];
2399 fextname
= os
.path
.splitext(outfile
)[1];
2400 catfp
= CompressOpenFile(outfile
, True, compressionlevel
);
2401 catver
= formatspecs
[6];
2402 fileheaderver
= str(int(catver
.replace(".", "")));
2403 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
2404 catfp
.write(fileheader
.encode('UTF-8'));
2407 for line
in sys
.stdin
:
2408 infilelist
.append(line
.strip());
2409 infilelist
= list(filter(None, infilelist
));
2410 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
2411 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
2413 with
UncompressFile(infiles
, formatspecs
, "r") as finfile
:
2414 for line
in finfile
:
2415 infilelist
.append(line
.strip());
2416 infilelist
= list(filter(None, infilelist
));
2418 if(isinstance(infiles
, (list, tuple, ))):
2419 infilelist
= list(filter(None, infiles
));
2420 elif(isinstance(infiles
, (str, ))):
2421 infilelist
= list(filter(None, [infiles
]));
2423 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
2425 GetDirList
= ListDir(infilelist
, followlink
, False);
2433 inodetocatinode
= {};
2434 numfiles
= int(len(GetDirList
));
2435 fnumfiles
= format(int(len(GetDirList
)), 'x').lower();
2436 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2437 catfileheadercshex
= GetFileChecksum(fileheader
+ fnumfilesa
, checksumtype
, True, formatspecs
);
2438 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2439 catfp
.write(fnumfilesa
.encode('UTF-8'));
2442 os
.fsync(catfp
.fileno());
2443 except io
.UnsupportedOperation
:
2445 except AttributeError:
2447 except OSError as e
:
2449 for curfname
in GetDirList
:
2450 if(re
.findall("^[.|/]", curfname
)):
2453 fname
= "./"+curfname
;
2455 VerbosePrintOut(fname
);
2456 if(not followlink
or followlink
is None):
2457 fstatinfo
= os
.lstat(fname
);
2459 fstatinfo
= os
.stat(fname
);
2460 fpremode
= fstatinfo
.st_mode
;
2461 finode
= fstatinfo
.st_ino
;
2462 flinkcount
= fstatinfo
.st_nlink
;
2464 if(stat
.S_ISREG(fpremode
)):
2466 elif(stat
.S_ISLNK(fpremode
)):
2468 elif(stat
.S_ISCHR(fpremode
)):
2470 elif(stat
.S_ISBLK(fpremode
)):
2472 elif(stat
.S_ISDIR(fpremode
)):
2474 elif(stat
.S_ISFIFO(fpremode
)):
2476 elif(stat
.S_ISSOCK(fpremode
)):
2478 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
2480 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
2482 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
2487 fcurfid
= format(int(curfid
), 'x').lower();
2488 if(not followlink
and finode
!=0):
2490 if(finode
in inodelist
):
2492 flinkname
= inodetofile
[finode
];
2494 fcurinode
= format(int(finode
), 'x').lower();
2496 fcurinode
= format(int(inodetocatinode
[finode
]), 'x').lower();
2497 if(finode
not in inodelist
):
2498 inodelist
.append(finode
);
2499 inodetofile
.update({finode
: fname
});
2500 inodetocatinode
.update({finode
: curinode
});
2502 fcurinode
= format(int(finode
), 'x').lower();
2504 fcurinode
= format(int(curinode
), 'x').lower();
2505 curinode
= curinode
+ 1;
2507 fcurinode
= format(int(curinode
), 'x').lower();
2508 curinode
= curinode
+ 1;
2509 curfid
= curfid
+ 1;
2511 flinkname
= os
.readlink(fname
);
2512 fdev
= fstatinfo
.st_dev
;
2513 getfdev
= GetDevMajorMinor(fdev
);
2514 fdev_minor
= getfdev
[0];
2515 fdev_major
= getfdev
[1];
2516 frdev
= fstatinfo
.st_dev
;
2517 if(hasattr(fstatinfo
, "st_rdev")):
2518 frdev
= fstatinfo
.st_rdev
;
2520 frdev
= fstatinfo
.st_dev
;
2521 getfrdev
= GetDevMajorMinor(frdev
);
2522 frdev_minor
= getfrdev
[0];
2523 frdev_major
= getfrdev
[1];
2524 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
2525 fsize
= format(int("0"), 'x').lower();
2526 elif(ftype
==0 or ftype
==7):
2527 fsize
= format(int(fstatinfo
.st_size
), 'x').lower();
2529 fsize
= format(int(fstatinfo
.st_size
)).lower();
2530 fatime
= format(int(fstatinfo
.st_atime
), 'x').lower();
2531 fmtime
= format(int(fstatinfo
.st_mtime
), 'x').lower();
2532 fctime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
2533 if(hasattr(fstatinfo
, "st_birthtime")):
2534 fbtime
= format(int(fstatinfo
.st_birthtime
), 'x').lower();
2536 fbtime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
2537 fmode
= format(int(fstatinfo
.st_mode
), 'x').lower();
2538 fchmode
= format(int(stat
.S_IMODE(fstatinfo
.st_mode
)), 'x').lower();
2539 ftypemod
= format(int(stat
.S_IFMT(fstatinfo
.st_mode
)), 'x').lower();
2540 fuid
= format(int(fstatinfo
.st_uid
), 'x').lower();
2541 fgid
= format(int(fstatinfo
.st_gid
), 'x').lower();
2546 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
2547 funame
= userinfo
.pw_name
;
2556 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
2557 fgname
= groupinfo
.gr_name
;
2562 fdev_minor
= format(int(fdev_minor
), 'x').lower();
2563 fdev_major
= format(int(fdev_major
), 'x').lower();
2564 frdev_minor
= format(int(frdev_minor
), 'x').lower();
2565 frdev_major
= format(int(frdev_major
), 'x').lower();
2566 finode
= format(int(finode
), 'x').lower();
2567 flinkcount
= format(int(flinkcount
), 'x').lower();
2568 if(hasattr(fstatinfo
, "st_file_attributes")):
2569 fwinattributes
= format(int(fstatinfo
.st_file_attributes
), 'x').lower();
2571 fwinattributes
= format(int(0), 'x').lower();
2572 fcontents
= BytesIO();
2573 if(ftype
==0 or ftype
==7):
2574 with
open(fname
, "rb") as fpc
:
2575 shutil
.copyfileobj(fpc
, fcontents
);
2576 if(followlink
and (ftype
==1 or ftype
==2)):
2577 flstatinfo
= os
.stat(flinkname
);
2578 with
open(flinkname
, "rb") as fpc
:
2579 shutil
.copyfileobj(fpc
, fcontents
);
2580 fcontents
.seek(0, 0);
2581 ftypehex
= format(ftype
, 'x').lower();
2582 extrafields
= format(len(extradata
), 'x').lower();
2583 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
2584 if(len(extradata
)>0):
2585 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2586 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2587 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
];
2588 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
2589 catoutlenhex
= format(catoutlen
, 'x').lower();
2590 catoutlist
.insert(0, catoutlenhex
);
2591 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
2592 if(len(extradata
)>0):
2593 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2594 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
2595 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
2596 fcontents
.seek(0, 0);
2597 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
2598 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2599 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
2600 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
2601 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
2602 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2603 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
2604 nullstrecd
= formatspecs
[5].encode('UTF-8');
2605 fcontents
.seek(0, 0);
2606 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
2607 catfp
.write(catfileout
);
2610 os
.fsync(catfp
.fileno());
2611 except io
.UnsupportedOperation
:
2613 except AttributeError:
2615 except OSError as e
:
2618 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
2619 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2620 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
2623 os
.fsync(catfp
.fileno());
2624 except io
.UnsupportedOperation
:
2626 except AttributeError:
2628 except OSError as e
:
2632 if(hasattr(sys
.stdout
, "buffer")):
2633 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2635 shutil
.copyfileobj(catfp
, sys
.stdout
);
2636 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2637 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
2639 upload_file_to_internet_file(catfp
, outfile
);
2647 create_alias_function("Pack", __file_format_name__
, "", PackArchiveFile
);
2649 if(hasattr(shutil
, "register_archive_format")):
2650 def PackArchiveFileFunc(archive_name
, source_dir
, **kwargs
):
2651 return PackArchiveFile(source_dir
, archive_name
, False, "auto", None, False, "crc32", [], __file_format_delimiter__
, False, False);
2652 create_alias_function("Pack", __file_format_name__
, "Func", PackArchiveFileFunc
);
2654 def PackArchiveFileFromDirList(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2655 return PackArchiveFile(infiles
, outfile
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
2657 create_alias_function("Pack", __file_format_name__
, "FromDirList", PackArchiveFileFromDirList
);
2659 def PackArchiveFileFromTarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2660 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2661 outfile
= RemoveWindowsPath(outfile
);
2662 checksumtype
= checksumtype
.lower();
2663 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2664 checksumtype
="crc32";
2665 if(checksumtype
=="none"):
2667 if(not compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2668 compression
= "auto";
2669 if(compression
not in compressionlist
and compression
is None):
2670 compression
= "auto";
2672 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2673 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2674 if(os
.path
.exists(outfile
)):
2677 except OSError as e
:
2682 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2684 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2687 fbasename
= os
.path
.splitext(outfile
)[0];
2688 fextname
= os
.path
.splitext(outfile
)[1];
2689 catfp
= CompressOpenFile(outfile
, True, compressionlevel
);
2690 catver
= formatspecs
[6];
2691 fileheaderver
= str(int(catver
.replace(".", "")));
2692 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
2693 catfp
.write(fileheader
.encode('UTF-8'));
2699 inodetocatinode
= {};
2702 if(hasattr(sys
.stdin
, "buffer")):
2703 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
2705 shutil
.copyfileobj(sys
.stdin
, infile
);
2710 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
2711 infile
= download_file_from_internet_file(infile
);
2716 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2718 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
2720 if(not tarfile
.is_tarfile(infile
)):
2722 except AttributeError:
2723 if(not is_tarfile(infile
)):
2728 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2729 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
2731 tarfp
= tarfile
.open(infile
, "r");
2732 except FileNotFoundError
:
2734 numfiles
= int(len(tarfp
.getmembers()));
2735 fnumfiles
= format(int(len(tarfp
.getmembers())), 'x').lower();
2736 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2737 catfileheadercshex
= GetFileChecksum(fileheader
+ fnumfilesa
, checksumtype
, True, formatspecs
);
2738 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2739 catfp
.write(fnumfilesa
.encode('UTF-8'));
2742 os
.fsync(catfp
.fileno());
2743 except io
.UnsupportedOperation
:
2745 except AttributeError:
2747 except OSError as e
:
2749 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
2750 if(re
.findall("^[.|/]", member
.name
)):
2751 fname
= member
.name
;
2753 fname
= "./"+member
.name
;
2755 VerbosePrintOut(fname
);
2756 fpremode
= member
.mode
;
2757 ffullmode
= member
.mode
;
2761 ffullmode
= member
.mode
+ stat
.S_IFREG
;
2763 elif(member
.isdev()):
2764 ffullmode
= member
.mode
;
2766 elif(member
.islnk()):
2767 ffullmode
= member
.mode
+ stat
.S_IFREG
;
2769 elif(member
.issym()):
2770 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
2772 elif(member
.ischr()):
2773 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
2775 elif(member
.isblk()):
2776 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
2778 elif(member
.isdir()):
2779 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
2781 elif(member
.isfifo()):
2782 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
2784 elif(member
.issparse()):
2785 ffullmode
= member
.mode
;
2788 ffullmode
= member
.mode
;
2791 fcurfid
= format(int(curfid
), 'x').lower();
2792 fcurinode
= format(int(curfid
), 'x').lower();
2793 curfid
= curfid
+ 1;
2795 flinkname
= member
.linkname
;
2796 fdev_minor
= format(int(member
.devminor
), 'x').lower();
2797 fdev_major
= format(int(member
.devmajor
), 'x').lower();
2798 frdev_minor
= format(int(member
.devminor
), 'x').lower();
2799 frdev_major
= format(int(member
.devmajor
), 'x').lower();
2800 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
2801 fsize
= format(int("0"), 'x').lower();
2802 elif(ftype
==0 or ftype
==7):
2803 fsize
= format(int(member
.size
), 'x').lower();
2805 fsize
= format(int(member
.size
), 'x').lower();
2806 fatime
= format(int(member
.mtime
), 'x').lower();
2807 fmtime
= format(int(member
.mtime
), 'x').lower();
2808 fctime
= format(int(member
.mtime
), 'x').lower();
2809 fbtime
= format(int(member
.mtime
), 'x').lower();
2810 fmode
= format(int(ffullmode
), 'x').lower();
2811 fchmode
= format(int(stat
.S_IMODE(ffullmode
)), 'x').lower();
2812 ftypemod
= format(int(stat
.S_IFMT(ffullmode
)), 'x').lower();
2813 fuid
= format(int(member
.uid
), 'x').lower();
2814 fgid
= format(int(member
.gid
), 'x').lower();
2815 funame
= member
.uname
;
2816 fgname
= member
.gname
;
2817 flinkcount
= format(int(flinkcount
), 'x').lower();
2818 fwinattributes
= format(int(0), 'x').lower();
2819 fcontents
= BytesIO();
2820 if(ftype
==0 or ftype
==7):
2821 with tarfp
.extractfile(member
) as fpc
:
2822 shutil
.copyfileobj(fpc
, fcontents
);
2823 fcontents
.seek(0, 0);
2824 ftypehex
= format(ftype
, 'x').lower();
2825 extrafields
= format(len(extradata
), 'x').lower();
2826 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
2827 if(len(extradata
)>0):
2828 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2829 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2830 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
];
2831 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
2832 catoutlenhex
= format(catoutlen
, 'x').lower();
2833 catoutlist
.insert(0, catoutlenhex
);
2834 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
2835 if(len(extradata
)>0):
2836 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2837 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
2838 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
2839 fcontents
.seek(0, 0);
2840 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
2841 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2842 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
2843 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
2844 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
2845 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2846 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
2847 nullstrecd
= formatspecs
[5].encode('UTF-8');
2848 fcontents
.seek(0, 0);
2849 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
2850 catfp
.write(catfileout
);
2853 os
.fsync(catfp
.fileno());
2854 except io
.UnsupportedOperation
:
2856 except AttributeError:
2858 except OSError as e
:
2861 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
2862 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2863 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
2866 os
.fsync(catfp
.fileno());
2867 except io
.UnsupportedOperation
:
2869 except AttributeError:
2871 except OSError as e
:
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)\:\/\/", str(outfile
))):
2880 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
2882 upload_file_to_internet_file(catfp
, outfile
);
2890 create_alias_function("Pack", __file_format_name__
, "FromTarFile", PackArchiveFileFromTarFile
);
2892 def PackArchiveFileFromZipFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2893 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2894 outfile
= RemoveWindowsPath(outfile
);
2895 checksumtype
= checksumtype
.lower();
2896 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2897 checksumtype
="crc32";
2898 if(checksumtype
=="none"):
2900 if(not compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2901 compression
= "auto";
2902 if(compression
not in compressionlist
and compression
is None):
2903 compression
= "auto";
2905 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2906 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2907 if(os
.path
.exists(outfile
)):
2910 except OSError as e
:
2915 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2917 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2920 fbasename
= os
.path
.splitext(outfile
)[0];
2921 fextname
= os
.path
.splitext(outfile
)[1];
2922 catfp
= CompressOpenFile(outfile
, True, compressionlevel
);
2923 catver
= formatspecs
[6];
2924 fileheaderver
= str(int(catver
.replace(".", "")));
2925 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
2926 catfp
.write(fileheader
.encode('UTF-8'));
2932 inodetocatinode
= {};
2935 if(hasattr(sys
.stdin
, "buffer")):
2936 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
2938 shutil
.copyfileobj(sys
.stdin
, infile
);
2943 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
2944 infile
= download_file_from_internet_file(infile
);
2949 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2953 if(not zipfile
.is_zipfile(infile
)):
2956 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
2957 except FileNotFoundError
:
2959 ziptest
= zipfp
.testzip();
2961 VerbosePrintOut("Bad file found!");
2962 numfiles
= int(len(zipfp
.infolist()));
2963 fnumfiles
= format(int(len(zipfp
.infolist())), 'x').lower();
2964 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2965 catfileheadercshex
= GetFileChecksum(fileheader
+ fnumfilesa
, checksumtype
, True, formatspecs
);
2966 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2967 catfp
.write(fnumfilesa
.encode('UTF-8'));
2970 os
.fsync(catfp
.fileno());
2971 except io
.UnsupportedOperation
:
2973 except AttributeError:
2975 except OSError as e
:
2977 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
2978 if(re
.findall("^[.|/]", member
.filename
)):
2979 fname
= member
.filename
;
2981 fname
= "./"+member
.filename
;
2982 zipinfo
= zipfp
.getinfo(member
.filename
);
2984 VerbosePrintOut(fname
);
2985 if(not member
.is_dir()):
2986 fpremode
= int(stat
.S_IFREG
+ 438);
2987 elif(member
.is_dir()):
2988 fpremode
= int(stat
.S_IFDIR
+ 511);
2991 if(not member
.is_dir()):
2993 elif(member
.is_dir()):
2996 fcurfid
= format(int(curfid
), 'x').lower();
2997 fcurinode
= format(int(curfid
), 'x').lower();
2998 curfid
= curfid
+ 1;
2999 fdev_minor
= format(int(0), 'x').lower();
3000 fdev_major
= format(int(0), 'x').lower();
3001 frdev_minor
= format(int(0), 'x').lower();
3002 frdev_major
= format(int(0), 'x').lower();
3004 fsize
= format(int("0"), 'x').lower();
3006 fsize
= format(int(member
.file_size
), 'x').lower();
3008 fsize
= format(int(member
.file_size
), 'x').lower();
3009 fatime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
3010 fmtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
3011 fctime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
3012 fbtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
3013 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
3014 fwinattributes
= format(int(zipinfo
.external_attr
), 'x').lower();
3015 if(not member
.is_dir()):
3016 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
3017 fchmode
= stat
.S_IMODE(fmode
);
3018 ftypemod
= stat
.S_IFMT(fmode
);
3019 elif(member
.is_dir()):
3020 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
3021 fchmode
= stat
.S_IMODE(fmode
);
3022 ftypemod
= stat
.S_IFMT(fmode
);
3023 elif(zipinfo
.create_system
==3):
3024 fwinattributes
= format(int(0), 'x').lower();
3025 fmode
= format(int(zipinfo
.external_attr
), 'x').lower();
3026 fchmode
= stat
.S_IMODE(fmode
);
3027 ftypemod
= stat
.S_IFMT(fmode
);
3029 fwinattributes
= format(int(0), 'x').lower();
3030 if(not member
.is_dir()):
3031 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
3032 fchmode
= stat
.S_IMODE(fmode
);
3033 ftypemod
= stat
.S_IFMT(fmode
);
3034 elif(member
.is_dir()):
3035 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
3036 fchmode
= stat
.S_IMODE(fmode
);
3037 ftypemod
= stat
.S_IFMT(fmode
);
3039 fuid
= format(int(os
.getuid()), 'x').lower();
3040 except AttributeError:
3041 fuid
= format(int(0), 'x').lower();
3043 fuid
= format(int(0), 'x').lower();
3045 fgid
= format(int(os
.getgid()), 'x').lower();
3046 except AttributeError:
3047 fgid
= format(int(0), 'x').lower();
3049 fgid
= format(int(0), 'x').lower();
3053 userinfo
= pwd
.getpwuid(os
.getuid());
3054 funame
= userinfo
.pw_name
;
3057 except AttributeError:
3065 groupinfo
= grp
.getgrgid(os
.getgid());
3066 fgname
= groupinfo
.gr_name
;
3069 except AttributeError:
3073 fcontents
= BytesIO();
3075 fcontents
.write(zipfp
.read(member
.filename
));
3076 fcontents
.seek(0, 0);
3077 ftypehex
= format(ftype
, 'x').lower();
3078 extrafields
= format(len(extradata
), 'x').lower();
3079 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
3080 if(len(extradata
)>0):
3081 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3082 extrasizelen
= format(len(extrasizestr
), 'x').lower();
3083 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
];
3084 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
3085 catoutlenhex
= format(catoutlen
, 'x').lower();
3086 catoutlist
.insert(0, catoutlenhex
);
3087 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
3088 if(len(extradata
)>0):
3089 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3090 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
3091 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
3092 fcontents
.seek(0, 0);
3093 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
3094 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
3095 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
3096 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
3097 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
3098 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
3099 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
3100 nullstrecd
= formatspecs
[5].encode('UTF-8');
3101 fcontents
.seek(0, 0);
3102 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
3103 catfp
.write(catfileout
);
3106 os
.fsync(catfp
.fileno());
3107 except io
.UnsupportedOperation
:
3109 except AttributeError:
3111 except OSError as e
:
3114 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
3115 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3116 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
3119 os
.fsync(catfp
.fileno());
3120 except io
.UnsupportedOperation
:
3122 except AttributeError:
3124 except OSError as e
:
3128 if(hasattr(sys
.stdout
, "buffer")):
3129 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
3131 shutil
.copyfileobj(catfp
, sys
.stdout
);
3132 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3133 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
3135 upload_file_to_internet_file(catfp
, outfile
);
3143 create_alias_function("Pack", __file_format_name__
, "FromZipFile", PackArchiveFileFromZipFile
);
3145 if(not rarfile_support
):
3146 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
3149 if(rarfile_support
):
3150 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
3151 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3152 outfile
= RemoveWindowsPath(outfile
);
3153 checksumtype
= checksumtype
.lower();
3154 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
3155 checksumtype
="crc32";
3156 if(checksumtype
=="none"):
3158 if(not compression
or compression
=="catfile" or compression
==formatspecs
[2]):
3159 compression
= "auto";
3160 if(compression
not in compressionlist
and compression
is None):
3161 compression
= "auto";
3163 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
3164 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3165 if(os
.path
.exists(outfile
)):
3168 except OSError as e
:
3173 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3175 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3178 fbasename
= os
.path
.splitext(outfile
)[0];
3179 fextname
= os
.path
.splitext(outfile
)[1];
3180 catfp
= CompressOpenFile(outfile
, True, compressionlevel
);
3181 catver
= formatspecs
[6];
3182 fileheaderver
= str(int(catver
.replace(".", "")));
3183 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
3184 catfp
.write(fileheader
.encode('UTF-8'));
3190 inodetocatinode
= {};
3191 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
3193 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
3195 rarfp
= rarfile
.RarFile(infile
, "r");
3196 rartest
= rarfp
.testrar();
3198 VerbosePrintOut("Bad file found!");
3199 numfiles
= int(len(rarfp
.infolist()));
3200 fnumfiles
= format(int(len(rarfp
.infolist())), 'x').lower();
3201 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
3202 catfileheadercshex
= GetFileChecksum(fileheader
+ fnumfilesa
, checksumtype
, True, formatspecs
);
3203 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3204 catfp
.write(fnumfilesa
.encode('UTF-8'));
3207 os
.fsync(catfp
.fileno());
3208 except io
.UnsupportedOperation
:
3210 except AttributeError:
3212 except OSError as e
:
3214 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
3217 if(member
.host_os
==rarfile
.RAR_OS_UNIX
):
3220 member
.external_attr
3222 except AttributeError:
3224 elif(member
.host_os
==rarfile
.RAR_OS_WIN32
):
3227 member
.external_attr
3229 except AttributeError:
3234 if(re
.findall("^[.|/]", member
.filename
)):
3235 fname
= member
.filename
;
3237 fname
= "./"+member
.filename
;
3238 rarinfo
= rarfp
.getinfo(member
.filename
);
3240 VerbosePrintOut(fname
);
3241 if(is_unix
and member
.external_attr
!=0):
3242 fpremode
= int(member
.external_attr
);
3243 elif(member
.is_file()):
3244 fpremode
= int(stat
.S_IFREG
+ 438);
3245 elif(member
.is_symlink()):
3246 fpremode
= int(stat
.S_IFLNK
+ 438);
3247 elif(member
.is_dir()):
3248 fpremode
= int(stat
.S_IFDIR
+ 511);
3249 if(is_windows
and member
.external_attr
!=0):
3250 fwinattributes
= format(int(member
.external_attr
), 'x').lower();
3252 fwinattributes
= format(int(0), 'x').lower();
3255 if(member
.is_file()):
3257 elif(member
.is_symlink()):
3259 elif(member
.is_dir()):
3263 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
3264 fcurfid
= format(int(curfid
), 'x').lower();
3265 fcurinode
= format(int(curfid
), 'x').lower();
3266 curfid
= curfid
+ 1;
3267 fdev_minor
= format(int(0), 'x').lower();
3268 fdev_major
= format(int(0), 'x').lower();
3269 frdev_minor
= format(int(0), 'x').lower();
3270 frdev_major
= format(int(0), 'x').lower();
3272 fsize
= format(int("0"), 'x').lower();
3274 fsize
= format(int(member
.file_size
), 'x').lower();
3276 fsize
= format(int(member
.file_size
), 'x').lower();
3279 fatime
= format(int(member
.atime
.timestamp()), 'x').lower();
3281 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3282 except AttributeError:
3283 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3284 fmtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3287 fctime
= format(int(member
.ctime
.timestamp()), 'x').lower();
3289 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3290 except AttributeError:
3291 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3292 fbtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3293 if(is_unix
and member
.external_attr
!=0):
3294 fmode
= format(int(member
.external_attr
), 'x').lower();
3295 fchmode
= format(int(stat
.S_IMODE(member
.external_attr
)), 'x').lower();
3296 ftypemod
= format(int(stat
.S_IFMT(member
.external_attr
)), 'x').lower();
3297 elif(member
.is_file()):
3298 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
3299 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
3300 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
3301 elif(member
.is_symlink()):
3302 fmode
= format(int(stat
.S_IFLNK
+ 438), 'x').lower();
3303 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
3304 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
3305 elif(member
.is_dir()):
3306 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
3307 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3308 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3310 fuid
= format(int(os
.getuid()), 'x').lower();
3311 except AttributeError:
3312 fuid
= format(int(0), 'x').lower();
3314 fuid
= format(int(0), 'x').lower();
3316 fgid
= format(int(os
.getgid()), 'x').lower();
3317 except AttributeError:
3318 fgid
= format(int(0), 'x').lower();
3320 fgid
= format(int(0), 'x').lower();
3324 userinfo
= pwd
.getpwuid(os
.getuid());
3325 funame
= userinfo
.pw_name
;
3328 except AttributeError:
3336 groupinfo
= grp
.getgrgid(os
.getgid());
3337 fgname
= groupinfo
.gr_name
;
3340 except AttributeError:
3344 fcontents
= BytesIO();
3346 fcontents
.write(rarfp
.read(member
.filename
));
3347 fcontents
.seek(0, 0);
3348 ftypehex
= format(ftype
, 'x').lower();
3349 extrafields
= format(len(extradata
), 'x').lower();
3350 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
3351 if(len(extradata
)>0):
3352 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3353 extrasizelen
= format(len(extrasizestr
), 'x').lower();
3354 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
];
3355 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
3356 catoutlenhex
= format(catoutlen
, 'x').lower();
3357 catoutlist
.insert(0, catoutlenhex
);
3358 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
3359 if(len(extradata
)>0):
3360 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3361 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
3362 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
3363 fcontents
.seek(0, 0);
3364 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
3365 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
3366 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
3367 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
3368 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
3369 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5])
3370 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
3371 nullstrecd
= formatspecs
[5].encode('UTF-8');
3372 fcontents
.seek(0, 0);
3373 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
3374 catfp
.write(catfileout
);
3377 os
.fsync(catfp
.fileno());
3378 except io
.UnsupportedOperation
:
3380 except AttributeError:
3382 except OSError as e
:
3385 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
3386 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3387 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
)
3390 os
.fsync(catfp
.fileno());
3391 except io
.UnsupportedOperation
:
3393 except AttributeError:
3395 except OSError as e
:
3399 if(hasattr(sys
.stdout
, "buffer")):
3400 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
3402 shutil
.copyfileobj(catfp
, sys
.stdout
);
3403 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3404 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
3406 upload_file_to_internet_file(catfp
, outfile
);
3414 create_alias_function("Pack", __file_format_name__
, "FromRarFile", PackArchiveFileFromRarFile
);
3416 if(not py7zr_support
):
3417 def PackArchiveFileFromSevenZipFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
3421 def PackArchiveFileFromSevenZipFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
3422 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3423 outfile
= RemoveWindowsPath(outfile
);
3424 checksumtype
= checksumtype
.lower();
3425 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
3426 checksumtype
="crc32";
3427 if(checksumtype
=="none"):
3429 if(not compression
or compression
=="catfile" or compression
==formatspecs
[2]):
3430 compression
= "auto";
3431 if(compression
not in compressionlist
and compression
is None):
3432 compression
= "auto";
3434 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
3435 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3436 if(os
.path
.exists(outfile
)):
3439 except OSError as e
:
3444 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3446 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3449 fbasename
= os
.path
.splitext(outfile
)[0];
3450 fextname
= os
.path
.splitext(outfile
)[1];
3451 catfp
= CompressOpenFile(outfile
, True, compressionlevel
);
3452 catver
= formatspecs
[6];
3453 fileheaderver
= str(int(catver
.replace(".", "")));
3454 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
3455 catfp
.write(fileheader
.encode('UTF-8'));
3461 inodetocatinode
= {};
3462 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
3464 szpfp
= py7zr
.SevenZipFile(infile
, mode
="r");
3465 file_content
= szpfp
.readall();
3466 #sztest = szpfp.testzip();
3467 sztestalt
= szpfp
.test();
3469 VerbosePrintOut("Bad file found!");
3470 numfiles
= int(len(szpfp
.list()));
3471 fnumfiles
= format(int(len(szpfp
.list())), 'x').lower();
3472 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
3473 catfileheadercshex
= GetFileChecksum(fileheader
+ fnumfilesa
, checksumtype
, True, formatspecs
);
3474 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3475 catfp
.write(fnumfilesa
.encode('UTF-8'));
3478 os
.fsync(catfp
.fileno());
3479 except io
.UnsupportedOperation
:
3481 except AttributeError:
3483 except OSError as e
:
3485 for member
in sorted(szpfp
.list(), key
=lambda x
: x
.filename
):
3486 if(re
.findall("^[.|/]", member
.filename
)):
3487 fname
= member
.filename
;
3489 fname
= "./"+member
.filename
;
3491 VerbosePrintOut(fname
);
3492 if(not member
.is_directory
):
3493 fpremode
= int(stat
.S_IFREG
+ 438);
3494 elif(member
.is_directory
):
3495 fpremode
= int(stat
.S_IFDIR
+ 511);
3496 fwinattributes
= format(int(0), 'x').lower();
3499 if(member
.is_directory
):
3504 fcurfid
= format(int(curfid
), 'x').lower();
3505 fcurinode
= format(int(curfid
), 'x').lower();
3506 curfid
= curfid
+ 1;
3507 fdev_minor
= format(int(0), 'x').lower();
3508 fdev_major
= format(int(0), 'x').lower();
3509 frdev_minor
= format(int(0), 'x').lower();
3510 frdev_major
= format(int(0), 'x').lower();
3512 fsize
= format(int("0"), 'x').lower();
3513 fatime
= format(int(member
.creationtime
.timestamp()), 'x').lower();
3514 fmtime
= format(int(member
.creationtime
.timestamp()), 'x').lower();
3515 fctime
= format(int(member
.creationtime
.timestamp()), 'x').lower();
3516 fbtime
= format(int(member
.creationtime
.timestamp()), 'x').lower();
3517 if(member
.is_directory
):
3518 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
3519 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3520 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3522 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
3523 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
3524 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
3526 fuid
= format(int(os
.getuid()), 'x').lower();
3527 except AttributeError:
3528 fuid
= format(int(0), 'x').lower();
3530 fuid
= format(int(0), 'x').lower();
3532 fgid
= format(int(os
.getgid()), 'x').lower();
3533 except AttributeError:
3534 fgid
= format(int(0), 'x').lower();
3536 fgid
= format(int(0), 'x').lower();
3540 userinfo
= pwd
.getpwuid(os
.getuid());
3541 funame
= userinfo
.pw_name
;
3544 except AttributeError:
3552 groupinfo
= grp
.getgrgid(os
.getgid());
3553 fgname
= groupinfo
.gr_name
;
3556 except AttributeError:
3560 fcontents
= BytesIO();
3562 fcontents
.write(file_content
[member
.filename
].read());
3563 fsize
= format(fcontents
.tell(), 'x').lower();
3564 file_content
[member
.filename
].close();
3565 fcontents
.seek(0, 0);
3566 ftypehex
= format(ftype
, 'x').lower();
3567 extrafields
= format(len(extradata
), 'x').lower();
3568 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
3569 if(len(extradata
)>0):
3570 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3571 extrasizelen
= format(len(extrasizestr
), 'x').lower();
3572 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
];
3573 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
3574 catoutlenhex
= format(catoutlen
, 'x').lower();
3575 catoutlist
.insert(0, catoutlenhex
);
3576 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
3577 if(len(extradata
)>0):
3578 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3579 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
3580 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
3581 fcontents
.seek(0, 0);
3582 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
3583 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
3584 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
3585 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
3586 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
3587 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5])
3588 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
3589 nullstrecd
= formatspecs
[5].encode('UTF-8');
3590 fcontents
.seek(0, 0);
3591 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
3592 catfp
.write(catfileout
);
3595 os
.fsync(catfp
.fileno());
3596 except io
.UnsupportedOperation
:
3598 except AttributeError:
3600 except OSError as e
:
3603 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
3604 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3605 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
)
3608 os
.fsync(catfp
.fileno());
3609 except io
.UnsupportedOperation
:
3611 except AttributeError:
3613 except OSError as e
:
3617 if(hasattr(sys
.stdout
, "buffer")):
3618 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
3620 shutil
.copyfileobj(catfp
, sys
.stdout
);
3621 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3622 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
3624 upload_file_to_internet_file(catfp
, outfile
);
3632 create_alias_function("Pack", __file_format_name__
, "FromSevenZipFile", PackArchiveFileFromSevenZipFile
);
3634 def PackArchiveFileFromInFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
3635 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
3637 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
3638 if(checkcompressfile
=="tarfile"):
3639 return PackArchiveFileFromTarFile(infile
, outfile
, compression
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
3640 elif(checkcompressfile
=="zipfile"):
3641 return PackArchiveFileFromZipFile(infile
, outfile
, compression
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
3642 elif(checkcompressfile
=="catfile"):
3643 return RePackArchiveFile(infile
, outfile
, compression
, compressionlevel
, False, 0, 0, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
3644 elif(rarfile_support
and checkcompressfile
=="rarfile"):
3645 return PackArchiveFileFromRarFile(infile
, outfile
, compression
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
3646 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
3647 return PackArchiveFileFromSevenZipFile(infile
, outfile
, compression
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
3652 create_alias_function("Pack", __file_format_name__
, "FromInFile", PackArchiveFileFromInFile
);
3654 def ArchiveFileSeekToFileNum(infile
, seekto
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3655 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3658 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3659 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
3660 if(checkcompressfile
=="tarfile"):
3661 return TarFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3662 if(checkcompressfile
=="zipfile"):
3663 return ZipFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3664 if(rarfile_support
and checkcompressfile
=="rarfile"):
3665 return RarFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3666 if(py7zr_support
and checkcompressfile
=="7zipfile"):
3667 return SevenZipFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3668 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3675 if(hasattr(sys
.stdin
, "buffer")):
3676 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
3678 shutil
.copyfileobj(sys
.stdin
, catfp
);
3680 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3684 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
3685 catfp
= download_file_from_internet_file(infile
);
3687 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3692 infile
= RemoveWindowsPath(infile
);
3693 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
3694 if(checkcompressfile
=="tarfile"):
3695 return TarFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3696 if(checkcompressfile
=="zipfile"):
3697 return ZipFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3698 if(rarfile_support
and checkcompressfile
=="rarfile"):
3699 return RarFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3700 if(py7zr_support
and checkcompressfile
=="7zipfile"):
3701 return SevenZipFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3702 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3704 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
3705 if(not compresscheck
):
3706 fextname
= os
.path
.splitext(infile
)[1];
3707 if(fextname
==".gz"):
3708 compresscheck
= "gzip";
3709 elif(fextname
==".bz2"):
3710 compresscheck
= "bzip2";
3711 elif(fextname
==".zst"):
3712 compresscheck
= "zstd";
3713 elif(fextname
==".lz4" or fextname
==".clz4"):
3714 compresscheck
= "lz4";
3715 elif(fextname
==".lzo" or fextname
==".lzop"):
3716 compresscheck
= "lzo";
3717 elif(fextname
==".lzma" or fextname
==".xz"):
3718 compresscheck
= "lzma";
3721 if(not compresscheck
):
3723 catfp
= UncompressFile(infile
, formatspecs
, "rb");
3728 SeekToEndOfFile(catfp);
3730 SeekToEndOfFile(catfp);
3731 CatSize = catfp.tell();
3732 CatSizeEnd = CatSize;
3740 curloc
= catfp
.tell();
3743 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
3745 catfp
.seek(curloc
, 0);
3746 catstring
= catheader
[0];
3747 catversion
= re
.findall(r
"([\d]+)$", catstring
);
3748 fprenumfiles
= catheader
[1];
3749 fnumfiles
= int(fprenumfiles
, 16);
3750 fprechecksumtype
= catheader
[2];
3751 fprechecksum
= catheader
[3];
3752 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
3753 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3754 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
3755 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
3756 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3757 fheadtell
= len(fileheader
);
3758 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
3759 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
3760 VerbosePrintOut("'" + str(fprechecksum
) + "' != " + "'" + str(catfileheadercshex
) + "'");
3762 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
3763 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
3764 if(seekto
>=fnumfiles
):
3765 seekto
= fnumfiles
- 1;
3771 prefhstart
= catfp
.tell();
3773 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3775 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
3776 if(len(preheaderdata
)==0):
3778 prefheadsize
= int(preheaderdata
[0], 16);
3779 prefnumfields
= int(preheaderdata
[1], 16);
3780 preftype
= int(preheaderdata
[2], 16);
3781 if(re
.findall("^[.|/]", preheaderdata
[3])):
3782 prefname
= preheaderdata
[3];
3784 prefname
= "./"+preheaderdata
[3];
3785 prefbasedir
= os
.path
.dirname(prefname
);
3786 preflinkname
= preheaderdata
[4];
3787 prefsize
= int(preheaderdata
[5], 16);
3788 prefatime
= int(preheaderdata
[6], 16);
3789 prefmtime
= int(preheaderdata
[7], 16);
3790 prefctime
= int(preheaderdata
[8], 16);
3791 prefbtime
= int(preheaderdata
[9], 16);
3792 prefmode
= int(preheaderdata
[10], 16);
3793 prefchmode
= stat
.S_IMODE(prefmode
);
3794 preftypemod
= stat
.S_IFMT(prefmode
);
3795 prefwinattributes
= int(preheaderdata
[11], 16);
3796 prefuid
= int(preheaderdata
[12], 16);
3797 prefuname
= preheaderdata
[13];
3798 prefgid
= int(preheaderdata
[14], 16);
3799 prefgname
= preheaderdata
[15];
3800 fid
= int(preheaderdata
[16], 16);
3801 finode
= int(preheaderdata
[17], 16);
3802 flinkcount
= int(preheaderdata
[18], 16);
3803 prefdev_minor
= int(preheaderdata
[19], 16);
3804 prefdev_major
= int(preheaderdata
[20], 16);
3805 prefrdev_minor
= int(preheaderdata
[22], 16);
3806 prefrdev_major
= int(preheaderdata
[23], 16);
3807 prefextrasize
= int(preheaderdata
[24], 16);
3808 prefextrafields
= int(preheaderdata
[25], 16);
3809 extrafieldslist
= [];
3811 extraend
= extrastart
+ prefextrafields
;
3812 extrafieldslist
= [];
3813 if(extrastart
<extraend
):
3814 extrafieldslist
.append(preheaderdata
[extrastart
]);
3815 extrastart
= extrastart
+ 1;
3816 prefchecksumtype
= preheaderdata
[extrastart
].lower();
3817 prefcs
= preheaderdata
[extrastart
+ 1].lower();
3818 prefccs
= preheaderdata
[extrastart
+ 2].lower();
3820 hcmax
= len(preheaderdata
) - 2;
3823 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
3825 prenewfcs
= GetFileChecksum(hout
, preheaderdata
[-3].lower(), True, formatspecs
);
3826 if(prefcs
!=prenewfcs
and not skipchecksum
):
3827 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
3828 VerbosePrintOut("'" + str(prefcs
) + "' != " + "'" + str(prenewfcs
) + "'");
3830 valid_archive
= False;
3831 invalid_archive
= True;
3832 prefhend
= catfp
.tell() - 1;
3833 prefcontentstart
= catfp
.tell();
3835 pyhascontents
= False;
3837 prefcontents
= catfp
.read(prefsize
);
3838 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
3839 pyhascontents
= True;
3840 if(prefccs
!=prenewfccs
and not skipchecksum
):
3841 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
3842 VerbosePrintOut("'" + str(prefccs
) + "' != " + "'" + str(prenewfccs
) + "'");
3846 catfp
.seek(seekstart
, 0);
3848 catfheadsize
= int(preheaderdata
[0], 16);
3849 catfnumfields
= int(preheaderdata
[1], 16);
3850 catftype
= int(preheaderdata
[2], 16);
3851 if(re
.findall("^[.|/]", preheaderdata
[3])):
3852 catfname
= preheaderdata
[3];
3854 catfname
= "./"+preheaderdata
[3];
3855 catflinkname
= preheaderdata
[4];
3856 catfsize
= int(preheaderdata
[5], 16);
3857 catfbasedir
= os
.path
.dirname(catfname
);
3858 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
3860 catlist
.update({'catfp': catfp
});
3865 create_alias_function("", __file_format_name__
, "SeekToFileNum", ArchiveFileSeekToFileNum
);
3867 def ArchiveFileSeekToFileName(infile
, seekfile
=None, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3868 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3871 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3872 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
3873 if(checkcompressfile
=="tarfile"):
3874 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3875 if(checkcompressfile
=="zipfile"):
3876 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3877 if(rarfile_support
and checkcompressfile
=="rarfile"):
3878 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3879 if(py7zr_support
and checkcompressfile
=="7zipfile"):
3880 return SevenZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3881 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3888 if(hasattr(sys
.stdin
, "buffer")):
3889 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
3891 shutil
.copyfileobj(sys
.stdin
, catfp
);
3893 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3897 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
3898 catfp
= download_file_from_internet_file(infile
);
3899 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3905 infile
= RemoveWindowsPath(infile
);
3906 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
3907 if(checkcompressfile
=="tarfile"):
3908 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3909 if(checkcompressfile
=="zipfile"):
3910 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3911 if(rarfile_support
and checkcompressfile
=="rarfile"):
3912 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3913 if(py7zr_support
and checkcompressfile
=="7zipfile"):
3914 return SevenZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3915 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3917 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
3918 if(not compresscheck
):
3919 fextname
= os
.path
.splitext(infile
)[1];
3920 if(fextname
==".gz"):
3921 compresscheck
= "gzip";
3922 elif(fextname
==".bz2"):
3923 compresscheck
= "bzip2";
3924 elif(fextname
==".zst"):
3925 compresscheck
= "zstd";
3926 elif(fextname
==".lz4" or fextname
==".clz4"):
3927 compresscheck
= "lz4";
3928 elif(fextname
==".lzo" or fextname
==".lzop"):
3929 compresscheck
= "lzo";
3930 elif(fextname
==".lzma" or fextname
==".xz"):
3931 compresscheck
= "lzma";
3934 if(not compresscheck
):
3936 catfp
= UncompressFile(infile
, formatspecs
, "rb");
3941 SeekToEndOfFile(catfp);
3943 SeekToEndOfFile(catfp);
3944 CatSize = catfp.tell();
3945 CatSizeEnd = CatSize;
3953 curloc
= catfp
.tell();
3956 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
3958 catfp
.seek(curloc
, 0);
3959 catstring
= catheader
[0];
3960 catversion
= re
.findall(r
"([\d]+)$", catstring
);
3961 fprenumfiles
= catheader
[1];
3962 fnumfiles
= int(fprenumfiles
, 16);
3963 fprechecksumtype
= catheader
[2];
3964 fprechecksum
= catheader
[3];
3965 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
3966 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3967 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
3968 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
3969 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3970 fheadtell
= len(fileheader
);
3971 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
3972 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
3973 VerbosePrintOut("'" + str(fprechecksum
) + "' != " + "'" + str(catfileheadercshex
) + "'");
3975 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
3976 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
3977 seekto
= fnumfiles
- 1
3982 prefhstart
= catfp
.tell();
3984 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3986 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
3987 if(len(preheaderdata
)==0):
3989 prefheadsize
= int(preheaderdata
[0], 16);
3990 prefnumfields
= int(preheaderdata
[1], 16);
3991 preftype
= int(preheaderdata
[2], 16);
3992 if(re
.findall("^[.|/]", preheaderdata
[3])):
3993 prefname
= preheaderdata
[3];
3995 prefname
= "./"+preheaderdata
[3];
3996 prefbasedir
= os
.path
.dirname(prefname
);
3997 preflinkname
= preheaderdata
[4];
3998 prefsize
= int(preheaderdata
[5], 16);
3999 prefatime
= int(preheaderdata
[6], 16);
4000 prefmtime
= int(preheaderdata
[7], 16);
4001 prefctime
= int(preheaderdata
[8], 16);
4002 prefbtime
= int(preheaderdata
[9], 16);
4003 prefmode
= int(preheaderdata
[10], 16);
4004 prefchmode
= stat
.S_IMODE(prefmode
);
4005 preftypemod
= stat
.S_IFMT(prefmode
);
4006 prefwinattributes
= int(preheaderdata
[11], 16);
4007 prefuid
= int(preheaderdata
[12], 16);
4008 prefuname
= preheaderdata
[13];
4009 prefgid
= int(preheaderdata
[14], 16);
4010 prefgname
= preheaderdata
[15];
4011 fid
= int(preheaderdata
[16], 16);
4012 finode
= int(preheaderdata
[17], 16);
4013 flinkcount
= int(preheaderdata
[18], 16);
4014 prefdev_minor
= int(preheaderdata
[19], 16);
4015 prefdev_major
= int(preheaderdata
[20], 16);
4016 prefrdev_minor
= int(preheaderdata
[22], 16);
4017 prefrdev_major
= int(preheaderdata
[23], 16);
4018 prefextrasize
= int(preheaderdata
[24], 16);
4019 prefextrafields
= int(preheaderdata
[25], 16);
4020 extrafieldslist
= [];
4022 extraend
= extrastart
+ prefextrafields
;
4023 extrafieldslist
= [];
4024 if(extrastart
<extraend
):
4025 extrafieldslist
.append(preheaderdata
[extrastart
]);
4026 extrastart
= extrastart
+ 1;
4027 prefchecksumtype
= preheaderdata
[extrastart
].lower();
4028 prefcs
= preheaderdata
[extrastart
+ 1].lower();
4029 prefccs
= preheaderdata
[extrastart
+ 2].lower();
4031 hcmax
= len(preheaderdata
) - 2;
4034 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
4036 prenewfcs
= GetFileChecksum(hout
, preheaderdata
[-3].lower(), True, formatspecs
);
4037 if(prefcs
!=prenewfcs
and not skipchecksum
):
4038 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
4039 VerbosePrintOut("'" + str(prefcs
) + "' != " + "'" + str(prenewfcs
) + "'");
4041 valid_archive
= False;
4042 invalid_archive
= True;
4043 prefhend
= catfp
.tell() - 1;
4044 prefcontentstart
= catfp
.tell();
4046 pyhascontents
= False;
4048 prefcontents
= catfp
.read(prefsize
);
4049 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
4050 pyhascontents
= True;
4051 if(prefccs
!=prenewfccs
and not skipchecksum
):
4052 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
4053 VerbosePrintOut("'" + str(prefccs
) + "' != " + "'" + str(prenewfccs
) + "'");
4058 prefname
= preheaderdata
[2];
4059 if(re
.findall("^[.|/]", preheaderdata
[2])):
4060 prefname
= preheaderdata
[2];
4062 prefname
= "./"+preheaderdata
[2];
4063 if(prefname
==seekfile
):
4066 catfp
.seek(seekstart
, 0);
4068 catfheadsize
= int(preheaderdata
[0], 16);
4069 catfnumfields
= int(preheaderdata
[1], 16);
4070 catftype
= int(preheaderdata
[2], 16);
4071 if(re
.findall("^[.|/]", preheaderdata
[3])):
4072 catfname
= preheaderdata
[3];
4074 catfname
= "./"+preheaderdata
[3];
4075 catflinkname
= preheaderdata
[4];
4076 catfsize
= int(preheaderdata
[5], 16);
4077 catfbasedir
= os
.path
.dirname(catfname
);
4079 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
4083 catlist
.update({'catfp': catfp
});
4088 create_alias_function("", __file_format_name__
, "SeekToFileName", ArchiveFileSeekToFileName
);
4090 def ArchiveFileValidate(infile
, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
4092 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
4093 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4096 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4097 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
4098 if(checkcompressfile
=="tarfile"):
4099 return TarFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4100 if(checkcompressfile
=="zipfile"):
4101 return ZipFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4102 if(rarfile_support
and checkcompressfile
=="rarfile"):
4103 return RarFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4104 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4105 return SevenZipFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4106 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4113 if(hasattr(sys
.stdin
, "buffer")):
4114 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
4116 shutil
.copyfileobj(sys
.stdin
, catfp
);
4118 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4122 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4123 catfp
= download_file_from_internet_file(infile
);
4124 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4130 infile
= RemoveWindowsPath(infile
);
4131 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
4132 if(checkcompressfile
=="tarfile"):
4133 return TarFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4134 if(checkcompressfile
=="zipfile"):
4135 return ZipFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4136 if(rarfile_support
and checkcompressfile
=="rarfile"):
4137 return RarFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4138 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4139 return SevenZipFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4140 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4142 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
4143 if(not compresscheck
):
4144 fextname
= os
.path
.splitext(infile
)[1];
4145 if(fextname
==".gz"):
4146 compresscheck
= "gzip";
4147 elif(fextname
==".bz2"):
4148 compresscheck
= "bzip2";
4149 elif(fextname
==".zst"):
4150 compresscheck
= "zstd";
4151 elif(fextname
==".lz4" or fextname
==".clz4"):
4152 compresscheck
= "lz4";
4153 elif(fextname
==".lzo" or fextname
==".lzop"):
4154 compresscheck
= "lzo";
4155 elif(fextname
==".lzma" or fextname
==".xz"):
4156 compresscheck
= "lzma";
4159 if(not compresscheck
):
4161 catfp
= UncompressFile(infile
, formatspecs
, "rb");
4166 SeekToEndOfFile(catfp);
4168 SeekToEndOfFile(catfp);
4169 CatSize = catfp.tell();
4170 CatSizeEnd = CatSize;
4178 curloc
= catfp
.tell();
4181 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
4183 catfp
.seek(curloc
, 0);
4184 catstring
= catheader
[0];
4185 catversion
= re
.findall(r
"([\d]+)$", catstring
);
4186 fprenumfiles
= catheader
[1];
4187 fnumfiles
= int(fprenumfiles
, 16);
4188 fprechecksumtype
= catheader
[2];
4189 fprechecksum
= catheader
[3];
4191 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
4192 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4193 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
4194 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
4195 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4196 valid_archive
= True;
4197 invalid_archive
= False;
4199 VerbosePrintOut(infile
);
4200 VerbosePrintOut("Number of Records " + str(fnumfiles
));
4201 if(fprechecksum
==catfileheadercshex
):
4203 VerbosePrintOut("File Header Checksum Passed at offset " + str(0));
4204 VerbosePrintOut("'" + str(fprechecksum
) + "' == " + "'" + str(catfileheadercshex
) + "'");
4207 VerbosePrintOut("File Header Checksum Failed at offset " + str(0));
4208 VerbosePrintOut("'" + str(fprechecksum
) + "' != " + "'" + str(catfileheadercshex
) + "'");
4209 valid_archive
= False;
4210 invalid_archive
= True;
4212 VerbosePrintOut("");
4213 while(il
<fnumfiles
):
4214 catfhstart
= catfp
.tell();
4216 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
4218 catheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
4219 if(len(catheaderdata
)==0):
4221 catfheadsize
= int(catheaderdata
[0], 16);
4222 catfnumfields
= int(catheaderdata
[1], 16);
4223 catftype
= int(catheaderdata
[2], 16);
4224 if(re
.findall("^[.|/]", catheaderdata
[3])):
4225 catfname
= catheaderdata
[3];
4227 catfname
= "./"+catheaderdata
[3];
4228 catfbasedir
= os
.path
.dirname(catfname
);
4229 catflinkname
= catheaderdata
[4];
4230 catfsize
= int(catheaderdata
[5], 16);
4231 catfatime
= int(catheaderdata
[6], 16);
4232 catfmtime
= int(catheaderdata
[7], 16);
4233 catfctime
= int(catheaderdata
[8], 16);
4234 catfbtime
= int(catheaderdata
[9], 16);
4235 catfmode
= int(catheaderdata
[10], 16);
4236 catfchmode
= stat
.S_IMODE(catfmode
);
4237 catftypemod
= stat
.S_IFMT(catfmode
);
4238 prefwinattributes
= int(catheaderdata
[11], 16);
4239 catfuid
= int(catheaderdata
[12], 16);
4240 catfuname
= catheaderdata
[13];
4241 catfgid
= int(catheaderdata
[14], 16);
4242 catfgname
= catheaderdata
[15];
4243 fid
= int(catheaderdata
[16], 16);
4244 finode
= int(catheaderdata
[17], 16);
4245 flinkcount
= int(catheaderdata
[18], 16);
4246 catfdev_minor
= int(catheaderdata
[19], 16);
4247 catfdev_major
= int(catheaderdata
[20], 16);
4248 catfrdev_minor
= int(catheaderdata
[21], 16);
4249 catfrdev_major
= int(catheaderdata
[22], 16);
4250 catfextrasize
= int(catheaderdata
[23], 16);
4251 catfextrafields
= int(catheaderdata
[24], 16);
4252 extrafieldslist
= [];
4254 extraend
= extrastart
+ catfextrafields
;
4255 extrafieldslist
= [];
4256 if(extrastart
<extraend
):
4257 extrafieldslist
.append(catheaderdata
[extrastart
]);
4258 extrastart
= extrastart
+ 1;
4259 catfchecksumtype
= catheaderdata
[extrastart
].lower();
4260 catfcs
= catheaderdata
[extrastart
+ 1].lower();
4261 catfccs
= catheaderdata
[extrastart
+ 2].lower();
4263 hcmax
= len(catheaderdata
) - 2;
4266 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[5]);
4268 catnewfcs
= GetFileChecksum(hout
, catheaderdata
[-3].lower(), True, formatspecs
);
4270 VerbosePrintOut(catfname
);
4271 VerbosePrintOut("Record Number " + str(il
) + "; File ID " + str(fid
) + "; iNode Number " + str(finode
));
4272 if(catfcs
==catnewfcs
):
4274 VerbosePrintOut("File Header Checksum Passed at offset " + str(catfhstart
));
4275 VerbosePrintOut("'" + str(catfcs
) + "' == " + "'" + str(catnewfcs
) + "'");
4278 VerbosePrintOut("File Header Checksum Failed at offset " + str(catfhstart
));
4279 VerbosePrintOut("'" + str(catfcs
) + "' != " + "'" + str(catnewfcs
) + "'");
4280 valid_archive
= False;
4281 invalid_archive
= True;
4282 catfhend
= catfp
.tell() - 1;
4283 catfcontentstart
= catfp
.tell();
4285 pyhascontents
= False;
4287 catfcontents
= catfp
.read(catfsize
);
4288 catnewfccs
= GetFileChecksum(catfcontents
, catheaderdata
[-3].lower(), False, formatspecs
);
4289 pyhascontents
= True;
4290 if(catfccs
==catnewfccs
):
4292 VerbosePrintOut("File Content Checksum Passed at offset " + str(catfcontentstart
));
4293 VerbosePrintOut("'" + str(catfccs
) + "' == " + "'" + str(catnewfccs
) + "'");
4296 VerbosePrintOut("File Content Checksum Failed at offset " + str(catfcontentstart
));
4297 VerbosePrintOut("'" + str(catfccs
) + "' != " + "'" + str(catnewfccs
) + "'");
4298 valid_archive
= False;
4299 invalid_archive
= True;
4301 VerbosePrintOut("");
4314 create_alias_function("", __file_format_name__
, "Validate", ArchiveFileValidate
);
4316 def ArchiveFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4317 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4320 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4321 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
4322 if(checkcompressfile
=="tarfile"):
4323 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4324 if(checkcompressfile
=="zipfile"):
4325 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4326 if(rarfile_support
and checkcompressfile
=="rarfile"):
4327 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4328 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4329 return SevenZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4330 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4337 if(hasattr(sys
.stdin
, "buffer")):
4338 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
4340 shutil
.copyfileobj(sys
.stdin
, catfp
);
4342 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4346 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4347 catfp
= download_file_from_internet_file(infile
);
4348 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4354 infile
= RemoveWindowsPath(infile
);
4355 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
4356 if(checkcompressfile
=="tarfile"):
4357 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4358 if(checkcompressfile
=="zipfile"):
4359 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4360 if(rarfile_support
and checkcompressfile
=="rarfile"):
4361 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4362 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4363 return SevenZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4364 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4366 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
4367 if(not compresscheck
):
4368 fextname
= os
.path
.splitext(infile
)[1];
4369 if(fextname
==".gz"):
4370 compresscheck
= "gzip";
4371 elif(fextname
==".bz2"):
4372 compresscheck
= "bzip2";
4373 elif(fextname
==".zst"):
4374 compresscheck
= "zstd";
4375 elif(fextname
==".lz4" or fextname
==".clz4"):
4376 compresscheck
= "lz4";
4377 elif(fextname
==".lzo" or fextname
==".lzop"):
4378 compresscheck
= "lzo";
4379 elif(fextname
==".lzma" or fextname
==".xz"):
4380 compresscheck
= "lzma";
4383 if(not compresscheck
):
4385 catfp
= UncompressFile(infile
, formatspecs
, "rb");
4390 SeekToEndOfFile(catfp);
4392 SeekToEndOfFile(catfp);
4393 CatSize = catfp.tell();
4394 CatSizeEnd = CatSize;
4402 curloc
= catfp
.tell();
4405 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
4407 catfp
.seek(curloc
, 0);
4408 catstring
= catheader
[0];
4409 catversion
= re
.findall(r
"([\d]+)$", catstring
);
4410 fprenumfiles
= catheader
[1];
4411 fnumfiles
= int(fprenumfiles
, 16);
4412 fprechecksumtype
= catheader
[2];
4413 fprechecksum
= catheader
[3];
4414 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
4415 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4416 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
4417 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
4418 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4419 fheadtell
= len(fileheader
);
4420 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
4421 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
4422 VerbosePrintOut("'" + str(fprechecksum
) + "' != " + "'" + str(catfileheadercshex
) + "'");
4424 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4425 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
4426 if(seekstart
<0 and seekstart
>fnumfiles
):
4428 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
4429 seekend
= fnumfiles
;
4430 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
4431 seekend
= fnumfiles
- abs(seekend
);
4434 while(il
< seekstart
):
4435 prefhstart
= catfp
.tell();
4437 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
4439 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
4440 if(len(preheaderdata
)==0):
4442 prefheadsize
= int(preheaderdata
[0], 16);
4443 prefnumfields
= int(preheaderdata
[1], 16);
4444 if(re
.findall("^[.|/]", preheaderdata
[3])):
4445 prefname
= preheaderdata
[3];
4447 prefname
= "./"+preheaderdata
[3];
4448 prefsize
= int(preheaderdata
[5], 16);
4449 prefextrasize
= int(preheaderdata
[23], 16);
4450 prefextrafields
= int(preheaderdata
[24], 16);
4451 extrafieldslist
= [];
4453 extraend
= extrastart
+ prefextrafields
;
4454 extrafieldslist
= [];
4455 if(extrastart
<extraend
):
4456 extrafieldslist
.append(preheaderdata
[extrastart
]);
4457 extrastart
= extrastart
+ 1;
4458 prefchecksumtype
= preheaderdata
[extrastart
].lower();
4459 prefcs
= preheaderdata
[extrastart
+ 1].lower();
4460 prefccs
= preheaderdata
[extrastart
+ 2].lower();
4462 hcmax
= len(preheaderdata
) - 2;
4465 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
4467 prenewfcs
= GetFileChecksum(hout
, preheaderdata
[-3].lower(), True, formatspecs
);
4468 if(prefcs
!=prenewfcs
and not skipchecksum
):
4469 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
4470 VerbosePrintOut("'" + str(prefcs
) + "' != " + "'" + str(prenewfcs
) + "'");
4472 valid_archive
= False;
4473 invalid_archive
= True;
4474 prefhend
= catfp
.tell() - 1;
4475 prefcontentstart
= catfp
.tell();
4477 pyhascontents
= False;
4479 prefcontents
= catfp
.read(prefsize
);
4480 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
4481 pyhascontents
= True;
4482 if(prefccs
!=prenewfccs
and not skipchecksum
):
4483 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
4484 VerbosePrintOut("'" + str(prefccs
) + "' != " + "'" + str(prenewfccs
) + "'");
4488 fileidnum
= seekstart
;
4490 while(fileidnum
<seekend
):
4491 catfhstart
= catfp
.tell();
4493 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
4495 catheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
4496 if(len(catheaderdata
)==0):
4498 catfheadsize
= int(catheaderdata
[0], 16);
4499 catfnumfields
= int(catheaderdata
[1], 16);
4500 catftype
= int(catheaderdata
[2], 16);
4501 if(re
.findall("^[.|/]", catheaderdata
[3])):
4502 catfname
= catheaderdata
[3];
4504 catfname
= "./"+catheaderdata
[3];
4505 catfbasedir
= os
.path
.dirname(catfname
);
4506 catflinkname
= catheaderdata
[4];
4507 catfsize
= int(catheaderdata
[5], 16);
4508 catfatime
= int(catheaderdata
[6], 16);
4509 catfmtime
= int(catheaderdata
[7], 16);
4510 catfctime
= int(catheaderdata
[8], 16);
4511 catfbtime
= int(catheaderdata
[9], 16);
4512 catfmode
= int(catheaderdata
[10], 16);
4513 catfchmode
= stat
.S_IMODE(catfmode
);
4514 catftypemod
= stat
.S_IFMT(catfmode
);
4515 catfwinattributes
= int(catheaderdata
[11], 16);
4516 catfuid
= int(catheaderdata
[12], 16);
4517 catfuname
= catheaderdata
[13];
4518 catfgid
= int(catheaderdata
[14], 16);
4519 catfgname
= catheaderdata
[15];
4520 fid
= int(catheaderdata
[16], 16);
4521 finode
= int(catheaderdata
[17], 16);
4522 flinkcount
= int(catheaderdata
[18], 16);
4523 catfdev_minor
= int(catheaderdata
[19], 16);
4524 catfdev_major
= int(catheaderdata
[20], 16);
4525 catfrdev_minor
= int(catheaderdata
[21], 16);
4526 catfrdev_major
= int(catheaderdata
[22], 16);
4527 catfextrasize
= int(catheaderdata
[23], 16);
4528 catfextrafields
= int(catheaderdata
[24], 16);
4529 extrafieldslist
= [];
4531 extraend
= extrastart
+ catfextrafields
;
4532 extrafieldslist
= [];
4533 if(extrastart
<extraend
):
4534 extrafieldslist
.append(catheaderdata
[extrastart
]);
4535 extrastart
= extrastart
+ 1;
4536 catfchecksumtype
= catheaderdata
[extrastart
].lower();
4537 catfcs
= catheaderdata
[extrastart
+ 1].lower();
4538 catfccs
= catheaderdata
[extrastart
+ 2].lower();
4540 hcmax
= len(catheaderdata
) - 2;
4543 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[5]);
4545 catnewfcs
= GetFileChecksum(hout
, catheaderdata
[-3].lower(), True, formatspecs
);
4546 if(catfcs
!=catnewfcs
and not skipchecksum
):
4547 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfhstart
));
4548 VerbosePrintOut("'" + str(catfcs
) + "' != " + "'" + str(catnewfcs
) + "'");
4550 catfhend
= catfp
.tell() - 1;
4551 catfcontentstart
= catfp
.tell();
4552 catfcontents
= BytesIO();
4553 pyhascontents
= False;
4554 catfcontents
.seek(0, 0);
4555 if(catfsize
>0 and not listonly
):
4556 catfcontents
.write(catfp
.read(catfsize
));
4557 catnewfccs
= GetFileChecksum(catfcontents
.read(), catheaderdata
[-3].lower(), False, formatspecs
);
4558 pyhascontents
= True;
4559 if(catfccs
!=catnewfccs
and skipchecksum
):
4560 VerbosePrintOut("File Content Checksum Error with file " + catfname
+ " at offset " + str(catfcontentstart
));
4561 VerbosePrintOut("'" + str(catfccs
) + "' != " + "'" + str(catnewfccs
) + "'");
4563 if(catfsize
>0 and listonly
):
4564 catfp
.seek(catfsize
, 1);
4565 pyhascontents
= False;
4567 catfcontentend
= catfp
.tell() - 1;
4568 catfcontents
.seek(0, 0);
4569 catlist
['ffilelist'].update({realidnum
: {'fid': realidnum
, 'fidalt': fileidnum
, 'fheadersize': catfheadsize
, 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
, 'fatime': catfatime
, 'fmtime': catfmtime
, 'fctime': catfctime
, 'fbtime': catfbtime
, 'fmode': catfmode
, 'fchmode': catfchmode
, 'ftypemod': catftypemod
, 'fwinattributes': catfwinattributes
, 'fuid': catfuid
, 'funame': catfuname
, 'fgid': catfgid
, 'fgname': catfgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': catfdev_minor
, 'fmajor': catfdev_major
, 'frminor': catfrdev_minor
, 'frmajor': catfrdev_major
, 'fchecksumtype': catfchecksumtype
, 'fnumfields': catfnumfields
+ 2, 'frawheader': catheaderdata
, 'fextrafields': catfextrafields
, 'fextrafieldsize': catfextrasize
, 'fextralist': extrafieldslist
, 'fheaderchecksum': catfcs
, 'fcontentchecksum': catfccs
, 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': catfcontents
} });
4570 fileidnum
= fileidnum
+ 1;
4571 realidnum
= realidnum
+ 1;
4573 catlist
.update({'catfp': catfp
});
4578 create_alias_function("", __file_format_name__
, "ToArray", ArchiveFileToArray
);
4580 def ArchiveFileStringToArray(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4581 catfp
= BytesIO(catstr
);
4582 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4583 return listcatfiles
;
4585 create_alias_function("", __file_format_name__
, "StringToArray", ArchiveFileStringToArray
);
4587 def TarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4589 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4590 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4591 return listcatfiles
;
4593 def ZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4595 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4596 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4597 return listcatfiles
;
4599 if(not rarfile_support
):
4600 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4603 if(rarfile_support
):
4604 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4606 catfp
= PackArchiveFileFromSevenZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4607 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4608 return listcatfiles
;
4610 if(not py7zr_support
):
4611 def SevenZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4615 def SevenZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4617 catfp
= PackArchiveFileFromSevenZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4618 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4619 return listcatfiles
;
4621 def InFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4622 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
4623 if(checkcompressfile
=="tarfile"):
4624 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4625 elif(checkcompressfile
=="zipfile"):
4626 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4627 elif(checkcompressfile
=="catfile"):
4628 return ArchiveFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4629 elif(rarfile_support
and checkcompressfile
=="rarfile"):
4630 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4631 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
4632 return SevenZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4637 def ListDirToArrayAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4638 catver
= formatspecs
[6];
4639 fileheaderver
= str(int(catver
.replace(".", "")));
4640 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4641 advancedlist
= formatspecs
[8];
4642 altinode
= formatspecs
[9];
4645 for line
in sys
.stdin
:
4646 infilelist
.append(line
.strip());
4647 infilelist
= list(filter(None, infilelist
));
4648 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
4649 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
4651 with
UncompressFile(infiles
, formatspecs
, "r") as finfile
:
4652 for line
in finfile
:
4653 infilelist
.append(line
.strip());
4654 infilelist
= list(filter(None, infilelist
));
4656 if(isinstance(infiles
, (list, tuple, ))):
4657 infilelist
= list(filter(None, infiles
));
4658 elif(isinstance(infiles
, (str, ))):
4659 infilelist
= list(filter(None, [infiles
]));
4661 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
4663 GetDirList
= ListDir(infilelist
, followlink
, False);
4671 inodetocatinode
= {};
4673 fnumfiles
= int(len(GetDirList
));
4674 catver
= formatspecs
[6];
4675 fileheaderver
= str(int(catver
.replace(".", "")));
4676 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4677 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4678 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4679 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4680 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4681 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
4682 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4683 fheadtell
= len(fileheader
);
4684 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4685 for curfname
in GetDirList
:
4686 if(re
.findall("^[.|/]", curfname
)):
4689 fname
= "./"+curfname
;
4691 VerbosePrintOut(fname
);
4692 if(not followlink
or followlink
is None):
4693 fstatinfo
= os
.lstat(fname
);
4695 fstatinfo
= os
.stat(fname
);
4696 fpremode
= fstatinfo
.st_mode
;
4697 finode
= fstatinfo
.st_ino
;
4698 flinkcount
= fstatinfo
.st_nlink
;
4700 if(stat
.S_ISREG(fpremode
)):
4702 elif(stat
.S_ISLNK(fpremode
)):
4704 elif(stat
.S_ISCHR(fpremode
)):
4706 elif(stat
.S_ISBLK(fpremode
)):
4708 elif(stat
.S_ISDIR(fpremode
)):
4710 elif(stat
.S_ISFIFO(fpremode
)):
4712 elif(stat
.S_ISSOCK(fpremode
)):
4714 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
4716 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
4718 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
4723 fbasedir
= os
.path
.dirname(fname
);
4725 if(not followlink
and finode
!=0):
4727 if(finode
in inodelist
):
4729 flinkname
= inodetofile
[finode
];
4733 fcurinode
= inodetocatinode
[finode
];
4734 if(finode
not in inodelist
):
4735 inodelist
.append(finode
);
4736 inodetofile
.update({finode
: fname
});
4737 inodetocatinode
.update({finode
: curinode
});
4741 fcurinode
= curinode
;
4742 curinode
= curinode
+ 1;
4744 fcurinode
= curinode
;
4745 curinode
= curinode
+ 1;
4746 curfid
= curfid
+ 1;
4748 flinkname
= os
.readlink(fname
);
4749 fdev
= fstatinfo
.st_dev
;
4750 getfdev
= GetDevMajorMinor(fdev
);
4751 fdev_minor
= getfdev
[0];
4752 fdev_major
= getfdev
[1];
4753 frdev
= fstatinfo
.st_dev
;
4754 if(hasattr(fstatinfo
, "st_rdev")):
4755 frdev
= fstatinfo
.st_rdev
;
4757 frdev
= fstatinfo
.st_dev
;
4758 getfrdev
= GetDevMajorMinor(frdev
);
4759 frdev_minor
= getfrdev
[0];
4760 frdev_major
= getfrdev
[1];
4761 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
4763 if(ftype
==0 or ftype
==7):
4764 fsize
= fstatinfo
.st_size
;
4765 fatime
= fstatinfo
.st_atime
;
4766 fmtime
= fstatinfo
.st_mtime
;
4767 fctime
= fstatinfo
.st_ctime
;
4768 if(hasattr(fstatinfo
, "st_birthtime")):
4769 fbtime
= fstatinfo
.st_birthtime
;
4771 fbtime
= fstatinfo
.st_ctime
;
4772 fmode
= fstatinfo
.st_mode
;
4773 fchmode
= stat
.S_IMODE(fstatinfo
.st_mode
);
4774 ftypemod
= stat
.S_IFMT(fstatinfo
.st_mode
);
4775 fuid
= fstatinfo
.st_uid
;
4776 fgid
= fstatinfo
.st_gid
;
4781 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
4782 funame
= userinfo
.pw_name
;
4791 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
4792 fgname
= groupinfo
.gr_name
;
4797 fdev_minor
= fdev_minor
;
4798 fdev_major
= fdev_major
;
4799 frdev_minor
= frdev_minor
;
4800 frdev_major
= frdev_major
;
4802 flinkcount
= flinkcount
;
4803 if(hasattr(fstatinfo
, "st_file_attributes")):
4804 fwinattributes
= fstatinfo
.st_file_attributes
;
4807 fcontents
= BytesIO();
4808 if(ftype
==0 or ftype
==7):
4809 with
open(fname
, "rb") as fpc
:
4810 shutil
.copyfileobj(fpc
, fcontents
);
4811 if(followlink
and (ftype
==1 or ftype
==2)):
4812 flstatinfo
= os
.stat(flinkname
);
4813 with
open(flinkname
, "rb") as fpc
:
4814 shutil
.copyfileobj(fpc
, fcontents
);
4815 fcontents
.seek(0, 0);
4816 ftypehex
= format(ftype
, 'x').lower();
4817 extrafields
= len(extradata
);
4818 extrafieldslist
= extradata
;
4819 catfextrafields
= extrafields
;
4820 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
4821 if(len(extradata
)>0):
4822 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4823 extrasizelen
= len(extrasizestr
);
4824 extrasizelenhex
= format(extrasizelen
, 'x').lower();
4825 catoutlist
= [ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fwinattributes
), 'x').lower(), format(int(fuid
), 'x').lower(), funame
, format(int(fgid
), 'x').lower(), fgname
, format(int(fcurfid
), 'x').lower(), format(int(fcurinode
), 'x').lower(), format(int(flinkcount
), 'x').lower(), format(int(fdev_minor
), 'x').lower(), format(int(fdev_major
), 'x').lower(), format(int(frdev_minor
), 'x').lower(), format(int(frdev_major
), 'x').lower(), extrasizelenhex
, format(catfextrafields
, 'x').lower()];
4826 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
4827 catoutlenhex
= format(catoutlen
, 'x').lower();
4828 catoutlist
.insert(0, catoutlenhex
);
4829 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
4830 if(len(extradata
)>0):
4831 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4832 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
4833 catfnumfields
= catoutlen
;
4834 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
4835 fcontents
.seek(0, 0);
4836 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
4837 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4838 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
4839 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
4840 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
4841 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4842 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
4843 nullstrecd
= formatspecs
[5].encode('UTF-8');
4844 fheadtell
+= len(catfileoutstr
) + 1;
4845 catfcontentend
= fheadtell
- 1;
4846 fcontents
.seek(0, 0);
4847 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
4848 pyhascontents
= False;
4849 if(int(fsize
)>0 and not listonly
):
4850 pyhascontents
= True;
4851 if(int(fsize
)>0 and listonly
):
4852 fcontents
= BytesIO();
4853 pyhascontents
= False;
4854 fcontents
.seek(0, 0);
4855 catlist
['ffilelist'].update({fileidnum
: {'fid': fileidnum
, 'fidalt': fileidnum
, 'fheadersize': int(catheaersize
, 16), 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fwinattributes': fwinattributes
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': checksumtype
, 'fnumfields': catfnumfields
+ 2, 'frawheader': catheaderdata
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
4856 fileidnum
= fileidnum
+ 1;
4859 def TarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4865 inodetocatinode
= {};
4869 if(hasattr(sys
.stdin
, "buffer")):
4870 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
4872 shutil
.copyfileobj(sys
.stdin
, infile
);
4877 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4878 infile
= download_file_from_internet_file(infile
);
4883 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
4885 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
4887 if(not tarfile
.is_tarfile(infile
)):
4889 except AttributeError:
4890 if(not is_tarfile(infile
)):
4895 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4896 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
4898 tarfp
= tarfile
.open(infile
, "r");
4899 except FileNotFoundError
:
4901 fnumfiles
= int(len(tarfp
.getmembers()));
4902 catver
= formatspecs
[6];
4903 fileheaderver
= str(int(catver
.replace(".", "")));
4904 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4905 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4906 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4907 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4908 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4909 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
4910 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4911 fheadtell
= len(fileheader
);
4912 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4913 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
4914 if(re
.findall("^[.|/]", member
.name
)):
4915 fname
= member
.name
;
4917 fname
= "./"+member
.name
;
4919 VerbosePrintOut(fname
);
4920 fpremode
= member
.mode
;
4921 ffullmode
= member
.mode
;
4925 ffullmode
= member
.mode
+ stat
.S_IFREG
;
4927 elif(member
.isdev()):
4928 ffullmode
= member
.mode
;
4930 elif(member
.islnk()):
4931 ffullmode
= member
.mode
+ stat
.S_IFREG
;
4933 elif(member
.issym()):
4934 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
4936 elif(member
.ischr()):
4937 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
4939 elif(member
.isblk()):
4940 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
4942 elif(member
.isdir()):
4943 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
4945 elif(member
.isfifo()):
4946 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
4948 elif(member
.issparse()):
4949 ffullmode
= member
.mode
;
4952 ffullmode
= member
.mode
;
4955 fbasedir
= os
.path
.dirname(fname
);
4959 curfid
= curfid
+ 1;
4961 flinkname
= member
.linkname
;
4962 fdev_minor
= member
.devminor
;
4963 fdev_major
= member
.devmajor
;
4964 frdev_minor
= member
.devminor
;
4965 frdev_major
= member
.devmajor
;
4966 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
4968 elif(ftype
==0 or ftype
==7):
4969 fsize
= member
.size
;
4971 fsize
= member
.size
;
4972 fatime
= member
.mtime
;
4973 fmtime
= member
.mtime
;
4974 fctime
= member
.mtime
;
4975 fbtime
= member
.mtime
;
4977 fchmode
= stat
.S_IMODE(ffullmode
);
4978 ftypemod
= stat
.S_IFMT(ffullmode
);
4981 funame
= member
.uname
;
4982 fgname
= member
.gname
;
4983 flinkcount
= flinkcount
;
4984 fwinattributes
= int(0);
4985 fcontents
= BytesIO();
4986 if(ftype
==0 or ftype
==7):
4987 with tarfp
.extractfile(member
) as fpc
:
4988 shutil
.copyfileobj(fpc
, fcontents
);
4989 fcontents
.seek(0, 0);
4990 ftypehex
= format(ftype
, 'x').lower();
4991 extrafields
= len(extradata
);
4992 extrafieldslist
= extradata
;
4993 catfextrafields
= extrafields
;
4994 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
4995 if(len(extradata
)>0):
4996 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4997 extrasizelen
= len(extrasizestr
);
4998 extrasizelenhex
= format(extrasizelen
, 'x').lower();
4999 catoutlist
= [ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fwinattributes
), 'x').lower(), format(int(fuid
), 'x').lower(), funame
, format(int(fgid
), 'x').lower(), fgname
, format(int(fcurfid
), 'x').lower(), format(int(fcurinode
), 'x').lower(), format(int(flinkcount
), 'x').lower(), format(int(fdev_minor
), 'x').lower(), format(int(fdev_major
), 'x').lower(), format(int(frdev_minor
), 'x').lower(), format(int(frdev_major
), 'x').lower(), extrasizelenhex
, format(catfextrafields
, 'x').lower()];
5000 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5001 catoutlenhex
= format(catoutlen
, 'x').lower();
5002 catoutlist
.insert(0, catoutlenhex
);
5003 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5004 if(len(extradata
)>0):
5005 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5006 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5007 catfnumfields
= catoutlen
;
5008 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5009 fcontents
.seek(0, 0);
5010 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
5011 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5012 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
5013 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5014 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5015 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5016 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5017 nullstrecd
= formatspecs
[5].encode('UTF-8');
5018 fheadtell
+= len(catfileoutstr
) + 1;
5019 catfcontentend
= fheadtell
- 1;
5020 fcontents
.seek(0, 0);
5021 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
5022 pyhascontents
= False;
5023 if(int(fsize
)>0 and not listonly
):
5024 pyhascontents
= True;
5025 if(int(fsize
)>0 and listonly
):
5027 pyhascontents
= False;
5028 fcontents
.seek(0, 0);
5029 catlist
['ffilelist'].update({fileidnum
: {'fid': fileidnum
, 'fidalt': fileidnum
, 'fheadersize': int(catheaersize
, 16), 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fwinattributes': fwinattributes
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': checksumtype
, 'fnumfields': catfnumfields
+ 2, 'frawheader': catheaderdata
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
5030 fileidnum
= fileidnum
+ 1;
5033 def ZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5039 inodetocatinode
= {};
5043 if(hasattr(sys
.stdin
, "buffer")):
5044 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
5046 shutil
.copyfileobj(sys
.stdin
, infile
);
5051 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
5052 infile
= download_file_from_internet_file(infile
);
5057 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5061 if(not zipfile
.is_zipfile(infile
)):
5064 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
5065 except FileNotFoundError
:
5067 ziptest
= zipfp
.testzip();
5069 VerbosePrintOut("Bad file found!");
5070 fnumfiles
= int(len(zipfp
.infolist()));
5071 catver
= formatspecs
[6];
5072 fileheaderver
= str(int(catver
.replace(".", "")));
5073 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5074 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5075 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5076 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5077 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
5078 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
5079 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
5080 fheadtell
= len(fileheader
);
5081 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5082 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
5083 if(re
.findall("^[.|/]", member
.filename
)):
5084 fname
= member
.filename
;
5086 fname
= "./"+member
.filename
;
5087 zipinfo
= zipfp
.getinfo(member
.filename
);
5089 VerbosePrintOut(fname
);
5090 if(not member
.is_dir()):
5091 fpremode
= stat
.S_IFREG
+ 438;
5092 elif(member
.is_dir()):
5093 fpremode
= stat
.S_IFDIR
+ 511;
5096 if(not member
.is_dir()):
5098 elif(member
.is_dir()):
5101 fbasedir
= os
.path
.dirname(fname
);
5105 curfid
= curfid
+ 1;
5113 fsize
= member
.file_size
;
5115 fsize
= member
.file_size
;
5116 fatime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5117 fmtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5118 fctime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5119 fbtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5120 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
5121 fwinattributes
= int(zipinfo
.external_attr
);
5122 if(not member
.is_dir()):
5123 fmode
= int(stat
.S_IFREG
+ 438);
5124 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
5125 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
5126 elif(member
.is_dir()):
5127 fmode
= int(stat
.S_IFDIR
+ 511);
5128 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
5129 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
5130 elif(zipinfo
.create_system
==3):
5131 fwinattributes
= int(0);
5132 fmode
= int(zipinfo
.external_attr
);
5134 fwinattributes
= int(0);
5135 if(not member
.is_dir()):
5136 fmode
= int(stat
.S_IFREG
+ 438);
5137 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
5138 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
5139 elif(member
.is_dir()):
5140 fmode
= int(stat
.S_IFDIR
+ 511);
5141 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
5142 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
5145 except AttributeError:
5151 except AttributeError:
5158 userinfo
= pwd
.getpwuid(os
.getuid());
5159 funame
= userinfo
.pw_name
;
5162 except AttributeError:
5170 groupinfo
= grp
.getgrgid(os
.getgid());
5171 fgname
= groupinfo
.gr_name
;
5174 except AttributeError:
5178 fcontents
= BytesIO();
5180 fcontents
.write(zipfp
.read(member
.filename
));
5181 fcontents
.seek(0, 0);
5182 ftypehex
= format(ftype
, 'x').lower();
5183 extrafields
= len(extradata
);
5184 extrafieldslist
= extradata
;
5185 catfextrafields
= extrafields
;
5186 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5187 if(len(extradata
)>0):
5188 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5189 extrasizelen
= len(extrasizestr
);
5190 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5191 catoutlist
= [ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fwinattributes
), 'x').lower(), format(int(fuid
), 'x').lower(), funame
, format(int(fgid
), 'x').lower(), fgname
, format(int(fcurfid
), 'x').lower(), format(int(fcurinode
), 'x').lower(), format(int(flinkcount
), 'x').lower(), format(int(fdev_minor
), 'x').lower(), format(int(fdev_major
), 'x').lower(), format(int(frdev_minor
), 'x').lower(), format(int(frdev_major
), 'x').lower(), extrasizelenhex
, format(catfextrafields
, 'x').lower()];
5192 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5193 catoutlenhex
= format(catoutlen
, 'x').lower();
5194 catoutlist
.insert(0, catoutlenhex
);
5195 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5196 if(len(extradata
)>0):
5197 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5198 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5199 catfnumfields
= catoutlen
;
5200 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5201 fcontents
.seek(0, 0);
5202 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
5203 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5204 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
5205 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5206 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5207 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5208 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5209 nullstrecd
= formatspecs
[5].encode('UTF-8');
5210 fheadtell
+= len(catfileoutstr
) + 1;
5211 catfcontentend
= fheadtell
- 1;
5212 fcontents
.seek(0, 0);
5213 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
5214 pyhascontents
= False;
5215 if(int(fsize
)>0 and not listonly
):
5216 pyhascontents
= True;
5217 if(int(fsize
)>0 and listonly
):
5218 fcontents
= BytesIO();
5219 pyhascontents
= False;
5220 fcontents
.seek(0, 0);
5221 catlist
['ffilelist'].update({fileidnum
: {'fid': fileidnum
, 'fidalt': fileidnum
, 'fheadersize': int(catheaersize
, 16), 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fwinattributes': fwinattributes
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': checksumtype
, 'fnumfields': catfnumfields
+ 2, 'frawheader': catheaderdata
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
5222 fileidnum
= fileidnum
+ 1;
5225 if(not rarfile_support
):
5226 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5229 if(rarfile_support
):
5230 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5236 inodetocatinode
= {};
5238 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
5240 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
5242 rarfp
= rarfile
.RarFile(infile
, "r");
5243 rartest
= rarfp
.testrar();
5245 VerbosePrintOut("Bad file found!");
5246 fnumfiles
= int(len(rarfp
.infolist()));
5247 catver
= formatspecs
[6];
5248 fileheaderver
= str(int(catver
.replace(".", "")));
5249 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5250 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5251 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5252 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5253 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
5254 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
5255 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
5256 fheadtell
= len(fileheader
);
5257 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5258 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
5261 if(member
.host_os
==rarfile
.RAR_OS_UNIX
):
5264 member
.external_attr
5266 except AttributeError:
5268 elif(member
.host_os
==rarfile
.RAR_OS_WIN32
):
5271 member
.external_attr
5273 except AttributeError:
5278 if(re
.findall("^[.|/]", member
.filename
)):
5279 fname
= member
.filename
;
5281 fname
= "./"+member
.filename
;
5282 rarinfo
= rarfp
.getinfo(member
.filename
);
5284 VerbosePrintOut(fname
);
5285 if(is_unix
and member
.external_attr
!=0):
5286 fpremode
= int(member
.external_attr
);
5287 elif(member
.is_file()):
5288 fpremode
= stat
.S_IFREG
+ 438;
5289 elif(member
.is_symlink()):
5290 fpremode
= stat
.S_IFLNK
+ 438;
5291 elif(member
.is_dir()):
5292 fpremode
= stat
.S_IFDIR
+ 511;
5293 if(is_windows
and member
.external_attr
!=0):
5294 fwinattributes
= int(member
.external_attr
);
5296 fwinattributes
= int(0);
5299 if(member
.is_file()):
5301 elif(member
.is_symlink()):
5303 elif(member
.is_dir()):
5307 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
5308 fbasedir
= os
.path
.dirname(fname
);
5312 curfid
= curfid
+ 1;
5320 fsize
= member
.file_size
;
5323 fatime
= int(member
.atime
.timestamp());
5325 fatime
= int(member
.mtime
.timestamp());
5326 except AttributeError:
5327 fatime
= int(member
.mtime
.timestamp());
5328 fmtime
= int(member
.mtime
.timestamp());
5331 fctime
= int(member
.ctime
.timestamp());
5333 fctime
= int(member
.mtime
.timestamp());
5334 except AttributeError:
5335 fctime
= int(member
.mtime
.timestamp());
5336 fbtime
= int(member
.mtime
.timestamp());
5337 if(is_unix
and member
.external_attr
!=0):
5338 fmode
= int(member
.external_attr
);
5339 fchmode
= int(stat
.S_IMODE(member
.external_attr
));
5340 ftypemod
= int(stat
.S_IFMT(member
.external_attr
));
5341 elif(member
.is_file()):
5342 fmode
= int(stat
.S_IFREG
+ 438)
5343 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
5344 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
5345 elif(member
.is_symlink()):
5346 fmode
= int(stat
.S_IFLNK
+ 438)
5347 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
5348 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
5349 elif(member
.is_dir()):
5350 fmode
= int(stat
.S_IFDIR
+ 511)
5351 fchmode
= int(stat
.S_IMODE(stat
.S_IFDIR
+ 511));
5352 ftypemod
= int(stat
.S_IFMT(stat
.S_IFDIR
+ 511));
5355 except AttributeError:
5361 except AttributeError:
5368 userinfo
= pwd
.getpwuid(os
.getuid());
5369 funame
= userinfo
.pw_name
;
5372 except AttributeError:
5380 groupinfo
= grp
.getgrgid(os
.getgid());
5381 fgname
= groupinfo
.gr_name
;
5384 except AttributeError:
5388 fcontents
= BytesIO();
5390 fcontents
.write(rarfp
.read(member
.filename
));
5391 fcontents
.seek(0, 0);
5392 ftypehex
= format(ftype
, 'x').lower();
5393 extrafields
= len(extradata
);
5394 extrafieldslist
= extradata
;
5395 catfextrafields
= extrafields
;
5396 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5397 if(len(extradata
)>0):
5398 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5399 extrasizelen
= len(extrasizestr
);
5400 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5401 catoutlist
= [ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fwinattributes
), 'x').lower(), format(int(fuid
), 'x').lower(), funame
, format(int(fgid
), 'x').lower(), fgname
, format(int(fcurfid
), 'x').lower(), format(int(fcurinode
), 'x').lower(), format(int(flinkcount
), 'x').lower(), format(int(fdev_minor
), 'x').lower(), format(int(fdev_major
), 'x').lower(), format(int(frdev_minor
), 'x').lower(), format(int(frdev_major
), 'x').lower(), extrasizelenhex
, format(catfextrafields
, 'x').lower()];
5402 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5403 catoutlenhex
= format(catoutlen
, 'x').lower();
5404 catoutlist
.insert(0, catoutlenhex
);
5405 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5406 if(len(extradata
)>0):
5407 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5408 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5409 catfnumfields
= 24 + catfextrafields
;
5410 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5411 fcontents
.seek(0, 0);
5412 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
5413 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5414 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
5415 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5416 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5417 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5418 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5419 nullstrecd
= formatspecs
[5].encode('UTF-8');
5420 fheadtell
+= len(catfileoutstr
) + 1;
5421 catfcontentend
= fheadtell
- 1;
5422 fcontents
.seek(0, 0);
5423 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
5424 pyhascontents
= False;
5425 if(int(fsize
)>0 and not listonly
):
5426 pyhascontents
= True;
5427 if(int(fsize
)>0 and listonly
):
5428 fcontents
= BytesIO();
5429 pyhascontents
= False;
5430 fcontents
.seek(0, 0);
5431 catlist
['ffilelist'].update({fileidnum
: {'fid': fileidnum
, 'fidalt': fileidnum
, 'fheadersize': int(catheaersize
, 16), 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fwinattributes': fwinattributes
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': checksumtype
, 'fnumfields': catfnumfields
+ 2, 'frawheader': catheaderdata
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
5432 fileidnum
= fileidnum
+ 1;
5435 if(not py7zr_support
):
5436 def SevenZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5440 def SevenZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5446 inodetocatinode
= {};
5448 szpfp
= py7zr
.SevenZipFile(infile
, mode
="r");
5449 file_content
= szpfp
.readall();
5450 #sztest = szpfp.testzip();
5451 sztestalt
= szpfp
.test();
5453 VerbosePrintOut("Bad file found!");
5454 numfiles
= int(len(szpfp
.list()));
5455 catver
= formatspecs
[6];
5456 fileheaderver
= str(int(catver
.replace(".", "")));
5457 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5458 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5459 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5460 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5461 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
5462 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
5463 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
5464 fheadtell
= len(fileheader
);
5465 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5466 for member
in sorted(szpfp
.list(), key
=lambda x
: x
.filename
):
5467 if(re
.findall("^[.|/]", member
.filename
)):
5468 fname
= member
.filename
;
5470 fname
= "./"+member
.filename
;
5471 if(not member
.is_directory
):
5472 fpremode
= int(stat
.S_IFREG
+ 438);
5473 elif(member
.is_directory
):
5474 fpremode
= int(stat
.S_IFDIR
+ 511);
5475 fwinattributes
= int(0);
5478 if(member
.is_directory
):
5483 fbasedir
= os
.path
.dirname(fname
);
5487 curfid
= curfid
+ 1;
5494 fatime
= int(member
.creationtime
.timestamp());
5495 fmtime
= int(member
.creationtime
.timestamp());
5496 fctime
= int(member
.creationtime
.timestamp());
5497 fbtime
= int(member
.creationtime
.timestamp());
5498 if(member
.is_directory
):
5499 fmode
= int(stat
.S_IFDIR
+ 511)
5500 fchmode
= int(stat
.S_IMODE(stat
.S_IFDIR
+ 511));
5501 ftypemod
= int(stat
.S_IFMT(stat
.S_IFDIR
+ 511));
5503 fmode
= int(stat
.S_IFLNK
+ 438)
5504 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
5505 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
5508 except AttributeError:
5514 except AttributeError:
5521 userinfo
= pwd
.getpwuid(os
.getuid());
5522 funame
= userinfo
.pw_name
;
5525 except AttributeError:
5533 groupinfo
= grp
.getgrgid(os
.getgid());
5534 fgname
= groupinfo
.gr_name
;
5537 except AttributeError:
5541 fcontents
= BytesIO();
5543 fcontents
.write(file_content
[member
.filename
].read());
5544 fsize
= format(fcontents
.tell(), 'x').lower();
5546 fcontents
.seek(0, 0);
5547 ftypehex
= format(ftype
, 'x').lower();
5548 extrafields
= len(extradata
);
5549 extrafieldslist
= extradata
;
5550 catfextrafields
= extrafields
;
5551 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5552 if(len(extradata
)>0):
5553 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5554 extrasizelen
= len(extrasizestr
);
5555 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5556 catoutlist
= [ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fwinattributes
), 'x').lower(), format(int(fuid
), 'x').lower(), funame
, format(int(fgid
), 'x').lower(), fgname
, format(int(fcurfid
), 'x').lower(), format(int(fcurinode
), 'x').lower(), format(int(flinkcount
), 'x').lower(), format(int(fdev_minor
), 'x').lower(), format(int(fdev_major
), 'x').lower(), format(int(frdev_minor
), 'x').lower(), format(int(frdev_major
), 'x').lower(), extrasizelenhex
, format(catfextrafields
, 'x').lower()];
5557 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5558 catoutlenhex
= format(catoutlen
, 'x').lower();
5559 catoutlist
.insert(0, catoutlenhex
);
5560 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5561 if(len(extradata
)>0):
5562 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5563 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5564 catfnumfields
= 24 + catfextrafields
;
5565 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5566 fcontents
.seek(0, 0);
5567 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
5568 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5569 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
5570 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5571 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5572 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5573 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5574 nullstrecd
= formatspecs
[5].encode('UTF-8');
5575 fheadtell
+= len(catfileoutstr
) + 1;
5576 catfcontentend
= fheadtell
- 1;
5577 fcontents
.seek(0, 0);
5578 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
5579 pyhascontents
= False;
5580 if(int(fsize
)>0 and not listonly
):
5581 pyhascontents
= True;
5582 if(int(fsize
)>0 and listonly
):
5584 pyhascontents
= False;
5585 fcontents
.seek(0, 0);
5586 catlist
['ffilelist'].update({fileidnum
: {'fid': fileidnum
, 'fidalt': fileidnum
, 'fheadersize': int(catheaersize
, 16), 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fwinattributes': fwinattributes
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': checksumtype
, 'fnumfields': catfnumfields
+ 2, 'frawheader': catheaderdata
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
5587 fileidnum
= fileidnum
+ 1;
5590 def InFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5591 checkcompressfile
= CheckCompressionSubType(infiles
, formatspecs
, True);
5592 if(checkcompressfile
=="tarfile"):
5593 return TarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5594 elif(checkcompressfile
=="zipfile"):
5595 return ZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5596 elif(checkcompressfile
=="catfile"):
5597 return ArchiveFileToArray(infile
, 0, 0, listonly
, False, formatspecs
, False);
5598 elif(rarfile_support
and checkcompressfile
=="rarfile"):
5599 return RarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5600 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
5601 return SevenZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5606 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):
5607 outarray
= BytesIO();
5608 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
5609 listcatfiles
= ArchiveFileToArray(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5610 return listcatfiles
;
5612 def ArchiveFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5613 if(isinstance(infile
, dict)):
5614 listcatfiles
= infile
;
5616 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5617 infile
= RemoveWindowsPath(infile
);
5618 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5619 if(not listcatfiles
):
5621 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': {}}}};
5623 catarray
.update({'catfp': listcatfiles
['catfp']});
5624 lenlist
= len(listcatfiles
['ffilelist']);
5626 lcfx
= int(listcatfiles
['fnumfiles']);
5627 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5628 lcfx
= int(lenlist
);
5630 lcfx
= int(listcatfiles
['fnumfiles']);
5632 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5633 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5634 catarray
['filetoid'].update(filetoidarray
);
5635 catarray
['idtofile'].update(idtofilearray
);
5636 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5637 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5638 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5639 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5640 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5641 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5642 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5643 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5644 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5645 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5646 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5647 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5648 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5649 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5650 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5651 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5652 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5653 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5654 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5655 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5656 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5657 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5658 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5659 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5660 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5661 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5662 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5663 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5664 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5665 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5666 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5670 create_alias_function("", __file_format_name__
, "ToArrayIndex", ArchiveFileToArrayIndex
);
5672 def ListDirToArrayIndexAlt(infiles
, dirlistfromtxt
=False, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5673 listcatfiles
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5674 if(not listcatfiles
):
5676 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': {}}}};
5677 lenlist
= len(listcatfiles
['ffilelist']);
5679 lcfx
= int(listcatfiles
['fnumfiles']);
5680 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5681 lcfx
= int(lenlist
);
5683 lcfx
= int(listcatfiles
['fnumfiles']);
5685 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5686 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5687 catarray
['filetoid'].update(filetoidarray
);
5688 catarray
['idtofile'].update(idtofilearray
);
5689 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5690 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5691 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5692 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5693 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5694 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5695 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5696 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5697 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5698 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5699 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5700 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5701 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5702 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5703 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5704 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5705 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5706 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5707 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5708 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5709 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5710 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5711 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5712 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5713 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5714 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5715 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5716 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5717 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5718 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5719 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5723 def TarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5724 listcatfiles
= TarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5725 if(not listcatfiles
):
5727 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': {}}}};
5728 lenlist
= len(listcatfiles
['ffilelist']);
5730 lcfx
= int(listcatfiles
['fnumfiles']);
5731 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5732 lcfx
= int(lenlist
);
5734 lcfx
= int(listcatfiles
['fnumfiles']);
5736 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5737 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5738 catarray
['filetoid'].update(filetoidarray
);
5739 catarray
['idtofile'].update(idtofilearray
);
5740 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5741 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5742 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5743 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5744 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5745 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5746 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5747 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5748 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5749 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5750 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5751 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5752 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5753 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5754 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5755 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5756 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5757 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5758 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5759 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5760 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5761 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5762 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5763 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5764 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5765 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5766 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5767 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5768 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5769 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5770 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5774 def ZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5775 listcatfiles
= ZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5776 if(not listcatfiles
):
5778 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': {}}}};
5779 lenlist
= len(listcatfiles
['ffilelist']);
5781 lcfx
= int(listcatfiles
['fnumfiles']);
5782 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5783 lcfx
= int(lenlist
);
5785 lcfx
= int(listcatfiles
['fnumfiles']);
5787 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5788 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5789 catarray
['filetoid'].update(filetoidarray
);
5790 catarray
['idtofile'].update(idtofilearray
);
5791 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5792 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5793 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5794 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5795 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5796 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5797 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5798 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5799 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5800 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5801 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5802 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5803 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5804 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5805 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5806 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5807 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5808 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5809 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5810 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5811 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5812 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5813 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5814 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5815 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5816 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5817 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5818 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5819 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5820 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5821 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5825 if(not rarfile_support
):
5826 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5829 if(rarfile_support
):
5830 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5831 listcatfiles
= RarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5832 if(not listcatfiles
):
5834 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': {}}}};
5835 lenlist
= len(listcatfiles
['ffilelist']);
5837 lcfx
= int(listcatfiles
['fnumfiles']);
5838 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5839 lcfx
= int(lenlist
);
5841 lcfx
= int(listcatfiles
['fnumfiles']);
5843 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5844 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5845 catarray
['filetoid'].update(filetoidarray
);
5846 catarray
['idtofile'].update(idtofilearray
);
5847 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5848 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5849 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5850 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5851 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5852 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5853 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5854 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5855 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5856 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5857 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5858 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5859 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5860 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5861 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5862 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5863 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5864 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5865 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5866 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5867 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5868 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5869 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5870 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5871 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5872 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5873 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5874 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5875 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5876 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5877 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5881 if(not py7zr_support
):
5882 def SevenZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5886 def SevenZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5887 listcatfiles
= SevenZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5888 if(not listcatfiles
):
5890 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': {}}}};
5891 lenlist
= len(listcatfiles
['ffilelist']);
5893 lcfx
= int(listcatfiles
['fnumfiles']);
5894 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5895 lcfx
= int(lenlist
);
5897 lcfx
= int(listcatfiles
['fnumfiles']);
5899 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5900 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5901 catarray
['filetoid'].update(filetoidarray
);
5902 catarray
['idtofile'].update(idtofilearray
);
5903 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5904 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5905 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5906 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5907 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5908 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5909 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5910 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5911 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5912 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5913 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5914 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5915 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5916 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5917 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5918 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5919 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5920 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5921 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5922 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5923 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5924 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5925 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5926 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5927 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5928 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5929 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5930 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5931 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5932 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5933 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5937 def ArchiveFileStringToArrayIndex(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5938 catfp
= BytesIO(catstr
);
5939 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5940 return listcatfiles
;
5942 create_alias_function("", __file_format_name__
, "StringToArrayIndex", ArchiveFileStringToArrayIndex
);
5944 def TarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5946 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5947 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5948 return listcatfiles
;
5950 def ZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5952 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5953 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5954 return listcatfiles
;
5956 if(not rarfile_support
):
5957 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5960 if(rarfile_support
):
5961 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5963 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5964 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5965 return listcatfiles
;
5967 if(not py7zr_support
):
5968 def SevenZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5972 def SevenZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5974 catfp
= PackArchiveFileFromSevenZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5975 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5976 return listcatfiles
;
5978 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):
5979 outarray
= BytesIO();
5980 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, verbose
, True);
5981 listcatfiles
= ArchiveFileToArrayIndex(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
)
5982 return listcatfiles
;
5984 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):
5985 if(isinstance(infile
, dict)):
5986 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5987 listcatfiles
= prelistcatfiles
['list'];
5989 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5990 infile
= RemoveWindowsPath(infile
);
5992 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5993 listcatfiles
= prelistcatfiles
['list'];
5995 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5996 if(outfile
!="-" and not hasattr(infile
, "read") and not hasattr(outfile
, "write")):
5997 outfile
= RemoveWindowsPath(outfile
);
5998 checksumtype
= checksumtype
.lower();
5999 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
6000 checksumtype
="crc32";
6001 if(checksumtype
=="none"):
6003 if(not compression
or compression
=="catfile" or compression
==formatspecs
[2]):
6004 compression
= "auto";
6005 if(compression
not in compressionlist
and compression
is None):
6006 compression
= "auto";
6008 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6009 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
6010 if(os
.path
.exists(outfile
)):
6013 except OSError as e
:
6015 if(not listcatfiles
):
6020 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
6022 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
6025 fbasename
= os
.path
.splitext(outfile
)[0];
6026 fextname
= os
.path
.splitext(outfile
)[1];
6027 catfp
= CompressOpenFile(outfile
, True, compressionlevel
);
6028 catver
= formatspecs
[6];
6029 fileheaderver
= str(int(catver
.replace(".", "")));
6030 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
6031 catfp
.write(fileheader
.encode('UTF-8'));
6032 lenlist
= len(listcatfiles
['ffilelist']);
6033 fnumfiles
= int(listcatfiles
['fnumfiles']);
6034 if(lenlist
>fnumfiles
or lenlist
<fnumfiles
):
6035 fnumfiles
= lenlist
;
6036 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
6037 fnumfilesa
= AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
6038 catfileheadercshex
= GetFileChecksum(fileheader
+ fnumfilesa
, checksumtype
, True, formatspecs
);
6039 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
6040 catfp
.write(fnumfilesa
.encode('UTF-8'));
6043 os
.fsync(catfp
.fileno());
6044 except io
.UnsupportedOperation
:
6046 except AttributeError:
6048 except OSError as e
:
6050 lenlist
= len(listcatfiles
['ffilelist']);
6051 fnumfiles
= int(listcatfiles
['fnumfiles']);
6053 lcfx
= int(listcatfiles
['fnumfiles']);
6054 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6055 lcfx
= int(lenlist
);
6057 lcfx
= int(listcatfiles
['fnumfiles']);
6065 if(re
.findall("^[.|/]", listcatfiles
['ffilelist'][reallcfi
]['fname'])):
6066 fname
= listcatfiles
['ffilelist'][reallcfi
]['fname'];
6068 fname
= "./"+listcatfiles
['ffilelist'][reallcfi
]['fname'];
6070 VerbosePrintOut(fname
);
6071 fheadersize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fheadersize']), 'x').lower();
6072 fsize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fsize']), 'x').lower();
6073 flinkname
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
6074 fatime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fatime']), 'x').lower();
6075 fmtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmtime']), 'x').lower();
6076 fctime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fctime']), 'x').lower();
6077 fbtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fbtime']), 'x').lower();
6078 fmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmode']), 'x').lower();
6079 fchmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fchmode']), 'x').lower();
6080 fuid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fuid']), 'x').lower();
6081 funame
= listcatfiles
['ffilelist'][reallcfi
]['funame'];
6082 fgid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fgid']), 'x').lower();
6083 fgname
= listcatfiles
['ffilelist'][reallcfi
]['fgname'];
6084 finode
= listcatfiles
['ffilelist'][reallcfi
]['finode'];
6085 flinkcount
= listcatfiles
['ffilelist'][reallcfi
]['flinkcount'];
6086 fwinattributes
= flinkinfo
['ffilelist'][reallcfi
]['fwinattributes'];
6087 fdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fminor']), 'x').lower();
6088 fdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmajor']), 'x').lower();
6089 frdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frminor']), 'x').lower();
6090 frdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frmajor']), 'x').lower();
6091 if(len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] and len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>0):
6092 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
6093 if(len(extradata
)>0):
6094 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(extradata
);
6095 listcatfiles
['ffilelist'][reallcfi
]['fextralist'] = extradata
;
6096 extrafields
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']), 'x').lower();
6097 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
6098 if(len(extradata
)>0):
6099 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
6100 extrasizelen
= format(len(extrasizestr
), 'x').lower();
6101 fcontents
= listcatfiles
['ffilelist'][reallcfi
]['fcontents'];
6103 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']==1 or listcatfiles
['ffilelist'][reallcfi
]['ftype']==2):
6104 getflinkpath
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
6105 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
6106 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
6107 fheadersize
= format(int(flinkinfo
['fheadersize']), 'x').lower();
6108 fsize
= format(int(flinkinfo
['fsize']), 'x').lower();
6109 flinkname
= flinkinfo
['flinkname'];
6110 fatime
= format(int(flinkinfo
['fatime']), 'x').lower();
6111 fmtime
= format(int(flinkinfo
['fmtime']), 'x').lower();
6112 fctime
= format(int(flinkinfo
['fctime']), 'x').lower();
6113 fbtime
= format(int(flinkinfo
['fbtime']), 'x').lower();
6114 fmode
= format(int(flinkinfo
['fmode']), 'x').lower();
6115 fchmode
= format(int(flinkinfo
['fchmode']), 'x').lower();
6116 fuid
= format(int(flinkinfo
['fuid']), 'x').lower();
6117 funame
= flinkinfo
['funame'];
6118 fgid
= format(int(flinkinfo
['fgid']), 'x').lower();
6119 fgname
= flinkinfo
['fgname'];
6120 finode
= flinkinfo
['finode'];
6121 flinkcount
= flinkinfo
['flinkcount'];
6122 fwinattributes
= flinkinfo
['fwinattributes'];
6123 fdev_minor
= format(int(flinkinfo
['fminor']), 'x').lower();
6124 fdev_major
= format(int(flinkinfo
['fmajor']), 'x').lower();
6125 frdev_minor
= format(int(flinkinfo
['frminor']), 'x').lower();
6126 frdev_major
= format(int(flinkinfo
['frmajor']), 'x').lower();
6127 if(len(flinkinfo
['fextralist'])>flinkinfo
['fextrafields'] and len(flinkinfo
['fextralist'])>0):
6128 flinkinfo
['fextrafields'] = len(flinkinfo
['fextralist']);
6129 if(len(extradata
)>0):
6130 flinkinfo
['fextrafields'] = len(extradata
);
6131 flinkinfo
['fextralist'] = extradata
;
6132 extrafields
= format(int(flinkinfo
['fextrafields']), 'x').lower();
6133 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
6134 if(len(extradata
)>0):
6135 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
6136 extrasizelen
= format(len(extrasizestr
), 'x').lower();
6137 fcontents
= flinkinfo
['fcontents'];
6138 ftypehex
= format(flinkinfo
['ftype'], 'x').lower();
6140 ftypehex
= format(listcatfiles
['ffilelist'][reallcfi
]['ftype'], 'x').lower();
6141 fcurfid
= format(curfid
, 'x').lower();
6142 if(not followlink
and finode
!=0):
6143 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=1):
6144 fcurinode
= format(int(curinode
), 'x').lower();
6145 inodetofile
.update({curinode
: fname
});
6146 filetoinode
.update({fname
: curinode
});
6147 curinode
= curinode
+ 1;
6149 fcurinode
= format(int(filetoinode
[flinkname
]), 'x').lower();
6151 fcurinode
= format(int(curinode
), 'x').lower();
6152 curinode
= curinode
+ 1;
6153 curfid
= curfid
+ 1;
6154 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
];
6155 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
6156 catoutlenhex
= format(catoutlen
, 'x').lower();
6157 catoutlist
.insert(0, catoutlenhex
);
6158 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
6159 if(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']>0):
6160 extrafieldslist
= [];
6162 exil
= listcatfiles
['ffilelist'][reallcfi
]['fextrafields'];
6164 extrafieldslist
.append(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
6166 catfileoutstr
+= AppendNullBytes([extrafieldslist
], formatspecs
[5]);
6167 catfileoutstr
+= AppendNullBytes([checksumtype
], formatspecs
[5]);
6168 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
6169 fcontents
.seek(0, 0);
6170 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
6171 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
6172 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
6173 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
6174 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
6175 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
6176 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
6177 nullstrecd
= formatspecs
[5].encode('UTF-8');
6178 fcontents
.seek(0, 0);
6179 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
6180 catfp
.write(catfileout
);
6183 os
.fsync(catfp
.fileno());
6184 except io
.UnsupportedOperation
:
6186 except AttributeError:
6188 except OSError as e
:
6191 reallcfi
= reallcfi
+ 1;
6193 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
6194 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
6195 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
6198 os
.fsync(catfp
.fileno());
6199 except io
.UnsupportedOperation
:
6201 except AttributeError:
6203 except OSError as e
:
6207 if(hasattr(sys
.stdout
, "buffer")):
6208 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
6210 shutil
.copyfileobj(catfp
, sys
.stdout
);
6211 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
6212 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
6214 upload_file_to_internet_file(catfp
, outfile
);
6222 create_alias_function("RePack", __file_format_name__
, "", RePackArchiveFile
);
6224 def RePackArchiveFileFromString(catstr
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6225 catfp
= BytesIO(catstr
);
6226 listcatfiles
= RePackArchiveFile(catfp
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
6227 return listcatfiles
;
6229 create_alias_function("RePack", __file_format_name__
, "FromString", RePackArchiveFileFromString
);
6231 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):
6232 outarray
= BytesIO();
6233 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
6234 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
6235 return listcatfiles
;
6237 create_alias_function("Pack", __file_format_name__
, "FromListDir", PackArchiveFileFromListDir
);
6239 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):
6240 if(outdir
is not None):
6241 outdir
= RemoveWindowsPath(outdir
);
6243 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6244 if(isinstance(infile
, dict)):
6245 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6246 listcatfiles
= prelistcatfiles
['list'];
6248 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6249 infile
= RemoveWindowsPath(infile
);
6251 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6252 listcatfiles
= prelistcatfiles
['list'];
6254 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6255 if(not listcatfiles
):
6257 lenlist
= len(listcatfiles
['ffilelist']);
6258 fnumfiles
= int(listcatfiles
['fnumfiles']);
6260 lcfx
= int(listcatfiles
['fnumfiles']);
6261 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6262 lcfx
= int(lenlist
);
6264 lcfx
= int(listcatfiles
['fnumfiles']);
6270 userinfo
= pwd
.getpwuid(listcatfiles
['ffilelist'][lcfi
]['fuid']);
6271 funame
= userinfo
.pw_name
;
6280 groupinfo
= grp
.getgrgid(listcatfiles
['ffilelist'][lcfi
]['fgid']);
6281 fgname
= groupinfo
.gr_name
;
6287 VerbosePrintOut(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6288 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6289 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
6290 listcatfiles
['ffilelist'][lcfi
]['fcontents'].seek(0, 0);
6291 shutil
.copyfileobj(listcatfiles
['ffilelist'][lcfi
]['fcontents'], fpc
);
6294 os
.fsync(fpc
.fileno());
6295 except io
.UnsupportedOperation
:
6297 except AttributeError:
6299 except OSError as e
:
6301 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
6302 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
6303 if(preservepermissions
):
6304 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6306 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
6307 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6309 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6310 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
6311 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
6316 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
6317 funame
= userinfo
.pw_name
;
6326 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
6327 fgname
= groupinfo
.gr_name
;
6332 if(flinkinfo
['ftype']==0 or flinkinfo
['ftype']==7):
6333 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
6334 flinkinfo
['fcontents'].seek(0, 0);
6335 shutil
.copyfileobj(flinkinfo
['fcontents'], fpc
);
6338 os
.fsync(fpc
.fileno());
6339 except io
.UnsupportedOperation
:
6341 except AttributeError:
6343 except OSError as e
:
6345 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6346 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6347 if(preservepermissions
):
6348 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6350 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6351 if(flinkinfo
['ftype']==1):
6352 os
.link(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6353 if(flinkinfo
['ftype']==2):
6354 os
.symlink(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6355 if(flinkinfo
['ftype']==5):
6356 if(preservepermissions
):
6357 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6359 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6360 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6361 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6362 if(preservepermissions
):
6363 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6365 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6366 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
6367 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6369 os
.link(listcatfiles
['ffilelist'][lcfi
]['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6370 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6372 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6373 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
6374 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
6379 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
6380 funame
= userinfo
.pw_name
;
6389 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
6390 fgname
= groupinfo
.gr_name
;
6395 if(flinkinfo
['ftype']==0 or flinkinfo
['ftype']==7):
6396 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
6397 flinkinfo
['fcontents'].seek(0, 0);
6398 shutil
.copyfileobj(flinkinfo
['fcontents'], fpc
);
6401 os
.fsync(fpc
.fileno());
6402 except io
.UnsupportedOperation
:
6404 except AttributeError:
6406 except OSError as e
:
6408 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6409 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6410 if(preservepermissions
):
6411 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6413 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6414 if(flinkinfo
['ftype']==1):
6415 os
.link(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6416 if(flinkinfo
['ftype']==2):
6417 os
.symlink(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6418 if(flinkinfo
['ftype']==5):
6419 if(preservepermissions
):
6420 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6422 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6423 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6424 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6425 if(preservepermissions
):
6426 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6428 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6429 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
6430 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6432 os
.symlink(listcatfiles
['ffilelist'][lcfi
]['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6433 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6434 if(preservepermissions
):
6435 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6437 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6438 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
6439 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
6440 if(preservepermissions
):
6441 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6443 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
6444 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6 and hasattr(os
, "mkfifo")):
6445 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6448 return listcatfiles
['ffilelist']['catfp'];
6452 create_alias_function("UnPack", __file_format_name__
, "", UnPackArchiveFile
);
6454 if(hasattr(shutil
, "register_unpack_format")):
6455 def UnPackArchiveFileFunc(archive_name
, extract_dir
=None, **kwargs
):
6456 return UnPackArchiveFile(archive_name
, extract_dir
, False, 0, 0, False, __file_format_delimiter__
, False, False);
6457 create_alias_function("UnPack", __file_format_name__
, "Func", UnPackArchiveFileFunc
);
6459 def UnPackArchiveFileString(catstr
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6460 catfp
= BytesIO(catstr
);
6461 listcatfiles
= UnPackArchiveFile(catfp
, outdir
, followlink
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6462 return listcatfiles
;
6464 create_alias_function("UnPack", __file_format_name__
, "String", UnPackArchiveFileString
);
6466 def ArchiveFileListFiles(infile
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6467 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6468 if(isinstance(infile
, dict)):
6469 listcatfiles
= infile
;
6471 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6472 infile
= RemoveWindowsPath(infile
);
6473 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, True, skipchecksum
, formatspecs
, returnfp
);
6474 if(not listcatfiles
):
6476 lenlist
= len(listcatfiles
['ffilelist']);
6477 fnumfiles
= int(listcatfiles
['fnumfiles']);
6479 lcfx
= int(listcatfiles
['fnumfiles']);
6480 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6481 lcfx
= int(lenlist
);
6483 lcfx
= int(listcatfiles
['fnumfiles']);
6486 returnval
.update({lcfi
: listcatfiles
['ffilelist'][lcfi
]['fname']});
6488 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
6490 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' } };
6491 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'];
6492 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6493 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " link to " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6494 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6495 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " -> " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6496 fuprint
= listcatfiles
['ffilelist'][lcfi
]['funame'];
6497 if(len(fuprint
)<=0):
6498 fuprint
= listcatfiles
['ffilelist'][lcfi
]['fuid'];
6499 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgname'];
6500 if(len(fgprint
)<=0):
6501 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgid'];
6502 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
));
6505 return listcatfiles
['catfp'];
6509 create_alias_function("", __file_format_name__
, "ListFiles", ArchiveFileListFiles
);
6511 def ArchiveFileStringListFiles(catstr
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6512 catfp
= BytesIO(catstr
);
6513 listcatfiles
= ArchiveFileListFiles(catstr
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6514 return listcatfiles
;
6516 create_alias_function("", __file_format_name__
, "StringListFiles", ArchiveFileStringListFiles
);
6518 def TarFileListFiles(infile
, verbose
=False, returnfp
=False):
6519 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6522 if(hasattr(sys
.stdin
, "buffer")):
6523 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
6525 shutil
.copyfileobj(sys
.stdin
, infile
);
6530 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
6531 infile
= download_file_from_internet_file(infile
);
6536 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6538 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
6540 if(not tarfile
.is_tarfile(infile
)):
6542 except AttributeError:
6543 if(not is_tarfile(infile
)):
6548 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
6549 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
6551 tarfp
= tarfile
.open(infile
, "r");
6552 except FileNotFoundError
:
6556 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
6557 returnval
.update({lcfi
: member
.name
});
6558 fpremode
= member
.mode
;
6559 ffullmode
= member
.mode
;
6563 ffullmode
= member
.mode
+ stat
.S_IFREG
;
6565 elif(member
.isdev()):
6566 ffullmode
= member
.mode
;
6568 elif(member
.islnk()):
6569 ffullmode
= member
.mode
+ stat
.S_IFREG
;
6571 elif(member
.issym()):
6572 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
6574 elif(member
.ischr()):
6575 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
6577 elif(member
.isblk()):
6578 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
6580 elif(member
.isdir()):
6581 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
6583 elif(member
.isfifo()):
6584 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
6586 elif(member
.issparse()):
6587 ffullmode
= member
.mode
;
6590 VerbosePrintOut(member
.name
);
6592 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' } };
6593 printfname
= member
.name
;
6595 printfname
= member
.name
+ " link to " + member
.linkname
;
6596 elif(member
.issym()):
6597 printfname
= member
.name
+ " -> " + member
.linkname
;
6598 fuprint
= member
.uname
;
6599 if(len(fuprint
)<=0):
6600 fuprint
= member
.uid
;
6601 fgprint
= member
.gname
;
6602 if(len(fgprint
)<=0):
6603 fgprint
= member
.gid
;
6604 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
));
6607 return listcatfiles
['catfp'];
6611 def ZipFileListFiles(infile
, verbose
=False, returnfp
=False):
6612 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6615 if(hasattr(sys
.stdin
, "buffer")):
6616 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
6618 shutil
.copyfileobj(sys
.stdin
, infile
);
6623 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
6624 infile
= download_file_from_internet_file(infile
);
6629 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6633 if(not zipfile
.is_zipfile(infile
)):
6636 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
6637 except FileNotFoundError
:
6641 ziptest
= zipfp
.testzip();
6643 VerbosePrintOut("Bad file found!");
6644 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
6645 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
6646 fwinattributes
= int(zipinfo
.external_attr
);
6647 if(not member
.is_dir()):
6648 fmode
= int(stat
.S_IFREG
+ 438);
6649 fchmode
= int(stat
.S_IMODE(fmode
));
6650 ftypemod
= int(stat
.S_IFMT(fmode
));
6651 elif(member
.is_dir()):
6652 fmode
= int(stat
.S_IFDIR
+ 511);
6653 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
6654 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
6655 elif(zipinfo
.create_system
==3):
6656 fwinattributes
=int(0);
6657 fmode
= int(zipinfo
.external_attr
);
6658 fchmode
= int(stat
.S_IMODE(fmode
));
6659 ftypemod
= int(stat
.S_IFMT(fmode
));
6661 fwinattributes
= int(0);
6662 if(not member
.is_dir()):
6663 fmode
= int(stat
.S_IFREG
+ 438);
6664 fchmode
= int(stat
.S_IMODE(fmode
));
6665 ftypemod
= int(stat
.S_IFMT(fmode
));
6666 elif(member
.is_dir()):
6667 fmode
= int(stat
.S_IFDIR
+ 511);
6668 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
6669 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
6670 returnval
.update({lcfi
: member
.filename
});
6672 VerbosePrintOut(member
.filename
);
6674 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' } };
6676 for fmodval
in str(oct(fmode
))[-3:]:
6677 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
6678 if(not member
.is_dir()):
6680 permissionstr
= "-" + permissionstr
;
6681 elif(member
.is_dir()):
6683 permissionstr
= "d" + permissionstr
;
6684 printfname
= member
.filename
;
6686 fuid
= int(os
.getuid());
6687 except AttributeError:
6692 fgid
= int(os
.getgid());
6693 except AttributeError:
6700 userinfo
= pwd
.getpwuid(os
.getuid());
6701 funame
= userinfo
.pw_name
;
6704 except AttributeError:
6712 groupinfo
= grp
.getgrgid(os
.getgid());
6713 fgname
= groupinfo
.gr_name
;
6716 except AttributeError:
6721 if(len(fuprint
)<=0):
6722 fuprint
= str(fuid
);
6724 if(len(fgprint
)<=0):
6725 fgprint
= str(fgid
);
6726 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
));
6729 return listcatfiles
['catfp'];
6733 if(not rarfile_support
):
6734 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
6735 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6736 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6739 if(rarfile_support
):
6740 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
6741 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6742 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6744 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
6748 rarfp
= rarfile
.RarFile(infile
, "r");
6749 rartest
= rarfp
.testrar();
6751 VerbosePrintOut("Bad file found!");
6752 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
6755 if(member
.host_os
==rarfile
.RAR_OS_UNIX
):
6758 member
.external_attr
6760 except AttributeError:
6762 elif(member
.host_os
==rarfile
.RAR_OS_WIN32
):
6765 member
.external_attr
6767 except AttributeError:
6772 if(is_unix
and member
.external_attr
!=0):
6773 fpremode
= int(member
.external_attr
);
6774 elif(member
.is_file()):
6775 fpremode
= int(stat
.S_IFREG
+ 438);
6776 elif(member
.is_symlink()):
6777 fpremode
= int(stat
.S_IFLNK
+ 438);
6778 elif(member
.is_dir()):
6779 fpremode
= int(stat
.S_IFDIR
+ 511);
6780 if(is_windows
and member
.external_attr
!=0):
6781 fwinattributes
= int(member
.external_attr
);
6783 fwinattributes
= int(0);
6784 if(is_unix
and member
.external_attr
!=0):
6785 fmode
= int(member
.external_attr
);
6786 fchmode
= int(stat
.S_IMODE(member
.external_attr
));
6787 ftypemod
= int(stat
.S_IFMT(member
.external_attr
));
6788 elif(member
.is_file()):
6789 fmode
= int(stat
.S_IFREG
+ 438);
6790 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
6791 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
6792 elif(member
.is_symlink()):
6793 fmode
= int(stat
.S_IFLNK
+ 438);
6794 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFLNK
+ 438)));
6795 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFLNK
+ 438)));
6796 elif(member
.is_dir()):
6797 fmode
= int(stat
.S_IFDIR
+ 511);
6798 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
6799 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
6800 returnval
.update({lcfi
: member
.filename
});
6802 VerbosePrintOut(member
.filename
);
6804 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' } };
6806 for fmodval
in str(oct(fmode
))[-3:]:
6807 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
6808 if(member
.is_file()):
6810 permissionstr
= "-" + permissionstr
;
6811 printfname
= member
.filename
;
6812 elif(member
.is_symlink()):
6814 permissionstr
= "l" + permissionstr
;
6815 printfname
= member
.name
+ " -> " + member
.read().decode("UTF-8");
6816 elif(member
.is_dir()):
6818 permissionstr
= "d" + permissionstr
;
6819 printfname
= member
.filename
;
6821 fuid
= int(os
.getuid());
6822 except AttributeError:
6827 fgid
= int(os
.getgid());
6828 except AttributeError:
6835 userinfo
= pwd
.getpwuid(os
.getuid());
6836 funame
= userinfo
.pw_name
;
6839 except AttributeError:
6847 groupinfo
= grp
.getgrgid(os
.getgid());
6848 fgname
= groupinfo
.gr_name
;
6851 except AttributeError:
6856 if(len(fuprint
)<=0):
6857 fuprint
= str(fuid
);
6859 if(len(fgprint
)<=0):
6860 fgprint
= str(fgid
);
6861 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(member
.file_size
).rjust(15) + " " + member
.mtime
.strftime('%Y-%m-%d %H:%M') + " " + printfname
));
6864 return listcatfiles
['catfp'];
6868 if(not py7zr_support
):
6869 def SevenZipFileListFiles(infile
, verbose
=False, returnfp
=False):
6870 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6871 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6875 def SevenZipFileListFiles(infile
, verbose
=False, returnfp
=False):
6876 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6877 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6881 szpfp
= py7zr
.SevenZipFile(infile
, mode
="r");
6882 file_content
= szpfp
.readall();
6883 #sztest = szpfp.testzip();
6884 sztestalt
= szpfp
.test();
6886 VerbosePrintOut("Bad file found!");
6887 for member
in sorted(szpfp
.list(), key
=lambda x
: x
.filename
):
6888 if(re
.findall("^[.|/]", member
.filename
)):
6889 fname
= member
.filename
;
6891 fname
= "./"+member
.filename
;
6892 if(not member
.is_directory
):
6893 fpremode
= int(stat
.S_IFREG
+ 438);
6894 elif(member
.is_directory
):
6895 fpremode
= int(stat
.S_IFDIR
+ 511);
6896 fwinattributes
= int(0);
6897 if(member
.is_directory
):
6898 fmode
= int(stat
.S_IFDIR
+ 511);
6899 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
6900 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
6902 fmode
= int(stat
.S_IFLNK
+ 438);
6903 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFLNK
+ 438)));
6904 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFLNK
+ 438)));
6905 returnval
.update({lcfi
: member
.filename
});
6907 VerbosePrintOut(member
.filename
);
6909 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' } };
6911 for fmodval
in str(oct(fmode
))[-3:]:
6912 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
6914 if(not member
.is_directory
):
6916 permissionstr
= "-" + permissionstr
;
6917 printfname
= member
.filename
;
6918 elif(member
.is_directory
):
6920 permissionstr
= "d" + permissionstr
;
6921 printfname
= member
.filename
;
6923 fsize
= len(file_content
[member
.filename
].read());
6924 file_content
[member
.filename
].close();
6926 fuid
= int(os
.getuid());
6927 except AttributeError:
6932 fgid
= int(os
.getgid());
6933 except AttributeError:
6940 userinfo
= pwd
.getpwuid(os
.getuid());
6941 funame
= userinfo
.pw_name
;
6944 except AttributeError:
6952 groupinfo
= grp
.getgrgid(os
.getgid());
6953 fgname
= groupinfo
.gr_name
;
6956 except AttributeError:
6961 if(len(fuprint
)<=0):
6962 fuprint
= str(fuid
);
6964 if(len(fgprint
)<=0):
6965 fgprint
= str(fgid
);
6966 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(fsize
).rjust(15) + " " + member
.creationtime
.strftime('%Y-%m-%d %H:%M') + " " + printfname
));
6969 return listcatfiles
['catfp'];
6973 def InFileListFiles(infile
, verbose
=False, formatspecs
=__file_format_list__
, returnfp
=False):
6974 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6975 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
6976 if(checkcompressfile
=="tarfile"):
6977 return TarFileListFiles(infile
, verbose
, returnfp
);
6978 elif(checkcompressfile
=="zipfile"):
6979 return ZipFileListFiles(infile
, verbose
, returnfp
);
6980 elif(checkcompressfile
=="catfile"):
6981 return ArchiveFileListFiles(infile
, 0, 0, False, formatspecs
, verbose
, returnfp
);
6982 elif(rarfile_support
and checkcompressfile
=="rarfile"):
6983 return RarFileListFiles(infile
, verbose
, returnfp
);
6984 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
6985 return SevenZipFileListFiles(infile
, verbose
, returnfp
);
6990 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):
6991 outarray
= BytesIO();
6992 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, False, True);
6993 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6994 return listcatfiles
;
6996 def ListDirListFilesAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=False, seekstart
=0, seekend
=0, skipchecksum
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6997 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, formatspecs
, verbose
);
6998 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6999 return listcatfiles
;
7001 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):
7002 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, False, checksumtype
, extradata
, formatspecs
, False);
7003 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, followlink
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
7004 return listcatfiles
;
7006 create_alias_function("Pack", __file_format_name__
, "FromListDirAlt", PackArchiveFileFromListDirAlt
);
7008 def PackArchiveFileFromTarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7009 outarray
= TarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
7010 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
7011 return listcatfiles
;
7013 create_alias_function("Pack", __file_format_name__
, "FromTarFileAlt", PackArchiveFileFromTarFileAlt
);
7015 def PackArchiveFileFromZipFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7016 outarray
= ZipFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
7017 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
7018 return listcatfiles
;
7020 create_alias_function("Pack", __file_format_name__
, "FromZipFileAlt", PackArchiveFileFromZipFileAlt
);
7022 if(not rarfile_support
):
7023 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7026 if(rarfile_support
):
7027 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7028 outarray
= RarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
7029 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
7030 return listcatfiles
;
7032 create_alias_function("Pack", __file_format_name__
, "FromRarFileAlt", PackArchiveFileFromRarFileAlt
);
7034 if(not py7zr_support
):
7035 def PackArchiveFileFromSevenZipFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7039 def PackArchiveFileFromSevenZipFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7040 outarray
= SevenZipFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
7041 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
7042 return listcatfiles
;
7044 create_alias_function("Pack", __file_format_name__
, "FromSevenZipFileAlt", PackArchiveFileFromSevenZipFileAlt
);
7046 def download_file_from_ftp_file(url
):
7047 urlparts
= urlparse(url
);
7048 file_name
= os
.path
.basename(urlparts
.path
);
7049 file_dir
= os
.path
.dirname(urlparts
.path
);
7050 if(urlparts
.username
is not None):
7051 ftp_username
= urlparts
.username
;
7053 ftp_username
= "anonymous";
7054 if(urlparts
.password
is not None):
7055 ftp_password
= urlparts
.password
;
7056 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7057 ftp_password
= "anonymous";
7060 if(urlparts
.scheme
=="ftp"):
7062 elif(urlparts
.scheme
=="ftps"):
7066 if(urlparts
.scheme
=="sftp"):
7068 return download_file_from_pysftp_file(url
);
7070 return download_file_from_sftp_file(url
);
7071 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7072 return download_file_from_http_file(url
);
7073 ftp_port
= urlparts
.port
;
7074 if(urlparts
.port
is None):
7077 ftp
.connect(urlparts
.hostname
, ftp_port
);
7078 except socket
.gaierror
:
7079 log
.info("Error With URL "+url
);
7081 except socket
.timeout
:
7082 log
.info("Error With URL "+url
);
7084 ftp
.login(urlparts
.username
, urlparts
.password
);
7085 if(urlparts
.scheme
=="ftps"):
7087 ftpfile
= BytesIO();
7088 ftp
.retrbinary("RETR "+urlparts
.path
, ftpfile
.write
);
7089 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
7094 def download_file_from_ftp_string(url
):
7095 ftpfile
= download_file_from_ftp_file(url
);
7096 return ftpfile
.read();
7098 def upload_file_to_ftp_file(ftpfile
, url
):
7099 urlparts
= urlparse(url
);
7100 file_name
= os
.path
.basename(urlparts
.path
);
7101 file_dir
= os
.path
.dirname(urlparts
.path
);
7102 if(urlparts
.username
is not None):
7103 ftp_username
= urlparts
.username
;
7105 ftp_username
= "anonymous";
7106 if(urlparts
.password
is not None):
7107 ftp_password
= urlparts
.password
;
7108 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7109 ftp_password
= "anonymous";
7112 if(urlparts
.scheme
=="ftp"):
7114 elif(urlparts
.scheme
=="ftps"):
7118 if(urlparts
.scheme
=="sftp"):
7120 return upload_file_to_pysftp_file(url
);
7122 return upload_file_to_sftp_file(url
);
7123 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7125 ftp_port
= urlparts
.port
;
7126 if(urlparts
.port
is None):
7129 ftp
.connect(urlparts
.hostname
, ftp_port
);
7130 except socket
.gaierror
:
7131 log
.info("Error With URL "+url
);
7133 except socket
.timeout
:
7134 log
.info("Error With URL "+url
);
7136 ftp
.login(urlparts
.username
, urlparts
.password
);
7137 if(urlparts
.scheme
=="ftps"):
7139 ftp
.storbinary("STOR "+urlparts
.path
, ftpfile
);
7144 def upload_file_to_ftp_string(ftpstring
, url
):
7145 ftpfileo
= BytesIO(ftpstring
);
7146 ftpfile
= upload_file_to_ftp_file(ftpfileo
, url
);
7150 def download_file_from_http_file(url
, headers
=geturls_headers_pycatfile_python_alt
):
7151 # Parse the URL to extract username and password if present
7152 urlparts
= urlparse(url
);
7153 username
= urlparts
.username
;
7154 password
= urlparts
.password
;
7155 # Rebuild the URL without the username and password
7156 netloc
= urlparts
.hostname
;
7157 if(urlparts
.scheme
=="sftp"):
7159 return download_file_from_pysftp_file(url
);
7161 return download_file_from_sftp_file(url
);
7162 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7163 return download_file_from_ftp_file(url
);
7165 netloc
+= ':' + str(urlparts
.port
);
7166 rebuilt_url
= urlunparse((urlparts
.scheme
, netloc
, urlparts
.path
, urlparts
.params
, urlparts
.query
, urlparts
.fragment
));
7167 # Create a temporary file object
7168 httpfile
= BytesIO();
7170 # Use the requests library if available
7171 if username
and password
:
7172 response
= requests
.get(rebuilt_url
, headers
=headers
, auth
=(username
, password
), stream
=True);
7174 response
= requests
.get(rebuilt_url
, headers
=headers
, stream
=True);
7175 response
.raw
.decode_content
= True
7176 shutil
.copyfileobj(response
.raw
, httpfile
);
7178 # Build a Request object for urllib
7179 request
= Request(rebuilt_url
, headers
=headers
);
7180 # Create an opener object for handling URLs
7181 if username
and password
:
7182 # Create a password manager
7183 password_mgr
= HTTPPasswordMgrWithDefaultRealm();
7184 # Add the username and password
7185 password_mgr
.add_password(None, rebuilt_url
, username
, password
);
7186 # Create an authentication handler using the password manager
7187 auth_handler
= HTTPBasicAuthHandler(password_mgr
);
7188 # Build the opener with the authentication handler
7189 opener
= build_opener(auth_handler
);
7191 opener
= build_opener();
7192 with opener
.open(request
) as response
:
7193 shutil
.copyfileobj(response
, httpfile
);
7194 # Reset file pointer to the start
7195 httpfile
.seek(0, 0);
7196 # Return the temporary file object
7199 def download_file_from_http_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
7200 httpfile
= download_file_from_http_file(url
, headers
);
7201 return ftpfile
.read();
7204 def download_file_from_sftp_file(url
):
7205 urlparts
= urlparse(url
);
7206 file_name
= os
.path
.basename(urlparts
.path
);
7207 file_dir
= os
.path
.dirname(urlparts
.path
);
7208 sftp_port
= urlparts
.port
;
7209 if(urlparts
.port
is None):
7212 sftp_port
= urlparts
.port
;
7213 if(urlparts
.username
is not None):
7214 sftp_username
= urlparts
.username
;
7216 sftp_username
= "anonymous";
7217 if(urlparts
.password
is not None):
7218 sftp_password
= urlparts
.password
;
7219 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7220 sftp_password
= "anonymous";
7223 if(urlparts
.scheme
=="ftp"):
7224 return download_file_from_ftp_file(url
);
7225 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7226 return download_file_from_http_file(url
);
7227 if(urlparts
.scheme
!="sftp"):
7229 ssh
= paramiko
.SSHClient();
7230 ssh
.load_system_host_keys();
7231 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
7233 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7234 except paramiko
.ssh_exception
.SSHException
:
7236 except socket
.gaierror
:
7237 log
.info("Error With URL "+url
);
7239 except socket
.timeout
:
7240 log
.info("Error With URL "+url
);
7242 sftp
= ssh
.open_sftp();
7243 sftpfile
= BytesIO();
7244 sftp
.getfo(urlparts
.path
, sftpfile
);
7247 sftpfile
.seek(0, 0);
7250 def download_file_from_sftp_file(url
):
7254 def download_file_from_sftp_string(url
):
7255 sftpfile
= download_file_from_sftp_file(url
);
7256 return sftpfile
.read();
7258 def download_file_from_ftp_string(url
):
7262 def upload_file_to_sftp_file(sftpfile
, url
):
7263 urlparts
= urlparse(url
);
7264 file_name
= os
.path
.basename(urlparts
.path
);
7265 file_dir
= os
.path
.dirname(urlparts
.path
);
7266 sftp_port
= urlparts
.port
;
7267 if(urlparts
.port
is None):
7270 sftp_port
= urlparts
.port
;
7271 if(urlparts
.username
is not None):
7272 sftp_username
= urlparts
.username
;
7274 sftp_username
= "anonymous";
7275 if(urlparts
.password
is not None):
7276 sftp_password
= urlparts
.password
;
7277 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7278 sftp_password
= "anonymous";
7281 if(urlparts
.scheme
=="ftp"):
7282 return upload_file_to_ftp_file(url
);
7283 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7285 if(urlparts
.scheme
!="sftp"):
7287 ssh
= paramiko
.SSHClient();
7288 ssh
.load_system_host_keys();
7289 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
7291 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7292 except paramiko
.ssh_exception
.SSHException
:
7294 except socket
.gaierror
:
7295 log
.info("Error With URL "+url
);
7297 except socket
.timeout
:
7298 log
.info("Error With URL "+url
);
7300 sftp
= ssh
.open_sftp();
7301 sftp
.putfo(sftpfile
, urlparts
.path
);
7304 sftpfile
.seek(0, 0);
7307 def upload_file_to_sftp_file(sftpfile
, url
):
7311 def upload_file_to_sftp_string(sftpstring
, url
):
7312 sftpfileo
= BytesIO(sftpstring
);
7313 sftpfile
= upload_file_to_sftp_files(ftpfileo
, url
);
7317 def upload_file_to_sftp_string(url
):
7321 def download_file_from_pysftp_file(url
):
7322 urlparts
= urlparse(url
);
7323 file_name
= os
.path
.basename(urlparts
.path
);
7324 file_dir
= os
.path
.dirname(urlparts
.path
);
7325 sftp_port
= urlparts
.port
;
7326 if(urlparts
.port
is None):
7329 sftp_port
= urlparts
.port
;
7330 if(urlparts
.username
is not None):
7331 sftp_username
= urlparts
.username
;
7333 sftp_username
= "anonymous";
7334 if(urlparts
.password
is not None):
7335 sftp_password
= urlparts
.password
;
7336 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7337 sftp_password
= "anonymous";
7340 if(urlparts
.scheme
=="ftp"):
7341 return download_file_from_ftp_file(url
);
7342 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7343 return download_file_from_http_file(url
);
7344 if(urlparts
.scheme
!="sftp"):
7347 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7348 except paramiko
.ssh_exception
.SSHException
:
7350 except socket
.gaierror
:
7351 log
.info("Error With URL "+url
);
7353 except socket
.timeout
:
7354 log
.info("Error With URL "+url
);
7356 sftp
= ssh
.open_sftp();
7357 sftpfile
= BytesIO();
7358 sftp
.getfo(urlparts
.path
, sftpfile
);
7361 sftpfile
.seek(0, 0);
7364 def download_file_from_pysftp_file(url
):
7368 def download_file_from_pysftp_string(url
):
7369 sftpfile
= download_file_from_pysftp_file(url
);
7370 return sftpfile
.read();
7372 def download_file_from_ftp_string(url
):
7376 def upload_file_to_pysftp_file(sftpfile
, url
):
7377 urlparts
= urlparse(url
);
7378 file_name
= os
.path
.basename(urlparts
.path
);
7379 file_dir
= os
.path
.dirname(urlparts
.path
);
7380 sftp_port
= urlparts
.port
;
7381 if(urlparts
.port
is None):
7384 sftp_port
= urlparts
.port
;
7385 if(urlparts
.username
is not None):
7386 sftp_username
= urlparts
.username
;
7388 sftp_username
= "anonymous";
7389 if(urlparts
.password
is not None):
7390 sftp_password
= urlparts
.password
;
7391 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7392 sftp_password
= "anonymous";
7395 if(urlparts
.scheme
=="ftp"):
7396 return upload_file_to_ftp_file(url
);
7397 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7399 if(urlparts
.scheme
!="sftp"):
7402 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7403 except paramiko
.ssh_exception
.SSHException
:
7405 except socket
.gaierror
:
7406 log
.info("Error With URL "+url
);
7408 except socket
.timeout
:
7409 log
.info("Error With URL "+url
);
7411 sftp
= ssh
.open_sftp();
7412 sftp
.putfo(sftpfile
, urlparts
.path
);
7415 sftpfile
.seek(0, 0);
7418 def upload_file_to_pysftp_file(sftpfile
, url
):
7422 def upload_file_to_pysftp_string(sftpstring
, url
):
7423 sftpfileo
= BytesIO(sftpstring
);
7424 sftpfile
= upload_file_to_pysftp_files(ftpfileo
, url
);
7428 def upload_file_to_pysftp_string(url
):
7431 def download_file_from_internet_file(url
, headers
=geturls_headers_pycatfile_python_alt
):
7432 urlparts
= urlparse(url
);
7433 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7434 return download_file_from_http_file(url
, headers
);
7435 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7436 return download_file_from_ftp_file(url
);
7437 elif(urlparts
.scheme
=="sftp"):
7438 if(__use_pysftp__
and havepysftp
):
7439 return download_file_from_pysftp_file(url
);
7441 return download_file_from_sftp_file(url
);
7446 def download_file_from_internet_uncompress_file(url
, headers
=geturls_headers_pycatfile_python_alt
, formatspecs
=__file_format_list__
):
7447 fp
= download_file_from_internet_file(url
);
7448 fp
= UncompressArchiveFile(fp
, formatspecs
);
7454 def download_file_from_internet_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
7455 urlparts
= urlparse(url
);
7456 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7457 return download_file_from_http_string(url
, headers
);
7458 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7459 return download_file_from_ftp_string(url
);
7460 elif(urlparts
.scheme
=="sftp"):
7461 if(__use_pysftp__
and havepysftp
):
7462 return download_file_from_pysftp_string(url
);
7464 return download_file_from_sftp_string(url
);
7469 def download_file_from_internet_uncompress_string(url
, headers
=geturls_headers_pycatfile_python_alt
, formatspecs
=__file_format_list__
):
7470 fp
= download_file_from_internet_string(url
);
7471 fp
= UncompressArchiveFile(fp
, formatspecs
);
7477 def upload_file_to_internet_file(ifp
, url
):
7478 urlparts
= urlparse(url
);
7479 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7481 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7482 return upload_file_to_ftp_file(ifp
, url
);
7483 elif(urlparts
.scheme
=="sftp"):
7484 if(__use_pysftp__
and havepysftp
):
7485 return upload_file_to_pysftp_file(ifp
, url
);
7487 return upload_file_to_sftp_file(ifp
, url
);
7492 def upload_file_to_internet_compress_file(ifp
, url
, formatspecs
=__file_format_list__
):
7493 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
7497 upload_file_to_internet_file(catfp
, outfile
);
7500 def upload_file_to_internet_string(ifp
, url
):
7501 urlparts
= urlparse(url
);
7502 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7504 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7505 return upload_file_to_ftp_string(ifp
, url
);
7506 elif(urlparts
.scheme
=="sftp"):
7507 if(__use_pysftp__
and havepysftp
):
7508 return upload_file_to_pysftp_string(ifp
, url
);
7510 return upload_file_to_sftp_string(ifp
, url
);
7515 def upload_file_to_internet_compress_string(ifp
, url
, formatspecs
=__file_format_list__
):
7516 catfp
= CompressArchiveFile(BytesIO(ifp
), compression
, compressionlevel
, formatspecs
);
7520 upload_file_to_internet_file(catfp
, outfile
);
7524 if(hasattr(shutil
, "register_archive_format")):
7525 # Register the packing format
7526 shutil
.register_archive_format(__file_format_name__
, PackArchiveFileFunc
, description
='Pack concatenated files');
7527 except shutil
.RegistryError
:
7531 if(hasattr(shutil
, "register_unpack_format")):
7532 # Register the unpacking format
7533 shutil
.register_unpack_format(__file_format_name__
, archivefile_extensions
, UnPackArchiveFileFunc
, description
='UnPack concatenated files');
7534 except shutil
.RegistryError
: