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 compressionlistalt
= ['gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzma', 'xz'];
214 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
215 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
217 tarfile_mimetype
= "application/tar";
218 tarfile_tar_mimetype
= tarfile_mimetype
;
219 zipfile_mimetype
= "application/zip";
220 zipfile_zip_mimetype
= zipfile_mimetype
;
221 rarfile_mimetype
= "application/rar";
222 rarfile_rar_mimetype
= rarfile_mimetype
;
223 archivefile_mimetype
= "application/x-"+__file_format_list__
[1]+"";
224 archivefile_cat_mimetype
= archivefile_mimetype
;
225 archivefile_gzip_mimetype
= "application/x-"+__file_format_list__
[1]+"+gzip";
226 archivefile_gz_mimetype
= archivefile_gzip_mimetype
;
227 archivefile_bzip2_mimetype
= "application/x-"+__file_format_list__
[1]+"+bzip2";
228 archivefile_bz2_mimetype
= archivefile_bzip2_mimetype
;
229 archivefile_lz4_mimetype
= "application/x-"+__file_format_list__
[1]+"+lz4";
230 archivefile_lzop_mimetype
= "application/x-"+__file_format_list__
[1]+"+lzop";
231 archivefile_lzo_mimetype
= archivefile_lzop_mimetype
;
232 archivefile_zstandard_mimetype
= "application/x-"+__file_format_list__
[1]+"+zstandard";
233 archivefile_zstd_mimetype
= archivefile_zstandard_mimetype
;
234 archivefile_lzma_mimetype
= "application/x-"+__file_format_list__
[1]+"+lzma";
235 archivefile_xz_mimetype
= "application/x-"+__file_format_list__
[1]+"+xz";
236 archivefile_extensions
= ['.cat', '.cat.gz', '.cat.bz2', '.cat.zst', '.cat.lz4', '.cat.lzo', '.cat.lzop', '.cat.lzma', '.cat.xz'];
238 if __name__
== "__main__":
240 curscrpath
= os
.path
.dirname(sys
.argv
[0]);
244 curscrpath
= curscrpath
.replace(os
.sep
, "/");
245 curscrpath
= curscrpath
+ "/";
246 scrfile
= curscrpath
+ "catfile.py";
247 if(os
.path
.exists(scrfile
) and os
.path
.isfile(scrfile
)):
248 scrcmd
= subprocess
.Popen([sys
.executable
, scrfile
] + sys
.argv
[1:]);
251 def VerbosePrintOut(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
257 "warning": logging
.warning
,
258 "error": logging
.error
,
259 "critical": logging
.critical
,
260 "exception": logging
.exception
,
261 "logalt": lambda x
: logging
.log(dgblevel
, x
),
262 "debug": logging
.debug
264 log_function
= log_functions
.get(outtype
);
266 log_function(dbgtxt
);
270 def VerbosePrintOutReturn(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
271 VerbosePrintOut(dbgtxt
, outtype
, dbgenable
, dgblevel
);
274 def RemoveWindowsPath(dpath
):
278 dpath
= dpath
.replace(os
.path
.sep
, "/");
279 dpath
= dpath
.rstrip("/");
280 if(dpath
=="." or dpath
==".."):
284 def NormalizeRelativePath(inpath
):
285 inpath
= RemoveWindowsPath(inpath
);
286 if(os
.path
.isabs(inpath
)):
289 if(inpath
.startswith("./") or inpath
.startswith("../")):
292 outpath
= "./" + inpath
;
295 def PrependPath(base_dir
, child_path
):
296 # Check if base_dir is None or empty, if so, return child_path as is
299 # Ensure base_dir ends with exactly one slash
300 if not base_dir
.endswith('/'):
302 # Check if child_path starts with ./ or ../ (indicating a relative path)
303 if child_path
.startswith('./') or child_path
.startswith('../'):
304 # For relative paths, we don't alter the child_path
305 return base_dir
+ child_path
;
307 # For non-relative paths, ensure there's no starting slash on child_path to avoid double slashes
308 return base_dir
+ child_path
.lstrip('/');
310 def ListDir(dirpath
, followlink
=False, duplicates
=False):
311 if(isinstance(dirpath
, (list, tuple, ))):
312 dirpath
= list(filter(None, dirpath
));
313 elif(isinstance(dirpath
, (str, ))):
314 dirpath
= list(filter(None, [dirpath
]));
316 for mydirfile
in dirpath
:
317 if(not os
.path
.exists(mydirfile
)):
319 mydirfile
= NormalizeRelativePath(mydirfile
);
320 if(os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
)):
321 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
));
322 if(os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
)):
323 for root
, dirs
, filenames
in os
.walk(mydirfile
):
325 dpath
= RemoveWindowsPath(dpath
);
326 if(dpath
not in retlist
and not duplicates
):
327 retlist
.append(dpath
);
329 retlist
.append(dpath
);
330 for file in filenames
:
331 fpath
= os
.path
.join(root
, file);
332 fpath
= RemoveWindowsPath(fpath
);
333 if(fpath
not in retlist
and not duplicates
):
334 retlist
.append(fpath
);
336 retlist
.append(fpath
);
338 retlist
.append(RemoveWindowsPath(mydirfile
));
341 def ListDirAdvanced(dirpath
, followlink
=False, duplicates
=False):
342 if isinstance(dirpath
, (list, tuple)):
343 dirpath
= list(filter(None, dirpath
));
344 elif isinstance(dirpath
, str):
345 dirpath
= list(filter(None, [dirpath
]));
347 for mydirfile
in dirpath
:
348 if not os
.path
.exists(mydirfile
):
350 mydirfile
= NormalizeRelativePath(mydirfile
);
351 if os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
) and followlink
:
352 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
))
353 if os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
):
354 for root
, dirs
, filenames
in os
.walk(mydirfile
):
355 # Sort dirs and filenames alphabetically in place
356 dirs
.sort(key
=lambda x
: x
.lower());
357 filenames
.sort(key
=lambda x
: x
.lower());
358 dpath
= RemoveWindowsPath(root
);
359 if not duplicates
and dpath
not in retlist
:
360 retlist
.append(dpath
);
362 retlist
.append(dpath
);
363 for file in filenames
:
364 fpath
= os
.path
.join(root
, file);
365 fpath
= RemoveWindowsPath(fpath
);
366 if not duplicates
and fpath
not in retlist
:
367 retlist
.append(fpath
);
369 retlist
.append(fpath
);
371 retlist
.append(RemoveWindowsPath(mydirfile
));
374 def create_alias_function(prefix
, base_name
, suffix
, target_function
):
375 # Define a new function that wraps the target function
376 def alias_function(*args
, **kwargs
):
377 return target_function(*args
, **kwargs
);
379 # Create the function name by combining the prefix, base name, and the suffix
380 function_name
= "{}{}{}".format(prefix
, base_name
, suffix
);
382 # Add the new function to the global namespace
383 globals()[function_name
] = alias_function
;
385 def create_alias_function_alt(prefix
, base_name
, suffix
, target_function
):
386 # Create the function name by combining the prefix, base name, and the suffix
387 # Use the format method for string formatting, compatible with Python 2 and 3
388 function_name
= "{}{}{}".format(prefix
, base_name
, suffix
)
389 # Add the new function (alias of the target_function) to the global namespace
390 # This line is compatible as-is with both Python 2 and 3
391 globals()[function_name
] = target_function
393 # initial_value can be 0xFFFF or 0x0000
394 def crc16_ansi(msg
, initial_value
=0xFFFF):
395 # CRC-16-IBM / CRC-16-ANSI polynomial and initial value
396 poly
= 0x8005; # Polynomial for CRC-16-IBM / CRC-16-ANSI
397 crc
= initial_value
; # Initial value
399 crc ^
= b
<< 8; # XOR byte into CRC top byte
400 for _
in range(8): # Process each bit
401 if crc
& 0x8000: # If the top bit is set
402 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
404 crc
= crc
<< 1; # Just shift left
405 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
408 # initial_value can be 0xFFFF or 0x0000
409 def crc16_ibm(msg
, initial_value
=0xFFFF):
410 return crc16_ansi(msg
, initial_value
);
412 # initial_value is 0xFFFF
414 return crc16_ansi(msg
, 0xFFFF);
416 # initial_value can be 0xFFFF, 0x1D0F or 0x0000
417 def crc16_ccitt(msg
, initial_value
=0xFFFF):
418 # CRC-16-CCITT polynomial
419 poly
= 0x1021; # Polynomial for CRC-16-CCITT
420 # Use the specified initial value
423 crc ^
= b
<< 8; # XOR byte into CRC top byte
424 for _
in range(8): # Process each bit
425 if crc
& 0x8000: # If the top bit is set
426 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
428 crc
= crc
<< 1; # Just shift left
429 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
432 # initial_value can be 0x42F0E1EBA9EA3693 or 0x0000000000000000
433 def crc64_ecma(msg
, initial_value
=0x0000000000000000):
434 # CRC-64-ECMA polynomial and initial value
435 poly
= 0x42F0E1EBA9EA3693;
436 crc
= initial_value
; # Initial value for CRC-64-ECMA
438 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
439 for _
in range(8): # Process each bit
440 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
441 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
443 crc
<<= 1; # Just shift left if the MSB is 0
444 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
447 # initial_value can be 0x000000000000001B or 0xFFFFFFFFFFFFFFFF
448 def crc64_iso(msg
, initial_value
=0xFFFFFFFFFFFFFFFF):
449 # CRC-64-ISO polynomial and initial value
450 poly
= 0x000000000000001B;
451 crc
= initial_value
; # Common initial value for CRC-64-ISO
453 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
454 for _
in range(8): # Process each bit
455 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
456 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
458 crc
<<= 1; # Just shift left if the MSB is 0
459 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
462 def GetDataFromArray(data
, path
, default
=None):
466 element
= element
[key
];
468 except (KeyError, TypeError, IndexError):
471 def GetDataFromArrayAlt(structure
, path
, default
=None):
474 if isinstance(element
, dict) and key
in element
:
475 element
= element
[key
];
476 elif isinstance(element
, list) and isinstance(key
, int) and -len(element
) <= key
< len(element
):
477 element
= element
[key
];
482 def GetHeaderChecksum(inlist
=[], checksumtype
="crc32", encodedata
=True, formatspecs
=__file_format_list__
):
483 if isinstance(inlist
, list):
484 fileheader
= AppendNullBytes(inlist
, formatspecs
[5]);
486 fileheader
= AppendNullByte(inlist
, formatspecs
[5]);
488 fileheader
= fileheader
.encode('UTF-8');
489 if(checksumtype
=="none" or checksumtype
==""):
490 catfileheadercshex
= format(0, 'x').lower();
491 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
492 catfileheadercshex
= format(crc16(fileheader
) & 0xffff, '04x').lower();
493 elif(checksumtype
=="crc16_ccitt"):
494 catfileheadercshex
= format(crc16_ccitt(fileheader
) & 0xffff, '04x').lower();
495 elif(checksumtype
=="adler32"):
496 catfileheadercshex
= format(zlib
.adler32(fileheader
) & 0xffffffff, '08x').lower();
497 elif(checksumtype
=="crc32"):
498 catfileheadercshex
= format(crc32(fileheader
) & 0xffffffff, '08x').lower();
499 elif(checksumtype
=="crc64_ecma"):
500 catfileheadercshex
= format(crc64_ecma(fileheader
) & 0xffffffffffffffff, '016x').lower();
501 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
502 catfileheadercshex
= format(crc64_iso(fileheader
) & 0xffffffffffffffff, '016x').lower();
503 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
504 checksumoutstr
= hashlib
.new(checksumtype
);
505 checksumoutstr
.update(fileheader
);
506 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
508 catfileheadercshex
= format(0, 'x').lower();
509 return catfileheadercshex
;
511 def GetFileChecksum(instr
, checksumtype
="crc32", encodedata
=True, formatspecs
=__file_format_list__
):
513 instr
= instr
.encode('UTF-8');
514 if(checksumtype
=="none" or checksumtype
==""):
515 catinstrcshex
= format(0, 'x').lower();
516 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
517 catinstrcshex
= format(crc16(instr
) & 0xffff, '04x').lower();
518 elif(checksumtype
=="crc16_ccitt"):
519 catinstrcshex
= format(crc16_ccitt(instr
) & 0xffff, '04x').lower();
520 elif(checksumtype
=="adler32"):
521 catinstrcshex
= format(zlib
.adler32(instr
) & 0xffffffff, '08x').lower();
522 elif(checksumtype
=="crc32"):
523 catinstrcshex
= format(crc32(instr
) & 0xffffffff, '08x').lower();
524 elif(checksumtype
=="crc64_ecma"):
525 catinstrcshex
= format(crc64_ecma(instr
) & 0xffffffffffffffff, '016x').lower();
526 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
527 catinstrcshex
= format(crc64_iso(instr
) & 0xffffffffffffffff, '016x').lower();
528 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
529 checksumoutstr
= hashlib
.new(checksumtype
);
530 checksumoutstr
.update(instr
);
531 catinstrcshex
= checksumoutstr
.hexdigest().lower();
533 catinstrcshex
= format(0, 'x').lower();
534 return catinstrcshex
;
536 def ValidateHeaderChecksum(inlist
=[], checksumtype
="crc32", inchecksum
="0", formatspecs
=__file_format_list__
):
537 catfileheadercshex
= GetHeaderChecksum(inlist
, checksumtype
, True, formatspecs
);
538 inchecksum
= inchecksum
.lower();
539 catfileheadercshex
= catfileheadercshex
.lower();
540 if(inchecksum
==catfileheadercshex
):
545 def ValidateFileChecksum(infile
, checksumtype
="crc32", inchecksum
="0", formatspecs
=__file_format_list__
):
546 catinfilecshex
= GetFileChecksum(infile
, checksumtype
, True, formatspecs
);
547 inchecksum
= inchecksum
.lower();
548 catinfilecshex
= catinfilecshex
.lower();
549 if(inchecksum
==catinfilecshex
):
554 def ReadTillNullByte(fp
, delimiter
=__file_format_delimiter__
):
557 nullbyte
= delimiter
.encode("UTF-8");
559 curbyte
= fp
.read(1);
560 if(curbyte
==nullbyte
or not curbyte
):
562 curfullbyte
= curfullbyte
+ curbyte
;
563 return curfullbyte
.decode('UTF-8');
565 def ReadUntilNullByte(fp
, delimiter
=__file_format_delimiter__
):
566 return ReadTillNullByte(fp
, delimiter
);
568 def SeekToEndOfFile(fp
):
572 if(lasttell
==fp
.tell()):
574 lasttell
= fp
.tell();
577 def ReadFileHeaderData(fp
, rounds
=0, delimiter
=__file_format_delimiter__
):
581 while(rocount
<roend
):
582 HeaderOut
.append(ReadTillNullByte(fp
, delimiter
));
583 rocount
= rocount
+ 1;
586 def ReadFileHeaderDataBySize(fp
, delimiter
=__file_format_delimiter__
):
587 headerpresize
= ReadTillNullByte(fp
, delimiter
);
588 headersize
= int(headerpresize
, 16);
591 headercontent
= str(fp
.read(headersize
).decode('UTF-8')).split(delimiter
);
594 roend
= int(len(headercontent
));
595 HeaderOut
= [headerpresize
];
596 while(rocount
<roend
):
597 HeaderOut
.append(headercontent
[rocount
]);
598 rocount
= rocount
+ 1;
601 def ReadFileHeaderDataWoSize(fp
, delimiter
=__file_format_delimiter__
):
602 preheaderdata
= ReadFileHeaderData(fp
, 2, delimiter
);
603 headersize
= int(preheaderdata
[0], 16);
604 headernumfields
= int(preheaderdata
[1], 16);
605 if(headersize
<=0 or headernumfields
<=0):
607 headerdata
= ReadFileHeaderData(fp
, headernumfields
, delimiter
);
608 HeaderOut
= preheaderdata
+ headerdata
;
611 def ReadFileHeaderDataBySizeWithContent(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
612 delimiter
= formatspecs
[5];
613 fheaderstart
= fp
.tell();
614 HeaderOut
= ReadFileHeaderDataBySize(fp
, delimiter
);
615 if(len(HeaderOut
)==0):
617 if(re
.findall("^[.|/]", HeaderOut
[3])):
618 fname
= HeaderOut
[3];
620 fname
= "./"+HeaderOut
[3];
621 fchecksumtype
= HeaderOut
[-3].lower();
622 fcs
= HeaderOut
[-2].lower();
623 fccs
= HeaderOut
[-1].lower();
624 fsize
= int(HeaderOut
[5], 16);
625 fcompression
= HeaderOut
[12];
626 fcsize
= int(HeaderOut
[13], 16);
627 newfcs
= GetHeaderChecksum(HeaderOut
[:-2], HeaderOut
[-3].lower(), True, formatspecs
);
628 if(fcs
!=newfcs
and not skipchecksum
):
629 VerbosePrintOut("File Header Checksum Error with file " + fname
+ " at offset " + str(fheaderstart
));
630 VerbosePrintOut("'" + str(fcs
) + "' != " + "'" + str(newfcs
) + "'");
632 fhend
= fp
.tell() - 1;
633 fcontentstart
= fp
.tell();
634 fcontents
= BytesIO();
635 if(fsize
>0 and not listonly
):
636 if(fcompression
=="none" or fcompression
=="" or fcompression
=="auto"):
637 fcontents
.write(fp
.read(fsize
));
639 fcontents
.write(fp
.read(fcsize
));
640 elif(fsize
>0 and listonly
):
641 if(fcompression
=="none" or fcompression
=="" or fcompression
=="auto"):
645 fcontents
.seek(0, 0);
646 newfccs
= GetFileChecksum(fcontents
.read(), HeaderOut
[-3].lower(), False, formatspecs
);
647 if(fccs
!=newfccs
and not skipchecksum
and not listonly
):
648 VerbosePrintOut("File Content Checksum Error with file " + fname
+ " at offset " + str(fcontentstart
));
649 VerbosePrintOut("'" + str(fccs
) + "' != " + "'" + str(newfccs
) + "'");
651 if(fcompression
=="none" or fcompression
=="" or fcompression
=="auto"):
654 fcontents
.seek(0, 0);
655 fcontents
= UncompressArchiveFile(fcontents
, formatspecs
);
657 fcontentend
= fp
.tell() - 1;
658 HeaderOut
.append(fcontents
);
661 def ReadFileHeaderDataBySizeWithContentToArray(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
662 delimiter
= formatspecs
[5];
663 fheaderstart
= fp
.tell();
665 HeaderOut
= ReadFileHeaderDataBySize(fp
, delimiter
);
667 HeaderOut
= ReadFileHeaderDataWoSize(fp
, delimiter
);
668 if(len(HeaderOut
)==0):
670 fheadsize
= int(HeaderOut
[0], 16);
671 fnumfields
= int(HeaderOut
[1], 16);
672 ftype
= int(HeaderOut
[2], 16);
673 if(re
.findall("^[.|/]", HeaderOut
[3])):
674 fname
= HeaderOut
[3];
676 fname
= "./"+HeaderOut
[3];
677 fbasedir
= os
.path
.dirname(fname
);
678 flinkname
= HeaderOut
[4];
679 fsize
= int(HeaderOut
[5], 16);
680 fatime
= int(HeaderOut
[6], 16);
681 fmtime
= int(HeaderOut
[7], 16);
682 fctime
= int(HeaderOut
[8], 16);
683 fbtime
= int(HeaderOut
[9], 16);
684 fmode
= int(HeaderOut
[10], 16);
685 fchmode
= stat
.S_IMODE(fmode
);
686 ftypemod
= stat
.S_IFMT(fmode
);
687 fwinattributes
= int(HeaderOut
[11], 16);
688 fcompression
= HeaderOut
[12];
689 fcsize
= int(HeaderOut
[13], 16);
690 fuid
= int(HeaderOut
[14], 16);
691 funame
= HeaderOut
[15];
692 fgid
= int(HeaderOut
[16], 16);
693 fgname
= HeaderOut
[17];
694 fid
= int(HeaderOut
[18], 16);
695 finode
= int(HeaderOut
[19], 16);
696 flinkcount
= int(HeaderOut
[20], 16);
697 fdev_minor
= int(HeaderOut
[21], 16);
698 fdev_major
= int(HeaderOut
[22], 16);
699 frdev_minor
= int(HeaderOut
[23], 16);
700 frdev_major
= int(HeaderOut
[24], 16);
701 fextrasize
= int(HeaderOut
[25], 16);
702 fextrafields
= int(HeaderOut
[26], 16);
703 extrafieldslist
= [];
705 extraend
= extrastart
+ fextrafields
;
706 extrafieldslist
= [];
707 if(extrastart
<extraend
):
708 extrafieldslist
.append(HeaderOut
[extrastart
]);
709 extrastart
= extrastart
+ 1;
710 fchecksumtype
= HeaderOut
[extrastart
].lower();
711 fcs
= HeaderOut
[extrastart
+ 1].lower();
712 fccs
= HeaderOut
[extrastart
+ 2].lower();
713 newfcs
= GetHeaderChecksum(HeaderOut
[:-2], HeaderOut
[-3].lower(), True, formatspecs
);
714 if(fcs
!=newfcs
and not skipchecksum
):
715 VerbosePrintOut("File Header Checksum Error with file " + fname
+ " at offset " + str(fheaderstart
));
716 VerbosePrintOut("'" + str(fcs
) + "' != " + "'" + str(newfcs
) + "'");
718 fhend
= fp
.tell() - 1;
719 fcontentstart
= fp
.tell();
720 fcontents
= BytesIO();
721 pyhascontents
= False;
722 if(fsize
>0 and not listonly
):
723 if(fcompression
=="none" or fcompression
=="" or fcompression
=="auto"):
724 fcontents
.write(fp
.read(fsize
));
726 fcontents
.write(fp
.read(fcsize
));
727 pyhascontents
= True;
728 elif(fsize
>0 and listonly
):
729 if(fcompression
=="none" or fcompression
=="" or fcompression
=="auto"):
733 pyhascontents
= False;
734 fcontents
.seek(0, 0);
735 newfccs
= GetFileChecksum(fcontents
.read(), HeaderOut
[-3].lower(), False, formatspecs
);
736 if(fccs
!=newfccs
and not skipchecksum
and not listonly
):
737 VerbosePrintOut("File Content Checksum Error with file " + fname
+ " at offset " + str(fcontentstart
));
738 VerbosePrintOut("'" + str(fccs
) + "' != " + "'" + str(newfccs
) + "'");
740 if(fcompression
=="none" or fcompression
=="" or fcompression
=="auto"):
743 fcontents
.seek(0, 0);
744 fcontents
= UncompressArchiveFile(fcontents
, formatspecs
);
746 fcontentend
= fp
.tell() - 1;
747 fcontents
.seek(0, 0);
748 catlist
= {'fheadersize': fheadsize
, 'fhstart': fheaderstart
, 'fhend': fhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fwinattributes': fwinattributes
, 'fcompression': fcompression
, 'fcsize': fcsize
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, '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
};
751 def ReadFileHeaderDataBySizeWithContentToList(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
752 delimiter
= formatspecs
[5];
753 fheaderstart
= fp
.tell();
755 HeaderOut
= ReadFileHeaderDataBySize(fp
, delimiter
);
757 HeaderOut
= ReadFileHeaderDataWoSize(fp
, delimiter
);
758 if(len(HeaderOut
)==0):
760 fheadsize
= int(HeaderOut
[0], 16);
761 fnumfields
= int(HeaderOut
[1], 16);
762 ftype
= int(HeaderOut
[2], 16);
763 if(re
.findall("^[.|/]", HeaderOut
[3])):
764 fname
= HeaderOut
[3];
766 fname
= "./"+HeaderOut
[3];
767 fbasedir
= os
.path
.dirname(fname
);
768 flinkname
= HeaderOut
[4];
769 fsize
= int(HeaderOut
[5], 16);
770 fatime
= int(HeaderOut
[6], 16);
771 fmtime
= int(HeaderOut
[7], 16);
772 fctime
= int(HeaderOut
[8], 16);
773 fbtime
= int(HeaderOut
[9], 16);
774 fmode
= int(HeaderOut
[10], 16);
775 fchmode
= stat
.S_IMODE(fmode
);
776 ftypemod
= stat
.S_IFMT(fmode
);
777 fwinattributes
= int(HeaderOut
[11], 16);
778 fcompression
= HeaderOut
[12];
779 fcsize
= int(HeaderOut
[13], 16);
780 fuid
= int(HeaderOut
[14], 16);
781 funame
= HeaderOut
[15];
782 fgid
= int(HeaderOut
[16], 16);
783 fgname
= HeaderOut
[17];
784 fid
= int(HeaderOut
[18], 16);
785 finode
= int(HeaderOut
[19], 16);
786 flinkcount
= int(HeaderOut
[20], 16);
787 fdev_minor
= int(HeaderOut
[21], 16);
788 fdev_major
= int(HeaderOut
[22], 16);
789 frdev_minor
= int(HeaderOut
[23], 16);
790 frdev_major
= int(HeaderOut
[24], 16);
791 fextrasize
= int(HeaderOut
[25], 16);
792 fextrafields
= int(HeaderOut
[26], 16);
793 extrafieldslist
= [];
795 extraend
= extrastart
+ fextrafields
;
796 extrafieldslist
= [];
797 if(extrastart
<extraend
):
798 extrafieldslist
.append(HeaderOut
[extrastart
]);
799 extrastart
= extrastart
+ 1;
800 fchecksumtype
= HeaderOut
[extrastart
].lower();
801 fcs
= HeaderOut
[extrastart
+ 1].lower();
802 fccs
= HeaderOut
[extrastart
+ 2].lower();
803 newfcs
= GetHeaderChecksum(HeaderOut
[:-2], HeaderOut
[-3].lower(), True, formatspecs
);
804 if(fcs
!=newfcs
and not skipchecksum
):
805 VerbosePrintOut("File Header Checksum Error with file " + fname
+ " at offset " + str(fheaderstart
));
806 VerbosePrintOut("'" + str(fcs
) + "' != " + "'" + str(newfcs
) + "'");
808 fhend
= fp
.tell() - 1;
809 fcontentstart
= fp
.tell();
810 fcontents
= BytesIO();
811 pyhascontents
= False;
812 if(fsize
>0 and not listonly
):
813 if(fcompression
=="none" or fcompression
=="" or fcompression
=="auto"):
814 fcontents
.write(fp
.read(fsize
));
816 fcontents
.write(fp
.read(fcsize
));
817 pyhascontents
= True;
818 elif(fsize
>0 and listonly
):
819 if(fcompression
=="none" or fcompression
=="" or fcompression
=="auto"):
823 pyhascontents
= False;
824 fcontents
.seek(0, 0);
825 newfccs
= GetFileChecksum(fcontents
.read(), HeaderOut
[-3].lower(), False, formatspecs
);
826 if(fccs
!=newfccs
and not skipchecksum
and not listonly
):
827 VerbosePrintOut("File Content Checksum Error with file " + fname
+ " at offset " + str(fcontentstart
));
828 VerbosePrintOut("'" + str(fccs
) + "' != " + "'" + str(newfccs
) + "'");
830 if(fcompression
=="none" or fcompression
=="" or fcompression
=="auto"):
833 fcontents
.seek(0, 0);
834 fcontents
= UncompressArchiveFile(fcontents
, formatspecs
);
836 fcontentend
= fp
.tell() - 1;
837 catlist
= [ftype
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fcompression
, fcsize
, fuid
, funame
, fgid
, fgname
, fid
, finode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrafieldslist
, fchecksumtype
, fcontents
];
840 def ReadFileDataBySizeWithContent(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
841 delimiter
= formatspecs
[5];
845 catheader
= ReadFileHeaderData(fp
, 4, delimiter
);
848 headercheck
= ValidateHeaderChecksum(catheader
[:-1], catheader
[2], catheader
[3], formatspecs
);
849 newfcs
= GetHeaderChecksum(catheader
[:-2], catheader
[2], True, formatspecs
);
850 if(not headercheck
and not skipchecksum
):
851 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
852 VerbosePrintOut("'" + str(newfcs
) + "' != " + "'" + str(catheader
[3]) + "'");
854 fnumfiles
= int(catheader
[1], 16);
857 while(countnum
< fnumfiles
):
858 HeaderOut
= ReadFileHeaderDataBySizeWithContent(fp
, listonly
, skipchecksum
, formatspecs
)
859 if(len(HeaderOut
)==0):
861 flist
.append(HeaderOut
);
862 countnum
= countnum
+ 1;
865 def ReadFileDataBySizeWithContentToArray(fp
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
866 delimiter
= formatspecs
[5];
870 catheader
= ReadFileHeaderData(fp
, 4, delimiter
);
873 headercheck
= ValidateHeaderChecksum(catheader
[:-1], catheader
[2], catheader
[3], formatspecs
);
874 newfcs
= GetHeaderChecksum(catheader
[:-2], catheader
[2], True, formatspecs
);
875 if(not headercheck
and not skipchecksum
):
876 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
877 VerbosePrintOut("'" + str(newfcs
) + "' != " + "'" + str(catheader
[3]) + "'");
879 catstring
= catheader
[0];
880 catversion
= re
.findall(r
"([\d]+)$", catstring
);
881 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
882 fprenumfiles
= catheader
[1];
883 fnumfiles
= int(fprenumfiles
, 16);
884 fprechecksumtype
= catheader
[2];
885 fprechecksum
= catheader
[3];
886 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
887 if(seekstart
<0 and seekstart
>fnumfiles
):
889 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
891 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
892 seekend
= fnumfiles
- abs(seekend
);
895 while(il
< seekstart
):
896 prefhstart
= fp
.tell();
897 preheaderdata
= ReadFileHeaderDataBySize(fp
, formatspecs
[5]);
898 if(len(preheaderdata
)==0):
900 prefsize
= int(preheaderdata
[5], 16);
901 prenewfcs
= GetHeaderChecksum(preheaderdata
[:-2], preheaderdata
[-3].lower(), True, formatspecs
);
902 prefcs
= preheaderdata
[-2];
903 if(prefcs
!=prenewfcs
and not skipchecksum
):
904 VVerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
905 VerbosePrintOut("'" + str(prefcs
) + "' != " + "'" + str(prenewfcs
) + "'");
907 valid_archive
= False;
908 invalid_archive
= True;
909 prefhend
= fp
.tell() - 1;
910 prefcontentstart
= fp
.tell();
911 prefcontents
= BytesIO();
912 pyhascontents
= False;
914 prefcontents
.write(fp
.read(prefsize
));
915 prefcontents
.seek(0, 0);
916 prenewfccs
= GetFileChecksum(prefcontents
.read(), preheaderdata
[-3].lower(), False, formatspecs
);
917 prefccs
= preheaderdata
[-1];
918 pyhascontents
= True;
919 if(prefccs
!=prenewfccs
and not skipchecksum
):
920 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
921 VerbosePrintOut("'" + str(prefccs
) + "' != " + "'" + str(prenewfccs
) + "'");
926 countnum
= seekstart
;
927 while(countnum
< seekend
):
928 catlist
['ffilelist'].update({realidnum
: {'fid': realidnum
, 'fidalt': realidnum
}});
929 HeaderOut
= ReadFileHeaderDataBySizeWithContentToArray(fp
, listonly
, skipchecksum
, formatspecs
);
932 catlist
['ffilelist'][realidnum
].update(HeaderOut
);
933 countnum
= countnum
+ 1;
934 realidnum
= realidnum
+ 1;
937 def ReadFileDataBySizeWithContentToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
938 if(isinstance(infile
, dict)):
939 listcatfiles
= infile
;
941 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
943 listcatfiles
= ReadFileDataBySizeWithContentToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
);
944 if(not listcatfiles
):
946 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': {}}}};
947 lenlist
= len(listcatfiles
['ffilelist']);
949 lcfx
= int(listcatfiles
['fnumfiles']);
950 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
953 lcfx
= int(listcatfiles
['fnumfiles']);
955 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
956 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
957 catarray
['filetoid'].update(filetoidarray
);
958 catarray
['idtofile'].update(idtofilearray
);
959 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
960 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
961 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
962 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
963 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
964 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
965 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
966 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
967 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
968 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
969 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
970 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
971 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
972 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
973 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
974 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
975 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
976 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
977 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
978 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
979 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
980 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
981 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
982 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
983 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
984 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
985 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
986 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
987 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
988 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
989 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
993 def ReadFileDataBySizeWithContentToList(fp
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
994 delimiter
= formatspecs
[5];
998 catheader
= ReadFileHeaderData(fp
, 4, delimiter
);
1001 headercheck
= ValidateHeaderChecksum(catheader
[:-1], catheader
[2], catheader
[3], formatspecs
);
1002 newfcs
= GetHeaderChecksum(catheader
[:-2], catheader
[2], True, formatspecs
);
1003 if(not headercheck
and not skipchecksum
):
1004 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
1005 VerbosePrintOut("'" + str(newfcs
) + "' != " + "'" + str(catheader
[3]) + "'");
1007 catstring
= catheader
[0];
1008 catversion
= re
.findall(r
"([\d]+)$", catstring
);
1009 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
1010 fprenumfiles
= catheader
[1];
1011 fnumfiles
= int(fprenumfiles
, 16);
1012 fprechecksumtype
= catheader
[2];
1013 fprechecksum
= catheader
[3];
1015 if(seekstart
<0 and seekstart
>fnumfiles
):
1017 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
1018 seekend
= fnumfiles
;
1019 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
1020 seekend
= fnumfiles
- abs(seekend
);
1023 while(il
< seekstart
):
1024 prefhstart
= fp
.tell();
1025 preheaderdata
= ReadFileHeaderDataBySize(fp
, formatspecs
[5]);
1026 if(len(preheaderdata
)==0):
1028 prefsize
= int(preheaderdata
[5], 16);
1029 prenewfcs
= GetHeaderChecksum(preheaderdata
[:-2], preheaderdata
[-3].lower(), True, formatspecs
);
1030 prefcs
= preheaderdata
[-2];
1031 if(prefcs
!=prenewfcs
and not skipchecksum
):
1032 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
1033 VerbosePrintOut("'" + str(prefcs
) + "' != " + "'" + str(prenewfcs
) + "'");
1035 valid_archive
= False;
1036 invalid_archive
= True;
1037 prefhend
= fp
.tell() - 1;
1038 prefcontentstart
= fp
.tell();
1040 pyhascontents
= False;
1042 prefcontents
= fp
.read(prefsize
);
1043 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
1044 prefccs
= preheaderdata
[-1];
1045 pyhascontents
= True;
1046 if(prefccs
!=prenewfccs
and not skipchecksum
):
1047 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
1048 VerbosePrintOut("'" + str(prefccs
) + "' != " + "'" + str(prenewfccs
) + "'");
1053 countnum
= seekstart
;
1054 while(countnum
< seekend
):
1055 HeaderOut
= ReadFileHeaderDataBySizeWithContentToList(fp
, listonly
, skipchecksum
, formatspecs
);
1058 catlist
.append(HeaderOut
);
1059 countnum
= countnum
+ 1;
1060 realidnum
= realidnum
+ 1;
1063 def ReadInFileBySizeWithContentToArray(infile
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
1064 delimiter
= formatspecs
[5];
1065 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1068 fp
= UncompressArchiveFile(fp
, formatspecs
);
1069 checkcompressfile
= CheckCompressionSubType(fp
, formatspecs
, True);
1070 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
1077 if(hasattr(sys
.stdin
, "buffer")):
1078 shutil
.copyfileobj(sys
.stdin
.buffer, fp
);
1080 shutil
.copyfileobj(sys
.stdin
, fp
);
1082 fp
= UncompressArchiveFile(fp
, formatspecs
);
1086 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
1087 fp
= download_file_from_internet_file(infile
);
1088 fp
= UncompressArchiveFile(fp
, formatspecs
);
1094 infile
= RemoveWindowsPath(infile
);
1095 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
1096 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
1098 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
1099 if(not compresscheck
):
1100 fextname
= os
.path
.splitext(infile
)[1];
1101 if(fextname
==".gz"):
1102 compresscheck
= "gzip";
1103 elif(fextname
==".bz2"):
1104 compresscheck
= "bzip2";
1105 elif(fextname
==".zst"):
1106 compresscheck
= "zstd";
1107 elif(fextname
==".lz4" or fextname
==".clz4"):
1108 compresscheck
= "lz4";
1109 elif(fextname
==".lzo" or fextname
==".lzop"):
1110 compresscheck
= "lzo";
1111 elif(fextname
==".lzma" or fextname
==".xz"):
1112 compresscheck
= "lzma";
1115 if(not compresscheck
):
1117 fp
= UncompressFile(infile
, formatspecs
, "rb");
1118 return ReadFileDataBySizeWithContentToArray(fp
, listonly
, skipchecksum
, formatspecs
);
1120 def ReadInFileBySizeWithContentToList(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
1121 delimiter
= formatspecs
[5];
1122 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1125 fp
= UncompressArchiveFile(fp
, formatspecs
);
1126 checkcompressfile
= CheckCompressionSubType(fp
, formatspecs
, True);
1127 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
1134 if(hasattr(sys
.stdin
, "buffer")):
1135 shutil
.copyfileobj(sys
.stdin
.buffer, fp
);
1137 shutil
.copyfileobj(sys
.stdin
, fp
);
1139 fp
= UncompressArchiveFile(fp
, formatspecs
);
1143 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
1144 fp
= download_file_from_internet_file(infile
);
1145 fp
= UncompressArchiveFile(fp
, formatspecs
);
1151 infile
= RemoveWindowsPath(infile
);
1152 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
1153 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
1155 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
1156 if(not compresscheck
):
1157 fextname
= os
.path
.splitext(infile
)[1];
1158 if(fextname
==".gz"):
1159 compresscheck
= "gzip";
1160 elif(fextname
==".bz2"):
1161 compresscheck
= "bzip2";
1162 elif(fextname
==".zst"):
1163 compresscheck
= "zstd";
1164 elif(fextname
==".lz4" or fextname
==".clz4"):
1165 compresscheck
= "lz4";
1166 elif(fextname
==".lzo" or fextname
==".lzop"):
1167 compresscheck
= "lzo";
1168 elif(fextname
==".lzma" or fextname
==".xz"):
1169 compresscheck
= "lzma";
1172 if(not compresscheck
):
1174 fp
= UncompressFile(infile
, formatspecs
, "rb");
1175 return ReadFileDataBySizeWithContentToList(fp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
);
1177 def ReadInFileBySizeWithContentToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
):
1178 if(isinstance(infile
, dict)):
1179 listcatfiles
= infile
;
1181 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
1182 infile
= RemoveWindowsPath(infile
);
1183 listcatfiles
= ReadInFileBySizeWithContentToArray(infile
, listonly
, skipchecksum
, formatspecs
);
1184 if(not listcatfiles
):
1186 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': {}}}};
1187 lenlist
= len(listcatfiles
['ffilelist']);
1189 lcfx
= int(listcatfiles
['fnumfiles']);
1190 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
1191 lcfx
= int(lenlist
);
1193 lcfx
= int(listcatfiles
['fnumfiles']);
1195 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
1196 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
1197 catarray
['filetoid'].update(filetoidarray
);
1198 catarray
['idtofile'].update(idtofilearray
);
1199 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
1200 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
1201 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
1202 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
1203 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
1204 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
1205 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
1206 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
1207 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
1208 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
1209 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
1210 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
1211 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
1212 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
1213 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
1214 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
1215 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
1216 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
1217 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
1218 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
1219 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
1220 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
1221 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
1222 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
1223 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
1224 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
1225 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
1226 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
1227 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
1228 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
1229 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
1233 def AppendNullByte(indata
, delimiter
=__file_format_delimiter__
):
1234 outdata
= str(indata
) + delimiter
;
1237 def AppendNullBytes(indata
=[], delimiter
=__file_format_delimiter__
):
1242 outdata
= outdata
+ AppendNullByte(indata
[inum
], delimiter
);
1246 def AppendFileHeader(fp
, numfiles
, checksumtype
="crc32", formatspecs
=__file_format_list__
):
1247 delimiter
= formatspecs
[5];
1248 catver
= formatspecs
[6];
1249 fileheaderver
= str(int(catver
.replace(".", "")));
1250 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
1251 fp
.write(fileheader
.encode('UTF-8'));
1252 fnumfiles
= format(int(numfiles
), 'x').lower();
1253 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
1254 catfileheadercshex
= GetFileChecksum(fileheader
+ fnumfilesa
, checksumtype
, True, formatspecs
);
1255 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
1256 fp
.write(fnumfilesa
.encode('UTF-8'));
1259 os
.fsync(fp
.fileno());
1260 except io
.UnsupportedOperation
:
1262 except AttributeError:
1264 except OSError as e
:
1268 def MakeEmptyFilePointer(fp
, checksumtype
="crc32", formatspecs
=__file_format_list__
):
1269 AppendFileHeader(fp
, 0, checksumtype
, formatspecs
);
1272 def MakeEmptyFile(outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", formatspecs
=__file_format_list__
, returnfp
=False):
1273 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1274 if(os
.path
.exists(outfile
)):
1277 except OSError as e
:
1281 catfpfp
= BytesIO();
1282 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1284 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1287 fbasename
= os
.path
.splitext(outfile
)[0];
1288 fextname
= os
.path
.splitext(outfile
)[1];
1289 catfp
= CompressOpenFile(outfile
, True, compressionlevel
);
1290 catfp
= AppendFileHeader(catfp
, 0, checksumtype
, formatspecs
);
1291 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1292 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
1295 os
.fsync(catfp
.fileno());
1296 except io
.UnsupportedOperation
:
1298 except AttributeError:
1300 except OSError as e
:
1304 if(hasattr(sys
.stdout
, "buffer")):
1305 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1307 shutil
.copyfileobj(catfp
, sys
.stdout
);
1308 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1309 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
1311 upload_file_to_internet_file(catfp
, outfile
);
1319 def AppendFileHeaderWithContent(fp
, filevalues
=[], extradata
=[], filecontent
="", checksumtype
="crc32", formatspecs
=__file_format_list__
):
1320 extrafields
= format(len(extradata
), 'x').lower();
1321 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
1322 if(len(extradata
)>0):
1323 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
1324 extrasizelen
= format(len(extrasizestr
), 'x').lower();
1325 catoutlen
= len(filevalues
) + len(extradata
) + 5;
1326 catoutlenhex
= format(catoutlen
, 'x').lower();
1327 catoutlist
= filevalues
;
1328 catoutlist
.insert(0, catoutlenhex
);
1329 catoutlist
.append(extrasizelen
);
1330 catoutlist
.append(extrafields
);
1331 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
1332 if(len(extradata
)>0):
1333 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
1334 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
1335 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
1336 catfilecontentcshex
= GetFileChecksum(filecontent
, checksumtype
, False, formatspecs
);
1337 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1338 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
1339 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
1340 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
1341 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1342 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
1343 nullstrecd
= formatspecs
[5].encode('UTF-8');
1344 catfileout
= catfileoutstrecd
+ filecontent
+ nullstrecd
;
1345 fp
.write(catfileout
);
1348 os
.fsync(fp
.fileno());
1349 except io
.UnsupportedOperation
:
1351 except AttributeError:
1353 except OSError as e
:
1357 def AppendFilesWithContent(infiles
, fp
, dirlistfromtxt
=False, filevalues
=[], extradata
=[], compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False):
1358 advancedlist
= formatspecs
[8];
1359 altinode
= formatspecs
[9];
1361 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1363 for line
in sys
.stdin
:
1364 infilelist
.append(line
.strip());
1365 infilelist
= list(filter(None, infilelist
));
1366 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
1367 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
1369 with
UncompressFile(infiles
, formatspecs
, "r") as finfile
:
1370 for line
in finfile
:
1371 infilelist
.append(line
.strip());
1372 infilelist
= list(filter(None, infilelist
));
1374 if(isinstance(infiles
, (list, tuple, ))):
1375 infilelist
= list(filter(None, infiles
));
1376 elif(isinstance(infiles
, (str, ))):
1377 infilelist
= list(filter(None, [infiles
]));
1379 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
1381 GetDirList
= ListDir(infilelist
, followlink
, False);
1389 inodetocatinode
= {};
1390 numfiles
= int(len(GetDirList
));
1391 fnumfiles
= format(numfiles
, 'x').lower();
1392 AppendFileHeader(fp
, fnumfiles
, checksumtype
, formatspecs
);
1393 for curfname
in GetDirList
:
1394 if(re
.findall("^[.|/]", curfname
)):
1397 fname
= "./"+curfname
;
1399 VerbosePrintOut(fname
);
1400 if(not followlink
or followlink
is None):
1401 fstatinfo
= os
.lstat(fname
);
1403 fstatinfo
= os
.stat(fname
);
1404 fpremode
= fstatinfo
.st_mode
;
1405 finode
= fstatinfo
.st_ino
;
1406 flinkcount
= fstatinfo
.st_nlink
;
1408 if(stat
.S_ISREG(fpremode
)):
1410 elif(stat
.S_ISLNK(fpremode
)):
1412 elif(stat
.S_ISCHR(fpremode
)):
1414 elif(stat
.S_ISBLK(fpremode
)):
1416 elif(stat
.S_ISDIR(fpremode
)):
1418 elif(stat
.S_ISFIFO(fpremode
)):
1420 elif(stat
.S_ISSOCK(fpremode
)):
1422 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
1424 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
1426 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
1431 fcurfid
= format(int(curfid
), 'x').lower();
1432 if(not followlink
and finode
!=0):
1434 if(finode
in inodelist
):
1436 flinkname
= inodetofile
[finode
];
1438 fcurinode
= format(int(finode
), 'x').lower();
1440 fcurinode
= format(int(inodetocatinode
[finode
]), 'x').lower();
1441 if(finode
not in inodelist
):
1442 inodelist
.append(finode
);
1443 inodetofile
.update({finode
: fname
});
1444 inodetocatinode
.update({finode
: curinode
});
1446 fcurinode
= format(int(finode
), 'x').lower();
1448 fcurinode
= format(int(curinode
), 'x').lower();
1449 curinode
= curinode
+ 1;
1451 fcurinode
= format(int(curinode
), 'x').lower();
1452 curinode
= curinode
+ 1;
1453 curfid
= curfid
+ 1;
1455 flinkname
= os
.readlink(fname
);
1456 fdev
= fstatinfo
.st_dev
;
1457 getfdev
= GetDevMajorMinor(fdev
);
1458 fdev_minor
= getfdev
[0];
1459 fdev_major
= getfdev
[1];
1460 frdev
= fstatinfo
.st_dev
;
1461 if(hasattr(fstatinfo
, "st_rdev")):
1462 frdev
= fstatinfo
.st_rdev
;
1464 frdev
= fstatinfo
.st_dev
;
1465 getfrdev
= GetDevMajorMinor(frdev
);
1466 frdev_minor
= getfrdev
[0];
1467 frdev_major
= getfrdev
[1];
1468 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
1469 fsize
= format(int("0"), 'x').lower();
1470 elif(ftype
==0 or ftype
==7):
1471 fsize
= format(int(fstatinfo
.st_size
), 'x').lower();
1473 fsize
= format(int(fstatinfo
.st_size
)).lower();
1474 fatime
= format(int(fstatinfo
.st_atime
), 'x').lower();
1475 fmtime
= format(int(fstatinfo
.st_mtime
), 'x').lower();
1476 fctime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1477 if(hasattr(fstatinfo
, "st_birthtime")):
1478 fbtime
= format(int(fstatinfo
.st_birthtime
), 'x').lower();
1480 fbtime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1481 fmode
= format(int(fstatinfo
.st_mode
), 'x').lower();
1482 fchmode
= format(int(stat
.S_IMODE(fstatinfo
.st_mode
)), 'x').lower();
1483 ftypemod
= format(int(stat
.S_IFMT(fstatinfo
.st_mode
)), 'x').lower();
1484 fuid
= format(int(fstatinfo
.st_uid
), 'x').lower();
1485 fgid
= format(int(fstatinfo
.st_gid
), 'x').lower();
1490 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
1491 funame
= userinfo
.pw_name
;
1500 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
1501 fgname
= groupinfo
.gr_name
;
1506 fdev_minor
= format(int(fdev_minor
), 'x').lower();
1507 fdev_major
= format(int(fdev_major
), 'x').lower();
1508 frdev_minor
= format(int(frdev_minor
), 'x').lower();
1509 frdev_major
= format(int(frdev_major
), 'x').lower();
1510 finode
= format(int(finode
), 'x').lower();
1511 flinkcount
= format(int(flinkcount
), 'x').lower();
1512 if(hasattr(fstatinfo
, "st_file_attributes")):
1513 fwinattributes
= format(int(fstatinfo
.st_file_attributes
), 'x').lower();
1515 fwinattributes
= format(int(0), 'x').lower();
1517 fcsize
= format(int(0), 'x').lower();
1518 fcontents
= BytesIO();
1520 if(ftype
==0 or ftype
==7):
1521 with
open(fname
, "rb") as fpc
:
1522 shutil
.copyfileobj(fpc
, fcontents
);
1523 if(not compresswholefile
):
1524 fcontents
.seek(0, 2);
1525 ucfsize
= fcontents
.tell();
1526 fcontents
.seek(0, 0);
1527 cfcontents
= BytesIO();
1528 shutil
.copyfileobj(fcontents
, cfcontents
);
1529 cfcontents
.seek(0, 0);
1530 cfcontents
= CompressArchiveFile(cfcontents
, compression
, compressionlevel
, formatspecs
);
1531 cfcontents
.seek(0, 2);
1532 cfsize
= cfcontents
.tell();
1533 if(ucfsize
> cfsize
):
1534 fcsize
= format(int(cfsize
), 'x').lower();
1535 fcompression
= compression
;
1537 fcontents
= cfcontents
;
1538 if(followlink
and (ftype
==1 or ftype
==2)):
1539 flstatinfo
= os
.stat(flinkname
);
1540 with
open(flinkname
, "rb") as fpc
:
1541 shutil
.copyfileobj(fpc
, fcontents
);
1542 if(not compresswholefile
):
1543 fcontents
.seek(0, 2);
1544 ucfsize
= fcontents
.tell();
1545 fcontents
.seek(0, 0);
1546 cfcontents
= BytesIO();
1547 shutil
.copyfileobj(fcontents
, cfcontents
);
1548 cfcontents
.seek(0, 0);
1549 cfcontents
= CompressArchiveFile(cfcontents
, compression
, compressionlevel
, formatspecs
);
1550 cfcontents
.seek(0, 2);
1551 cfsize
= cfcontents
.tell();
1552 if(ucfsize
> cfsize
):
1553 fcsize
= format(int(cfsize
), 'x').lower();
1554 fcompression
= compression
;
1556 fcontents
= cfcontents
;
1557 if(fcompression
=="auto" or fcompression
=="none"):
1559 fcontents
.seek(0, 0);
1560 ftypehex
= format(ftype
, 'x').lower();
1561 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fcompression
, fcsize
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
];
1562 fp
= AppendFileHeaderWithContent(fp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
);
1564 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
1568 def AppendListsWithContent(inlist
, fp
, dirlistfromtxt
=False, filevalues
=[], extradata
=[], compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False):
1570 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1571 GetDirList
= inlist
;
1579 inodetocatinode
= {};
1580 numfiles
= int(len(GetDirList
));
1581 fnumfiles
= format(numfiles
, 'x').lower();
1582 AppendFileHeader(fp
, fnumfiles
, checksumtype
, formatspecs
);
1583 for curfname
in GetDirList
:
1584 ftype
= format(curfname
[0], 'x').lower();
1585 if(re
.findall("^[.|/]", curfname
[1])):
1586 fname
= curfname
[1];
1588 fname
= "./"+curfname
[1];
1589 fbasedir
= os
.path
.dirname(fname
);
1590 flinkname
= curfname
[2];
1591 fsize
= format(curfname
[3], 'x').lower();
1592 fatime
= format(curfname
[4], 'x').lower();
1593 fmtime
= format(curfname
[5], 'x').lower();
1594 fctime
= format(curfname
[6], 'x').lower();
1595 fbtime
= format(curfname
[7], 'x').lower();
1596 fmode
= format(curfname
[8], 'x').lower();
1597 fwinattributes
= format(curfname
[9], 'x').lower();
1598 fcompression
= curfname
[10];
1599 fcsize
= int(curfname
[11], 16);
1600 fuid
= format(curfname
[12], 'x').lower();
1601 funame
= curfname
[13];
1602 fgid
= format(curfname
[14], 'x').lower();
1603 fgname
= curfname
[15];
1604 fid
= format(curfname
[16], 'x').lower();
1605 finode
= format(curfname
[17], 'x').lower();
1606 flinkcount
= format(curfname
[18], 'x').lower();
1607 fdev_minor
= format(curfname
[19], 'x').lower();
1608 fdev_major
= format(curfname
[20], 'x').lower();
1609 frdev_minor
= format(curfname
[21], 'x').lower();
1610 frdev_major
= format(curfname
[22], 'x').lower();
1611 extradata
= curfname
[23];
1612 fchecksumtype
= curfname
[24];
1613 fcontents
= curfname
[25];
1614 catoutlist
= [ftype
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fcompression
, fcsize
, fuid
, funame
, fgid
, fgname
, fid
, finode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
];
1615 fp
= AppendFileHeaderWithContent(fp
, catoutlist
, extradata
, fcontents
, checksumtype
, formatspecs
);
1617 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
1621 def AppendInFileWithContent(infile
, fp
, dirlistfromtxt
=False, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False):
1622 inlist
= ReadInFileBySizeWithContentToList(infile
, 0, 0, False, False, formatspecs
);
1623 return AppendListsWithContent(inlist
, fp
, dirlistfromtxt
, filevalues
, extradata
, followlink
, checksumtype
, formatspecs
, verbose
);
1625 def AppendFilesWithContentToOutFile(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1626 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1627 if(os
.path
.exists(outfile
)):
1630 except OSError as e
:
1634 catfpfp
= BytesIO();
1635 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1637 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1640 fbasename
= os
.path
.splitext(outfile
)[0];
1641 fextname
= os
.path
.splitext(outfile
)[1];
1642 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
);
1643 catfp
= AppendFilesWithContent(infiles
, catfp
, dirlistfromtxt
, filevalues
, extradata
, compression
, compresswholefile
, compressionlevel
, followlink
, checksumtype
, formatspecs
, verbose
);
1644 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1645 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
1648 os
.fsync(catfp
.fileno());
1649 except io
.UnsupportedOperation
:
1651 except AttributeError:
1653 except OSError as e
:
1657 if(hasattr(sys
.stdout
, "buffer")):
1658 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1660 shutil
.copyfileobj(catfp
, sys
.stdout
);
1661 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1662 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
1664 upload_file_to_internet_file(catfp
, outfile
);
1672 def AppendListsWithContentToOutFile(inlist
, outfile
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1673 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1674 if(os
.path
.exists(outfile
)):
1677 except OSError as e
:
1681 catfpfp
= BytesIO();
1682 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1684 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1687 fbasename
= os
.path
.splitext(outfile
)[0];
1688 fextname
= os
.path
.splitext(outfile
)[1];
1689 catfp
= CompressOpenFile(outfile
, True, compressionlevel
);
1690 catfp
= AppendListsWithContent(inlist
, catfp
, dirlistfromtxt
, filevalues
, extradata
, compression
=, compresswholefile
, compressionlevel
, followlink
, checksumtype
, formatspecs
, verbose
);
1691 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1692 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
1695 os
.fsync(catfp
.fileno());
1696 except io
.UnsupportedOperation
:
1698 except AttributeError:
1700 except OSError as e
:
1704 if(hasattr(sys
.stdout
, "buffer")):
1705 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1707 shutil
.copyfileobj(catfp
, sys
.stdout
);
1708 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1709 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
1711 upload_file_to_internet_file(catfp
, outfile
);
1719 def AppendInFileWithContentToOutFile(infile
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1720 inlist
= ReadInFileBySizeWithContentToList(infile
, 0, 0, False, False, formatspecs
);
1721 return AppendListsWithContentToOutFile(inlist
, outfile
, dirlistfromtxt
, compression
, compressionlevel
, filevalues
, extradata
, followlink
, checksumtype
, formatspecs
, verbose
, returnfp
);
1723 def PrintPermissionString(fchmode
, ftype
):
1724 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' } };
1726 for fmodval
in str(oct(fchmode
))[-3:]:
1727 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
1728 if(ftype
==0 or ftype
==7):
1729 permissionstr
= "-" + permissionstr
;
1731 permissionstr
= "h" + permissionstr
;
1733 permissionstr
= "l" + permissionstr
;
1735 permissionstr
= "c" + permissionstr
;
1737 permissionstr
= "b" + permissionstr
;
1739 permissionstr
= "d" + permissionstr
;
1741 permissionstr
= "f" + permissionstr
;
1743 permissionstr
= "D" + permissionstr
;
1745 permissionstr
= "p" + permissionstr
;
1747 permissionstr
= "w" + permissionstr
;
1749 permissionoutstr
= stat
.filemode(fchmode
);
1750 except AttributeError:
1751 permissionoutstr
= permissionstr
;
1753 permissionoutstr
= permissionstr
;
1754 return permissionoutstr
;
1756 def PrintPermissionStringAlt(fchmode
, ftype
):
1758 '0': '---', '1': '--x', '2': '-w-', '3': '-wx',
1759 '4': 'r--', '5': 'r-x', '6': 'rw-', '7': 'rwx'
1761 # Translate file mode into permission string
1762 permissionstr
= ''.join([permissions
[i
] for i
in str(oct(fchmode
))[-3:]]);
1763 # Append file type indicator
1765 0: '-', 1: 'h', 2: 'l', 3: 'c', 4: 'b',
1766 5: 'd', 6: 'f', 8: 'D', 9: 'p', 10: 'w'
1768 file_type
= type_indicators
.get(ftype
, '-');
1769 permissionstr
= file_type
+ permissionstr
;
1771 permissionoutstr
= stat
.filemode(fchmode
);
1772 except AttributeError:
1773 permissionoutstr
= permissionstr
;
1774 return permissionoutstr
;
1776 def CompressionSupport():
1777 compression_list
= [];
1780 compression_list
.append("gz");
1781 compression_list
.append("gzip");
1786 compression_list
.append("bz2");
1787 compression_list
.append("bzip2");
1792 compression_list
.append("lz4");
1797 compression_list
.append("lzo");
1798 compression_list
.append("lzop");
1803 compression_list
.append("zstd");
1804 compression_list
.append("zstandard");
1809 compression_list
.append("lzma");
1810 compression_list
.append("xz");
1813 return compression_list
;
1815 def CheckCompressionType(infile
, formatspecs
=__file_format_list__
, closefp
=True):
1816 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1820 catfp
= open(infile
, "rb");
1821 except FileNotFoundError
:
1824 prefp
= catfp
.read(2);
1826 if(prefp
==binascii
.unhexlify("1f8b")):
1829 prefp
= catfp
.read(3);
1830 if(prefp
==binascii
.unhexlify("425a68")):
1832 if(prefp
==binascii
.unhexlify("5d0000")):
1835 prefp
= catfp
.read(4);
1836 if(prefp
==binascii
.unhexlify("28b52ffd")):
1838 if(prefp
==binascii
.unhexlify("04224d18")):
1840 if(prefp
==binascii
.unhexlify("504B0304")):
1841 filetype
= "zipfile";
1843 prefp
= catfp
.read(5);
1844 if(prefp
==binascii
.unhexlify("7573746172")):
1845 filetype
= "tarfile";
1847 prefp
= catfp
.read(6);
1848 if(prefp
==binascii
.unhexlify("fd377a585a00")):
1850 if(prefp
==binascii
.unhexlify("377abcaf271c")):
1851 filetype
= "7zipfile";
1853 prefp
= catfp
.read(7);
1854 if(prefp
==binascii
.unhexlify("526172211a0700")):
1855 filetype
= "rarfile";
1856 if(prefp
==binascii
.unhexlify("43617446696c65")):
1857 filetype
= "catfile";
1859 prefp
= catfp
.read(8);
1860 if(prefp
==binascii
.unhexlify("526172211a070100")):
1861 filetype
= "rarfile";
1863 prefp
= catfp
.read(formatspecs
[3]);
1864 if(prefp
==binascii
.unhexlify(formatspecs
[4])):
1865 filetype
= formatspecs
[2];
1867 prefp
= catfp
.read(9);
1868 if(prefp
==binascii
.unhexlify("894c5a4f000d0a1a0a")):
1871 prefp
= catfp
.read(10);
1872 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
1873 filetype
= "tarfile";
1875 if(filetype
=="gzip" or filetype
=="bzip2" or filetype
=="lzma" or filetype
=="zstd" or filetype
=="lz4"):
1876 if(is_tarfile(catfp
)):
1877 filetype
= "tarfile";
1879 if(is_tarfile(catfp
)):
1880 filetype
= "tarfile";
1881 elif(zipfile
.is_zipfile(catfp
)):
1882 filetype
= "zipfile";
1883 elif(rarfile
.is_rarfile(catfp
) or rarfile
.is_rarfile_sfx(catfp
)):
1884 filetype
= "rarile";
1892 def CheckCompressionTypeFromString(instring
, formatspecs
=__file_format_list__
, closefp
=True):
1894 instringsfile
= BytesIO(instring
);
1896 instringsfile
= BytesIO(instring
.encode("UTF-8"));
1897 return CheckCompressionType(instringsfile
, formatspecs
, closefp
);
1899 def GetCompressionMimeType(infile
, formatspecs
=__file_format_list__
):
1900 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
1901 if(compresscheck
=="gzip" or compresscheck
=="gz"):
1902 return archivefile_gzip_mimetype
;
1903 if(compresscheck
=="bzip2" or compresscheck
=="bz2"):
1904 return archivefile_bzip2_mimetype
;
1905 if(compresscheck
=="zstd" or compresscheck
=="zstandard"):
1906 return archivefile_zstandard_mimetype
;
1907 if(compresscheck
=="lz4"):
1908 return archivefile_lz4_mimetype
;
1909 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
1910 return archivefile_lzop_mimetype
;
1911 if(compresscheck
=="lzma"):
1912 return archivefile_lzma_mimetype
;
1913 if(compresscheck
=="xz"):
1914 return archivefile_xz_mimetype
;
1915 if(compresscheck
=="catfile" or compresscheck
=="cat" or compresscheck
==formatspecs
[2]):
1916 return archivefile_cat_mimetype
;
1917 if(not compresscheck
):
1921 def UncompressArchiveFile(fp
, formatspecs
=__file_format_list__
):
1922 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
1924 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
1925 if(compresscheck
=="gzip"):
1930 catfp
= gzip
.GzipFile(fileobj
=fp
, mode
="rb");
1931 if(compresscheck
=="bzip2"):
1937 catfp
.write(bz2
.decompress(fp
.read()));
1938 if(compresscheck
=="zstd"):
1944 catfp
.write(zstandard
.decompress(fp
.read()));
1945 if(compresscheck
=="lz4"):
1951 catfp
.write(lz4
.frame
.decompress(fp
.read()));
1952 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
1958 catfp
.write(lzo
.decompress(fp
.read()));
1959 if(compresscheck
=="lzma" or compresscheck
=="xz"):
1965 catfp
.write(lzma
.decompress(fp
.read()));
1966 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[2]):
1968 if(not compresscheck
):
1974 with fp
as fpcontent
:
1976 catfp
.write(lzma
.decompress(fp
.read()));
1977 except lzma
.LZMAError
:
1979 if(compresscheck
!="catfile" or compresscheck
!=formatspecs
[2]):
1983 create_alias_function("Uncompress", __file_format_name__
, "", UncompressArchiveFile
);
1985 def UncompressFile(infile
, formatspecs
=__file_format_list__
, mode
="rb"):
1986 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
1987 if(sys
.version_info
[0]==2 and compresscheck
):
1993 if(compresscheck
=="gzip"):
1999 filefp
= gzip
.open(infile
, mode
, encoding
="UTF-8");
2000 except (ValueError, TypeError) as e
:
2001 filefp
= gzip
.open(infile
, mode
);
2002 if(compresscheck
=="bzip2"):
2008 filefp
= bz2
.open(infile
, mode
, encoding
="UTF-8");
2009 except (ValueError, TypeError) as e
:
2010 filefp
= bz2
.open(infile
, mode
);
2011 if(compresscheck
=="zstd"):
2017 filefp
= zstandard
.open(infile
, mode
, encoding
="UTF-8");
2018 except (ValueError, TypeError) as e
:
2019 filefp
= zstandard
.open(infile
, mode
);
2020 if(compresscheck
=="lz4"):
2026 filefp
= lz4
.frame
.open(infile
, mode
, encoding
="UTF-8");
2027 except (ValueError, TypeError) as e
:
2028 filefp
= lz4
.frame
.open(infile
, mode
);
2029 if(compresscheck
=="lzo"):
2035 filefp
= lzo
.open(infile
, mode
, encoding
="UTF-8");
2036 except (ValueError, TypeError) as e
:
2037 filefp
= lzo
.open(infile
, mode
);
2038 if(compresscheck
=="lzma"):
2044 filefp
= lzma
.open(infile
, mode
, encoding
="UTF-8");
2045 except (ValueError, TypeError) as e
:
2046 filefp
= lzma
.open(infile
, mode
);
2047 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[2]):
2049 filefp
= open(infile
, mode
, encoding
="UTF-8");
2050 except (ValueError, TypeError) as e
:
2051 filefp
= open(infile
, mode
);
2052 if(not compresscheck
):
2054 filefp
= open(infile
, mode
, encoding
="UTF-8");
2055 except (ValueError, TypeError) as e
:
2056 filefp
= open(infile
, mode
);
2057 except FileNotFoundError
:
2061 def UncompressString(infile
):
2062 compresscheck
= CheckCompressionTypeFromString(infile
, formatspecs
, False);
2063 if(compresscheck
=="gzip"):
2068 fileuz
= gzip
.decompress(infile
);
2069 if(compresscheck
=="bzip2"):
2074 fileuz
= bz2
.decompress(infile
);
2075 if(compresscheck
=="zstd"):
2080 fileuz
= zstandard
.decompress(infile
);
2081 if(compresscheck
=="lz4"):
2086 fileuz
= lz4
.frame
.decompress(infile
);
2087 if(compresscheck
=="lzo"):
2092 fileuz
= lzo
.decompress(infile
);
2093 if(compresscheck
=="lzma"):
2098 fileuz
= lzma
.decompress(infile
);
2099 if(not compresscheck
):
2101 if(hasattr(fileuz
, 'decode')):
2102 fileuz
= fileuz
.decode("UTF-8");
2105 def UncompressStringAlt(infile
):
2106 filefp
= StringIO();
2107 outstring
= UncompressString(infile
);
2108 filefp
.write(outstring
);
2112 def CheckCompressionSubType(infile
, formatspecs
=__file_format_list__
, closefp
=True):
2113 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
2114 if(not compresscheck
):
2115 fextname
= os
.path
.splitext(infile
)[1];
2116 if(fextname
==".gz"):
2117 compresscheck
= "gzip";
2118 elif(fextname
==".bz2"):
2119 compresscheck
= "bzip2";
2120 elif(fextname
==".zst"):
2121 compresscheck
= "zstd";
2122 elif(fextname
==".lz4"):
2123 compresscheck
= "lz4";
2124 elif(fextname
==".lzo" or fextname
==".lzop"):
2125 compresscheck
= "lzo";
2126 elif(fextname
==".lzma" or fextname
==".xz"):
2127 compresscheck
= "lzma";
2130 if(compresscheck
=="gzip" or compresscheck
=="bzip2" or compresscheck
=="lzma" or compresscheck
=="zstd" or compresscheck
=="lz4"):
2131 if(is_tarfile(infile
)):
2132 filetype
= "tarfile";
2133 if(not compresscheck
):
2134 if(is_tarfile(infile
)):
2136 elif(zipfile
.is_zipfile(infile
)):
2138 elif(rarfile
.is_rarfile(infile
) or rarfile
.is_rarfile_sfx(infile
)):
2143 if(compresscheck
=="catfile"):
2145 if(compresscheck
==formatspecs
[2]):
2146 return formatspecs
[2];
2147 if(compresscheck
=="tarfile"):
2149 if(compresscheck
=="zipfile"):
2151 if(rarfile_support
and compresscheck
=="rarfile"):
2153 if(py7zr_support
and compresscheck
=="7zipfile"):
2155 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2156 catfp
= UncompressArchiveFile(infile
, formatspecs
[2]);
2159 if(compresscheck
=="gzip"):
2164 catfp
= gzip
.GzipFile(infile
, "rb");
2165 if(compresscheck
=="bzip2"):
2170 catfp
= bz2
.BZ2File(infile
, "rb");
2171 if(compresscheck
=="lz4"):
2176 catfp
= lz4
.frame
.open(infile
, "rb");
2177 if(compresscheck
=="zstd"):
2182 catfp
= zstandard
.open(infile
, "rb");
2183 if(compresscheck
=="lzma" or compresscheck
=="xz"):
2188 catfp
= lzma
.open(infile
, "rb");
2189 except FileNotFoundError
:
2192 prefp
= catfp
.read(5);
2193 if(prefp
==binascii
.unhexlify("7573746172")):
2194 filetype
= "tarfile";
2196 prefp
= catfp
.read(7);
2197 if(prefp
==binascii
.unhexlify("43617446696c65")):
2198 filetype
= "catfile";
2200 prefp
= catfp
.read(formatspecs
[3]);
2201 if(prefp
==binascii
.unhexlify(formatspecs
[4])):
2202 filetype
= formatspecs
[2];
2204 prefp
= catfp
.read(10);
2205 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
2206 filetype
= "tarfile";
2212 def GZipCompress(data
, compresslevel
=9):
2217 tmpfp
= tempfile
.NamedTemporaryFile("wb", delete
=False);
2219 tmpfp
= gzip
.GzipFile(tmpfp
.name
, mode
="wb", compresslevel
=compresslevel
);
2223 catfp
= open(tmpfp
.name
, "rb");
2224 except FileNotFoundError
:
2226 catdata
= catfp
.read();
2230 def CompressArchiveFile(fp
, compression
="auto", compressionlevel
=None, formatspecs
=__file_format_list__
):
2231 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
2234 if(not compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2235 compression
= "auto";
2236 if(compression
not in compressionlist
and compression
is None):
2237 compression
= "auto";
2238 if(compression
=="gzip"):
2244 if(compressionlevel
is None):
2245 compressionlevel
= 9;
2247 compressionlevel
= int(compressionlevel
);
2248 catfp
.write(gzip
.compress(fp
.read(), compresslevel
=compressionlevel
));
2249 if(compression
=="bzip2"):
2255 if(compressionlevel
is None):
2256 compressionlevel
= 9;
2258 compressionlevel
= int(compressionlevel
);
2259 catfp
.write(bz2
.compress(fp
.read(), compresslevel
=compressionlevel
));
2260 if(compression
=="lz4"):
2266 if(compressionlevel
is None):
2267 compressionlevel
= 9;
2269 compressionlevel
= int(compressionlevel
);
2270 catfp
.write(lz4
.frame
.compress(fp
.read(), compression_level
=compressionlevel
));
2271 if(compression
=="lzo" or compression
=="lzop"):
2277 if(compressionlevel
is None):
2278 compressionlevel
= 9;
2280 compressionlevel
= int(compressionlevel
);
2281 catfp
.write(lzo
.compress(fp
.read(), compresslevel
=compressionlevel
));
2282 if(compression
=="zstd"):
2288 if(compressionlevel
is None):
2289 compressionlevel
= 10;
2291 compressionlevel
= int(compressionlevel
);
2292 catfp
.write(zstandard
.compress(fp
.read(), level
=compressionlevel
));
2293 if(compression
=="lzma"):
2299 if(compressionlevel
is None):
2300 compressionlevel
= 9;
2302 compressionlevel
= int(compressionlevel
);
2303 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]));
2304 if(compression
=="xz"):
2310 if(compressionlevel
is None):
2311 compressionlevel
= 9;
2313 compressionlevel
= int(compressionlevel
);
2314 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]));
2315 if(compression
=="auto" or compression
is None):
2320 create_alias_function("Compress", __file_format_name__
, "", CompressArchiveFile
);
2322 def CompressOpenFile(outfile
, compressionenable
=True, compressionlevel
=None):
2323 if(outfile
is None):
2325 fbasename
= os
.path
.splitext(outfile
)[0];
2326 fextname
= os
.path
.splitext(outfile
)[1];
2327 if(compressionlevel
is None and fextname
!=".zst"):
2328 compressionlevel
= 9;
2329 elif(compressionlevel
is None and fextname
==".zst"):
2330 compressionlevel
= 10;
2332 compressionlevel
= int(compressionlevel
);
2333 if(sys
.version_info
[0]==2):
2338 if(fextname
not in outextlistwd
or not compressionenable
):
2340 outfp
= open(outfile
, "wb", encoding
="UTF-8");
2341 except (ValueError, TypeError) as e
:
2342 outfp
= open(outfile
, "wb");
2343 elif(fextname
==".gz"):
2349 outfp
= gzip
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
2350 except (ValueError, TypeError) as e
:
2351 outfp
= gzip
.open(outfile
, mode
, compressionlevel
);
2352 elif(fextname
==".bz2"):
2358 outfp
= bz2
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
2359 except (ValueError, TypeError) as e
:
2360 outfp
= bz2
.open(outfile
, mode
, compressionlevel
);
2361 elif(fextname
==".zst"):
2367 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
), encoding
="UTF-8");
2368 except (ValueError, TypeError) as e
:
2369 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
));
2370 elif(fextname
==".xz"):
2376 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}], encoding
="UTF-8");
2377 except (ValueError, TypeError) as e
:
2378 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]);
2379 elif(fextname
==".lz4"):
2385 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
, encoding
="UTF-8");
2386 except (ValueError, TypeError) as e
:
2387 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
);
2388 elif(fextname
==".lzo"):
2394 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
, encoding
="UTF-8");
2395 except (ValueError, TypeError) as e
:
2396 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
);
2397 elif(fextname
==".lzma"):
2403 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}], encoding
="UTF-8");
2404 except (ValueError, TypeError) as e
:
2405 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]);
2406 except FileNotFoundError
:
2410 def GetDevMajorMinor(fdev
):
2412 if(hasattr(os
, "minor")):
2413 retdev
.append(os
.minor(fdev
));
2416 if(hasattr(os
, "major")):
2417 retdev
.append(os
.major(fdev
));
2422 def CheckSumSupport(checkfor
, guaranteed
=True):
2424 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
2426 hash_list
= sorted(list(hashlib
.algorithms_available
));
2427 checklistout
= sorted(hash_list
+ ['adler32', 'crc16', 'crc16_ansi', 'crc16_ibm', 'crc16_ccitt', 'crc32', 'crc64', 'crc64_ecma', 'crc64_iso', 'none']);
2428 if(checkfor
in checklistout
):
2433 def CheckSumSupportAlt(checkfor
, guaranteed
=True):
2435 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
2437 hash_list
= sorted(list(hashlib
.algorithms_available
));
2438 checklistout
= hash_list
;
2439 if(checkfor
in checklistout
):
2444 def PackArchiveFile(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2445 advancedlist
= formatspecs
[8];
2446 altinode
= formatspecs
[9];
2447 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2448 outfile
= RemoveWindowsPath(outfile
);
2449 checksumtype
= checksumtype
.lower();
2450 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2451 checksumtype
="crc32";
2452 if(checksumtype
=="none"):
2454 if(not compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2455 compression
= "auto";
2456 if(compression
not in compressionlist
and compression
is None):
2457 compression
= "auto";
2459 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2460 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2461 if(os
.path
.exists(outfile
)):
2464 except OSError as e
:
2469 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2471 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2474 fbasename
= os
.path
.splitext(outfile
)[0];
2475 fextname
= os
.path
.splitext(outfile
)[1];
2476 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
);
2477 catver
= formatspecs
[6];
2478 fileheaderver
= str(int(catver
.replace(".", "")));
2481 for line
in sys
.stdin
:
2482 infilelist
.append(line
.strip());
2483 infilelist
= list(filter(None, infilelist
));
2484 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
2485 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
2487 with
UncompressFile(infiles
, formatspecs
, "r") as finfile
:
2488 for line
in finfile
:
2489 infilelist
.append(line
.strip());
2490 infilelist
= list(filter(None, infilelist
));
2492 if(isinstance(infiles
, (list, tuple, ))):
2493 infilelist
= list(filter(None, infiles
));
2494 elif(isinstance(infiles
, (str, ))):
2495 infilelist
= list(filter(None, [infiles
]));
2497 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
2499 GetDirList
= ListDir(infilelist
, followlink
, False);
2507 inodetocatinode
= {};
2508 numfiles
= int(len(GetDirList
));
2509 AppendFileHeader(catfp
, numfiles
, checksumtype
, formatspecs
);
2510 for curfname
in GetDirList
:
2511 if(re
.findall("^[.|/]", curfname
)):
2514 fname
= "./"+curfname
;
2516 VerbosePrintOut(fname
);
2517 if(not followlink
or followlink
is None):
2518 fstatinfo
= os
.lstat(fname
);
2520 fstatinfo
= os
.stat(fname
);
2521 fpremode
= fstatinfo
.st_mode
;
2522 finode
= fstatinfo
.st_ino
;
2523 flinkcount
= fstatinfo
.st_nlink
;
2525 if(stat
.S_ISREG(fpremode
)):
2527 elif(stat
.S_ISLNK(fpremode
)):
2529 elif(stat
.S_ISCHR(fpremode
)):
2531 elif(stat
.S_ISBLK(fpremode
)):
2533 elif(stat
.S_ISDIR(fpremode
)):
2535 elif(stat
.S_ISFIFO(fpremode
)):
2537 elif(stat
.S_ISSOCK(fpremode
)):
2539 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
2541 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
2543 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
2548 fcurfid
= format(int(curfid
), 'x').lower();
2549 if(not followlink
and finode
!=0):
2551 if(finode
in inodelist
):
2553 flinkname
= inodetofile
[finode
];
2555 fcurinode
= format(int(finode
), 'x').lower();
2557 fcurinode
= format(int(inodetocatinode
[finode
]), 'x').lower();
2558 if(finode
not in inodelist
):
2559 inodelist
.append(finode
);
2560 inodetofile
.update({finode
: fname
});
2561 inodetocatinode
.update({finode
: curinode
});
2563 fcurinode
= format(int(finode
), 'x').lower();
2565 fcurinode
= format(int(curinode
), 'x').lower();
2566 curinode
= curinode
+ 1;
2568 fcurinode
= format(int(curinode
), 'x').lower();
2569 curinode
= curinode
+ 1;
2570 curfid
= curfid
+ 1;
2572 flinkname
= os
.readlink(fname
);
2573 fdev
= fstatinfo
.st_dev
;
2574 getfdev
= GetDevMajorMinor(fdev
);
2575 fdev_minor
= getfdev
[0];
2576 fdev_major
= getfdev
[1];
2577 frdev
= fstatinfo
.st_dev
;
2578 if(hasattr(fstatinfo
, "st_rdev")):
2579 frdev
= fstatinfo
.st_rdev
;
2581 frdev
= fstatinfo
.st_dev
;
2582 getfrdev
= GetDevMajorMinor(frdev
);
2583 frdev_minor
= getfrdev
[0];
2584 frdev_major
= getfrdev
[1];
2585 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
2586 fsize
= format(int("0"), 'x').lower();
2587 elif(ftype
==0 or ftype
==7):
2588 fsize
= format(int(fstatinfo
.st_size
), 'x').lower();
2590 fsize
= format(int(fstatinfo
.st_size
)).lower();
2591 fatime
= format(int(fstatinfo
.st_atime
), 'x').lower();
2592 fmtime
= format(int(fstatinfo
.st_mtime
), 'x').lower();
2593 fctime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
2594 if(hasattr(fstatinfo
, "st_birthtime")):
2595 fbtime
= format(int(fstatinfo
.st_birthtime
), 'x').lower();
2597 fbtime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
2598 fmode
= format(int(fstatinfo
.st_mode
), 'x').lower();
2599 fchmode
= format(int(stat
.S_IMODE(fstatinfo
.st_mode
)), 'x').lower();
2600 ftypemod
= format(int(stat
.S_IFMT(fstatinfo
.st_mode
)), 'x').lower();
2601 fuid
= format(int(fstatinfo
.st_uid
), 'x').lower();
2602 fgid
= format(int(fstatinfo
.st_gid
), 'x').lower();
2607 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
2608 funame
= userinfo
.pw_name
;
2617 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
2618 fgname
= groupinfo
.gr_name
;
2623 fdev_minor
= format(int(fdev_minor
), 'x').lower();
2624 fdev_major
= format(int(fdev_major
), 'x').lower();
2625 frdev_minor
= format(int(frdev_minor
), 'x').lower();
2626 frdev_major
= format(int(frdev_major
), 'x').lower();
2627 finode
= format(int(finode
), 'x').lower();
2628 flinkcount
= format(int(flinkcount
), 'x').lower();
2629 if(hasattr(fstatinfo
, "st_file_attributes")):
2630 fwinattributes
= format(int(fstatinfo
.st_file_attributes
), 'x').lower();
2632 fwinattributes
= format(int(0), 'x').lower();
2634 fcsize
= format(int(0), 'x').lower();
2635 fcontents
= BytesIO();
2636 if(ftype
==0 or ftype
==7):
2637 with
open(fname
, "rb") as fpc
:
2638 shutil
.copyfileobj(fpc
, fcontents
);
2639 if(not compresswholefile
):
2640 fcontents
.seek(0, 2);
2641 ucfsize
= fcontents
.tell();
2642 fcontents
.seek(0, 0);
2643 cfcontents
= BytesIO();
2644 shutil
.copyfileobj(fcontents
, cfcontents
);
2645 cfcontents
.seek(0, 0);
2646 cfcontents
= CompressArchiveFile(cfcontents
, compression
, compressionlevel
, formatspecs
);
2647 cfcontents
.seek(0, 2);
2648 cfsize
= cfcontents
.tell();
2649 if(ucfsize
> cfsize
):
2650 fcsize
= format(int(cfsize
), 'x').lower();
2651 fcompression
= compression
;
2653 fcontents
= cfcontents
;
2654 if(fcompression
=="auto" or fcompression
=="none"):
2656 if(followlink
and (ftype
==1 or ftype
==2)):
2657 flstatinfo
= os
.stat(flinkname
);
2658 with
open(flinkname
, "rb") as fpc
:
2659 shutil
.copyfileobj(fpc
, fcontents
);
2660 if(not compresswholefile
):
2661 fcontents
.seek(0, 2);
2662 ucfsize
= fcontents
.tell();
2663 fcontents
.seek(0, 0);
2664 cfcontents
= BytesIO();
2665 shutil
.copyfileobj(fcontents
, cfcontents
);
2666 cfcontents
.seek(0, 0);
2667 cfcontents
= CompressArchiveFile(cfcontents
, compression
, compressionlevel
, formatspecs
);
2668 cfcontents
.seek(0, 2);
2669 cfsize
= cfcontents
.tell();
2670 if(ucfsize
> cfsize
):
2671 fcsize
= format(int(cfsize
), 'x').lower();
2672 fcompression
= compression
;
2674 fcontents
= cfcontents
;
2675 fcontents
.seek(0, 0);
2676 ftypehex
= format(ftype
, 'x').lower();
2677 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fcompression
, fcsize
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
];
2678 AppendFileHeaderWithContent(catfp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
);
2681 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
2682 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2683 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
2686 os
.fsync(catfp
.fileno());
2687 except io
.UnsupportedOperation
:
2689 except AttributeError:
2691 except OSError as e
:
2695 if(hasattr(sys
.stdout
, "buffer")):
2696 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2698 shutil
.copyfileobj(catfp
, sys
.stdout
);
2699 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2700 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
2702 upload_file_to_internet_file(catfp
, outfile
);
2710 create_alias_function("Pack", __file_format_name__
, "", PackArchiveFile
);
2712 if(hasattr(shutil
, "register_archive_format")):
2713 def PackArchiveFileFunc(archive_name
, source_dir
, **kwargs
):
2714 return PackArchiveFile(source_dir
, archive_name
, False, "auto", True, None, False, "crc32", [], __file_format_delimiter__
, False, False);
2715 create_alias_function("Pack", __file_format_name__
, "Func", PackArchiveFileFunc
);
2717 def PackArchiveFileFromDirList(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2718 return PackArchiveFile(infiles
, outfile
, dirlistfromtxt
, compression
, compresswholefile
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
2720 create_alias_function("Pack", __file_format_name__
, "FromDirList", PackArchiveFileFromDirList
);
2722 def PackArchiveFileFromTarFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2723 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2724 outfile
= RemoveWindowsPath(outfile
);
2725 checksumtype
= checksumtype
.lower();
2726 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2727 checksumtype
="crc32";
2728 if(checksumtype
=="none"):
2730 if(not compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2731 compression
= "auto";
2732 if(compression
not in compressionlist
and compression
is None):
2733 compression
= "auto";
2735 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2736 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2737 if(os
.path
.exists(outfile
)):
2740 except OSError as e
:
2745 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2747 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2750 fbasename
= os
.path
.splitext(outfile
)[0];
2751 fextname
= os
.path
.splitext(outfile
)[1];
2752 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
);
2753 catver
= formatspecs
[6];
2754 fileheaderver
= str(int(catver
.replace(".", "")));
2760 inodetocatinode
= {};
2763 if(hasattr(sys
.stdin
, "buffer")):
2764 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
2766 shutil
.copyfileobj(sys
.stdin
, infile
);
2771 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
2772 infile
= download_file_from_internet_file(infile
);
2777 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2779 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
2781 if(not tarfile
.is_tarfile(infile
)):
2783 except AttributeError:
2784 if(not is_tarfile(infile
)):
2789 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2790 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
2792 tarfp
= tarfile
.open(infile
, "r");
2793 except FileNotFoundError
:
2795 numfiles
= int(len(tarfp
.getmembers()));
2796 AppendFileHeader(catfp
, numfiles
, checksumtype
, formatspecs
);
2797 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
2798 if(re
.findall("^[.|/]", member
.name
)):
2799 fname
= member
.name
;
2801 fname
= "./"+member
.name
;
2803 VerbosePrintOut(fname
);
2804 fpremode
= member
.mode
;
2805 ffullmode
= member
.mode
;
2809 ffullmode
= member
.mode
+ stat
.S_IFREG
;
2811 elif(member
.isdev()):
2812 ffullmode
= member
.mode
;
2814 elif(member
.islnk()):
2815 ffullmode
= member
.mode
+ stat
.S_IFREG
;
2817 elif(member
.issym()):
2818 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
2820 elif(member
.ischr()):
2821 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
2823 elif(member
.isblk()):
2824 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
2826 elif(member
.isdir()):
2827 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
2829 elif(member
.isfifo()):
2830 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
2832 elif(member
.issparse()):
2833 ffullmode
= member
.mode
;
2836 ffullmode
= member
.mode
;
2839 fcurfid
= format(int(curfid
), 'x').lower();
2840 fcurinode
= format(int(curfid
), 'x').lower();
2841 curfid
= curfid
+ 1;
2843 flinkname
= member
.linkname
;
2844 fdev_minor
= format(int(member
.devminor
), 'x').lower();
2845 fdev_major
= format(int(member
.devmajor
), 'x').lower();
2846 frdev_minor
= format(int(member
.devminor
), 'x').lower();
2847 frdev_major
= format(int(member
.devmajor
), 'x').lower();
2848 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
2849 fsize
= format(int("0"), 'x').lower();
2850 elif(ftype
==0 or ftype
==7):
2851 fsize
= format(int(member
.size
), 'x').lower();
2853 fsize
= format(int(member
.size
), 'x').lower();
2854 fatime
= format(int(member
.mtime
), 'x').lower();
2855 fmtime
= format(int(member
.mtime
), 'x').lower();
2856 fctime
= format(int(member
.mtime
), 'x').lower();
2857 fbtime
= format(int(member
.mtime
), 'x').lower();
2858 fmode
= format(int(ffullmode
), 'x').lower();
2859 fchmode
= format(int(stat
.S_IMODE(ffullmode
)), 'x').lower();
2860 ftypemod
= format(int(stat
.S_IFMT(ffullmode
)), 'x').lower();
2861 fuid
= format(int(member
.uid
), 'x').lower();
2862 fgid
= format(int(member
.gid
), 'x').lower();
2863 funame
= member
.uname
;
2864 fgname
= member
.gname
;
2865 flinkcount
= format(int(flinkcount
), 'x').lower();
2866 fwinattributes
= format(int(0), 'x').lower();
2868 fcsize
= format(int(0), 'x').lower();
2869 fcontents
= BytesIO();
2870 if(ftype
==0 or ftype
==7):
2871 with tarfp
.extractfile(member
) as fpc
:
2872 shutil
.copyfileobj(fpc
, fcontents
);
2873 if(not compresswholefile
):
2874 fcontents
.seek(0, 2);
2875 ucfsize
= fcontents
.tell();
2876 fcontents
.seek(0, 0);
2877 cfcontents
= BytesIO();
2878 shutil
.copyfileobj(fcontents
, cfcontents
);
2879 cfcontents
.seek(0, 0);
2880 cfcontents
= CompressArchiveFile(cfcontents
, compression
, compressionlevel
, formatspecs
);
2881 cfcontents
.seek(0, 2);
2882 cfsize
= cfcontents
.tell();
2883 if(ucfsize
> cfsize
):
2884 fcsize
= format(int(cfsize
), 'x').lower();
2885 fcompression
= compression
;
2887 fcontents
= cfcontents
;
2888 if(fcompression
=="auto" or fcompression
=="none"):
2890 fcontents
.seek(0, 0);
2891 ftypehex
= format(ftype
, 'x').lower();
2892 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fcompression
, fcsize
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
];
2893 AppendFileHeaderWithContent(catfp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
);
2896 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
2897 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2898 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
2901 os
.fsync(catfp
.fileno());
2902 except io
.UnsupportedOperation
:
2904 except AttributeError:
2906 except OSError as e
:
2910 if(hasattr(sys
.stdout
, "buffer")):
2911 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2913 shutil
.copyfileobj(catfp
, sys
.stdout
);
2914 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2915 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
2917 upload_file_to_internet_file(catfp
, outfile
);
2925 create_alias_function("Pack", __file_format_name__
, "FromTarFile", PackArchiveFileFromTarFile
);
2927 def PackArchiveFileFromZipFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2928 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2929 outfile
= RemoveWindowsPath(outfile
);
2930 checksumtype
= checksumtype
.lower();
2931 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2932 checksumtype
="crc32";
2933 if(checksumtype
=="none"):
2935 if(not compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2936 compression
= "auto";
2937 if(compression
not in compressionlist
and compression
is None):
2938 compression
= "auto";
2940 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2941 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2942 if(os
.path
.exists(outfile
)):
2945 except OSError as e
:
2950 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2952 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2955 fbasename
= os
.path
.splitext(outfile
)[0];
2956 fextname
= os
.path
.splitext(outfile
)[1];
2957 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
);
2958 catver
= formatspecs
[6];
2959 fileheaderver
= str(int(catver
.replace(".", "")));
2965 inodetocatinode
= {};
2968 if(hasattr(sys
.stdin
, "buffer")):
2969 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
2971 shutil
.copyfileobj(sys
.stdin
, infile
);
2976 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
2977 infile
= download_file_from_internet_file(infile
);
2982 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2986 if(not zipfile
.is_zipfile(infile
)):
2989 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
2990 except FileNotFoundError
:
2992 ziptest
= zipfp
.testzip();
2994 VerbosePrintOut("Bad file found!");
2995 numfiles
= int(len(zipfp
.infolist()));
2996 AppendFileHeader(catfp
, numfiles
, checksumtype
, formatspecs
);
2997 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
2998 if(re
.findall("^[.|/]", member
.filename
)):
2999 fname
= member
.filename
;
3001 fname
= "./"+member
.filename
;
3002 zipinfo
= zipfp
.getinfo(member
.filename
);
3004 VerbosePrintOut(fname
);
3005 if(not member
.is_dir()):
3006 fpremode
= int(stat
.S_IFREG
+ 438);
3007 elif(member
.is_dir()):
3008 fpremode
= int(stat
.S_IFDIR
+ 511);
3011 if(not member
.is_dir()):
3013 elif(member
.is_dir()):
3016 fcurfid
= format(int(curfid
), 'x').lower();
3017 fcurinode
= format(int(curfid
), 'x').lower();
3018 curfid
= curfid
+ 1;
3019 fdev_minor
= format(int(0), 'x').lower();
3020 fdev_major
= format(int(0), 'x').lower();
3021 frdev_minor
= format(int(0), 'x').lower();
3022 frdev_major
= format(int(0), 'x').lower();
3024 fsize
= format(int("0"), 'x').lower();
3026 fsize
= format(int(member
.file_size
), 'x').lower();
3028 fsize
= format(int(member
.file_size
), 'x').lower();
3029 fatime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
3030 fmtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
3031 fctime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
3032 fbtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
3033 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
3034 fwinattributes
= format(int(zipinfo
.external_attr
), 'x').lower();
3035 if(not member
.is_dir()):
3036 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
3037 fchmode
= stat
.S_IMODE(fmode
);
3038 ftypemod
= stat
.S_IFMT(fmode
);
3039 elif(member
.is_dir()):
3040 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
3041 fchmode
= stat
.S_IMODE(fmode
);
3042 ftypemod
= stat
.S_IFMT(fmode
);
3043 elif(zipinfo
.create_system
==3):
3044 fwinattributes
= format(int(0), 'x').lower();
3045 fmode
= format(int(zipinfo
.external_attr
), 'x').lower();
3046 fchmode
= stat
.S_IMODE(fmode
);
3047 ftypemod
= stat
.S_IFMT(fmode
);
3049 fwinattributes
= format(int(0), 'x').lower();
3050 if(not member
.is_dir()):
3051 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
3052 fchmode
= stat
.S_IMODE(fmode
);
3053 ftypemod
= stat
.S_IFMT(fmode
);
3054 elif(member
.is_dir()):
3055 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
3056 fchmode
= stat
.S_IMODE(fmode
);
3057 ftypemod
= stat
.S_IFMT(fmode
);
3059 fcsize
= format(int(0), 'x').lower();
3061 fuid
= format(int(os
.getuid()), 'x').lower();
3062 except AttributeError:
3063 fuid
= format(int(0), 'x').lower();
3065 fuid
= format(int(0), 'x').lower();
3067 fgid
= format(int(os
.getgid()), 'x').lower();
3068 except AttributeError:
3069 fgid
= format(int(0), 'x').lower();
3071 fgid
= format(int(0), 'x').lower();
3075 userinfo
= pwd
.getpwuid(os
.getuid());
3076 funame
= userinfo
.pw_name
;
3079 except AttributeError:
3087 groupinfo
= grp
.getgrgid(os
.getgid());
3088 fgname
= groupinfo
.gr_name
;
3091 except AttributeError:
3095 fcontents
= BytesIO();
3097 fcontents
.write(zipfp
.read(member
.filename
));
3098 if(not compresswholefile
):
3099 fcontents
.seek(0, 2);
3100 ucfsize
= fcontents
.tell();
3101 fcontents
.seek(0, 0);
3102 cfcontents
= BytesIO();
3103 shutil
.copyfileobj(fcontents
, cfcontents
);
3104 cfcontents
.seek(0, 0);
3105 cfcontents
= CompressArchiveFile(cfcontents
, compression
, compressionlevel
, formatspecs
);
3106 cfcontents
.seek(0, 2);
3107 cfsize
= cfcontents
.tell();
3108 if(ucfsize
> cfsize
):
3109 fcsize
= format(int(cfsize
), 'x').lower();
3110 fcompression
= compression
;
3112 fcontents
= cfcontents
;
3113 if(fcompression
=="auto" or fcompression
=="none"):
3115 fcontents
.seek(0, 0);
3116 ftypehex
= format(ftype
, 'x').lower();
3117 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fcompression
, fcsize
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
];
3118 AppendFileHeaderWithContent(catfp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
);
3121 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
3122 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3123 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
3126 os
.fsync(catfp
.fileno());
3127 except io
.UnsupportedOperation
:
3129 except AttributeError:
3131 except OSError as e
:
3135 if(hasattr(sys
.stdout
, "buffer")):
3136 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
3138 shutil
.copyfileobj(catfp
, sys
.stdout
);
3139 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3140 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
3142 upload_file_to_internet_file(catfp
, outfile
);
3150 create_alias_function("Pack", __file_format_name__
, "FromZipFile", PackArchiveFileFromZipFile
);
3152 if(not rarfile_support
):
3153 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
3156 if(rarfile_support
):
3157 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
3158 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3159 outfile
= RemoveWindowsPath(outfile
);
3160 checksumtype
= checksumtype
.lower();
3161 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
3162 checksumtype
="crc32";
3163 if(checksumtype
=="none"):
3165 if(not compression
or compression
=="catfile" or compression
==formatspecs
[2]):
3166 compression
= "auto";
3167 if(compression
not in compressionlist
and compression
is None):
3168 compression
= "auto";
3170 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
3171 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3172 if(os
.path
.exists(outfile
)):
3175 except OSError as e
:
3180 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3182 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3185 fbasename
= os
.path
.splitext(outfile
)[0];
3186 fextname
= os
.path
.splitext(outfile
)[1];
3187 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
);
3188 catver
= formatspecs
[6];
3189 fileheaderver
= str(int(catver
.replace(".", "")));
3195 inodetocatinode
= {};
3196 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
3198 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
3200 rarfp
= rarfile
.RarFile(infile
, "r");
3201 rartest
= rarfp
.testrar();
3203 VerbosePrintOut("Bad file found!");
3204 numfiles
= int(len(rarfp
.infolist()));
3205 AppendFileHeader(catfp
, numfiles
, checksumtype
, formatspecs
);
3208 os
.fsync(catfp
.fileno());
3209 except io
.UnsupportedOperation
:
3211 except AttributeError:
3213 except OSError as e
:
3215 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
3218 if(member
.host_os
==rarfile
.RAR_OS_UNIX
):
3221 member
.external_attr
3223 except AttributeError:
3225 elif(member
.host_os
==rarfile
.RAR_OS_WIN32
):
3228 member
.external_attr
3230 except AttributeError:
3235 if(re
.findall("^[.|/]", member
.filename
)):
3236 fname
= member
.filename
;
3238 fname
= "./"+member
.filename
;
3239 rarinfo
= rarfp
.getinfo(member
.filename
);
3241 VerbosePrintOut(fname
);
3242 if(is_unix
and member
.external_attr
!=0):
3243 fpremode
= int(member
.external_attr
);
3244 elif(member
.is_file()):
3245 fpremode
= int(stat
.S_IFREG
+ 438);
3246 elif(member
.is_symlink()):
3247 fpremode
= int(stat
.S_IFLNK
+ 438);
3248 elif(member
.is_dir()):
3249 fpremode
= int(stat
.S_IFDIR
+ 511);
3250 if(is_windows
and member
.external_attr
!=0):
3251 fwinattributes
= format(int(member
.external_attr
), 'x').lower();
3253 fwinattributes
= format(int(0), 'x').lower();
3255 fcsize
= format(int(0), 'x').lower();
3258 if(member
.is_file()):
3260 elif(member
.is_symlink()):
3262 elif(member
.is_dir()):
3266 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
3267 fcurfid
= format(int(curfid
), 'x').lower();
3268 fcurinode
= format(int(curfid
), 'x').lower();
3269 curfid
= curfid
+ 1;
3270 fdev_minor
= format(int(0), 'x').lower();
3271 fdev_major
= format(int(0), 'x').lower();
3272 frdev_minor
= format(int(0), 'x').lower();
3273 frdev_major
= format(int(0), 'x').lower();
3275 fsize
= format(int("0"), 'x').lower();
3277 fsize
= format(int(member
.file_size
), 'x').lower();
3279 fsize
= format(int(member
.file_size
), 'x').lower();
3282 fatime
= format(int(member
.atime
.timestamp()), 'x').lower();
3284 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3285 except AttributeError:
3286 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3287 fmtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3290 fctime
= format(int(member
.ctime
.timestamp()), 'x').lower();
3292 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3293 except AttributeError:
3294 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3295 fbtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3296 if(is_unix
and member
.external_attr
!=0):
3297 fmode
= format(int(member
.external_attr
), 'x').lower();
3298 fchmode
= format(int(stat
.S_IMODE(member
.external_attr
)), 'x').lower();
3299 ftypemod
= format(int(stat
.S_IFMT(member
.external_attr
)), 'x').lower();
3300 elif(member
.is_file()):
3301 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
3302 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
3303 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
3304 elif(member
.is_symlink()):
3305 fmode
= format(int(stat
.S_IFLNK
+ 438), 'x').lower();
3306 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
3307 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
3308 elif(member
.is_dir()):
3309 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
3310 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3311 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3313 fuid
= format(int(os
.getuid()), 'x').lower();
3314 except AttributeError:
3315 fuid
= format(int(0), 'x').lower();
3317 fuid
= format(int(0), 'x').lower();
3319 fgid
= format(int(os
.getgid()), 'x').lower();
3320 except AttributeError:
3321 fgid
= format(int(0), 'x').lower();
3323 fgid
= format(int(0), 'x').lower();
3327 userinfo
= pwd
.getpwuid(os
.getuid());
3328 funame
= userinfo
.pw_name
;
3331 except AttributeError:
3339 groupinfo
= grp
.getgrgid(os
.getgid());
3340 fgname
= groupinfo
.gr_name
;
3343 except AttributeError:
3347 fcontents
= BytesIO();
3349 fcontents
.write(rarfp
.read(member
.filename
));
3350 if(not compresswholefile
):
3351 fcontents
.seek(0, 2);
3352 ucfsize
= fcontents
.tell();
3353 fcontents
.seek(0, 0);
3354 cfcontents
= BytesIO();
3355 shutil
.copyfileobj(fcontents
, cfcontents
);
3356 cfcontents
.seek(0, 0);
3357 cfcontents
= CompressArchiveFile(cfcontents
, compression
, compressionlevel
, formatspecs
);
3358 cfcontents
.seek(0, 2);
3359 cfsize
= cfcontents
.tell();
3360 if(ucfsize
> cfsize
):
3361 fcsize
= format(int(cfsize
), 'x').lower();
3362 fcompression
= compression
;
3364 fcontents
= cfcontents
;
3365 if(fcompression
=="auto" or fcompression
=="none"):
3367 fcontents
.seek(0, 0);
3368 ftypehex
= format(ftype
, 'x').lower();
3369 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fcompression
, fcsize
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
];
3370 AppendFileHeaderWithContent(catfp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
);
3373 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
3374 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3375 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
)
3378 os
.fsync(catfp
.fileno());
3379 except io
.UnsupportedOperation
:
3381 except AttributeError:
3383 except OSError as e
:
3387 if(hasattr(sys
.stdout
, "buffer")):
3388 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
3390 shutil
.copyfileobj(catfp
, sys
.stdout
);
3391 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3392 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
3394 upload_file_to_internet_file(catfp
, outfile
);
3402 create_alias_function("Pack", __file_format_name__
, "FromRarFile", PackArchiveFileFromRarFile
);
3404 if(not py7zr_support
):
3405 def PackArchiveFileFromSevenZipFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
3409 def PackArchiveFileFromSevenZipFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
3410 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3411 outfile
= RemoveWindowsPath(outfile
);
3412 checksumtype
= checksumtype
.lower();
3413 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
3414 checksumtype
="crc32";
3415 if(checksumtype
=="none"):
3417 if(not compression
or compression
=="catfile" or compression
==formatspecs
[2]):
3418 compression
= "auto";
3419 if(compression
not in compressionlist
and compression
is None):
3420 compression
= "auto";
3422 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
3423 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3424 if(os
.path
.exists(outfile
)):
3427 except OSError as e
:
3432 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3434 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3437 fbasename
= os
.path
.splitext(outfile
)[0];
3438 fextname
= os
.path
.splitext(outfile
)[1];
3439 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
);
3440 catver
= formatspecs
[6];
3441 fileheaderver
= str(int(catver
.replace(".", "")));
3447 inodetocatinode
= {};
3448 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
3450 szpfp
= py7zr
.SevenZipFile(infile
, mode
="r");
3451 file_content
= szpfp
.readall();
3452 #sztest = szpfp.testzip();
3453 sztestalt
= szpfp
.test();
3455 VerbosePrintOut("Bad file found!");
3456 numfiles
= int(len(szpfp
.list()));
3457 AppendFileHeader(catfp
, numfiles
, checksumtype
, formatspecs
);
3458 for member
in sorted(szpfp
.list(), key
=lambda x
: x
.filename
):
3459 if(re
.findall("^[.|/]", member
.filename
)):
3460 fname
= member
.filename
;
3462 fname
= "./"+member
.filename
;
3464 VerbosePrintOut(fname
);
3465 if(not member
.is_directory
):
3466 fpremode
= int(stat
.S_IFREG
+ 438);
3467 elif(member
.is_directory
):
3468 fpremode
= int(stat
.S_IFDIR
+ 511);
3469 fwinattributes
= format(int(0), 'x').lower();
3471 fcsize
= format(int(0), 'x').lower();
3474 if(member
.is_directory
):
3479 fcurfid
= format(int(curfid
), 'x').lower();
3480 fcurinode
= format(int(curfid
), 'x').lower();
3481 curfid
= curfid
+ 1;
3482 fdev_minor
= format(int(0), 'x').lower();
3483 fdev_major
= format(int(0), 'x').lower();
3484 frdev_minor
= format(int(0), 'x').lower();
3485 frdev_major
= format(int(0), 'x').lower();
3487 fsize
= format(int("0"), 'x').lower();
3488 fatime
= format(int(member
.creationtime
.timestamp()), 'x').lower();
3489 fmtime
= format(int(member
.creationtime
.timestamp()), 'x').lower();
3490 fctime
= format(int(member
.creationtime
.timestamp()), 'x').lower();
3491 fbtime
= format(int(member
.creationtime
.timestamp()), 'x').lower();
3492 if(member
.is_directory
):
3493 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
3494 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3495 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3497 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
3498 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
3499 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
3501 fuid
= format(int(os
.getuid()), 'x').lower();
3502 except AttributeError:
3503 fuid
= format(int(0), 'x').lower();
3505 fuid
= format(int(0), 'x').lower();
3507 fgid
= format(int(os
.getgid()), 'x').lower();
3508 except AttributeError:
3509 fgid
= format(int(0), 'x').lower();
3511 fgid
= format(int(0), 'x').lower();
3515 userinfo
= pwd
.getpwuid(os
.getuid());
3516 funame
= userinfo
.pw_name
;
3519 except AttributeError:
3527 groupinfo
= grp
.getgrgid(os
.getgid());
3528 fgname
= groupinfo
.gr_name
;
3531 except AttributeError:
3535 fcontents
= BytesIO();
3537 fcontents
.write(file_content
[member
.filename
].read());
3538 fsize
= format(fcontents
.tell(), 'x').lower();
3539 file_content
[member
.filename
].close();
3540 if(not compresswholefile
):
3541 fcontents
.seek(0, 2);
3542 ucfsize
= fcontents
.tell();
3543 fcontents
.seek(0, 0);
3544 cfcontents
= BytesIO();
3545 shutil
.copyfileobj(fcontents
, cfcontents
);
3546 cfcontents
.seek(0, 0);
3547 cfcontents
= CompressArchiveFile(cfcontents
, compression
, compressionlevel
, formatspecs
);
3548 cfcontents
.seek(0, 2);
3549 cfsize
= cfcontents
.tell();
3550 if(ucfsize
> cfsize
):
3551 fcsize
= format(int(cfsize
), 'x').lower();
3552 fcompression
= compression
;
3554 fcontents
= cfcontents
;
3555 if(fcompression
=="auto" or fcompression
=="none"):
3557 fcontents
.seek(0, 0);
3558 ftypehex
= format(ftype
, 'x').lower();
3559 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fcompression
, fcsize
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
];
3560 AppendFileHeaderWithContent(catfp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
);
3563 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
3564 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3565 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
)
3568 os
.fsync(catfp
.fileno());
3569 except io
.UnsupportedOperation
:
3571 except AttributeError:
3573 except OSError as e
:
3577 if(hasattr(sys
.stdout
, "buffer")):
3578 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
3580 shutil
.copyfileobj(catfp
, sys
.stdout
);
3581 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3582 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
3584 upload_file_to_internet_file(catfp
, outfile
);
3592 create_alias_function("Pack", __file_format_name__
, "FromSevenZipFile", PackArchiveFileFromSevenZipFile
);
3594 def PackArchiveFileFromInFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
3595 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
3597 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
3598 if(checkcompressfile
=="tarfile"):
3599 return PackArchiveFileFromTarFile(infile
, outfile
, compression
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
3600 elif(checkcompressfile
=="zipfile"):
3601 return PackArchiveFileFromZipFile(infile
, outfile
, compression
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
3602 elif(checkcompressfile
=="catfile"):
3603 return RePackArchiveFile(infile
, outfile
, compression
, compressionlevel
, False, 0, 0, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
3604 elif(rarfile_support
and checkcompressfile
=="rarfile"):
3605 return PackArchiveFileFromRarFile(infile
, outfile
, compression
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
3606 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
3607 return PackArchiveFileFromSevenZipFile(infile
, outfile
, compression
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
3612 create_alias_function("Pack", __file_format_name__
, "FromInFile", PackArchiveFileFromInFile
);
3614 def ArchiveFileSeekToFileNum(infile
, seekto
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3615 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3618 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3619 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
3620 if(checkcompressfile
=="tarfile"):
3621 return TarFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3622 if(checkcompressfile
=="zipfile"):
3623 return ZipFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3624 if(rarfile_support
and checkcompressfile
=="rarfile"):
3625 return RarFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3626 if(py7zr_support
and checkcompressfile
=="7zipfile"):
3627 return SevenZipFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3628 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3635 if(hasattr(sys
.stdin
, "buffer")):
3636 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
3638 shutil
.copyfileobj(sys
.stdin
, catfp
);
3640 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3644 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
3645 catfp
= download_file_from_internet_file(infile
);
3647 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3652 infile
= RemoveWindowsPath(infile
);
3653 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
3654 if(checkcompressfile
=="tarfile"):
3655 return TarFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3656 if(checkcompressfile
=="zipfile"):
3657 return ZipFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3658 if(rarfile_support
and checkcompressfile
=="rarfile"):
3659 return RarFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3660 if(py7zr_support
and checkcompressfile
=="7zipfile"):
3661 return SevenZipFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3662 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3664 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
3665 if(not compresscheck
):
3666 fextname
= os
.path
.splitext(infile
)[1];
3667 if(fextname
==".gz"):
3668 compresscheck
= "gzip";
3669 elif(fextname
==".bz2"):
3670 compresscheck
= "bzip2";
3671 elif(fextname
==".zst"):
3672 compresscheck
= "zstd";
3673 elif(fextname
==".lz4" or fextname
==".clz4"):
3674 compresscheck
= "lz4";
3675 elif(fextname
==".lzo" or fextname
==".lzop"):
3676 compresscheck
= "lzo";
3677 elif(fextname
==".lzma" or fextname
==".xz"):
3678 compresscheck
= "lzma";
3681 if(not compresscheck
):
3683 catfp
= UncompressFile(infile
, formatspecs
, "rb");
3688 SeekToEndOfFile(catfp);
3690 SeekToEndOfFile(catfp);
3691 CatSize = catfp.tell();
3692 CatSizeEnd = CatSize;
3700 curloc
= catfp
.tell();
3703 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
3705 catfp
.seek(curloc
, 0);
3706 catstring
= catheader
[0];
3707 catversion
= re
.findall(r
"([\d]+)$", catstring
);
3708 fprenumfiles
= catheader
[1];
3709 fnumfiles
= int(fprenumfiles
, 16);
3710 fprechecksumtype
= catheader
[2];
3711 fprechecksum
= catheader
[3];
3712 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
3713 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3714 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
3715 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
3716 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3717 fheadtell
= len(fileheader
);
3718 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
3719 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
3720 VerbosePrintOut("'" + str(fprechecksum
) + "' != " + "'" + str(catfileheadercshex
) + "'");
3722 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
3723 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
3724 if(seekto
>=fnumfiles
):
3725 seekto
= fnumfiles
- 1;
3731 prefhstart
= catfp
.tell();
3733 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3735 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
3736 if(len(preheaderdata
)==0):
3738 prefheadsize
= int(preheaderdata
[0], 16);
3739 prefnumfields
= int(preheaderdata
[1], 16);
3740 preftype
= int(preheaderdata
[2], 16);
3741 if(re
.findall("^[.|/]", preheaderdata
[3])):
3742 prefname
= preheaderdata
[3];
3744 prefname
= "./"+preheaderdata
[3];
3745 prefbasedir
= os
.path
.dirname(prefname
);
3746 preflinkname
= preheaderdata
[4];
3747 prefsize
= int(preheaderdata
[5], 16);
3748 prefatime
= int(preheaderdata
[6], 16);
3749 prefmtime
= int(preheaderdata
[7], 16);
3750 prefctime
= int(preheaderdata
[8], 16);
3751 prefbtime
= int(preheaderdata
[9], 16);
3752 prefmode
= int(preheaderdata
[10], 16);
3753 prefchmode
= stat
.S_IMODE(prefmode
);
3754 preftypemod
= stat
.S_IFMT(prefmode
);
3755 prefwinattributes
= int(preheaderdata
[11], 16);
3756 prefcompression
= preheaderdata
[12];
3757 prefcsize
= int(preheaderdata
[13], 16);
3758 prefuid
= int(preheaderdata
[14], 16);
3759 prefuname
= preheaderdata
[15];
3760 prefgid
= int(preheaderdata
[16], 16);
3761 prefgname
= preheaderdata
[17];
3762 fid
= int(preheaderdata
[18], 16);
3763 finode
= int(preheaderdata
[19], 16);
3764 flinkcount
= int(preheaderdata
[20], 16);
3765 prefdev_minor
= int(preheaderdata
[21], 16);
3766 prefdev_major
= int(preheaderdata
[22], 16);
3767 prefrdev_minor
= int(preheaderdata
[23], 16);
3768 prefrdev_major
= int(preheaderdata
[24], 16);
3769 prefextrasize
= int(preheaderdata
[25], 16);
3770 prefextrafields
= int(preheaderdata
[26], 16);
3771 extrafieldslist
= [];
3773 extraend
= extrastart
+ prefextrafields
;
3774 extrafieldslist
= [];
3775 if(extrastart
<extraend
):
3776 extrafieldslist
.append(preheaderdata
[extrastart
]);
3777 extrastart
= extrastart
+ 1;
3778 prefchecksumtype
= preheaderdata
[extrastart
].lower();
3779 prefcs
= preheaderdata
[extrastart
+ 1].lower();
3780 prefccs
= preheaderdata
[extrastart
+ 2].lower();
3782 hcmax
= len(preheaderdata
) - 2;
3785 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
3787 prenewfcs
= GetFileChecksum(hout
, preheaderdata
[-3].lower(), True, formatspecs
);
3788 if(prefcs
!=prenewfcs
and not skipchecksum
):
3789 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
3790 VerbosePrintOut("'" + str(prefcs
) + "' != " + "'" + str(prenewfcs
) + "'");
3792 valid_archive
= False;
3793 invalid_archive
= True;
3794 prefhend
= catfp
.tell() - 1;
3795 prefcontentstart
= catfp
.tell();
3797 pyhascontents
= False;
3799 if(prefcompression
):
3800 prefcontents
= catfp
.read(prefsize
);
3802 prefcontents
= catfp
.read(prefcsize
);
3803 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
3804 pyhascontents
= True;
3805 if(prefccs
!=prenewfccs
and not skipchecksum
):
3806 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
3807 VerbosePrintOut("'" + str(prefccs
) + "' != " + "'" + str(prenewfccs
) + "'");
3811 catfp
.seek(seekstart
, 0);
3813 catfheadsize
= int(preheaderdata
[0], 16);
3814 catfnumfields
= int(preheaderdata
[1], 16);
3815 catftype
= int(preheaderdata
[2], 16);
3816 if(re
.findall("^[.|/]", preheaderdata
[3])):
3817 catfname
= preheaderdata
[3];
3819 catfname
= "./"+preheaderdata
[3];
3820 catflinkname
= preheaderdata
[4];
3821 catfsize
= int(preheaderdata
[5], 16);
3822 catfbasedir
= os
.path
.dirname(catfname
);
3823 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
3825 catlist
.update({'catfp': catfp
});
3830 create_alias_function("", __file_format_name__
, "SeekToFileNum", ArchiveFileSeekToFileNum
);
3832 def ArchiveFileSeekToFileName(infile
, seekfile
=None, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3833 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3836 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3837 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
3838 if(checkcompressfile
=="tarfile"):
3839 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3840 if(checkcompressfile
=="zipfile"):
3841 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3842 if(rarfile_support
and checkcompressfile
=="rarfile"):
3843 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3844 if(py7zr_support
and checkcompressfile
=="7zipfile"):
3845 return SevenZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3846 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3853 if(hasattr(sys
.stdin
, "buffer")):
3854 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
3856 shutil
.copyfileobj(sys
.stdin
, catfp
);
3858 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3862 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
3863 catfp
= download_file_from_internet_file(infile
);
3864 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3870 infile
= RemoveWindowsPath(infile
);
3871 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
3872 if(checkcompressfile
=="tarfile"):
3873 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3874 if(checkcompressfile
=="zipfile"):
3875 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3876 if(rarfile_support
and checkcompressfile
=="rarfile"):
3877 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3878 if(py7zr_support
and checkcompressfile
=="7zipfile"):
3879 return SevenZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3880 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3882 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
3883 if(not compresscheck
):
3884 fextname
= os
.path
.splitext(infile
)[1];
3885 if(fextname
==".gz"):
3886 compresscheck
= "gzip";
3887 elif(fextname
==".bz2"):
3888 compresscheck
= "bzip2";
3889 elif(fextname
==".zst"):
3890 compresscheck
= "zstd";
3891 elif(fextname
==".lz4" or fextname
==".clz4"):
3892 compresscheck
= "lz4";
3893 elif(fextname
==".lzo" or fextname
==".lzop"):
3894 compresscheck
= "lzo";
3895 elif(fextname
==".lzma" or fextname
==".xz"):
3896 compresscheck
= "lzma";
3899 if(not compresscheck
):
3901 catfp
= UncompressFile(infile
, formatspecs
, "rb");
3906 SeekToEndOfFile(catfp);
3908 SeekToEndOfFile(catfp);
3909 CatSize = catfp.tell();
3910 CatSizeEnd = CatSize;
3918 curloc
= catfp
.tell();
3921 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
3923 catfp
.seek(curloc
, 0);
3924 catstring
= catheader
[0];
3925 catversion
= re
.findall(r
"([\d]+)$", catstring
);
3926 fprenumfiles
= catheader
[1];
3927 fnumfiles
= int(fprenumfiles
, 16);
3928 fprechecksumtype
= catheader
[2];
3929 fprechecksum
= catheader
[3];
3930 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
3931 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3932 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
3933 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
3934 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3935 fheadtell
= len(fileheader
);
3936 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
3937 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
3938 VerbosePrintOut("'" + str(fprechecksum
) + "' != " + "'" + str(catfileheadercshex
) + "'");
3940 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
3941 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
3942 seekto
= fnumfiles
- 1
3947 prefhstart
= catfp
.tell();
3949 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3951 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
3952 if(len(preheaderdata
)==0):
3954 prefheadsize
= int(preheaderdata
[0], 16);
3955 prefnumfields
= int(preheaderdata
[1], 16);
3956 preftype
= int(preheaderdata
[2], 16);
3957 if(re
.findall("^[.|/]", preheaderdata
[3])):
3958 prefname
= preheaderdata
[3];
3960 prefname
= "./"+preheaderdata
[3];
3961 prefbasedir
= os
.path
.dirname(prefname
);
3962 preflinkname
= preheaderdata
[4];
3963 prefsize
= int(preheaderdata
[5], 16);
3964 prefatime
= int(preheaderdata
[6], 16);
3965 prefmtime
= int(preheaderdata
[7], 16);
3966 prefctime
= int(preheaderdata
[8], 16);
3967 prefbtime
= int(preheaderdata
[9], 16);
3968 prefmode
= int(preheaderdata
[10], 16);
3969 prefchmode
= stat
.S_IMODE(prefmode
);
3970 preftypemod
= stat
.S_IFMT(prefmode
);
3971 prefwinattributes
= int(preheaderdata
[11], 16);
3972 prefcompression
= preheaderdata
[12];
3973 prefcsize
= int(preheaderdata
[13], 16);
3974 prefuid
= int(preheaderdata
[14], 16);
3975 prefuname
= preheaderdata
[15];
3976 prefgid
= int(preheaderdata
[16], 16);
3977 prefgname
= preheaderdata
[17];
3978 fid
= int(preheaderdata
[18], 16);
3979 finode
= int(preheaderdata
[19], 16);
3980 flinkcount
= int(preheaderdata
[20], 16);
3981 prefdev_minor
= int(preheaderdata
[21], 16);
3982 prefdev_major
= int(preheaderdata
[22], 16);
3983 prefrdev_minor
= int(preheaderdata
[23], 16);
3984 prefrdev_major
= int(preheaderdata
[24], 16);
3985 prefextrasize
= int(preheaderdata
[25], 16);
3986 prefextrafields
= int(preheaderdata
[26], 16);
3987 extrafieldslist
= [];
3989 extraend
= extrastart
+ prefextrafields
;
3990 extrafieldslist
= [];
3991 if(extrastart
<extraend
):
3992 extrafieldslist
.append(preheaderdata
[extrastart
]);
3993 extrastart
= extrastart
+ 1;
3994 prefchecksumtype
= preheaderdata
[extrastart
].lower();
3995 prefcs
= preheaderdata
[extrastart
+ 1].lower();
3996 prefccs
= preheaderdata
[extrastart
+ 2].lower();
3998 hcmax
= len(preheaderdata
) - 2;
4001 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
4003 prenewfcs
= GetFileChecksum(hout
, preheaderdata
[-3].lower(), True, formatspecs
);
4004 if(prefcs
!=prenewfcs
and not skipchecksum
):
4005 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
4006 VerbosePrintOut("'" + str(prefcs
) + "' != " + "'" + str(prenewfcs
) + "'");
4008 valid_archive
= False;
4009 invalid_archive
= True;
4010 prefhend
= catfp
.tell() - 1;
4011 prefcontentstart
= catfp
.tell();
4013 pyhascontents
= False;
4015 if(prefcompression
):
4016 prefcontents
= catfp
.read(prefsize
);
4018 prefcontents
= catfp
.read(prefcsize
);
4019 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
4020 pyhascontents
= True;
4021 if(prefccs
!=prenewfccs
and not skipchecksum
):
4022 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
4023 VerbosePrintOut("'" + str(prefccs
) + "' != " + "'" + str(prenewfccs
) + "'");
4028 prefname
= preheaderdata
[2];
4029 if(re
.findall("^[.|/]", preheaderdata
[2])):
4030 prefname
= preheaderdata
[2];
4032 prefname
= "./"+preheaderdata
[2];
4033 if(prefname
==seekfile
):
4036 catfp
.seek(seekstart
, 0);
4038 catfheadsize
= int(preheaderdata
[0], 16);
4039 catfnumfields
= int(preheaderdata
[1], 16);
4040 catftype
= int(preheaderdata
[2], 16);
4041 if(re
.findall("^[.|/]", preheaderdata
[3])):
4042 catfname
= preheaderdata
[3];
4044 catfname
= "./"+preheaderdata
[3];
4045 catflinkname
= preheaderdata
[4];
4046 catfsize
= int(preheaderdata
[5], 16);
4047 catfbasedir
= os
.path
.dirname(catfname
);
4049 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
4053 catlist
.update({'catfp': catfp
});
4058 create_alias_function("", __file_format_name__
, "SeekToFileName", ArchiveFileSeekToFileName
);
4060 def ArchiveFileValidate(infile
, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
4062 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
4063 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4066 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4067 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
4068 if(checkcompressfile
=="tarfile"):
4069 return TarFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4070 if(checkcompressfile
=="zipfile"):
4071 return ZipFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4072 if(rarfile_support
and checkcompressfile
=="rarfile"):
4073 return RarFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4074 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4075 return SevenZipFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4076 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4083 if(hasattr(sys
.stdin
, "buffer")):
4084 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
4086 shutil
.copyfileobj(sys
.stdin
, catfp
);
4088 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4092 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4093 catfp
= download_file_from_internet_file(infile
);
4094 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4100 infile
= RemoveWindowsPath(infile
);
4101 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
4102 if(checkcompressfile
=="tarfile"):
4103 return TarFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4104 if(checkcompressfile
=="zipfile"):
4105 return ZipFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4106 if(rarfile_support
and checkcompressfile
=="rarfile"):
4107 return RarFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4108 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4109 return SevenZipFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4110 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4112 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
4113 if(not compresscheck
):
4114 fextname
= os
.path
.splitext(infile
)[1];
4115 if(fextname
==".gz"):
4116 compresscheck
= "gzip";
4117 elif(fextname
==".bz2"):
4118 compresscheck
= "bzip2";
4119 elif(fextname
==".zst"):
4120 compresscheck
= "zstd";
4121 elif(fextname
==".lz4" or fextname
==".clz4"):
4122 compresscheck
= "lz4";
4123 elif(fextname
==".lzo" or fextname
==".lzop"):
4124 compresscheck
= "lzo";
4125 elif(fextname
==".lzma" or fextname
==".xz"):
4126 compresscheck
= "lzma";
4129 if(not compresscheck
):
4131 catfp
= UncompressFile(infile
, formatspecs
, "rb");
4136 SeekToEndOfFile(catfp);
4138 SeekToEndOfFile(catfp);
4139 CatSize = catfp.tell();
4140 CatSizeEnd = CatSize;
4148 curloc
= catfp
.tell();
4151 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
4153 catfp
.seek(curloc
, 0);
4154 catstring
= catheader
[0];
4155 catversion
= re
.findall(r
"([\d]+)$", catstring
);
4156 fprenumfiles
= catheader
[1];
4157 fnumfiles
= int(fprenumfiles
, 16);
4158 fprechecksumtype
= catheader
[2];
4159 fprechecksum
= catheader
[3];
4161 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
4162 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4163 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
4164 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
4165 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4166 valid_archive
= True;
4167 invalid_archive
= False;
4169 VerbosePrintOut(infile
);
4170 VerbosePrintOut("Number of Records " + str(fnumfiles
));
4171 if(fprechecksum
==catfileheadercshex
):
4173 VerbosePrintOut("File Header Checksum Passed at offset " + str(0));
4174 VerbosePrintOut("'" + str(fprechecksum
) + "' == " + "'" + str(catfileheadercshex
) + "'");
4177 VerbosePrintOut("File Header Checksum Failed at offset " + str(0));
4178 VerbosePrintOut("'" + str(fprechecksum
) + "' != " + "'" + str(catfileheadercshex
) + "'");
4179 valid_archive
= False;
4180 invalid_archive
= True;
4182 VerbosePrintOut("");
4183 while(il
<fnumfiles
):
4184 catfhstart
= catfp
.tell();
4186 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
4188 catheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
4189 if(len(catheaderdata
)==0):
4191 catfheadsize
= int(catheaderdata
[0], 16);
4192 catfnumfields
= int(catheaderdata
[1], 16);
4193 catftype
= int(catheaderdata
[2], 16);
4194 if(re
.findall("^[.|/]", catheaderdata
[3])):
4195 catfname
= catheaderdata
[3];
4197 catfname
= "./"+catheaderdata
[3];
4198 catfbasedir
= os
.path
.dirname(catfname
);
4199 catflinkname
= catheaderdata
[4];
4200 catfsize
= int(catheaderdata
[5], 16);
4201 catfatime
= int(catheaderdata
[6], 16);
4202 catfmtime
= int(catheaderdata
[7], 16);
4203 catfctime
= int(catheaderdata
[8], 16);
4204 catfbtime
= int(catheaderdata
[9], 16);
4205 catfmode
= int(catheaderdata
[10], 16);
4206 catfchmode
= stat
.S_IMODE(catfmode
);
4207 catftypemod
= stat
.S_IFMT(catfmode
);
4208 catfwinattributes
= int(catheaderdata
[11], 16);
4209 catfcompression
= catheaderdata
[12];
4210 catfcsize
= int(catheaderdata
[13], 16);
4211 catfuid
= int(catheaderdata
[14], 16);
4212 catfuname
= catheaderdata
[15];
4213 catfgid
= int(catheaderdata
[16], 16);
4214 catfgname
= catheaderdata
[17];
4215 fid
= int(catheaderdata
[18], 16);
4216 finode
= int(catheaderdata
[19], 16);
4217 flinkcount
= int(catheaderdata
[20], 16);
4218 catfdev_minor
= int(catheaderdata
[21], 16);
4219 catfdev_major
= int(catheaderdata
[22], 16);
4220 catfrdev_minor
= int(catheaderdata
[23], 16);
4221 catfrdev_major
= int(catheaderdata
[24], 16);
4222 catfextrasize
= int(catheaderdata
[25], 16);
4223 catfextrafields
= int(catheaderdata
[26], 16);
4224 extrafieldslist
= [];
4226 extraend
= extrastart
+ catfextrafields
;
4227 extrafieldslist
= [];
4228 if(extrastart
<extraend
):
4229 extrafieldslist
.append(catheaderdata
[extrastart
]);
4230 extrastart
= extrastart
+ 1;
4231 catfchecksumtype
= catheaderdata
[extrastart
].lower();
4232 catfcs
= catheaderdata
[extrastart
+ 1].lower();
4233 catfccs
= catheaderdata
[extrastart
+ 2].lower();
4235 hcmax
= len(catheaderdata
) - 2;
4238 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[5]);
4240 catnewfcs
= GetFileChecksum(hout
, catheaderdata
[-3].lower(), True, formatspecs
);
4242 VerbosePrintOut(catfname
);
4243 VerbosePrintOut("Record Number " + str(il
) + "; File ID " + str(fid
) + "; iNode Number " + str(finode
));
4244 if(catfcs
==catnewfcs
):
4246 VerbosePrintOut("File Header Checksum Passed at offset " + str(catfhstart
));
4247 VerbosePrintOut("'" + str(catfcs
) + "' == " + "'" + str(catnewfcs
) + "'");
4250 VerbosePrintOut("File Header Checksum Failed at offset " + str(catfhstart
));
4251 VerbosePrintOut("'" + str(catfcs
) + "' != " + "'" + str(catnewfcs
) + "'");
4252 valid_archive
= False;
4253 invalid_archive
= True;
4254 catfhend
= catfp
.tell() - 1;
4255 catfcontentstart
= catfp
.tell();
4257 pyhascontents
= False;
4259 if(catfcompression
=="none" or catfcompression
=="" or catfcompression
=="auto"):
4260 catfcontents
= catfp
.read(catfsize
);
4262 catfcontents
= catfp
.read(catfcsize
);
4263 catnewfccs
= GetFileChecksum(catfcontents
, catheaderdata
[-3].lower(), False, formatspecs
);
4264 pyhascontents
= True;
4265 if(catfccs
==catnewfccs
):
4267 VerbosePrintOut("File Content Checksum Passed at offset " + str(catfcontentstart
));
4268 VerbosePrintOut("'" + str(catfccs
) + "' == " + "'" + str(catnewfccs
) + "'");
4271 VerbosePrintOut("File Content Checksum Failed at offset " + str(catfcontentstart
));
4272 VerbosePrintOut("'" + str(catfccs
) + "' != " + "'" + str(catnewfccs
) + "'");
4273 valid_archive
= False;
4274 invalid_archive
= True;
4276 VerbosePrintOut("");
4289 create_alias_function("", __file_format_name__
, "Validate", ArchiveFileValidate
);
4291 def ArchiveFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4292 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4295 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4296 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
4297 if(checkcompressfile
=="tarfile"):
4298 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4299 if(checkcompressfile
=="zipfile"):
4300 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4301 if(rarfile_support
and checkcompressfile
=="rarfile"):
4302 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4303 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4304 return SevenZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4305 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4312 if(hasattr(sys
.stdin
, "buffer")):
4313 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
4315 shutil
.copyfileobj(sys
.stdin
, catfp
);
4317 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4321 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4322 catfp
= download_file_from_internet_file(infile
);
4323 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4329 infile
= RemoveWindowsPath(infile
);
4330 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
4331 if(checkcompressfile
=="tarfile"):
4332 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4333 if(checkcompressfile
=="zipfile"):
4334 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4335 if(rarfile_support
and checkcompressfile
=="rarfile"):
4336 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4337 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4338 return SevenZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4339 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
4341 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
4342 if(not compresscheck
):
4343 fextname
= os
.path
.splitext(infile
)[1];
4344 if(fextname
==".gz"):
4345 compresscheck
= "gzip";
4346 elif(fextname
==".bz2"):
4347 compresscheck
= "bzip2";
4348 elif(fextname
==".zst"):
4349 compresscheck
= "zstd";
4350 elif(fextname
==".lz4" or fextname
==".clz4"):
4351 compresscheck
= "lz4";
4352 elif(fextname
==".lzo" or fextname
==".lzop"):
4353 compresscheck
= "lzo";
4354 elif(fextname
==".lzma" or fextname
==".xz"):
4355 compresscheck
= "lzma";
4358 if(not compresscheck
):
4360 catfp
= UncompressFile(infile
, formatspecs
, "rb");
4365 SeekToEndOfFile(catfp);
4367 SeekToEndOfFile(catfp);
4368 CatSize = catfp.tell();
4369 CatSizeEnd = CatSize;
4377 curloc
= catfp
.tell();
4380 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
4382 catfp
.seek(curloc
, 0);
4383 catstring
= catheader
[0];
4384 catversion
= re
.findall(r
"([\d]+)$", catstring
);
4385 fprenumfiles
= catheader
[1];
4386 fnumfiles
= int(fprenumfiles
, 16);
4387 fprechecksumtype
= catheader
[2];
4388 fprechecksum
= catheader
[3];
4389 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
4390 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4391 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
4392 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
4393 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4394 fheadtell
= len(fileheader
);
4395 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
4396 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
4397 VerbosePrintOut("'" + str(fprechecksum
) + "' != " + "'" + str(catfileheadercshex
) + "'");
4399 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4400 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
4401 if(seekstart
<0 and seekstart
>fnumfiles
):
4403 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
4404 seekend
= fnumfiles
;
4405 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
4406 seekend
= fnumfiles
- abs(seekend
);
4409 while(il
< seekstart
):
4410 prefhstart
= catfp
.tell();
4412 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
4414 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
4415 if(len(preheaderdata
)==0):
4417 prefheadsize
= int(preheaderdata
[0], 16);
4418 prefnumfields
= int(preheaderdata
[1], 16);
4419 if(re
.findall("^[.|/]", preheaderdata
[3])):
4420 prefname
= preheaderdata
[3];
4422 prefname
= "./"+preheaderdata
[3];
4423 prefsize
= int(preheaderdata
[5], 16);
4424 prefcompression
= preheaderdata
[12];
4425 prefcsize
= int(preheaderdata
[13], 16);
4426 prefextrasize
= int(preheaderdata
[24], 16);
4427 prefextrafields
= int(preheaderdata
[25], 16);
4428 extrafieldslist
= [];
4430 extraend
= extrastart
+ prefextrafields
;
4431 extrafieldslist
= [];
4432 if(extrastart
<extraend
):
4433 extrafieldslist
.append(preheaderdata
[extrastart
]);
4434 extrastart
= extrastart
+ 1;
4435 prefchecksumtype
= preheaderdata
[extrastart
].lower();
4436 prefcs
= preheaderdata
[extrastart
+ 1].lower();
4437 prefccs
= preheaderdata
[extrastart
+ 2].lower();
4439 hcmax
= len(preheaderdata
) - 2;
4442 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
4444 prenewfcs
= GetFileChecksum(hout
, preheaderdata
[-3].lower(), True, formatspecs
);
4445 if(prefcs
!=prenewfcs
and not skipchecksum
):
4446 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
4447 VerbosePrintOut("'" + str(prefcs
) + "' != " + "'" + str(prenewfcs
) + "'");
4449 valid_archive
= False;
4450 invalid_archive
= True;
4451 prefhend
= catfp
.tell() - 1;
4452 prefcontentstart
= catfp
.tell();
4454 pyhascontents
= False;
4456 if(prefcompression
=="none" or prefcompression
=="" or prefcompression
=="auto"):
4457 prefcontents
= catfp
.read(prefsize
);
4459 prefcontents
= catfp
.read(prefcsize
);
4460 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
4461 pyhascontents
= True;
4462 if(prefccs
!=prenewfccs
and not skipchecksum
):
4463 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
4464 VerbosePrintOut("'" + str(prefccs
) + "' != " + "'" + str(prenewfccs
) + "'");
4468 fileidnum
= seekstart
;
4470 while(fileidnum
<seekend
):
4471 catfhstart
= catfp
.tell();
4473 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
4475 catheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
[5]);
4476 if(len(catheaderdata
)==0):
4478 catfheadsize
= int(catheaderdata
[0], 16);
4479 catfnumfields
= int(catheaderdata
[1], 16);
4480 catftype
= int(catheaderdata
[2], 16);
4481 if(re
.findall("^[.|/]", catheaderdata
[3])):
4482 catfname
= catheaderdata
[3];
4484 catfname
= "./"+catheaderdata
[3];
4485 catfbasedir
= os
.path
.dirname(catfname
);
4486 catflinkname
= catheaderdata
[4];
4487 catfsize
= int(catheaderdata
[5], 16);
4488 catfatime
= int(catheaderdata
[6], 16);
4489 catfmtime
= int(catheaderdata
[7], 16);
4490 catfctime
= int(catheaderdata
[8], 16);
4491 catfbtime
= int(catheaderdata
[9], 16);
4492 catfmode
= int(catheaderdata
[10], 16);
4493 catfchmode
= stat
.S_IMODE(catfmode
);
4494 catftypemod
= stat
.S_IFMT(catfmode
);
4495 catfwinattributes
= int(catheaderdata
[11], 16);
4496 catfcompression
= catheaderdata
[12];
4497 catfcsize
= int(catheaderdata
[13], 16);
4498 catfuid
= int(catheaderdata
[14], 16);
4499 catfuname
= catheaderdata
[15];
4500 catfgid
= int(catheaderdata
[16], 16);
4501 catfgname
= catheaderdata
[17];
4502 catfid
= int(catheaderdata
[18], 16);
4503 catfinode
= int(catheaderdata
[19], 16);
4504 catflinkcount
= int(catheaderdata
[20], 16);
4505 catfdev_minor
= int(catheaderdata
[21], 16);
4506 catfdev_major
= int(catheaderdata
[22], 16);
4507 catfrdev_minor
= int(catheaderdata
[23], 16);
4508 catfrdev_major
= int(catheaderdata
[24], 16);
4509 catfextrasize
= int(catheaderdata
[25], 16);
4510 catfextrafields
= int(catheaderdata
[26], 16);
4511 extrafieldslist
= [];
4513 extraend
= extrastart
+ catfextrafields
;
4514 extrafieldslist
= [];
4515 if(extrastart
<extraend
):
4516 extrafieldslist
.append(catheaderdata
[extrastart
]);
4517 extrastart
= extrastart
+ 1;
4518 catfchecksumtype
= catheaderdata
[extrastart
].lower();
4519 catfcs
= catheaderdata
[extrastart
+ 1].lower();
4520 catfccs
= catheaderdata
[extrastart
+ 2].lower();
4522 hcmax
= len(catheaderdata
) - 2;
4525 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[5]);
4527 catnewfcs
= GetFileChecksum(hout
, catheaderdata
[-3].lower(), True, formatspecs
);
4528 if(catfcs
!=catnewfcs
and not skipchecksum
):
4529 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfhstart
));
4530 VerbosePrintOut("'" + str(catfcs
) + "' != " + "'" + str(catnewfcs
) + "'");
4532 catfhend
= catfp
.tell() - 1;
4533 catfcontentstart
= catfp
.tell();
4534 catfcontents
= BytesIO();
4535 pyhascontents
= False;
4536 if(catfsize
>0 and not listonly
):
4537 if(catfcompression
=="none" or catfcompression
=="" or catfcompression
=="auto"):
4538 catfcontents
.write(catfp
.read(catfsize
));
4540 catfcontents
.write(catfp
.read(catfcsize
));
4541 catfcontents
.seek(0, 0);
4542 catnewfccs
= GetFileChecksum(catfcontents
.read(), catheaderdata
[-3].lower(), False, formatspecs
);
4543 pyhascontents
= True;
4544 if(catfccs
!=catnewfccs
and skipchecksum
):
4545 VerbosePrintOut("File Content Checksum Error with file " + catfname
+ " at offset " + str(catfcontentstart
));
4546 VerbosePrintOut("'" + str(catfccs
) + "' != " + "'" + str(catnewfccs
) + "'");
4548 if(catfcompression
=="none" or catfcompression
=="" or catfcompression
=="auto"):
4551 catfcontents
.seek(0, 0);
4552 catfcontents
= UncompressArchiveFile(catfcontents
, formatspecs
);
4553 if(catfsize
>0 and listonly
):
4554 catfp
.seek(catfsize
, 1);
4555 pyhascontents
= False;
4557 catfcontentend
= catfp
.tell() - 1;
4558 catfcontents
.seek(0, 0);
4559 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
, 'fcompression': catfcompression
, 'fcsize': catfcsize
, 'fuid': catfuid
, 'funame': catfuname
, 'fgid': catfgid
, 'fgname': catfgname
, 'finode': catfinode
, 'flinkcount': catflinkcount
, '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
} });
4560 fileidnum
= fileidnum
+ 1;
4561 realidnum
= realidnum
+ 1;
4563 catlist
.update({'catfp': catfp
});
4568 create_alias_function("", __file_format_name__
, "ToArray", ArchiveFileToArray
);
4570 def ArchiveFileStringToArray(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4571 catfp
= BytesIO(catstr
);
4572 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4573 return listcatfiles
;
4575 create_alias_function("", __file_format_name__
, "StringToArray", ArchiveFileStringToArray
);
4577 def TarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4579 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4580 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4581 return listcatfiles
;
4583 def ZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4585 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4586 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4587 return listcatfiles
;
4589 if(not rarfile_support
):
4590 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4593 if(rarfile_support
):
4594 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4596 catfp
= PackArchiveFileFromSevenZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4597 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4598 return listcatfiles
;
4600 if(not py7zr_support
):
4601 def SevenZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4605 def SevenZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4607 catfp
= PackArchiveFileFromSevenZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4608 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4609 return listcatfiles
;
4611 def InFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4612 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
4613 if(checkcompressfile
=="tarfile"):
4614 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4615 elif(checkcompressfile
=="zipfile"):
4616 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4617 elif(checkcompressfile
=="catfile"):
4618 return ArchiveFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4619 elif(rarfile_support
and checkcompressfile
=="rarfile"):
4620 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4621 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
4622 return SevenZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4627 def ListDirToArrayAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4628 catver
= formatspecs
[6];
4629 fileheaderver
= str(int(catver
.replace(".", "")));
4630 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4631 advancedlist
= formatspecs
[8];
4632 altinode
= formatspecs
[9];
4635 for line
in sys
.stdin
:
4636 infilelist
.append(line
.strip());
4637 infilelist
= list(filter(None, infilelist
));
4638 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
4639 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
4641 with
UncompressFile(infiles
, formatspecs
, "r") as finfile
:
4642 for line
in finfile
:
4643 infilelist
.append(line
.strip());
4644 infilelist
= list(filter(None, infilelist
));
4646 if(isinstance(infiles
, (list, tuple, ))):
4647 infilelist
= list(filter(None, infiles
));
4648 elif(isinstance(infiles
, (str, ))):
4649 infilelist
= list(filter(None, [infiles
]));
4651 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
4653 GetDirList
= ListDir(infilelist
, followlink
, False);
4661 inodetocatinode
= {};
4663 fnumfiles
= int(len(GetDirList
));
4664 catver
= formatspecs
[6];
4665 fileheaderver
= str(int(catver
.replace(".", "")));
4666 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4667 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4668 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4669 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4670 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4671 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
4672 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4673 fheadtell
= len(fileheader
);
4674 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4675 for curfname
in GetDirList
:
4676 if(re
.findall("^[.|/]", curfname
)):
4679 fname
= "./"+curfname
;
4681 VerbosePrintOut(fname
);
4682 if(not followlink
or followlink
is None):
4683 fstatinfo
= os
.lstat(fname
);
4685 fstatinfo
= os
.stat(fname
);
4686 fpremode
= fstatinfo
.st_mode
;
4687 finode
= fstatinfo
.st_ino
;
4688 flinkcount
= fstatinfo
.st_nlink
;
4690 if(stat
.S_ISREG(fpremode
)):
4692 elif(stat
.S_ISLNK(fpremode
)):
4694 elif(stat
.S_ISCHR(fpremode
)):
4696 elif(stat
.S_ISBLK(fpremode
)):
4698 elif(stat
.S_ISDIR(fpremode
)):
4700 elif(stat
.S_ISFIFO(fpremode
)):
4702 elif(stat
.S_ISSOCK(fpremode
)):
4704 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
4706 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
4708 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
4713 fbasedir
= os
.path
.dirname(fname
);
4715 if(not followlink
and finode
!=0):
4717 if(finode
in inodelist
):
4719 flinkname
= inodetofile
[finode
];
4723 fcurinode
= inodetocatinode
[finode
];
4724 if(finode
not in inodelist
):
4725 inodelist
.append(finode
);
4726 inodetofile
.update({finode
: fname
});
4727 inodetocatinode
.update({finode
: curinode
});
4731 fcurinode
= curinode
;
4732 curinode
= curinode
+ 1;
4734 fcurinode
= curinode
;
4735 curinode
= curinode
+ 1;
4736 curfid
= curfid
+ 1;
4738 flinkname
= os
.readlink(fname
);
4739 fdev
= fstatinfo
.st_dev
;
4740 getfdev
= GetDevMajorMinor(fdev
);
4741 fdev_minor
= getfdev
[0];
4742 fdev_major
= getfdev
[1];
4743 frdev
= fstatinfo
.st_dev
;
4744 if(hasattr(fstatinfo
, "st_rdev")):
4745 frdev
= fstatinfo
.st_rdev
;
4747 frdev
= fstatinfo
.st_dev
;
4748 getfrdev
= GetDevMajorMinor(frdev
);
4749 frdev_minor
= getfrdev
[0];
4750 frdev_major
= getfrdev
[1];
4751 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
4753 if(ftype
==0 or ftype
==7):
4754 fsize
= fstatinfo
.st_size
;
4755 fatime
= fstatinfo
.st_atime
;
4756 fmtime
= fstatinfo
.st_mtime
;
4757 fctime
= fstatinfo
.st_ctime
;
4758 if(hasattr(fstatinfo
, "st_birthtime")):
4759 fbtime
= fstatinfo
.st_birthtime
;
4761 fbtime
= fstatinfo
.st_ctime
;
4762 fmode
= fstatinfo
.st_mode
;
4763 fchmode
= stat
.S_IMODE(fstatinfo
.st_mode
);
4764 ftypemod
= stat
.S_IFMT(fstatinfo
.st_mode
);
4765 fuid
= fstatinfo
.st_uid
;
4766 fgid
= fstatinfo
.st_gid
;
4771 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
4772 funame
= userinfo
.pw_name
;
4781 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
4782 fgname
= groupinfo
.gr_name
;
4787 fdev_minor
= fdev_minor
;
4788 fdev_major
= fdev_major
;
4789 frdev_minor
= frdev_minor
;
4790 frdev_major
= frdev_major
;
4791 flinkcount
= flinkcount
;
4792 if(hasattr(fstatinfo
, "st_file_attributes")):
4793 fwinattributes
= fstatinfo
.st_file_attributes
;
4798 fcontents
= BytesIO();
4799 if(ftype
==0 or ftype
==7):
4800 with
open(fname
, "rb") as fpc
:
4801 shutil
.copyfileobj(fpc
, fcontents
);
4802 if(followlink
and (ftype
==1 or ftype
==2)):
4803 flstatinfo
= os
.stat(flinkname
);
4804 with
open(flinkname
, "rb") as fpc
:
4805 shutil
.copyfileobj(fpc
, fcontents
);
4806 fcontents
.seek(0, 0);
4807 ftypehex
= format(ftype
, 'x').lower();
4808 extrafields
= len(extradata
);
4809 extrafieldslist
= extradata
;
4810 catfextrafields
= extrafields
;
4811 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
4812 if(len(extradata
)>0):
4813 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4814 extrasizelen
= len(extrasizestr
);
4815 extrasizelenhex
= format(extrasizelen
, 'x').lower();
4816 catoutlist
= [ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fwinattributes
), 'x').lower(), fcompression
, format(int(fcsize
), 'x').lower(), format(int(fuid
), 'x').lower(), 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()];
4817 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
4818 catoutlenhex
= format(catoutlen
, 'x').lower();
4819 catoutlist
.insert(0, catoutlenhex
);
4820 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
4821 if(len(extradata
)>0):
4822 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4823 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
4824 catfnumfields
= catoutlen
;
4825 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
4826 fcontents
.seek(0, 0);
4827 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
4828 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4829 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
4830 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
4831 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
4832 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4833 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
4834 nullstrecd
= formatspecs
[5].encode('UTF-8');
4835 fheadtell
+= len(catfileoutstr
) + 1;
4836 catfcontentend
= fheadtell
- 1;
4837 fcontents
.seek(0, 0);
4838 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
4839 pyhascontents
= False;
4840 if(int(fsize
)>0 and not listonly
):
4841 pyhascontents
= True;
4842 if(int(fsize
)>0 and listonly
):
4843 fcontents
= BytesIO();
4844 pyhascontents
= False;
4845 fcontents
.seek(0, 0);
4846 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
, 'fcompression': fcompression
, 'fcsize': fcsize
, '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
} });
4847 fileidnum
= fileidnum
+ 1;
4850 def TarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4856 inodetocatinode
= {};
4860 if(hasattr(sys
.stdin
, "buffer")):
4861 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
4863 shutil
.copyfileobj(sys
.stdin
, infile
);
4868 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4869 infile
= download_file_from_internet_file(infile
);
4874 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
4876 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
4878 if(not tarfile
.is_tarfile(infile
)):
4880 except AttributeError:
4881 if(not is_tarfile(infile
)):
4886 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4887 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
4889 tarfp
= tarfile
.open(infile
, "r");
4890 except FileNotFoundError
:
4892 fnumfiles
= int(len(tarfp
.getmembers()));
4893 catver
= formatspecs
[6];
4894 fileheaderver
= str(int(catver
.replace(".", "")));
4895 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4896 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4897 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4898 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4899 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4900 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
4901 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4902 fheadtell
= len(fileheader
);
4903 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4904 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
4905 if(re
.findall("^[.|/]", member
.name
)):
4906 fname
= member
.name
;
4908 fname
= "./"+member
.name
;
4910 VerbosePrintOut(fname
);
4911 fpremode
= member
.mode
;
4912 ffullmode
= member
.mode
;
4916 ffullmode
= member
.mode
+ stat
.S_IFREG
;
4918 elif(member
.isdev()):
4919 ffullmode
= member
.mode
;
4921 elif(member
.islnk()):
4922 ffullmode
= member
.mode
+ stat
.S_IFREG
;
4924 elif(member
.issym()):
4925 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
4927 elif(member
.ischr()):
4928 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
4930 elif(member
.isblk()):
4931 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
4933 elif(member
.isdir()):
4934 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
4936 elif(member
.isfifo()):
4937 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
4939 elif(member
.issparse()):
4940 ffullmode
= member
.mode
;
4943 ffullmode
= member
.mode
;
4946 fbasedir
= os
.path
.dirname(fname
);
4950 curfid
= curfid
+ 1;
4952 flinkname
= member
.linkname
;
4953 fdev_minor
= member
.devminor
;
4954 fdev_major
= member
.devmajor
;
4955 frdev_minor
= member
.devminor
;
4956 frdev_major
= member
.devmajor
;
4957 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
4959 elif(ftype
==0 or ftype
==7):
4960 fsize
= member
.size
;
4962 fsize
= member
.size
;
4963 fatime
= member
.mtime
;
4964 fmtime
= member
.mtime
;
4965 fctime
= member
.mtime
;
4966 fbtime
= member
.mtime
;
4968 fchmode
= stat
.S_IMODE(ffullmode
);
4969 ftypemod
= stat
.S_IFMT(ffullmode
);
4972 funame
= member
.uname
;
4973 fgname
= member
.gname
;
4974 flinkcount
= flinkcount
;
4975 fwinattributes
= int(0);
4978 fcontents
= BytesIO();
4979 if(ftype
==0 or ftype
==7):
4980 with tarfp
.extractfile(member
) as fpc
:
4981 shutil
.copyfileobj(fpc
, fcontents
);
4982 fcontents
.seek(0, 0);
4983 ftypehex
= format(ftype
, 'x').lower();
4984 extrafields
= len(extradata
);
4985 extrafieldslist
= extradata
;
4986 catfextrafields
= extrafields
;
4987 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
4988 if(len(extradata
)>0):
4989 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4990 extrasizelen
= len(extrasizestr
);
4991 extrasizelenhex
= format(extrasizelen
, 'x').lower();
4992 catoutlist
= [ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fwinattributes
), 'x').lower(), fcompression
, format(int(fcsize
), 'x').lower(), format(int(fuid
), 'x').lower(), 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()];
4993 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
4994 catoutlenhex
= format(catoutlen
, 'x').lower();
4995 catoutlist
.insert(0, catoutlenhex
);
4996 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
4997 if(len(extradata
)>0):
4998 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4999 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5000 catfnumfields
= catoutlen
;
5001 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5002 fcontents
.seek(0, 0);
5003 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
5004 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5005 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
5006 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5007 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5008 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5009 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5010 nullstrecd
= formatspecs
[5].encode('UTF-8');
5011 fheadtell
+= len(catfileoutstr
) + 1;
5012 catfcontentend
= fheadtell
- 1;
5013 fcontents
.seek(0, 0);
5014 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
5015 pyhascontents
= False;
5016 if(int(fsize
)>0 and not listonly
):
5017 pyhascontents
= True;
5018 if(int(fsize
)>0 and listonly
):
5020 pyhascontents
= False;
5021 fcontents
.seek(0, 0);
5022 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
, 'fcompression': fcompression
, 'fcsize': fcsize
, '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
} });
5023 fileidnum
= fileidnum
+ 1;
5026 def ZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5032 inodetocatinode
= {};
5036 if(hasattr(sys
.stdin
, "buffer")):
5037 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
5039 shutil
.copyfileobj(sys
.stdin
, infile
);
5044 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
5045 infile
= download_file_from_internet_file(infile
);
5050 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5054 if(not zipfile
.is_zipfile(infile
)):
5057 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
5058 except FileNotFoundError
:
5060 ziptest
= zipfp
.testzip();
5062 VerbosePrintOut("Bad file found!");
5063 fnumfiles
= int(len(zipfp
.infolist()));
5064 catver
= formatspecs
[6];
5065 fileheaderver
= str(int(catver
.replace(".", "")));
5066 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5067 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5068 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5069 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5070 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
5071 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
5072 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
5073 fheadtell
= len(fileheader
);
5074 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5075 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
5076 if(re
.findall("^[.|/]", member
.filename
)):
5077 fname
= member
.filename
;
5079 fname
= "./"+member
.filename
;
5080 zipinfo
= zipfp
.getinfo(member
.filename
);
5082 VerbosePrintOut(fname
);
5083 if(not member
.is_dir()):
5084 fpremode
= stat
.S_IFREG
+ 438;
5085 elif(member
.is_dir()):
5086 fpremode
= stat
.S_IFDIR
+ 511;
5089 if(not member
.is_dir()):
5091 elif(member
.is_dir()):
5094 fbasedir
= os
.path
.dirname(fname
);
5098 curfid
= curfid
+ 1;
5106 fsize
= member
.file_size
;
5108 fsize
= member
.file_size
;
5109 fatime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5110 fmtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5111 fctime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5112 fbtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5113 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
5114 fwinattributes
= int(zipinfo
.external_attr
);
5115 if(not member
.is_dir()):
5116 fmode
= int(stat
.S_IFREG
+ 438);
5117 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
5118 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
5119 elif(member
.is_dir()):
5120 fmode
= int(stat
.S_IFDIR
+ 511);
5121 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
5122 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
5123 elif(zipinfo
.create_system
==3):
5124 fwinattributes
= int(0);
5125 fmode
= int(zipinfo
.external_attr
);
5127 fwinattributes
= int(0);
5128 if(not member
.is_dir()):
5129 fmode
= int(stat
.S_IFREG
+ 438);
5130 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
5131 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
5132 elif(member
.is_dir()):
5133 fmode
= int(stat
.S_IFDIR
+ 511);
5134 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
5135 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
5140 except AttributeError:
5146 except AttributeError:
5153 userinfo
= pwd
.getpwuid(os
.getuid());
5154 funame
= userinfo
.pw_name
;
5157 except AttributeError:
5165 groupinfo
= grp
.getgrgid(os
.getgid());
5166 fgname
= groupinfo
.gr_name
;
5169 except AttributeError:
5173 fcontents
= BytesIO();
5175 fcontents
.write(zipfp
.read(member
.filename
));
5176 fcontents
.seek(0, 0);
5177 ftypehex
= format(ftype
, 'x').lower();
5178 extrafields
= len(extradata
);
5179 extrafieldslist
= extradata
;
5180 catfextrafields
= extrafields
;
5181 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5182 if(len(extradata
)>0):
5183 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5184 extrasizelen
= len(extrasizestr
);
5185 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5186 catoutlist
= [ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fwinattributes
), 'x').lower(), fcompression
, format(int(fcsize
), 'x').lower(), format(int(fuid
), 'x').lower(), 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()];
5187 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5188 catoutlenhex
= format(catoutlen
, 'x').lower();
5189 catoutlist
.insert(0, catoutlenhex
);
5190 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5191 if(len(extradata
)>0):
5192 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5193 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5194 catfnumfields
= catoutlen
;
5195 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5196 fcontents
.seek(0, 0);
5197 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
5198 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5199 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
5200 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5201 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5202 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5203 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5204 nullstrecd
= formatspecs
[5].encode('UTF-8');
5205 fheadtell
+= len(catfileoutstr
) + 1;
5206 catfcontentend
= fheadtell
- 1;
5207 fcontents
.seek(0, 0);
5208 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
5209 pyhascontents
= False;
5210 if(int(fsize
)>0 and not listonly
):
5211 pyhascontents
= True;
5212 if(int(fsize
)>0 and listonly
):
5213 fcontents
= BytesIO();
5214 pyhascontents
= False;
5215 fcontents
.seek(0, 0);
5216 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
, 'fcompression': fcompression
, 'fcsize': fcsize
, '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
} });
5217 fileidnum
= fileidnum
+ 1;
5220 if(not rarfile_support
):
5221 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5224 if(rarfile_support
):
5225 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5231 inodetocatinode
= {};
5233 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
5235 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
5237 rarfp
= rarfile
.RarFile(infile
, "r");
5238 rartest
= rarfp
.testrar();
5240 VerbosePrintOut("Bad file found!");
5241 fnumfiles
= int(len(rarfp
.infolist()));
5242 catver
= formatspecs
[6];
5243 fileheaderver
= str(int(catver
.replace(".", "")));
5244 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5245 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5246 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5247 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5248 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
5249 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
5250 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
5251 fheadtell
= len(fileheader
);
5252 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5253 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
5256 if(member
.host_os
==rarfile
.RAR_OS_UNIX
):
5259 member
.external_attr
5261 except AttributeError:
5263 elif(member
.host_os
==rarfile
.RAR_OS_WIN32
):
5266 member
.external_attr
5268 except AttributeError:
5273 if(re
.findall("^[.|/]", member
.filename
)):
5274 fname
= member
.filename
;
5276 fname
= "./"+member
.filename
;
5277 rarinfo
= rarfp
.getinfo(member
.filename
);
5279 VerbosePrintOut(fname
);
5280 if(is_unix
and member
.external_attr
!=0):
5281 fpremode
= int(member
.external_attr
);
5282 elif(member
.is_file()):
5283 fpremode
= stat
.S_IFREG
+ 438;
5284 elif(member
.is_symlink()):
5285 fpremode
= stat
.S_IFLNK
+ 438;
5286 elif(member
.is_dir()):
5287 fpremode
= stat
.S_IFDIR
+ 511;
5288 if(is_windows
and member
.external_attr
!=0):
5289 fwinattributes
= int(member
.external_attr
);
5291 fwinattributes
= int(0);
5296 if(member
.is_file()):
5298 elif(member
.is_symlink()):
5300 elif(member
.is_dir()):
5304 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
5305 fbasedir
= os
.path
.dirname(fname
);
5309 curfid
= curfid
+ 1;
5317 fsize
= member
.file_size
;
5320 fatime
= int(member
.atime
.timestamp());
5322 fatime
= int(member
.mtime
.timestamp());
5323 except AttributeError:
5324 fatime
= int(member
.mtime
.timestamp());
5325 fmtime
= int(member
.mtime
.timestamp());
5328 fctime
= int(member
.ctime
.timestamp());
5330 fctime
= int(member
.mtime
.timestamp());
5331 except AttributeError:
5332 fctime
= int(member
.mtime
.timestamp());
5333 fbtime
= int(member
.mtime
.timestamp());
5334 if(is_unix
and member
.external_attr
!=0):
5335 fmode
= int(member
.external_attr
);
5336 fchmode
= int(stat
.S_IMODE(member
.external_attr
));
5337 ftypemod
= int(stat
.S_IFMT(member
.external_attr
));
5338 elif(member
.is_file()):
5339 fmode
= int(stat
.S_IFREG
+ 438)
5340 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
5341 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
5342 elif(member
.is_symlink()):
5343 fmode
= int(stat
.S_IFLNK
+ 438)
5344 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
5345 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
5346 elif(member
.is_dir()):
5347 fmode
= int(stat
.S_IFDIR
+ 511)
5348 fchmode
= int(stat
.S_IMODE(stat
.S_IFDIR
+ 511));
5349 ftypemod
= int(stat
.S_IFMT(stat
.S_IFDIR
+ 511));
5352 except AttributeError:
5358 except AttributeError:
5365 userinfo
= pwd
.getpwuid(os
.getuid());
5366 funame
= userinfo
.pw_name
;
5369 except AttributeError:
5377 groupinfo
= grp
.getgrgid(os
.getgid());
5378 fgname
= groupinfo
.gr_name
;
5381 except AttributeError:
5385 fcontents
= BytesIO();
5387 fcontents
.write(rarfp
.read(member
.filename
));
5388 fcontents
.seek(0, 0);
5389 ftypehex
= format(ftype
, 'x').lower();
5390 extrafields
= len(extradata
);
5391 extrafieldslist
= extradata
;
5392 catfextrafields
= extrafields
;
5393 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5394 if(len(extradata
)>0):
5395 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5396 extrasizelen
= len(extrasizestr
);
5397 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5398 catoutlist
= [ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fwinattributes
), 'x').lower(), fcompression
, format(int(fcsize
), 'x').lower(), format(int(fuid
), 'x').lower(), 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()];
5399 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5400 catoutlenhex
= format(catoutlen
, 'x').lower();
5401 catoutlist
.insert(0, catoutlenhex
);
5402 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5403 if(len(extradata
)>0):
5404 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5405 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5406 catfnumfields
= 24 + catfextrafields
;
5407 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5408 fcontents
.seek(0, 0);
5409 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
5410 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5411 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
5412 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5413 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5414 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5415 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5416 nullstrecd
= formatspecs
[5].encode('UTF-8');
5417 fheadtell
+= len(catfileoutstr
) + 1;
5418 catfcontentend
= fheadtell
- 1;
5419 fcontents
.seek(0, 0);
5420 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
5421 pyhascontents
= False;
5422 if(int(fsize
)>0 and not listonly
):
5423 pyhascontents
= True;
5424 if(int(fsize
)>0 and listonly
):
5425 fcontents
= BytesIO();
5426 pyhascontents
= False;
5427 fcontents
.seek(0, 0);
5428 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
, 'fcompression': fcompression
, 'fcsize': fcsize
, '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
} });
5429 fileidnum
= fileidnum
+ 1;
5432 if(not py7zr_support
):
5433 def SevenZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5437 def SevenZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5443 inodetocatinode
= {};
5445 szpfp
= py7zr
.SevenZipFile(infile
, mode
="r");
5446 file_content
= szpfp
.readall();
5447 #sztest = szpfp.testzip();
5448 sztestalt
= szpfp
.test();
5450 VerbosePrintOut("Bad file found!");
5451 numfiles
= int(len(szpfp
.list()));
5452 catver
= formatspecs
[6];
5453 fileheaderver
= str(int(catver
.replace(".", "")));
5454 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5455 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5456 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5457 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5458 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
5459 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
5460 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
5461 fheadtell
= len(fileheader
);
5462 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5463 for member
in sorted(szpfp
.list(), key
=lambda x
: x
.filename
):
5464 if(re
.findall("^[.|/]", member
.filename
)):
5465 fname
= member
.filename
;
5467 fname
= "./"+member
.filename
;
5468 if(not member
.is_directory
):
5469 fpremode
= int(stat
.S_IFREG
+ 438);
5470 elif(member
.is_directory
):
5471 fpremode
= int(stat
.S_IFDIR
+ 511);
5472 fwinattributes
= int(0);
5477 if(member
.is_directory
):
5482 fbasedir
= os
.path
.dirname(fname
);
5486 curfid
= curfid
+ 1;
5493 fatime
= int(member
.creationtime
.timestamp());
5494 fmtime
= int(member
.creationtime
.timestamp());
5495 fctime
= int(member
.creationtime
.timestamp());
5496 fbtime
= int(member
.creationtime
.timestamp());
5497 if(member
.is_directory
):
5498 fmode
= int(stat
.S_IFDIR
+ 511)
5499 fchmode
= int(stat
.S_IMODE(stat
.S_IFDIR
+ 511));
5500 ftypemod
= int(stat
.S_IFMT(stat
.S_IFDIR
+ 511));
5502 fmode
= int(stat
.S_IFLNK
+ 438)
5503 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
5504 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
5507 except AttributeError:
5513 except AttributeError:
5520 userinfo
= pwd
.getpwuid(os
.getuid());
5521 funame
= userinfo
.pw_name
;
5524 except AttributeError:
5532 groupinfo
= grp
.getgrgid(os
.getgid());
5533 fgname
= groupinfo
.gr_name
;
5536 except AttributeError:
5540 fcontents
= BytesIO();
5542 fcontents
.write(file_content
[member
.filename
].read());
5543 fsize
= format(fcontents
.tell(), 'x').lower();
5545 fcontents
.seek(0, 0);
5546 ftypehex
= format(ftype
, 'x').lower();
5547 extrafields
= len(extradata
);
5548 extrafieldslist
= extradata
;
5549 catfextrafields
= extrafields
;
5550 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5551 if(len(extradata
)>0):
5552 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5553 extrasizelen
= len(extrasizestr
);
5554 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5555 catoutlist
= [ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fwinattributes
), 'x').lower(), fcompression
, format(int(fcsize
), 'x').lower(), format(int(fuid
), 'x').lower(), 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()];
5556 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5557 catoutlenhex
= format(catoutlen
, 'x').lower();
5558 catoutlist
.insert(0, catoutlenhex
);
5559 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
[5]);
5560 if(len(extradata
)>0):
5561 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5562 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
5563 catfnumfields
= 24 + catfextrafields
;
5564 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5565 fcontents
.seek(0, 0);
5566 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
5567 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5568 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
5569 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5570 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5571 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5572 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5573 nullstrecd
= formatspecs
[5].encode('UTF-8');
5574 fheadtell
+= len(catfileoutstr
) + 1;
5575 catfcontentend
= fheadtell
- 1;
5576 fcontents
.seek(0, 0);
5577 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
5578 pyhascontents
= False;
5579 if(int(fsize
)>0 and not listonly
):
5580 pyhascontents
= True;
5581 if(int(fsize
)>0 and listonly
):
5583 pyhascontents
= False;
5584 fcontents
.seek(0, 0);
5585 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
, 'fcompression': fcompression
, 'fcsize': fcsize
, '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
} });
5586 fileidnum
= fileidnum
+ 1;
5589 def InFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5590 checkcompressfile
= CheckCompressionSubType(infiles
, formatspecs
, True);
5591 if(checkcompressfile
=="tarfile"):
5592 return TarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5593 elif(checkcompressfile
=="zipfile"):
5594 return ZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5595 elif(checkcompressfile
=="catfile"):
5596 return ArchiveFileToArray(infile
, 0, 0, listonly
, False, formatspecs
, False);
5597 elif(rarfile_support
and checkcompressfile
=="rarfile"):
5598 return RarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5599 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
5600 return SevenZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5605 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):
5606 outarray
= BytesIO();
5607 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
5608 listcatfiles
= ArchiveFileToArray(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5609 return listcatfiles
;
5611 def ArchiveFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5612 if(isinstance(infile
, dict)):
5613 listcatfiles
= infile
;
5615 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5616 infile
= RemoveWindowsPath(infile
);
5617 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5618 if(not listcatfiles
):
5620 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': {}}}};
5622 catarray
.update({'catfp': listcatfiles
['catfp']});
5623 lenlist
= len(listcatfiles
['ffilelist']);
5625 lcfx
= int(listcatfiles
['fnumfiles']);
5626 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5627 lcfx
= int(lenlist
);
5629 lcfx
= int(listcatfiles
['fnumfiles']);
5631 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5632 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5633 catarray
['filetoid'].update(filetoidarray
);
5634 catarray
['idtofile'].update(idtofilearray
);
5635 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5636 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5637 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5638 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5639 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5640 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5641 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5642 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5643 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5644 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5645 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5646 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5647 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5648 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5649 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5650 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5651 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5652 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5653 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5654 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5655 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5656 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5657 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5658 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5659 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5660 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5661 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5662 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5663 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5664 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5665 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5669 create_alias_function("", __file_format_name__
, "ToArrayIndex", ArchiveFileToArrayIndex
);
5671 def ListDirToArrayIndexAlt(infiles
, dirlistfromtxt
=False, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5672 listcatfiles
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5673 if(not listcatfiles
):
5675 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': {}}}};
5676 lenlist
= len(listcatfiles
['ffilelist']);
5678 lcfx
= int(listcatfiles
['fnumfiles']);
5679 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5680 lcfx
= int(lenlist
);
5682 lcfx
= int(listcatfiles
['fnumfiles']);
5684 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5685 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5686 catarray
['filetoid'].update(filetoidarray
);
5687 catarray
['idtofile'].update(idtofilearray
);
5688 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5689 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5690 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5691 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5692 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5693 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5694 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5695 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5696 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5697 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5698 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5699 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5700 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5701 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5702 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5703 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5704 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5705 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5706 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5707 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5708 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5709 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5710 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5711 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5712 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5713 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5714 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5715 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5716 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5717 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5718 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5722 def TarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5723 listcatfiles
= TarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5724 if(not listcatfiles
):
5726 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': {}}}};
5727 lenlist
= len(listcatfiles
['ffilelist']);
5729 lcfx
= int(listcatfiles
['fnumfiles']);
5730 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5731 lcfx
= int(lenlist
);
5733 lcfx
= int(listcatfiles
['fnumfiles']);
5735 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5736 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5737 catarray
['filetoid'].update(filetoidarray
);
5738 catarray
['idtofile'].update(idtofilearray
);
5739 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5740 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5741 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5742 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5743 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5744 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5745 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5746 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5747 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5748 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5749 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5750 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5751 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5752 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5753 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5754 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5755 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5756 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5757 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5758 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5759 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5760 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5761 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5762 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5763 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5764 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5765 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5766 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5767 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5768 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5769 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5773 def ZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5774 listcatfiles
= ZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5775 if(not listcatfiles
):
5777 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': {}}}};
5778 lenlist
= len(listcatfiles
['ffilelist']);
5780 lcfx
= int(listcatfiles
['fnumfiles']);
5781 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5782 lcfx
= int(lenlist
);
5784 lcfx
= int(listcatfiles
['fnumfiles']);
5786 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5787 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5788 catarray
['filetoid'].update(filetoidarray
);
5789 catarray
['idtofile'].update(idtofilearray
);
5790 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5791 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5792 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5793 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5794 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5795 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5796 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5797 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5798 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5799 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5800 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5801 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5802 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5803 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5804 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5805 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5806 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5807 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5808 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5809 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5810 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5811 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5812 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5813 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5814 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5815 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5816 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5817 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5818 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5819 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5820 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5824 if(not rarfile_support
):
5825 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5828 if(rarfile_support
):
5829 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5830 listcatfiles
= RarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5831 if(not listcatfiles
):
5833 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': {}}}};
5834 lenlist
= len(listcatfiles
['ffilelist']);
5836 lcfx
= int(listcatfiles
['fnumfiles']);
5837 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5838 lcfx
= int(lenlist
);
5840 lcfx
= int(listcatfiles
['fnumfiles']);
5842 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5843 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5844 catarray
['filetoid'].update(filetoidarray
);
5845 catarray
['idtofile'].update(idtofilearray
);
5846 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5847 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5848 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5849 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5850 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5851 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5852 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5853 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5854 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5855 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5856 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5857 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5858 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5859 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5860 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5861 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5862 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5863 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5864 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5865 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5866 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5867 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5868 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5869 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5870 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5871 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5872 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5873 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5874 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5875 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5876 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5880 if(not py7zr_support
):
5881 def SevenZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5885 def SevenZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5886 listcatfiles
= SevenZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5887 if(not listcatfiles
):
5889 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': {}}}};
5890 lenlist
= len(listcatfiles
['ffilelist']);
5892 lcfx
= int(listcatfiles
['fnumfiles']);
5893 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5894 lcfx
= int(lenlist
);
5896 lcfx
= int(listcatfiles
['fnumfiles']);
5898 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5899 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5900 catarray
['filetoid'].update(filetoidarray
);
5901 catarray
['idtofile'].update(idtofilearray
);
5902 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5903 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5904 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5905 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5906 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5907 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5908 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5909 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5910 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5911 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5912 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5913 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5914 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5915 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5916 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5917 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5918 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5919 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5920 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5921 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5922 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5923 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5924 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5925 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5926 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5927 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5928 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5929 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5930 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5931 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5932 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5936 def ArchiveFileStringToArrayIndex(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5937 catfp
= BytesIO(catstr
);
5938 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5939 return listcatfiles
;
5941 create_alias_function("", __file_format_name__
, "StringToArrayIndex", ArchiveFileStringToArrayIndex
);
5943 def TarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5945 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5946 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5947 return listcatfiles
;
5949 def ZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5951 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5952 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5953 return listcatfiles
;
5955 if(not rarfile_support
):
5956 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5959 if(rarfile_support
):
5960 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5962 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5963 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5964 return listcatfiles
;
5966 if(not py7zr_support
):
5967 def SevenZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5971 def SevenZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5973 catfp
= PackArchiveFileFromSevenZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5974 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5975 return listcatfiles
;
5977 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):
5978 outarray
= BytesIO();
5979 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, verbose
, True);
5980 listcatfiles
= ArchiveFileToArrayIndex(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
)
5981 return listcatfiles
;
5983 def RePackArchiveFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5984 if(isinstance(infile
, dict)):
5985 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5986 listcatfiles
= prelistcatfiles
['list'];
5988 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5989 infile
= RemoveWindowsPath(infile
);
5991 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5992 listcatfiles
= prelistcatfiles
['list'];
5994 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5995 if(outfile
!="-" and not hasattr(infile
, "read") and not hasattr(outfile
, "write")):
5996 outfile
= RemoveWindowsPath(outfile
);
5997 checksumtype
= checksumtype
.lower();
5998 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
5999 checksumtype
="crc32";
6000 if(checksumtype
=="none"):
6002 if(not compression
or compression
=="catfile" or compression
==formatspecs
[2]):
6003 compression
= "auto";
6004 if(compression
not in compressionlist
and compression
is None):
6005 compression
= "auto";
6007 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6008 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
6009 if(os
.path
.exists(outfile
)):
6012 except OSError as e
:
6014 if(not listcatfiles
):
6019 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
6021 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
6024 fbasename
= os
.path
.splitext(outfile
)[0];
6025 fextname
= os
.path
.splitext(outfile
)[1];
6026 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
);
6027 catver
= formatspecs
[6];
6028 fileheaderver
= str(int(catver
.replace(".", "")));
6029 lenlist
= len(listcatfiles
['ffilelist']);
6030 fnumfiles
= int(listcatfiles
['fnumfiles']);
6031 if(lenlist
>fnumfiles
or lenlist
<fnumfiles
):
6032 fnumfiles
= lenlist
;
6033 AppendFileHeader(catfp
, fnumfiles
, checksumtype
, formatspecs
);
6034 lenlist
= len(listcatfiles
['ffilelist']);
6035 fnumfiles
= int(listcatfiles
['fnumfiles']);
6037 lcfx
= int(listcatfiles
['fnumfiles']);
6038 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6039 lcfx
= int(lenlist
);
6041 lcfx
= int(listcatfiles
['fnumfiles']);
6049 if(re
.findall("^[.|/]", listcatfiles
['ffilelist'][reallcfi
]['fname'])):
6050 fname
= listcatfiles
['ffilelist'][reallcfi
]['fname'];
6052 fname
= "./"+listcatfiles
['ffilelist'][reallcfi
]['fname'];
6054 VerbosePrintOut(fname
);
6055 fheadersize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fheadersize']), 'x').lower();
6056 fsize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fsize']), 'x').lower();
6057 flinkname
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
6058 fatime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fatime']), 'x').lower();
6059 fmtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmtime']), 'x').lower();
6060 fctime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fctime']), 'x').lower();
6061 fbtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fbtime']), 'x').lower();
6062 fmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmode']), 'x').lower();
6063 fchmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fchmode']), 'x').lower();
6064 fuid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fuid']), 'x').lower();
6065 funame
= listcatfiles
['ffilelist'][reallcfi
]['funame'];
6066 fgid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fgid']), 'x').lower();
6067 fgname
= listcatfiles
['ffilelist'][reallcfi
]['fgname'];
6068 finode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['finode']), 'x').lower();
6069 flinkcount
= format(int(listcatfiles
['ffilelist'][reallcfi
]['flinkcount']), 'x').lower();
6070 fwinattributes
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fwinattributes']), 'x').lower();
6071 fcompression
= listcatfiles
['ffilelist'][reallcfi
]['fcompression'];
6072 fcsize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fcsize']), 'x').lower();
6073 fdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fminor']), 'x').lower();
6074 fdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmajor']), 'x').lower();
6075 frdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frminor']), 'x').lower();
6076 frdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frmajor']), 'x').lower();
6077 if(len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] and len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>0):
6078 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
6079 if(not followlink
and len(extradata
)<0):
6080 extradata
= listcatfiles
['ffilelist'][reallcfi
]['fextralist'];
6081 fcontents
= listcatfiles
['ffilelist'][reallcfi
]['fcontents'];
6083 fcsize
= format(int(0), 'x').lower();
6084 if(not compresswholefile
):
6085 fcontents
.seek(0, 2);
6086 ucfsize
= fcontents
.tell();
6087 fcontents
.seek(0, 0);
6088 cfcontents
= BytesIO();
6089 shutil
.copyfileobj(fcontents
, cfcontents
);
6090 cfcontents
.seek(0, 0);
6091 cfcontents
= CompressArchiveFile(cfcontents
, compression
, compressionlevel
, formatspecs
);
6092 cfcontents
.seek(0, 2);
6093 cfsize
= cfcontents
.tell();
6094 if(ucfsize
> cfsize
):
6095 fcsize
= format(int(cfsize
), 'x').lower();
6096 fcompression
= compression
;
6098 fcontents
= cfcontents
;
6100 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']==1 or listcatfiles
['ffilelist'][reallcfi
]['ftype']==2):
6101 getflinkpath
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
6102 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
6103 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
6104 fheadersize
= format(int(flinkinfo
['fheadersize']), 'x').lower();
6105 fsize
= format(int(flinkinfo
['fsize']), 'x').lower();
6106 flinkname
= flinkinfo
['flinkname'];
6107 fatime
= format(int(flinkinfo
['fatime']), 'x').lower();
6108 fmtime
= format(int(flinkinfo
['fmtime']), 'x').lower();
6109 fctime
= format(int(flinkinfo
['fctime']), 'x').lower();
6110 fbtime
= format(int(flinkinfo
['fbtime']), 'x').lower();
6111 fmode
= format(int(flinkinfo
['fmode']), 'x').lower();
6112 fchmode
= format(int(flinkinfo
['fchmode']), 'x').lower();
6113 fuid
= format(int(flinkinfo
['fuid']), 'x').lower();
6114 funame
= flinkinfo
['funame'];
6115 fgid
= format(int(flinkinfo
['fgid']), 'x').lower();
6116 fgname
= flinkinfo
['fgname'];
6117 finode
= format(int(flinkinfo
['finode']), 'x').lower();
6118 flinkcount
= format(int(flinkinfo
['flinkcount']), 'x').lower();
6119 fwinattributes
= format(int(flinkinfo
['fwinattributes']), 'x').lower();
6120 fcompression
= flinkinfo
['fcompression'];
6121 fcsize
= format(int(flinkinfo
['fcsize']), 'x').lower();
6122 fdev_minor
= format(int(flinkinfo
['fminor']), 'x').lower();
6123 fdev_major
= format(int(flinkinfo
['fmajor']), 'x').lower();
6124 frdev_minor
= format(int(flinkinfo
['frminor']), 'x').lower();
6125 frdev_major
= format(int(flinkinfo
['frmajor']), 'x').lower();
6126 if(len(flinkinfo
['fextralist'])>flinkinfo
['fextrafields'] and len(flinkinfo
['fextralist'])>0):
6127 flinkinfo
['fextrafields'] = len(flinkinfo
['fextralist']);
6128 if(len(extradata
)<0):
6129 extradata
= flinkinfo
['fextralist'];
6130 fcontents
= flinkinfo
['fcontents'];
6131 ftypehex
= format(flinkinfo
['ftype'], 'x').lower();
6133 ftypehex
= format(listcatfiles
['ffilelist'][reallcfi
]['ftype'], 'x').lower();
6134 fcurfid
= format(curfid
, 'x').lower();
6135 if(not followlink
and finode
!=0):
6136 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=1):
6137 fcurinode
= format(int(curinode
), 'x').lower();
6138 inodetofile
.update({curinode
: fname
});
6139 filetoinode
.update({fname
: curinode
});
6140 curinode
= curinode
+ 1;
6142 fcurinode
= format(int(filetoinode
[flinkname
]), 'x').lower();
6144 fcurinode
= format(int(curinode
), 'x').lower();
6145 curinode
= curinode
+ 1;
6146 curfid
= curfid
+ 1;
6147 if(fcompression
=="auto" or fcompression
=="none"):
6149 catoutlist
= [ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fwinattributes
, fcompression
, fcsize
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
];
6150 AppendFileHeaderWithContent(catfp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
);
6153 reallcfi
= reallcfi
+ 1;
6155 catfp
.write(AppendNullBytes([0, 0], formatspecs
[5]).encode("UTF-8"));
6156 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
6157 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
6160 os
.fsync(catfp
.fileno());
6161 except io
.UnsupportedOperation
:
6163 except AttributeError:
6165 except OSError as e
:
6169 if(hasattr(sys
.stdout
, "buffer")):
6170 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
6172 shutil
.copyfileobj(catfp
, sys
.stdout
);
6173 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
6174 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
6176 upload_file_to_internet_file(catfp
, outfile
);
6184 create_alias_function("RePack", __file_format_name__
, "", RePackArchiveFile
);
6186 def RePackArchiveFileFromString(catstr
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6187 catfp
= BytesIO(catstr
);
6188 listcatfiles
= RePackArchiveFile(catfp
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
6189 return listcatfiles
;
6191 create_alias_function("RePack", __file_format_name__
, "FromString", RePackArchiveFileFromString
);
6193 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):
6194 outarray
= BytesIO();
6195 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
6196 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
6197 return listcatfiles
;
6199 create_alias_function("Pack", __file_format_name__
, "FromListDir", PackArchiveFileFromListDir
);
6201 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):
6202 if(outdir
is not None):
6203 outdir
= RemoveWindowsPath(outdir
);
6205 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6206 if(isinstance(infile
, dict)):
6207 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6208 listcatfiles
= prelistcatfiles
['list'];
6210 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6211 infile
= RemoveWindowsPath(infile
);
6213 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6214 listcatfiles
= prelistcatfiles
['list'];
6216 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6217 if(not listcatfiles
):
6219 lenlist
= len(listcatfiles
['ffilelist']);
6220 fnumfiles
= int(listcatfiles
['fnumfiles']);
6222 lcfx
= int(listcatfiles
['fnumfiles']);
6223 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6224 lcfx
= int(lenlist
);
6226 lcfx
= int(listcatfiles
['fnumfiles']);
6232 userinfo
= pwd
.getpwuid(listcatfiles
['ffilelist'][lcfi
]['fuid']);
6233 funame
= userinfo
.pw_name
;
6242 groupinfo
= grp
.getgrgid(listcatfiles
['ffilelist'][lcfi
]['fgid']);
6243 fgname
= groupinfo
.gr_name
;
6249 VerbosePrintOut(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6250 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6251 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
6252 listcatfiles
['ffilelist'][lcfi
]['fcontents'].seek(0, 0);
6253 shutil
.copyfileobj(listcatfiles
['ffilelist'][lcfi
]['fcontents'], fpc
);
6256 os
.fsync(fpc
.fileno());
6257 except io
.UnsupportedOperation
:
6259 except AttributeError:
6261 except OSError as e
:
6263 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
6264 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
6265 if(preservepermissions
):
6266 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6268 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
6269 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6271 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6272 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
6273 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
6278 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
6279 funame
= userinfo
.pw_name
;
6288 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
6289 fgname
= groupinfo
.gr_name
;
6294 if(flinkinfo
['ftype']==0 or flinkinfo
['ftype']==7):
6295 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
6296 flinkinfo
['fcontents'].seek(0, 0);
6297 shutil
.copyfileobj(flinkinfo
['fcontents'], fpc
);
6300 os
.fsync(fpc
.fileno());
6301 except io
.UnsupportedOperation
:
6303 except AttributeError:
6305 except OSError as e
:
6307 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6308 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6309 if(preservepermissions
):
6310 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6312 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6313 if(flinkinfo
['ftype']==1):
6314 os
.link(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6315 if(flinkinfo
['ftype']==2):
6316 os
.symlink(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6317 if(flinkinfo
['ftype']==5):
6318 if(preservepermissions
):
6319 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6321 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6322 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6323 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6324 if(preservepermissions
):
6325 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6327 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6328 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
6329 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6331 os
.link(listcatfiles
['ffilelist'][lcfi
]['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6332 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6334 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6335 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
6336 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
6341 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
6342 funame
= userinfo
.pw_name
;
6351 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
6352 fgname
= groupinfo
.gr_name
;
6357 if(flinkinfo
['ftype']==0 or flinkinfo
['ftype']==7):
6358 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
6359 flinkinfo
['fcontents'].seek(0, 0);
6360 shutil
.copyfileobj(flinkinfo
['fcontents'], fpc
);
6363 os
.fsync(fpc
.fileno());
6364 except io
.UnsupportedOperation
:
6366 except AttributeError:
6368 except OSError as e
:
6370 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6371 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6372 if(preservepermissions
):
6373 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6375 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6376 if(flinkinfo
['ftype']==1):
6377 os
.link(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6378 if(flinkinfo
['ftype']==2):
6379 os
.symlink(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6380 if(flinkinfo
['ftype']==5):
6381 if(preservepermissions
):
6382 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6384 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6385 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6386 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6387 if(preservepermissions
):
6388 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6390 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6391 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
6392 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6394 os
.symlink(listcatfiles
['ffilelist'][lcfi
]['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6395 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6396 if(preservepermissions
):
6397 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6399 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6400 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
6401 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
6402 if(preservepermissions
):
6403 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6405 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
6406 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6 and hasattr(os
, "mkfifo")):
6407 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6410 return listcatfiles
['ffilelist']['catfp'];
6414 create_alias_function("UnPack", __file_format_name__
, "", UnPackArchiveFile
);
6416 if(hasattr(shutil
, "register_unpack_format")):
6417 def UnPackArchiveFileFunc(archive_name
, extract_dir
=None, **kwargs
):
6418 return UnPackArchiveFile(archive_name
, extract_dir
, False, 0, 0, False, __file_format_delimiter__
, False, False);
6419 create_alias_function("UnPack", __file_format_name__
, "Func", UnPackArchiveFileFunc
);
6421 def UnPackArchiveFileString(catstr
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6422 catfp
= BytesIO(catstr
);
6423 listcatfiles
= UnPackArchiveFile(catfp
, outdir
, followlink
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6424 return listcatfiles
;
6426 create_alias_function("UnPack", __file_format_name__
, "String", UnPackArchiveFileString
);
6428 def ArchiveFileListFiles(infile
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6429 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6430 if(isinstance(infile
, dict)):
6431 listcatfiles
= infile
;
6433 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6434 infile
= RemoveWindowsPath(infile
);
6435 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, True, skipchecksum
, formatspecs
, returnfp
);
6436 if(not listcatfiles
):
6438 lenlist
= len(listcatfiles
['ffilelist']);
6439 fnumfiles
= int(listcatfiles
['fnumfiles']);
6441 lcfx
= int(listcatfiles
['fnumfiles']);
6442 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6443 lcfx
= int(lenlist
);
6445 lcfx
= int(listcatfiles
['fnumfiles']);
6448 returnval
.update({lcfi
: listcatfiles
['ffilelist'][lcfi
]['fname']});
6450 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
6452 permissions
= { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
6453 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'];
6454 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6455 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " link to " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6456 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6457 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " -> " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6458 fuprint
= listcatfiles
['ffilelist'][lcfi
]['funame'];
6459 if(len(fuprint
)<=0):
6460 fuprint
= listcatfiles
['ffilelist'][lcfi
]['fuid'];
6461 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgname'];
6462 if(len(fgprint
)<=0):
6463 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgid'];
6464 VerbosePrintOut(PrintPermissionString(listcatfiles
['ffilelist'][lcfi
]['fmode'], listcatfiles
['ffilelist'][lcfi
]['ftype']) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(listcatfiles
['ffilelist'][lcfi
]['fsize']).rjust(15) + " " + datetime
.datetime
.utcfromtimestamp(listcatfiles
['ffilelist'][lcfi
]['fmtime']).strftime('%Y-%m-%d %H:%M') + " " + printfname
));
6467 return listcatfiles
['catfp'];
6471 create_alias_function("", __file_format_name__
, "ListFiles", ArchiveFileListFiles
);
6473 def ArchiveFileStringListFiles(catstr
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6474 catfp
= BytesIO(catstr
);
6475 listcatfiles
= ArchiveFileListFiles(catstr
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6476 return listcatfiles
;
6478 create_alias_function("", __file_format_name__
, "StringListFiles", ArchiveFileStringListFiles
);
6480 def TarFileListFiles(infile
, verbose
=False, returnfp
=False):
6481 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6484 if(hasattr(sys
.stdin
, "buffer")):
6485 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
6487 shutil
.copyfileobj(sys
.stdin
, infile
);
6492 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
6493 infile
= download_file_from_internet_file(infile
);
6498 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6500 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
6502 if(not tarfile
.is_tarfile(infile
)):
6504 except AttributeError:
6505 if(not is_tarfile(infile
)):
6510 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
6511 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
6513 tarfp
= tarfile
.open(infile
, "r");
6514 except FileNotFoundError
:
6518 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
6519 returnval
.update({lcfi
: member
.name
});
6520 fpremode
= member
.mode
;
6521 ffullmode
= member
.mode
;
6525 ffullmode
= member
.mode
+ stat
.S_IFREG
;
6527 elif(member
.isdev()):
6528 ffullmode
= member
.mode
;
6530 elif(member
.islnk()):
6531 ffullmode
= member
.mode
+ stat
.S_IFREG
;
6533 elif(member
.issym()):
6534 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
6536 elif(member
.ischr()):
6537 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
6539 elif(member
.isblk()):
6540 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
6542 elif(member
.isdir()):
6543 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
6545 elif(member
.isfifo()):
6546 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
6548 elif(member
.issparse()):
6549 ffullmode
= member
.mode
;
6552 VerbosePrintOut(member
.name
);
6554 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' } };
6555 printfname
= member
.name
;
6557 printfname
= member
.name
+ " link to " + member
.linkname
;
6558 elif(member
.issym()):
6559 printfname
= member
.name
+ " -> " + member
.linkname
;
6560 fuprint
= member
.uname
;
6561 if(len(fuprint
)<=0):
6562 fuprint
= member
.uid
;
6563 fgprint
= member
.gname
;
6564 if(len(fgprint
)<=0):
6565 fgprint
= member
.gid
;
6566 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
));
6569 return listcatfiles
['catfp'];
6573 def ZipFileListFiles(infile
, verbose
=False, returnfp
=False):
6574 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6577 if(hasattr(sys
.stdin
, "buffer")):
6578 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
6580 shutil
.copyfileobj(sys
.stdin
, infile
);
6585 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
6586 infile
= download_file_from_internet_file(infile
);
6591 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6595 if(not zipfile
.is_zipfile(infile
)):
6598 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
6599 except FileNotFoundError
:
6603 ziptest
= zipfp
.testzip();
6605 VerbosePrintOut("Bad file found!");
6606 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
6607 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
6608 fwinattributes
= int(zipinfo
.external_attr
);
6609 if(not member
.is_dir()):
6610 fmode
= int(stat
.S_IFREG
+ 438);
6611 fchmode
= int(stat
.S_IMODE(fmode
));
6612 ftypemod
= int(stat
.S_IFMT(fmode
));
6613 elif(member
.is_dir()):
6614 fmode
= int(stat
.S_IFDIR
+ 511);
6615 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
6616 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
6617 elif(zipinfo
.create_system
==3):
6618 fwinattributes
=int(0);
6619 fmode
= int(zipinfo
.external_attr
);
6620 fchmode
= int(stat
.S_IMODE(fmode
));
6621 ftypemod
= int(stat
.S_IFMT(fmode
));
6623 fwinattributes
= int(0);
6624 if(not member
.is_dir()):
6625 fmode
= int(stat
.S_IFREG
+ 438);
6626 fchmode
= int(stat
.S_IMODE(fmode
));
6627 ftypemod
= int(stat
.S_IFMT(fmode
));
6628 elif(member
.is_dir()):
6629 fmode
= int(stat
.S_IFDIR
+ 511);
6630 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
6631 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
6632 returnval
.update({lcfi
: member
.filename
});
6634 VerbosePrintOut(member
.filename
);
6636 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' } };
6638 for fmodval
in str(oct(fmode
))[-3:]:
6639 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
6640 if(not member
.is_dir()):
6642 permissionstr
= "-" + permissionstr
;
6643 elif(member
.is_dir()):
6645 permissionstr
= "d" + permissionstr
;
6646 printfname
= member
.filename
;
6648 fuid
= int(os
.getuid());
6649 except AttributeError:
6654 fgid
= int(os
.getgid());
6655 except AttributeError:
6662 userinfo
= pwd
.getpwuid(os
.getuid());
6663 funame
= userinfo
.pw_name
;
6666 except AttributeError:
6674 groupinfo
= grp
.getgrgid(os
.getgid());
6675 fgname
= groupinfo
.gr_name
;
6678 except AttributeError:
6683 if(len(fuprint
)<=0):
6684 fuprint
= str(fuid
);
6686 if(len(fgprint
)<=0):
6687 fgprint
= str(fgid
);
6688 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
));
6691 return listcatfiles
['catfp'];
6695 if(not rarfile_support
):
6696 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
6697 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6698 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6701 if(rarfile_support
):
6702 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
6703 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6704 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6706 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
6710 rarfp
= rarfile
.RarFile(infile
, "r");
6711 rartest
= rarfp
.testrar();
6713 VerbosePrintOut("Bad file found!");
6714 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
6717 if(member
.host_os
==rarfile
.RAR_OS_UNIX
):
6720 member
.external_attr
6722 except AttributeError:
6724 elif(member
.host_os
==rarfile
.RAR_OS_WIN32
):
6727 member
.external_attr
6729 except AttributeError:
6734 if(is_unix
and member
.external_attr
!=0):
6735 fpremode
= int(member
.external_attr
);
6736 elif(member
.is_file()):
6737 fpremode
= int(stat
.S_IFREG
+ 438);
6738 elif(member
.is_symlink()):
6739 fpremode
= int(stat
.S_IFLNK
+ 438);
6740 elif(member
.is_dir()):
6741 fpremode
= int(stat
.S_IFDIR
+ 511);
6742 if(is_windows
and member
.external_attr
!=0):
6743 fwinattributes
= int(member
.external_attr
);
6745 fwinattributes
= int(0);
6746 if(is_unix
and member
.external_attr
!=0):
6747 fmode
= int(member
.external_attr
);
6748 fchmode
= int(stat
.S_IMODE(member
.external_attr
));
6749 ftypemod
= int(stat
.S_IFMT(member
.external_attr
));
6750 elif(member
.is_file()):
6751 fmode
= int(stat
.S_IFREG
+ 438);
6752 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
6753 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
6754 elif(member
.is_symlink()):
6755 fmode
= int(stat
.S_IFLNK
+ 438);
6756 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFLNK
+ 438)));
6757 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFLNK
+ 438)));
6758 elif(member
.is_dir()):
6759 fmode
= int(stat
.S_IFDIR
+ 511);
6760 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
6761 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
6762 returnval
.update({lcfi
: member
.filename
});
6764 VerbosePrintOut(member
.filename
);
6766 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' } };
6768 for fmodval
in str(oct(fmode
))[-3:]:
6769 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
6770 if(member
.is_file()):
6772 permissionstr
= "-" + permissionstr
;
6773 printfname
= member
.filename
;
6774 elif(member
.is_symlink()):
6776 permissionstr
= "l" + permissionstr
;
6777 printfname
= member
.name
+ " -> " + member
.read().decode("UTF-8");
6778 elif(member
.is_dir()):
6780 permissionstr
= "d" + permissionstr
;
6781 printfname
= member
.filename
;
6783 fuid
= int(os
.getuid());
6784 except AttributeError:
6789 fgid
= int(os
.getgid());
6790 except AttributeError:
6797 userinfo
= pwd
.getpwuid(os
.getuid());
6798 funame
= userinfo
.pw_name
;
6801 except AttributeError:
6809 groupinfo
= grp
.getgrgid(os
.getgid());
6810 fgname
= groupinfo
.gr_name
;
6813 except AttributeError:
6818 if(len(fuprint
)<=0):
6819 fuprint
= str(fuid
);
6821 if(len(fgprint
)<=0):
6822 fgprint
= str(fgid
);
6823 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(member
.file_size
).rjust(15) + " " + member
.mtime
.strftime('%Y-%m-%d %H:%M') + " " + printfname
));
6826 return listcatfiles
['catfp'];
6830 if(not py7zr_support
):
6831 def SevenZipFileListFiles(infile
, verbose
=False, returnfp
=False):
6832 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6833 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6837 def SevenZipFileListFiles(infile
, verbose
=False, returnfp
=False):
6838 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6839 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6843 szpfp
= py7zr
.SevenZipFile(infile
, mode
="r");
6844 file_content
= szpfp
.readall();
6845 #sztest = szpfp.testzip();
6846 sztestalt
= szpfp
.test();
6848 VerbosePrintOut("Bad file found!");
6849 for member
in sorted(szpfp
.list(), key
=lambda x
: x
.filename
):
6850 if(re
.findall("^[.|/]", member
.filename
)):
6851 fname
= member
.filename
;
6853 fname
= "./"+member
.filename
;
6854 if(not member
.is_directory
):
6855 fpremode
= int(stat
.S_IFREG
+ 438);
6856 elif(member
.is_directory
):
6857 fpremode
= int(stat
.S_IFDIR
+ 511);
6858 fwinattributes
= int(0);
6859 if(member
.is_directory
):
6860 fmode
= int(stat
.S_IFDIR
+ 511);
6861 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
6862 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
6864 fmode
= int(stat
.S_IFLNK
+ 438);
6865 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFLNK
+ 438)));
6866 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFLNK
+ 438)));
6867 returnval
.update({lcfi
: member
.filename
});
6869 VerbosePrintOut(member
.filename
);
6871 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' } };
6873 for fmodval
in str(oct(fmode
))[-3:]:
6874 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
6876 if(not member
.is_directory
):
6878 permissionstr
= "-" + permissionstr
;
6879 printfname
= member
.filename
;
6880 elif(member
.is_directory
):
6882 permissionstr
= "d" + permissionstr
;
6883 printfname
= member
.filename
;
6885 fsize
= len(file_content
[member
.filename
].read());
6886 file_content
[member
.filename
].close();
6888 fuid
= int(os
.getuid());
6889 except AttributeError:
6894 fgid
= int(os
.getgid());
6895 except AttributeError:
6902 userinfo
= pwd
.getpwuid(os
.getuid());
6903 funame
= userinfo
.pw_name
;
6906 except AttributeError:
6914 groupinfo
= grp
.getgrgid(os
.getgid());
6915 fgname
= groupinfo
.gr_name
;
6918 except AttributeError:
6923 if(len(fuprint
)<=0):
6924 fuprint
= str(fuid
);
6926 if(len(fgprint
)<=0):
6927 fgprint
= str(fgid
);
6928 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(fsize
).rjust(15) + " " + member
.creationtime
.strftime('%Y-%m-%d %H:%M') + " " + printfname
));
6931 return listcatfiles
['catfp'];
6935 def InFileListFiles(infile
, verbose
=False, formatspecs
=__file_format_list__
, returnfp
=False):
6936 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6937 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
6938 if(checkcompressfile
=="tarfile"):
6939 return TarFileListFiles(infile
, verbose
, returnfp
);
6940 elif(checkcompressfile
=="zipfile"):
6941 return ZipFileListFiles(infile
, verbose
, returnfp
);
6942 elif(checkcompressfile
=="catfile"):
6943 return ArchiveFileListFiles(infile
, 0, 0, False, formatspecs
, verbose
, returnfp
);
6944 elif(rarfile_support
and checkcompressfile
=="rarfile"):
6945 return RarFileListFiles(infile
, verbose
, returnfp
);
6946 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
6947 return SevenZipFileListFiles(infile
, verbose
, returnfp
);
6952 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):
6953 outarray
= BytesIO();
6954 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, False, True);
6955 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6956 return listcatfiles
;
6958 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):
6959 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, formatspecs
, verbose
);
6960 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6961 return listcatfiles
;
6963 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):
6964 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, False, checksumtype
, extradata
, formatspecs
, False);
6965 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, followlink
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
6966 return listcatfiles
;
6968 create_alias_function("Pack", __file_format_name__
, "FromListDirAlt", PackArchiveFileFromListDirAlt
);
6970 def PackArchiveFileFromTarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6971 outarray
= TarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
6972 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
6973 return listcatfiles
;
6975 create_alias_function("Pack", __file_format_name__
, "FromTarFileAlt", PackArchiveFileFromTarFileAlt
);
6977 def PackArchiveFileFromZipFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6978 outarray
= ZipFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
6979 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
6980 return listcatfiles
;
6982 create_alias_function("Pack", __file_format_name__
, "FromZipFileAlt", PackArchiveFileFromZipFileAlt
);
6984 if(not rarfile_support
):
6985 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6988 if(rarfile_support
):
6989 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6990 outarray
= RarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
6991 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
6992 return listcatfiles
;
6994 create_alias_function("Pack", __file_format_name__
, "FromRarFileAlt", PackArchiveFileFromRarFileAlt
);
6996 if(not py7zr_support
):
6997 def PackArchiveFileFromSevenZipFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7001 def PackArchiveFileFromSevenZipFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
7002 outarray
= SevenZipFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
7003 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
7004 return listcatfiles
;
7006 create_alias_function("Pack", __file_format_name__
, "FromSevenZipFileAlt", PackArchiveFileFromSevenZipFileAlt
);
7008 def download_file_from_ftp_file(url
):
7009 urlparts
= urlparse(url
);
7010 file_name
= os
.path
.basename(urlparts
.path
);
7011 file_dir
= os
.path
.dirname(urlparts
.path
);
7012 if(urlparts
.username
is not None):
7013 ftp_username
= urlparts
.username
;
7015 ftp_username
= "anonymous";
7016 if(urlparts
.password
is not None):
7017 ftp_password
= urlparts
.password
;
7018 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7019 ftp_password
= "anonymous";
7022 if(urlparts
.scheme
=="ftp"):
7024 elif(urlparts
.scheme
=="ftps"):
7028 if(urlparts
.scheme
=="sftp"):
7030 return download_file_from_pysftp_file(url
);
7032 return download_file_from_sftp_file(url
);
7033 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7034 return download_file_from_http_file(url
);
7035 ftp_port
= urlparts
.port
;
7036 if(urlparts
.port
is None):
7039 ftp
.connect(urlparts
.hostname
, ftp_port
);
7040 except socket
.gaierror
:
7041 log
.info("Error With URL "+url
);
7043 except socket
.timeout
:
7044 log
.info("Error With URL "+url
);
7046 ftp
.login(urlparts
.username
, urlparts
.password
);
7047 if(urlparts
.scheme
=="ftps"):
7049 ftpfile
= BytesIO();
7050 ftp
.retrbinary("RETR "+urlparts
.path
, ftpfile
.write
);
7051 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
7056 def download_file_from_ftp_string(url
):
7057 ftpfile
= download_file_from_ftp_file(url
);
7058 return ftpfile
.read();
7060 def upload_file_to_ftp_file(ftpfile
, url
):
7061 urlparts
= urlparse(url
);
7062 file_name
= os
.path
.basename(urlparts
.path
);
7063 file_dir
= os
.path
.dirname(urlparts
.path
);
7064 if(urlparts
.username
is not None):
7065 ftp_username
= urlparts
.username
;
7067 ftp_username
= "anonymous";
7068 if(urlparts
.password
is not None):
7069 ftp_password
= urlparts
.password
;
7070 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7071 ftp_password
= "anonymous";
7074 if(urlparts
.scheme
=="ftp"):
7076 elif(urlparts
.scheme
=="ftps"):
7080 if(urlparts
.scheme
=="sftp"):
7082 return upload_file_to_pysftp_file(url
);
7084 return upload_file_to_sftp_file(url
);
7085 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7087 ftp_port
= urlparts
.port
;
7088 if(urlparts
.port
is None):
7091 ftp
.connect(urlparts
.hostname
, ftp_port
);
7092 except socket
.gaierror
:
7093 log
.info("Error With URL "+url
);
7095 except socket
.timeout
:
7096 log
.info("Error With URL "+url
);
7098 ftp
.login(urlparts
.username
, urlparts
.password
);
7099 if(urlparts
.scheme
=="ftps"):
7101 ftp
.storbinary("STOR "+urlparts
.path
, ftpfile
);
7106 def upload_file_to_ftp_string(ftpstring
, url
):
7107 ftpfileo
= BytesIO(ftpstring
);
7108 ftpfile
= upload_file_to_ftp_file(ftpfileo
, url
);
7112 def download_file_from_http_file(url
, headers
=geturls_headers_pycatfile_python_alt
):
7113 # Parse the URL to extract username and password if present
7114 urlparts
= urlparse(url
);
7115 username
= urlparts
.username
;
7116 password
= urlparts
.password
;
7117 # Rebuild the URL without the username and password
7118 netloc
= urlparts
.hostname
;
7119 if(urlparts
.scheme
=="sftp"):
7121 return download_file_from_pysftp_file(url
);
7123 return download_file_from_sftp_file(url
);
7124 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7125 return download_file_from_ftp_file(url
);
7127 netloc
+= ':' + str(urlparts
.port
);
7128 rebuilt_url
= urlunparse((urlparts
.scheme
, netloc
, urlparts
.path
, urlparts
.params
, urlparts
.query
, urlparts
.fragment
));
7129 # Create a temporary file object
7130 httpfile
= BytesIO();
7132 # Use the requests library if available
7133 if username
and password
:
7134 response
= requests
.get(rebuilt_url
, headers
=headers
, auth
=(username
, password
), stream
=True);
7136 response
= requests
.get(rebuilt_url
, headers
=headers
, stream
=True);
7137 response
.raw
.decode_content
= True
7138 shutil
.copyfileobj(response
.raw
, httpfile
);
7140 # Build a Request object for urllib
7141 request
= Request(rebuilt_url
, headers
=headers
);
7142 # Create an opener object for handling URLs
7143 if username
and password
:
7144 # Create a password manager
7145 password_mgr
= HTTPPasswordMgrWithDefaultRealm();
7146 # Add the username and password
7147 password_mgr
.add_password(None, rebuilt_url
, username
, password
);
7148 # Create an authentication handler using the password manager
7149 auth_handler
= HTTPBasicAuthHandler(password_mgr
);
7150 # Build the opener with the authentication handler
7151 opener
= build_opener(auth_handler
);
7153 opener
= build_opener();
7154 with opener
.open(request
) as response
:
7155 shutil
.copyfileobj(response
, httpfile
);
7156 # Reset file pointer to the start
7157 httpfile
.seek(0, 0);
7158 # Return the temporary file object
7161 def download_file_from_http_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
7162 httpfile
= download_file_from_http_file(url
, headers
);
7163 return ftpfile
.read();
7166 def download_file_from_sftp_file(url
):
7167 urlparts
= urlparse(url
);
7168 file_name
= os
.path
.basename(urlparts
.path
);
7169 file_dir
= os
.path
.dirname(urlparts
.path
);
7170 sftp_port
= urlparts
.port
;
7171 if(urlparts
.port
is None):
7174 sftp_port
= urlparts
.port
;
7175 if(urlparts
.username
is not None):
7176 sftp_username
= urlparts
.username
;
7178 sftp_username
= "anonymous";
7179 if(urlparts
.password
is not None):
7180 sftp_password
= urlparts
.password
;
7181 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7182 sftp_password
= "anonymous";
7185 if(urlparts
.scheme
=="ftp"):
7186 return download_file_from_ftp_file(url
);
7187 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7188 return download_file_from_http_file(url
);
7189 if(urlparts
.scheme
!="sftp"):
7191 ssh
= paramiko
.SSHClient();
7192 ssh
.load_system_host_keys();
7193 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
7195 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7196 except paramiko
.ssh_exception
.SSHException
:
7198 except socket
.gaierror
:
7199 log
.info("Error With URL "+url
);
7201 except socket
.timeout
:
7202 log
.info("Error With URL "+url
);
7204 sftp
= ssh
.open_sftp();
7205 sftpfile
= BytesIO();
7206 sftp
.getfo(urlparts
.path
, sftpfile
);
7209 sftpfile
.seek(0, 0);
7212 def download_file_from_sftp_file(url
):
7216 def download_file_from_sftp_string(url
):
7217 sftpfile
= download_file_from_sftp_file(url
);
7218 return sftpfile
.read();
7220 def download_file_from_ftp_string(url
):
7224 def upload_file_to_sftp_file(sftpfile
, url
):
7225 urlparts
= urlparse(url
);
7226 file_name
= os
.path
.basename(urlparts
.path
);
7227 file_dir
= os
.path
.dirname(urlparts
.path
);
7228 sftp_port
= urlparts
.port
;
7229 if(urlparts
.port
is None):
7232 sftp_port
= urlparts
.port
;
7233 if(urlparts
.username
is not None):
7234 sftp_username
= urlparts
.username
;
7236 sftp_username
= "anonymous";
7237 if(urlparts
.password
is not None):
7238 sftp_password
= urlparts
.password
;
7239 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7240 sftp_password
= "anonymous";
7243 if(urlparts
.scheme
=="ftp"):
7244 return upload_file_to_ftp_file(url
);
7245 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7247 if(urlparts
.scheme
!="sftp"):
7249 ssh
= paramiko
.SSHClient();
7250 ssh
.load_system_host_keys();
7251 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
7253 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7254 except paramiko
.ssh_exception
.SSHException
:
7256 except socket
.gaierror
:
7257 log
.info("Error With URL "+url
);
7259 except socket
.timeout
:
7260 log
.info("Error With URL "+url
);
7262 sftp
= ssh
.open_sftp();
7263 sftp
.putfo(sftpfile
, urlparts
.path
);
7266 sftpfile
.seek(0, 0);
7269 def upload_file_to_sftp_file(sftpfile
, url
):
7273 def upload_file_to_sftp_string(sftpstring
, url
):
7274 sftpfileo
= BytesIO(sftpstring
);
7275 sftpfile
= upload_file_to_sftp_files(ftpfileo
, url
);
7279 def upload_file_to_sftp_string(url
):
7283 def download_file_from_pysftp_file(url
):
7284 urlparts
= urlparse(url
);
7285 file_name
= os
.path
.basename(urlparts
.path
);
7286 file_dir
= os
.path
.dirname(urlparts
.path
);
7287 sftp_port
= urlparts
.port
;
7288 if(urlparts
.port
is None):
7291 sftp_port
= urlparts
.port
;
7292 if(urlparts
.username
is not None):
7293 sftp_username
= urlparts
.username
;
7295 sftp_username
= "anonymous";
7296 if(urlparts
.password
is not None):
7297 sftp_password
= urlparts
.password
;
7298 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7299 sftp_password
= "anonymous";
7302 if(urlparts
.scheme
=="ftp"):
7303 return download_file_from_ftp_file(url
);
7304 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7305 return download_file_from_http_file(url
);
7306 if(urlparts
.scheme
!="sftp"):
7309 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7310 except paramiko
.ssh_exception
.SSHException
:
7312 except socket
.gaierror
:
7313 log
.info("Error With URL "+url
);
7315 except socket
.timeout
:
7316 log
.info("Error With URL "+url
);
7318 sftp
= ssh
.open_sftp();
7319 sftpfile
= BytesIO();
7320 sftp
.getfo(urlparts
.path
, sftpfile
);
7323 sftpfile
.seek(0, 0);
7326 def download_file_from_pysftp_file(url
):
7330 def download_file_from_pysftp_string(url
):
7331 sftpfile
= download_file_from_pysftp_file(url
);
7332 return sftpfile
.read();
7334 def download_file_from_ftp_string(url
):
7338 def upload_file_to_pysftp_file(sftpfile
, url
):
7339 urlparts
= urlparse(url
);
7340 file_name
= os
.path
.basename(urlparts
.path
);
7341 file_dir
= os
.path
.dirname(urlparts
.path
);
7342 sftp_port
= urlparts
.port
;
7343 if(urlparts
.port
is None):
7346 sftp_port
= urlparts
.port
;
7347 if(urlparts
.username
is not None):
7348 sftp_username
= urlparts
.username
;
7350 sftp_username
= "anonymous";
7351 if(urlparts
.password
is not None):
7352 sftp_password
= urlparts
.password
;
7353 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7354 sftp_password
= "anonymous";
7357 if(urlparts
.scheme
=="ftp"):
7358 return upload_file_to_ftp_file(url
);
7359 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7361 if(urlparts
.scheme
!="sftp"):
7364 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7365 except paramiko
.ssh_exception
.SSHException
:
7367 except socket
.gaierror
:
7368 log
.info("Error With URL "+url
);
7370 except socket
.timeout
:
7371 log
.info("Error With URL "+url
);
7373 sftp
= ssh
.open_sftp();
7374 sftp
.putfo(sftpfile
, urlparts
.path
);
7377 sftpfile
.seek(0, 0);
7380 def upload_file_to_pysftp_file(sftpfile
, url
):
7384 def upload_file_to_pysftp_string(sftpstring
, url
):
7385 sftpfileo
= BytesIO(sftpstring
);
7386 sftpfile
= upload_file_to_pysftp_files(ftpfileo
, url
);
7390 def upload_file_to_pysftp_string(url
):
7393 def download_file_from_internet_file(url
, headers
=geturls_headers_pycatfile_python_alt
):
7394 urlparts
= urlparse(url
);
7395 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7396 return download_file_from_http_file(url
, headers
);
7397 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7398 return download_file_from_ftp_file(url
);
7399 elif(urlparts
.scheme
=="sftp"):
7400 if(__use_pysftp__
and havepysftp
):
7401 return download_file_from_pysftp_file(url
);
7403 return download_file_from_sftp_file(url
);
7408 def download_file_from_internet_uncompress_file(url
, headers
=geturls_headers_pycatfile_python_alt
, formatspecs
=__file_format_list__
):
7409 fp
= download_file_from_internet_file(url
);
7410 fp
= UncompressArchiveFile(fp
, formatspecs
);
7416 def download_file_from_internet_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
7417 urlparts
= urlparse(url
);
7418 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7419 return download_file_from_http_string(url
, headers
);
7420 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7421 return download_file_from_ftp_string(url
);
7422 elif(urlparts
.scheme
=="sftp"):
7423 if(__use_pysftp__
and havepysftp
):
7424 return download_file_from_pysftp_string(url
);
7426 return download_file_from_sftp_string(url
);
7431 def download_file_from_internet_uncompress_string(url
, headers
=geturls_headers_pycatfile_python_alt
, formatspecs
=__file_format_list__
):
7432 fp
= download_file_from_internet_string(url
);
7433 fp
= UncompressArchiveFile(fp
, formatspecs
);
7439 def upload_file_to_internet_file(ifp
, url
):
7440 urlparts
= urlparse(url
);
7441 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7443 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7444 return upload_file_to_ftp_file(ifp
, url
);
7445 elif(urlparts
.scheme
=="sftp"):
7446 if(__use_pysftp__
and havepysftp
):
7447 return upload_file_to_pysftp_file(ifp
, url
);
7449 return upload_file_to_sftp_file(ifp
, url
);
7454 def upload_file_to_internet_compress_file(ifp
, url
, formatspecs
=__file_format_list__
):
7455 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
7459 upload_file_to_internet_file(catfp
, outfile
);
7462 def upload_file_to_internet_string(ifp
, url
):
7463 urlparts
= urlparse(url
);
7464 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7466 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7467 return upload_file_to_ftp_string(ifp
, url
);
7468 elif(urlparts
.scheme
=="sftp"):
7469 if(__use_pysftp__
and havepysftp
):
7470 return upload_file_to_pysftp_string(ifp
, url
);
7472 return upload_file_to_sftp_string(ifp
, url
);
7477 def upload_file_to_internet_compress_string(ifp
, url
, formatspecs
=__file_format_list__
):
7478 catfp
= CompressArchiveFile(BytesIO(ifp
), compression
, compressionlevel
, formatspecs
);
7482 upload_file_to_internet_file(catfp
, outfile
);
7486 if(hasattr(shutil
, "register_archive_format")):
7487 # Register the packing format
7488 shutil
.register_archive_format(__file_format_name__
, PackArchiveFileFunc
, description
='Pack concatenated files');
7489 except shutil
.RegistryError
:
7493 if(hasattr(shutil
, "register_unpack_format")):
7494 # Register the unpacking format
7495 shutil
.register_unpack_format(__file_format_name__
, archivefile_extensions
, UnPackArchiveFileFunc
, description
='UnPack concatenated files');
7496 except shutil
.RegistryError
: