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/12/2024 Ver. 0.11.0 RC 1 - Author: cooldude2k $
20 from __future__
import absolute_import
, division
, print_function
, unicode_literals
;
21 import io
, os
, re
, sys
, time
, stat
, zlib
, base64
, shutil
, socket
, hashlib
, datetime
, logging
, binascii
, tempfile
, zipfile
, platform
;
25 from ftplib
import FTP
, FTP_TLS
;
28 from ftplib
import FTP
;
29 if(sys
.version
[0]=="2"):
30 from urlparse
import urlparse
, urlunparse
;
31 elif(sys
.version
[0]>="3"):
32 from urllib
.parse
import urlunparse
;
33 from urllib
.parse
import urlparse
;
35 if os
.name
== 'nt': # Only modify if on Windows
36 if sys
.version
[0] == "2":
38 sys
.stdout
= codecs
.getwriter('utf-8')(sys
.stdout
);
39 sys
.stderr
= codecs
.getwriter('utf-8')(sys
.stderr
);
42 sys
.stdout
= io
.TextIOWrapper(sys
.stdout
.buffer, encoding
='utf-8', errors
='replace', line_buffering
=True);
43 sys
.stderr
= io
.TextIOWrapper(sys
.stderr
.buffer, encoding
='utf-8', errors
='replace', line_buffering
=True);
45 hashlib_guaranteed
= False;
46 os
.environ
["PYTHONIOENCODING"] = "UTF-8";
47 os
.environ
["LC_CTYPE"] = "UTF-8";
51 sys
.setdefaultencoding('UTF-8');
54 except AttributeError:
58 except AttributeError:
62 from zlib
import crc32
;
64 from binascii
import crc32
;
66 if(sys
.version_info
[0]==2):
67 FileNotFoundError
= IOError;
69 rarfile_support
= False;
72 rarfile_support
= True;
74 rarfile_support
= False;
76 py7zr_support
= False;
81 py7zr_support
= False;
84 from xtarfile
import is_tarfile
;
87 from safetar
import is_tarfile
;
89 from tarfile
import is_tarfile
;
92 import xtarfile
as tarfile
;
95 import safetar
as tarfile
;
104 haveparamiko
= False;
113 haverequests
= False;
118 haverequests
= False;
122 logging
.getLogger("urllib3").setLevel(logging
.WARNING
);
126 from urllib
.request
import Request
, build_opener
, HTTPBasicAuthHandler
;
127 from urllib
.parse
import urlparse
;
130 from urllib2
import Request
, build_opener
, HTTPBasicAuthHandler
;
131 from urlparse
import urlparse
;
133 if(sys
.version
[0]=="2"):
135 from io
import StringIO
, BytesIO
;
138 from cStringIO
import StringIO
;
139 from cStringIO
import StringIO
as BytesIO
;
141 from StringIO
import StringIO
;
142 from StringIO
import StringIO
as BytesIO
;
143 elif(sys
.version
[0]>="3"):
144 from io
import StringIO
, BytesIO
;
149 from cStringIO
import StringIO
as BytesIO
;
155 from StringIO
import StringIO
as BytesIO
;
161 from io
import BytesIO
;
166 __use_pysftp__
= False;
168 __use_pysftp__
= False;
169 __file_format_name__
= "CatFile";
170 __program_name__
= "Py"+__file_format_name__
;
171 __file_format_lower__
= __file_format_name__
.lower();
172 __file_format_magic__
= __file_format_name__
;
173 __file_format_len__
= len(__file_format_magic__
);
174 __file_format_hex__
= binascii
.hexlify(__file_format_magic__
.encode("UTF-8")).decode("UTF-8");
175 __file_format_delimiter__
= "\x00";
176 __file_format_ver__
= "001";
177 __use_new_style__
= True;
178 __use_advanced_list__
= True;
179 __use_alt_inode__
= False;
180 __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__
];
181 __file_format_dict__
= {'format_name': __file_format_name__
, 'format_magic': __file_format_magic__
, 'format_lower': __file_format_lower__
, 'format_len': __file_format_len__
, 'format_hex': __file_format_hex__
, 'format_delimiter': __file_format_delimiter__
, 'format_ver': __file_format_ver__
, 'new_style': __use_new_style__
, 'use_advanced_list': __use_advanced_list__
, 'use_alt_inode': __use_alt_inode__
};
182 __project__
= __program_name__
;
183 __project_url__
= "https://github.com/GameMaker2k/PyCatFile";
184 __version_info__
= (0, 11, 0, "RC 1", 1);
185 __version_date_info__
= (2024, 5, 12, "RC 1", 1);
186 __version_date__
= str(__version_date_info__
[0]) + "." + str(__version_date_info__
[1]).zfill(2) + "." + str(__version_date_info__
[2]).zfill(2);
187 __revision__
= __version_info__
[3];
188 __revision_id__
= "$Id$";
189 if(__version_info__
[4] is not None):
190 __version_date_plusrc__
= __version_date__
+ "-" + str(__version_date_info__
[4]);
191 if(__version_info__
[4] is None):
192 __version_date_plusrc__
= __version_date__
;
193 if(__version_info__
[3] is not None):
194 __version__
= str(__version_info__
[0]) + "." + str(__version_info__
[1]) + "." + str(__version_info__
[2]) + " " + str(__version_info__
[3]);
195 if(__version_info__
[3] is None):
196 __version__
= str(__version_info__
[0]) + "." + str(__version_info__
[1]) + "." + str(__version_info__
[2]);
198 PyBitness
= platform
.architecture();
199 if(PyBitness
=="32bit" or PyBitness
=="32"):
201 elif(PyBitness
=="64bit" or PyBitness
=="64"):
206 geturls_ua_pycatfile_python
= "Mozilla/5.0 (compatible; {proname}/{prover}; +{prourl})".format(proname
=__project__
, prover
=__version__
, prourl
=__project_url__
);
207 if(platform
.python_implementation()!=""):
208 py_implementation
= platform
.python_implementation();
209 if(platform
.python_implementation()==""):
210 py_implementation
= "Python";
211 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__
);
212 geturls_ua_googlebot_google
= "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
213 geturls_ua_googlebot_google_old
= "Googlebot/2.1 (+http://www.google.com/bot.html)";
214 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
)};
215 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
)};
216 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"};
217 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"};
219 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
220 compressionlistalt
= ['gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzma', 'xz'];
221 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
222 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
224 tarfile_mimetype
= "application/tar";
225 tarfile_tar_mimetype
= tarfile_mimetype
;
226 zipfile_mimetype
= "application/zip";
227 zipfile_zip_mimetype
= zipfile_mimetype
;
228 rarfile_mimetype
= "application/rar";
229 rarfile_rar_mimetype
= rarfile_mimetype
;
230 archivefile_mimetype
= "application/x-"+__file_format_dict__
['format_lower']+"";
231 archivefile_cat_mimetype
= archivefile_mimetype
;
232 archivefile_gzip_mimetype
= "application/x-"+__file_format_dict__
['format_lower']+"+gzip";
233 archivefile_gz_mimetype
= archivefile_gzip_mimetype
;
234 archivefile_bzip2_mimetype
= "application/x-"+__file_format_dict__
['format_lower']+"+bzip2";
235 archivefile_bz2_mimetype
= archivefile_bzip2_mimetype
;
236 archivefile_lz4_mimetype
= "application/x-"+__file_format_dict__
['format_lower']+"+lz4";
237 archivefile_lzop_mimetype
= "application/x-"+__file_format_dict__
['format_lower']+"+lzop";
238 archivefile_lzo_mimetype
= archivefile_lzop_mimetype
;
239 archivefile_zstandard_mimetype
= "application/x-"+__file_format_dict__
['format_lower']+"+zstandard";
240 archivefile_zstd_mimetype
= archivefile_zstandard_mimetype
;
241 archivefile_lzma_mimetype
= "application/x-"+__file_format_dict__
['format_lower']+"+lzma";
242 archivefile_xz_mimetype
= "application/x-"+__file_format_dict__
['format_lower']+"+xz";
243 archivefile_extensions
= ['.cat', '.cat.gz', '.cat.bz2', '.cat.zst', '.cat.lz4', '.cat.lzo', '.cat.lzop', '.cat.lzma', '.cat.xz'];
245 if __name__
== "__main__":
247 curscrpath
= os
.path
.dirname(sys
.argv
[0]);
251 curscrpath
= curscrpath
.replace(os
.sep
, "/");
252 curscrpath
= curscrpath
+ "/";
253 scrfile
= curscrpath
+ "catfile.py";
254 if(os
.path
.exists(scrfile
) and os
.path
.isfile(scrfile
)):
255 scrcmd
= subprocess
.Popen([sys
.executable
, scrfile
] + sys
.argv
[1:]);
258 def VerbosePrintOut(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
264 "warning": logging
.warning
,
265 "error": logging
.error
,
266 "critical": logging
.critical
,
267 "exception": logging
.exception
,
268 "logalt": lambda x
: logging
.log(dgblevel
, x
),
269 "debug": logging
.debug
271 log_function
= log_functions
.get(outtype
);
273 log_function(dbgtxt
);
277 def VerbosePrintOutReturn(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
278 VerbosePrintOut(dbgtxt
, outtype
, dbgenable
, dgblevel
);
281 def RemoveWindowsPath(dpath
):
285 dpath
= dpath
.replace(os
.path
.sep
, "/");
286 dpath
= dpath
.rstrip("/");
287 if(dpath
=="." or dpath
==".."):
291 def NormalizeRelativePath(inpath
):
292 inpath
= RemoveWindowsPath(inpath
);
293 if(os
.path
.isabs(inpath
)):
296 if(inpath
.startswith("./") or inpath
.startswith("../")):
299 outpath
= "./" + inpath
;
302 def PrependPath(base_dir
, child_path
):
303 # Check if base_dir is None or empty, if so, return child_path as is
306 # Ensure base_dir ends with exactly one slash
307 if not base_dir
.endswith('/'):
309 # Check if child_path starts with ./ or ../ (indicating a relative path)
310 if child_path
.startswith('./') or child_path
.startswith('../'):
311 # For relative paths, we don't alter the child_path
312 return base_dir
+ child_path
;
314 # For non-relative paths, ensure there's no starting slash on child_path to avoid double slashes
315 return base_dir
+ child_path
.lstrip('/');
317 def ListDir(dirpath
, followlink
=False, duplicates
=False):
318 if(isinstance(dirpath
, (list, tuple, ))):
319 dirpath
= list(filter(None, dirpath
));
320 elif(isinstance(dirpath
, (str, ))):
321 dirpath
= list(filter(None, [dirpath
]));
323 for mydirfile
in dirpath
:
324 if(not os
.path
.exists(mydirfile
)):
326 mydirfile
= NormalizeRelativePath(mydirfile
);
327 if(os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
)):
328 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
));
329 if(os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
)):
330 for root
, dirs
, filenames
in os
.walk(mydirfile
):
332 dpath
= RemoveWindowsPath(dpath
);
333 if(dpath
not in retlist
and not duplicates
):
334 retlist
.append(dpath
);
336 retlist
.append(dpath
);
337 for file in filenames
:
338 fpath
= os
.path
.join(root
, file);
339 fpath
= RemoveWindowsPath(fpath
);
340 if(fpath
not in retlist
and not duplicates
):
341 retlist
.append(fpath
);
343 retlist
.append(fpath
);
345 retlist
.append(RemoveWindowsPath(mydirfile
));
348 def ListDirAdvanced(dirpath
, followlink
=False, duplicates
=False):
349 if isinstance(dirpath
, (list, tuple)):
350 dirpath
= list(filter(None, dirpath
));
351 elif isinstance(dirpath
, str):
352 dirpath
= list(filter(None, [dirpath
]));
354 for mydirfile
in dirpath
:
355 if not os
.path
.exists(mydirfile
):
357 mydirfile
= NormalizeRelativePath(mydirfile
);
358 if os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
) and followlink
:
359 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
))
360 if os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
):
361 for root
, dirs
, filenames
in os
.walk(mydirfile
):
362 # Sort dirs and filenames alphabetically in place
363 dirs
.sort(key
=lambda x
: x
.lower());
364 filenames
.sort(key
=lambda x
: x
.lower());
365 dpath
= RemoveWindowsPath(root
);
366 if not duplicates
and dpath
not in retlist
:
367 retlist
.append(dpath
);
369 retlist
.append(dpath
);
370 for file in filenames
:
371 fpath
= os
.path
.join(root
, file);
372 fpath
= RemoveWindowsPath(fpath
);
373 if not duplicates
and fpath
not in retlist
:
374 retlist
.append(fpath
);
376 retlist
.append(fpath
);
378 retlist
.append(RemoveWindowsPath(mydirfile
));
381 def create_alias_function(prefix
, base_name
, suffix
, target_function
):
382 # Define a new function that wraps the target function
383 def alias_function(*args
, **kwargs
):
384 return target_function(*args
, **kwargs
);
386 # Create the function name by combining the prefix, base name, and the suffix
387 function_name
= "{}{}{}".format(prefix
, base_name
, suffix
);
389 # Add the new function to the global namespace
390 globals()[function_name
] = alias_function
;
392 def create_alias_function_alt(prefix
, base_name
, suffix
, target_function
):
393 # Create the function name by combining the prefix, base name, and the suffix
394 # Use the format method for string formatting, compatible with Python 2 and 3
395 function_name
= "{}{}{}".format(prefix
, base_name
, suffix
)
396 # Add the new function (alias of the target_function) to the global namespace
397 # This line is compatible as-is with both Python 2 and 3
398 globals()[function_name
] = target_function
400 def FormatSpecsListToDict(formatspecs
=__file_format_list__
):
401 if(isinstance(formatspecs
, (list, tuple, ))):
402 return {'format_name': formatspecs
[0], 'format_magic': formatspecs
[1], 'format_lower': formatspecs
[2], 'format_len': formatspecs
[3], 'format_hex': formatspecs
[4], 'format_delimiter': formatspecs
[5], 'format_ver': formatspecs
[6], 'new_style': formatspecs
[7], 'use_advanced_list': formatspecs
[8], 'use_alt_inode': formatspecs
[9]};
403 elif(isinstance(formatspecs
, (dict, ))):
406 return __file_format_dict__
;
407 return __file_format_dict__
;
409 # initial_value can be 0xFFFF or 0x0000
410 def crc16_ansi(msg
, initial_value
=0xFFFF):
411 # CRC-16-IBM / CRC-16-ANSI polynomial and initial value
412 poly
= 0x8005; # Polynomial for CRC-16-IBM / CRC-16-ANSI
413 crc
= initial_value
; # Initial value
415 crc ^
= b
<< 8; # XOR byte into CRC top byte
416 for _
in range(8): # Process each bit
417 if crc
& 0x8000: # If the top bit is set
418 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
420 crc
= crc
<< 1; # Just shift left
421 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
424 # initial_value can be 0xFFFF or 0x0000
425 def crc16_ibm(msg
, initial_value
=0xFFFF):
426 return crc16_ansi(msg
, initial_value
);
428 # initial_value is 0xFFFF
430 return crc16_ansi(msg
, 0xFFFF);
432 # initial_value can be 0xFFFF, 0x1D0F or 0x0000
433 def crc16_ccitt(msg
, initial_value
=0xFFFF):
434 # CRC-16-CCITT polynomial
435 poly
= 0x1021; # Polynomial for CRC-16-CCITT
436 # Use the specified initial value
439 crc ^
= b
<< 8; # XOR byte into CRC top byte
440 for _
in range(8): # Process each bit
441 if crc
& 0x8000: # If the top bit is set
442 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
444 crc
= crc
<< 1; # Just shift left
445 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
448 # initial_value can be 0x42F0E1EBA9EA3693 or 0x0000000000000000
449 def crc64_ecma(msg
, initial_value
=0x0000000000000000):
450 # CRC-64-ECMA polynomial and initial value
451 poly
= 0x42F0E1EBA9EA3693;
452 crc
= initial_value
; # Initial value for CRC-64-ECMA
454 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
455 for _
in range(8): # Process each bit
456 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
457 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
459 crc
<<= 1; # Just shift left if the MSB is 0
460 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
463 # initial_value can be 0x000000000000001B or 0xFFFFFFFFFFFFFFFF
464 def crc64_iso(msg
, initial_value
=0xFFFFFFFFFFFFFFFF):
465 # CRC-64-ISO polynomial and initial value
466 poly
= 0x000000000000001B;
467 crc
= initial_value
; # Common initial value for CRC-64-ISO
469 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
470 for _
in range(8): # Process each bit
471 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
472 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
474 crc
<<= 1; # Just shift left if the MSB is 0
475 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
478 def GetDataFromArray(data
, path
, default
=None):
482 element
= element
[key
];
484 except (KeyError, TypeError, IndexError):
487 def GetDataFromArrayAlt(structure
, path
, default
=None):
490 if isinstance(element
, dict) and key
in element
:
491 element
= element
[key
];
492 elif isinstance(element
, list) and isinstance(key
, int) and -len(element
) <= key
< len(element
):
493 element
= element
[key
];
498 def GetHeaderChecksum(inlist
=[], checksumtype
="crc32", encodedata
=True, formatspecs
=__file_format_dict__
):
499 formatspecs
= FormatSpecsListToDict(formatspecs
);
500 if isinstance(inlist
, list):
501 fileheader
= AppendNullBytes(inlist
, formatspecs
['format_delimiter']);
503 fileheader
= AppendNullByte(inlist
, formatspecs
['format_delimiter']);
505 fileheader
= fileheader
.encode('UTF-8');
506 if(checksumtype
=="none" or checksumtype
==""):
507 catfileheadercshex
= format(0, 'x').lower();
508 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
509 catfileheadercshex
= format(crc16(fileheader
) & 0xffff, '04x').lower();
510 elif(checksumtype
=="crc16_ccitt"):
511 catfileheadercshex
= format(crc16_ccitt(fileheader
) & 0xffff, '04x').lower();
512 elif(checksumtype
=="adler32"):
513 catfileheadercshex
= format(zlib
.adler32(fileheader
) & 0xffffffff, '08x').lower();
514 elif(checksumtype
=="crc32"):
515 catfileheadercshex
= format(crc32(fileheader
) & 0xffffffff, '08x').lower();
516 elif(checksumtype
=="crc64_ecma"):
517 catfileheadercshex
= format(crc64_ecma(fileheader
) & 0xffffffffffffffff, '016x').lower();
518 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
519 catfileheadercshex
= format(crc64_iso(fileheader
) & 0xffffffffffffffff, '016x').lower();
520 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
521 checksumoutstr
= hashlib
.new(checksumtype
);
522 checksumoutstr
.update(fileheader
);
523 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
525 catfileheadercshex
= format(0, 'x').lower();
526 return catfileheadercshex
;
528 def GetFileChecksum(instr
, checksumtype
="crc32", encodedata
=True, formatspecs
=__file_format_dict__
):
529 formatspecs
= FormatSpecsListToDict(formatspecs
);
531 instr
= instr
.encode('UTF-8');
532 if(checksumtype
=="none" or checksumtype
==""):
533 catinstrcshex
= format(0, 'x').lower();
534 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
535 catinstrcshex
= format(crc16(instr
) & 0xffff, '04x').lower();
536 elif(checksumtype
=="crc16_ccitt"):
537 catinstrcshex
= format(crc16_ccitt(instr
) & 0xffff, '04x').lower();
538 elif(checksumtype
=="adler32"):
539 catinstrcshex
= format(zlib
.adler32(instr
) & 0xffffffff, '08x').lower();
540 elif(checksumtype
=="crc32"):
541 catinstrcshex
= format(crc32(instr
) & 0xffffffff, '08x').lower();
542 elif(checksumtype
=="crc64_ecma"):
543 catinstrcshex
= format(crc64_ecma(instr
) & 0xffffffffffffffff, '016x').lower();
544 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
545 catinstrcshex
= format(crc64_iso(instr
) & 0xffffffffffffffff, '016x').lower();
546 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
547 checksumoutstr
= hashlib
.new(checksumtype
);
548 checksumoutstr
.update(instr
);
549 catinstrcshex
= checksumoutstr
.hexdigest().lower();
551 catinstrcshex
= format(0, 'x').lower();
552 return catinstrcshex
;
554 def ValidateHeaderChecksum(inlist
=[], checksumtype
="crc32", inchecksum
="0", formatspecs
=__file_format_dict__
):
555 formatspecs
= FormatSpecsListToDict(formatspecs
);
556 catfileheadercshex
= GetHeaderChecksum(inlist
, checksumtype
, True, formatspecs
);
557 inchecksum
= inchecksum
.lower();
558 catfileheadercshex
= catfileheadercshex
.lower();
559 if(inchecksum
==catfileheadercshex
):
564 def ValidateFileChecksum(infile
, checksumtype
="crc32", inchecksum
="0", formatspecs
=__file_format_dict__
):
565 formatspecs
= FormatSpecsListToDict(formatspecs
);
566 catinfilecshex
= GetFileChecksum(infile
, checksumtype
, True, formatspecs
);
567 inchecksum
= inchecksum
.lower();
568 catinfilecshex
= catinfilecshex
.lower();
569 if(inchecksum
==catinfilecshex
):
574 def ReadTillNullByteOld(fp
, delimiter
=__file_format_dict__
['format_delimiter']):
577 nullbyte
= delimiter
.encode("UTF-8");
579 curbyte
= fp
.read(1);
580 if(curbyte
==nullbyte
or not curbyte
):
582 curfullbyte
= curfullbyte
+ curbyte
;
583 return curfullbyte
.decode('UTF-8');
585 def ReadUntilNullByteOld(fp
, delimiter
=__file_format_dict__
['format_delimiter']):
586 return ReadTillNullByteOld(fp
, delimiter
);
588 def ReadTillNullByteAlt(fp
, delimiter
=__file_format_dict__
['format_delimiter'], chunk_size
=1024, max_read
=1024000):
589 delimiter
= delimiter
.encode('UTF-8') # Ensure the delimiter is in bytes
590 buffer = bytearray();
592 delimiter_length
= len(delimiter
);
594 chunk
= fp
.read(chunk_size
)
596 # End of file reached without finding the delimiter
598 buffer.extend(chunk
);
599 total_read
+= len(chunk
);
600 if delimiter
in buffer:
601 # Delimiter found, calculate where to reset the file pointer
602 index
= buffer.find(delimiter
);
603 # Calculate how many extra bytes were read after the delimiter
604 extra_bytes_read
= len(buffer) - (index
+ delimiter_length
);
605 # Move the file pointer back to just after the delimiter
606 fp
.seek(-extra_bytes_read
, 1);
607 buffer = buffer[:index
];
609 if total_read
>= max_read
:
610 # Stop reading if max limit is reached to prevent excessive memory usage
611 raise MemoryError("Maximum read limit reached without finding the delimiter.");
612 # Check for incomplete UTF-8 sequences at the end of the buffer
613 if len(buffer) > 1 and 128 <= buffer[-1] < 192:
614 # This suggests that the last byte might be the start of a multi-byte character
615 # Try to read one more byte to complete the character
616 extra_byte
= fp
.read(1);
618 buffer.extend(extra_byte
);
620 # No more data available
623 return buffer.decode('UTF-8', errors
='replace');
624 except UnicodeDecodeError:
625 return buffer.decode('UTF-8', errors
='replace');
627 def ReadUntilNullByteAlt(fp
, delimiter
=__file_format_dict__
['format_delimiter'], chunk_size
=1024, max_read
=1024000):
628 return ReadTillNullByteAlt(fp
, delimiter
, chunk_size
, max_read
);
630 def ReadTillNullByte(fp
, delimiter
=__file_format_dict__
['format_delimiter'], max_read
=1024000):
631 curfullbyte
= bytearray();
632 nullbyte
= delimiter
.encode("UTF-8");
633 total_read
= 0; # Track the total number of bytes read
635 curbyte
= fp
.read(1);
636 if curbyte
== nullbyte
or not curbyte
:
638 curfullbyte
.extend(curbyte
);
640 if total_read
>= max_read
:
641 raise MemoryError("Maximum read limit reached without finding the delimiter.");
642 # Decode the full byte array to string once out of the loop
644 return curfullbyte
.decode('UTF-8');
645 except UnicodeDecodeError:
646 # Handle potential partial UTF-8 characters
647 for i
in range(1, 4):
649 return curfullbyte
[:-i
].decode('UTF-8');
650 except UnicodeDecodeError:
652 raise; # Re-raise if decoding fails even after trimming
654 def ReadUntilNullByte(fp
, delimiter
=__file_format_dict__
['format_delimiter'], max_read
=1024000):
655 return ReadTillNullByte(fp
, delimiter
, max_read
);
657 def ReadTillNullByteByNum(fp
, delimiter
=__file_format_dict__
['format_delimiter'], num_delimiters
=1, chunk_size
=1024, max_read
=1024000):
658 delimiter
= delimiter
.encode('UTF-8'); # Ensure the delimiter is in bytes
659 buffer = bytearray();
661 delimiter_length
= len(delimiter
);
663 while len(results
) < num_delimiters
:
664 chunk
= fp
.read(chunk_size
);
666 # End of file reached; decode whatever is collected if it's the last needed part
668 results
.append(buffer.decode('UTF-8', errors
='replace'));
671 total_read
+= len(chunk
);
672 # Check if we have found the delimiter
673 while delimiter
in buffer:
674 index
= buffer.find(delimiter
);
675 # Decode the section before the delimiter
676 results
.append(buffer[:index
].decode('UTF-8', errors
='replace'));
677 # Remove the processed part from the buffer
678 buffer = buffer[index
+ delimiter_length
:];
679 if len(results
) == num_delimiters
:
680 # If reached the required number of delimiters, adjust the file pointer and stop
681 fp
.seek(-len(buffer), 1);
683 if total_read
>= max_read
:
684 # Stop reading if max limit is reached to prevent excessive memory usage
685 raise MemoryError("Maximum read limit reached without finding the delimiter.");
686 # Check for incomplete UTF-8 sequences at the end of the buffer
687 if len(buffer) > 1 and 128 <= buffer[-1] < 192:
688 # This suggests that the last byte might be the start of a multi-byte character
689 # Try to read one more byte to complete the character
690 extra_byte
= fp
.read(1);
692 buffer.extend(extra_byte
);
694 # No more data available
696 # Process remaining buffer if less than the required number of delimiters were found
697 if len(buffer) > 0 and len(results
) < num_delimiters
:
698 results
.append(buffer.decode('UTF-8', errors
='replace'));
701 def ReadUntilNullByteByNum(fp
, delimiter
=__file_format_dict__
['format_delimiter'], num_delimiters
=1, chunk_size
=1024, max_read
=1024000):
702 return ReadTillNullByteByNum(fp
, delimiter
, num_delimiters
, chunk_size
, max_read
);
704 def SeekToEndOfFile(fp
):
708 if(lasttell
==fp
.tell()):
710 lasttell
= fp
.tell();
713 def ReadFileHeaderData(fp
, rounds
=0, delimiter
=__file_format_dict__
['format_delimiter']):
717 while(rocount
<roend
):
718 HeaderOut
.append(ReadTillNullByte(fp
, delimiter
));
719 rocount
= rocount
+ 1;
722 def ReadFileHeaderDataBySize(fp
, delimiter
=__file_format_dict__
['format_delimiter']):
723 headerpresize
= ReadTillNullByte(fp
, delimiter
);
724 headersize
= int(headerpresize
, 16);
727 headercontent
= str(fp
.read(headersize
).decode('UTF-8')).split(delimiter
);
730 roend
= int(len(headercontent
));
731 HeaderOut
= [headerpresize
];
732 while(rocount
<roend
):
733 HeaderOut
.append(headercontent
[rocount
]);
734 rocount
= rocount
+ 1;
737 def ReadFileHeaderDataWoSize(fp
, delimiter
=__file_format_dict__
['format_delimiter']):
738 preheaderdata
= ReadFileHeaderData(fp
, 2, delimiter
);
739 headersize
= int(preheaderdata
[0], 16);
740 headernumfields
= int(preheaderdata
[1], 16);
741 if(headersize
<=0 or headernumfields
<=0):
743 headerdata
= ReadTillNullByteByNum(fp
, delimiter
, headernumfields
);
744 #headerdata = ReadFileHeaderData(fp, headernumfields, delimiter);
745 HeaderOut
= preheaderdata
+ headerdata
;
748 def ReadFileHeaderDataBySizeWithContent(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
):
749 formatspecs
= FormatSpecsListToDict(formatspecs
);
750 delimiter
= formatspecs
['format_delimiter'];
751 fheaderstart
= fp
.tell();
752 HeaderOut
= ReadFileHeaderDataBySize(fp
, delimiter
);
753 if(len(HeaderOut
)==0):
755 if(re
.findall("^[.|/]", HeaderOut
[3])):
756 fname
= HeaderOut
[3];
758 fname
= "./"+HeaderOut
[3];
759 fchecksumtype
= HeaderOut
[-3].lower();
760 fcs
= HeaderOut
[-2].lower();
761 fccs
= HeaderOut
[-1].lower();
762 fsize
= int(HeaderOut
[5], 16);
763 fcompression
= HeaderOut
[12];
764 fcsize
= int(HeaderOut
[13], 16);
765 newfcs
= GetHeaderChecksum(HeaderOut
[:-2], HeaderOut
[-3].lower(), True, formatspecs
);
766 if(fcs
!=newfcs
and not skipchecksum
):
767 VerbosePrintOut("File Header Checksum Error with file " + fname
+ " at offset " + str(fheaderstart
));
768 VerbosePrintOut("'" + str(fcs
) + "' != " + "'" + str(newfcs
) + "'");
770 fhend
= fp
.tell() - 1;
771 fcontentstart
= fp
.tell();
772 fcontents
= BytesIO();
773 if(fsize
>0 and not listonly
):
774 if(fcompression
=="none" or fcompression
=="" or fcompression
=="auto"):
775 fcontents
.write(fp
.read(fsize
));
777 fcontents
.write(fp
.read(fcsize
));
778 elif(fsize
>0 and listonly
):
779 if(fcompression
=="none" or fcompression
=="" or fcompression
=="auto"):
783 fcontents
.seek(0, 0);
784 newfccs
= GetFileChecksum(fcontents
.read(), HeaderOut
[-3].lower(), False, formatspecs
);
785 if(fccs
!=newfccs
and not skipchecksum
and not listonly
):
786 VerbosePrintOut("File Content Checksum Error with file " + fname
+ " at offset " + str(fcontentstart
));
787 VerbosePrintOut("'" + str(fccs
) + "' != " + "'" + str(newfccs
) + "'");
789 if(fcompression
=="none" or fcompression
=="" or fcompression
=="auto"):
792 fcontents
.seek(0, 0);
793 fcontents
= UncompressArchiveFile(fcontents
, formatspecs
);
795 fcontentend
= fp
.tell() - 1;
796 HeaderOut
.append(fcontents
);
799 def ReadFileHeaderDataBySizeWithContentToArray(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
):
800 formatspecs
= FormatSpecsListToDict(formatspecs
);
801 delimiter
= formatspecs
['format_delimiter'];
802 fheaderstart
= fp
.tell();
803 if(formatspecs
['new_style']):
804 HeaderOut
= ReadFileHeaderDataBySize(fp
, delimiter
);
806 HeaderOut
= ReadFileHeaderDataWoSize(fp
, delimiter
);
807 if(len(HeaderOut
)==0):
809 fheadsize
= int(HeaderOut
[0], 16);
810 fnumfields
= int(HeaderOut
[1], 16);
811 ftype
= int(HeaderOut
[2], 16);
812 if(re
.findall("^[.|/]", HeaderOut
[3])):
813 fname
= HeaderOut
[3];
815 fname
= "./"+HeaderOut
[3];
816 fbasedir
= os
.path
.dirname(fname
);
817 flinkname
= HeaderOut
[4];
818 fsize
= int(HeaderOut
[5], 16);
819 fatime
= int(HeaderOut
[6], 16);
820 fmtime
= int(HeaderOut
[7], 16);
821 fctime
= int(HeaderOut
[8], 16);
822 fbtime
= int(HeaderOut
[9], 16);
823 fmode
= int(HeaderOut
[10], 16);
824 fchmode
= stat
.S_IMODE(fmode
);
825 ftypemod
= stat
.S_IFMT(fmode
);
826 fwinattributes
= int(HeaderOut
[11], 16);
827 fcompression
= HeaderOut
[12];
828 fcsize
= int(HeaderOut
[13], 16);
829 fuid
= int(HeaderOut
[14], 16);
830 funame
= HeaderOut
[15];
831 fgid
= int(HeaderOut
[16], 16);
832 fgname
= HeaderOut
[17];
833 fid
= int(HeaderOut
[18], 16);
834 finode
= int(HeaderOut
[19], 16);
835 flinkcount
= int(HeaderOut
[20], 16);
836 fdev_minor
= int(HeaderOut
[21], 16);
837 fdev_major
= int(HeaderOut
[22], 16);
838 frdev_minor
= int(HeaderOut
[23], 16);
839 frdev_major
= int(HeaderOut
[24], 16);
840 fextrasize
= int(HeaderOut
[25], 16);
841 fextrafields
= int(HeaderOut
[26], 16);
842 extrafieldslist
= [];
844 extraend
= extrastart
+ fextrafields
;
845 extrafieldslist
= [];
846 if(extrastart
<extraend
):
847 extrafieldslist
.append(HeaderOut
[extrastart
]);
848 extrastart
= extrastart
+ 1;
849 fchecksumtype
= HeaderOut
[extrastart
].lower();
850 fcs
= HeaderOut
[extrastart
+ 1].lower();
851 fccs
= HeaderOut
[extrastart
+ 2].lower();
852 newfcs
= GetHeaderChecksum(HeaderOut
[:-2], HeaderOut
[-3].lower(), True, formatspecs
);
853 if(fcs
!=newfcs
and not skipchecksum
):
854 VerbosePrintOut("File Header Checksum Error with file " + fname
+ " at offset " + str(fheaderstart
));
855 VerbosePrintOut("'" + str(fcs
) + "' != " + "'" + str(newfcs
) + "'");
857 fhend
= fp
.tell() - 1;
858 fcontentstart
= fp
.tell();
859 fcontents
= BytesIO();
860 pyhascontents
= False;
861 if(fsize
>0 and not listonly
):
862 if(fcompression
=="none" or fcompression
=="" or fcompression
=="auto"):
863 fcontents
.write(fp
.read(fsize
));
865 fcontents
.write(fp
.read(fcsize
));
866 pyhascontents
= True;
867 elif(fsize
>0 and listonly
):
868 if(fcompression
=="none" or fcompression
=="" or fcompression
=="auto"):
872 pyhascontents
= False;
873 fcontents
.seek(0, 0);
874 newfccs
= GetFileChecksum(fcontents
.read(), HeaderOut
[-3].lower(), False, formatspecs
);
875 if(fccs
!=newfccs
and not skipchecksum
and not listonly
):
876 VerbosePrintOut("File Content Checksum Error with file " + fname
+ " at offset " + str(fcontentstart
));
877 VerbosePrintOut("'" + str(fccs
) + "' != " + "'" + str(newfccs
) + "'");
879 if(fcompression
=="none" or fcompression
=="" or fcompression
=="auto"):
882 fcontents
.seek(0, 0);
883 fcontents
= UncompressArchiveFile(fcontents
, formatspecs
);
885 fcontentend
= fp
.tell() - 1;
886 fcontents
.seek(0, 0);
887 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
};
890 def ReadFileHeaderDataBySizeWithContentToList(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
):
891 formatspecs
= FormatSpecsListToDict(formatspecs
);
892 delimiter
= formatspecs
['format_delimiter'];
893 fheaderstart
= fp
.tell();
894 if(formatspecs
['new_style']):
895 HeaderOut
= ReadFileHeaderDataBySize(fp
, delimiter
);
897 HeaderOut
= ReadFileHeaderDataWoSize(fp
, delimiter
);
898 if(len(HeaderOut
)==0):
900 fheadsize
= int(HeaderOut
[0], 16);
901 fnumfields
= int(HeaderOut
[1], 16);
902 ftype
= int(HeaderOut
[2], 16);
903 if(re
.findall("^[.|/]", HeaderOut
[3])):
904 fname
= HeaderOut
[3];
906 fname
= "./"+HeaderOut
[3];
907 fbasedir
= os
.path
.dirname(fname
);
908 flinkname
= HeaderOut
[4];
909 fsize
= int(HeaderOut
[5], 16);
910 fatime
= int(HeaderOut
[6], 16);
911 fmtime
= int(HeaderOut
[7], 16);
912 fctime
= int(HeaderOut
[8], 16);
913 fbtime
= int(HeaderOut
[9], 16);
914 fmode
= int(HeaderOut
[10], 16);
915 fchmode
= stat
.S_IMODE(fmode
);
916 ftypemod
= stat
.S_IFMT(fmode
);
917 fwinattributes
= int(HeaderOut
[11], 16);
918 fcompression
= HeaderOut
[12];
919 fcsize
= int(HeaderOut
[13], 16);
920 fuid
= int(HeaderOut
[14], 16);
921 funame
= HeaderOut
[15];
922 fgid
= int(HeaderOut
[16], 16);
923 fgname
= HeaderOut
[17];
924 fid
= int(HeaderOut
[18], 16);
925 finode
= int(HeaderOut
[19], 16);
926 flinkcount
= int(HeaderOut
[20], 16);
927 fdev_minor
= int(HeaderOut
[21], 16);
928 fdev_major
= int(HeaderOut
[22], 16);
929 frdev_minor
= int(HeaderOut
[23], 16);
930 frdev_major
= int(HeaderOut
[24], 16);
931 fextrasize
= int(HeaderOut
[25], 16);
932 fextrafields
= int(HeaderOut
[26], 16);
933 extrafieldslist
= [];
935 extraend
= extrastart
+ fextrafields
;
936 extrafieldslist
= [];
937 if(extrastart
<extraend
):
938 extrafieldslist
.append(HeaderOut
[extrastart
]);
939 extrastart
= extrastart
+ 1;
940 fchecksumtype
= HeaderOut
[extrastart
].lower();
941 fcs
= HeaderOut
[extrastart
+ 1].lower();
942 fccs
= HeaderOut
[extrastart
+ 2].lower();
943 newfcs
= GetHeaderChecksum(HeaderOut
[:-2], HeaderOut
[-3].lower(), True, formatspecs
);
944 if(fcs
!=newfcs
and not skipchecksum
):
945 VerbosePrintOut("File Header Checksum Error with file " + fname
+ " at offset " + str(fheaderstart
));
946 VerbosePrintOut("'" + str(fcs
) + "' != " + "'" + str(newfcs
) + "'");
948 fhend
= fp
.tell() - 1;
949 fcontentstart
= fp
.tell();
950 fcontents
= BytesIO();
951 pyhascontents
= False;
952 if(fsize
>0 and not listonly
):
953 if(fcompression
=="none" or fcompression
=="" or fcompression
=="auto"):
954 fcontents
.write(fp
.read(fsize
));
956 fcontents
.write(fp
.read(fcsize
));
957 pyhascontents
= True;
958 elif(fsize
>0 and listonly
):
959 if(fcompression
=="none" or fcompression
=="" or fcompression
=="atuo"):
963 pyhascontents
= False;
964 fcontents
.seek(0, 0);
965 newfccs
= GetFileChecksum(fcontents
.read(), HeaderOut
[-3].lower(), False, formatspecs
);
966 if(fccs
!=newfccs
and not skipchecksum
and not listonly
):
967 VerbosePrintOut("File Content Checksum Error with file " + fname
+ " at offset " + str(fcontentstart
));
968 VerbosePrintOut("'" + str(fccs
) + "' != " + "'" + str(newfccs
) + "'");
970 if(fcompression
=="none" or fcompression
=="" or fcompression
=="auto"):
973 fcontents
.seek(0, 0);
974 fcontents
= UncompressArchiveFile(fcontents
, formatspecs
);
976 fcontentend
= fp
.tell() - 1;
977 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
];
980 def ReadFileDataBySizeWithContent(fp
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
):
981 formatspecs
= FormatSpecsListToDict(formatspecs
);
982 delimiter
= formatspecs
['format_delimiter'];
986 catheader
= ReadFileHeaderData(fp
, 4, delimiter
);
989 headercheck
= ValidateHeaderChecksum(catheader
[:-1], catheader
[2], catheader
[3], formatspecs
);
990 newfcs
= GetHeaderChecksum(catheader
[:-2], catheader
[2], True, formatspecs
);
991 if(not headercheck
and not skipchecksum
):
992 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
993 VerbosePrintOut("'" + str(newfcs
) + "' != " + "'" + str(catheader
[3]) + "'");
995 fnumfiles
= int(catheader
[1], 16);
998 while(countnum
< fnumfiles
):
999 HeaderOut
= ReadFileHeaderDataBySizeWithContent(fp
, listonly
, skipchecksum
, formatspecs
)
1000 if(len(HeaderOut
)==0):
1002 flist
.append(HeaderOut
);
1003 countnum
= countnum
+ 1;
1006 def ReadFileDataBySizeWithContentToArray(fp
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
):
1007 formatspecs
= FormatSpecsListToDict(formatspecs
);
1008 delimiter
= formatspecs
['format_delimiter'];
1012 catheader
= ReadFileHeaderData(fp
, 4, delimiter
);
1015 headercheck
= ValidateHeaderChecksum(catheader
[:-1], catheader
[2], catheader
[3], formatspecs
);
1016 newfcs
= GetHeaderChecksum(catheader
[:-2], catheader
[2], True, formatspecs
);
1017 if(not headercheck
and not skipchecksum
):
1018 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
1019 VerbosePrintOut("'" + str(newfcs
) + "' != " + "'" + str(catheader
[3]) + "'");
1021 catstring
= catheader
[0];
1022 catversion
= re
.findall(r
"([\d]+)$", catstring
);
1023 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
1024 fprenumfiles
= catheader
[1];
1025 fnumfiles
= int(fprenumfiles
, 16);
1026 fprechecksumtype
= catheader
[2];
1027 fprechecksum
= catheader
[3];
1028 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
1029 if(seekstart
<0 and seekstart
>fnumfiles
):
1031 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
1032 seekend
= fnumfiles
;
1033 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
1034 seekend
= fnumfiles
- abs(seekend
);
1037 while(il
< seekstart
):
1038 prefhstart
= fp
.tell();
1039 preheaderdata
= ReadFileHeaderDataBySize(fp
, formatspecs
['format_delimiter']);
1040 if(len(preheaderdata
)==0):
1042 prefsize
= int(preheaderdata
[5], 16);
1043 prenewfcs
= GetHeaderChecksum(preheaderdata
[:-2], preheaderdata
[-3].lower(), True, formatspecs
);
1044 prefcs
= preheaderdata
[-2];
1045 if(prefcs
!=prenewfcs
and not skipchecksum
):
1046 VVerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
1047 VerbosePrintOut("'" + str(prefcs
) + "' != " + "'" + str(prenewfcs
) + "'");
1049 valid_archive
= False;
1050 invalid_archive
= True;
1051 prefhend
= fp
.tell() - 1;
1052 prefcontentstart
= fp
.tell();
1053 prefcontents
= BytesIO();
1054 pyhascontents
= False;
1056 prefcontents
.write(fp
.read(prefsize
));
1057 prefcontents
.seek(0, 0);
1058 prenewfccs
= GetFileChecksum(prefcontents
.read(), preheaderdata
[-3].lower(), False, formatspecs
);
1059 prefccs
= preheaderdata
[-1];
1060 pyhascontents
= True;
1061 if(prefccs
!=prenewfccs
and not skipchecksum
):
1062 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
1063 VerbosePrintOut("'" + str(prefccs
) + "' != " + "'" + str(prenewfccs
) + "'");
1068 countnum
= seekstart
;
1069 while(countnum
< seekend
):
1070 catlist
['ffilelist'].update({realidnum
: {'fid': realidnum
, 'fidalt': realidnum
}});
1071 HeaderOut
= ReadFileHeaderDataBySizeWithContentToArray(fp
, listonly
, skipchecksum
, formatspecs
);
1072 if(len(HeaderOut
)==0):
1074 catlist
['ffilelist'][realidnum
].update(HeaderOut
);
1075 countnum
= countnum
+ 1;
1076 realidnum
= realidnum
+ 1;
1079 def ReadFileDataBySizeWithContentToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
):
1080 formatspecs
= FormatSpecsListToDict(formatspecs
);
1081 if(isinstance(infile
, dict)):
1082 listcatfiles
= infile
;
1084 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
1086 listcatfiles
= ReadFileDataBySizeWithContentToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
);
1087 if(not listcatfiles
):
1089 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': {}}}};
1090 lenlist
= len(listcatfiles
['ffilelist']);
1092 lcfx
= int(listcatfiles
['fnumfiles']);
1093 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
1094 lcfx
= int(lenlist
);
1096 lcfx
= int(listcatfiles
['fnumfiles']);
1098 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
1099 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
1100 catarray
['filetoid'].update(filetoidarray
);
1101 catarray
['idtofile'].update(idtofilearray
);
1102 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
1103 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
1104 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
1105 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
1106 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
1107 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
1108 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
1109 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
1110 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
1111 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
1112 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
1113 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
1114 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
1115 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
1116 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
1117 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
1118 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
1119 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
1120 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
1121 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
1122 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
1123 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
1124 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
1125 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
1126 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
1127 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
1128 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
1129 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
1130 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
1131 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
1132 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
1136 def ReadFileDataBySizeWithContentToList(fp
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
):
1137 formatspecs
= FormatSpecsListToDict(formatspecs
);
1138 delimiter
= formatspecs
['format_delimiter'];
1142 catheader
= ReadFileHeaderData(fp
, 4, delimiter
);
1145 headercheck
= ValidateHeaderChecksum(catheader
[:-1], catheader
[2], catheader
[3], formatspecs
);
1146 newfcs
= GetHeaderChecksum(catheader
[:-2], catheader
[2], True, formatspecs
);
1147 if(not headercheck
and not skipchecksum
):
1148 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
1149 VerbosePrintOut("'" + str(newfcs
) + "' != " + "'" + str(catheader
[3]) + "'");
1151 catstring
= catheader
[0];
1152 catversion
= re
.findall(r
"([\d]+)$", catstring
);
1153 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
1154 fprenumfiles
= catheader
[1];
1155 fnumfiles
= int(fprenumfiles
, 16);
1156 fprechecksumtype
= catheader
[2];
1157 fprechecksum
= catheader
[3];
1159 if(seekstart
<0 and seekstart
>fnumfiles
):
1161 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
1162 seekend
= fnumfiles
;
1163 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
1164 seekend
= fnumfiles
- abs(seekend
);
1167 while(il
< seekstart
):
1168 prefhstart
= fp
.tell();
1169 preheaderdata
= ReadFileHeaderDataBySize(fp
, formatspecs
['format_delimiter']);
1170 if(len(preheaderdata
)==0):
1172 prefsize
= int(preheaderdata
[5], 16);
1173 prefcompression
= preheaderdata
[12];
1174 prefcsize
= int(preheaderdata
[13], 16);
1175 prenewfcs
= GetHeaderChecksum(preheaderdata
[:-2], preheaderdata
[-3].lower(), True, formatspecs
);
1176 prefcs
= preheaderdata
[-2];
1177 if(prefcs
!=prenewfcs
and not skipchecksum
):
1178 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
1179 VerbosePrintOut("'" + str(prefcs
) + "' != " + "'" + str(prenewfcs
) + "'");
1181 valid_archive
= False;
1182 invalid_archive
= True;
1183 prefhend
= fp
.tell() - 1;
1184 prefcontentstart
= fp
.tell();
1186 pyhascontents
= False;
1188 if(prefcompression
=="none" or prefcompression
=="" or prefcompression
=="auto"):
1189 prefcontents
= catfp
.read(prefsize
);
1191 prefcontents
= catfp
.read(prefcsize
);
1192 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
1193 prefccs
= preheaderdata
[-1];
1194 pyhascontents
= True;
1195 if(prefccs
!=prenewfccs
and not skipchecksum
):
1196 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
1197 VerbosePrintOut("'" + str(prefccs
) + "' != " + "'" + str(prenewfccs
) + "'");
1202 countnum
= seekstart
;
1203 while(countnum
< seekend
):
1204 HeaderOut
= ReadFileHeaderDataBySizeWithContentToList(fp
, listonly
, skipchecksum
, formatspecs
);
1205 if(len(HeaderOut
)==0):
1207 catlist
.append(HeaderOut
);
1208 countnum
= countnum
+ 1;
1209 realidnum
= realidnum
+ 1;
1212 def ReadInFileBySizeWithContentToArray(infile
, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
):
1213 formatspecs
= FormatSpecsListToDict(formatspecs
);
1214 delimiter
= formatspecs
['format_delimiter'];
1215 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1218 fp
= UncompressArchiveFile(fp
, formatspecs
);
1219 checkcompressfile
= CheckCompressionSubType(fp
, formatspecs
, True);
1220 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
['format_lower']):
1227 if(hasattr(sys
.stdin
, "buffer")):
1228 shutil
.copyfileobj(sys
.stdin
.buffer, fp
);
1230 shutil
.copyfileobj(sys
.stdin
, fp
);
1232 fp
= UncompressArchiveFile(fp
, formatspecs
);
1236 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
1237 fp
= download_file_from_internet_file(infile
);
1238 fp
= UncompressArchiveFile(fp
, formatspecs
);
1244 infile
= RemoveWindowsPath(infile
);
1245 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
1246 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
['format_lower']):
1248 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
1249 if(not compresscheck
):
1250 fextname
= os
.path
.splitext(infile
)[1];
1251 if(fextname
==".gz"):
1252 compresscheck
= "gzip";
1253 elif(fextname
==".bz2"):
1254 compresscheck
= "bzip2";
1255 elif(fextname
==".zst"):
1256 compresscheck
= "zstd";
1257 elif(fextname
==".lz4" or fextname
==".clz4"):
1258 compresscheck
= "lz4";
1259 elif(fextname
==".lzo" or fextname
==".lzop"):
1260 compresscheck
= "lzo";
1261 elif(fextname
==".lzma" or fextname
==".xz"):
1262 compresscheck
= "lzma";
1265 if(not compresscheck
):
1267 fp
= UncompressFile(infile
, formatspecs
, "rb");
1268 return ReadFileDataBySizeWithContentToArray(fp
, listonly
, skipchecksum
, formatspecs
);
1270 def ReadInFileBySizeWithContentToList(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
):
1271 formatspecs
= FormatSpecsListToDict(formatspecs
);
1272 delimiter
= formatspecs
['format_delimiter'];
1273 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1276 fp
= UncompressArchiveFile(fp
, formatspecs
);
1277 checkcompressfile
= CheckCompressionSubType(fp
, formatspecs
, True);
1278 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
['format_lower']):
1285 if(hasattr(sys
.stdin
, "buffer")):
1286 shutil
.copyfileobj(sys
.stdin
.buffer, fp
);
1288 shutil
.copyfileobj(sys
.stdin
, fp
);
1290 fp
= UncompressArchiveFile(fp
, formatspecs
);
1294 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
1295 fp
= download_file_from_internet_file(infile
);
1296 fp
= UncompressArchiveFile(fp
, formatspecs
);
1302 infile
= RemoveWindowsPath(infile
);
1303 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
1304 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
['format_lower']):
1306 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
1307 if(not compresscheck
):
1308 fextname
= os
.path
.splitext(infile
)[1];
1309 if(fextname
==".gz"):
1310 compresscheck
= "gzip";
1311 elif(fextname
==".bz2"):
1312 compresscheck
= "bzip2";
1313 elif(fextname
==".zst"):
1314 compresscheck
= "zstd";
1315 elif(fextname
==".lz4" or fextname
==".clz4"):
1316 compresscheck
= "lz4";
1317 elif(fextname
==".lzo" or fextname
==".lzop"):
1318 compresscheck
= "lzo";
1319 elif(fextname
==".lzma" or fextname
==".xz"):
1320 compresscheck
= "lzma";
1323 if(not compresscheck
):
1325 fp
= UncompressFile(infile
, formatspecs
, "rb");
1326 return ReadFileDataBySizeWithContentToList(fp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
);
1328 def ReadInFileBySizeWithContentToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
):
1329 formatspecs
= FormatSpecsListToDict(formatspecs
);
1330 if(isinstance(infile
, dict)):
1331 listcatfiles
= infile
;
1333 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
1334 infile
= RemoveWindowsPath(infile
);
1335 listcatfiles
= ReadInFileBySizeWithContentToArray(infile
, listonly
, skipchecksum
, formatspecs
);
1336 if(not listcatfiles
):
1338 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': {}}}};
1339 lenlist
= len(listcatfiles
['ffilelist']);
1341 lcfx
= int(listcatfiles
['fnumfiles']);
1342 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
1343 lcfx
= int(lenlist
);
1345 lcfx
= int(listcatfiles
['fnumfiles']);
1347 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
1348 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
1349 catarray
['filetoid'].update(filetoidarray
);
1350 catarray
['idtofile'].update(idtofilearray
);
1351 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
1352 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
1353 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
1354 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
1355 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
1356 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
1357 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
1358 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
1359 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
1360 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
1361 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
1362 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
1363 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
1364 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
1365 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
1366 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
1367 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
1368 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
1369 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
1370 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
1371 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
1372 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
1373 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
1374 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
1375 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
1376 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
1377 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
1378 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
1379 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
1380 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
1381 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
1385 def AppendNullByte(indata
, delimiter
=__file_format_dict__
['format_delimiter']):
1386 outdata
= str(indata
) + delimiter
;
1389 def AppendNullBytes(indata
=[], delimiter
=__file_format_dict__
['format_delimiter']):
1394 outdata
= outdata
+ AppendNullByte(indata
[inum
], delimiter
);
1398 def AppendFileHeader(fp
, numfiles
, checksumtype
="crc32", formatspecs
=__file_format_dict__
):
1399 formatspecs
= FormatSpecsListToDict(formatspecs
);
1400 delimiter
= formatspecs
['format_delimiter'];
1401 catver
= formatspecs
['format_ver'];
1402 fileheaderver
= str(int(catver
.replace(".", "")));
1403 fileheader
= AppendNullByte(formatspecs
['format_magic'] + fileheaderver
, formatspecs
['format_delimiter']);
1404 fp
.write(fileheader
.encode('UTF-8'));
1405 fnumfiles
= format(int(numfiles
), 'x').lower();
1406 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
['format_delimiter']);
1407 catfileheadercshex
= GetFileChecksum(fileheader
+ fnumfilesa
, checksumtype
, True, formatspecs
);
1408 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter']);
1409 fp
.write(fnumfilesa
.encode('UTF-8'));
1412 os
.fsync(fp
.fileno());
1413 except io
.UnsupportedOperation
:
1415 except AttributeError:
1417 except OSError as e
:
1421 def MakeEmptyFilePointer(fp
, checksumtype
="crc32", formatspecs
=__file_format_dict__
):
1422 formatspecs
= FormatSpecsListToDict(formatspecs
);
1423 AppendFileHeader(fp
, 0, checksumtype
, formatspecs
);
1426 def MakeEmptyFile(outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", formatspecs
=__file_format_dict__
, returnfp
=False):
1427 formatspecs
= FormatSpecsListToDict(formatspecs
);
1428 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1429 if(os
.path
.exists(outfile
)):
1432 except OSError as e
:
1436 catfpfp
= BytesIO();
1437 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1439 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1442 fbasename
= os
.path
.splitext(outfile
)[0];
1443 fextname
= os
.path
.splitext(outfile
)[1];
1444 catfp
= CompressOpenFile(outfile
, True, compressionlevel
);
1445 catfp
= AppendFileHeader(catfp
, 0, checksumtype
, formatspecs
);
1446 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1447 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
1450 os
.fsync(catfp
.fileno());
1451 except io
.UnsupportedOperation
:
1453 except AttributeError:
1455 except OSError as e
:
1459 if(hasattr(sys
.stdout
, "buffer")):
1460 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1462 shutil
.copyfileobj(catfp
, sys
.stdout
);
1463 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1464 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
1466 upload_file_to_internet_file(catfp
, outfile
);
1474 def AppendFileHeaderWithContent(fp
, filevalues
=[], extradata
=[], filecontent
="", checksumtype
="crc32", formatspecs
=__file_format_dict__
):
1475 formatspecs
= FormatSpecsListToDict(formatspecs
);
1476 extrafields
= format(len(extradata
), 'x').lower();
1477 extrasizestr
= AppendNullByte(extrafields
, formatspecs
['format_delimiter']);
1478 if(len(extradata
)>0):
1479 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
['format_delimiter']);
1480 extrasizelen
= format(len(extrasizestr
), 'x').lower();
1481 catoutlen
= len(filevalues
) + len(extradata
) + 5;
1482 catoutlenhex
= format(catoutlen
, 'x').lower();
1483 catoutlist
= filevalues
;
1484 catoutlist
.insert(0, catoutlenhex
);
1485 catoutlist
.append(extrasizelen
);
1486 catoutlist
.append(extrafields
);
1487 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
['format_delimiter']);
1488 if(len(extradata
)>0):
1489 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
['format_delimiter']);
1490 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
['format_delimiter']);
1491 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
1492 catfilecontentcshex
= GetFileChecksum(filecontent
, checksumtype
, False, formatspecs
);
1493 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
['format_delimiter']);
1494 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
1495 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
['format_delimiter']) + catfileoutstr
;
1496 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
1497 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
['format_delimiter']);
1498 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
1499 nullstrecd
= formatspecs
['format_delimiter'].encode('UTF-8');
1500 catfileout
= catfileoutstrecd
+ filecontent
+ nullstrecd
;
1501 fp
.write(catfileout
);
1504 os
.fsync(fp
.fileno());
1505 except io
.UnsupportedOperation
:
1507 except AttributeError:
1509 except OSError as e
:
1513 def AppendFilesWithContent(infiles
, fp
, dirlistfromtxt
=False, filevalues
=[], extradata
=[], compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_dict__
, verbose
=False):
1514 formatspecs
= FormatSpecsListToDict(formatspecs
);
1515 advancedlist
= formatspecs
['use_advanced_list'];
1516 altinode
= formatspecs
['use_alt_inode'];
1518 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1520 for line
in sys
.stdin
:
1521 infilelist
.append(line
.strip());
1522 infilelist
= list(filter(None, infilelist
));
1523 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
1524 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
1526 with
UncompressFile(infiles
, formatspecs
, "r") as finfile
:
1527 for line
in finfile
:
1528 infilelist
.append(line
.strip());
1529 infilelist
= list(filter(None, infilelist
));
1531 if(isinstance(infiles
, (list, tuple, ))):
1532 infilelist
= list(filter(None, infiles
));
1533 elif(isinstance(infiles
, (str, ))):
1534 infilelist
= list(filter(None, [infiles
]));
1536 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
1538 GetDirList
= ListDir(infilelist
, followlink
, False);
1546 inodetocatinode
= {};
1547 numfiles
= int(len(GetDirList
));
1548 fnumfiles
= format(numfiles
, 'x').lower();
1549 AppendFileHeader(fp
, fnumfiles
, checksumtype
, formatspecs
);
1550 for curfname
in GetDirList
:
1551 if(re
.findall("^[.|/]", curfname
)):
1554 fname
= "./"+curfname
;
1556 VerbosePrintOut(fname
);
1557 if(not followlink
or followlink
is None):
1558 fstatinfo
= os
.lstat(fname
);
1560 fstatinfo
= os
.stat(fname
);
1561 fpremode
= fstatinfo
.st_mode
;
1562 finode
= fstatinfo
.st_ino
;
1563 flinkcount
= fstatinfo
.st_nlink
;
1565 if(stat
.S_ISREG(fpremode
)):
1567 elif(stat
.S_ISLNK(fpremode
)):
1569 elif(stat
.S_ISCHR(fpremode
)):
1571 elif(stat
.S_ISBLK(fpremode
)):
1573 elif(stat
.S_ISDIR(fpremode
)):
1575 elif(stat
.S_ISFIFO(fpremode
)):
1577 elif(stat
.S_ISSOCK(fpremode
)):
1579 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
1581 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
1583 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
1588 fcurfid
= format(int(curfid
), 'x').lower();
1589 if(not followlink
and finode
!=0):
1591 if(finode
in inodelist
):
1593 flinkname
= inodetofile
[finode
];
1595 fcurinode
= format(int(finode
), 'x').lower();
1597 fcurinode
= format(int(inodetocatinode
[finode
]), 'x').lower();
1598 if(finode
not in inodelist
):
1599 inodelist
.append(finode
);
1600 inodetofile
.update({finode
: fname
});
1601 inodetocatinode
.update({finode
: curinode
});
1603 fcurinode
= format(int(finode
), 'x').lower();
1605 fcurinode
= format(int(curinode
), 'x').lower();
1606 curinode
= curinode
+ 1;
1608 fcurinode
= format(int(curinode
), 'x').lower();
1609 curinode
= curinode
+ 1;
1610 curfid
= curfid
+ 1;
1612 flinkname
= os
.readlink(fname
);
1613 fdev
= fstatinfo
.st_dev
;
1614 getfdev
= GetDevMajorMinor(fdev
);
1615 fdev_minor
= getfdev
[0];
1616 fdev_major
= getfdev
[1];
1617 frdev
= fstatinfo
.st_dev
;
1618 if(hasattr(fstatinfo
, "st_rdev")):
1619 frdev
= fstatinfo
.st_rdev
;
1621 frdev
= fstatinfo
.st_dev
;
1622 getfrdev
= GetDevMajorMinor(frdev
);
1623 frdev_minor
= getfrdev
[0];
1624 frdev_major
= getfrdev
[1];
1625 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
1626 fsize
= format(int("0"), 'x').lower();
1627 elif(ftype
==0 or ftype
==7):
1628 fsize
= format(int(fstatinfo
.st_size
), 'x').lower();
1630 fsize
= format(int(fstatinfo
.st_size
)).lower();
1631 fatime
= format(int(fstatinfo
.st_atime
), 'x').lower();
1632 fmtime
= format(int(fstatinfo
.st_mtime
), 'x').lower();
1633 fctime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1634 if(hasattr(fstatinfo
, "st_birthtime")):
1635 fbtime
= format(int(fstatinfo
.st_birthtime
), 'x').lower();
1637 fbtime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1638 fmode
= format(int(fstatinfo
.st_mode
), 'x').lower();
1639 fchmode
= format(int(stat
.S_IMODE(fstatinfo
.st_mode
)), 'x').lower();
1640 ftypemod
= format(int(stat
.S_IFMT(fstatinfo
.st_mode
)), 'x').lower();
1641 fuid
= format(int(fstatinfo
.st_uid
), 'x').lower();
1642 fgid
= format(int(fstatinfo
.st_gid
), 'x').lower();
1647 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
1648 funame
= userinfo
.pw_name
;
1657 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
1658 fgname
= groupinfo
.gr_name
;
1663 fdev_minor
= format(int(fdev_minor
), 'x').lower();
1664 fdev_major
= format(int(fdev_major
), 'x').lower();
1665 frdev_minor
= format(int(frdev_minor
), 'x').lower();
1666 frdev_major
= format(int(frdev_major
), 'x').lower();
1667 finode
= format(int(finode
), 'x').lower();
1668 flinkcount
= format(int(flinkcount
), 'x').lower();
1669 if(hasattr(fstatinfo
, "st_file_attributes")):
1670 fwinattributes
= format(int(fstatinfo
.st_file_attributes
), 'x').lower();
1672 fwinattributes
= format(int(0), 'x').lower();
1674 fcsize
= format(int(0), 'x').lower();
1675 fcontents
= BytesIO();
1677 if(ftype
==0 or ftype
==7):
1678 with
open(fname
, "rb") as fpc
:
1679 shutil
.copyfileobj(fpc
, fcontents
);
1680 if(not compresswholefile
):
1681 fcontents
.seek(0, 2);
1682 ucfsize
= fcontents
.tell();
1683 fcontents
.seek(0, 0);
1684 if(compression
=="auto"):
1685 ilsize
= len(compressionlistalt
);
1688 while(ilmin
< ilsize
):
1689 cfcontents
= BytesIO();
1690 shutil
.copyfileobj(fcontents
, cfcontents
);
1691 fcontents
.seek(0, 0);
1692 cfcontents
.seek(0, 0);
1693 cfcontents
= CompressArchiveFile(cfcontents
, compressionlistalt
[ilmin
], compressionlevel
, formatspecs
);
1695 cfcontents
.seek(0, 2);
1696 ilcsize
.append(cfcontents
.tell());
1700 ilcsize
.append(sys
.maxint
);
1701 except AttributeError:
1702 ilcsize
.append(sys
.maxsize
);
1704 ilcmin
= ilcsize
.index(min(ilcsize
));
1705 compression
= compressionlistalt
[ilcmin
];
1706 fcontents
.seek(0, 0);
1707 cfcontents
= BytesIO();
1708 shutil
.copyfileobj(fcontents
, cfcontents
);
1709 cfcontents
.seek(0, 0);
1710 cfcontents
= CompressArchiveFile(cfcontents
, compression
, compressionlevel
, formatspecs
);
1711 cfcontents
.seek(0, 2);
1712 cfsize
= cfcontents
.tell();
1713 if(ucfsize
> cfsize
):
1714 fcsize
= format(int(cfsize
), 'x').lower();
1715 fcompression
= compression
;
1717 fcontents
= cfcontents
;
1718 if(followlink
and (ftype
==1 or ftype
==2)):
1719 flstatinfo
= os
.stat(flinkname
);
1720 with
open(flinkname
, "rb") as fpc
:
1721 shutil
.copyfileobj(fpc
, fcontents
);
1722 if(not compresswholefile
):
1723 fcontents
.seek(0, 2);
1724 ucfsize
= fcontents
.tell();
1725 fcontents
.seek(0, 0);
1726 if(compression
=="auto"):
1727 ilsize
= len(compressionlistalt
);
1730 while(ilmin
< ilsize
):
1731 cfcontents
= BytesIO();
1732 shutil
.copyfileobj(fcontents
, cfcontents
);
1733 fcontents
.seek(0, 0);
1734 cfcontents
.seek(0, 0);
1735 cfcontents
= CompressArchiveFile(cfcontents
, compressionlistalt
[ilmin
], compressionlevel
, formatspecs
);
1737 cfcontents
.seek(0, 2);
1738 ilcsize
.append(cfcontents
.tell());
1742 ilcsize
.append(sys
.maxint
);
1743 except AttributeError:
1744 ilcsize
.append(sys
.maxsize
);
1746 ilcmin
= ilcsize
.index(min(ilcsize
));
1747 compression
= compressionlistalt
[ilcmin
];
1748 fcontents
.seek(0, 0);
1749 cfcontents
= BytesIO();
1750 shutil
.copyfileobj(fcontents
, cfcontents
);
1751 cfcontents
.seek(0, 0);
1752 cfcontents
= CompressArchiveFile(cfcontents
, compression
, compressionlevel
, formatspecs
);
1753 cfcontents
.seek(0, 2);
1754 cfsize
= cfcontents
.tell();
1755 if(ucfsize
> cfsize
):
1756 fcsize
= format(int(cfsize
), 'x').lower();
1757 fcompression
= compression
;
1759 fcontents
= cfcontents
;
1760 if(fcompression
=="none"):
1762 fcontents
.seek(0, 0);
1763 ftypehex
= format(ftype
, 'x').lower();
1764 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
];
1765 fp
= AppendFileHeaderWithContent(fp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
);
1767 catfp
.write(AppendNullBytes([0, 0], formatspecs
['format_delimiter']).encode("UTF-8"));
1771 def AppendListsWithContent(inlist
, fp
, dirlistfromtxt
=False, filevalues
=[], extradata
=[], compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_dict__
, verbose
=False):
1772 formatspecs
= FormatSpecsListToDict(formatspecs
);
1774 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1775 GetDirList
= inlist
;
1783 inodetocatinode
= {};
1784 numfiles
= int(len(GetDirList
));
1785 fnumfiles
= format(numfiles
, 'x').lower();
1786 AppendFileHeader(fp
, fnumfiles
, checksumtype
, formatspecs
);
1787 for curfname
in GetDirList
:
1788 ftype
= format(curfname
[0], 'x').lower();
1789 if(re
.findall("^[.|/]", curfname
[1])):
1790 fname
= curfname
[1];
1792 fname
= "./"+curfname
[1];
1793 fbasedir
= os
.path
.dirname(fname
);
1794 flinkname
= curfname
[2];
1795 fsize
= format(curfname
[3], 'x').lower();
1796 fatime
= format(curfname
[4], 'x').lower();
1797 fmtime
= format(curfname
[5], 'x').lower();
1798 fctime
= format(curfname
[6], 'x').lower();
1799 fbtime
= format(curfname
[7], 'x').lower();
1800 fmode
= format(curfname
[8], 'x').lower();
1801 fwinattributes
= format(curfname
[9], 'x').lower();
1802 fcompression
= curfname
[10];
1803 fcsize
= format(curfname
[11], 'x').lower();
1804 fuid
= format(curfname
[12], 'x').lower();
1805 funame
= curfname
[13];
1806 fgid
= format(curfname
[14], 'x').lower();
1807 fgname
= curfname
[15];
1808 fid
= format(curfname
[16], 'x').lower();
1809 finode
= format(curfname
[17], 'x').lower();
1810 flinkcount
= format(curfname
[18], 'x').lower();
1811 fdev_minor
= format(curfname
[19], 'x').lower();
1812 fdev_major
= format(curfname
[20], 'x').lower();
1813 frdev_minor
= format(curfname
[21], 'x').lower();
1814 frdev_major
= format(curfname
[22], 'x').lower();
1815 extradata
= curfname
[23];
1816 fchecksumtype
= curfname
[24];
1817 fcontents
= curfname
[25];
1818 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
];
1819 fcontents
.seek(0, 0);
1820 fp
= AppendFileHeaderWithContent(fp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
);
1822 fp
.write(AppendNullBytes([0, 0], formatspecs
['format_delimiter']).encode("UTF-8"));
1825 def AppendInFileWithContent(infile
, fp
, dirlistfromtxt
=False, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_dict__
, verbose
=False):
1826 formatspecs
= FormatSpecsListToDict(formatspecs
);
1827 inlist
= ReadInFileBySizeWithContentToList(infile
, 0, 0, False, False, formatspecs
);
1828 return AppendListsWithContent(inlist
, fp
, dirlistfromtxt
, filevalues
, extradata
, followlink
, checksumtype
, formatspecs
, verbose
);
1830 def AppendFilesWithContentToOutFile(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
1831 formatspecs
= FormatSpecsListToDict(formatspecs
);
1832 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1833 if(os
.path
.exists(outfile
)):
1836 except OSError as e
:
1840 catfpfp
= BytesIO();
1841 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1843 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1846 fbasename
= os
.path
.splitext(outfile
)[0];
1847 fextname
= os
.path
.splitext(outfile
)[1];
1848 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
);
1849 catfp
= AppendFilesWithContent(infiles
, catfp
, dirlistfromtxt
, filevalues
, extradata
, compression
, compresswholefile
, compressionlevel
, followlink
, checksumtype
, formatspecs
, verbose
);
1850 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1851 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
1854 os
.fsync(catfp
.fileno());
1855 except io
.UnsupportedOperation
:
1857 except AttributeError:
1859 except OSError as e
:
1863 if(hasattr(sys
.stdout
, "buffer")):
1864 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1866 shutil
.copyfileobj(catfp
, sys
.stdout
);
1867 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1868 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
1870 upload_file_to_internet_file(catfp
, outfile
);
1878 def AppendListsWithContentToOutFile(inlist
, outfile
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
1879 formatspecs
= FormatSpecsListToDict(formatspecs
);
1880 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1881 if(os
.path
.exists(outfile
)):
1884 except OSError as e
:
1888 catfpfp
= BytesIO();
1889 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1891 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1894 fbasename
= os
.path
.splitext(outfile
)[0];
1895 fextname
= os
.path
.splitext(outfile
)[1];
1896 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
);
1897 catfp
= AppendListsWithContent(inlist
, catfp
, dirlistfromtxt
, filevalues
, extradata
, compression
, compresswholefile
, compressionlevel
, followlink
, checksumtype
, formatspecs
, verbose
);
1898 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1899 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
1902 os
.fsync(catfp
.fileno());
1903 except io
.UnsupportedOperation
:
1905 except AttributeError:
1907 except OSError as e
:
1911 if(hasattr(sys
.stdout
, "buffer")):
1912 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1914 shutil
.copyfileobj(catfp
, sys
.stdout
);
1915 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
1916 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
1918 upload_file_to_internet_file(catfp
, outfile
);
1926 def AppendInFileWithContentToOutFile(infile
, outfile
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, filevalues
=[], extradata
=[], followlink
=False, checksumtype
="crc32", formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
1927 formatspecs
= FormatSpecsListToDict(formatspecs
);
1928 inlist
= ReadInFileBySizeWithContentToList(infile
, 0, 0, False, False, formatspecs
);
1929 return AppendListsWithContentToOutFile(inlist
, outfile
, dirlistfromtxt
, compression
, compresswholefile
, compressionlevel
, filevalues
, extradata
, followlink
, checksumtype
, formatspecs
, verbose
, returnfp
);
1931 def PrintPermissionString(fchmode
, ftype
):
1932 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' } };
1934 for fmodval
in str(oct(fchmode
))[-3:]:
1935 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
1936 if(ftype
==0 or ftype
==7):
1937 permissionstr
= "-" + permissionstr
;
1939 permissionstr
= "h" + permissionstr
;
1941 permissionstr
= "l" + permissionstr
;
1943 permissionstr
= "c" + permissionstr
;
1945 permissionstr
= "b" + permissionstr
;
1947 permissionstr
= "d" + permissionstr
;
1949 permissionstr
= "f" + permissionstr
;
1951 permissionstr
= "D" + permissionstr
;
1953 permissionstr
= "p" + permissionstr
;
1955 permissionstr
= "w" + permissionstr
;
1957 permissionoutstr
= stat
.filemode(fchmode
);
1958 except AttributeError:
1959 permissionoutstr
= permissionstr
;
1961 permissionoutstr
= permissionstr
;
1962 return permissionoutstr
;
1964 def PrintPermissionStringAlt(fchmode
, ftype
):
1966 '0': '---', '1': '--x', '2': '-w-', '3': '-wx',
1967 '4': 'r--', '5': 'r-x', '6': 'rw-', '7': 'rwx'
1969 # Translate file mode into permission string
1970 permissionstr
= ''.join([permissions
[i
] for i
in str(oct(fchmode
))[-3:]]);
1971 # Append file type indicator
1973 0: '-', 1: 'h', 2: 'l', 3: 'c', 4: 'b',
1974 5: 'd', 6: 'f', 8: 'D', 9: 'p', 10: 'w'
1976 file_type
= type_indicators
.get(ftype
, '-');
1977 permissionstr
= file_type
+ permissionstr
;
1979 permissionoutstr
= stat
.filemode(fchmode
);
1980 except AttributeError:
1981 permissionoutstr
= permissionstr
;
1982 return permissionoutstr
;
1984 def CompressionSupport():
1985 compression_list
= [];
1988 compression_list
.append("gz");
1989 compression_list
.append("gzip");
1994 compression_list
.append("bz2");
1995 compression_list
.append("bzip2");
2000 compression_list
.append("lz4");
2005 compression_list
.append("lzo");
2006 compression_list
.append("lzop");
2011 compression_list
.append("zstd");
2012 compression_list
.append("zstandard");
2017 compression_list
.append("lzma");
2018 compression_list
.append("xz");
2021 return compression_list
;
2023 def CheckCompressionType(infile
, formatspecs
=__file_format_dict__
, closefp
=True):
2024 formatspecs
= FormatSpecsListToDict(formatspecs
);
2025 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2029 catfp
= open(infile
, "rb");
2030 except FileNotFoundError
:
2033 prefp
= catfp
.read(2);
2035 if(prefp
==binascii
.unhexlify("1f8b")):
2038 prefp
= catfp
.read(3);
2039 if(prefp
==binascii
.unhexlify("425a68")):
2041 if(prefp
==binascii
.unhexlify("5d0000")):
2044 prefp
= catfp
.read(4);
2045 if(prefp
==binascii
.unhexlify("28b52ffd")):
2047 if(prefp
==binascii
.unhexlify("04224d18")):
2049 if(prefp
==binascii
.unhexlify("504B0304")):
2050 filetype
= "zipfile";
2052 prefp
= catfp
.read(5);
2053 if(prefp
==binascii
.unhexlify("7573746172")):
2054 filetype
= "tarfile";
2056 prefp
= catfp
.read(6);
2057 if(prefp
==binascii
.unhexlify("fd377a585a00")):
2059 if(prefp
==binascii
.unhexlify("377abcaf271c")):
2060 filetype
= "7zipfile";
2062 prefp
= catfp
.read(7);
2063 if(prefp
==binascii
.unhexlify("526172211a0700")):
2064 filetype
= "rarfile";
2065 if(prefp
==binascii
.unhexlify("43617446696c65")):
2066 filetype
= "catfile";
2068 prefp
= catfp
.read(8);
2069 if(prefp
==binascii
.unhexlify("526172211a070100")):
2070 filetype
= "rarfile";
2072 prefp
= catfp
.read(formatspecs
['format_len']);
2073 if(prefp
==binascii
.unhexlify(formatspecs
['format_hex'])):
2074 filetype
= formatspecs
['format_lower'];
2076 prefp
= catfp
.read(9);
2077 if(prefp
==binascii
.unhexlify("894c5a4f000d0a1a0a")):
2080 prefp
= catfp
.read(10);
2081 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
2082 filetype
= "tarfile";
2084 if(filetype
=="gzip" or filetype
=="bzip2" or filetype
=="lzma" or filetype
=="zstd" or filetype
=="lz4"):
2085 if(is_tarfile(catfp
)):
2086 filetype
= "tarfile";
2088 if(is_tarfile(catfp
)):
2089 filetype
= "tarfile";
2090 elif(zipfile
.is_zipfile(catfp
)):
2091 filetype
= "zipfile";
2092 elif(rarfile
.is_rarfile(catfp
) or rarfile
.is_rarfile_sfx(catfp
)):
2093 filetype
= "rarile";
2101 def CheckCompressionTypeFromString(instring
, formatspecs
=__file_format_dict__
, closefp
=True):
2102 formatspecs
= FormatSpecsListToDict(formatspecs
);
2104 instringsfile
= BytesIO(instring
);
2106 instringsfile
= BytesIO(instring
.encode("UTF-8"));
2107 return CheckCompressionType(instringsfile
, formatspecs
, closefp
);
2109 def GetCompressionMimeType(infile
, formatspecs
=__file_format_dict__
):
2110 formatspecs
= FormatSpecsListToDict(formatspecs
);
2111 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
2112 if(compresscheck
=="gzip" or compresscheck
=="gz"):
2113 return archivefile_gzip_mimetype
;
2114 if(compresscheck
=="bzip2" or compresscheck
=="bz2"):
2115 return archivefile_bzip2_mimetype
;
2116 if(compresscheck
=="zstd" or compresscheck
=="zstandard"):
2117 return archivefile_zstandard_mimetype
;
2118 if(compresscheck
=="lz4"):
2119 return archivefile_lz4_mimetype
;
2120 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
2121 return archivefile_lzop_mimetype
;
2122 if(compresscheck
=="lzma"):
2123 return archivefile_lzma_mimetype
;
2124 if(compresscheck
=="xz"):
2125 return archivefile_xz_mimetype
;
2126 if(compresscheck
=="catfile" or compresscheck
=="cat" or compresscheck
==formatspecs
['format_lower']):
2127 return archivefile_cat_mimetype
;
2128 if(not compresscheck
):
2132 def UncompressArchiveFile(fp
, formatspecs
=__file_format_dict__
):
2133 formatspecs
= FormatSpecsListToDict(formatspecs
);
2134 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
2136 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
2137 if(compresscheck
=="gzip"):
2142 catfp
= gzip
.GzipFile(fileobj
=fp
, mode
="rb");
2143 if(compresscheck
=="bzip2"):
2149 catfp
.write(bz2
.decompress(fp
.read()));
2150 if(compresscheck
=="zstd"):
2156 catfp
.write(zstandard
.decompress(fp
.read()));
2157 if(compresscheck
=="lz4"):
2163 catfp
.write(lz4
.frame
.decompress(fp
.read()));
2164 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
2170 catfp
.write(lzo
.decompress(fp
.read()));
2171 if(compresscheck
=="lzma" or compresscheck
=="xz"):
2177 catfp
.write(lzma
.decompress(fp
.read()));
2178 if(compresscheck
=="catfile" or compresscheck
==formatspecs
['format_lower']):
2180 if(not compresscheck
):
2186 with fp
as fpcontent
:
2188 catfp
.write(lzma
.decompress(fp
.read()));
2189 except lzma
.LZMAError
:
2191 if(compresscheck
!="catfile" or compresscheck
!=formatspecs
['format_lower']):
2195 create_alias_function("Uncompress", __file_format_name__
, "", UncompressArchiveFile
);
2197 def UncompressFile(infile
, formatspecs
=__file_format_dict__
, mode
="rb"):
2198 formatspecs
= FormatSpecsListToDict(formatspecs
);
2199 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
2200 if(sys
.version_info
[0]==2 and compresscheck
):
2206 if(compresscheck
=="gzip"):
2212 filefp
= gzip
.open(infile
, mode
, encoding
="UTF-8");
2213 except (ValueError, TypeError) as e
:
2214 filefp
= gzip
.open(infile
, mode
);
2215 if(compresscheck
=="bzip2"):
2221 filefp
= bz2
.open(infile
, mode
, encoding
="UTF-8");
2222 except (ValueError, TypeError) as e
:
2223 filefp
= bz2
.open(infile
, mode
);
2224 if(compresscheck
=="zstd"):
2230 filefp
= zstandard
.open(infile
, mode
, encoding
="UTF-8");
2231 except (ValueError, TypeError) as e
:
2232 filefp
= zstandard
.open(infile
, mode
);
2233 if(compresscheck
=="lz4"):
2239 filefp
= lz4
.frame
.open(infile
, mode
, encoding
="UTF-8");
2240 except (ValueError, TypeError) as e
:
2241 filefp
= lz4
.frame
.open(infile
, mode
);
2242 if(compresscheck
=="lzo"):
2248 filefp
= lzo
.open(infile
, mode
, encoding
="UTF-8");
2249 except (ValueError, TypeError) as e
:
2250 filefp
= lzo
.open(infile
, mode
);
2251 if(compresscheck
=="lzma"):
2257 filefp
= lzma
.open(infile
, mode
, encoding
="UTF-8");
2258 except (ValueError, TypeError) as e
:
2259 filefp
= lzma
.open(infile
, mode
);
2260 if(compresscheck
=="catfile" or compresscheck
==formatspecs
['format_lower']):
2262 filefp
= open(infile
, mode
, encoding
="UTF-8");
2263 except (ValueError, TypeError) as e
:
2264 filefp
= open(infile
, mode
);
2265 if(not compresscheck
):
2267 filefp
= open(infile
, mode
, encoding
="UTF-8");
2268 except (ValueError, TypeError) as e
:
2269 filefp
= open(infile
, mode
);
2270 except FileNotFoundError
:
2274 def UncompressString(infile
):
2275 compresscheck
= CheckCompressionTypeFromString(infile
, formatspecs
, False);
2276 if(compresscheck
=="gzip"):
2281 fileuz
= gzip
.decompress(infile
);
2282 if(compresscheck
=="bzip2"):
2287 fileuz
= bz2
.decompress(infile
);
2288 if(compresscheck
=="zstd"):
2293 fileuz
= zstandard
.decompress(infile
);
2294 if(compresscheck
=="lz4"):
2299 fileuz
= lz4
.frame
.decompress(infile
);
2300 if(compresscheck
=="lzo"):
2305 fileuz
= lzo
.decompress(infile
);
2306 if(compresscheck
=="lzma"):
2311 fileuz
= lzma
.decompress(infile
);
2312 if(not compresscheck
):
2314 if(hasattr(fileuz
, 'decode')):
2315 fileuz
= fileuz
.decode("UTF-8");
2318 def UncompressStringAlt(infile
):
2319 filefp
= StringIO();
2320 outstring
= UncompressString(infile
);
2321 filefp
.write(outstring
);
2325 def CheckCompressionSubType(infile
, formatspecs
=__file_format_dict__
, closefp
=True):
2326 formatspecs
= FormatSpecsListToDict(formatspecs
);
2327 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
2328 if(not compresscheck
):
2329 fextname
= os
.path
.splitext(infile
)[1];
2330 if(fextname
==".gz"):
2331 compresscheck
= "gzip";
2332 elif(fextname
==".bz2"):
2333 compresscheck
= "bzip2";
2334 elif(fextname
==".zst"):
2335 compresscheck
= "zstd";
2336 elif(fextname
==".lz4"):
2337 compresscheck
= "lz4";
2338 elif(fextname
==".lzo" or fextname
==".lzop"):
2339 compresscheck
= "lzo";
2340 elif(fextname
==".lzma" or fextname
==".xz"):
2341 compresscheck
= "lzma";
2344 if(compresscheck
=="gzip" or compresscheck
=="bzip2" or compresscheck
=="lzma" or compresscheck
=="zstd" or compresscheck
=="lz4"):
2345 if(is_tarfile(infile
)):
2346 filetype
= "tarfile";
2347 if(not compresscheck
):
2348 if(is_tarfile(infile
)):
2350 elif(zipfile
.is_zipfile(infile
)):
2352 elif(rarfile
.is_rarfile(infile
) or rarfile
.is_rarfile_sfx(infile
)):
2357 if(compresscheck
=="catfile"):
2359 if(compresscheck
==formatspecs
['format_lower']):
2360 return formatspecs
['format_lower'];
2361 if(compresscheck
=="tarfile"):
2363 if(compresscheck
=="zipfile"):
2365 if(rarfile_support
and compresscheck
=="rarfile"):
2367 if(py7zr_support
and compresscheck
=="7zipfile"):
2369 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2370 catfp
= UncompressArchiveFile(infile
, formatspecs
['format_lower']);
2373 if(compresscheck
=="gzip"):
2378 catfp
= gzip
.GzipFile(infile
, "rb");
2379 if(compresscheck
=="bzip2"):
2384 catfp
= bz2
.BZ2File(infile
, "rb");
2385 if(compresscheck
=="lz4"):
2390 catfp
= lz4
.frame
.open(infile
, "rb");
2391 if(compresscheck
=="zstd"):
2396 catfp
= zstandard
.open(infile
, "rb");
2397 if(compresscheck
=="lzma" or compresscheck
=="xz"):
2402 catfp
= lzma
.open(infile
, "rb");
2403 except FileNotFoundError
:
2406 prefp
= catfp
.read(5);
2407 if(prefp
==binascii
.unhexlify("7573746172")):
2408 filetype
= "tarfile";
2410 prefp
= catfp
.read(7);
2411 if(prefp
==binascii
.unhexlify("43617446696c65")):
2412 filetype
= "catfile";
2414 prefp
= catfp
.read(formatspecs
['format_len']);
2415 if(prefp
==binascii
.unhexlify(formatspecs
['format_hex'])):
2416 filetype
= formatspecs
['format_lower'];
2418 prefp
= catfp
.read(10);
2419 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
2420 filetype
= "tarfile";
2426 def GZipCompress(data
, compresslevel
=9):
2431 tmpfp
= tempfile
.NamedTemporaryFile("wb", delete
=False);
2433 tmpfp
= gzip
.GzipFile(tmpfp
.name
, mode
="wb", compresslevel
=compresslevel
);
2437 catfp
= open(tmpfp
.name
, "rb");
2438 except FileNotFoundError
:
2440 catdata
= catfp
.read();
2444 def CompressArchiveFile(fp
, compression
="auto", compressionlevel
=None, formatspecs
=__file_format_dict__
):
2445 formatspecs
= FormatSpecsListToDict(formatspecs
);
2446 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
2449 if(not compression
or compression
=="catfile" or compression
==formatspecs
['format_lower']):
2450 compression
= "auto";
2451 if(compression
not in compressionlist
and compression
is None):
2452 compression
= "auto";
2453 if(compression
=="gzip"):
2459 if(compressionlevel
is None):
2460 compressionlevel
= 9;
2462 compressionlevel
= int(compressionlevel
);
2463 catfp
.write(gzip
.compress(fp
.read(), compresslevel
=compressionlevel
));
2464 if(compression
=="bzip2"):
2470 if(compressionlevel
is None):
2471 compressionlevel
= 9;
2473 compressionlevel
= int(compressionlevel
);
2474 catfp
.write(bz2
.compress(fp
.read(), compresslevel
=compressionlevel
));
2475 if(compression
=="lz4"):
2481 if(compressionlevel
is None):
2482 compressionlevel
= 9;
2484 compressionlevel
= int(compressionlevel
);
2485 catfp
.write(lz4
.frame
.compress(fp
.read(), compression_level
=compressionlevel
));
2486 if(compression
=="lzo" or compression
=="lzop"):
2492 if(compressionlevel
is None):
2493 compressionlevel
= 9;
2495 compressionlevel
= int(compressionlevel
);
2496 catfp
.write(lzo
.compress(fp
.read(), compresslevel
=compressionlevel
));
2497 if(compression
=="zstd"):
2503 if(compressionlevel
is None):
2504 compressionlevel
= 10;
2506 compressionlevel
= int(compressionlevel
);
2507 catfp
.write(zstandard
.compress(fp
.read(), level
=compressionlevel
));
2508 if(compression
=="lzma"):
2514 if(compressionlevel
is None):
2515 compressionlevel
= 9;
2517 compressionlevel
= int(compressionlevel
);
2518 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]));
2519 if(compression
=="xz"):
2525 if(compressionlevel
is None):
2526 compressionlevel
= 9;
2528 compressionlevel
= int(compressionlevel
);
2529 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]));
2530 if(compression
=="auto" or compression
is None):
2535 create_alias_function("Compress", __file_format_name__
, "", CompressArchiveFile
);
2537 def CompressOpenFile(outfile
, compressionenable
=True, compressionlevel
=None):
2538 if(outfile
is None):
2540 fbasename
= os
.path
.splitext(outfile
)[0];
2541 fextname
= os
.path
.splitext(outfile
)[1];
2542 if(compressionlevel
is None and fextname
!=".zst"):
2543 compressionlevel
= 9;
2544 elif(compressionlevel
is None and fextname
==".zst"):
2545 compressionlevel
= 10;
2547 compressionlevel
= int(compressionlevel
);
2548 if(sys
.version_info
[0]==2):
2553 if(fextname
not in outextlistwd
or not compressionenable
):
2555 outfp
= open(outfile
, "wb", encoding
="UTF-8");
2556 except (ValueError, TypeError) as e
:
2557 outfp
= open(outfile
, "wb");
2558 elif(fextname
==".gz"):
2564 outfp
= gzip
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
2565 except (ValueError, TypeError) as e
:
2566 outfp
= gzip
.open(outfile
, mode
, compressionlevel
);
2567 elif(fextname
==".bz2"):
2573 outfp
= bz2
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
2574 except (ValueError, TypeError) as e
:
2575 outfp
= bz2
.open(outfile
, mode
, compressionlevel
);
2576 elif(fextname
==".zst"):
2582 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
), encoding
="UTF-8");
2583 except (ValueError, TypeError) as e
:
2584 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
));
2585 elif(fextname
==".xz"):
2591 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}], encoding
="UTF-8");
2592 except (ValueError, TypeError) as e
:
2593 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]);
2594 elif(fextname
==".lz4"):
2600 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
, encoding
="UTF-8");
2601 except (ValueError, TypeError) as e
:
2602 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
);
2603 elif(fextname
==".lzo"):
2609 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
, encoding
="UTF-8");
2610 except (ValueError, TypeError) as e
:
2611 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
);
2612 elif(fextname
==".lzma"):
2618 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}], encoding
="UTF-8");
2619 except (ValueError, TypeError) as e
:
2620 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]);
2621 except FileNotFoundError
:
2625 def GetDevMajorMinor(fdev
):
2627 if(hasattr(os
, "minor")):
2628 retdev
.append(os
.minor(fdev
));
2631 if(hasattr(os
, "major")):
2632 retdev
.append(os
.major(fdev
));
2637 def CheckSumSupport(checkfor
, guaranteed
=True):
2640 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
2641 except AttributeError:
2642 hash_list
= sorted(list(hashlib
.algorithms
));
2645 hash_list
= sorted(list(hashlib
.algorithms_available
));
2646 except AttributeError:
2647 hash_list
= sorted(list(hashlib
.algorithms
));
2648 checklistout
= sorted(hash_list
+ ['adler32', 'crc16', 'crc16_ansi', 'crc16_ibm', 'crc16_ccitt', 'crc32', 'crc64', 'crc64_ecma', 'crc64_iso', 'none']);
2649 if(checkfor
in checklistout
):
2654 def CheckSumSupportAlt(checkfor
, guaranteed
=True):
2657 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
2658 except AttributeError:
2659 hash_list
= sorted(list(hashlib
.algorithms
));
2662 hash_list
= sorted(list(hashlib
.algorithms_available
));
2663 except AttributeError:
2664 hash_list
= sorted(list(hashlib
.algorithms
));
2665 checklistout
= hash_list
;
2666 if(checkfor
in checklistout
):
2671 def PackArchiveFile(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
2672 formatspecs
= FormatSpecsListToDict(formatspecs
);
2673 advancedlist
= formatspecs
['use_advanced_list'];
2674 altinode
= formatspecs
['use_alt_inode'];
2675 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2676 outfile
= RemoveWindowsPath(outfile
);
2677 checksumtype
= checksumtype
.lower();
2678 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2679 checksumtype
="crc32";
2680 if(checksumtype
=="none"):
2682 if(not compression
or compression
=="catfile" or compression
==formatspecs
['format_lower']):
2683 compression
= "auto";
2684 if(compression
not in compressionlist
and compression
is None):
2685 compression
= "auto";
2687 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2688 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2689 if(os
.path
.exists(outfile
)):
2692 except OSError as e
:
2697 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2699 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2702 fbasename
= os
.path
.splitext(outfile
)[0];
2703 fextname
= os
.path
.splitext(outfile
)[1];
2704 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
);
2705 catver
= formatspecs
['format_ver'];
2706 fileheaderver
= str(int(catver
.replace(".", "")));
2709 for line
in sys
.stdin
:
2710 infilelist
.append(line
.strip());
2711 infilelist
= list(filter(None, infilelist
));
2712 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
2713 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
2715 with
UncompressFile(infiles
, formatspecs
, "r") as finfile
:
2716 for line
in finfile
:
2717 infilelist
.append(line
.strip());
2718 infilelist
= list(filter(None, infilelist
));
2720 if(isinstance(infiles
, (list, tuple, ))):
2721 infilelist
= list(filter(None, infiles
));
2722 elif(isinstance(infiles
, (str, ))):
2723 infilelist
= list(filter(None, [infiles
]));
2725 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
2727 GetDirList
= ListDir(infilelist
, followlink
, False);
2735 inodetocatinode
= {};
2736 numfiles
= int(len(GetDirList
));
2737 catfp
= AppendFileHeader(catfp
, numfiles
, checksumtype
, formatspecs
);
2738 for curfname
in GetDirList
:
2739 if(re
.findall("^[.|/]", curfname
)):
2742 fname
= "./"+curfname
;
2744 VerbosePrintOut(fname
);
2745 if(not followlink
or followlink
is None):
2746 fstatinfo
= os
.lstat(fname
);
2748 fstatinfo
= os
.stat(fname
);
2749 fpremode
= fstatinfo
.st_mode
;
2750 finode
= fstatinfo
.st_ino
;
2751 flinkcount
= fstatinfo
.st_nlink
;
2753 if(stat
.S_ISREG(fpremode
)):
2755 elif(stat
.S_ISLNK(fpremode
)):
2757 elif(stat
.S_ISCHR(fpremode
)):
2759 elif(stat
.S_ISBLK(fpremode
)):
2761 elif(stat
.S_ISDIR(fpremode
)):
2763 elif(stat
.S_ISFIFO(fpremode
)):
2765 elif(stat
.S_ISSOCK(fpremode
)):
2767 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
2769 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
2771 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
2776 fcurfid
= format(int(curfid
), 'x').lower();
2777 if(not followlink
and finode
!=0):
2779 if(finode
in inodelist
):
2781 flinkname
= inodetofile
[finode
];
2783 fcurinode
= format(int(finode
), 'x').lower();
2785 fcurinode
= format(int(inodetocatinode
[finode
]), 'x').lower();
2786 if(finode
not in inodelist
):
2787 inodelist
.append(finode
);
2788 inodetofile
.update({finode
: fname
});
2789 inodetocatinode
.update({finode
: curinode
});
2791 fcurinode
= format(int(finode
), 'x').lower();
2793 fcurinode
= format(int(curinode
), 'x').lower();
2794 curinode
= curinode
+ 1;
2796 fcurinode
= format(int(curinode
), 'x').lower();
2797 curinode
= curinode
+ 1;
2798 curfid
= curfid
+ 1;
2800 flinkname
= os
.readlink(fname
);
2801 fdev
= fstatinfo
.st_dev
;
2802 getfdev
= GetDevMajorMinor(fdev
);
2803 fdev_minor
= getfdev
[0];
2804 fdev_major
= getfdev
[1];
2805 frdev
= fstatinfo
.st_dev
;
2806 if(hasattr(fstatinfo
, "st_rdev")):
2807 frdev
= fstatinfo
.st_rdev
;
2809 frdev
= fstatinfo
.st_dev
;
2810 getfrdev
= GetDevMajorMinor(frdev
);
2811 frdev_minor
= getfrdev
[0];
2812 frdev_major
= getfrdev
[1];
2813 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
2814 fsize
= format(int("0"), 'x').lower();
2815 elif(ftype
==0 or ftype
==7):
2816 fsize
= format(int(fstatinfo
.st_size
), 'x').lower();
2818 fsize
= format(int(fstatinfo
.st_size
)).lower();
2819 fatime
= format(int(fstatinfo
.st_atime
), 'x').lower();
2820 fmtime
= format(int(fstatinfo
.st_mtime
), 'x').lower();
2821 fctime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
2822 if(hasattr(fstatinfo
, "st_birthtime")):
2823 fbtime
= format(int(fstatinfo
.st_birthtime
), 'x').lower();
2825 fbtime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
2826 fmode
= format(int(fstatinfo
.st_mode
), 'x').lower();
2827 fchmode
= format(int(stat
.S_IMODE(fstatinfo
.st_mode
)), 'x').lower();
2828 ftypemod
= format(int(stat
.S_IFMT(fstatinfo
.st_mode
)), 'x').lower();
2829 fuid
= format(int(fstatinfo
.st_uid
), 'x').lower();
2830 fgid
= format(int(fstatinfo
.st_gid
), 'x').lower();
2835 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
2836 funame
= userinfo
.pw_name
;
2845 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
2846 fgname
= groupinfo
.gr_name
;
2851 fdev_minor
= format(int(fdev_minor
), 'x').lower();
2852 fdev_major
= format(int(fdev_major
), 'x').lower();
2853 frdev_minor
= format(int(frdev_minor
), 'x').lower();
2854 frdev_major
= format(int(frdev_major
), 'x').lower();
2855 finode
= format(int(finode
), 'x').lower();
2856 flinkcount
= format(int(flinkcount
), 'x').lower();
2857 if(hasattr(fstatinfo
, "st_file_attributes")):
2858 fwinattributes
= format(int(fstatinfo
.st_file_attributes
), 'x').lower();
2860 fwinattributes
= format(int(0), 'x').lower();
2862 fcsize
= format(int(0), 'x').lower();
2863 fcontents
= BytesIO();
2864 if(ftype
==0 or ftype
==7):
2865 with
open(fname
, "rb") as fpc
:
2866 shutil
.copyfileobj(fpc
, fcontents
);
2867 if(not compresswholefile
):
2868 fcontents
.seek(0, 2);
2869 ucfsize
= fcontents
.tell();
2870 fcontents
.seek(0, 0);
2871 if(compression
=="auto"):
2872 ilsize
= len(compressionlistalt
);
2875 while(ilmin
< ilsize
):
2876 cfcontents
= BytesIO();
2877 shutil
.copyfileobj(fcontents
, cfcontents
);
2878 fcontents
.seek(0, 0);
2879 cfcontents
.seek(0, 0);
2880 cfcontents
= CompressArchiveFile(cfcontents
, compressionlistalt
[ilmin
], compressionlevel
, formatspecs
);
2882 cfcontents
.seek(0, 2);
2883 ilcsize
.append(cfcontents
.tell());
2887 ilcsize
.append(sys
.maxint
);
2888 except AttributeError:
2889 ilcsize
.append(sys
.maxsize
);
2891 ilcmin
= ilcsize
.index(min(ilcsize
));
2892 compression
= compressionlistalt
[ilcmin
];
2893 fcontents
.seek(0, 0);
2894 cfcontents
= BytesIO();
2895 shutil
.copyfileobj(fcontents
, cfcontents
);
2896 cfcontents
.seek(0, 0);
2897 cfcontents
= CompressArchiveFile(cfcontents
, compression
, compressionlevel
, formatspecs
);
2898 cfcontents
.seek(0, 2);
2899 cfsize
= cfcontents
.tell();
2900 if(ucfsize
> cfsize
):
2901 fcsize
= format(int(cfsize
), 'x').lower();
2902 fcompression
= compression
;
2904 fcontents
= cfcontents
;
2905 if(fcompression
=="none"):
2907 if(followlink
and (ftype
==1 or ftype
==2)):
2908 flstatinfo
= os
.stat(flinkname
);
2909 with
open(flinkname
, "rb") as fpc
:
2910 shutil
.copyfileobj(fpc
, fcontents
);
2911 if(not compresswholefile
):
2912 fcontents
.seek(0, 2);
2913 ucfsize
= fcontents
.tell();
2914 fcontents
.seek(0, 0);
2915 if(compression
=="auto"):
2916 ilsize
= len(compressionlistalt
);
2919 while(ilmin
< ilsize
):
2920 cfcontents
= BytesIO();
2921 shutil
.copyfileobj(fcontents
, cfcontents
);
2922 fcontents
.seek(0, 0);
2923 cfcontents
.seek(0, 0);
2924 cfcontents
= CompressArchiveFile(cfcontents
, compressionlistalt
[ilmin
], compressionlevel
, formatspecs
);
2926 cfcontents
.seek(0, 2);
2927 ilcsize
.append(cfcontents
.tell());
2931 ilcsize
.append(sys
.maxint
);
2932 except AttributeError:
2933 ilcsize
.append(sys
.maxsize
);
2935 ilcmin
= ilcsize
.index(min(ilcsize
));
2936 compression
= compressionlistalt
[ilcmin
];
2937 fcontents
.seek(0, 0);
2938 cfcontents
= BytesIO();
2939 shutil
.copyfileobj(fcontents
, cfcontents
);
2940 cfcontents
.seek(0, 0);
2941 cfcontents
= CompressArchiveFile(cfcontents
, compression
, compressionlevel
, formatspecs
);
2942 cfcontents
.seek(0, 2);
2943 cfsize
= cfcontents
.tell();
2944 if(ucfsize
> cfsize
):
2945 fcsize
= format(int(cfsize
), 'x').lower();
2946 fcompression
= compression
;
2948 fcontents
= cfcontents
;
2949 fcontents
.seek(0, 0);
2950 ftypehex
= format(ftype
, 'x').lower();
2951 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
];
2952 catfp
= AppendFileHeaderWithContent(catfp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
);
2955 catfp
.write(AppendNullBytes([0, 0], formatspecs
['format_delimiter']).encode("UTF-8"));
2956 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2957 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
2960 os
.fsync(catfp
.fileno());
2961 except io
.UnsupportedOperation
:
2963 except AttributeError:
2965 except OSError as e
:
2969 if(hasattr(sys
.stdout
, "buffer")):
2970 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2972 shutil
.copyfileobj(catfp
, sys
.stdout
);
2973 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
2974 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
2976 upload_file_to_internet_file(catfp
, outfile
);
2984 create_alias_function("Pack", __file_format_name__
, "", PackArchiveFile
);
2986 if(hasattr(shutil
, "register_archive_format")):
2987 def PackArchiveFileFunc(archive_name
, source_dir
, **kwargs
):
2988 return PackArchiveFile(source_dir
, archive_name
, False, "auto", True, None, False, "crc32", [], __file_format_dict__
['format_delimiter'], False, False);
2989 create_alias_function("Pack", __file_format_name__
, "Func", PackArchiveFileFunc
);
2991 def PackArchiveFileFromDirList(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
2992 formatspecs
= FormatSpecsListToDict(formatspecs
);
2993 return PackArchiveFile(infiles
, outfile
, dirlistfromtxt
, compression
, compresswholefile
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
2995 create_alias_function("Pack", __file_format_name__
, "FromDirList", PackArchiveFileFromDirList
);
2997 def PackArchiveFileFromTarFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
2998 formatspecs
= FormatSpecsListToDict(formatspecs
);
2999 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3000 outfile
= RemoveWindowsPath(outfile
);
3001 checksumtype
= checksumtype
.lower();
3002 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
3003 checksumtype
="crc32";
3004 if(checksumtype
=="none"):
3006 if(not compression
or compression
=="catfile" or compression
==formatspecs
['format_lower']):
3007 compression
= "auto";
3008 if(compression
not in compressionlist
and compression
is None):
3009 compression
= "auto";
3011 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
3012 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3013 if(os
.path
.exists(outfile
)):
3016 except OSError as e
:
3021 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3023 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3026 fbasename
= os
.path
.splitext(outfile
)[0];
3027 fextname
= os
.path
.splitext(outfile
)[1];
3028 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
);
3029 catver
= formatspecs
['format_ver'];
3030 fileheaderver
= str(int(catver
.replace(".", "")));
3036 inodetocatinode
= {};
3039 if(hasattr(sys
.stdin
, "buffer")):
3040 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
3042 shutil
.copyfileobj(sys
.stdin
, infile
);
3047 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
3048 infile
= download_file_from_internet_file(infile
);
3053 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
3055 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
3057 if(not tarfile
.is_tarfile(infile
)):
3059 except AttributeError:
3060 if(not is_tarfile(infile
)):
3065 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3066 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
3068 tarfp
= tarfile
.open(infile
, "r");
3069 except FileNotFoundError
:
3071 numfiles
= int(len(tarfp
.getmembers()));
3072 catfp
= AppendFileHeader(catfp
, numfiles
, checksumtype
, formatspecs
);
3073 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
3074 if(re
.findall("^[.|/]", member
.name
)):
3075 fname
= member
.name
;
3077 fname
= "./"+member
.name
;
3079 VerbosePrintOut(fname
);
3080 fpremode
= member
.mode
;
3081 ffullmode
= member
.mode
;
3085 ffullmode
= member
.mode
+ stat
.S_IFREG
;
3087 elif(member
.isdev()):
3088 ffullmode
= member
.mode
;
3090 elif(member
.islnk()):
3091 ffullmode
= member
.mode
+ stat
.S_IFREG
;
3093 elif(member
.issym()):
3094 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
3096 elif(member
.ischr()):
3097 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
3099 elif(member
.isblk()):
3100 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
3102 elif(member
.isdir()):
3103 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
3105 elif(member
.isfifo()):
3106 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
3108 elif(member
.issparse()):
3109 ffullmode
= member
.mode
;
3112 ffullmode
= member
.mode
;
3115 fcurfid
= format(int(curfid
), 'x').lower();
3116 fcurinode
= format(int(curfid
), 'x').lower();
3117 curfid
= curfid
+ 1;
3119 flinkname
= member
.linkname
;
3120 fdev_minor
= format(int(member
.devminor
), 'x').lower();
3121 fdev_major
= format(int(member
.devmajor
), 'x').lower();
3122 frdev_minor
= format(int(member
.devminor
), 'x').lower();
3123 frdev_major
= format(int(member
.devmajor
), 'x').lower();
3124 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
3125 fsize
= format(int("0"), 'x').lower();
3126 elif(ftype
==0 or ftype
==7):
3127 fsize
= format(int(member
.size
), 'x').lower();
3129 fsize
= format(int(member
.size
), 'x').lower();
3130 fatime
= format(int(member
.mtime
), 'x').lower();
3131 fmtime
= format(int(member
.mtime
), 'x').lower();
3132 fctime
= format(int(member
.mtime
), 'x').lower();
3133 fbtime
= format(int(member
.mtime
), 'x').lower();
3134 fmode
= format(int(ffullmode
), 'x').lower();
3135 fchmode
= format(int(stat
.S_IMODE(ffullmode
)), 'x').lower();
3136 ftypemod
= format(int(stat
.S_IFMT(ffullmode
)), 'x').lower();
3137 fuid
= format(int(member
.uid
), 'x').lower();
3138 fgid
= format(int(member
.gid
), 'x').lower();
3139 funame
= member
.uname
;
3140 fgname
= member
.gname
;
3141 flinkcount
= format(int(flinkcount
), 'x').lower();
3142 fwinattributes
= format(int(0), 'x').lower();
3144 fcsize
= format(int(0), 'x').lower();
3145 fcontents
= BytesIO();
3146 if(ftype
==0 or ftype
==7):
3147 with tarfp
.extractfile(member
) as fpc
:
3148 shutil
.copyfileobj(fpc
, fcontents
);
3149 if(not compresswholefile
):
3150 fcontents
.seek(0, 2);
3151 ucfsize
= fcontents
.tell();
3152 fcontents
.seek(0, 0);
3153 if(compression
=="auto"):
3154 ilsize
= len(compressionlistalt
);
3157 while(ilmin
< ilsize
):
3158 cfcontents
= BytesIO();
3159 shutil
.copyfileobj(fcontents
, cfcontents
);
3160 fcontents
.seek(0, 0);
3161 cfcontents
.seek(0, 0);
3162 cfcontents
= CompressArchiveFile(cfcontents
, compressionlistalt
[ilmin
], compressionlevel
, formatspecs
);
3164 cfcontents
.seek(0, 2);
3165 ilcsize
.append(cfcontents
.tell());
3169 ilcsize
.append(sys
.maxint
);
3170 except AttributeError:
3171 ilcsize
.append(sys
.maxsize
);
3173 ilcmin
= ilcsize
.index(min(ilcsize
));
3174 compression
= compressionlistalt
[ilcmin
];
3175 fcontents
.seek(0, 0);
3176 cfcontents
= BytesIO();
3177 shutil
.copyfileobj(fcontents
, cfcontents
);
3178 cfcontents
.seek(0, 0);
3179 cfcontents
= CompressArchiveFile(cfcontents
, compression
, compressionlevel
, formatspecs
);
3180 cfcontents
.seek(0, 2);
3181 cfsize
= cfcontents
.tell();
3182 if(ucfsize
> cfsize
):
3183 fcsize
= format(int(cfsize
), 'x').lower();
3184 fcompression
= compression
;
3186 fcontents
= cfcontents
;
3187 if(fcompression
=="none"):
3189 fcontents
.seek(0, 0);
3190 ftypehex
= format(ftype
, 'x').lower();
3191 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
];
3192 catfp
= AppendFileHeaderWithContent(catfp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
);
3195 catfp
.write(AppendNullBytes([0, 0], formatspecs
['format_delimiter']).encode("UTF-8"));
3196 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3197 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
3200 os
.fsync(catfp
.fileno());
3201 except io
.UnsupportedOperation
:
3203 except AttributeError:
3205 except OSError as e
:
3209 if(hasattr(sys
.stdout
, "buffer")):
3210 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
3212 shutil
.copyfileobj(catfp
, sys
.stdout
);
3213 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3214 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
3216 upload_file_to_internet_file(catfp
, outfile
);
3224 create_alias_function("Pack", __file_format_name__
, "FromTarFile", PackArchiveFileFromTarFile
);
3226 def PackArchiveFileFromZipFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
3227 formatspecs
= FormatSpecsListToDict(formatspecs
);
3228 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3229 outfile
= RemoveWindowsPath(outfile
);
3230 checksumtype
= checksumtype
.lower();
3231 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
3232 checksumtype
="crc32";
3233 if(checksumtype
=="none"):
3235 if(not compression
or compression
=="catfile" or compression
==formatspecs
['format_lower']):
3236 compression
= "auto";
3237 if(compression
not in compressionlist
and compression
is None):
3238 compression
= "auto";
3240 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
3241 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3242 if(os
.path
.exists(outfile
)):
3245 except OSError as e
:
3250 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3252 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3255 fbasename
= os
.path
.splitext(outfile
)[0];
3256 fextname
= os
.path
.splitext(outfile
)[1];
3257 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
);
3258 catver
= formatspecs
['format_ver'];
3259 fileheaderver
= str(int(catver
.replace(".", "")));
3265 inodetocatinode
= {};
3268 if(hasattr(sys
.stdin
, "buffer")):
3269 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
3271 shutil
.copyfileobj(sys
.stdin
, infile
);
3276 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
3277 infile
= download_file_from_internet_file(infile
);
3282 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
3286 if(not zipfile
.is_zipfile(infile
)):
3289 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
3290 except FileNotFoundError
:
3292 ziptest
= zipfp
.testzip();
3294 VerbosePrintOut("Bad file found!");
3295 numfiles
= int(len(zipfp
.infolist()));
3296 catfp
= AppendFileHeader(catfp
, numfiles
, checksumtype
, formatspecs
);
3297 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
3298 if(re
.findall("^[.|/]", member
.filename
)):
3299 fname
= member
.filename
;
3301 fname
= "./"+member
.filename
;
3302 zipinfo
= zipfp
.getinfo(member
.filename
);
3304 VerbosePrintOut(fname
);
3305 if(not member
.is_dir()):
3306 fpremode
= int(stat
.S_IFREG
+ 438);
3307 elif(member
.is_dir()):
3308 fpremode
= int(stat
.S_IFDIR
+ 511);
3311 if(not member
.is_dir()):
3313 elif(member
.is_dir()):
3316 fcurfid
= format(int(curfid
), 'x').lower();
3317 fcurinode
= format(int(curfid
), 'x').lower();
3318 curfid
= curfid
+ 1;
3319 fdev_minor
= format(int(0), 'x').lower();
3320 fdev_major
= format(int(0), 'x').lower();
3321 frdev_minor
= format(int(0), 'x').lower();
3322 frdev_major
= format(int(0), 'x').lower();
3324 fsize
= format(int("0"), 'x').lower();
3326 fsize
= format(int(member
.file_size
), 'x').lower();
3328 fsize
= format(int(member
.file_size
), 'x').lower();
3329 fatime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
3330 fmtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
3331 fctime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
3332 fbtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
3333 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
3334 fwinattributes
= format(int(zipinfo
.external_attr
), 'x').lower();
3335 if(not member
.is_dir()):
3336 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
3337 fchmode
= stat
.S_IMODE(fmode
);
3338 ftypemod
= stat
.S_IFMT(fmode
);
3339 elif(member
.is_dir()):
3340 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
3341 fchmode
= stat
.S_IMODE(fmode
);
3342 ftypemod
= stat
.S_IFMT(fmode
);
3343 elif(zipinfo
.create_system
==3):
3344 fwinattributes
= format(int(0), 'x').lower();
3345 fmode
= format(int(zipinfo
.external_attr
), 'x').lower();
3346 fchmode
= stat
.S_IMODE(fmode
);
3347 ftypemod
= stat
.S_IFMT(fmode
);
3349 fwinattributes
= format(int(0), 'x').lower();
3350 if(not member
.is_dir()):
3351 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
3352 fchmode
= stat
.S_IMODE(fmode
);
3353 ftypemod
= stat
.S_IFMT(fmode
);
3354 elif(member
.is_dir()):
3355 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
3356 fchmode
= stat
.S_IMODE(fmode
);
3357 ftypemod
= stat
.S_IFMT(fmode
);
3359 fcsize
= format(int(0), 'x').lower();
3361 fuid
= format(int(os
.getuid()), 'x').lower();
3362 except AttributeError:
3363 fuid
= format(int(0), 'x').lower();
3365 fuid
= format(int(0), 'x').lower();
3367 fgid
= format(int(os
.getgid()), 'x').lower();
3368 except AttributeError:
3369 fgid
= format(int(0), 'x').lower();
3371 fgid
= format(int(0), 'x').lower();
3375 userinfo
= pwd
.getpwuid(os
.getuid());
3376 funame
= userinfo
.pw_name
;
3379 except AttributeError:
3387 groupinfo
= grp
.getgrgid(os
.getgid());
3388 fgname
= groupinfo
.gr_name
;
3391 except AttributeError:
3395 fcontents
= BytesIO();
3397 fcontents
.write(zipfp
.read(member
.filename
));
3398 if(not compresswholefile
):
3399 fcontents
.seek(0, 2);
3400 ucfsize
= fcontents
.tell();
3401 fcontents
.seek(0, 0);
3402 if(compression
=="auto"):
3403 ilsize
= len(compressionlistalt
);
3406 while(ilmin
< ilsize
):
3407 cfcontents
= BytesIO();
3408 shutil
.copyfileobj(fcontents
, cfcontents
);
3409 fcontents
.seek(0, 0);
3410 cfcontents
.seek(0, 0);
3411 cfcontents
= CompressArchiveFile(cfcontents
, compressionlistalt
[ilmin
], compressionlevel
, formatspecs
);
3412 cfcontents
.seek(0, 2);
3413 ilcsize
.append(cfcontents
.tell());
3416 ilcmin
= ilcsize
.index(min(ilcsize
));
3417 compression
= compressionlistalt
[ilcmin
];
3418 fcontents
.seek(0, 0);
3419 cfcontents
= BytesIO();
3420 shutil
.copyfileobj(fcontents
, cfcontents
);
3421 cfcontents
.seek(0, 0);
3422 cfcontents
= CompressArchiveFile(cfcontents
, compression
, compressionlevel
, formatspecs
);
3423 cfcontents
.seek(0, 2);
3424 cfsize
= cfcontents
.tell();
3425 if(ucfsize
> cfsize
):
3426 fcsize
= format(int(cfsize
), 'x').lower();
3427 fcompression
= compression
;
3429 fcontents
= cfcontents
;
3430 if(fcompression
=="none"):
3432 fcontents
.seek(0, 0);
3433 ftypehex
= format(ftype
, 'x').lower();
3434 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
];
3435 catfp
= AppendFileHeaderWithContent(catfp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
);
3438 catfp
.write(AppendNullBytes([0, 0], formatspecs
['format_delimiter']).encode("UTF-8"));
3439 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3440 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
3443 os
.fsync(catfp
.fileno());
3444 except io
.UnsupportedOperation
:
3446 except AttributeError:
3448 except OSError as e
:
3452 if(hasattr(sys
.stdout
, "buffer")):
3453 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
3455 shutil
.copyfileobj(catfp
, sys
.stdout
);
3456 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3457 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
3459 upload_file_to_internet_file(catfp
, outfile
);
3467 create_alias_function("Pack", __file_format_name__
, "FromZipFile", PackArchiveFileFromZipFile
);
3469 if(not rarfile_support
):
3470 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
3473 if(rarfile_support
):
3474 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
3475 formatspecs
= FormatSpecsListToDict(formatspecs
);
3476 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3477 outfile
= RemoveWindowsPath(outfile
);
3478 checksumtype
= checksumtype
.lower();
3479 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
3480 checksumtype
="crc32";
3481 if(checksumtype
=="none"):
3483 if(not compression
or compression
=="catfile" or compression
==formatspecs
['format_lower']):
3484 compression
= "auto";
3485 if(compression
not in compressionlist
and compression
is None):
3486 compression
= "auto";
3488 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
3489 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3490 if(os
.path
.exists(outfile
)):
3493 except OSError as e
:
3498 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3500 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3503 fbasename
= os
.path
.splitext(outfile
)[0];
3504 fextname
= os
.path
.splitext(outfile
)[1];
3505 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
);
3506 catver
= formatspecs
['format_ver'];
3507 fileheaderver
= str(int(catver
.replace(".", "")));
3513 inodetocatinode
= {};
3514 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
3516 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
3518 rarfp
= rarfile
.RarFile(infile
, "r");
3519 rartest
= rarfp
.testrar();
3521 VerbosePrintOut("Bad file found!");
3522 numfiles
= int(len(rarfp
.infolist()));
3523 catfp
= AppendFileHeader(catfp
, numfiles
, checksumtype
, formatspecs
);
3526 os
.fsync(catfp
.fileno());
3527 except io
.UnsupportedOperation
:
3529 except AttributeError:
3531 except OSError as e
:
3533 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
3536 if(member
.host_os
==rarfile
.RAR_OS_UNIX
):
3539 member
.external_attr
3541 except AttributeError:
3543 elif(member
.host_os
==rarfile
.RAR_OS_WIN32
):
3546 member
.external_attr
3548 except AttributeError:
3553 if(re
.findall("^[.|/]", member
.filename
)):
3554 fname
= member
.filename
;
3556 fname
= "./"+member
.filename
;
3557 rarinfo
= rarfp
.getinfo(member
.filename
);
3559 VerbosePrintOut(fname
);
3560 if(is_unix
and member
.external_attr
!=0):
3561 fpremode
= int(member
.external_attr
);
3562 elif(member
.is_file()):
3563 fpremode
= int(stat
.S_IFREG
+ 438);
3564 elif(member
.is_symlink()):
3565 fpremode
= int(stat
.S_IFLNK
+ 438);
3566 elif(member
.is_dir()):
3567 fpremode
= int(stat
.S_IFDIR
+ 511);
3568 if(is_windows
and member
.external_attr
!=0):
3569 fwinattributes
= format(int(member
.external_attr
), 'x').lower();
3571 fwinattributes
= format(int(0), 'x').lower();
3573 fcsize
= format(int(0), 'x').lower();
3576 if(member
.is_file()):
3578 elif(member
.is_symlink()):
3580 elif(member
.is_dir()):
3584 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
3585 fcurfid
= format(int(curfid
), 'x').lower();
3586 fcurinode
= format(int(curfid
), 'x').lower();
3587 curfid
= curfid
+ 1;
3588 fdev_minor
= format(int(0), 'x').lower();
3589 fdev_major
= format(int(0), 'x').lower();
3590 frdev_minor
= format(int(0), 'x').lower();
3591 frdev_major
= format(int(0), 'x').lower();
3593 fsize
= format(int("0"), 'x').lower();
3595 fsize
= format(int(member
.file_size
), 'x').lower();
3597 fsize
= format(int(member
.file_size
), 'x').lower();
3600 fatime
= format(int(member
.atime
.timestamp()), 'x').lower();
3602 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3603 except AttributeError:
3604 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3605 fmtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3608 fctime
= format(int(member
.ctime
.timestamp()), 'x').lower();
3610 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3611 except AttributeError:
3612 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3613 fbtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
3614 if(is_unix
and member
.external_attr
!=0):
3615 fmode
= format(int(member
.external_attr
), 'x').lower();
3616 fchmode
= format(int(stat
.S_IMODE(member
.external_attr
)), 'x').lower();
3617 ftypemod
= format(int(stat
.S_IFMT(member
.external_attr
)), 'x').lower();
3618 elif(member
.is_file()):
3619 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
3620 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
3621 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
3622 elif(member
.is_symlink()):
3623 fmode
= format(int(stat
.S_IFLNK
+ 438), 'x').lower();
3624 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
3625 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
3626 elif(member
.is_dir()):
3627 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
3628 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3629 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3631 fuid
= format(int(os
.getuid()), 'x').lower();
3632 except AttributeError:
3633 fuid
= format(int(0), 'x').lower();
3635 fuid
= format(int(0), 'x').lower();
3637 fgid
= format(int(os
.getgid()), 'x').lower();
3638 except AttributeError:
3639 fgid
= format(int(0), 'x').lower();
3641 fgid
= format(int(0), 'x').lower();
3645 userinfo
= pwd
.getpwuid(os
.getuid());
3646 funame
= userinfo
.pw_name
;
3649 except AttributeError:
3657 groupinfo
= grp
.getgrgid(os
.getgid());
3658 fgname
= groupinfo
.gr_name
;
3661 except AttributeError:
3665 fcontents
= BytesIO();
3667 fcontents
.write(rarfp
.read(member
.filename
));
3668 if(not compresswholefile
):
3669 fcontents
.seek(0, 2);
3670 ucfsize
= fcontents
.tell();
3671 fcontents
.seek(0, 0);
3672 if(compression
=="auto"):
3673 ilsize
= len(compressionlistalt
);
3676 while(ilmin
< ilsize
):
3677 cfcontents
= BytesIO();
3678 shutil
.copyfileobj(fcontents
, cfcontents
);
3679 fcontents
.seek(0, 0);
3680 cfcontents
.seek(0, 0);
3681 cfcontents
= CompressArchiveFile(cfcontents
, compressionlistalt
[ilmin
], compressionlevel
, formatspecs
);
3683 cfcontents
.seek(0, 2);
3684 ilcsize
.append(cfcontents
.tell());
3688 ilcsize
.append(sys
.maxint
);
3689 except AttributeError:
3690 ilcsize
.append(sys
.maxsize
);
3692 ilcmin
= ilcsize
.index(min(ilcsize
));
3693 compression
= compressionlistalt
[ilcmin
];
3694 fcontents
.seek(0, 0);
3695 cfcontents
= BytesIO();
3696 shutil
.copyfileobj(fcontents
, cfcontents
);
3697 cfcontents
.seek(0, 0);
3698 cfcontents
= CompressArchiveFile(cfcontents
, compression
, compressionlevel
, formatspecs
);
3699 cfcontents
.seek(0, 2);
3700 cfsize
= cfcontents
.tell();
3701 if(ucfsize
> cfsize
):
3702 fcsize
= format(int(cfsize
), 'x').lower();
3703 fcompression
= compression
;
3705 fcontents
= cfcontents
;
3706 if(fcompression
=="none"):
3708 fcontents
.seek(0, 0);
3709 ftypehex
= format(ftype
, 'x').lower();
3710 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
];
3711 catfp
= AppendFileHeaderWithContent(catfp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
);
3714 catfp
.write(AppendNullBytes([0, 0], formatspecs
['format_delimiter']).encode("UTF-8"));
3715 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3716 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
)
3719 os
.fsync(catfp
.fileno());
3720 except io
.UnsupportedOperation
:
3722 except AttributeError:
3724 except OSError as e
:
3728 if(hasattr(sys
.stdout
, "buffer")):
3729 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
3731 shutil
.copyfileobj(catfp
, sys
.stdout
);
3732 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3733 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
3735 upload_file_to_internet_file(catfp
, outfile
);
3743 create_alias_function("Pack", __file_format_name__
, "FromRarFile", PackArchiveFileFromRarFile
);
3745 if(not py7zr_support
):
3746 def PackArchiveFileFromSevenZipFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
3750 def PackArchiveFileFromSevenZipFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
3751 formatspecs
= FormatSpecsListToDict(formatspecs
);
3752 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3753 outfile
= RemoveWindowsPath(outfile
);
3754 checksumtype
= checksumtype
.lower();
3755 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
3756 checksumtype
="crc32";
3757 if(checksumtype
=="none"):
3759 if(not compression
or compression
=="catfile" or compression
==formatspecs
['format_lower']):
3760 compression
= "auto";
3761 if(compression
not in compressionlist
and compression
is None):
3762 compression
= "auto";
3764 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
3765 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
3766 if(os
.path
.exists(outfile
)):
3769 except OSError as e
:
3774 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3776 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3779 fbasename
= os
.path
.splitext(outfile
)[0];
3780 fextname
= os
.path
.splitext(outfile
)[1];
3781 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
);
3782 catver
= formatspecs
['format_ver'];
3783 fileheaderver
= str(int(catver
.replace(".", "")));
3789 inodetocatinode
= {};
3790 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
3792 szpfp
= py7zr
.SevenZipFile(infile
, mode
="r");
3793 file_content
= szpfp
.readall();
3794 #sztest = szpfp.testzip();
3795 sztestalt
= szpfp
.test();
3797 VerbosePrintOut("Bad file found!");
3798 numfiles
= int(len(szpfp
.list()));
3799 AppendFileHeader(catfp
, numfiles
, checksumtype
, formatspecs
);
3800 for member
in sorted(szpfp
.list(), key
=lambda x
: x
.filename
):
3801 if(re
.findall("^[.|/]", member
.filename
)):
3802 fname
= member
.filename
;
3804 fname
= "./"+member
.filename
;
3806 VerbosePrintOut(fname
);
3807 if(not member
.is_directory
):
3808 fpremode
= int(stat
.S_IFREG
+ 438);
3809 elif(member
.is_directory
):
3810 fpremode
= int(stat
.S_IFDIR
+ 511);
3811 fwinattributes
= format(int(0), 'x').lower();
3813 fcsize
= format(int(0), 'x').lower();
3816 if(member
.is_directory
):
3821 fcurfid
= format(int(curfid
), 'x').lower();
3822 fcurinode
= format(int(curfid
), 'x').lower();
3823 curfid
= curfid
+ 1;
3824 fdev_minor
= format(int(0), 'x').lower();
3825 fdev_major
= format(int(0), 'x').lower();
3826 frdev_minor
= format(int(0), 'x').lower();
3827 frdev_major
= format(int(0), 'x').lower();
3829 fsize
= format(int("0"), 'x').lower();
3830 fatime
= format(int(member
.creationtime
.timestamp()), 'x').lower();
3831 fmtime
= format(int(member
.creationtime
.timestamp()), 'x').lower();
3832 fctime
= format(int(member
.creationtime
.timestamp()), 'x').lower();
3833 fbtime
= format(int(member
.creationtime
.timestamp()), 'x').lower();
3834 if(member
.is_directory
):
3835 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
3836 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3837 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
3839 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
3840 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
3841 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
3843 fuid
= format(int(os
.getuid()), 'x').lower();
3844 except AttributeError:
3845 fuid
= format(int(0), 'x').lower();
3847 fuid
= format(int(0), 'x').lower();
3849 fgid
= format(int(os
.getgid()), 'x').lower();
3850 except AttributeError:
3851 fgid
= format(int(0), 'x').lower();
3853 fgid
= format(int(0), 'x').lower();
3857 userinfo
= pwd
.getpwuid(os
.getuid());
3858 funame
= userinfo
.pw_name
;
3861 except AttributeError:
3869 groupinfo
= grp
.getgrgid(os
.getgid());
3870 fgname
= groupinfo
.gr_name
;
3873 except AttributeError:
3877 fcontents
= BytesIO();
3879 fcontents
.write(file_content
[member
.filename
].read());
3880 fsize
= format(fcontents
.tell(), 'x').lower();
3881 file_content
[member
.filename
].close();
3882 if(not compresswholefile
):
3883 fcontents
.seek(0, 2);
3884 ucfsize
= fcontents
.tell();
3885 fcontents
.seek(0, 0);
3886 if(compression
=="auto"):
3887 ilsize
= len(compressionlistalt
);
3890 while(ilmin
< ilsize
):
3891 cfcontents
= BytesIO();
3892 shutil
.copyfileobj(fcontents
, cfcontents
);
3893 fcontents
.seek(0, 0);
3894 cfcontents
.seek(0, 0);
3895 cfcontents
= CompressArchiveFile(cfcontents
, compressionlistalt
[ilmin
], compressionlevel
, formatspecs
);
3897 cfcontents
.seek(0, 2);
3898 ilcsize
.append(cfcontents
.tell());
3902 ilcsize
.append(sys
.maxint
);
3903 except AttributeError:
3904 ilcsize
.append(sys
.maxsize
);
3906 ilcmin
= ilcsize
.index(min(ilcsize
));
3907 compression
= compressionlistalt
[ilcmin
];
3908 fcontents
.seek(0, 0);
3909 cfcontents
= BytesIO();
3910 shutil
.copyfileobj(fcontents
, cfcontents
);
3911 cfcontents
.seek(0, 0);
3912 cfcontents
= CompressArchiveFile(cfcontents
, compression
, compressionlevel
, formatspecs
);
3913 cfcontents
.seek(0, 2);
3914 cfsize
= cfcontents
.tell();
3915 if(ucfsize
> cfsize
):
3916 fcsize
= format(int(cfsize
), 'x').lower();
3917 fcompression
= compression
;
3919 fcontents
= cfcontents
;
3920 if(fcompression
=="none"):
3922 fcontents
.seek(0, 0);
3923 ftypehex
= format(ftype
, 'x').lower();
3924 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
];
3925 catfp
= AppendFileHeaderWithContent(catfp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
);
3928 catfp
.write(AppendNullBytes([0, 0], formatspecs
['format_delimiter']).encode("UTF-8"));
3929 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
3930 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
)
3933 os
.fsync(catfp
.fileno());
3934 except io
.UnsupportedOperation
:
3936 except AttributeError:
3938 except OSError as e
:
3942 if(hasattr(sys
.stdout
, "buffer")):
3943 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
3945 shutil
.copyfileobj(catfp
, sys
.stdout
);
3946 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
3947 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
3949 upload_file_to_internet_file(catfp
, outfile
);
3957 create_alias_function("Pack", __file_format_name__
, "FromSevenZipFile", PackArchiveFileFromSevenZipFile
);
3959 def PackArchiveFileFromInFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
3960 formatspecs
= FormatSpecsListToDict(formatspecs
);
3961 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
3963 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
3964 if(checkcompressfile
=="tarfile"):
3965 return PackArchiveFileFromTarFile(infile
, outfile
, compression
, compresswholefile
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
3966 elif(checkcompressfile
=="zipfile"):
3967 return PackArchiveFileFromZipFile(infile
, outfile
, compression
, compresswholefile
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
3968 elif(checkcompressfile
=="catfile"):
3969 return RePackArchiveFile(infile
, outfile
, compression
, compresswholefile
, compressionlevel
, False, 0, 0, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
3970 elif(rarfile_support
and checkcompressfile
=="rarfile"):
3971 return PackArchiveFileFromRarFile(infile
, outfile
, compression
, compresswholefile
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
3972 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
3973 return PackArchiveFileFromSevenZipFile(infile
, outfile
, compression
, compresswholefile
, compressionlevel
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
3978 create_alias_function("Pack", __file_format_name__
, "FromInFile", PackArchiveFileFromInFile
);
3980 def ArchiveFileSeekToFileNum(infile
, seekto
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
3981 formatspecs
= FormatSpecsListToDict(formatspecs
);
3982 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3985 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3986 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
3987 if(checkcompressfile
=="tarfile"):
3988 return TarFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3989 if(checkcompressfile
=="zipfile"):
3990 return ZipFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3991 if(rarfile_support
and checkcompressfile
=="rarfile"):
3992 return RarFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3993 if(py7zr_support
and checkcompressfile
=="7zipfile"):
3994 return SevenZipFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
3995 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
['format_lower']):
4002 if(hasattr(sys
.stdin
, "buffer")):
4003 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
4005 shutil
.copyfileobj(sys
.stdin
, catfp
);
4007 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4011 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4012 catfp
= download_file_from_internet_file(infile
);
4014 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4019 infile
= RemoveWindowsPath(infile
);
4020 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
4021 if(checkcompressfile
=="tarfile"):
4022 return TarFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4023 if(checkcompressfile
=="zipfile"):
4024 return ZipFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4025 if(rarfile_support
and checkcompressfile
=="rarfile"):
4026 return RarFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4027 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4028 return SevenZipFileToArray(infile
, seekto
, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4029 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
['format_lower']):
4031 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
4032 if(not compresscheck
):
4033 fextname
= os
.path
.splitext(infile
)[1];
4034 if(fextname
==".gz"):
4035 compresscheck
= "gzip";
4036 elif(fextname
==".bz2"):
4037 compresscheck
= "bzip2";
4038 elif(fextname
==".zst"):
4039 compresscheck
= "zstd";
4040 elif(fextname
==".lz4" or fextname
==".clz4"):
4041 compresscheck
= "lz4";
4042 elif(fextname
==".lzo" or fextname
==".lzop"):
4043 compresscheck
= "lzo";
4044 elif(fextname
==".lzma" or fextname
==".xz"):
4045 compresscheck
= "lzma";
4048 if(not compresscheck
):
4050 catfp
= UncompressFile(infile
, formatspecs
, "rb");
4055 SeekToEndOfFile(catfp);
4057 SeekToEndOfFile(catfp);
4058 CatSize = catfp.tell();
4059 CatSizeEnd = CatSize;
4067 curloc
= catfp
.tell();
4070 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
['format_delimiter']);
4072 catfp
.seek(curloc
, 0);
4073 catstring
= catheader
[0];
4074 catversion
= re
.findall(r
"([\d]+)$", catstring
);
4075 fprenumfiles
= catheader
[1];
4076 fnumfiles
= int(fprenumfiles
, 16);
4077 fprechecksumtype
= catheader
[2];
4078 fprechecksum
= catheader
[3];
4079 fileheader
= AppendNullByte(catstring
, formatspecs
['format_delimiter']);
4080 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4081 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
['format_delimiter']);
4082 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
4083 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter']);
4084 fheadtell
= len(fileheader
);
4085 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
4086 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
4087 VerbosePrintOut("'" + str(fprechecksum
) + "' != " + "'" + str(catfileheadercshex
) + "'");
4089 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4090 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
4091 if(seekto
>=fnumfiles
):
4092 seekto
= fnumfiles
- 1;
4098 prefhstart
= catfp
.tell();
4099 if(formatspecs
['new_style']):
4100 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
['format_delimiter']);
4102 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
['format_delimiter']);
4103 if(len(preheaderdata
)==0):
4105 prefheadsize
= int(preheaderdata
[0], 16);
4106 prefnumfields
= int(preheaderdata
[1], 16);
4107 preftype
= int(preheaderdata
[2], 16);
4108 if(re
.findall("^[.|/]", preheaderdata
[3])):
4109 prefname
= preheaderdata
[3];
4111 prefname
= "./"+preheaderdata
[3];
4112 prefbasedir
= os
.path
.dirname(prefname
);
4113 preflinkname
= preheaderdata
[4];
4114 prefsize
= int(preheaderdata
[5], 16);
4115 prefatime
= int(preheaderdata
[6], 16);
4116 prefmtime
= int(preheaderdata
[7], 16);
4117 prefctime
= int(preheaderdata
[8], 16);
4118 prefbtime
= int(preheaderdata
[9], 16);
4119 prefmode
= int(preheaderdata
[10], 16);
4120 prefchmode
= stat
.S_IMODE(prefmode
);
4121 preftypemod
= stat
.S_IFMT(prefmode
);
4122 prefwinattributes
= int(preheaderdata
[11], 16);
4123 prefcompression
= preheaderdata
[12];
4124 prefcsize
= int(preheaderdata
[13], 16);
4125 prefuid
= int(preheaderdata
[14], 16);
4126 prefuname
= preheaderdata
[15];
4127 prefgid
= int(preheaderdata
[16], 16);
4128 prefgname
= preheaderdata
[17];
4129 fid
= int(preheaderdata
[18], 16);
4130 finode
= int(preheaderdata
[19], 16);
4131 flinkcount
= int(preheaderdata
[20], 16);
4132 prefdev_minor
= int(preheaderdata
[21], 16);
4133 prefdev_major
= int(preheaderdata
[22], 16);
4134 prefrdev_minor
= int(preheaderdata
[23], 16);
4135 prefrdev_major
= int(preheaderdata
[24], 16);
4136 prefextrasize
= int(preheaderdata
[25], 16);
4137 prefextrafields
= int(preheaderdata
[26], 16);
4138 extrafieldslist
= [];
4140 extraend
= extrastart
+ prefextrafields
;
4141 extrafieldslist
= [];
4142 if(extrastart
<extraend
):
4143 extrafieldslist
.append(preheaderdata
[extrastart
]);
4144 extrastart
= extrastart
+ 1;
4145 prefchecksumtype
= preheaderdata
[extrastart
].lower();
4146 prefcs
= preheaderdata
[extrastart
+ 1].lower();
4147 prefccs
= preheaderdata
[extrastart
+ 2].lower();
4149 hcmax
= len(preheaderdata
) - 2;
4152 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
['format_delimiter']);
4154 prenewfcs
= GetFileChecksum(hout
, preheaderdata
[-3].lower(), True, formatspecs
);
4155 if(prefcs
!=prenewfcs
and not skipchecksum
):
4156 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
4157 VerbosePrintOut("'" + str(prefcs
) + "' != " + "'" + str(prenewfcs
) + "'");
4159 valid_archive
= False;
4160 invalid_archive
= True;
4161 prefhend
= catfp
.tell() - 1;
4162 prefcontentstart
= catfp
.tell();
4164 pyhascontents
= False;
4166 if(prefcompression
):
4167 prefcontents
= catfp
.read(prefsize
);
4169 prefcontents
= catfp
.read(prefcsize
);
4170 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
4171 pyhascontents
= True;
4172 if(prefccs
!=prenewfccs
and not skipchecksum
):
4173 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
4174 VerbosePrintOut("'" + str(prefccs
) + "' != " + "'" + str(prenewfccs
) + "'");
4178 catfp
.seek(seekstart
, 0);
4180 catfheadsize
= int(preheaderdata
[0], 16);
4181 catfnumfields
= int(preheaderdata
[1], 16);
4182 catftype
= int(preheaderdata
[2], 16);
4183 if(re
.findall("^[.|/]", preheaderdata
[3])):
4184 catfname
= preheaderdata
[3];
4186 catfname
= "./"+preheaderdata
[3];
4187 catflinkname
= preheaderdata
[4];
4188 catfsize
= int(preheaderdata
[5], 16);
4189 catfbasedir
= os
.path
.dirname(catfname
);
4190 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
4192 catlist
.update({'catfp': catfp
});
4197 create_alias_function("", __file_format_name__
, "SeekToFileNum", ArchiveFileSeekToFileNum
);
4199 def ArchiveFileSeekToFileName(infile
, seekfile
=None, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
4200 formatspecs
= FormatSpecsListToDict(formatspecs
);
4201 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4204 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4205 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
4206 if(checkcompressfile
=="tarfile"):
4207 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4208 if(checkcompressfile
=="zipfile"):
4209 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4210 if(rarfile_support
and checkcompressfile
=="rarfile"):
4211 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4212 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4213 return SevenZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4214 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
['format_lower']):
4221 if(hasattr(sys
.stdin
, "buffer")):
4222 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
4224 shutil
.copyfileobj(sys
.stdin
, catfp
);
4226 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4230 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4231 catfp
= download_file_from_internet_file(infile
);
4232 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4238 infile
= RemoveWindowsPath(infile
);
4239 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
4240 if(checkcompressfile
=="tarfile"):
4241 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4242 if(checkcompressfile
=="zipfile"):
4243 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4244 if(rarfile_support
and checkcompressfile
=="rarfile"):
4245 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4246 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4247 return SevenZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
4248 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
['format_lower']):
4250 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
4251 if(not compresscheck
):
4252 fextname
= os
.path
.splitext(infile
)[1];
4253 if(fextname
==".gz"):
4254 compresscheck
= "gzip";
4255 elif(fextname
==".bz2"):
4256 compresscheck
= "bzip2";
4257 elif(fextname
==".zst"):
4258 compresscheck
= "zstd";
4259 elif(fextname
==".lz4" or fextname
==".clz4"):
4260 compresscheck
= "lz4";
4261 elif(fextname
==".lzo" or fextname
==".lzop"):
4262 compresscheck
= "lzo";
4263 elif(fextname
==".lzma" or fextname
==".xz"):
4264 compresscheck
= "lzma";
4267 if(not compresscheck
):
4269 catfp
= UncompressFile(infile
, formatspecs
, "rb");
4274 SeekToEndOfFile(catfp);
4276 SeekToEndOfFile(catfp);
4277 CatSize = catfp.tell();
4278 CatSizeEnd = CatSize;
4286 curloc
= catfp
.tell();
4289 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
['format_delimiter']);
4291 catfp
.seek(curloc
, 0);
4292 catstring
= catheader
[0];
4293 catversion
= re
.findall(r
"([\d]+)$", catstring
);
4294 fprenumfiles
= catheader
[1];
4295 fnumfiles
= int(fprenumfiles
, 16);
4296 fprechecksumtype
= catheader
[2];
4297 fprechecksum
= catheader
[3];
4298 fileheader
= AppendNullByte(catstring
, formatspecs
['format_delimiter']);
4299 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4300 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
['format_delimiter']);
4301 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
4302 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter']);
4303 fheadtell
= len(fileheader
);
4304 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
4305 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
4306 VerbosePrintOut("'" + str(fprechecksum
) + "' != " + "'" + str(catfileheadercshex
) + "'");
4308 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4309 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
4310 seekto
= fnumfiles
- 1
4315 prefhstart
= catfp
.tell();
4316 if(formatspecs
['new_style']):
4317 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
['format_delimiter']);
4319 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
['format_delimiter']);
4320 if(len(preheaderdata
)==0):
4322 prefheadsize
= int(preheaderdata
[0], 16);
4323 prefnumfields
= int(preheaderdata
[1], 16);
4324 preftype
= int(preheaderdata
[2], 16);
4325 if(re
.findall("^[.|/]", preheaderdata
[3])):
4326 prefname
= preheaderdata
[3];
4328 prefname
= "./"+preheaderdata
[3];
4329 prefbasedir
= os
.path
.dirname(prefname
);
4330 preflinkname
= preheaderdata
[4];
4331 prefsize
= int(preheaderdata
[5], 16);
4332 prefatime
= int(preheaderdata
[6], 16);
4333 prefmtime
= int(preheaderdata
[7], 16);
4334 prefctime
= int(preheaderdata
[8], 16);
4335 prefbtime
= int(preheaderdata
[9], 16);
4336 prefmode
= int(preheaderdata
[10], 16);
4337 prefchmode
= stat
.S_IMODE(prefmode
);
4338 preftypemod
= stat
.S_IFMT(prefmode
);
4339 prefwinattributes
= int(preheaderdata
[11], 16);
4340 prefcompression
= preheaderdata
[12];
4341 prefcsize
= int(preheaderdata
[13], 16);
4342 prefuid
= int(preheaderdata
[14], 16);
4343 prefuname
= preheaderdata
[15];
4344 prefgid
= int(preheaderdata
[16], 16);
4345 prefgname
= preheaderdata
[17];
4346 fid
= int(preheaderdata
[18], 16);
4347 finode
= int(preheaderdata
[19], 16);
4348 flinkcount
= int(preheaderdata
[20], 16);
4349 prefdev_minor
= int(preheaderdata
[21], 16);
4350 prefdev_major
= int(preheaderdata
[22], 16);
4351 prefrdev_minor
= int(preheaderdata
[23], 16);
4352 prefrdev_major
= int(preheaderdata
[24], 16);
4353 prefextrasize
= int(preheaderdata
[25], 16);
4354 prefextrafields
= int(preheaderdata
[26], 16);
4355 extrafieldslist
= [];
4357 extraend
= extrastart
+ prefextrafields
;
4358 extrafieldslist
= [];
4359 if(extrastart
<extraend
):
4360 extrafieldslist
.append(preheaderdata
[extrastart
]);
4361 extrastart
= extrastart
+ 1;
4362 prefchecksumtype
= preheaderdata
[extrastart
].lower();
4363 prefcs
= preheaderdata
[extrastart
+ 1].lower();
4364 prefccs
= preheaderdata
[extrastart
+ 2].lower();
4366 hcmax
= len(preheaderdata
) - 2;
4369 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
['format_delimiter']);
4371 prenewfcs
= GetFileChecksum(hout
, preheaderdata
[-3].lower(), True, formatspecs
);
4372 if(prefcs
!=prenewfcs
and not skipchecksum
):
4373 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
4374 VerbosePrintOut("'" + str(prefcs
) + "' != " + "'" + str(prenewfcs
) + "'");
4376 valid_archive
= False;
4377 invalid_archive
= True;
4378 prefhend
= catfp
.tell() - 1;
4379 prefcontentstart
= catfp
.tell();
4381 pyhascontents
= False;
4383 if(prefcompression
):
4384 prefcontents
= catfp
.read(prefsize
);
4386 prefcontents
= catfp
.read(prefcsize
);
4387 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
4388 pyhascontents
= True;
4389 if(prefccs
!=prenewfccs
and not skipchecksum
):
4390 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
4391 VerbosePrintOut("'" + str(prefccs
) + "' != " + "'" + str(prenewfccs
) + "'");
4396 prefname
= preheaderdata
[2];
4397 if(re
.findall("^[.|/]", preheaderdata
[2])):
4398 prefname
= preheaderdata
[2];
4400 prefname
= "./"+preheaderdata
[2];
4401 if(prefname
==seekfile
):
4404 catfp
.seek(seekstart
, 0);
4406 catfheadsize
= int(preheaderdata
[0], 16);
4407 catfnumfields
= int(preheaderdata
[1], 16);
4408 catftype
= int(preheaderdata
[2], 16);
4409 if(re
.findall("^[.|/]", preheaderdata
[3])):
4410 catfname
= preheaderdata
[3];
4412 catfname
= "./"+preheaderdata
[3];
4413 catflinkname
= preheaderdata
[4];
4414 catfsize
= int(preheaderdata
[5], 16);
4415 catfbasedir
= os
.path
.dirname(catfname
);
4417 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
4421 catlist
.update({'catfp': catfp
});
4426 create_alias_function("", __file_format_name__
, "SeekToFileName", ArchiveFileSeekToFileName
);
4428 def ArchiveFileValidate(infile
, formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
4429 formatspecs
= FormatSpecsListToDict(formatspecs
);
4431 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
4432 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4435 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4436 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
4437 if(checkcompressfile
=="tarfile"):
4438 return TarFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4439 if(checkcompressfile
=="zipfile"):
4440 return ZipFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4441 if(rarfile_support
and checkcompressfile
=="rarfile"):
4442 return RarFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4443 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4444 return SevenZipFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4445 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
['format_lower']):
4452 if(hasattr(sys
.stdin
, "buffer")):
4453 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
4455 shutil
.copyfileobj(sys
.stdin
, catfp
);
4457 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4461 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4462 catfp
= download_file_from_internet_file(infile
);
4463 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4469 infile
= RemoveWindowsPath(infile
);
4470 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
4471 if(checkcompressfile
=="tarfile"):
4472 return TarFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4473 if(checkcompressfile
=="zipfile"):
4474 return ZipFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4475 if(rarfile_support
and checkcompressfile
=="rarfile"):
4476 return RarFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4477 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4478 return SevenZipFileToArray(infile
, 0, 0, False, False, formatspecs
, returnfp
);
4479 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
['format_lower']):
4481 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
4482 if(not compresscheck
):
4483 fextname
= os
.path
.splitext(infile
)[1];
4484 if(fextname
==".gz"):
4485 compresscheck
= "gzip";
4486 elif(fextname
==".bz2"):
4487 compresscheck
= "bzip2";
4488 elif(fextname
==".zst"):
4489 compresscheck
= "zstd";
4490 elif(fextname
==".lz4" or fextname
==".clz4"):
4491 compresscheck
= "lz4";
4492 elif(fextname
==".lzo" or fextname
==".lzop"):
4493 compresscheck
= "lzo";
4494 elif(fextname
==".lzma" or fextname
==".xz"):
4495 compresscheck
= "lzma";
4498 if(not compresscheck
):
4500 catfp
= UncompressFile(infile
, formatspecs
, "rb");
4505 SeekToEndOfFile(catfp);
4507 SeekToEndOfFile(catfp);
4508 CatSize = catfp.tell();
4509 CatSizeEnd = CatSize;
4517 curloc
= catfp
.tell();
4520 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
['format_delimiter']);
4522 catfp
.seek(curloc
, 0);
4523 catstring
= catheader
[0];
4524 catversion
= re
.findall(r
"([\d]+)$", catstring
);
4525 fprenumfiles
= catheader
[1];
4526 fnumfiles
= int(fprenumfiles
, 16);
4527 fprechecksumtype
= catheader
[2];
4528 fprechecksum
= catheader
[3];
4530 fileheader
= AppendNullByte(catstring
, formatspecs
['format_delimiter']);
4531 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4532 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
['format_delimiter']);
4533 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
4534 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter']);
4535 valid_archive
= True;
4536 invalid_archive
= False;
4538 VerbosePrintOut(infile
);
4539 VerbosePrintOut("Number of Records " + str(fnumfiles
));
4540 if(fprechecksum
==catfileheadercshex
):
4542 VerbosePrintOut("File Header Checksum Passed at offset " + str(0));
4543 VerbosePrintOut("'" + str(fprechecksum
) + "' == " + "'" + str(catfileheadercshex
) + "'");
4546 VerbosePrintOut("File Header Checksum Failed at offset " + str(0));
4547 VerbosePrintOut("'" + str(fprechecksum
) + "' != " + "'" + str(catfileheadercshex
) + "'");
4548 valid_archive
= False;
4549 invalid_archive
= True;
4551 VerbosePrintOut("");
4552 while(il
<fnumfiles
):
4553 catfhstart
= catfp
.tell();
4554 if(formatspecs
['new_style']):
4555 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
['format_delimiter']);
4557 catheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
['format_delimiter']);
4558 if(len(catheaderdata
)==0):
4560 catfheadsize
= int(catheaderdata
[0], 16);
4561 catfnumfields
= int(catheaderdata
[1], 16);
4562 catftype
= int(catheaderdata
[2], 16);
4563 if(re
.findall("^[.|/]", catheaderdata
[3])):
4564 catfname
= catheaderdata
[3];
4566 catfname
= "./"+catheaderdata
[3];
4567 catfbasedir
= os
.path
.dirname(catfname
);
4568 catflinkname
= catheaderdata
[4];
4569 catfsize
= int(catheaderdata
[5], 16);
4570 catfatime
= int(catheaderdata
[6], 16);
4571 catfmtime
= int(catheaderdata
[7], 16);
4572 catfctime
= int(catheaderdata
[8], 16);
4573 catfbtime
= int(catheaderdata
[9], 16);
4574 catfmode
= int(catheaderdata
[10], 16);
4575 catfchmode
= stat
.S_IMODE(catfmode
);
4576 catftypemod
= stat
.S_IFMT(catfmode
);
4577 catfwinattributes
= int(catheaderdata
[11], 16);
4578 catfcompression
= catheaderdata
[12];
4579 catfcsize
= int(catheaderdata
[13], 16);
4580 catfuid
= int(catheaderdata
[14], 16);
4581 catfuname
= catheaderdata
[15];
4582 catfgid
= int(catheaderdata
[16], 16);
4583 catfgname
= catheaderdata
[17];
4584 fid
= int(catheaderdata
[18], 16);
4585 finode
= int(catheaderdata
[19], 16);
4586 flinkcount
= int(catheaderdata
[20], 16);
4587 catfdev_minor
= int(catheaderdata
[21], 16);
4588 catfdev_major
= int(catheaderdata
[22], 16);
4589 catfrdev_minor
= int(catheaderdata
[23], 16);
4590 catfrdev_major
= int(catheaderdata
[24], 16);
4591 catfextrasize
= int(catheaderdata
[25], 16);
4592 catfextrafields
= int(catheaderdata
[26], 16);
4593 extrafieldslist
= [];
4595 extraend
= extrastart
+ catfextrafields
;
4596 extrafieldslist
= [];
4597 if(extrastart
<extraend
):
4598 extrafieldslist
.append(catheaderdata
[extrastart
]);
4599 extrastart
= extrastart
+ 1;
4600 catfchecksumtype
= catheaderdata
[extrastart
].lower();
4601 catfcs
= catheaderdata
[extrastart
+ 1].lower();
4602 catfccs
= catheaderdata
[extrastart
+ 2].lower();
4604 hcmax
= len(catheaderdata
) - 2;
4607 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
['format_delimiter']);
4609 catnewfcs
= GetFileChecksum(hout
, catheaderdata
[-3].lower(), True, formatspecs
);
4611 VerbosePrintOut(catfname
);
4612 VerbosePrintOut("Record Number " + str(il
) + "; File ID " + str(fid
) + "; iNode Number " + str(finode
));
4613 if(catfcs
==catnewfcs
):
4615 VerbosePrintOut("File Header Checksum Passed at offset " + str(catfhstart
));
4616 VerbosePrintOut("'" + str(catfcs
) + "' == " + "'" + str(catnewfcs
) + "'");
4619 VerbosePrintOut("File Header Checksum Failed at offset " + str(catfhstart
));
4620 VerbosePrintOut("'" + str(catfcs
) + "' != " + "'" + str(catnewfcs
) + "'");
4621 valid_archive
= False;
4622 invalid_archive
= True;
4623 catfhend
= catfp
.tell() - 1;
4624 catfcontentstart
= catfp
.tell();
4626 pyhascontents
= False;
4628 if(catfcompression
=="none" or catfcompression
=="" or catfcompression
=="auto"):
4629 catfcontents
= catfp
.read(catfsize
);
4631 catfcontents
= catfp
.read(catfcsize
);
4632 catnewfccs
= GetFileChecksum(catfcontents
, catheaderdata
[-3].lower(), False, formatspecs
);
4633 pyhascontents
= True;
4634 if(catfccs
==catnewfccs
):
4636 VerbosePrintOut("File Content Checksum Passed at offset " + str(catfcontentstart
));
4637 VerbosePrintOut("'" + str(catfccs
) + "' == " + "'" + str(catnewfccs
) + "'");
4640 VerbosePrintOut("File Content Checksum Failed at offset " + str(catfcontentstart
));
4641 VerbosePrintOut("'" + str(catfccs
) + "' != " + "'" + str(catnewfccs
) + "'");
4642 valid_archive
= False;
4643 invalid_archive
= True;
4645 VerbosePrintOut("");
4658 create_alias_function("", __file_format_name__
, "Validate", ArchiveFileValidate
);
4660 def ArchiveFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
4661 formatspecs
= FormatSpecsListToDict(formatspecs
);
4662 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4665 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4666 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
, True);
4667 if(checkcompressfile
=="tarfile"):
4668 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4669 if(checkcompressfile
=="zipfile"):
4670 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4671 if(rarfile_support
and checkcompressfile
=="rarfile"):
4672 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4673 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4674 return SevenZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4675 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
['format_lower']):
4682 if(hasattr(sys
.stdin
, "buffer")):
4683 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
4685 shutil
.copyfileobj(sys
.stdin
, catfp
);
4687 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4691 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
4692 catfp
= download_file_from_internet_file(infile
);
4693 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
4699 infile
= RemoveWindowsPath(infile
);
4700 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
4701 if(checkcompressfile
=="tarfile"):
4702 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4703 if(checkcompressfile
=="zipfile"):
4704 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4705 if(rarfile_support
and checkcompressfile
=="rarfile"):
4706 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4707 if(py7zr_support
and checkcompressfile
=="7zipfile"):
4708 return SevenZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4709 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
['format_lower']):
4711 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
4712 if(not compresscheck
):
4713 fextname
= os
.path
.splitext(infile
)[1];
4714 if(fextname
==".gz"):
4715 compresscheck
= "gzip";
4716 elif(fextname
==".bz2"):
4717 compresscheck
= "bzip2";
4718 elif(fextname
==".zst"):
4719 compresscheck
= "zstd";
4720 elif(fextname
==".lz4" or fextname
==".clz4"):
4721 compresscheck
= "lz4";
4722 elif(fextname
==".lzo" or fextname
==".lzop"):
4723 compresscheck
= "lzo";
4724 elif(fextname
==".lzma" or fextname
==".xz"):
4725 compresscheck
= "lzma";
4728 if(not compresscheck
):
4730 catfp
= UncompressFile(infile
, formatspecs
, "rb");
4735 SeekToEndOfFile(catfp);
4737 SeekToEndOfFile(catfp);
4738 CatSize = catfp.tell();
4739 CatSizeEnd = CatSize;
4747 curloc
= catfp
.tell();
4750 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
['format_delimiter']);
4752 catfp
.seek(curloc
, 0);
4753 catstring
= catheader
[0];
4754 catversion
= re
.findall(r
"([\d]+)$", catstring
);
4755 fprenumfiles
= catheader
[1];
4756 fnumfiles
= int(fprenumfiles
, 16);
4757 fprechecksumtype
= catheader
[2];
4758 fprechecksum
= catheader
[3];
4759 fileheader
= AppendNullByte(catstring
, formatspecs
['format_delimiter']);
4760 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4761 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
['format_delimiter']);
4762 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
4763 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter']);
4764 fheadtell
= len(fileheader
);
4765 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
4766 VerbosePrintOut("File Header Checksum Error with file at offset " + str(0));
4767 VerbosePrintOut("'" + str(fprechecksum
) + "' != " + "'" + str(catfileheadercshex
) + "'");
4769 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4770 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
4771 if(seekstart
<0 and seekstart
>fnumfiles
):
4773 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
4774 seekend
= fnumfiles
;
4775 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
4776 seekend
= fnumfiles
- abs(seekend
);
4779 while(il
< seekstart
):
4780 prefhstart
= catfp
.tell();
4781 if(formatspecs
['new_style']):
4782 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
['format_delimiter']);
4784 preheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
['format_delimiter']);
4785 if(len(preheaderdata
)==0):
4787 prefheadsize
= int(preheaderdata
[0], 16);
4788 prefnumfields
= int(preheaderdata
[1], 16);
4789 if(re
.findall("^[.|/]", preheaderdata
[3])):
4790 prefname
= preheaderdata
[3];
4792 prefname
= "./"+preheaderdata
[3];
4793 prefsize
= int(preheaderdata
[5], 16);
4794 prefcompression
= preheaderdata
[12];
4795 prefcsize
= int(preheaderdata
[13], 16);
4796 prefextrasize
= int(preheaderdata
[24], 16);
4797 prefextrafields
= int(preheaderdata
[25], 16);
4798 extrafieldslist
= [];
4800 extraend
= extrastart
+ prefextrafields
;
4801 extrafieldslist
= [];
4802 if(extrastart
<extraend
):
4803 extrafieldslist
.append(preheaderdata
[extrastart
]);
4804 extrastart
= extrastart
+ 1;
4805 prefchecksumtype
= preheaderdata
[extrastart
].lower();
4806 prefcs
= preheaderdata
[extrastart
+ 1].lower();
4807 prefccs
= preheaderdata
[extrastart
+ 2].lower();
4809 hcmax
= len(preheaderdata
) - 2;
4812 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
['format_delimiter']);
4814 prenewfcs
= GetFileChecksum(hout
, preheaderdata
[-3].lower(), True, formatspecs
);
4815 if(prefcs
!=prenewfcs
and not skipchecksum
):
4816 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
4817 VerbosePrintOut("'" + str(prefcs
) + "' != " + "'" + str(prenewfcs
) + "'");
4819 valid_archive
= False;
4820 invalid_archive
= True;
4821 prefhend
= catfp
.tell() - 1;
4822 prefcontentstart
= catfp
.tell();
4824 pyhascontents
= False;
4826 if(prefcompression
=="none" or prefcompression
=="" or prefcompression
=="auto"):
4827 prefcontents
= catfp
.read(prefsize
);
4829 prefcontents
= catfp
.read(prefcsize
);
4830 prenewfccs
= GetFileChecksum(prefcontents
, preheaderdata
[-3].lower(), False, formatspecs
);
4831 pyhascontents
= True;
4832 if(prefccs
!=prenewfccs
and not skipchecksum
):
4833 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
4834 VerbosePrintOut("'" + str(prefccs
) + "' != " + "'" + str(prenewfccs
) + "'");
4838 fileidnum
= seekstart
;
4840 while(fileidnum
<seekend
):
4841 catfhstart
= catfp
.tell();
4842 if(formatspecs
['new_style']):
4843 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
['format_delimiter']);
4845 catheaderdata
= ReadFileHeaderDataWoSize(catfp
, formatspecs
['format_delimiter']);
4846 if(len(catheaderdata
)==0):
4848 catfheadsize
= int(catheaderdata
[0], 16);
4849 catfnumfields
= int(catheaderdata
[1], 16);
4850 catftype
= int(catheaderdata
[2], 16);
4851 if(re
.findall("^[.|/]", catheaderdata
[3])):
4852 catfname
= catheaderdata
[3];
4854 catfname
= "./"+catheaderdata
[3];
4855 catfbasedir
= os
.path
.dirname(catfname
);
4856 catflinkname
= catheaderdata
[4];
4857 catfsize
= int(catheaderdata
[5], 16);
4858 catfatime
= int(catheaderdata
[6], 16);
4859 catfmtime
= int(catheaderdata
[7], 16);
4860 catfctime
= int(catheaderdata
[8], 16);
4861 catfbtime
= int(catheaderdata
[9], 16);
4862 catfmode
= int(catheaderdata
[10], 16);
4863 catfchmode
= stat
.S_IMODE(catfmode
);
4864 catftypemod
= stat
.S_IFMT(catfmode
);
4865 catfwinattributes
= int(catheaderdata
[11], 16);
4866 catfcompression
= catheaderdata
[12];
4867 catfcsize
= int(catheaderdata
[13], 16);
4868 catfuid
= int(catheaderdata
[14], 16);
4869 catfuname
= catheaderdata
[15];
4870 catfgid
= int(catheaderdata
[16], 16);
4871 catfgname
= catheaderdata
[17];
4872 catfid
= int(catheaderdata
[18], 16);
4873 catfinode
= int(catheaderdata
[19], 16);
4874 catflinkcount
= int(catheaderdata
[20], 16);
4875 catfdev_minor
= int(catheaderdata
[21], 16);
4876 catfdev_major
= int(catheaderdata
[22], 16);
4877 catfrdev_minor
= int(catheaderdata
[23], 16);
4878 catfrdev_major
= int(catheaderdata
[24], 16);
4879 catfextrasize
= int(catheaderdata
[25], 16);
4880 catfextrafields
= int(catheaderdata
[26], 16);
4881 extrafieldslist
= [];
4883 extraend
= extrastart
+ catfextrafields
;
4884 extrafieldslist
= [];
4885 if(extrastart
<extraend
):
4886 extrafieldslist
.append(catheaderdata
[extrastart
]);
4887 extrastart
= extrastart
+ 1;
4888 catfchecksumtype
= catheaderdata
[extrastart
].lower();
4889 catfcs
= catheaderdata
[extrastart
+ 1].lower();
4890 catfccs
= catheaderdata
[extrastart
+ 2].lower();
4892 hcmax
= len(catheaderdata
) - 2;
4895 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
['format_delimiter']);
4897 catnewfcs
= GetFileChecksum(hout
, catheaderdata
[-3].lower(), True, formatspecs
);
4898 if(catfcs
!=catnewfcs
and not skipchecksum
):
4899 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfhstart
));
4900 VerbosePrintOut("'" + str(catfcs
) + "' != " + "'" + str(catnewfcs
) + "'");
4902 catfhend
= catfp
.tell() - 1;
4903 catfcontentstart
= catfp
.tell();
4904 catfcontents
= BytesIO();
4905 pyhascontents
= False;
4906 if(catfsize
>0 and not listonly
):
4907 if(catfcompression
=="none" or catfcompression
=="" or catfcompression
=="auto"):
4908 catfcontents
.write(catfp
.read(catfsize
));
4910 catfcontents
.write(catfp
.read(catfcsize
));
4911 catfcontents
.seek(0, 0);
4912 catnewfccs
= GetFileChecksum(catfcontents
.read(), catheaderdata
[-3].lower(), False, formatspecs
);
4913 pyhascontents
= True;
4914 if(catfccs
!=catnewfccs
and skipchecksum
):
4915 VerbosePrintOut("File Content Checksum Error with file " + catfname
+ " at offset " + str(catfcontentstart
));
4916 VerbosePrintOut("'" + str(catfccs
) + "' != " + "'" + str(catnewfccs
) + "'");
4918 if(catfcompression
=="none" or catfcompression
=="" or catfcompression
=="auto"):
4921 catfcontents
.seek(0, 0);
4922 catfcontents
= UncompressArchiveFile(catfcontents
, formatspecs
);
4923 if(catfsize
>0 and listonly
):
4924 if(catfcompression
=="none" or catfcompression
=="" or catfcompression
=="auto"):
4925 catfp
.seek(catfsize
, 1);
4927 catfp
.seek(catfcsize
, 1);
4928 pyhascontents
= False;
4930 catfcontentend
= catfp
.tell() - 1;
4931 catfcontents
.seek(0, 0);
4932 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
} });
4933 fileidnum
= fileidnum
+ 1;
4934 realidnum
= realidnum
+ 1;
4936 catlist
.update({'catfp': catfp
});
4941 create_alias_function("", __file_format_name__
, "ToArray", ArchiveFileToArray
);
4943 def ArchiveFileStringToArray(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
4944 formatspecs
= FormatSpecsListToDict(formatspecs
);
4945 catfp
= BytesIO(catstr
);
4946 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4947 return listcatfiles
;
4949 create_alias_function("", __file_format_name__
, "StringToArray", ArchiveFileStringToArray
);
4951 def TarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
4952 formatspecs
= FormatSpecsListToDict(formatspecs
);
4954 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4955 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4956 return listcatfiles
;
4958 def ZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
4959 formatspecs
= FormatSpecsListToDict(formatspecs
);
4961 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4962 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4963 return listcatfiles
;
4965 if(not rarfile_support
):
4966 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
4969 if(rarfile_support
):
4970 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
4971 formatspecs
= FormatSpecsListToDict(formatspecs
);
4973 catfp
= PackArchiveFileFromSevenZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4974 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4975 return listcatfiles
;
4977 if(not py7zr_support
):
4978 def SevenZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
4982 def SevenZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
4983 formatspecs
= FormatSpecsListToDict(formatspecs
);
4985 catfp
= PackArchiveFileFromSevenZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4986 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4987 return listcatfiles
;
4989 def InFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
4990 formatspecs
= FormatSpecsListToDict(formatspecs
);
4991 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
4992 if(checkcompressfile
=="tarfile"):
4993 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4994 elif(checkcompressfile
=="zipfile"):
4995 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4996 elif(checkcompressfile
=="catfile"):
4997 return ArchiveFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4998 elif(rarfile_support
and checkcompressfile
=="rarfile"):
4999 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5000 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
5001 return SevenZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5006 def ListDirToArrayAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
5007 formatspecs
= FormatSpecsListToDict(formatspecs
);
5008 catver
= formatspecs
['format_ver'];
5009 fileheaderver
= str(int(catver
.replace(".", "")));
5010 fileheader
= AppendNullByte(formatspecs
['format_magic'] + fileheaderver
, formatspecs
['format_delimiter']);
5011 advancedlist
= formatspecs
['use_advanced_list'];
5012 altinode
= formatspecs
['use_alt_inode'];
5015 for line
in sys
.stdin
:
5016 infilelist
.append(line
.strip());
5017 infilelist
= list(filter(None, infilelist
));
5018 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
5019 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
5021 with
UncompressFile(infiles
, formatspecs
, "r") as finfile
:
5022 for line
in finfile
:
5023 infilelist
.append(line
.strip());
5024 infilelist
= list(filter(None, infilelist
));
5026 if(isinstance(infiles
, (list, tuple, ))):
5027 infilelist
= list(filter(None, infiles
));
5028 elif(isinstance(infiles
, (str, ))):
5029 infilelist
= list(filter(None, [infiles
]));
5031 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
5033 GetDirList
= ListDir(infilelist
, followlink
, False);
5041 inodetocatinode
= {};
5043 fnumfiles
= int(len(GetDirList
));
5044 catver
= formatspecs
['format_ver'];
5045 fileheaderver
= str(int(catver
.replace(".", "")));
5046 fileheader
= AppendNullByte(formatspecs
['format_magic'] + fileheaderver
, formatspecs
['format_delimiter']);
5047 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5048 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
['format_delimiter']);
5049 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5050 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5051 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
5052 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter']);
5053 fheadtell
= len(fileheader
);
5054 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5055 for curfname
in GetDirList
:
5056 if(re
.findall("^[.|/]", curfname
)):
5059 fname
= "./"+curfname
;
5061 VerbosePrintOut(fname
);
5062 if(not followlink
or followlink
is None):
5063 fstatinfo
= os
.lstat(fname
);
5065 fstatinfo
= os
.stat(fname
);
5066 fpremode
= fstatinfo
.st_mode
;
5067 finode
= fstatinfo
.st_ino
;
5068 flinkcount
= fstatinfo
.st_nlink
;
5070 if(stat
.S_ISREG(fpremode
)):
5072 elif(stat
.S_ISLNK(fpremode
)):
5074 elif(stat
.S_ISCHR(fpremode
)):
5076 elif(stat
.S_ISBLK(fpremode
)):
5078 elif(stat
.S_ISDIR(fpremode
)):
5080 elif(stat
.S_ISFIFO(fpremode
)):
5082 elif(stat
.S_ISSOCK(fpremode
)):
5084 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
5086 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
5088 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
5093 fbasedir
= os
.path
.dirname(fname
);
5095 if(not followlink
and finode
!=0):
5097 if(finode
in inodelist
):
5099 flinkname
= inodetofile
[finode
];
5103 fcurinode
= inodetocatinode
[finode
];
5104 if(finode
not in inodelist
):
5105 inodelist
.append(finode
);
5106 inodetofile
.update({finode
: fname
});
5107 inodetocatinode
.update({finode
: curinode
});
5111 fcurinode
= curinode
;
5112 curinode
= curinode
+ 1;
5114 fcurinode
= curinode
;
5115 curinode
= curinode
+ 1;
5116 curfid
= curfid
+ 1;
5118 flinkname
= os
.readlink(fname
);
5119 fdev
= fstatinfo
.st_dev
;
5120 getfdev
= GetDevMajorMinor(fdev
);
5121 fdev_minor
= getfdev
[0];
5122 fdev_major
= getfdev
[1];
5123 frdev
= fstatinfo
.st_dev
;
5124 if(hasattr(fstatinfo
, "st_rdev")):
5125 frdev
= fstatinfo
.st_rdev
;
5127 frdev
= fstatinfo
.st_dev
;
5128 getfrdev
= GetDevMajorMinor(frdev
);
5129 frdev_minor
= getfrdev
[0];
5130 frdev_major
= getfrdev
[1];
5131 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
5133 if(ftype
==0 or ftype
==7):
5134 fsize
= fstatinfo
.st_size
;
5135 fatime
= fstatinfo
.st_atime
;
5136 fmtime
= fstatinfo
.st_mtime
;
5137 fctime
= fstatinfo
.st_ctime
;
5138 if(hasattr(fstatinfo
, "st_birthtime")):
5139 fbtime
= fstatinfo
.st_birthtime
;
5141 fbtime
= fstatinfo
.st_ctime
;
5142 fmode
= fstatinfo
.st_mode
;
5143 fchmode
= stat
.S_IMODE(fstatinfo
.st_mode
);
5144 ftypemod
= stat
.S_IFMT(fstatinfo
.st_mode
);
5145 fuid
= fstatinfo
.st_uid
;
5146 fgid
= fstatinfo
.st_gid
;
5151 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
5152 funame
= userinfo
.pw_name
;
5161 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
5162 fgname
= groupinfo
.gr_name
;
5167 fdev_minor
= fdev_minor
;
5168 fdev_major
= fdev_major
;
5169 frdev_minor
= frdev_minor
;
5170 frdev_major
= frdev_major
;
5171 flinkcount
= flinkcount
;
5172 if(hasattr(fstatinfo
, "st_file_attributes")):
5173 fwinattributes
= fstatinfo
.st_file_attributes
;
5178 fcontents
= BytesIO();
5179 if(ftype
==0 or ftype
==7):
5180 with
open(fname
, "rb") as fpc
:
5181 shutil
.copyfileobj(fpc
, fcontents
);
5182 if(followlink
and (ftype
==1 or ftype
==2)):
5183 flstatinfo
= os
.stat(flinkname
);
5184 with
open(flinkname
, "rb") as fpc
:
5185 shutil
.copyfileobj(fpc
, fcontents
);
5186 fcontents
.seek(0, 0);
5187 ftypehex
= format(ftype
, 'x').lower();
5188 extrafields
= len(extradata
);
5189 extrafieldslist
= extradata
;
5190 catfextrafields
= extrafields
;
5191 extrasizestr
= AppendNullByte(extrafields
, formatspecs
['format_delimiter']);
5192 if(len(extradata
)>0):
5193 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
['format_delimiter']);
5194 extrasizelen
= len(extrasizestr
);
5195 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5196 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()];
5197 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5198 catoutlenhex
= format(catoutlen
, 'x').lower();
5199 catoutlist
.insert(0, catoutlenhex
);
5200 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
['format_delimiter']);
5201 if(len(extradata
)>0):
5202 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
['format_delimiter']);
5203 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
['format_delimiter']);
5204 catfnumfields
= catoutlen
;
5205 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5206 fcontents
.seek(0, 0);
5207 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
5208 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
['format_delimiter']);
5209 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
5210 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
['format_delimiter']) + catfileoutstr
;
5211 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5212 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
['format_delimiter']);
5213 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5214 nullstrecd
= formatspecs
['format_delimiter'].encode('UTF-8');
5215 fheadtell
+= len(catfileoutstr
) + 1;
5216 catfcontentend
= fheadtell
- 1;
5217 fcontents
.seek(0, 0);
5218 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
5219 pyhascontents
= False;
5220 if(int(fsize
)>0 and not listonly
):
5221 pyhascontents
= True;
5222 if(int(fsize
)>0 and listonly
):
5223 fcontents
= BytesIO();
5224 pyhascontents
= False;
5225 fcontents
.seek(0, 0);
5226 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
} });
5227 fileidnum
= fileidnum
+ 1;
5230 def TarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
5231 formatspecs
= FormatSpecsListToDict(formatspecs
);
5237 inodetocatinode
= {};
5241 if(hasattr(sys
.stdin
, "buffer")):
5242 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
5244 shutil
.copyfileobj(sys
.stdin
, infile
);
5249 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
5250 infile
= download_file_from_internet_file(infile
);
5255 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5257 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
5259 if(not tarfile
.is_tarfile(infile
)):
5261 except AttributeError:
5262 if(not is_tarfile(infile
)):
5267 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
5268 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
5270 tarfp
= tarfile
.open(infile
, "r");
5271 except FileNotFoundError
:
5273 fnumfiles
= int(len(tarfp
.getmembers()));
5274 catver
= formatspecs
['format_ver'];
5275 fileheaderver
= str(int(catver
.replace(".", "")));
5276 fileheader
= AppendNullByte(formatspecs
['format_magic'] + fileheaderver
, formatspecs
['format_delimiter']);
5277 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5278 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
['format_delimiter']);
5279 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5280 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5281 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
5282 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter']);
5283 fheadtell
= len(fileheader
);
5284 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5285 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
5286 if(re
.findall("^[.|/]", member
.name
)):
5287 fname
= member
.name
;
5289 fname
= "./"+member
.name
;
5291 VerbosePrintOut(fname
);
5292 fpremode
= member
.mode
;
5293 ffullmode
= member
.mode
;
5297 ffullmode
= member
.mode
+ stat
.S_IFREG
;
5299 elif(member
.isdev()):
5300 ffullmode
= member
.mode
;
5302 elif(member
.islnk()):
5303 ffullmode
= member
.mode
+ stat
.S_IFREG
;
5305 elif(member
.issym()):
5306 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
5308 elif(member
.ischr()):
5309 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
5311 elif(member
.isblk()):
5312 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
5314 elif(member
.isdir()):
5315 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
5317 elif(member
.isfifo()):
5318 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
5320 elif(member
.issparse()):
5321 ffullmode
= member
.mode
;
5324 ffullmode
= member
.mode
;
5327 fbasedir
= os
.path
.dirname(fname
);
5331 curfid
= curfid
+ 1;
5333 flinkname
= member
.linkname
;
5334 fdev_minor
= member
.devminor
;
5335 fdev_major
= member
.devmajor
;
5336 frdev_minor
= member
.devminor
;
5337 frdev_major
= member
.devmajor
;
5338 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
5340 elif(ftype
==0 or ftype
==7):
5341 fsize
= member
.size
;
5343 fsize
= member
.size
;
5344 fatime
= member
.mtime
;
5345 fmtime
= member
.mtime
;
5346 fctime
= member
.mtime
;
5347 fbtime
= member
.mtime
;
5349 fchmode
= stat
.S_IMODE(ffullmode
);
5350 ftypemod
= stat
.S_IFMT(ffullmode
);
5353 funame
= member
.uname
;
5354 fgname
= member
.gname
;
5355 flinkcount
= flinkcount
;
5356 fwinattributes
= int(0);
5359 fcontents
= BytesIO();
5360 if(ftype
==0 or ftype
==7):
5361 with tarfp
.extractfile(member
) as fpc
:
5362 shutil
.copyfileobj(fpc
, fcontents
);
5363 fcontents
.seek(0, 0);
5364 ftypehex
= format(ftype
, 'x').lower();
5365 extrafields
= len(extradata
);
5366 extrafieldslist
= extradata
;
5367 catfextrafields
= extrafields
;
5368 extrasizestr
= AppendNullByte(extrafields
, formatspecs
['format_delimiter']);
5369 if(len(extradata
)>0):
5370 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
['format_delimiter']);
5371 extrasizelen
= len(extrasizestr
);
5372 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5373 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()];
5374 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5375 catoutlenhex
= format(catoutlen
, 'x').lower();
5376 catoutlist
.insert(0, catoutlenhex
);
5377 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
['format_delimiter']);
5378 if(len(extradata
)>0):
5379 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
['format_delimiter']);
5380 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
['format_delimiter']);
5381 catfnumfields
= catoutlen
;
5382 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5383 fcontents
.seek(0, 0);
5384 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
5385 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
['format_delimiter']);
5386 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
5387 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
['format_delimiter']) + catfileoutstr
;
5388 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5389 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
['format_delimiter']);
5390 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5391 nullstrecd
= formatspecs
['format_delimiter'].encode('UTF-8');
5392 fheadtell
+= len(catfileoutstr
) + 1;
5393 catfcontentend
= fheadtell
- 1;
5394 fcontents
.seek(0, 0);
5395 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
5396 pyhascontents
= False;
5397 if(int(fsize
)>0 and not listonly
):
5398 pyhascontents
= True;
5399 if(int(fsize
)>0 and listonly
):
5400 fcontents
= BytesIO();
5401 pyhascontents
= False;
5402 fcontents
.seek(0, 0);
5403 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
} });
5404 fileidnum
= fileidnum
+ 1;
5407 def ZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
5408 formatspecs
= FormatSpecsListToDict(formatspecs
);
5414 inodetocatinode
= {};
5418 if(hasattr(sys
.stdin
, "buffer")):
5419 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
5421 shutil
.copyfileobj(sys
.stdin
, infile
);
5426 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
5427 infile
= download_file_from_internet_file(infile
);
5432 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5436 if(not zipfile
.is_zipfile(infile
)):
5439 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
5440 except FileNotFoundError
:
5442 ziptest
= zipfp
.testzip();
5444 VerbosePrintOut("Bad file found!");
5445 fnumfiles
= int(len(zipfp
.infolist()));
5446 catver
= formatspecs
['format_ver'];
5447 fileheaderver
= str(int(catver
.replace(".", "")));
5448 fileheader
= AppendNullByte(formatspecs
['format_magic'] + fileheaderver
, formatspecs
['format_delimiter']);
5449 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5450 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5451 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5452 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
['format_delimiter']);
5453 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
5454 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter']);
5455 fheadtell
= len(fileheader
);
5456 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5457 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
5458 if(re
.findall("^[.|/]", member
.filename
)):
5459 fname
= member
.filename
;
5461 fname
= "./"+member
.filename
;
5462 zipinfo
= zipfp
.getinfo(member
.filename
);
5464 VerbosePrintOut(fname
);
5465 if(not member
.is_dir()):
5466 fpremode
= stat
.S_IFREG
+ 438;
5467 elif(member
.is_dir()):
5468 fpremode
= stat
.S_IFDIR
+ 511;
5471 if(not member
.is_dir()):
5473 elif(member
.is_dir()):
5476 fbasedir
= os
.path
.dirname(fname
);
5480 curfid
= curfid
+ 1;
5488 fsize
= member
.file_size
;
5490 fsize
= member
.file_size
;
5491 fatime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5492 fmtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5493 fctime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5494 fbtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
5495 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
5496 fwinattributes
= int(zipinfo
.external_attr
);
5497 if(not member
.is_dir()):
5498 fmode
= int(stat
.S_IFREG
+ 438);
5499 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
5500 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
5501 elif(member
.is_dir()):
5502 fmode
= int(stat
.S_IFDIR
+ 511);
5503 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
5504 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
5505 elif(zipinfo
.create_system
==3):
5506 fwinattributes
= int(0);
5507 fmode
= int(zipinfo
.external_attr
);
5509 fwinattributes
= int(0);
5510 if(not member
.is_dir()):
5511 fmode
= int(stat
.S_IFREG
+ 438);
5512 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
5513 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
5514 elif(member
.is_dir()):
5515 fmode
= int(stat
.S_IFDIR
+ 511);
5516 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
5517 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
5522 except AttributeError:
5528 except AttributeError:
5535 userinfo
= pwd
.getpwuid(os
.getuid());
5536 funame
= userinfo
.pw_name
;
5539 except AttributeError:
5547 groupinfo
= grp
.getgrgid(os
.getgid());
5548 fgname
= groupinfo
.gr_name
;
5551 except AttributeError:
5555 fcontents
= BytesIO();
5557 fcontents
.write(zipfp
.read(member
.filename
));
5558 fcontents
.seek(0, 0);
5559 ftypehex
= format(ftype
, 'x').lower();
5560 extrafields
= len(extradata
);
5561 extrafieldslist
= extradata
;
5562 catfextrafields
= extrafields
;
5563 extrasizestr
= AppendNullByte(extrafields
, formatspecs
['format_delimiter']);
5564 if(len(extradata
)>0):
5565 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
['format_delimiter']);
5566 extrasizelen
= len(extrasizestr
);
5567 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5568 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()];
5569 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5570 catoutlenhex
= format(catoutlen
, 'x').lower();
5571 catoutlist
.insert(0, catoutlenhex
);
5572 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
['format_delimiter']);
5573 if(len(extradata
)>0):
5574 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
['format_delimiter']);
5575 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
['format_delimiter']);
5576 catfnumfields
= catoutlen
;
5577 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5578 fcontents
.seek(0, 0);
5579 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
5580 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
['format_delimiter']);
5581 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
5582 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
['format_delimiter']) + catfileoutstr
;
5583 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5584 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
['format_delimiter']);
5585 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5586 nullstrecd
= formatspecs
['format_delimiter'].encode('UTF-8');
5587 fheadtell
+= len(catfileoutstr
) + 1;
5588 catfcontentend
= fheadtell
- 1;
5589 fcontents
.seek(0, 0);
5590 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
5591 pyhascontents
= False;
5592 if(int(fsize
)>0 and not listonly
):
5593 pyhascontents
= True;
5594 if(int(fsize
)>0 and listonly
):
5595 fcontents
= BytesIO();
5596 pyhascontents
= False;
5597 fcontents
.seek(0, 0);
5598 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
} });
5599 fileidnum
= fileidnum
+ 1;
5602 if(not rarfile_support
):
5603 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
5606 if(rarfile_support
):
5607 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
5608 formatspecs
= FormatSpecsListToDict(formatspecs
);
5614 inodetocatinode
= {};
5616 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
5618 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
5620 rarfp
= rarfile
.RarFile(infile
, "r");
5621 rartest
= rarfp
.testrar();
5623 VerbosePrintOut("Bad file found!");
5624 fnumfiles
= int(len(rarfp
.infolist()));
5625 catver
= formatspecs
['format_ver'];
5626 fileheaderver
= str(int(catver
.replace(".", "")));
5627 fileheader
= AppendNullByte(formatspecs
['format_magic'] + fileheaderver
, formatspecs
['format_delimiter']);
5628 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5629 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5630 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5631 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
['format_delimiter']);
5632 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
5633 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter']);
5634 fheadtell
= len(fileheader
);
5635 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5636 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
5639 if(member
.host_os
==rarfile
.RAR_OS_UNIX
):
5642 member
.external_attr
5644 except AttributeError:
5646 elif(member
.host_os
==rarfile
.RAR_OS_WIN32
):
5649 member
.external_attr
5651 except AttributeError:
5656 if(re
.findall("^[.|/]", member
.filename
)):
5657 fname
= member
.filename
;
5659 fname
= "./"+member
.filename
;
5660 rarinfo
= rarfp
.getinfo(member
.filename
);
5662 VerbosePrintOut(fname
);
5663 if(is_unix
and member
.external_attr
!=0):
5664 fpremode
= int(member
.external_attr
);
5665 elif(member
.is_file()):
5666 fpremode
= stat
.S_IFREG
+ 438;
5667 elif(member
.is_symlink()):
5668 fpremode
= stat
.S_IFLNK
+ 438;
5669 elif(member
.is_dir()):
5670 fpremode
= stat
.S_IFDIR
+ 511;
5671 if(is_windows
and member
.external_attr
!=0):
5672 fwinattributes
= int(member
.external_attr
);
5674 fwinattributes
= int(0);
5679 if(member
.is_file()):
5681 elif(member
.is_symlink()):
5683 elif(member
.is_dir()):
5687 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
5688 fbasedir
= os
.path
.dirname(fname
);
5692 curfid
= curfid
+ 1;
5700 fsize
= member
.file_size
;
5703 fatime
= int(member
.atime
.timestamp());
5705 fatime
= int(member
.mtime
.timestamp());
5706 except AttributeError:
5707 fatime
= int(member
.mtime
.timestamp());
5708 fmtime
= int(member
.mtime
.timestamp());
5711 fctime
= int(member
.ctime
.timestamp());
5713 fctime
= int(member
.mtime
.timestamp());
5714 except AttributeError:
5715 fctime
= int(member
.mtime
.timestamp());
5716 fbtime
= int(member
.mtime
.timestamp());
5717 if(is_unix
and member
.external_attr
!=0):
5718 fmode
= int(member
.external_attr
);
5719 fchmode
= int(stat
.S_IMODE(member
.external_attr
));
5720 ftypemod
= int(stat
.S_IFMT(member
.external_attr
));
5721 elif(member
.is_file()):
5722 fmode
= int(stat
.S_IFREG
+ 438)
5723 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
5724 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
5725 elif(member
.is_symlink()):
5726 fmode
= int(stat
.S_IFLNK
+ 438)
5727 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
5728 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
5729 elif(member
.is_dir()):
5730 fmode
= int(stat
.S_IFDIR
+ 511)
5731 fchmode
= int(stat
.S_IMODE(stat
.S_IFDIR
+ 511));
5732 ftypemod
= int(stat
.S_IFMT(stat
.S_IFDIR
+ 511));
5735 except AttributeError:
5741 except AttributeError:
5748 userinfo
= pwd
.getpwuid(os
.getuid());
5749 funame
= userinfo
.pw_name
;
5752 except AttributeError:
5760 groupinfo
= grp
.getgrgid(os
.getgid());
5761 fgname
= groupinfo
.gr_name
;
5764 except AttributeError:
5768 fcontents
= BytesIO();
5770 fcontents
.write(rarfp
.read(member
.filename
));
5771 fcontents
.seek(0, 0);
5772 ftypehex
= format(ftype
, 'x').lower();
5773 extrafields
= len(extradata
);
5774 extrafieldslist
= extradata
;
5775 catfextrafields
= extrafields
;
5776 extrasizestr
= AppendNullByte(extrafields
, formatspecs
['format_delimiter']);
5777 if(len(extradata
)>0):
5778 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
['format_delimiter']);
5779 extrasizelen
= len(extrasizestr
);
5780 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5781 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()];
5782 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5783 catoutlenhex
= format(catoutlen
, 'x').lower();
5784 catoutlist
.insert(0, catoutlenhex
);
5785 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
['format_delimiter']);
5786 if(len(extradata
)>0):
5787 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
['format_delimiter']);
5788 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
['format_delimiter']);
5789 catfnumfields
= 24 + catfextrafields
;
5790 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5791 fcontents
.seek(0, 0);
5792 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
5793 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
['format_delimiter']);
5794 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
5795 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
['format_delimiter']) + catfileoutstr
;
5796 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5797 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
['format_delimiter']);
5798 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5799 nullstrecd
= formatspecs
['format_delimiter'].encode('UTF-8');
5800 fheadtell
+= len(catfileoutstr
) + 1;
5801 catfcontentend
= fheadtell
- 1;
5802 fcontents
.seek(0, 0);
5803 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
5804 pyhascontents
= False;
5805 if(int(fsize
)>0 and not listonly
):
5806 pyhascontents
= True;
5807 if(int(fsize
)>0 and listonly
):
5808 fcontents
= BytesIO();
5809 pyhascontents
= False;
5810 fcontents
.seek(0, 0);
5811 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
} });
5812 fileidnum
= fileidnum
+ 1;
5815 if(not py7zr_support
):
5816 def SevenZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
5820 def SevenZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
5821 formatspecs
= FormatSpecsListToDict(formatspecs
);
5827 inodetocatinode
= {};
5829 szpfp
= py7zr
.SevenZipFile(infile
, mode
="r");
5830 file_content
= szpfp
.readall();
5831 #sztest = szpfp.testzip();
5832 sztestalt
= szpfp
.test();
5834 VerbosePrintOut("Bad file found!");
5835 numfiles
= int(len(szpfp
.list()));
5836 catver
= formatspecs
['format_ver'];
5837 fileheaderver
= str(int(catver
.replace(".", "")));
5838 fileheader
= AppendNullByte(formatspecs
['format_magic'] + fileheaderver
, formatspecs
['format_delimiter']);
5839 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
5840 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
5841 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5842 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
['format_delimiter']);
5843 catfileheadercshex
= GetFileChecksum(fileheader
, fprechecksumtype
, True, formatspecs
);
5844 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
['format_delimiter']);
5845 fheadtell
= len(fileheader
);
5846 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
5847 for member
in sorted(szpfp
.list(), key
=lambda x
: x
.filename
):
5848 if(re
.findall("^[.|/]", member
.filename
)):
5849 fname
= member
.filename
;
5851 fname
= "./"+member
.filename
;
5852 if(not member
.is_directory
):
5853 fpremode
= int(stat
.S_IFREG
+ 438);
5854 elif(member
.is_directory
):
5855 fpremode
= int(stat
.S_IFDIR
+ 511);
5856 fwinattributes
= int(0);
5861 if(member
.is_directory
):
5866 fbasedir
= os
.path
.dirname(fname
);
5870 curfid
= curfid
+ 1;
5877 fatime
= int(member
.creationtime
.timestamp());
5878 fmtime
= int(member
.creationtime
.timestamp());
5879 fctime
= int(member
.creationtime
.timestamp());
5880 fbtime
= int(member
.creationtime
.timestamp());
5881 if(member
.is_directory
):
5882 fmode
= int(stat
.S_IFDIR
+ 511)
5883 fchmode
= int(stat
.S_IMODE(stat
.S_IFDIR
+ 511));
5884 ftypemod
= int(stat
.S_IFMT(stat
.S_IFDIR
+ 511));
5886 fmode
= int(stat
.S_IFLNK
+ 438)
5887 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
5888 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
5891 except AttributeError:
5897 except AttributeError:
5904 userinfo
= pwd
.getpwuid(os
.getuid());
5905 funame
= userinfo
.pw_name
;
5908 except AttributeError:
5916 groupinfo
= grp
.getgrgid(os
.getgid());
5917 fgname
= groupinfo
.gr_name
;
5920 except AttributeError:
5924 fcontents
= BytesIO();
5926 fcontents
.write(file_content
[member
.filename
].read());
5927 fsize
= format(fcontents
.tell(), 'x').lower();
5929 fcontents
.seek(0, 0);
5930 ftypehex
= format(ftype
, 'x').lower();
5931 extrafields
= len(extradata
);
5932 extrafieldslist
= extradata
;
5933 catfextrafields
= extrafields
;
5934 extrasizestr
= AppendNullByte(extrafields
, formatspecs
['format_delimiter']);
5935 if(len(extradata
)>0):
5936 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
['format_delimiter']);
5937 extrasizelen
= len(extrasizestr
);
5938 extrasizelenhex
= format(extrasizelen
, 'x').lower();
5939 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()];
5940 catoutlen
= len(catoutlist
) + len(extradata
) + 3;
5941 catoutlenhex
= format(catoutlen
, 'x').lower();
5942 catoutlist
.insert(0, catoutlenhex
);
5943 catfileoutstr
= AppendNullBytes(catoutlist
, formatspecs
['format_delimiter']);
5944 if(len(extradata
)>0):
5945 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
['format_delimiter']);
5946 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
['format_delimiter']);
5947 catfnumfields
= 24 + catfextrafields
;
5948 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5949 fcontents
.seek(0, 0);
5950 catfilecontentcshex
= GetFileChecksum(fcontents
.read(), checksumtype
, False, formatspecs
);
5951 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
['format_delimiter']);
5952 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower();
5953 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
['format_delimiter']) + catfileoutstr
;
5954 catfileheadercshex
= GetFileChecksum(catfileoutstr
, checksumtype
, True, formatspecs
);
5955 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
['format_delimiter']);
5956 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5957 nullstrecd
= formatspecs
['format_delimiter'].encode('UTF-8');
5958 fheadtell
+= len(catfileoutstr
) + 1;
5959 catfcontentend
= fheadtell
- 1;
5960 fcontents
.seek(0, 0);
5961 catfileout
= catfileoutstrecd
+ fcontents
.read() + nullstrecd
;
5962 pyhascontents
= False;
5963 if(int(fsize
)>0 and not listonly
):
5964 pyhascontents
= True;
5965 if(int(fsize
)>0 and listonly
):
5966 fcontents
= BytesIO();
5967 pyhascontents
= False;
5968 fcontents
.seek(0, 0);
5969 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
} });
5970 fileidnum
= fileidnum
+ 1;
5973 def InFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
5974 formatspecs
= FormatSpecsListToDict(formatspecs
);
5975 checkcompressfile
= CheckCompressionSubType(infiles
, formatspecs
, True);
5976 if(checkcompressfile
=="tarfile"):
5977 return TarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5978 elif(checkcompressfile
=="zipfile"):
5979 return ZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5980 elif(checkcompressfile
=="catfile"):
5981 return ArchiveFileToArray(infile
, 0, 0, listonly
, False, formatspecs
, False);
5982 elif(rarfile_support
and checkcompressfile
=="rarfile"):
5983 return RarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5984 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
5985 return SevenZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5990 def ListDirToArray(infiles
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
5991 formatspecs
= FormatSpecsListToDict(formatspecs
);
5992 outarray
= BytesIO();
5993 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compresswholefile
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
5994 listcatfiles
= ArchiveFileToArray(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5995 return listcatfiles
;
5997 def ArchiveFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
5998 formatspecs
= FormatSpecsListToDict(formatspecs
);
5999 if(isinstance(infile
, dict)):
6000 listcatfiles
= infile
;
6002 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6003 infile
= RemoveWindowsPath(infile
);
6004 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6005 if(not listcatfiles
):
6007 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': {}}}};
6009 catarray
.update({'catfp': listcatfiles
['catfp']});
6010 lenlist
= len(listcatfiles
['ffilelist']);
6012 lcfx
= int(listcatfiles
['fnumfiles']);
6013 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6014 lcfx
= int(lenlist
);
6016 lcfx
= int(listcatfiles
['fnumfiles']);
6018 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
6019 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
6020 catarray
['filetoid'].update(filetoidarray
);
6021 catarray
['idtofile'].update(idtofilearray
);
6022 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6023 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
6024 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
6025 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6026 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
6027 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
6028 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6029 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6030 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6031 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6032 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6033 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6034 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6035 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
6036 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
6037 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
6038 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6039 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6040 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
6041 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
6042 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
6043 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6044 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6045 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6046 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
6047 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
6048 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
6049 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6050 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6051 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6052 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6056 create_alias_function("", __file_format_name__
, "ToArrayIndex", ArchiveFileToArrayIndex
);
6058 def ListDirToArrayIndexAlt(infiles
, dirlistfromtxt
=False, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
6059 formatspecs
= FormatSpecsListToDict(formatspecs
);
6060 listcatfiles
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
6061 if(not listcatfiles
):
6063 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': {}}}};
6064 lenlist
= len(listcatfiles
['ffilelist']);
6066 lcfx
= int(listcatfiles
['fnumfiles']);
6067 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6068 lcfx
= int(lenlist
);
6070 lcfx
= int(listcatfiles
['fnumfiles']);
6072 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
6073 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
6074 catarray
['filetoid'].update(filetoidarray
);
6075 catarray
['idtofile'].update(idtofilearray
);
6076 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6077 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
6078 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
6079 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6080 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
6081 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
6082 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6083 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6084 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6085 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6086 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6087 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6088 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6089 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
6090 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
6091 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
6092 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6093 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6094 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
6095 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
6096 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
6097 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6098 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6099 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6100 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
6101 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
6102 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
6103 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6104 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6105 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6106 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6110 def TarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
6111 formatspecs
= FormatSpecsListToDict(formatspecs
);
6112 listcatfiles
= TarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
6113 if(not listcatfiles
):
6115 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': {}}}};
6116 lenlist
= len(listcatfiles
['ffilelist']);
6118 lcfx
= int(listcatfiles
['fnumfiles']);
6119 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6120 lcfx
= int(lenlist
);
6122 lcfx
= int(listcatfiles
['fnumfiles']);
6124 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
6125 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
6126 catarray
['filetoid'].update(filetoidarray
);
6127 catarray
['idtofile'].update(idtofilearray
);
6128 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6129 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
6130 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
6131 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6132 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
6133 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
6134 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6135 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6136 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6137 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6138 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6139 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6140 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6141 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
6142 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
6143 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
6144 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6145 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6146 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
6147 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
6148 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
6149 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6150 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6151 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6152 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
6153 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
6154 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
6155 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6156 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6157 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6158 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6162 def ZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
6163 formatspecs
= FormatSpecsListToDict(formatspecs
);
6164 listcatfiles
= ZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
6165 if(not listcatfiles
):
6167 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': {}}}};
6168 lenlist
= len(listcatfiles
['ffilelist']);
6170 lcfx
= int(listcatfiles
['fnumfiles']);
6171 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6172 lcfx
= int(lenlist
);
6174 lcfx
= int(listcatfiles
['fnumfiles']);
6176 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
6177 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
6178 catarray
['filetoid'].update(filetoidarray
);
6179 catarray
['idtofile'].update(idtofilearray
);
6180 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6181 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
6182 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
6183 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6184 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
6185 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
6186 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6187 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6188 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6189 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6190 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6191 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6192 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6193 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
6194 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
6195 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
6196 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6197 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6198 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
6199 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
6200 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
6201 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6202 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6203 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6204 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
6205 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
6206 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
6207 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6208 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6209 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6210 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6214 if(not rarfile_support
):
6215 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
6218 if(rarfile_support
):
6219 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
6220 formatspecs
= FormatSpecsListToDict(formatspecs
);
6221 listcatfiles
= RarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
6222 if(not listcatfiles
):
6224 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': {}}}};
6225 lenlist
= len(listcatfiles
['ffilelist']);
6227 lcfx
= int(listcatfiles
['fnumfiles']);
6228 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6229 lcfx
= int(lenlist
);
6231 lcfx
= int(listcatfiles
['fnumfiles']);
6233 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
6234 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
6235 catarray
['filetoid'].update(filetoidarray
);
6236 catarray
['idtofile'].update(idtofilearray
);
6237 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6238 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
6239 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
6240 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6241 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
6242 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
6243 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6244 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6245 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6246 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6247 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6248 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6249 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6250 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
6251 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
6252 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
6253 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6254 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6255 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
6256 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
6257 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
6258 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6259 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6260 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6261 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
6262 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
6263 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
6264 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6265 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6266 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6267 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6271 if(not py7zr_support
):
6272 def SevenZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
6276 def SevenZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False):
6277 formatspecs
= FormatSpecsListToDict(formatspecs
);
6278 listcatfiles
= SevenZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
6279 if(not listcatfiles
):
6281 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': {}}}};
6282 lenlist
= len(listcatfiles
['ffilelist']);
6284 lcfx
= int(listcatfiles
['fnumfiles']);
6285 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6286 lcfx
= int(lenlist
);
6288 lcfx
= int(listcatfiles
['fnumfiles']);
6290 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
6291 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
6292 catarray
['filetoid'].update(filetoidarray
);
6293 catarray
['idtofile'].update(idtofilearray
);
6294 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6295 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
6296 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
6297 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6298 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
6299 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
6300 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6301 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6302 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6303 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6304 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6305 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
6306 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
6307 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
6308 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
6309 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
6310 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6311 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6312 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
6313 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
6314 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
6315 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6316 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6317 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6318 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
6319 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
6320 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
6321 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
6322 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
6323 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
6324 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
6328 def ArchiveFileStringToArrayIndex(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
6329 formatspecs
= FormatSpecsListToDict(formatspecs
);
6330 catfp
= BytesIO(catstr
);
6331 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6332 return listcatfiles
;
6334 create_alias_function("", __file_format_name__
, "StringToArrayIndex", ArchiveFileStringToArrayIndex
);
6336 def TarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
6337 formatspecs
= FormatSpecsListToDict(formatspecs
);
6339 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
6340 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6341 return listcatfiles
;
6343 def ZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
6344 formatspecs
= FormatSpecsListToDict(formatspecs
);
6346 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
6347 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6348 return listcatfiles
;
6350 if(not rarfile_support
):
6351 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
6354 if(rarfile_support
):
6355 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
6356 formatspecs
= FormatSpecsListToDict(formatspecs
);
6358 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
6359 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6360 return listcatfiles
;
6362 if(not py7zr_support
):
6363 def SevenZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
6367 def SevenZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
6368 formatspecs
= FormatSpecsListToDict(formatspecs
);
6370 catfp
= PackArchiveFileFromSevenZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
6371 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
6372 return listcatfiles
;
6374 def ListDirToArrayIndex(infiles
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, checksumtype
="crc32", formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
6375 formatspecs
= FormatSpecsListToDict(formatspecs
);
6376 outarray
= BytesIO();
6377 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compresswholefile
, compressionlevel
, followlink
, checksumtype
, formatspecs
, verbose
, True);
6378 listcatfiles
= ArchiveFileToArrayIndex(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
)
6379 return listcatfiles
;
6381 def RePackArchiveFile(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
6382 formatspecs
= FormatSpecsListToDict(formatspecs
);
6383 if(isinstance(infile
, dict)):
6384 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6385 listcatfiles
= prelistcatfiles
['list'];
6387 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6388 infile
= RemoveWindowsPath(infile
);
6390 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6391 listcatfiles
= prelistcatfiles
['list'];
6393 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6394 if(outfile
!="-" and not hasattr(infile
, "read") and not hasattr(outfile
, "write")):
6395 outfile
= RemoveWindowsPath(outfile
);
6396 checksumtype
= checksumtype
.lower();
6397 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
6398 checksumtype
="crc32";
6399 if(checksumtype
=="none"):
6401 if(not compression
or compression
=="catfile" or compression
==formatspecs
['format_lower']):
6402 compression
= "auto";
6403 if(compression
not in compressionlist
and compression
is None):
6404 compression
= "auto";
6406 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6407 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
6408 if(os
.path
.exists(outfile
)):
6411 except OSError as e
:
6413 if(not listcatfiles
):
6418 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
6420 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
6423 fbasename
= os
.path
.splitext(outfile
)[0];
6424 fextname
= os
.path
.splitext(outfile
)[1];
6425 catfp
= CompressOpenFile(outfile
, compresswholefile
, compressionlevel
);
6426 catver
= formatspecs
['format_ver'];
6427 fileheaderver
= str(int(catver
.replace(".", "")));
6428 lenlist
= len(listcatfiles
['ffilelist']);
6429 fnumfiles
= int(listcatfiles
['fnumfiles']);
6430 if(lenlist
>fnumfiles
or lenlist
<fnumfiles
):
6431 fnumfiles
= lenlist
;
6432 AppendFileHeader(catfp
, fnumfiles
, checksumtype
, formatspecs
);
6433 lenlist
= len(listcatfiles
['ffilelist']);
6434 fnumfiles
= int(listcatfiles
['fnumfiles']);
6436 lcfx
= int(listcatfiles
['fnumfiles']);
6437 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6438 lcfx
= int(lenlist
);
6440 lcfx
= int(listcatfiles
['fnumfiles']);
6448 if(re
.findall("^[.|/]", listcatfiles
['ffilelist'][reallcfi
]['fname'])):
6449 fname
= listcatfiles
['ffilelist'][reallcfi
]['fname'];
6451 fname
= "./"+listcatfiles
['ffilelist'][reallcfi
]['fname'];
6453 VerbosePrintOut(fname
);
6454 fheadersize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fheadersize']), 'x').lower();
6455 fsize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fsize']), 'x').lower();
6456 flinkname
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
6457 fatime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fatime']), 'x').lower();
6458 fmtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmtime']), 'x').lower();
6459 fctime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fctime']), 'x').lower();
6460 fbtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fbtime']), 'x').lower();
6461 fmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmode']), 'x').lower();
6462 fchmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fchmode']), 'x').lower();
6463 fuid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fuid']), 'x').lower();
6464 funame
= listcatfiles
['ffilelist'][reallcfi
]['funame'];
6465 fgid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fgid']), 'x').lower();
6466 fgname
= listcatfiles
['ffilelist'][reallcfi
]['fgname'];
6467 finode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['finode']), 'x').lower();
6468 flinkcount
= format(int(listcatfiles
['ffilelist'][reallcfi
]['flinkcount']), 'x').lower();
6469 fwinattributes
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fwinattributes']), 'x').lower();
6470 fcompression
= listcatfiles
['ffilelist'][reallcfi
]['fcompression'];
6471 fcsize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fcsize']), 'x').lower();
6472 fdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fminor']), 'x').lower();
6473 fdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmajor']), 'x').lower();
6474 frdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frminor']), 'x').lower();
6475 frdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frmajor']), 'x').lower();
6476 if(len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] and len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>0):
6477 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
6478 if(not followlink
and len(extradata
)<0):
6479 extradata
= listcatfiles
['ffilelist'][reallcfi
]['fextralist'];
6480 fcontents
= listcatfiles
['ffilelist'][reallcfi
]['fcontents'];
6482 fcsize
= format(int(0), 'x').lower();
6483 if(not compresswholefile
):
6484 fcontents
.seek(0, 2);
6485 ucfsize
= fcontents
.tell();
6486 fcontents
.seek(0, 0);
6487 if(compression
=="auto"):
6488 ilsize
= len(compressionlistalt
);
6491 while(ilmin
< ilsize
):
6492 cfcontents
= BytesIO();
6493 shutil
.copyfileobj(fcontents
, cfcontents
);
6494 fcontents
.seek(0, 0);
6495 cfcontents
.seek(0, 0);
6496 cfcontents
= CompressArchiveFile(cfcontents
, compressionlistalt
[ilmin
], compressionlevel
, formatspecs
);
6498 cfcontents
.seek(0, 2);
6499 ilcsize
.append(cfcontents
.tell());
6503 ilcsize
.append(sys
.maxint
);
6504 except AttributeError:
6505 ilcsize
.append(sys
.maxsize
);
6507 ilcmin
= ilcsize
.index(min(ilcsize
));
6508 compression
= compressionlistalt
[ilcmin
];
6509 fcontents
.seek(0, 0);
6510 cfcontents
= BytesIO();
6511 shutil
.copyfileobj(fcontents
, cfcontents
);
6512 cfcontents
.seek(0, 0);
6513 cfcontents
= CompressArchiveFile(cfcontents
, compression
, compressionlevel
, formatspecs
);
6514 cfcontents
.seek(0, 2);
6515 cfsize
= cfcontents
.tell();
6516 if(ucfsize
> cfsize
):
6517 fcsize
= format(int(cfsize
), 'x').lower();
6518 fcompression
= compression
;
6520 fcontents
= cfcontents
;
6522 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']==1 or listcatfiles
['ffilelist'][reallcfi
]['ftype']==2):
6523 getflinkpath
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
6524 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
6525 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
6526 fheadersize
= format(int(flinkinfo
['fheadersize']), 'x').lower();
6527 fsize
= format(int(flinkinfo
['fsize']), 'x').lower();
6528 flinkname
= flinkinfo
['flinkname'];
6529 fatime
= format(int(flinkinfo
['fatime']), 'x').lower();
6530 fmtime
= format(int(flinkinfo
['fmtime']), 'x').lower();
6531 fctime
= format(int(flinkinfo
['fctime']), 'x').lower();
6532 fbtime
= format(int(flinkinfo
['fbtime']), 'x').lower();
6533 fmode
= format(int(flinkinfo
['fmode']), 'x').lower();
6534 fchmode
= format(int(flinkinfo
['fchmode']), 'x').lower();
6535 fuid
= format(int(flinkinfo
['fuid']), 'x').lower();
6536 funame
= flinkinfo
['funame'];
6537 fgid
= format(int(flinkinfo
['fgid']), 'x').lower();
6538 fgname
= flinkinfo
['fgname'];
6539 finode
= format(int(flinkinfo
['finode']), 'x').lower();
6540 flinkcount
= format(int(flinkinfo
['flinkcount']), 'x').lower();
6541 fwinattributes
= format(int(flinkinfo
['fwinattributes']), 'x').lower();
6542 fcompression
= flinkinfo
['fcompression'];
6543 fcsize
= format(int(flinkinfo
['fcsize']), 'x').lower();
6544 fdev_minor
= format(int(flinkinfo
['fminor']), 'x').lower();
6545 fdev_major
= format(int(flinkinfo
['fmajor']), 'x').lower();
6546 frdev_minor
= format(int(flinkinfo
['frminor']), 'x').lower();
6547 frdev_major
= format(int(flinkinfo
['frmajor']), 'x').lower();
6548 if(len(flinkinfo
['fextralist'])>flinkinfo
['fextrafields'] and len(flinkinfo
['fextralist'])>0):
6549 flinkinfo
['fextrafields'] = len(flinkinfo
['fextralist']);
6550 if(len(extradata
)<0):
6551 extradata
= flinkinfo
['fextralist'];
6552 fcontents
= flinkinfo
['fcontents'];
6553 ftypehex
= format(flinkinfo
['ftype'], 'x').lower();
6555 ftypehex
= format(listcatfiles
['ffilelist'][reallcfi
]['ftype'], 'x').lower();
6556 fcurfid
= format(curfid
, 'x').lower();
6557 if(not followlink
and finode
!=0):
6558 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=1):
6559 fcurinode
= format(int(curinode
), 'x').lower();
6560 inodetofile
.update({curinode
: fname
});
6561 filetoinode
.update({fname
: curinode
});
6562 curinode
= curinode
+ 1;
6564 fcurinode
= format(int(filetoinode
[flinkname
]), 'x').lower();
6566 fcurinode
= format(int(curinode
), 'x').lower();
6567 curinode
= curinode
+ 1;
6568 curfid
= curfid
+ 1;
6569 if(fcompression
=="none"):
6571 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
];
6572 catfp
= AppendFileHeaderWithContent(catfp
, catoutlist
, extradata
, fcontents
.read(), checksumtype
, formatspecs
);
6575 reallcfi
= reallcfi
+ 1;
6577 catfp
.write(AppendNullBytes([0, 0], formatspecs
['format_delimiter']).encode("UTF-8"));
6578 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
6579 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
6582 os
.fsync(catfp
.fileno());
6583 except io
.UnsupportedOperation
:
6585 except AttributeError:
6587 except OSError as e
:
6591 if(hasattr(sys
.stdout
, "buffer")):
6592 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
6594 shutil
.copyfileobj(catfp
, sys
.stdout
);
6595 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", str(outfile
))):
6596 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
6598 upload_file_to_internet_file(catfp
, outfile
);
6606 create_alias_function("RePack", __file_format_name__
, "", RePackArchiveFile
);
6608 def RePackArchiveFileFromString(catstr
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
6609 formatspecs
= FormatSpecsListToDict(formatspecs
);
6610 catfp
= BytesIO(catstr
);
6611 listcatfiles
= RePackArchiveFile(catfp
, compression
, compresswholefile
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
6612 return listcatfiles
;
6614 create_alias_function("RePack", __file_format_name__
, "FromString", RePackArchiveFileFromString
);
6616 def PackArchiveFileFromListDir(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, skipchecksum
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
6617 formatspecs
= FormatSpecsListToDict(formatspecs
);
6618 outarray
= BytesIO();
6619 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compresswholefile
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
6620 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compresswholefile
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
6621 return listcatfiles
;
6623 create_alias_function("Pack", __file_format_name__
, "FromListDir", PackArchiveFileFromListDir
);
6625 def UnPackArchiveFile(infile
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_dict__
, preservepermissions
=True, preservetime
=True, verbose
=False, returnfp
=False):
6626 formatspecs
= FormatSpecsListToDict(formatspecs
);
6627 if(outdir
is not None):
6628 outdir
= RemoveWindowsPath(outdir
);
6630 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6631 if(isinstance(infile
, dict)):
6632 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6633 listcatfiles
= prelistcatfiles
['list'];
6635 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6636 infile
= RemoveWindowsPath(infile
);
6638 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6639 listcatfiles
= prelistcatfiles
['list'];
6641 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
6642 if(not listcatfiles
):
6644 lenlist
= len(listcatfiles
['ffilelist']);
6645 fnumfiles
= int(listcatfiles
['fnumfiles']);
6647 lcfx
= int(listcatfiles
['fnumfiles']);
6648 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6649 lcfx
= int(lenlist
);
6651 lcfx
= int(listcatfiles
['fnumfiles']);
6657 userinfo
= pwd
.getpwuid(listcatfiles
['ffilelist'][lcfi
]['fuid']);
6658 funame
= userinfo
.pw_name
;
6667 groupinfo
= grp
.getgrgid(listcatfiles
['ffilelist'][lcfi
]['fgid']);
6668 fgname
= groupinfo
.gr_name
;
6674 VerbosePrintOut(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6675 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
6676 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
6677 listcatfiles
['ffilelist'][lcfi
]['fcontents'].seek(0, 0);
6678 shutil
.copyfileobj(listcatfiles
['ffilelist'][lcfi
]['fcontents'], fpc
);
6681 os
.fsync(fpc
.fileno());
6682 except io
.UnsupportedOperation
:
6684 except AttributeError:
6686 except OSError as e
:
6688 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
6689 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
6690 if(preservepermissions
):
6691 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6693 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
6694 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6696 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6697 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
6698 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
6703 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
6704 funame
= userinfo
.pw_name
;
6713 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
6714 fgname
= groupinfo
.gr_name
;
6719 if(flinkinfo
['ftype']==0 or flinkinfo
['ftype']==7):
6720 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
6721 flinkinfo
['fcontents'].seek(0, 0);
6722 shutil
.copyfileobj(flinkinfo
['fcontents'], fpc
);
6725 os
.fsync(fpc
.fileno());
6726 except io
.UnsupportedOperation
:
6728 except AttributeError:
6730 except OSError as e
:
6732 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6733 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6734 if(preservepermissions
):
6735 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6737 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6738 if(flinkinfo
['ftype']==1):
6739 os
.link(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6740 if(flinkinfo
['ftype']==2):
6741 os
.symlink(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6742 if(flinkinfo
['ftype']==5):
6743 if(preservepermissions
):
6744 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6746 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6747 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6748 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6749 if(preservepermissions
):
6750 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6752 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6753 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
6754 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6756 os
.link(listcatfiles
['ffilelist'][lcfi
]['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6757 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6759 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6760 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
6761 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
6766 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
6767 funame
= userinfo
.pw_name
;
6776 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
6777 fgname
= groupinfo
.gr_name
;
6782 if(flinkinfo
['ftype']==0 or flinkinfo
['ftype']==7):
6783 with
open(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), "wb") as fpc
:
6784 flinkinfo
['fcontents'].seek(0, 0);
6785 shutil
.copyfileobj(flinkinfo
['fcontents'], fpc
);
6788 os
.fsync(fpc
.fileno());
6789 except io
.UnsupportedOperation
:
6791 except AttributeError:
6793 except OSError as e
:
6795 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6796 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6797 if(preservepermissions
):
6798 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6800 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6801 if(flinkinfo
['ftype']==1):
6802 os
.link(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6803 if(flinkinfo
['ftype']==2):
6804 os
.symlink(flinkinfo
['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6805 if(flinkinfo
['ftype']==5):
6806 if(preservepermissions
):
6807 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6809 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6810 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
6811 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fuid'], flinkinfo
['fgid']);
6812 if(preservepermissions
):
6813 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6815 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (flinkinfo
['fatime'], flinkinfo
['fmtime']));
6816 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
6817 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), flinkinfo
['fchmode']);
6819 os
.symlink(listcatfiles
['ffilelist'][lcfi
]['flinkname'], PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6820 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
6821 if(preservepermissions
):
6822 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6824 os
.mkdir(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']));
6825 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
6826 os
.chown(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
6827 if(preservepermissions
):
6828 os
.chmod(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6830 os
.utime(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
6831 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6 and hasattr(os
, "mkfifo")):
6832 os
.mkfifo(PrependPath(outdir
, listcatfiles
['ffilelist'][lcfi
]['fname']), listcatfiles
['ffilelist'][lcfi
]['fchmode']);
6835 return listcatfiles
['ffilelist']['catfp'];
6839 create_alias_function("UnPack", __file_format_name__
, "", UnPackArchiveFile
);
6841 if(hasattr(shutil
, "register_unpack_format")):
6842 def UnPackArchiveFileFunc(archive_name
, extract_dir
=None, **kwargs
):
6843 return UnPackArchiveFile(archive_name
, extract_dir
, False, 0, 0, False, __file_format_dict__
['format_delimiter'], False, False);
6844 create_alias_function("UnPack", __file_format_name__
, "Func", UnPackArchiveFileFunc
);
6846 def UnPackArchiveFileString(catstr
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
6847 formatspecs
= FormatSpecsListToDict(formatspecs
);
6848 catfp
= BytesIO(catstr
);
6849 listcatfiles
= UnPackArchiveFile(catfp
, outdir
, followlink
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6850 return listcatfiles
;
6852 create_alias_function("UnPack", __file_format_name__
, "String", UnPackArchiveFileString
);
6854 def ArchiveFileListFiles(infile
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
6855 formatspecs
= FormatSpecsListToDict(formatspecs
);
6856 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6857 if(isinstance(infile
, dict)):
6858 listcatfiles
= infile
;
6860 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
6861 infile
= RemoveWindowsPath(infile
);
6862 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, True, skipchecksum
, formatspecs
, returnfp
);
6863 if(not listcatfiles
):
6865 lenlist
= len(listcatfiles
['ffilelist']);
6866 fnumfiles
= int(listcatfiles
['fnumfiles']);
6868 lcfx
= int(listcatfiles
['fnumfiles']);
6869 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
6870 lcfx
= int(lenlist
);
6872 lcfx
= int(listcatfiles
['fnumfiles']);
6875 returnval
.update({lcfi
: listcatfiles
['ffilelist'][lcfi
]['fname']});
6877 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
6879 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' } };
6880 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'];
6881 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
6882 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " link to " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6883 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
6884 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " -> " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
6885 fuprint
= listcatfiles
['ffilelist'][lcfi
]['funame'];
6886 if(len(fuprint
)<=0):
6887 fuprint
= listcatfiles
['ffilelist'][lcfi
]['fuid'];
6888 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgname'];
6889 if(len(fgprint
)<=0):
6890 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgid'];
6891 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
));
6894 return listcatfiles
['catfp'];
6898 create_alias_function("", __file_format_name__
, "ListFiles", ArchiveFileListFiles
);
6900 def ArchiveFileStringListFiles(catstr
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
6901 formatspecs
= FormatSpecsListToDict(formatspecs
);
6902 catfp
= BytesIO(catstr
);
6903 listcatfiles
= ArchiveFileListFiles(catstr
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6904 return listcatfiles
;
6906 create_alias_function("", __file_format_name__
, "StringListFiles", ArchiveFileStringListFiles
);
6908 def TarFileListFiles(infile
, verbose
=False, returnfp
=False):
6909 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
6912 if(hasattr(sys
.stdin
, "buffer")):
6913 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
6915 shutil
.copyfileobj(sys
.stdin
, infile
);
6920 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
6921 infile
= download_file_from_internet_file(infile
);
6926 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
6928 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
6930 if(not tarfile
.is_tarfile(infile
)):
6932 except AttributeError:
6933 if(not is_tarfile(infile
)):
6938 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
6939 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
6941 tarfp
= tarfile
.open(infile
, "r");
6942 except FileNotFoundError
:
6946 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
6947 returnval
.update({lcfi
: member
.name
});
6948 fpremode
= member
.mode
;
6949 ffullmode
= member
.mode
;
6953 ffullmode
= member
.mode
+ stat
.S_IFREG
;
6955 elif(member
.isdev()):
6956 ffullmode
= member
.mode
;
6958 elif(member
.islnk()):
6959 ffullmode
= member
.mode
+ stat
.S_IFREG
;
6961 elif(member
.issym()):
6962 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
6964 elif(member
.ischr()):
6965 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
6967 elif(member
.isblk()):
6968 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
6970 elif(member
.isdir()):
6971 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
6973 elif(member
.isfifo()):
6974 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
6976 elif(member
.issparse()):
6977 ffullmode
= member
.mode
;
6980 VerbosePrintOut(member
.name
);
6982 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' } };
6983 printfname
= member
.name
;
6985 printfname
= member
.name
+ " link to " + member
.linkname
;
6986 elif(member
.issym()):
6987 printfname
= member
.name
+ " -> " + member
.linkname
;
6988 fuprint
= member
.uname
;
6989 if(len(fuprint
)<=0):
6990 fuprint
= member
.uid
;
6991 fgprint
= member
.gname
;
6992 if(len(fgprint
)<=0):
6993 fgprint
= member
.gid
;
6994 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
));
6997 return listcatfiles
['catfp'];
7001 def ZipFileListFiles(infile
, verbose
=False, returnfp
=False):
7002 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7005 if(hasattr(sys
.stdin
, "buffer")):
7006 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
7008 shutil
.copyfileobj(sys
.stdin
, infile
);
7013 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", str(infile
))):
7014 infile
= download_file_from_internet_file(infile
);
7019 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
7023 if(not zipfile
.is_zipfile(infile
)):
7026 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
7027 except FileNotFoundError
:
7031 ziptest
= zipfp
.testzip();
7033 VerbosePrintOut("Bad file found!");
7034 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
7035 if(zipinfo
.create_system
==0 or zipinfo
.create_system
==10):
7036 fwinattributes
= int(zipinfo
.external_attr
);
7037 if(not member
.is_dir()):
7038 fmode
= int(stat
.S_IFREG
+ 438);
7039 fchmode
= int(stat
.S_IMODE(fmode
));
7040 ftypemod
= int(stat
.S_IFMT(fmode
));
7041 elif(member
.is_dir()):
7042 fmode
= int(stat
.S_IFDIR
+ 511);
7043 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
7044 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
7045 elif(zipinfo
.create_system
==3):
7046 fwinattributes
=int(0);
7047 fmode
= int(zipinfo
.external_attr
);
7048 fchmode
= int(stat
.S_IMODE(fmode
));
7049 ftypemod
= int(stat
.S_IFMT(fmode
));
7051 fwinattributes
= int(0);
7052 if(not member
.is_dir()):
7053 fmode
= int(stat
.S_IFREG
+ 438);
7054 fchmode
= int(stat
.S_IMODE(fmode
));
7055 ftypemod
= int(stat
.S_IFMT(fmode
));
7056 elif(member
.is_dir()):
7057 fmode
= int(stat
.S_IFDIR
+ 511);
7058 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
7059 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
7060 returnval
.update({lcfi
: member
.filename
});
7062 VerbosePrintOut(member
.filename
);
7064 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' } };
7066 for fmodval
in str(oct(fmode
))[-3:]:
7067 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
7068 if(not member
.is_dir()):
7070 permissionstr
= "-" + permissionstr
;
7071 elif(member
.is_dir()):
7073 permissionstr
= "d" + permissionstr
;
7074 printfname
= member
.filename
;
7076 fuid
= int(os
.getuid());
7077 except AttributeError:
7082 fgid
= int(os
.getgid());
7083 except AttributeError:
7090 userinfo
= pwd
.getpwuid(os
.getuid());
7091 funame
= userinfo
.pw_name
;
7094 except AttributeError:
7102 groupinfo
= grp
.getgrgid(os
.getgid());
7103 fgname
= groupinfo
.gr_name
;
7106 except AttributeError:
7111 if(len(fuprint
)<=0):
7112 fuprint
= str(fuid
);
7114 if(len(fgprint
)<=0):
7115 fgprint
= str(fgid
);
7116 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
));
7119 return listcatfiles
['catfp'];
7123 if(not rarfile_support
):
7124 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
7125 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7126 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
7129 if(rarfile_support
):
7130 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
7131 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7132 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
7134 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
7138 rarfp
= rarfile
.RarFile(infile
, "r");
7139 rartest
= rarfp
.testrar();
7141 VerbosePrintOut("Bad file found!");
7142 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
7145 if(member
.host_os
==rarfile
.RAR_OS_UNIX
):
7148 member
.external_attr
7150 except AttributeError:
7152 elif(member
.host_os
==rarfile
.RAR_OS_WIN32
):
7155 member
.external_attr
7157 except AttributeError:
7162 if(is_unix
and member
.external_attr
!=0):
7163 fpremode
= int(member
.external_attr
);
7164 elif(member
.is_file()):
7165 fpremode
= int(stat
.S_IFREG
+ 438);
7166 elif(member
.is_symlink()):
7167 fpremode
= int(stat
.S_IFLNK
+ 438);
7168 elif(member
.is_dir()):
7169 fpremode
= int(stat
.S_IFDIR
+ 511);
7170 if(is_windows
and member
.external_attr
!=0):
7171 fwinattributes
= int(member
.external_attr
);
7173 fwinattributes
= int(0);
7174 if(is_unix
and member
.external_attr
!=0):
7175 fmode
= int(member
.external_attr
);
7176 fchmode
= int(stat
.S_IMODE(member
.external_attr
));
7177 ftypemod
= int(stat
.S_IFMT(member
.external_attr
));
7178 elif(member
.is_file()):
7179 fmode
= int(stat
.S_IFREG
+ 438);
7180 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
7181 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
7182 elif(member
.is_symlink()):
7183 fmode
= int(stat
.S_IFLNK
+ 438);
7184 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFLNK
+ 438)));
7185 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFLNK
+ 438)));
7186 elif(member
.is_dir()):
7187 fmode
= int(stat
.S_IFDIR
+ 511);
7188 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
7189 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
7190 returnval
.update({lcfi
: member
.filename
});
7192 VerbosePrintOut(member
.filename
);
7194 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' } };
7196 for fmodval
in str(oct(fmode
))[-3:]:
7197 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
7198 if(member
.is_file()):
7200 permissionstr
= "-" + permissionstr
;
7201 printfname
= member
.filename
;
7202 elif(member
.is_symlink()):
7204 permissionstr
= "l" + permissionstr
;
7205 printfname
= member
.name
+ " -> " + member
.read().decode("UTF-8");
7206 elif(member
.is_dir()):
7208 permissionstr
= "d" + permissionstr
;
7209 printfname
= member
.filename
;
7211 fuid
= int(os
.getuid());
7212 except AttributeError:
7217 fgid
= int(os
.getgid());
7218 except AttributeError:
7225 userinfo
= pwd
.getpwuid(os
.getuid());
7226 funame
= userinfo
.pw_name
;
7229 except AttributeError:
7237 groupinfo
= grp
.getgrgid(os
.getgid());
7238 fgname
= groupinfo
.gr_name
;
7241 except AttributeError:
7246 if(len(fuprint
)<=0):
7247 fuprint
= str(fuid
);
7249 if(len(fgprint
)<=0):
7250 fgprint
= str(fgid
);
7251 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(member
.file_size
).rjust(15) + " " + member
.mtime
.strftime('%Y-%m-%d %H:%M') + " " + printfname
));
7254 return listcatfiles
['catfp'];
7258 if(not py7zr_support
):
7259 def SevenZipFileListFiles(infile
, verbose
=False, returnfp
=False):
7260 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7261 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
7265 def SevenZipFileListFiles(infile
, verbose
=False, returnfp
=False):
7266 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7267 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
7271 szpfp
= py7zr
.SevenZipFile(infile
, mode
="r");
7272 file_content
= szpfp
.readall();
7273 #sztest = szpfp.testzip();
7274 sztestalt
= szpfp
.test();
7276 VerbosePrintOut("Bad file found!");
7277 for member
in sorted(szpfp
.list(), key
=lambda x
: x
.filename
):
7278 if(re
.findall("^[.|/]", member
.filename
)):
7279 fname
= member
.filename
;
7281 fname
= "./"+member
.filename
;
7282 if(not member
.is_directory
):
7283 fpremode
= int(stat
.S_IFREG
+ 438);
7284 elif(member
.is_directory
):
7285 fpremode
= int(stat
.S_IFDIR
+ 511);
7286 fwinattributes
= int(0);
7287 if(member
.is_directory
):
7288 fmode
= int(stat
.S_IFDIR
+ 511);
7289 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
7290 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
7292 fmode
= int(stat
.S_IFLNK
+ 438);
7293 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFLNK
+ 438)));
7294 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFLNK
+ 438)));
7295 returnval
.update({lcfi
: member
.filename
});
7297 VerbosePrintOut(member
.filename
);
7299 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' } };
7301 for fmodval
in str(oct(fmode
))[-3:]:
7302 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
7304 if(not member
.is_directory
):
7306 permissionstr
= "-" + permissionstr
;
7307 printfname
= member
.filename
;
7308 elif(member
.is_directory
):
7310 permissionstr
= "d" + permissionstr
;
7311 printfname
= member
.filename
;
7313 fsize
= len(file_content
[member
.filename
].read());
7314 file_content
[member
.filename
].close();
7316 fuid
= int(os
.getuid());
7317 except AttributeError:
7322 fgid
= int(os
.getgid());
7323 except AttributeError:
7330 userinfo
= pwd
.getpwuid(os
.getuid());
7331 funame
= userinfo
.pw_name
;
7334 except AttributeError:
7342 groupinfo
= grp
.getgrgid(os
.getgid());
7343 fgname
= groupinfo
.gr_name
;
7346 except AttributeError:
7351 if(len(fuprint
)<=0):
7352 fuprint
= str(fuid
);
7354 if(len(fgprint
)<=0):
7355 fgprint
= str(fgid
);
7356 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(fsize
).rjust(15) + " " + member
.creationtime
.strftime('%Y-%m-%d %H:%M') + " " + printfname
));
7359 return listcatfiles
['catfp'];
7363 def InFileListFiles(infile
, verbose
=False, formatspecs
=__file_format_dict__
, returnfp
=False):
7364 formatspecs
= FormatSpecsListToDict(formatspecs
);
7365 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
7366 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
, True);
7367 if(checkcompressfile
=="tarfile"):
7368 return TarFileListFiles(infile
, verbose
, returnfp
);
7369 elif(checkcompressfile
=="zipfile"):
7370 return ZipFileListFiles(infile
, verbose
, returnfp
);
7371 elif(checkcompressfile
=="catfile"):
7372 return ArchiveFileListFiles(infile
, 0, 0, False, formatspecs
, verbose
, returnfp
);
7373 elif(rarfile_support
and checkcompressfile
=="rarfile"):
7374 return RarFileListFiles(infile
, verbose
, returnfp
);
7375 elif(py7zr_support
and checkcompressfile
=="7zipfile"):
7376 return SevenZipFileListFiles(infile
, verbose
, returnfp
);
7381 def ListDirListFiles(infiles
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, checksumtype
="crc32", formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
7382 formatspecs
= FormatSpecsListToDict(formatspecs
);
7383 outarray
= BytesIO();
7384 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compresswholefile
, compressionlevel
, followlink
, checksumtype
, formatspecs
, False, True);
7385 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
7386 return listcatfiles
;
7388 def ListDirListFilesAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=False, seekstart
=0, seekend
=0, skipchecksum
=False, checksumtype
="crc32", formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
7389 formatspecs
= FormatSpecsListToDict(formatspecs
);
7390 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, formatspecs
, verbose
);
7391 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
7392 return listcatfiles
;
7394 def PackArchiveFileFromListDirAlt(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compresswholefile
=True, compressionlevel
=None, followlink
=False, skipchecksum
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
7395 formatspecs
= FormatSpecsListToDict(formatspecs
);
7396 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, False, checksumtype
, extradata
, formatspecs
, False);
7397 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compresswholefile
, compressionlevel
, followlink
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
7398 return listcatfiles
;
7400 create_alias_function("Pack", __file_format_name__
, "FromListDirAlt", PackArchiveFileFromListDirAlt
);
7402 def PackArchiveFileFromTarFileAlt(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
7403 formatspecs
= FormatSpecsListToDict(formatspecs
);
7404 outarray
= TarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
7405 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compresswholefile
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
7406 return listcatfiles
;
7408 create_alias_function("Pack", __file_format_name__
, "FromTarFileAlt", PackArchiveFileFromTarFileAlt
);
7410 def PackArchiveFileFromZipFileAlt(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
7411 formatspecs
= FormatSpecsListToDict(formatspecs
);
7412 outarray
= ZipFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
7413 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compresswholefile
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
7414 return listcatfiles
;
7416 create_alias_function("Pack", __file_format_name__
, "FromZipFileAlt", PackArchiveFileFromZipFileAlt
);
7418 if(not rarfile_support
):
7419 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
7422 if(rarfile_support
):
7423 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
7424 formatspecs
= FormatSpecsListToDict(formatspecs
);
7425 outarray
= RarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
7426 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compresswholefile
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
7427 return listcatfiles
;
7429 create_alias_function("Pack", __file_format_name__
, "FromRarFileAlt", PackArchiveFileFromRarFileAlt
);
7431 if(not py7zr_support
):
7432 def PackArchiveFileFromSevenZipFileAlt(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
7436 def PackArchiveFileFromSevenZipFileAlt(infile
, outfile
, compression
="auto", compresswholefile
=True, compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_dict__
, verbose
=False, returnfp
=False):
7437 formatspecs
= FormatSpecsListToDict(formatspecs
);
7438 outarray
= SevenZipFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
7439 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compresswholefile
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
7440 return listcatfiles
;
7442 create_alias_function("Pack", __file_format_name__
, "FromSevenZipFileAlt", PackArchiveFileFromSevenZipFileAlt
);
7444 def download_file_from_ftp_file(url
):
7445 urlparts
= urlparse(url
);
7446 file_name
= os
.path
.basename(urlparts
.path
);
7447 file_dir
= os
.path
.dirname(urlparts
.path
);
7448 if(urlparts
.username
is not None):
7449 ftp_username
= urlparts
.username
;
7451 ftp_username
= "anonymous";
7452 if(urlparts
.password
is not None):
7453 ftp_password
= urlparts
.password
;
7454 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7455 ftp_password
= "anonymous";
7458 if(urlparts
.scheme
=="ftp"):
7460 elif(urlparts
.scheme
=="ftps" and ftpssl
):
7464 if(urlparts
.scheme
=="sftp"):
7466 return download_file_from_pysftp_file(url
);
7468 return download_file_from_sftp_file(url
);
7469 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7470 return download_file_from_http_file(url
);
7471 ftp_port
= urlparts
.port
;
7472 if(urlparts
.port
is None):
7475 ftp
.connect(urlparts
.hostname
, ftp_port
);
7476 except socket
.gaierror
:
7477 log
.info("Error With URL "+url
);
7479 except socket
.timeout
:
7480 log
.info("Error With URL "+url
);
7482 ftp
.login(urlparts
.username
, urlparts
.password
);
7483 if(urlparts
.scheme
=="ftps"):
7485 ftpfile
= BytesIO();
7486 ftp
.retrbinary("RETR "+urlparts
.path
, ftpfile
.write
);
7487 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
7492 def download_file_from_ftp_string(url
):
7493 ftpfile
= download_file_from_ftp_file(url
);
7494 return ftpfile
.read();
7496 def upload_file_to_ftp_file(ftpfile
, url
):
7497 urlparts
= urlparse(url
);
7498 file_name
= os
.path
.basename(urlparts
.path
);
7499 file_dir
= os
.path
.dirname(urlparts
.path
);
7500 if(urlparts
.username
is not None):
7501 ftp_username
= urlparts
.username
;
7503 ftp_username
= "anonymous";
7504 if(urlparts
.password
is not None):
7505 ftp_password
= urlparts
.password
;
7506 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7507 ftp_password
= "anonymous";
7510 if(urlparts
.scheme
=="ftp"):
7512 elif(urlparts
.scheme
=="ftps" and ftpssl
):
7516 if(urlparts
.scheme
=="sftp"):
7518 return upload_file_to_pysftp_file(url
);
7520 return upload_file_to_sftp_file(url
);
7521 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7523 ftp_port
= urlparts
.port
;
7524 if(urlparts
.port
is None):
7527 ftp
.connect(urlparts
.hostname
, ftp_port
);
7528 except socket
.gaierror
:
7529 log
.info("Error With URL "+url
);
7531 except socket
.timeout
:
7532 log
.info("Error With URL "+url
);
7534 ftp
.login(urlparts
.username
, urlparts
.password
);
7535 if(urlparts
.scheme
=="ftps"):
7537 ftp
.storbinary("STOR "+urlparts
.path
, ftpfile
);
7542 def upload_file_to_ftp_string(ftpstring
, url
):
7543 ftpfileo
= BytesIO(ftpstring
);
7544 ftpfile
= upload_file_to_ftp_file(ftpfileo
, url
);
7548 def download_file_from_http_file(url
, headers
=geturls_headers_pycatfile_python_alt
):
7549 # Parse the URL to extract username and password if present
7550 urlparts
= urlparse(url
);
7551 username
= urlparts
.username
;
7552 password
= urlparts
.password
;
7553 # Rebuild the URL without the username and password
7554 netloc
= urlparts
.hostname
;
7555 if(urlparts
.scheme
=="sftp"):
7557 return download_file_from_pysftp_file(url
);
7559 return download_file_from_sftp_file(url
);
7560 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7561 return download_file_from_ftp_file(url
);
7563 netloc
+= ':' + str(urlparts
.port
);
7564 rebuilt_url
= urlunparse((urlparts
.scheme
, netloc
, urlparts
.path
, urlparts
.params
, urlparts
.query
, urlparts
.fragment
));
7565 # Create a temporary file object
7566 httpfile
= BytesIO();
7568 # Use the requests library if available
7569 if username
and password
:
7570 response
= requests
.get(rebuilt_url
, headers
=headers
, auth
=(username
, password
), stream
=True);
7572 response
= requests
.get(rebuilt_url
, headers
=headers
, stream
=True);
7573 response
.raw
.decode_content
= True
7574 shutil
.copyfileobj(response
.raw
, httpfile
);
7576 # Build a Request object for urllib
7577 request
= Request(rebuilt_url
, headers
=headers
);
7578 # Create an opener object for handling URLs
7579 if username
and password
:
7580 # Create a password manager
7581 password_mgr
= HTTPPasswordMgrWithDefaultRealm();
7582 # Add the username and password
7583 password_mgr
.add_password(None, rebuilt_url
, username
, password
);
7584 # Create an authentication handler using the password manager
7585 auth_handler
= HTTPBasicAuthHandler(password_mgr
);
7586 # Build the opener with the authentication handler
7587 opener
= build_opener(auth_handler
);
7589 opener
= build_opener();
7590 with opener
.open(request
) as response
:
7591 shutil
.copyfileobj(response
, httpfile
);
7592 # Reset file pointer to the start
7593 httpfile
.seek(0, 0);
7594 # Return the temporary file object
7597 def download_file_from_http_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
7598 httpfile
= download_file_from_http_file(url
, headers
);
7599 return ftpfile
.read();
7602 def download_file_from_sftp_file(url
):
7603 urlparts
= urlparse(url
);
7604 file_name
= os
.path
.basename(urlparts
.path
);
7605 file_dir
= os
.path
.dirname(urlparts
.path
);
7606 sftp_port
= urlparts
.port
;
7607 if(urlparts
.port
is None):
7610 sftp_port
= urlparts
.port
;
7611 if(urlparts
.username
is not None):
7612 sftp_username
= urlparts
.username
;
7614 sftp_username
= "anonymous";
7615 if(urlparts
.password
is not None):
7616 sftp_password
= urlparts
.password
;
7617 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7618 sftp_password
= "anonymous";
7621 if(urlparts
.scheme
=="ftp"):
7622 return download_file_from_ftp_file(url
);
7623 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7624 return download_file_from_http_file(url
);
7625 if(urlparts
.scheme
!="sftp"):
7627 ssh
= paramiko
.SSHClient();
7628 ssh
.load_system_host_keys();
7629 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
7631 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7632 except paramiko
.ssh_exception
.SSHException
:
7634 except socket
.gaierror
:
7635 log
.info("Error With URL "+url
);
7637 except socket
.timeout
:
7638 log
.info("Error With URL "+url
);
7640 sftp
= ssh
.open_sftp();
7641 sftpfile
= BytesIO();
7642 sftp
.getfo(urlparts
.path
, sftpfile
);
7645 sftpfile
.seek(0, 0);
7648 def download_file_from_sftp_file(url
):
7652 def download_file_from_sftp_string(url
):
7653 sftpfile
= download_file_from_sftp_file(url
);
7654 return sftpfile
.read();
7656 def download_file_from_ftp_string(url
):
7660 def upload_file_to_sftp_file(sftpfile
, url
):
7661 urlparts
= urlparse(url
);
7662 file_name
= os
.path
.basename(urlparts
.path
);
7663 file_dir
= os
.path
.dirname(urlparts
.path
);
7664 sftp_port
= urlparts
.port
;
7665 if(urlparts
.port
is None):
7668 sftp_port
= urlparts
.port
;
7669 if(urlparts
.username
is not None):
7670 sftp_username
= urlparts
.username
;
7672 sftp_username
= "anonymous";
7673 if(urlparts
.password
is not None):
7674 sftp_password
= urlparts
.password
;
7675 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7676 sftp_password
= "anonymous";
7679 if(urlparts
.scheme
=="ftp"):
7680 return upload_file_to_ftp_file(url
);
7681 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7683 if(urlparts
.scheme
!="sftp"):
7685 ssh
= paramiko
.SSHClient();
7686 ssh
.load_system_host_keys();
7687 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
7689 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7690 except paramiko
.ssh_exception
.SSHException
:
7692 except socket
.gaierror
:
7693 log
.info("Error With URL "+url
);
7695 except socket
.timeout
:
7696 log
.info("Error With URL "+url
);
7698 sftp
= ssh
.open_sftp();
7699 sftp
.putfo(sftpfile
, urlparts
.path
);
7702 sftpfile
.seek(0, 0);
7705 def upload_file_to_sftp_file(sftpfile
, url
):
7709 def upload_file_to_sftp_string(sftpstring
, url
):
7710 sftpfileo
= BytesIO(sftpstring
);
7711 sftpfile
= upload_file_to_sftp_files(ftpfileo
, url
);
7715 def upload_file_to_sftp_string(url
):
7719 def download_file_from_pysftp_file(url
):
7720 urlparts
= urlparse(url
);
7721 file_name
= os
.path
.basename(urlparts
.path
);
7722 file_dir
= os
.path
.dirname(urlparts
.path
);
7723 sftp_port
= urlparts
.port
;
7724 if(urlparts
.port
is None):
7727 sftp_port
= urlparts
.port
;
7728 if(urlparts
.username
is not None):
7729 sftp_username
= urlparts
.username
;
7731 sftp_username
= "anonymous";
7732 if(urlparts
.password
is not None):
7733 sftp_password
= urlparts
.password
;
7734 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7735 sftp_password
= "anonymous";
7738 if(urlparts
.scheme
=="ftp"):
7739 return download_file_from_ftp_file(url
);
7740 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7741 return download_file_from_http_file(url
);
7742 if(urlparts
.scheme
!="sftp"):
7745 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7746 except paramiko
.ssh_exception
.SSHException
:
7748 except socket
.gaierror
:
7749 log
.info("Error With URL "+url
);
7751 except socket
.timeout
:
7752 log
.info("Error With URL "+url
);
7754 sftp
= ssh
.open_sftp();
7755 sftpfile
= BytesIO();
7756 sftp
.getfo(urlparts
.path
, sftpfile
);
7759 sftpfile
.seek(0, 0);
7762 def download_file_from_pysftp_file(url
):
7766 def download_file_from_pysftp_string(url
):
7767 sftpfile
= download_file_from_pysftp_file(url
);
7768 return sftpfile
.read();
7770 def download_file_from_ftp_string(url
):
7774 def upload_file_to_pysftp_file(sftpfile
, url
):
7775 urlparts
= urlparse(url
);
7776 file_name
= os
.path
.basename(urlparts
.path
);
7777 file_dir
= os
.path
.dirname(urlparts
.path
);
7778 sftp_port
= urlparts
.port
;
7779 if(urlparts
.port
is None):
7782 sftp_port
= urlparts
.port
;
7783 if(urlparts
.username
is not None):
7784 sftp_username
= urlparts
.username
;
7786 sftp_username
= "anonymous";
7787 if(urlparts
.password
is not None):
7788 sftp_password
= urlparts
.password
;
7789 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
7790 sftp_password
= "anonymous";
7793 if(urlparts
.scheme
=="ftp"):
7794 return upload_file_to_ftp_file(url
);
7795 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7797 if(urlparts
.scheme
!="sftp"):
7800 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
7801 except paramiko
.ssh_exception
.SSHException
:
7803 except socket
.gaierror
:
7804 log
.info("Error With URL "+url
);
7806 except socket
.timeout
:
7807 log
.info("Error With URL "+url
);
7809 sftp
= ssh
.open_sftp();
7810 sftp
.putfo(sftpfile
, urlparts
.path
);
7813 sftpfile
.seek(0, 0);
7816 def upload_file_to_pysftp_file(sftpfile
, url
):
7820 def upload_file_to_pysftp_string(sftpstring
, url
):
7821 sftpfileo
= BytesIO(sftpstring
);
7822 sftpfile
= upload_file_to_pysftp_files(ftpfileo
, url
);
7826 def upload_file_to_pysftp_string(url
):
7829 def download_file_from_internet_file(url
, headers
=geturls_headers_pycatfile_python_alt
):
7830 urlparts
= urlparse(url
);
7831 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7832 return download_file_from_http_file(url
, headers
);
7833 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7834 return download_file_from_ftp_file(url
);
7835 elif(urlparts
.scheme
=="sftp"):
7836 if(__use_pysftp__
and havepysftp
):
7837 return download_file_from_pysftp_file(url
);
7839 return download_file_from_sftp_file(url
);
7844 def download_file_from_internet_uncompress_file(url
, headers
=geturls_headers_pycatfile_python_alt
, formatspecs
=__file_format_dict__
):
7845 formatspecs
= FormatSpecsListToDict(formatspecs
);
7846 fp
= download_file_from_internet_file(url
);
7847 fp
= UncompressArchiveFile(fp
, formatspecs
);
7853 def download_file_from_internet_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
7854 urlparts
= urlparse(url
);
7855 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7856 return download_file_from_http_string(url
, headers
);
7857 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7858 return download_file_from_ftp_string(url
);
7859 elif(urlparts
.scheme
=="sftp"):
7860 if(__use_pysftp__
and havepysftp
):
7861 return download_file_from_pysftp_string(url
);
7863 return download_file_from_sftp_string(url
);
7868 def download_file_from_internet_uncompress_string(url
, headers
=geturls_headers_pycatfile_python_alt
, formatspecs
=__file_format_dict__
):
7869 formatspecs
= FormatSpecsListToDict(formatspecs
);
7870 fp
= download_file_from_internet_string(url
);
7871 fp
= UncompressArchiveFile(fp
, formatspecs
);
7877 def upload_file_to_internet_file(ifp
, url
):
7878 urlparts
= urlparse(url
);
7879 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7881 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7882 return upload_file_to_ftp_file(ifp
, url
);
7883 elif(urlparts
.scheme
=="sftp"):
7884 if(__use_pysftp__
and havepysftp
):
7885 return upload_file_to_pysftp_file(ifp
, url
);
7887 return upload_file_to_sftp_file(ifp
, url
);
7892 def upload_file_to_internet_compress_file(ifp
, url
, formatspecs
=__file_format_dict__
):
7893 formatspecs
= FormatSpecsListToDict(formatspecs
);
7894 catfp
= CompressArchiveFile(catfp
, compression
, compressionlevel
, formatspecs
);
7898 upload_file_to_internet_file(catfp
, outfile
);
7901 def upload_file_to_internet_string(ifp
, url
):
7902 urlparts
= urlparse(url
);
7903 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
7905 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
7906 return upload_file_to_ftp_string(ifp
, url
);
7907 elif(urlparts
.scheme
=="sftp"):
7908 if(__use_pysftp__
and havepysftp
):
7909 return upload_file_to_pysftp_string(ifp
, url
);
7911 return upload_file_to_sftp_string(ifp
, url
);
7916 def upload_file_to_internet_compress_string(ifp
, url
, formatspecs
=__file_format_dict__
):
7917 formatspecs
= FormatSpecsListToDict(formatspecs
);
7918 catfp
= CompressArchiveFile(BytesIO(ifp
), compression
, compressionlevel
, formatspecs
);
7922 upload_file_to_internet_file(catfp
, outfile
);
7926 if(hasattr(shutil
, "register_archive_format")):
7927 # Register the packing format
7928 shutil
.register_archive_format(__file_format_name__
, PackArchiveFileFunc
, description
='Pack concatenated files');
7929 except shutil
.RegistryError
:
7933 if(hasattr(shutil
, "register_unpack_format")):
7934 # Register the unpacking format
7935 shutil
.register_unpack_format(__file_format_name__
, archivefile_extensions
, UnPackArchiveFileFunc
, description
='UnPack concatenated files');
7936 except shutil
.RegistryError
: