2 # -*- coding: utf-8 -*-
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the Revised BSD License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 Revised BSD License for more details.
13 Copyright 2018-2024 Cool Dude 2k - http://idb.berlios.de/
14 Copyright 2018-2024 Game Maker 2k - http://intdb.sourceforge.net/
15 Copyright 2018-2024 Kazuki Przyborowski - https://github.com/KazukiPrzyborowski
17 $FileInfo: pycatfile.py - Last Update: 3/15/2024 Ver. 0.5.0 RC 1 - Author: cooldude2k $
20 from __future__
import absolute_import
, division
, print_function
, unicode_literals
;
21 import io
, os
, re
, sys
, time
, stat
, zlib
, base64
, shutil
, socket
, hashlib
, datetime
, logging
, binascii
, tempfile
, zipfile
, ftplib
, platform
;
22 from ftplib
import FTP
, FTP_TLS
;
23 if(sys
.version
[0]=="2"):
24 from urlparse
import urlparse
, urlunparse
, urlsplit
, urlunsplit
, urljoin
;
25 elif(sys
.version
[0]>="3"):
26 from urllib
.parse
import urlunparse
, urlsplit
, urlunsplit
, urljoin
, urlencode
;
27 from urllib
.parse
import urlparse
;
29 if os
.name
== 'nt': # Only modify if on Windows
30 if sys
.version
[0] == "2":
32 sys
.stdout
= codecs
.getwriter('utf-8')(sys
.stdout
);
33 sys
.stderr
= codecs
.getwriter('utf-8')(sys
.stderr
);
36 sys
.stdout
= io
.TextIOWrapper(sys
.stdout
.buffer, encoding
='utf-8', errors
='replace', line_buffering
=True);
37 sys
.stderr
= io
.TextIOWrapper(sys
.stderr
.buffer, encoding
='utf-8', errors
='replace', line_buffering
=True);
39 hashlib_guaranteed
= False;
40 os
.environ
["PYTHONIOENCODING"] = "UTF-8";
41 os
.environ
["LC_CTYPE"] = "UTF-8";
45 sys
.setdefaultencoding('UTF-8');
48 except AttributeError:
52 except AttributeError:
56 import simplejson
as json
;
61 from zlib
import crc32
;
63 from binascii
import crc32
;
65 rarfile_support
= False;
68 rarfile_support
= True;
70 rarfile_support
= False;
73 from safetar
import is_tarfile
;
76 from xtarfile
import is_tarfile
;
78 from tarfile
import is_tarfile
;
81 import safetar
as tarfile
;
84 import xtarfile
as tarfile
;
102 haverequests
= False;
107 haverequests
= False;
111 logging
.getLogger("urllib3").setLevel(logging
.WARNING
);
115 from urllib
.request
import Request
, build_opener
, HTTPBasicAuthHandler
;
116 from urllib
.parse
import urlparse
;
119 from urllib2
import Request
, build_opener
, HTTPBasicAuthHandler
;
120 from urlparse
import urlparse
;
122 if(sys
.version
[0]=="2"):
124 from io
import StringIO
, BytesIO
;
127 from cStringIO
import StringIO
;
128 from cStringIO
import StringIO
as BytesIO
;
130 from StringIO
import StringIO
;
131 from StringIO
import StringIO
as BytesIO
;
132 elif(sys
.version
[0]>="3"):
133 from io
import StringIO
, BytesIO
;
138 from cStringIO
import StringIO
as BytesIO
;
144 from StringIO
import StringIO
as BytesIO
;
150 from io
import BytesIO
;
155 __use_pysftp__
= False;
157 __use_pysftp__
= False;
158 __file_format_name__
= "CatFile";
159 __program_name__
= "Py"+__file_format_name__
;
160 __file_format_lower__
= __file_format_name__
.lower();
161 __file_format_magic__
= __file_format_name__
;
162 __file_format_len__
= len(__file_format_magic__
);
163 __file_format_hex__
= binascii
.hexlify(__file_format_magic__
.encode("UTF-8")).decode("UTF-8");
164 __file_format_delimiter__
= "\x00";
165 __file_format_ver__
= "001";
166 __use_new_style__
= True;
167 __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__
];
168 __project__
= __program_name__
;
169 __project_url__
= "https://github.com/GameMaker2k/PyCatFile";
170 __version_info__
= (0, 5, 0, "RC 1", 1);
171 __version_date_info__
= (2024, 3, 15, "RC 1", 1);
172 __version_date__
= str(__version_date_info__
[0]) + "." + str(__version_date_info__
[1]).zfill(2) + "." + str(__version_date_info__
[2]).zfill(2);
173 __revision__
= __version_info__
[3];
174 __revision_id__
= "$Id$";
175 if(__version_info__
[4] is not None):
176 __version_date_plusrc__
= __version_date__
+ "-" + str(__version_date_info__
[4]);
177 if(__version_info__
[4] is None):
178 __version_date_plusrc__
= __version_date__
;
179 if(__version_info__
[3] is not None):
180 __version__
= str(__version_info__
[0]) + "." + str(__version_info__
[1]) + "." + str(__version_info__
[2]) + " " + str(__version_info__
[3]);
181 if(__version_info__
[3] is None):
182 __version__
= str(__version_info__
[0]) + "." + str(__version_info__
[1]) + "." + str(__version_info__
[2]);
184 PyBitness
= platform
.architecture();
185 if(PyBitness
=="32bit" or PyBitness
=="32"):
187 elif(PyBitness
=="64bit" or PyBitness
=="64"):
192 geturls_ua_pycatfile_python
= "Mozilla/5.0 (compatible; {proname}/{prover}; +{prourl})".format(proname
=__project__
, prover
=__version__
, prourl
=__project_url__
);
193 if(platform
.python_implementation()!=""):
194 py_implementation
= platform
.python_implementation();
195 if(platform
.python_implementation()==""):
196 py_implementation
= "Python";
197 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__
);
198 geturls_ua_googlebot_google
= "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
199 geturls_ua_googlebot_google_old
= "Googlebot/2.1 (+http://www.google.com/bot.html)";
200 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
)};
201 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
)};
202 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"};
203 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"};
205 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'xz', 'lz4', 'lzo', 'lzma'];
206 outextlist
= ['gz', 'bz2', 'zst', 'xz', 'lz4', 'lzo', 'lzma'];
207 outextlistwd
= ['.gz', '.bz2', '.zst', '.xz', '.lz4', '.lzo', '.lzma'];
209 tarfile_mimetype
= "application/tar";
210 tarfile_tar_mimetype
= tarfile_mimetype
;
211 zipfile_mimetype
= "application/zip";
212 zipfile_zip_mimetype
= zipfile_mimetype
;
213 rarfile_mimetype
= "application/rar";
214 rarfile_rar_mimetype
= rarfile_mimetype
;
215 archivefile_mimetype
= "application/x-"+__file_format_list__
[1]+"";
216 archivefile_cat_mimetype
= archivefile_mimetype
;
217 archivefile_gzip_mimetype
= "application/x-"+__file_format_list__
[1]+"+gzip";
218 archivefile_gz_mimetype
= archivefile_gzip_mimetype
;
219 archivefile_bzip2_mimetype
= "application/x-"+__file_format_list__
[1]+"+bzip2";
220 archivefile_bz2_mimetype
= archivefile_bzip2_mimetype
;
221 archivefile_lz4_mimetype
= "application/x-"+__file_format_list__
[1]+"+lz4";
222 archivefile_lzop_mimetype
= "application/x-"+__file_format_list__
[1]+"+lzop";
223 archivefile_lzo_mimetype
= archivefile_lzop_mimetype
;
224 archivefile_zstandard_mimetype
= "application/x-"+__file_format_list__
[1]+"+zstandard";
225 archivefile_zstd_mimetype
= archivefile_zstandard_mimetype
;
226 archivefile_lzma_mimetype
= "application/x-"+__file_format_list__
[1]+"+lzma";
227 archivefile_xz_mimetype
= "application/x-"+__file_format_list__
[1]+"+xz";
228 archivefile_extensions
= ['.cat', '.cat.gz', '.cat.bz2', '.cat.zst', '.cat.lz4', '.cat.lzo', '.cat.lzop', '.cat.lzma', '.cat.xz'];
230 if __name__
== "__main__":
232 curscrpath
= os
.path
.dirname(sys
.argv
[0]);
236 curscrpath
= curscrpath
.replace(os
.sep
, "/");
237 curscrpath
= curscrpath
+ "/";
238 scrfile
= curscrpath
+ "catfile.py";
239 if(os
.path
.exists(scrfile
) and os
.path
.isfile(scrfile
)):
240 scrcmd
= subprocess
.Popen([sys
.executable
, scrfile
] + sys
.argv
[1:]);
243 def VerbosePrintOut(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
249 "warning": logging
.warning
,
250 "error": logging
.error
,
251 "critical": logging
.critical
,
252 "exception": logging
.exception
,
253 "logalt": lambda x
: logging
.log(dgblevel
, x
),
254 "debug": logging
.debug
256 log_function
= log_functions
.get(outtype
);
258 log_function(dbgtxt
);
262 def VerbosePrintOutReturn(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
263 VerbosePrintOut(dbgtxt
, outtype
, dbgenable
, dgblevel
);
266 def RemoveWindowsPath(dpath
):
270 dpath
= dpath
.replace(os
.path
.sep
, "/");
271 dpath
= dpath
.rstrip("/");
272 if(dpath
=="." or dpath
==".."):
276 def NormalizeRelativePath(inpath
):
277 inpath
= RemoveWindowsPath(inpath
);
278 if(os
.path
.isabs(inpath
)):
281 if(inpath
.startswith("./") or inpath
.startswith("../")):
284 outpath
= "./" + inpath
;
287 def ListDir(dirpath
, followlink
=False, duplicates
=False):
288 if(isinstance(dirpath
, (list, tuple, ))):
289 dirpath
= list(filter(None, dirpath
));
290 elif(isinstance(dirpath
, (str, ))):
291 dirpath
= list(filter(None, [dirpath
]));
293 for mydirfile
in dirpath
:
294 if(not os
.path
.exists(mydirfile
)):
296 mydirfile
= NormalizeRelativePath(mydirfile
);
297 if(os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
)):
298 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
));
299 if(os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
)):
300 for root
, dirs
, filenames
in os
.walk(mydirfile
):
302 dpath
= RemoveWindowsPath(dpath
);
303 if(dpath
not in retlist
and not duplicates
):
304 retlist
.append(dpath
);
306 retlist
.append(dpath
);
307 for file in filenames
:
308 fpath
= os
.path
.join(root
, file);
309 fpath
= RemoveWindowsPath(fpath
);
310 if(fpath
not in retlist
and not duplicates
):
311 retlist
.append(fpath
);
313 retlist
.append(fpath
);
315 retlist
.append(RemoveWindowsPath(mydirfile
));
318 def ListDirAdvanced(dirpath
, followlink
=False, duplicates
=False):
319 if isinstance(dirpath
, (list, tuple)):
320 dirpath
= list(filter(None, dirpath
));
321 elif isinstance(dirpath
, str):
322 dirpath
= list(filter(None, [dirpath
]));
324 for mydirfile
in dirpath
:
325 if not os
.path
.exists(mydirfile
):
327 mydirfile
= NormalizeRelativePath(mydirfile
);
328 if os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
) and followlink
:
329 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
))
330 if os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
):
331 for root
, dirs
, filenames
in os
.walk(mydirfile
):
332 # Sort dirs and filenames alphabetically in place
333 dirs
.sort(key
=lambda x
: x
.lower());
334 filenames
.sort(key
=lambda x
: x
.lower());
335 dpath
= RemoveWindowsPath(root
);
336 if not duplicates
and dpath
not in retlist
:
337 retlist
.append(dpath
);
339 retlist
.append(dpath
);
340 for file in filenames
:
341 fpath
= os
.path
.join(root
, file);
342 fpath
= RemoveWindowsPath(fpath
);
343 if not duplicates
and fpath
not in retlist
:
344 retlist
.append(fpath
);
346 retlist
.append(fpath
);
348 retlist
.append(RemoveWindowsPath(mydirfile
));
351 def create_alias_function(prefix
, base_name
, suffix
, target_function
):
352 # Define a new function that wraps the target function
353 def alias_function(*args
, **kwargs
):
354 return target_function(*args
, **kwargs
);
356 # Create the function name by combining the prefix, base name, and the suffix
357 function_name
= "{}{}{}".format(prefix
, base_name
, suffix
);
359 # Add the new function to the global namespace
360 globals()[function_name
] = alias_function
;
362 # initial_value can be 0xFFFF or 0x0000
363 def crc16_ansi(msg
, initial_value
=0xFFFF):
364 # CRC-16-IBM / CRC-16-ANSI polynomial and initial value
365 poly
= 0x8005; # Polynomial for CRC-16-IBM / CRC-16-ANSI
366 crc
= initial_value
; # Initial value
368 crc ^
= b
<< 8; # XOR byte into CRC top byte
369 for _
in range(8): # Process each bit
370 if crc
& 0x8000: # If the top bit is set
371 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
373 crc
= crc
<< 1; # Just shift left
374 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
377 # initial_value can be 0xFFFF or 0x0000
378 def crc16_ibm(msg
, initial_value
=0xFFFF):
379 return crc16_ansi(msg
, initial_value
);
381 # initial_value is 0xFFFF
383 return crc16_ansi(msg
, 0xFFFF);
385 # initial_value can be 0xFFFF, 0x1D0F or 0x0000
386 def crc16_ccitt(msg
, initial_value
=0xFFFF):
387 # CRC-16-CCITT polynomial
388 poly
= 0x1021; # Polynomial for CRC-16-CCITT
389 # Use the specified initial value
392 crc ^
= b
<< 8; # XOR byte into CRC top byte
393 for _
in range(8): # Process each bit
394 if crc
& 0x8000: # If the top bit is set
395 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
397 crc
= crc
<< 1; # Just shift left
398 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
401 # initial_value can be 0x42F0E1EBA9EA3693 or 0x0000000000000000
402 def crc64_ecma(msg
, initial_value
=0x0000000000000000):
403 # CRC-64-ECMA polynomial and initial value
404 poly
= 0x42F0E1EBA9EA3693;
405 crc
= initial_value
; # Initial value for CRC-64-ECMA
407 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
408 for _
in range(8): # Process each bit
409 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
410 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
412 crc
<<= 1; # Just shift left if the MSB is 0
413 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
416 # initial_value can be 0x000000000000001B or 0xFFFFFFFFFFFFFFFF
417 def crc64_iso(msg
, initial_value
=0xFFFFFFFFFFFFFFFF):
418 # CRC-64-ISO polynomial and initial value
419 poly
= 0x000000000000001B;
420 crc
= initial_value
; # Common initial value for CRC-64-ISO
422 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
423 for _
in range(8): # Process each bit
424 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
425 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
427 crc
<<= 1; # Just shift left if the MSB is 0
428 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
431 def GetDataFromArray(data
, path
, default
=None):
435 element
= element
[key
];
437 except (KeyError, TypeError, IndexError):
440 def GetDataFromArrayAlt(structure
, path
, default
=None):
443 if isinstance(element
, dict) and key
in element
:
444 element
= element
[key
];
445 elif isinstance(element
, list) and isinstance(key
, int) and -len(element
) <= key
< len(element
):
446 element
= element
[key
];
451 def ReadTillNullByte(fp
, delimiter
=__file_format_delimiter__
):
454 nullbyte
= delimiter
.encode("UTF-8");
456 curbyte
= fp
.read(1);
457 if(curbyte
==nullbyte
or not curbyte
):
459 curfullbyte
= curfullbyte
+ curbyte
;
460 return curfullbyte
.decode('UTF-8');
462 def ReadUntilNullByte(fp
, delimiter
=__file_format_delimiter__
):
463 return ReadTillNullByte(fp
, delimiter
);
465 def SeekToEndOfFile(fp
):
469 if(lasttell
==fp
.tell()):
471 lasttell
= fp
.tell();
474 def ReadFileHeaderData(fp
, rounds
=0, delimiter
=__file_format_delimiter__
):
478 while(rocount
<roend
):
479 HeaderOut
.append(ReadTillNullByte(fp
, delimiter
));
480 rocount
= rocount
+ 1;
483 def ReadFileHeaderDataBySize(fp
, delimiter
=__file_format_delimiter__
):
484 headerpresize
= ReadTillNullByte(fp
, delimiter
);
485 headersize
= int(headerpresize
, 16);
486 headercontent
= str(fp
.read(headersize
).decode('UTF-8')).split(delimiter
);
489 roend
= int(len(headercontent
));
490 HeaderOut
= [headerpresize
];
491 while(rocount
<roend
):
492 HeaderOut
.append(headercontent
[rocount
]);
493 rocount
= rocount
+ 1;
496 def ReadFileHeaderDataByList(fp
, listval
=[], delimiter
=__file_format_delimiter__
):
498 roend
= int(len(listval
));
500 while(rocount
<roend
):
501 RoundArray
= {listval
[rocount
]: ReadTillNullByte(fp
, delimiter
)};
502 HeaderOut
.update(RoundArray
);
503 rocount
= rocount
+ 1;
506 def ReadFileHeaderDataByListSize(fp
, listval
=[], delimiter
=__file_format_delimiter__
):
507 headerpresize
= ReadTillNullByte(fp
, delimiter
);
508 headersize
= int(headerpresize
, 16);
509 headercontent
= str(fp
.read(headersize
).decode('UTF-8')).split(delimiter
);
513 roend
= int(len(headercontent
));
514 HeaderOut
= {listval
[0]: headerpresize
};
515 while(rocount
<roend
):
516 RoundArray
= {listval
[rocount
]: headercontent
[rocount
]};
517 HeaderOut
.update(RoundArray
);
518 rocount
= rocount
+ 1;
519 listcount
= listcount
+ 1;
522 def AppendNullByte(indata
, delimiter
=__file_format_delimiter__
):
523 outdata
= str(indata
) + delimiter
;
526 def AppendNullBytes(indata
=[], delimiter
=__file_format_delimiter__
):
531 outdata
= outdata
+ AppendNullByte(indata
[inum
], delimiter
);
535 def ReadTillNullByteAlt(fp
, delimiter
=__file_format_delimiter__
):
536 """Read bytes from file pointer until a null byte is encountered."""
537 bytes_list
= [] # Use list for efficient append operation.
539 cur_byte
= fp
.read(1);
540 if cur_byte
== delimiter
.encode() or not cur_byte
:
542 bytes_list
.append(cur_byte
);
543 return b
''.join(bytes_list
).decode('UTF-8');
545 def ReadUntilNullByteAlt(fp
, delimiter
=__file_format_delimiter__
):
546 return ReadTillNullByteAlt(fp
, delimiter
);
548 def ReadFileHeaderDataAlt(fp
, rounds
=0, delimiter
=__file_format_delimiter__
):
549 """Read multiple null-byte terminated strings from a file."""
551 for round_count
in range(rounds
):
552 header_out
[round_count
] = ReadTillNullByteAlt(fp
, delimiter
);
555 def ReadFileHeaderDataBySizeAlt(fp
, delimiter
=__file_format_delimiter__
):
556 # Read and convert header size from hexadecimal to integer
557 header_pre_size
= ReadTillNullByte(fp
, delimiter
);
558 header_size
= int(header_pre_size
, 16);
559 # Read and split the header content
560 header_content
= str(fp
.read(header_size
).decode('UTF-8')).split(delimiter
);
562 # Prepend the pre-size and return the combined list
563 return [header_pre_size
] + header_content
;
565 def ReadFileHeaderDataByListSizeAlt(fp
, listval
=[], delimiter
=__file_format_delimiter__
):
566 # Read the size and content from the header
567 header_pre_size
= ReadTillNullByte(fp
, delimiter
);
568 header_size
= int(header_pre_size
, 16);
569 header_content
= str(fp
.read(header_size
).decode('UTF-8')).split(delimiter
);
571 # Initialize HeaderOut with the header pre-size if listval is not empty
572 HeaderOut
= {listval
[0]: header_pre_size
} if listval
else {};
573 # Map the remaining listval items to their corresponding header content, starting from the second item
574 for i
in range(1, min(len(header_content
) + 1, len(listval
))):
575 HeaderOut
[listval
[i
]] = header_content
[i
- 1]; # -1 because header_content is 0-indexed
578 def ReadFileHeaderDataByListAlt(fp
, listval
=[], delimiter
=__file_format_delimiter__
):
579 """Read multiple null-byte terminated strings from a file."""
581 for round_count
in listval
:
582 header_out
.append(ReadTillNullByteAlt(fp
, delimiter
));
585 def AppendNullByteAlt(indata
, delimiter
=__file_format_delimiter__
):
586 """Append a null byte to the given data."""
587 return str(indata
) + delimiter
;
589 def AppendNullBytesAlt(indata
=[], delimiter
=__file_format_delimiter__
):
590 """Append a null byte to each element in the list and concatenate."""
591 return delimiter
.join(map(str, indata
)) + delimiter
; # Efficient concatenation with null byte.
593 def PrintPermissionString(fchmode
, ftype
):
594 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' } };
596 for fmodval
in str(oct(fchmode
))[-3:]:
597 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
598 if(ftype
==0 or ftype
==7):
599 permissionstr
= "-" + permissionstr
;
601 permissionstr
= "h" + permissionstr
;
603 permissionstr
= "l" + permissionstr
;
605 permissionstr
= "c" + permissionstr
;
607 permissionstr
= "b" + permissionstr
;
609 permissionstr
= "d" + permissionstr
;
611 permissionstr
= "f" + permissionstr
;
613 permissionstr
= "D" + permissionstr
;
615 permissionstr
= "p" + permissionstr
;
617 permissionstr
= "w" + permissionstr
;
619 permissionoutstr
= stat
.filemode(fchmode
);
620 except AttributeError:
621 permissionoutstr
= permissionstr
;
623 permissionoutstr
= permissionstr
;
624 return permissionoutstr
;
626 def PrintPermissionStringAlt(fchmode
, ftype
):
628 '0': '---', '1': '--x', '2': '-w-', '3': '-wx',
629 '4': 'r--', '5': 'r-x', '6': 'rw-', '7': 'rwx'
631 # Translate file mode into permission string
632 permissionstr
= ''.join([permissions
[i
] for i
in str(oct(fchmode
))[-3:]]);
633 # Append file type indicator
635 0: '-', 1: 'h', 2: 'l', 3: 'c', 4: 'b',
636 5: 'd', 6: 'f', 8: 'D', 9: 'p', 10: 'w'
638 file_type
= type_indicators
.get(ftype
, '-');
639 permissionstr
= file_type
+ permissionstr
;
641 permissionoutstr
= stat
.filemode(fchmode
);
642 except AttributeError:
643 permissionoutstr
= permissionstr
;
644 return permissionoutstr
;
646 def CompressionSupport():
647 compression_list
= [];
650 compression_list
.append("gz");
651 compression_list
.append("gzip");
656 compression_list
.append("bz2");
657 compression_list
.append("bzip2");
662 compression_list
.append("lz4");
667 compression_list
.append("lzo");
668 compression_list
.append("lzop");
673 compression_list
.append("zstd");
674 compression_list
.append("zstandard");
679 compression_list
.append("lzma");
680 compression_list
.append("xz");
683 return compression_list
;
685 def CheckCompressionType(infile
, formatspecs
=__file_format_list__
, closefp
=True):
686 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
690 catfp
= open(infile
, "rb");
691 except FileNotFoundError
:
694 prefp
= catfp
.read(2);
696 if(prefp
==binascii
.unhexlify("1f8b")):
699 prefp
= catfp
.read(3);
700 if(prefp
==binascii
.unhexlify("425a68")):
702 if(prefp
==binascii
.unhexlify("5d0000")):
705 prefp
= catfp
.read(4);
706 if(prefp
==binascii
.unhexlify("28b52ffd")):
708 if(prefp
==binascii
.unhexlify("04224d18")):
710 if(prefp
==binascii
.unhexlify("504B0304")):
711 filetype
= "zipfile";
713 prefp
= catfp
.read(5);
714 if(prefp
==binascii
.unhexlify("7573746172")):
715 filetype
= "tarfile";
717 prefp
= catfp
.read(6);
718 if(prefp
==binascii
.unhexlify("fd377a585a00")):
721 prefp
= catfp
.read(7);
722 if(prefp
==binascii
.unhexlify("526172211a0700")):
723 filetype
= "rarfile";
724 if(prefp
==binascii
.unhexlify("43617446696c65")):
725 filetype
= "catfile";
727 prefp
= catfp
.read(8);
728 if(prefp
==binascii
.unhexlify("526172211a070100")):
729 filetype
= "rarfile";
731 prefp
= catfp
.read(formatspecs
[3]);
732 if(prefp
==binascii
.unhexlify(formatspecs
[4])):
733 filetype
= formatspecs
[2];
735 prefp
= catfp
.read(9);
736 if(prefp
==binascii
.unhexlify("894c5a4f000d0a1a0a")):
739 prefp
= catfp
.read(10);
740 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
741 filetype
= "tarfile";
747 def CheckCompressionTypeFromString(instring
, formatspecs
=__file_format_list__
, closefp
=True):
749 instringsfile
= BytesIO(instring
);
751 instringsfile
= BytesIO(instring
.encode("UTF-8"));
752 return CheckCompressionType(instringsfile
, formatspecs
, closefp
);
754 def GetCompressionMimeType(infile
, formatspecs
=__file_format_list__
):
755 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
756 if(compresscheck
=="gzip" or compresscheck
=="gz"):
757 return archivefile_gzip_mimetype
;
758 if(compresscheck
=="bzip2" or compresscheck
=="bz2"):
759 return archivefile_bzip2_mimetype
;
760 if(compresscheck
=="zstd" or compresscheck
=="zstandard"):
761 return archivefile_zstandard_mimetype
;
762 if(compresscheck
=="lz4"):
763 return archivefile_lz4_mimetype
;
764 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
765 return archivefile_lzop_mimetype
;
766 if(compresscheck
=="lzma"):
767 return archivefile_lzma_mimetype
;
768 if(compresscheck
=="xz"):
769 return archivefile_xz_mimetype
;
770 if(compresscheck
=="catfile" or compresscheck
=="cat" or compresscheck
==formatspecs
[2]):
771 return archivefile_cat_mimetype
;
772 if(not compresscheck
):
776 def UncompressArchiveFile(fp
, formatspecs
=__file_format_list__
):
777 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
779 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
780 if(compresscheck
=="gzip"):
785 catfp
= gzip
.GzipFile(fileobj
=fp
, mode
="rb");
786 if(compresscheck
=="bzip2"):
792 catfp
.write(bz2
.decompress(fp
.read()));
793 if(compresscheck
=="zstd"):
799 catfp
.write(zstandard
.decompress(fp
.read()));
800 if(compresscheck
=="lz4"):
806 catfp
.write(lz4
.frame
.decompress(fp
.read()));
807 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
813 catfp
.write(lzo
.decompress(fp
.read()));
814 if(compresscheck
=="lzma" or compresscheck
=="xz"):
820 catfp
.write(lzma
.decompress(fp
.read()));
821 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[2]):
823 if(not compresscheck
):
829 with fp
as fpcontent
:
831 catfp
.write(lzma
.decompress(fp
.read()));
832 except lzma
.LZMAError
:
836 create_alias_function("Uncompress", __file_format_name__
, "", UncompressArchiveFile
);
838 def UncompressFile(infile
, formatspecs
=__file_format_list__
, mode
="rb"):
839 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
840 if(sys
.version_info
[0]==2 and compresscheck
):
846 if(compresscheck
=="gzip"):
852 filefp
= gzip
.open(infile
, mode
, encoding
="UTF-8");
853 except (ValueError, TypeError) as e
:
854 filefp
= gzip
.open(infile
, mode
);
855 if(compresscheck
=="bzip2"):
861 filefp
= bz2
.open(infile
, mode
, encoding
="UTF-8");
862 except (ValueError, TypeError) as e
:
863 filefp
= bz2
.open(infile
, mode
);
864 if(compresscheck
=="zstd"):
870 filefp
= zstandard
.open(infile
, mode
, encoding
="UTF-8");
871 except (ValueError, TypeError) as e
:
872 filefp
= zstandard
.open(infile
, mode
);
873 if(compresscheck
=="lz4"):
879 filefp
= lz4
.frame
.open(infile
, mode
, encoding
="UTF-8");
880 except (ValueError, TypeError) as e
:
881 filefp
= lz4
.frame
.open(infile
, mode
);
882 if(compresscheck
=="lzo"):
888 filefp
= lzo
.open(infile
, mode
, encoding
="UTF-8");
889 except (ValueError, TypeError) as e
:
890 filefp
= lzo
.open(infile
, mode
);
891 if(compresscheck
=="lzma"):
897 filefp
= lzma
.open(infile
, mode
, encoding
="UTF-8");
898 except (ValueError, TypeError) as e
:
899 filefp
= lzma
.open(infile
, mode
);
900 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[2]):
902 filefp
= open(infile
, mode
, encoding
="UTF-8");
903 except (ValueError, TypeError) as e
:
904 filefp
= open(infile
, mode
);
905 if(not compresscheck
):
907 filefp
= open(infile
, mode
, encoding
="UTF-8");
908 except (ValueError, TypeError) as e
:
909 filefp
= open(infile
, mode
);
910 except FileNotFoundError
:
914 def UncompressString(infile
):
915 compresscheck
= CheckCompressionTypeFromString(infile
, formatspecs
, False);
916 if(compresscheck
=="gzip"):
921 fileuz
= gzip
.decompress(infile
);
922 if(compresscheck
=="bzip2"):
927 fileuz
= bz2
.decompress(infile
);
928 if(compresscheck
=="zstd"):
933 fileuz
= zstandard
.decompress(infile
);
934 if(compresscheck
=="lz4"):
939 fileuz
= lz4
.frame
.decompress(infile
);
940 if(compresscheck
=="lzo"):
945 fileuz
= lzo
.decompress(infile
);
946 if(compresscheck
=="lzma"):
951 fileuz
= lzma
.decompress(infile
);
952 if(not compresscheck
):
954 if(hasattr(fileuz
, 'decode')):
955 fileuz
= fileuz
.decode("UTF-8");
958 def UncompressStringAlt(infile
):
960 outstring
= UncompressString(infile
);
961 filefp
.write(outstring
);
965 def CheckCompressionSubType(infile
, formatspecs
=__file_format_list__
):
966 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
967 if(not compresscheck
):
968 fextname
= os
.path
.splitext(infile
)[1];
970 compresscheck
= "gzip";
971 if(fextname
==".bz2"):
972 compresscheck
= "bzip2";
973 if(fextname
==".zst"):
974 compresscheck
= "zstd";
975 if(fextname
==".lz4"):
976 compresscheck
= "lz4";
977 if(fextname
==".lzo" or fextname
==".lzop"):
978 compresscheck
= "lzo";
979 if(fextname
==".lzma" or fextname
==".xz"):
980 compresscheck
= "lzma";
981 if(not compresscheck
):
983 if(compresscheck
=="catfile"):
985 if(compresscheck
==formatspecs
[2]):
986 return formatspecs
[2];
987 if(compresscheck
=="tarfile"):
989 if(compresscheck
=="zipfile"):
991 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
992 catfp
= UncompressArchiveFile(infile
, formatspecs
[2]);
995 if(compresscheck
=="gzip"):
1000 catfp
= gzip
.GzipFile(infile
, "rb");
1001 if(compresscheck
=="bzip2"):
1006 catfp
= bz2
.BZ2File(infile
, "rb");
1007 if(compresscheck
=="lz4"):
1012 catfp
= lz4
.frame
.open(infile
, "rb");
1013 if(compresscheck
=="zstd"):
1018 catfp
= zstandard
.open(infile
, "rb");
1019 if(compresscheck
=="lzma" or compresscheck
=="xz"):
1024 catfp
= lzma
.open(infile
, "rb");
1025 except FileNotFoundError
:
1028 prefp
= catfp
.read(5);
1029 if(prefp
==binascii
.unhexlify("7573746172")):
1030 filetype
= "tarfile";
1032 prefp
= catfp
.read(7);
1033 if(prefp
==binascii
.unhexlify("43617446696c65")):
1034 filetype
= "catfile";
1036 prefp
= catfp
.read(formatspecs
[3]);
1037 if(prefp
==binascii
.unhexlify(formatspecs
[4])):
1038 filetype
= formatspecs
[2];
1040 prefp
= catfp
.read(10);
1041 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
1042 filetype
= "tarfile";
1047 def GZipCompress(data
, compresslevel
=9):
1052 tmpfp
= tempfile
.NamedTemporaryFile("wb", delete
=False);
1054 tmpfp
= gzip
.GzipFile(tmpfp
.name
, mode
="wb", compresslevel
=compresslevel
);
1058 catfp
= open(tmpfp
.name
, "rb");
1059 except FileNotFoundError
:
1061 catdata
= catfp
.read();
1065 def CompressArchiveFile(fp
, compression
="auto", compressionlevel
=None, formatspecs
=__file_format_list__
):
1066 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1067 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
1070 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
1072 if(compression
not in compressionlist
and compression
is None):
1073 compression
= "auto";
1074 if(compression
=="gzip"):
1080 if(compressionlevel
is None):
1081 compressionlevel
= 9;
1083 compressionlevel
= int(compressionlevel
);
1084 catfp
.write(gzip
.compress(fp
.read(), compresslevel
=compressionlevel
));
1085 if(compression
=="bzip2"):
1091 if(compressionlevel
is None):
1092 compressionlevel
= 9;
1094 compressionlevel
= int(compressionlevel
);
1095 catfp
.write(bz2
.compress(fp
.read(), compresslevel
=compressionlevel
));
1096 if(compression
=="lz4"):
1102 if(compressionlevel
is None):
1103 compressionlevel
= 9;
1105 compressionlevel
= int(compressionlevel
);
1106 catfp
.write(lz4
.frame
.compress(fp
.read(), compression_level
=compressionlevel
));
1107 if(compression
=="lzo" or compression
=="lzop"):
1113 if(compressionlevel
is None):
1114 compressionlevel
= 9;
1116 compressionlevel
= int(compressionlevel
);
1117 catfp
.write(lzo
.compress(fp
.read(), compresslevel
=compressionlevel
));
1118 if(compression
=="zstd"):
1124 if(compressionlevel
is None):
1125 compressionlevel
= 10;
1127 compressionlevel
= int(compressionlevel
);
1128 catfp
.write(zstandard
.compress(fp
.read(), level
=compressionlevel
));
1129 if(compression
=="lzma"):
1135 if(compressionlevel
is None):
1136 compressionlevel
= 9;
1138 compressionlevel
= int(compressionlevel
);
1139 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]));
1140 if(compression
=="xz"):
1146 if(compressionlevel
is None):
1147 compressionlevel
= 9;
1149 compressionlevel
= int(compressionlevel
);
1150 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]));
1151 if(compression
=="auto" or compression
is None):
1156 create_alias_function("Compress", __file_format_name__
, "", CompressArchiveFile
);
1158 def CompressOpenFile(outfile
, compressionlevel
=None):
1159 if(outfile
is None):
1161 fbasename
= os
.path
.splitext(outfile
)[0];
1162 fextname
= os
.path
.splitext(outfile
)[1];
1163 if(compressionlevel
is None and fextname
!=".zst"):
1164 compressionlevel
= 9;
1165 elif(compressionlevel
is None and fextname
==".zst"):
1166 compressionlevel
= 10;
1168 compressionlevel
= int(compressionlevel
);
1169 if(sys
.version_info
[0]==2):
1174 if(fextname
not in outextlistwd
):
1176 outfp
= open(outfile
, "wb", encoding
="UTF-8");
1177 except (ValueError, TypeError) as e
:
1178 outfp
= open(outfile
, "wb");
1179 elif(fextname
==".gz"):
1185 outfp
= gzip
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
1186 except (ValueError, TypeError) as e
:
1187 outfp
= gzip
.open(outfile
, mode
, compressionlevel
);
1188 elif(fextname
==".bz2"):
1194 outfp
= bz2
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
1195 except (ValueError, TypeError) as e
:
1196 outfp
= bz2
.open(outfile
, mode
, compressionlevel
);
1197 elif(fextname
==".zst"):
1203 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
), encoding
="UTF-8");
1204 except (ValueError, TypeError) as e
:
1205 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
));
1206 elif(fextname
==".xz"):
1212 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}], encoding
="UTF-8");
1213 except (ValueError, TypeError) as e
:
1214 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]);
1215 elif(fextname
==".lz4"):
1221 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
, encoding
="UTF-8");
1222 except (ValueError, TypeError) as e
:
1223 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
);
1224 elif(fextname
==".lzo"):
1230 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
, encoding
="UTF-8");
1231 except (ValueError, TypeError) as e
:
1232 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
);
1233 elif(fextname
==".lzma"):
1239 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}], encoding
="UTF-8");
1240 except (ValueError, TypeError) as e
:
1241 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]);
1242 except FileNotFoundError
:
1246 def GetDevMajorMinor(fdev
):
1248 if(hasattr(os
, "minor")):
1249 retdev
.append(os
.minor(fdev
));
1252 if(hasattr(os
, "major")):
1253 retdev
.append(os
.major(fdev
));
1258 def CheckSumSupport(checkfor
, guaranteed
=True):
1260 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
1262 hash_list
= sorted(list(hashlib
.algorithms_available
));
1263 checklistout
= sorted(hash_list
+ ['adler32', 'crc16', 'crc16_ansi', 'crc16_ibm', 'crc16_ccitt', 'crc32', 'crc64', 'crc64_ecma', 'crc64_iso', 'none']);
1264 if(checkfor
in checklistout
):
1269 def CheckSumSupportAlt(checkfor
, guaranteed
=True):
1271 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
1273 hash_list
= sorted(list(hashlib
.algorithms_available
));
1274 checklistout
= hash_list
;
1275 if(checkfor
in checklistout
):
1280 def PackArchiveFile(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1281 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1282 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1283 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1284 advancedlist
= True;
1285 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1286 outfile
= RemoveWindowsPath(outfile
);
1287 checksumtype
= checksumtype
.lower();
1288 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
1289 checksumtype
="crc32";
1290 if(checksumtype
=="none"):
1292 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
1294 if(compression
not in compressionlist
and compression
is None):
1295 compression
= "auto";
1297 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1298 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1299 if(os
.path
.exists(outfile
)):
1304 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1306 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
1309 fbasename
= os
.path
.splitext(outfile
)[0];
1310 fextname
= os
.path
.splitext(outfile
)[1];
1311 catfp
= CompressOpenFile(outfile
, compressionlevel
);
1312 catver
= formatspecs
[6];
1313 fileheaderver
= str(int(catver
.replace(".", "")));
1314 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
1315 catfp
.write(fileheader
.encode('UTF-8'));
1318 for line
in sys
.stdin
:
1319 infilelist
.append(line
.strip());
1320 infilelist
= list(filter(None, infilelist
));
1321 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
1322 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
1324 with
open(infiles
, "r") as finfile
:
1325 for line
in finfile
:
1326 infilelist
.append(line
.strip());
1327 infilelist
= list(filter(None, infilelist
));
1329 if(isinstance(infiles
, (list, tuple, ))):
1330 infilelist
= list(filter(None, infiles
));
1331 elif(isinstance(infiles
, (str, ))):
1332 infilelist
= list(filter(None, [infiles
]));
1334 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
1336 GetDirList
= ListDir(infilelist
, followlink
, False);
1344 inodetocatinode
= {};
1345 fnumfiles
= format(int(len(GetDirList
)), 'x').lower();
1346 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
1347 if(checksumtype
=="none" or checksumtype
==""):
1348 catfileheadercshex
= format(0, 'x').lower();
1349 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1350 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1351 elif(checksumtype
=="crc16_ccitt"):
1352 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1353 elif(checksumtype
=="adler32"):
1354 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1355 elif(checksumtype
=="crc32"):
1356 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1357 elif(checksumtype
=="crc64_ecma"):
1358 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1359 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1360 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1361 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1362 checksumoutstr
= hashlib
.new(checksumtype
);
1363 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
1364 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1366 catfileheadercshex
= format(0, 'x').lower
1367 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
1368 catfp
.write(fnumfilesa
.encode('UTF-8'));
1371 os
.fsync(catfp
.fileno());
1372 except io
.UnsupportedOperation
:
1374 except AttributeError:
1376 for curfname
in GetDirList
:
1377 catfhstart
= catfp
.tell();
1378 if(re
.findall("^[.|/]", curfname
)):
1381 fname
= "./"+curfname
;
1383 VerbosePrintOut(fname
);
1384 if(not followlink
or followlink
is None):
1385 fstatinfo
= os
.lstat(fname
);
1387 fstatinfo
= os
.stat(fname
);
1388 fpremode
= fstatinfo
.st_mode
;
1389 finode
= fstatinfo
.st_ino
;
1390 flinkcount
= fstatinfo
.st_nlink
;
1392 if(stat
.S_ISREG(fpremode
)):
1394 elif(stat
.S_ISLNK(fpremode
)):
1396 elif(stat
.S_ISCHR(fpremode
)):
1398 elif(stat
.S_ISBLK(fpremode
)):
1400 elif(stat
.S_ISDIR(fpremode
)):
1402 elif(stat
.S_ISFIFO(fpremode
)):
1404 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
1406 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
1408 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
1413 fcurfid
= format(int(curfid
), 'x').lower();
1414 if(not followlink
and finode
!=0):
1416 if(finode
in inodelist
):
1418 flinkname
= inodetofile
[finode
];
1419 fcurinode
= format(int(inodetocatinode
[finode
]), 'x').lower();
1420 if(finode
not in inodelist
):
1421 inodelist
.append(finode
);
1422 inodetofile
.update({finode
: fname
});
1423 inodetocatinode
.update({finode
: curinode
});
1424 fcurinode
= format(int(curinode
), 'x').lower();
1425 curinode
= curinode
+ 1;
1427 fcurinode
= format(int(curinode
), 'x').lower();
1428 curinode
= curinode
+ 1;
1429 curfid
= curfid
+ 1;
1431 flinkname
= os
.readlink(fname
);
1432 fdev
= fstatinfo
.st_dev
;
1433 getfdev
= GetDevMajorMinor(fdev
);
1434 fdev_minor
= getfdev
[0];
1435 fdev_major
= getfdev
[1];
1436 frdev
= fstatinfo
.st_dev
;
1437 if(hasattr(fstatinfo
, "st_rdev")):
1438 frdev
= fstatinfo
.st_rdev
;
1440 frdev
= fstatinfo
.st_dev
;
1441 getfrdev
= GetDevMajorMinor(frdev
);
1442 frdev_minor
= getfrdev
[0];
1443 frdev_major
= getfrdev
[1];
1444 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
1445 fsize
= format(int("0"), 'x').lower();
1446 elif(ftype
==0 or ftype
==7):
1447 fsize
= format(int(fstatinfo
.st_size
), 'x').lower();
1449 fsize
= format(int(fstatinfo
.st_size
)).lower();
1450 fatime
= format(int(fstatinfo
.st_atime
), 'x').lower();
1451 fmtime
= format(int(fstatinfo
.st_mtime
), 'x').lower();
1452 fctime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1453 if(hasattr(fstatinfo
, "st_birthtime")):
1454 fbtime
= format(int(fstatinfo
.st_birthtime
), 'x').lower();
1456 fbtime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1457 fmode
= format(int(fstatinfo
.st_mode
), 'x').lower();
1458 fchmode
= format(int(stat
.S_IMODE(fstatinfo
.st_mode
)), 'x').lower();
1459 ftypemod
= format(int(stat
.S_IFMT(fstatinfo
.st_mode
)), 'x').lower();
1460 fuid
= format(int(fstatinfo
.st_uid
), 'x').lower();
1461 fgid
= format(int(fstatinfo
.st_gid
), 'x').lower();
1466 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
1467 funame
= userinfo
.pw_name
;
1476 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
1477 fgname
= groupinfo
.gr_name
;
1482 fdev_minor
= format(int(fdev_minor
), 'x').lower();
1483 fdev_major
= format(int(fdev_major
), 'x').lower();
1484 frdev_minor
= format(int(frdev_minor
), 'x').lower();
1485 frdev_major
= format(int(frdev_major
), 'x').lower();
1486 finode
= format(int(finode
), 'x').lower();
1487 flinkcount
= format(int(flinkcount
), 'x').lower();
1488 if(hasattr(fstatinfo
, "st_file_attributes")):
1489 fwinattributes
= format(int(fstatinfo
.st_file_attributes
), 'x').lower();
1491 fwinattributes
= format(int(0), 'x').lower();
1492 fcontents
= "".encode('UTF-8');
1494 if(ftype
== 0 or ftype
== 7):
1495 with
open(fname
, "rb") as fpc
:
1497 chunk
= fpc
.read(chunk_size
);
1501 if(followlink
and (ftype
== 1 or ftype
== 2)):
1502 flstatinfo
= os
.stat(flinkname
);
1503 with
open(flinkname
, "rb") as fpc
:
1505 chunk
= fpc
.read(chunk_size
);
1509 ftypehex
= format(ftype
, 'x').lower();
1510 extrafields
= format(len(extradata
), 'x').lower();
1511 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
1512 if(len(extradata
)>0):
1513 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
1514 extrasizelen
= format(len(extrasizestr
), 'x').lower();
1515 catfileoutstr
= AppendNullBytes([ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
], formatspecs
[5]);
1516 if(len(extradata
)>0):
1517 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
1518 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
1519 if(checksumtype
=="none" or checksumtype
==""):
1520 catfileheadercshex
= format(0, 'x').lower();
1521 catfilecontentcshex
= format(0, 'x').lower();
1522 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1523 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
1524 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
1525 elif(checksumtype
=="crc16_ccitt"):
1526 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
1527 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
1528 elif(checksumtype
=="adler32"):
1529 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1530 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
1531 elif(checksumtype
=="crc32"):
1532 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1533 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
1534 elif(checksumtype
=="crc64_ecma"):
1535 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1536 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
1537 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1538 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1539 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
1540 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1541 checksumoutstr
= hashlib
.new(checksumtype
);
1542 checksumoutstr
.update("".encode('UTF-8'));
1543 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1544 checksumoutstr
= hashlib
.new(checksumtype
);
1545 checksumoutstr
.update(fcontents
);
1546 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
1548 catfileheadercshex
= format(0, 'x').lower();
1549 catfilecontentcshex
= format(0, 'x').lower();
1550 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1551 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
1552 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
1553 if(checksumtype
=="none" or checksumtype
==""):
1554 catfileheadercshex
= format(0, 'x').lower();
1555 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1556 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
1557 elif(checksumtype
=="crc16_ccitt"):
1558 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
1559 elif(checksumtype
=="adler32"):
1560 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
1561 elif(checksumtype
=="crc32"):
1562 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
1563 elif(checksumtype
=="crc64_ecma"):
1564 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1565 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1566 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1567 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1568 checksumoutstr
= hashlib
.new(checksumtype
);
1569 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
1570 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1572 catfileheadercshex
= format(0, 'x').lower();
1573 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1574 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
1575 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
1576 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
1577 nullstrecd
= formatspecs
[5].encode('UTF-8');
1578 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
1579 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
1580 catfp
.write(catfileout
);
1583 os
.fsync(catfp
.fileno());
1584 except io
.UnsupportedOperation
:
1586 except AttributeError:
1588 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1589 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1592 os
.fsync(catfp
.fileno());
1593 except io
.UnsupportedOperation
:
1595 except AttributeError:
1599 if(hasattr(sys
.stdout
, "buffer")):
1600 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1602 shutil
.copyfileobj(catfp
, sys
.stdout
);
1603 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
1604 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1606 upload_file_to_internet_file(catfp
, outfile
);
1614 create_alias_function("Pack", __file_format_name__
, "", PackArchiveFile
);
1616 if(hasattr(shutil
, "register_archive_format")):
1617 def PackArchiveFileFunc(archive_name
, source_dir
, **kwargs
):
1618 return PackArchiveFile(source_dir
, archive_name
, False, "auto", None, False, "crc32", [], __file_format_delimiter__
, False, False);
1619 create_alias_function("Pack", __file_format_name__
, "Func", PackArchiveFileFunc
);
1621 def PackArchiveFileFromDirList(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1622 return PackArchiveFile(infiles
, outfile
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
1624 def PackArchiveFileFromTarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1625 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1626 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1627 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1628 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1629 outfile
= RemoveWindowsPath(outfile
);
1630 checksumtype
= checksumtype
.lower();
1631 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
1632 checksumtype
="crc32";
1633 if(checksumtype
=="none"):
1635 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
1637 if(compression
not in compressionlist
and compression
is None):
1638 compression
= "auto";
1640 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1641 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1642 if(os
.path
.exists(outfile
)):
1647 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1649 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
1652 fbasename
= os
.path
.splitext(outfile
)[0];
1653 fextname
= os
.path
.splitext(outfile
)[1];
1654 catfp
= CompressOpenFile(outfile
, compressionlevel
);
1655 catver
= formatspecs
[6];
1656 fileheaderver
= str(int(catver
.replace(".", "")));
1657 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
1658 catfp
.write(fileheader
.encode('UTF-8'));
1664 inodetocatinode
= {};
1667 if(hasattr(sys
.stdin
, "buffer")):
1668 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
1670 shutil
.copyfileobj(sys
.stdin
, infile
);
1675 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
1676 infile
= download_file_from_internet_file(infile
);
1681 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
1683 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
1685 if(not tarfile
.is_tarfile(infile
)):
1687 except AttributeError:
1688 if(not is_tarfile(infile
)):
1693 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1694 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
1696 tarfp
= tarfile
.open(infile
, "r");
1697 except FileNotFoundError
:
1699 fnumfiles
= format(int(len(tarfp
.getmembers())), 'x').lower();
1700 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
1701 if(checksumtype
=="none" or checksumtype
==""):
1702 catfileheadercshex
= format(0, 'x').lower();
1703 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1704 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1705 elif(checksumtype
=="crc16_ccitt"):
1706 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1707 elif(checksumtype
=="adler32"):
1708 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1709 elif(checksumtype
=="crc32"):
1710 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1711 elif(checksumtype
=="crc64_ecma"):
1712 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1713 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1714 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1715 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1716 checksumoutstr
= hashlib
.new(checksumtype
);
1717 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
1718 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1720 catfileheadercshex
= format(0, 'x').lower();
1721 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
1722 catfp
.write(fnumfilesa
.encode('UTF-8'));
1725 os
.fsync(catfp
.fileno());
1726 except io
.UnsupportedOperation
:
1728 except AttributeError:
1730 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
1731 catfhstart
= catfp
.tell();
1732 if(re
.findall("^[.|/]", member
.name
)):
1733 fname
= member
.name
;
1735 fname
= "./"+member
.name
;
1737 VerbosePrintOut(fname
);
1738 fpremode
= member
.mode
;
1739 ffullmode
= member
.mode
;
1743 ffullmode
= member
.mode
+ stat
.S_IFREG
;
1745 elif(member
.isdev()):
1746 ffullmode
= member
.mode
;
1748 elif(member
.islnk()):
1749 ffullmode
= member
.mode
+ stat
.S_IFREG
;
1751 elif(member
.issym()):
1752 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
1754 elif(member
.ischr()):
1755 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
1757 elif(member
.isblk()):
1758 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
1760 elif(member
.isdir()):
1761 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
1763 elif(member
.isfifo()):
1764 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
1766 elif(member
.issparse()):
1767 ffullmode
= member
.mode
;
1770 ffullmode
= member
.mode
;
1773 fcurfid
= format(int(curfid
), 'x').lower();
1774 fcurinode
= format(int(0), 'x').lower();
1775 curfid
= curfid
+ 1;
1777 flinkname
= member
.linkname
;
1778 fdev_minor
= format(int(member
.devminor
), 'x').lower();
1779 fdev_major
= format(int(member
.devmajor
), 'x').lower();
1780 frdev_minor
= format(int(member
.devminor
), 'x').lower();
1781 frdev_major
= format(int(member
.devmajor
), 'x').lower();
1782 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
1783 fsize
= format(int("0"), 'x').lower();
1784 elif(ftype
==0 or ftype
==7):
1785 fsize
= format(int(member
.size
), 'x').lower();
1787 fsize
= format(int(member
.size
), 'x').lower();
1788 fatime
= format(int(member
.mtime
), 'x').lower();
1789 fmtime
= format(int(member
.mtime
), 'x').lower();
1790 fctime
= format(int(member
.mtime
), 'x').lower();
1791 fbtime
= format(int(member
.mtime
), 'x').lower();
1792 fmode
= format(int(ffullmode
), 'x').lower();
1793 fchmode
= format(int(stat
.S_IMODE(ffullmode
)), 'x').lower();
1794 ftypemod
= format(int(stat
.S_IFMT(ffullmode
)), 'x').lower();
1795 fuid
= format(int(member
.uid
), 'x').lower();
1796 fgid
= format(int(member
.gid
), 'x').lower();
1797 funame
= member
.uname
;
1798 fgname
= member
.gname
;
1799 flinkcount
= format(int(flinkcount
), 'x').lower();
1800 fcontents
= "".encode('UTF-8');
1802 if(ftype
== 0 or ftype
== 7):
1803 with tarfp
.extractfile(member
) as fpc
:
1805 chunk
= fpc
.read(chunk_size
);
1809 ftypehex
= format(ftype
, 'x').lower();
1810 extrafields
= format(len(extradata
), 'x').lower();
1811 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
1812 if(len(extradata
)>0):
1813 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
1814 extrasizelen
= format(len(extrasizestr
), 'x').lower();
1815 catfileoutstr
= AppendNullBytes([ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
], formatspecs
[5]);
1816 if(len(extradata
)>0):
1817 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
1818 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
1819 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
1820 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
1821 if(checksumtype
=="none" or checksumtype
==""):
1822 catfileheadercshex
= format(0, 'x').lower();
1823 catfilecontentcshex
= format(0, 'x').lower();
1824 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1825 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
1826 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
1827 elif(checksumtype
=="crc16_ccitt"):
1828 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
1829 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
1830 elif(checksumtype
=="adler32"):
1831 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1832 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
1833 elif(checksumtype
=="crc32"):
1834 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1835 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
1836 elif(checksumtype
=="crc64_ecma"):
1837 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1838 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
1839 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1840 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1841 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
1842 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1843 checksumoutstr
= hashlib
.new(checksumtype
);
1844 checksumoutstr
.update("".encode('UTF-8'));
1845 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1846 checksumoutstr
= hashlib
.new(checksumtype
);
1847 checksumoutstr
.update(fcontents
);
1848 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
1850 catfileheadercshex
= format(0, 'x').lower();
1851 catfilecontentcshex
= format(0, 'x').lower();
1852 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1853 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
1854 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
1855 if(checksumtype
=="none" or checksumtype
==""):
1856 catfileheadercshex
= format(0, 'x').lower();
1857 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1858 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
1859 elif(checksumtype
=="crc16_ccitt"):
1860 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
1861 elif(checksumtype
=="adler32"):
1862 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
1863 elif(checksumtype
=="crc32"):
1864 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
1865 elif(checksumtype
=="crc64_ecma"):
1866 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1867 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1868 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1869 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1870 checksumoutstr
= hashlib
.new(checksumtype
);
1871 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
1872 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1874 catfileheadercshex
= format(0, 'x').lower();
1875 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1876 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
1877 nullstrecd
= formatspecs
[5].encode('UTF-8');
1878 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
1879 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
1880 catfp
.write(catfileout
);
1883 os
.fsync(catfp
.fileno());
1884 except io
.UnsupportedOperation
:
1886 except AttributeError:
1888 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1889 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1892 os
.fsync(catfp
.fileno());
1893 except io
.UnsupportedOperation
:
1895 except AttributeError:
1899 if(hasattr(sys
.stdout
, "buffer")):
1900 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1902 shutil
.copyfileobj(catfp
, sys
.stdout
);
1903 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
1904 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1906 upload_file_from_internet_file(catfp
, outfile
);
1914 create_alias_function("Pack", __file_format_name__
, "FromTarFile", PackArchiveFileFromTarFile
);
1916 def PackArchiveFileFromZipFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1917 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1918 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1919 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1920 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1921 outfile
= RemoveWindowsPath(outfile
);
1922 checksumtype
= checksumtype
.lower();
1923 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
1924 checksumtype
="crc32";
1925 if(checksumtype
=="none"):
1927 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
1929 if(compression
not in compressionlist
and compression
is None):
1930 compression
= "auto";
1932 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1933 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1934 if(os
.path
.exists(outfile
)):
1939 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1941 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
1944 fbasename
= os
.path
.splitext(outfile
)[0];
1945 fextname
= os
.path
.splitext(outfile
)[1];
1946 catfp
= CompressOpenFile(outfile
, compressionlevel
);
1947 catver
= formatspecs
[6];
1948 fileheaderver
= str(int(catver
.replace(".", "")));
1949 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
1950 catfp
.write(fileheader
.encode('UTF-8'));
1956 inodetocatinode
= {};
1959 if(hasattr(sys
.stdin
, "buffer")):
1960 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
1962 shutil
.copyfileobj(sys
.stdin
, infile
);
1967 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
1968 infile
= download_file_from_internet_file(infile
);
1973 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
1977 if(not zipfile
.is_zipfile(infile
)):
1980 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
1981 except FileNotFoundError
:
1983 ziptest
= zipfp
.testzip();
1985 VerbosePrintOut("Bad file found!");
1986 fnumfiles
= format(int(len(zipfp
.infolist())), 'x').lower();
1987 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
1988 if(checksumtype
=="none" or checksumtype
==""):
1989 catfileheadercshex
= format(0, 'x').lower();
1990 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1991 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1992 elif(checksumtype
=="crc16_ccitt"):
1993 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1994 elif(checksumtype
=="adler32"):
1995 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1996 elif(checksumtype
=="crc32"):
1997 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1998 elif(checksumtype
=="crc64_ecma"):
1999 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2000 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2001 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2002 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2003 checksumoutstr
= hashlib
.new(checksumtype
);
2004 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
2005 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2007 catfileheadercshex
= format(0, 'x').lower();
2008 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2009 catfp
.write(fnumfilesa
.encode('UTF-8'));
2012 os
.fsync(catfp
.fileno());
2013 except io
.UnsupportedOperation
:
2015 except AttributeError:
2017 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
2018 catfhstart
= catfp
.tell();
2019 if(re
.findall("^[.|/]", member
.filename
)):
2020 fname
= member
.filename
;
2022 fname
= "./"+member
.filename
;
2023 zipinfo
= zipfp
.getinfo(member
.filename
);
2025 VerbosePrintOut(fname
);
2026 if(not member
.is_dir()):
2027 fpremode
= int(stat
.S_IFREG
+ 438);
2028 elif(member
.is_dir()):
2029 fpremode
= int(stat
.S_IFDIR
+ 511);
2032 if(not member
.is_dir()):
2034 elif(member
.is_dir()):
2037 fcurfid
= format(int(curfid
), 'x').lower();
2038 fcurinode
= format(int(0), 'x').lower();
2039 curfid
= curfid
+ 1;
2040 fdev_minor
= format(int(0), 'x').lower();
2041 fdev_major
= format(int(0), 'x').lower();
2042 frdev_minor
= format(int(0), 'x').lower();
2043 frdev_major
= format(int(0), 'x').lower();
2045 fsize
= format(int("0"), 'x').lower();
2047 fsize
= format(int(member
.file_size
), 'x').lower();
2049 fsize
= format(int(member
.file_size
), 'x').lower();
2050 fatime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2051 fmtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2052 fctime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2053 fbtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2054 if(not member
.is_dir()):
2055 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
2056 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
2057 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
2058 if(member
.is_dir()):
2059 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
2060 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
2061 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
2063 fuid
= format(int(os
.getuid()), 'x').lower();
2064 except AttributeError:
2065 fuid
= format(int(0), 'x').lower();
2067 fuid
= format(int(0), 'x').lower();
2069 fgid
= format(int(os
.getgid()), 'x').lower();
2070 except AttributeError:
2071 fgid
= format(int(0), 'x').lower();
2073 fgid
= format(int(0), 'x').lower();
2077 userinfo
= pwd
.getpwuid(os
.getuid());
2078 funame
= userinfo
.pw_name
;
2081 except AttributeError:
2089 groupinfo
= grp
.getgrgid(os
.getgid());
2090 fgname
= groupinfo
.gr_name
;
2093 except AttributeError:
2097 fcontents
= "".encode('UTF-8');
2099 fcontents
= zipfp
.read(member
.filename
);
2100 ftypehex
= format(ftype
, 'x').lower();
2101 extrafields
= format(len(extradata
), 'x').lower();
2102 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
2103 if(len(extradata
)>0):
2104 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2105 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2106 catfileoutstr
= AppendNullBytes([ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
], formatspecs
[5]);
2107 if(len(extradata
)>0):
2108 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2109 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
2110 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
2111 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
2112 if(checksumtype
=="none" or checksumtype
==""):
2113 catfileheadercshex
= format(0, 'x').lower();
2114 catfilecontentcshex
= format(0, 'x').lower();
2115 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2116 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2117 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
2118 elif(checksumtype
=="crc16_ccitt"):
2119 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2120 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
2121 elif(checksumtype
=="adler32"):
2122 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2123 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
2124 elif(checksumtype
=="crc32"):
2125 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2126 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
2127 elif(checksumtype
=="crc64_ecma"):
2128 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2129 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
2130 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2131 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2132 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
2133 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2134 checksumoutstr
= hashlib
.new(checksumtype
);
2135 checksumoutstr
.update("".encode('UTF-8'));
2136 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2137 checksumoutstr
= hashlib
.new(checksumtype
);
2138 checksumoutstr
.update(fcontents
);
2139 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
2141 catfileheadercshex
= format(0, 'x').lower();
2142 catfilecontentcshex
= format(0, 'x').lower();
2143 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2144 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2145 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
2146 if(checksumtype
=="none" or checksumtype
==""):
2147 catfileheadercshex
= format(0, 'x').lower();
2148 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2149 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2150 elif(checksumtype
=="crc16_ccitt"):
2151 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2152 elif(checksumtype
=="adler32"):
2153 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2154 elif(checksumtype
=="crc32"):
2155 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2156 elif(checksumtype
=="crc64_ecma"):
2157 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2158 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2159 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2160 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2161 checksumoutstr
= hashlib
.new(checksumtype
);
2162 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
2163 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2165 catfileheadercshex
= format(0, 'x').lower();
2166 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2167 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
2168 nullstrecd
= formatspecs
[5].encode('UTF-8');
2169 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
2170 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
2171 catfp
.write(catfileout
);
2174 os
.fsync(catfp
.fileno());
2175 except io
.UnsupportedOperation
:
2177 except AttributeError:
2179 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2180 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2183 os
.fsync(catfp
.fileno());
2184 except io
.UnsupportedOperation
:
2186 except AttributeError:
2190 if(hasattr(sys
.stdout
, "buffer")):
2191 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2193 shutil
.copyfileobj(catfp
, sys
.stdout
);
2194 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
2195 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2197 upload_file_to_internet_file(catfp
, outfile
);
2205 create_alias_function("Pack", __file_format_name__
, "FromZipFile", PackArchiveFileFromZipFile
);
2207 if(not rarfile_support
):
2208 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2211 if(rarfile_support
):
2212 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2213 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2214 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2215 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
2216 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2217 outfile
= RemoveWindowsPath(outfile
);
2218 checksumtype
= checksumtype
.lower();
2219 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2220 checksumtype
="crc32";
2221 if(checksumtype
=="none"):
2223 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2225 if(compression
not in compressionlist
and compression
is None):
2226 compression
= "auto";
2228 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2229 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2230 if(os
.path
.exists(outfile
)):
2235 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2237 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
2240 fbasename
= os
.path
.splitext(outfile
)[0];
2241 fextname
= os
.path
.splitext(outfile
)[1];
2242 catfp
= CompressOpenFile(outfile
, compressionlevel
);
2243 catver
= formatspecs
[6];
2244 fileheaderver
= str(int(catver
.replace(".", "")));
2245 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
2246 catfp
.write(fileheader
.encode('UTF-8'));
2252 inodetocatinode
= {};
2253 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2255 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
2257 rarfp
= rarfile
.RarFile(infile
, "r");
2258 rartest
= rarfp
.testrar();
2260 VerbosePrintOut("Bad file found!");
2261 fnumfiles
= format(int(len(rarfp
.infolist())), 'x').lower();
2262 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2263 if(checksumtype
=="none" or checksumtype
==""):
2264 catfileheadercshex
= format(0, 'x').lower();
2265 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2266 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2267 elif(checksumtype
=="crc16_ccitt"):
2268 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2269 elif(checksumtype
=="adler32"):
2270 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2271 elif(checksumtype
=="crc32"):
2272 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2273 elif(checksumtype
=="crc64_ecma"):
2274 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2275 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2276 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2277 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2278 checksumoutstr
= hashlib
.new(checksumtype
);
2279 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
2280 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2282 catfileheadercshex
= format(0, 'x').lower();
2283 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2284 catfp
.write(fnumfilesa
.encode('UTF-8'));
2287 os
.fsync(catfp
.fileno());
2288 except io
.UnsupportedOperation
:
2290 except AttributeError:
2292 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
2295 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
2298 member
.external_attr
2300 except AttributeError:
2302 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
2305 member
.external_attr
2307 except AttributeError:
2312 catfhstart
= catfp
.tell();
2313 if(re
.findall("^[.|/]", member
.filename
)):
2314 fname
= member
.filename
;
2316 fname
= "./"+member
.filename
;
2317 rarinfo
= rarfp
.getinfo(member
.filename
);
2319 VerbosePrintOut(fname
);
2320 if(is_unix
and member
.external_attr
!=0):
2321 fpremode
= int(member
.external_attr
);
2322 elif(member
.is_file()):
2323 fpremode
= int(stat
.S_IFREG
+ 438);
2324 elif(member
.is_symlink()):
2325 fpremode
= int(stat
.S_IFLNK
+ 438);
2326 elif(member
.is_dir()):
2327 fpremode
= int(stat
.S_IFDIR
+ 511);
2328 if(is_windows
and member
.external_attr
!=0):
2329 fwinattributes
= format(int(member
.external_attr
), 'x').lower();
2331 fwinattributes
= format(int(0), 'x').lower();
2334 if(member
.is_file()):
2336 elif(member
.is_symlink()):
2338 elif(member
.is_dir()):
2342 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
2343 fcurfid
= format(int(curfid
), 'x').lower();
2344 fcurinode
= format(int(0), 'x').lower();
2345 curfid
= curfid
+ 1;
2346 fdev_minor
= format(int(0), 'x').lower();
2347 fdev_major
= format(int(0), 'x').lower();
2348 frdev_minor
= format(int(0), 'x').lower();
2349 frdev_major
= format(int(0), 'x').lower();
2351 fsize
= format(int("0"), 'x').lower();
2353 fsize
= format(int(member
.file_size
), 'x').lower();
2355 fsize
= format(int(member
.file_size
), 'x').lower();
2358 fatime
= format(int(member
.atime
.timestamp()), 'x').lower();
2360 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2361 except AttributeError:
2362 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2363 fmtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2366 fctime
= format(int(member
.ctime
.timestamp()), 'x').lower();
2368 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2369 except AttributeError:
2370 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2371 fbtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2372 if(is_unix
and member
.external_attr
!=0):
2373 fmode
= format(int(member
.external_attr
), 'x').lower();
2374 fchmode
= format(int(stat
.S_IMODE(member
.external_attr
)), 'x').lower();
2375 ftypemod
= format(int(stat
.S_IFMT(member
.external_attr
)), 'x').lower();
2376 elif(member
.is_file()):
2377 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
2378 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
2379 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
2380 elif(member
.is_symlink()):
2381 fmode
= format(int(stat
.S_IFLNK
+ 438), 'x').lower();
2382 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
2383 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
2384 elif(member
.is_dir()):
2385 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
2386 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
2387 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
2389 fuid
= format(int(os
.getuid()), 'x').lower();
2390 except AttributeError:
2391 fuid
= format(int(0), 'x').lower();
2393 fuid
= format(int(0), 'x').lower();
2395 fgid
= format(int(os
.getgid()), 'x').lower();
2396 except AttributeError:
2397 fgid
= format(int(0), 'x').lower();
2399 fgid
= format(int(0), 'x').lower();
2403 userinfo
= pwd
.getpwuid(os
.getuid());
2404 funame
= userinfo
.pw_name
;
2407 except AttributeError:
2415 groupinfo
= grp
.getgrgid(os
.getgid());
2416 fgname
= groupinfo
.gr_name
;
2419 except AttributeError:
2423 fcontents
= "".encode('UTF-8');
2425 fcontents
= rarfp
.read(member
.filename
);
2426 ftypehex
= format(ftype
, 'x').lower();
2427 extrafields
= format(len(extradata
), 'x').lower();
2428 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
2429 if(len(extradata
)>0):
2430 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2431 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2432 catfileoutstr
= AppendNullBytes([ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
], formatspecs
[5]);
2433 if(len(extradata
)>0):
2434 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2435 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
2436 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
2437 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
2438 if(checksumtype
=="none" or checksumtype
==""):
2439 catfileheadercshex
= format(0, 'x').lower();
2440 catfilecontentcshex
= format(0, 'x').lower();
2441 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2442 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2443 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
2444 elif(checksumtype
=="crc16_ccitt"):
2445 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2446 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
2447 elif(checksumtype
=="adler32"):
2448 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2449 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
2450 elif(checksumtype
=="crc32"):
2451 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2452 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
2453 elif(checksumtype
=="crc64_ecma"):
2454 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2455 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
2456 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2457 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2458 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
2459 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2460 checksumoutstr
= hashlib
.new(checksumtype
);
2461 checksumoutstr
.update("".encode('UTF-8'));
2462 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2463 checksumoutstr
= hashlib
.new(checksumtype
);
2464 checksumoutstr
.update(fcontents
);
2465 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
2467 catfileheadercshex
= format(0, 'x').lower();
2468 catfilecontentcshex
= format(0, 'x').lower();
2469 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2470 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2471 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
2472 if(checksumtype
=="none" or checksumtype
==""):
2473 catfileheadercshex
= format(0, 'x').lower()
2474 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2475 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower()
2476 elif(checksumtype
=="crc16_ccitt"):
2477 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower()
2478 elif(checksumtype
=="adler32"):
2479 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower()
2480 elif(checksumtype
=="crc32"):
2481 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower()
2482 elif(checksumtype
=="crc64_ecma"):
2483 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
2484 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2485 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
2486 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2487 checksumoutstr
= hashlib
.new(checksumtype
)
2488 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'))
2489 catfileheadercshex
= checksumoutstr
.hexdigest().lower()
2491 catfileheadercshex
= format(0, 'x').lower()
2492 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5])
2493 catfileoutstrecd
= catfileoutstr
.encode('UTF-8')
2494 nullstrecd
= formatspecs
[5].encode('UTF-8')
2495 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
2496 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
)
2497 catfp
.write(catfileout
)
2500 os
.fsync(catfp
.fileno())
2501 except io
.UnsupportedOperation
:
2503 except AttributeError:
2505 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2506 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
)
2509 os
.fsync(catfp
.fileno())
2510 except io
.UnsupportedOperation
:
2512 except AttributeError:
2516 if(hasattr(sys
.stdout
, "buffer")):
2517 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2519 shutil
.copyfileobj(catfp
, sys
.stdout
);
2520 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
2521 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2523 upload_file_to_internet_file(catfp
, outfile
);
2531 create_alias_function("Pack", __file_format_name__
, "FromRarFile", PackArchiveFileFromRarFile
);
2533 def ArchiveFileSeekToFileNum(infile
, seekto
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
2534 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2537 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2538 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
2539 if(checkcompressfile
=="tarfile"):
2540 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2541 if(checkcompressfile
=="zipfile"):
2542 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2543 if(rarfile_support
and checkcompressfile
=="rarfile"):
2544 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2545 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
2552 if(hasattr(sys
.stdin
, "buffer")):
2553 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
2555 shutil
.copyfileobj(sys
.stdin
, catfp
);
2557 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2561 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
2562 catfp
= download_file_from_internet_file(infile
);
2564 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2569 infile
= RemoveWindowsPath(infile
);
2570 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
2571 if(checkcompressfile
=="tarfile"):
2572 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2573 if(checkcompressfile
=="zipfile"):
2574 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2575 if(rarfile_support
and checkcompressfile
=="rarfile"):
2576 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2577 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
2579 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
2580 if(not compresscheck
):
2581 fextname
= os
.path
.splitext(infile
)[1];
2582 if(fextname
==".gz"):
2583 compresscheck
= "gzip";
2584 if(fextname
==".bz2"):
2585 compresscheck
= "bzip2";
2586 if(fextname
==".zst"):
2587 compresscheck
= "zstd";
2588 if(fextname
==".lz4" or fextname
==".clz4"):
2589 compresscheck
= "lz4";
2590 if(fextname
==".lzo" or fextname
==".lzop"):
2591 compresscheck
= "lzo";
2592 if(fextname
==".lzma" or fextname
==".xz"):
2593 compresscheck
= "lzma";
2594 if(not compresscheck
):
2596 catfp
= UncompressFile(infile
, formatspecs
, "rb");
2601 SeekToEndOfFile(catfp);
2603 SeekToEndOfFile(catfp);
2604 CatSize = catfp.tell();
2605 CatSizeEnd = CatSize;
2613 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
2614 catstring
= catheader
[0];
2615 catversion
= re
.findall(r
"([\d]+)$", catstring
);
2616 fprenumfiles
= catheader
[1];
2617 fnumfiles
= int(fprenumfiles
, 16);
2618 fprechecksumtype
= catheader
[2];
2619 fprechecksum
= catheader
[3];
2620 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
2621 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
2622 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
2623 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
2624 catfileheadercshex
= format(0, 'x').lower();
2625 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
2626 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2627 elif(fprechecksumtype
=="crc16_ccitt"):
2628 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2629 elif(fprechecksumtype
=="adler32"):
2630 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2631 elif(fprechecksumtype
=="crc32"):
2632 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2633 elif(fprechecksumtype
=="crc64_ecma"):
2634 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2635 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
2636 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2637 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
2638 checksumoutstr
= hashlib
.new(fprechecksumtype
);
2639 checksumoutstr
.update(fileheader
.encode('UTF-8'));
2640 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2642 catfileheadercshex
= format(0, 'x').lower();
2643 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2644 fheadtell
= len(fileheader
);
2645 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
2646 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(catfp
.tell()));
2648 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
2649 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
2650 if(seekto
>=fnumfiles
):
2651 seekto
= fnumfiles
- 1;
2657 prefhstart
= catfp
.tell();
2659 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
2661 preheaderdata
= ReadFileHeaderData(catfp
, 23, formatspecs
[5]);
2662 prefheadsize
= int(preheaderdata
[0], 16);
2663 preftype
= int(preheaderdata
[1], 16);
2664 if(re
.findall("^[.|/]", preheaderdata
[2])):
2665 prefname
= preheaderdata
[2];
2667 prefname
= "./"+preheaderdata
[2];
2668 prefbasedir
= os
.path
.dirname(prefname
);
2669 preflinkname
= preheaderdata
[3];
2670 prefsize
= int(preheaderdata
[4], 16);
2671 prefatime
= int(preheaderdata
[5], 16);
2672 prefmtime
= int(preheaderdata
[6], 16);
2673 prefctime
= int(preheaderdata
[7], 16);
2674 prefbtime
= int(preheaderdata
[8], 16);
2675 prefmode
= int(preheaderdata
[9], 16);
2676 prefchmode
= stat
.S_IMODE(prefmode
);
2677 preftypemod
= stat
.S_IFMT(prefmode
);
2678 prefuid
= int(preheaderdata
[10], 16);
2679 prefuname
= preheaderdata
[11];
2680 prefgid
= int(preheaderdata
[12], 16);
2681 prefgname
= preheaderdata
[13];
2682 fid
= int(preheaderdata
[14], 16);
2683 finode
= int(preheaderdata
[15], 16);
2684 flinkcount
= int(preheaderdata
[16], 16);
2685 prefdev_minor
= int(preheaderdata
[17], 16);
2686 prefdev_major
= int(preheaderdata
[18], 16);
2687 prefrdev_minor
= int(preheaderdata
[19], 16);
2688 prefrdev_major
= int(preheaderdata
[20], 16);
2689 prefextrasize
= int(preheaderdata
[21], 16);
2690 prefextrafields
= int(preheaderdata
[22], 16);
2691 extrafieldslist
= [];
2694 extraend
= extrastart
+ prefextrafields
;
2695 extrafieldslist
= [];
2696 if(extrastart
<extraend
):
2697 extrafieldslist
.append(preheaderdata
[extrastart
]);
2698 extrastart
= extrastart
+ 1;
2699 prefchecksumtype
= preheaderdata
[extrastart
].lower();
2700 prefcs
= preheaderdata
[extrastart
+ 1].lower();
2701 prefccs
= preheaderdata
[extrastart
+ 2].lower();
2703 extrafieldslist
= [];
2705 extrafieldslist
= ReadFileHeaderData(catfp
, prefextrafields
, formatspecs
[5]);
2706 checksumsval
= ReadFileHeaderData(catfp
, 3, formatspecs
[5]);
2707 prefchecksumtype
= checksumsval
[0].lower();
2708 prefcs
= checksumsval
[1].lower();
2709 prefccs
= checksumsval
[2].lower();
2712 hcmax
= len(preheaderdata
) - 2;
2714 hcmax
= len(preheaderdata
);
2717 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
2719 prefnumfields
= 24 + prefextrafields
;
2720 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
2722 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
2723 prenewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
2724 elif(prefchecksumtype
=="adler32"):
2725 prenewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2726 elif(prefchecksumtype
=="crc32"):
2727 prenewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2728 elif(prefchecksumtype
=="crc64_ecma"):
2729 prenewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2730 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
2731 prenewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2732 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
2733 checksumoutstr
= hashlib
.new(prefchecksumtype
);
2734 checksumoutstr
.update(hout
.encode('UTF-8'));
2735 prenewfcs
= checksumoutstr
.hexdigest().lower();
2736 if(prefcs
!=prenewfcs
and not skipchecksum
):
2737 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
2739 valid_archive
= False;
2740 invalid_archive
= True;
2741 prefhend
= catfp
.tell() - 1;
2742 prefcontentstart
= catfp
.tell();
2744 pyhascontents
= False;
2746 prefcontents
= catfp
.read(prefsize
);
2747 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
2749 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
2750 prenewfccs
= format(crc16(prefcontents
) & 0xffff, '04x').lower();
2751 elif(prefchecksumtype
=="crc16_ccitt"):
2752 prenewfcs
= format(crc16_ccitt(prefcontents
) & 0xffff, '04x').lower();
2753 elif(prefchecksumtype
=="adler32"):
2754 prenewfccs
= format(zlib
.adler32(prefcontents
) & 0xffffffff, '08x').lower();
2755 elif(prefchecksumtype
=="crc32"):
2756 prenewfccs
= format(crc32(prefcontents
) & 0xffffffff, '08x').lower();
2757 elif(prefchecksumtype
=="crc64_ecma"):
2758 prenewfcs
= format(crc64_ecma(prefcontents
) & 0xffffffffffffffff, '016x').lower();
2759 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
2760 prenewfcs
= format(crc64_iso(prefcontents
) & 0xffffffffffffffff, '016x').lower();
2761 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
2762 checksumoutstr
= hashlib
.new(prefchecksumtype
);
2763 checksumoutstr
.update(prefcontents
);
2764 prenewfccs
= checksumoutstr
.hexdigest().lower();
2765 pyhascontents
= True;
2766 if(prefccs
!=prenewfccs
and not skipchecksum
):
2767 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
2771 catfp
.seek(seekstart
, 0);
2773 catfheadsize
= int(preheaderdata
[0], 16);
2774 catftype
= int(preheaderdata
[1], 16);
2775 if(re
.findall("^[.|/]", preheaderdata
[2])):
2776 catfname
= preheaderdata
[2];
2778 catfname
= "./"+preheaderdata
[2];
2779 catflinkname
= preheaderdata
[3];
2780 catfsize
= int(preheaderdata
[4], 16);
2781 catfbasedir
= os
.path
.dirname(catfname
);
2782 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
2784 catlist
.update({'catfp': catfp
});
2789 create_alias_function("", __file_format_name__
, "SeekToFileNum", ArchiveFileSeekToFileNum
);
2791 def ArchiveFileSeekToFileName(infile
, seekfile
=None, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
2792 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2795 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2796 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
2797 if(checkcompressfile
=="tarfile"):
2798 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2799 if(checkcompressfile
=="zipfile"):
2800 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2801 if(rarfile_support
and checkcompressfile
=="rarfile"):
2802 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2803 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
2810 if(hasattr(sys
.stdin
, "buffer")):
2811 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
2813 shutil
.copyfileobj(sys
.stdin
, catfp
);
2815 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2819 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
2820 catfp
= download_file_from_internet_file(infile
);
2821 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2827 infile
= RemoveWindowsPath(infile
);
2828 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
2829 if(checkcompressfile
=="tarfile"):
2830 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2831 if(checkcompressfile
=="zipfile"):
2832 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2833 if(rarfile_support
and checkcompressfile
=="rarfile"):
2834 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2835 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
2837 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
2838 if(not compresscheck
):
2839 fextname
= os
.path
.splitext(infile
)[1];
2840 if(fextname
==".gz"):
2841 compresscheck
= "gzip";
2842 if(fextname
==".bz2"):
2843 compresscheck
= "bzip2";
2844 if(fextname
==".zst"):
2845 compresscheck
= "zstd";
2846 if(fextname
==".lz4" or fextname
==".clz4"):
2847 compresscheck
= "lz4";
2848 if(fextname
==".lzo" or fextname
==".lzop"):
2849 compresscheck
= "lzo";
2850 if(fextname
==".lzma" or fextname
==".xz"):
2851 compresscheck
= "lzma";
2852 if(not compresscheck
):
2854 catfp
= UncompressFile(infile
, formatspecs
, "rb");
2859 SeekToEndOfFile(catfp);
2861 SeekToEndOfFile(catfp);
2862 CatSize = catfp.tell();
2863 CatSizeEnd = CatSize;
2871 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
2872 catstring
= catheader
[0];
2873 catversion
= re
.findall(r
"([\d]+)$", catstring
);
2874 fprenumfiles
= catheader
[1];
2875 fnumfiles
= int(fprenumfiles
, 16);
2876 fprechecksumtype
= catheader
[2];
2877 fprechecksum
= catheader
[3];
2878 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
2879 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
2880 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
2881 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
2882 catfileheadercshex
= format(0, 'x').lower();
2883 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
2884 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2885 elif(fprechecksumtype
=="crc16_ccitt"):
2886 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2887 elif(fprechecksumtype
=="adler32"):
2888 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2889 elif(fprechecksumtype
=="crc32"):
2890 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2891 elif(fprechecksumtype
=="crc64_ecma"):
2892 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2893 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
2894 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2895 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
2896 checksumoutstr
= hashlib
.new(fprechecksumtype
);
2897 checksumoutstr
.update(fileheader
.encode('UTF-8'));
2898 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2900 catfileheadercshex
= format(0, 'x').lower();
2901 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2902 fheadtell
= len(fileheader
);
2903 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
2904 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(catfp
.tell()));
2906 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
2907 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
2908 seekto
= fnumfiles
- 1
2913 prefhstart
= catfp
.tell();
2915 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
2917 preheaderdata
= ReadFileHeaderData(catfp
, 23, formatspecs
[5]);
2918 prefheadsize
= int(preheaderdata
[0], 16);
2919 preftype
= int(preheaderdata
[1], 16);
2920 if(re
.findall("^[.|/]", preheaderdata
[2])):
2921 prefname
= preheaderdata
[2];
2923 prefname
= "./"+preheaderdata
[2];
2924 prefbasedir
= os
.path
.dirname(prefname
);
2925 preflinkname
= preheaderdata
[3];
2926 prefsize
= int(preheaderdata
[4], 16);
2927 prefatime
= int(preheaderdata
[5], 16);
2928 prefmtime
= int(preheaderdata
[6], 16);
2929 prefctime
= int(preheaderdata
[7], 16);
2930 prefbtime
= int(preheaderdata
[8], 16);
2931 prefmode
= int(preheaderdata
[9], 16);
2932 prefchmode
= stat
.S_IMODE(prefmode
);
2933 preftypemod
= stat
.S_IFMT(prefmode
);
2934 prefuid
= int(preheaderdata
[10], 16);
2935 prefuname
= preheaderdata
[11];
2936 prefgid
= int(preheaderdata
[12], 16);
2937 prefgname
= preheaderdata
[13];
2938 fid
= int(preheaderdata
[14], 16);
2939 finode
= int(preheaderdata
[15], 16);
2940 flinkcount
= int(preheaderdata
[16], 16);
2941 prefdev_minor
= int(preheaderdata
[17], 16);
2942 prefdev_major
= int(preheaderdata
[18], 16);
2943 prefrdev_minor
= int(preheaderdata
[19], 16);
2944 prefrdev_major
= int(preheaderdata
[20], 16);
2945 prefextrasize
= int(preheaderdata
[21], 16);
2946 prefextrafields
= int(preheaderdata
[22], 16);
2947 extrafieldslist
= [];
2950 extraend
= extrastart
+ prefextrafields
;
2951 extrafieldslist
= [];
2952 if(extrastart
<extraend
):
2953 extrafieldslist
.append(preheaderdata
[extrastart
]);
2954 extrastart
= extrastart
+ 1;
2955 prefchecksumtype
= preheaderdata
[extrastart
].lower();
2956 prefcs
= preheaderdata
[extrastart
+ 1].lower();
2957 prefccs
= preheaderdata
[extrastart
+ 2].lower();
2959 extrafieldslist
= [];
2961 extrafieldslist
= ReadFileHeaderData(catfp
, prefextrafields
, formatspecs
[5]);
2962 checksumsval
= ReadFileHeaderData(catfp
, 3, formatspecs
[5]);
2963 prefchecksumtype
= checksumsval
[0].lower();
2964 prefcs
= checksumsval
[1].lower();
2965 prefccs
= checksumsval
[2].lower();
2968 hcmax
= len(preheaderdata
) - 2;
2970 hcmax
= len(preheaderdata
);
2973 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
2975 prefnumfields
= 24 + prefextrafields
;
2976 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
2978 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
2979 prenewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
2980 elif(prefchecksumtype
=="adler32"):
2981 prenewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2982 elif(prefchecksumtype
=="crc32"):
2983 prenewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2984 elif(prefchecksumtype
=="crc64_ecma"):
2985 prenewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2986 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
2987 prenewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2988 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
2989 checksumoutstr
= hashlib
.new(prefchecksumtype
);
2990 checksumoutstr
.update(hout
.encode('UTF-8'));
2991 prenewfcs
= checksumoutstr
.hexdigest().lower();
2992 if(prefcs
!=prenewfcs
and not skipchecksum
):
2993 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
2995 valid_archive
= False;
2996 invalid_archive
= True;
2997 prefhend
= catfp
.tell() - 1;
2998 prefcontentstart
= catfp
.tell();
3000 pyhascontents
= False;
3002 prefcontents
= catfp
.read(prefsize
);
3003 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
3005 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
3006 prenewfccs
= format(crc16(prefcontents
) & 0xffff, '04x').lower();
3007 elif(prefchecksumtype
=="crc16_ccitt"):
3008 prenewfcs
= format(crc16_ccitt(prefcontents
) & 0xffff, '04x').lower();
3009 elif(prefchecksumtype
=="adler32"):
3010 prenewfccs
= format(zlib
.adler32(prefcontents
) & 0xffffffff, '08x').lower();
3011 elif(prefchecksumtype
=="crc32"):
3012 prenewfccs
= format(crc32(prefcontents
) & 0xffffffff, '08x').lower();
3013 elif(prefchecksumtype
=="crc64_ecma"):
3014 prenewfcs
= format(crc64_ecma(prefcontents
) & 0xffffffffffffffff, '016x').lower();
3015 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
3016 prenewfcs
= format(crc64_iso(prefcontents
) & 0xffffffffffffffff, '016x').lower();
3017 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
3018 checksumoutstr
= hashlib
.new(prefchecksumtype
);
3019 checksumoutstr
.update(prefcontents
);
3020 prenewfccs
= checksumoutstr
.hexdigest().lower();
3021 pyhascontents
= True;
3022 if(prefccs
!=prenewfccs
and not skipchecksum
):
3023 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
3028 prefname
= preheaderdata
[2];
3029 if(re
.findall("^[.|/]", preheaderdata
[2])):
3030 prefname
= preheaderdata
[2];
3032 prefname
= "./"+preheaderdata
[2];
3033 if(prefname
==seekfile
):
3036 catfp
.seek(seekstart
, 0);
3038 catfheadsize
= int(preheaderdata
[0], 16);
3039 catftype
= int(preheaderdata
[1], 16);
3040 if(re
.findall("^[.|/]", preheaderdata
[2])):
3041 catfname
= preheaderdata
[2];
3043 catfname
= "./"+preheaderdata
[2];
3044 catflinkname
= preheaderdata
[3];
3045 catfsize
= int(preheaderdata
[4], 16);
3046 catfbasedir
= os
.path
.dirname(catfname
);
3048 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
3052 catlist
.update({'catfp': catfp
});
3057 create_alias_function("", __file_format_name__
, "SeekToFileName", ArchiveFileSeekToFileName
);
3059 def ArchiveFileValidate(infile
, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
3061 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
3062 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3065 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3066 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
3067 if(checkcompressfile
=="tarfile"):
3068 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3069 if(checkcompressfile
=="zipfile"):
3070 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3071 if(rarfile_support
and checkcompressfile
=="rarfile"):
3072 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3073 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3080 if(hasattr(sys
.stdin
, "buffer")):
3081 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
3083 shutil
.copyfileobj(sys
.stdin
, catfp
);
3085 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3089 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
3090 catfp
= download_file_from_internet_file(infile
);
3091 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3097 infile
= RemoveWindowsPath(infile
);
3098 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
3099 if(checkcompressfile
=="tarfile"):
3100 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3101 if(checkcompressfile
=="zipfile"):
3102 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3103 if(rarfile_support
and checkcompressfile
=="rarfile"):
3104 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3105 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3107 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
3108 if(not compresscheck
):
3109 fextname
= os
.path
.splitext(infile
)[1];
3110 if(fextname
==".gz"):
3111 compresscheck
= "gzip";
3112 if(fextname
==".bz2"):
3113 compresscheck
= "bzip2";
3114 if(fextname
==".zst"):
3115 compresscheck
= "zstd";
3116 if(fextname
==".lz4" or fextname
==".clz4"):
3117 compresscheck
= "lz4";
3118 if(fextname
==".lzo" or fextname
==".lzop"):
3119 compresscheck
= "lzo";
3120 if(fextname
==".lzma" or fextname
==".xz"):
3121 compresscheck
= "lzma";
3122 if(not compresscheck
):
3124 catfp
= UncompressFile(infile
, formatspecs
, "rb");
3129 SeekToEndOfFile(catfp);
3131 SeekToEndOfFile(catfp);
3132 CatSize = catfp.tell();
3133 CatSizeEnd = CatSize;
3141 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
3142 catstring
= catheader
[0];
3143 catversion
= re
.findall(r
"([\d]+)$", catstring
);
3144 fprenumfiles
= catheader
[1];
3145 fnumfiles
= int(fprenumfiles
, 16);
3146 fprechecksumtype
= catheader
[2];
3147 fprechecksum
= catheader
[3];
3149 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
3150 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3151 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
3152 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
3153 catfileheadercshex
= format(0, 'x').lower();
3154 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
3155 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3156 elif(fprechecksumtype
=="crc16_ccitt"):
3157 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3158 elif(fprechecksumtype
=="adler32"):
3159 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3160 elif(fprechecksumtype
=="crc32"):
3161 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3162 elif(fprechecksumtype
=="crc64_ecma"):
3163 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3164 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
3165 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3166 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
3167 checksumoutstr
= hashlib
.new(fprechecksumtype
);
3168 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3169 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3171 catfileheadercshex
= format(0, 'x').lower();
3172 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3173 valid_archive
= True;
3174 invalid_archive
= False;
3176 VerbosePrintOut("Checking File Header Checksum of file at offset " + str(0));
3177 VerbosePrintOut(infile
);
3178 if(fprechecksum
==catfileheadercshex
):
3180 VerbosePrintOut("File Header Checksum Passed");
3183 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
3184 valid_archive
= False;
3185 invalid_archive
= True;
3186 while(il
<fnumfiles
):
3187 catfhstart
= catfp
.tell();
3189 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3191 catheaderdata
= ReadFileHeaderData(catfp
, 23, formatspecs
[5]);
3192 catfheadsize
= int(catheaderdata
[0], 16);
3193 catftype
= int(catheaderdata
[1], 16);
3194 if(re
.findall("^[.|/]", catheaderdata
[2])):
3195 catfname
= catheaderdata
[2];
3197 catfname
= "./"+catheaderdata
[2];
3198 catfbasedir
= os
.path
.dirname(catfname
);
3199 catflinkname
= catheaderdata
[3];
3200 catfsize
= int(catheaderdata
[4], 16);
3201 catfatime
= int(catheaderdata
[5], 16);
3202 catfmtime
= int(catheaderdata
[6], 16);
3203 catfctime
= int(catheaderdata
[7], 16);
3204 catfbtime
= int(catheaderdata
[8], 16);
3205 catfmode
= int(catheaderdata
[9], 16);
3206 catfchmode
= stat
.S_IMODE(catfmode
);
3207 catftypemod
= stat
.S_IFMT(catfmode
);
3208 catfuid
= int(catheaderdata
[10], 16);
3209 catfuname
= catheaderdata
[11];
3210 catfgid
= int(catheaderdata
[12], 16);
3211 catfgname
= catheaderdata
[13];
3212 fid
= int(catheaderdata
[14], 16);
3213 finode
= int(catheaderdata
[15], 16);
3214 flinkcount
= int(catheaderdata
[16], 16);
3215 catfdev_minor
= int(catheaderdata
[17], 16);
3216 catfdev_major
= int(catheaderdata
[18], 16);
3217 catfrdev_minor
= int(catheaderdata
[19], 16);
3218 catfrdev_major
= int(catheaderdata
[20], 16);
3219 catfextrasize
= int(catheaderdata
[21], 16);
3220 catfextrafields
= int(catheaderdata
[22], 16);
3221 extrafieldslist
= [];
3224 extraend
= extrastart
+ catfextrafields
;
3225 extrafieldslist
= [];
3226 if(extrastart
<extraend
):
3227 extrafieldslist
.append(catheaderdata
[extrastart
]);
3228 extrastart
= extrastart
+ 1;
3229 catfchecksumtype
= catheaderdata
[extrastart
].lower();
3230 catfcs
= catheaderdata
[extrastart
+ 1].lower();
3231 catfccs
= catheaderdata
[extrastart
+ 2].lower();
3233 extrafieldslist
= [];
3235 extrafieldslist
= ReadFileHeaderData(catfp
, catfextrafields
, formatspecs
[5]);
3236 checksumsval
= ReadFileHeaderData(catfp
, 3, formatspecs
[5]);
3237 catfchecksumtype
= checksumsval
[0].lower();
3238 catfcs
= checksumsval
[1].lower();
3239 catfccs
= checksumsval
[2].lower();
3242 hcmax
= len(catheaderdata
) - 2;
3244 hcmax
= len(catheaderdata
);
3247 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[5]);
3249 catfnumfields
= 24 + catfextrafields
;
3250 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
3252 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
3253 catnewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
3254 elif(catfchecksumtype
=="adler32"):
3255 catnewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3256 elif(catfchecksumtype
=="crc32"):
3257 catnewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3258 elif(catfchecksumtype
=="crc64_ecma"):
3259 catnewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3260 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
3261 catnewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3262 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
3263 checksumoutstr
= hashlib
.new(catfchecksumtype
);
3264 checksumoutstr
.update(hout
.encode('UTF-8'));
3265 catnewfcs
= checksumoutstr
.hexdigest().lower();
3267 VerbosePrintOut("Checking File Header Checksum of file at offset " + str(catfhstart
));
3268 VerbosePrintOut(catfname
);
3269 if(catfcs
==catnewfcs
):
3271 VerbosePrintOut("File Header Checksum Passed");
3274 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfhstart
));
3275 valid_archive
= False;
3276 invalid_archive
= True;
3277 catfhend
= catfp
.tell() - 1;
3278 catfcontentstart
= catfp
.tell();
3280 pyhascontents
= False;
3282 catfcontents
= catfp
.read(catfsize
);
3283 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
3285 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
3286 catnewfccs
= format(crc16(catfcontents
) & 0xffff, '04x').lower();
3287 elif(catfchecksumtype
=="crc16_ccitt"):
3288 catnewfcs
= format(crc16_ccitt(catfcontents
) & 0xffff, '04x').lower();
3289 elif(catfchecksumtype
=="adler32"):
3290 catnewfccs
= format(zlib
.adler32(catfcontents
) & 0xffffffff, '08x').lower();
3291 elif(catfchecksumtype
=="crc32"):
3292 catnewfccs
= format(crc32(catfcontents
) & 0xffffffff, '08x').lower();
3293 elif(catfchecksumtype
=="crc64_ecma"):
3294 catnewfcs
= format(crc64_ecma(catfcontents
) & 0xffffffffffffffff, '016x').lower();
3295 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
3296 catnewfcs
= format(crc64_iso(catfcontents
) & 0xffffffffffffffff, '016x').lower();
3297 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
3298 checksumoutstr
= hashlib
.new(catfchecksumtype
);
3299 checksumoutstr
.update(catfcontents
);
3300 catnewfccs
= checksumoutstr
.hexdigest().lower();
3301 pyhascontents
= True;
3303 VerbosePrintOut("Checking File Content Checksum of file at offset " + str(catfcontentstart
));
3304 VerbosePrintOut(catfname
);
3305 if(catfccs
==catnewfccs
):
3307 VerbosePrintOut("File Content Checksum Passed");
3310 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfcontentstart
));
3311 valid_archive
= False;
3312 invalid_archive
= True;
3314 VerbosePrintOut("");
3327 create_alias_function("", __file_format_name__
, "Validate", ArchiveFileValidate
);
3329 def ArchiveFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3330 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3333 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3334 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
3335 if(checkcompressfile
=="tarfile"):
3336 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3337 if(checkcompressfile
=="zipfile"):
3338 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3339 if(rarfile_support
and checkcompressfile
=="rarfile"):
3340 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3341 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3348 if(hasattr(sys
.stdin
, "buffer")):
3349 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
3351 shutil
.copyfileobj(sys
.stdin
, catfp
);
3353 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3357 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
3358 catfp
= download_file_from_internet_file(infile
);
3359 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3365 infile
= RemoveWindowsPath(infile
);
3366 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
3367 if(checkcompressfile
=="tarfile"):
3368 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3369 if(checkcompressfile
=="zipfile"):
3370 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3371 if(rarfile_support
and checkcompressfile
=="rarfile"):
3372 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3373 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3375 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
3376 if(not compresscheck
):
3377 fextname
= os
.path
.splitext(infile
)[1];
3378 if(fextname
==".gz"):
3379 compresscheck
= "gzip";
3380 if(fextname
==".bz2"):
3381 compresscheck
= "bzip2";
3382 if(fextname
==".zst"):
3383 compresscheck
= "zstd";
3384 if(fextname
==".lz4" or fextname
==".clz4"):
3385 compresscheck
= "lz4";
3386 if(fextname
==".lzo" or fextname
==".lzop"):
3387 compresscheck
= "lzo";
3388 if(fextname
==".lzma" or fextname
==".xz"):
3389 compresscheck
= "lzma";
3390 if(not compresscheck
):
3392 catfp
= UncompressFile(infile
, formatspecs
, "rb");
3397 SeekToEndOfFile(catfp);
3399 SeekToEndOfFile(catfp);
3400 CatSize = catfp.tell();
3401 CatSizeEnd = CatSize;
3409 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
3410 catstring
= catheader
[0];
3411 catversion
= re
.findall(r
"([\d]+)$", catstring
);
3412 fprenumfiles
= catheader
[1];
3413 fnumfiles
= int(fprenumfiles
, 16);
3414 fprechecksumtype
= catheader
[2];
3415 fprechecksum
= catheader
[3];
3416 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
3417 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3418 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
3419 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
3420 catfileheadercshex
= format(0, 'x').lower();
3421 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
3422 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3423 elif(fprechecksumtype
=="crc16_ccitt"):
3424 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3425 elif(fprechecksumtype
=="adler32"):
3426 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3427 elif(fprechecksumtype
=="crc32"):
3428 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3429 elif(fprechecksumtype
=="crc64_ecma"):
3430 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3431 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
3432 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3433 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
3434 checksumoutstr
= hashlib
.new(fprechecksumtype
);
3435 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3436 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3438 catfileheadercshex
= format(0, 'x').lower();
3439 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3440 fheadtell
= len(fileheader
);
3441 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
3442 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
3444 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
3445 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
3446 if(seekstart
<0 and seekstart
>fnumfiles
):
3448 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
3449 seekend
= fnumfiles
;
3450 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
3451 seekend
= fnumfiles
- abs(seekend
);
3454 while(il
< seekstart
):
3455 prefhstart
= catfp
.tell();
3457 preheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3459 preheaderdata
= ReadFileHeaderData(catfp
, 23, formatspecs
[5]);
3460 prefheadsize
= int(preheaderdata
[0], 16);
3461 preftype
= int(preheaderdata
[1], 16);
3462 if(re
.findall("^[.|/]", preheaderdata
[2])):
3463 prefname
= preheaderdata
[2];
3465 prefname
= "./"+preheaderdata
[2];
3466 prefbasedir
= os
.path
.dirname(prefname
);
3467 preflinkname
= preheaderdata
[3];
3468 prefsize
= int(preheaderdata
[4], 16);
3469 prefatime
= int(preheaderdata
[5], 16);
3470 prefmtime
= int(preheaderdata
[6], 16);
3471 prefctime
= int(preheaderdata
[7], 16);
3472 prefbtime
= int(preheaderdata
[8], 16);
3473 prefmode
= int(preheaderdata
[9], 16);
3474 prefchmode
= stat
.S_IMODE(prefmode
);
3475 preftypemod
= stat
.S_IFMT(prefmode
);
3476 prefuid
= int(preheaderdata
[10], 16);
3477 prefuname
= preheaderdata
[11];
3478 prefgid
= int(preheaderdata
[12], 16);
3479 prefgname
= preheaderdata
[13];
3480 fid
= int(preheaderdata
[14], 16);
3481 finode
= int(preheaderdata
[15], 16);
3482 flinkcount
= int(preheaderdata
[16], 16);
3483 prefdev_minor
= int(preheaderdata
[17], 16);
3484 prefdev_major
= int(preheaderdata
[18], 16);
3485 prefrdev_minor
= int(preheaderdata
[19], 16);
3486 prefrdev_major
= int(preheaderdata
[20], 16);
3487 prefextrasize
= int(preheaderdata
[21], 16);
3488 prefextrafields
= int(preheaderdata
[22], 16);
3489 extrafieldslist
= [];
3492 extraend
= extrastart
+ prefextrafields
;
3493 extrafieldslist
= [];
3494 if(extrastart
<extraend
):
3495 extrafieldslist
.append(preheaderdata
[extrastart
]);
3496 extrastart
= extrastart
+ 1;
3497 prefchecksumtype
= preheaderdata
[extrastart
].lower();
3498 prefcs
= preheaderdata
[extrastart
+ 1].lower();
3499 prefccs
= preheaderdata
[extrastart
+ 2].lower();
3501 extrafieldslist
= [];
3503 extrafieldslist
= ReadFileHeaderData(catfp
, prefextrafields
, formatspecs
[5]);
3504 checksumsval
= ReadFileHeaderData(catfp
, 3, formatspecs
[5]);
3505 prefchecksumtype
= checksumsval
[0].lower();
3506 prefcs
= checksumsval
[1].lower();
3507 prefccs
= checksumsval
[2].lower();
3510 hcmax
= len(preheaderdata
) - 2;
3512 hcmax
= len(preheaderdata
);
3515 hout
= hout
+ AppendNullByte(preheaderdata
[hc
], formatspecs
[5]);
3517 prefnumfields
= 24 + prefextrafields
;
3518 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
3520 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
3521 prenewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
3522 elif(prefchecksumtype
=="adler32"):
3523 prenewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3524 elif(prefchecksumtype
=="crc32"):
3525 prenewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3526 elif(prefchecksumtype
=="crc64_ecma"):
3527 prenewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3528 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
3529 prenewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3530 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
3531 checksumoutstr
= hashlib
.new(prefchecksumtype
);
3532 checksumoutstr
.update(hout
.encode('UTF-8'));
3533 prenewfcs
= checksumoutstr
.hexdigest().lower();
3534 if(prefcs
!=prenewfcs
and not skipchecksum
):
3535 VerbosePrintOut("File Header Checksum Error with file " + prefname
+ " at offset " + str(prefhstart
));
3537 valid_archive
= False;
3538 invalid_archive
= True;
3539 prefhend
= catfp
.tell() - 1;
3540 prefcontentstart
= catfp
.tell();
3542 pyhascontents
= False;
3544 prefcontents
= catfp
.read(prefsize
);
3545 if(prefchecksumtype
=="none" or prefchecksumtype
==""):
3547 elif(prefchecksumtype
=="crc16" or prefchecksumtype
=="crc16_ansi" or prefchecksumtype
=="crc16_ibm"):
3548 prenewfccs
= format(crc16(prefcontents
) & 0xffff, '04x').lower();
3549 elif(prefchecksumtype
=="crc16_ccitt"):
3550 prenewfcs
= format(crc16_ccitt(prefcontents
) & 0xffff, '04x').lower();
3551 elif(prefchecksumtype
=="adler32"):
3552 prenewfccs
= format(zlib
.adler32(prefcontents
) & 0xffffffff, '08x').lower();
3553 elif(prefchecksumtype
=="crc32"):
3554 prenewfccs
= format(crc32(prefcontents
) & 0xffffffff, '08x').lower();
3555 elif(prefchecksumtype
=="crc64_ecma"):
3556 prenewfcs
= format(crc64_ecma(prefcontents
) & 0xffffffffffffffff, '016x').lower();
3557 elif(prefchecksumtype
=="crc64" or prefchecksumtype
=="crc64_iso"):
3558 prenewfcs
= format(crc64_iso(prefcontents
) & 0xffffffffffffffff, '016x').lower();
3559 elif(CheckSumSupportAlt(prefchecksumtype
, hashlib_guaranteed
)):
3560 checksumoutstr
= hashlib
.new(prefchecksumtype
);
3561 checksumoutstr
.update(prefcontents
);
3562 prenewfccs
= checksumoutstr
.hexdigest().lower();
3563 pyhascontents
= True;
3564 if(prefccs
!=prenewfccs
and not skipchecksum
):
3565 VerbosePrintOut("File Content Checksum Error with file " + prefname
+ " at offset " + str(prefcontentstart
));
3569 fileidnum
= seekstart
;
3571 while(fileidnum
<seekend
):
3572 catfhstart
= catfp
.tell();
3574 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3576 catheaderdata
= ReadFileHeaderData(catfp
, 23, formatspecs
[5]);
3577 catfheadsize
= int(catheaderdata
[0], 16);
3578 catftype
= int(catheaderdata
[1], 16);
3579 if(re
.findall("^[.|/]", catheaderdata
[2])):
3580 catfname
= catheaderdata
[2];
3582 catfname
= "./"+catheaderdata
[2];
3583 catfbasedir
= os
.path
.dirname(catfname
);
3584 catflinkname
= catheaderdata
[3];
3585 catfsize
= int(catheaderdata
[4], 16);
3586 catfatime
= int(catheaderdata
[5], 16);
3587 catfmtime
= int(catheaderdata
[6], 16);
3588 catfctime
= int(catheaderdata
[7], 16);
3589 catfbtime
= int(catheaderdata
[8], 16);
3590 catfmode
= int(catheaderdata
[9], 16);
3591 catfchmode
= stat
.S_IMODE(catfmode
);
3592 catftypemod
= stat
.S_IFMT(catfmode
);
3593 catfuid
= int(catheaderdata
[10], 16);
3594 catfuname
= catheaderdata
[11];
3595 catfgid
= int(catheaderdata
[12], 16);
3596 catfgname
= catheaderdata
[13];
3597 fid
= int(catheaderdata
[14], 16);
3598 finode
= int(catheaderdata
[15], 16);
3599 flinkcount
= int(catheaderdata
[16], 16);
3600 catfdev_minor
= int(catheaderdata
[17], 16);
3601 catfdev_major
= int(catheaderdata
[18], 16);
3602 catfrdev_minor
= int(catheaderdata
[19], 16);
3603 catfrdev_major
= int(catheaderdata
[20], 16);
3604 catfextrasize
= int(catheaderdata
[21], 16);
3605 catfextrafields
= int(catheaderdata
[22], 16);
3606 extrafieldslist
= [];
3609 extraend
= extrastart
+ catfextrafields
;
3610 extrafieldslist
= [];
3611 if(extrastart
<extraend
):
3612 extrafieldslist
.append(catheaderdata
[extrastart
]);
3613 extrastart
= extrastart
+ 1;
3614 catfchecksumtype
= catheaderdata
[extrastart
].lower();
3615 catfcs
= catheaderdata
[extrastart
+ 1].lower();
3616 catfccs
= catheaderdata
[extrastart
+ 2].lower();
3618 extrafieldslist
= [];
3620 extrafieldslist
= ReadFileHeaderData(catfp
, catfextrafields
, formatspecs
[5]);
3621 checksumsval
= ReadFileHeaderData(catfp
, 3, formatspecs
[5]);
3622 catfchecksumtype
= checksumsval
[0].lower();
3623 catfcs
= checksumsval
[1].lower();
3624 catfccs
= checksumsval
[2].lower();
3627 hcmax
= len(catheaderdata
) - 2;
3629 hcmax
= len(catheaderdata
);
3632 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[5]);
3634 catfnumfields
= 24 + catfextrafields
;
3635 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
3637 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
3638 catnewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
3639 elif(catfchecksumtype
=="adler32"):
3640 catnewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3641 elif(catfchecksumtype
=="crc32"):
3642 catnewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3643 elif(catfchecksumtype
=="crc64_ecma"):
3644 catnewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3645 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
3646 catnewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3647 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
3648 checksumoutstr
= hashlib
.new(catfchecksumtype
);
3649 checksumoutstr
.update(hout
.encode('UTF-8'));
3650 catnewfcs
= checksumoutstr
.hexdigest().lower();
3651 if(catfcs
!=catnewfcs
and not skipchecksum
):
3652 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfhstart
));
3654 catfhend
= catfp
.tell() - 1;
3655 catfcontentstart
= catfp
.tell();
3657 pyhascontents
= False;
3658 if(catfsize
>0 and not listonly
):
3659 catfcontents
= catfp
.read(catfsize
);
3660 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
3662 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
3663 catnewfccs
= format(crc16(catfcontents
) & 0xffff, '04x').lower();
3664 elif(catfchecksumtype
=="crc16_ccitt"):
3665 catnewfcs
= format(crc16_ccitt(catfcontents
) & 0xffff, '04x').lower();
3666 elif(catfchecksumtype
=="adler32"):
3667 catnewfccs
= format(zlib
.adler32(catfcontents
) & 0xffffffff, '08x').lower();
3668 elif(catfchecksumtype
=="crc32"):
3669 catnewfccs
= format(crc32(catfcontents
) & 0xffffffff, '08x').lower();
3670 elif(catfchecksumtype
=="crc64_ecma"):
3671 catnewfcs
= format(crc64_ecma(catfcontents
) & 0xffffffffffffffff, '016x').lower();
3672 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
3673 catnewfcs
= format(crc64_iso(catfcontents
) & 0xffffffffffffffff, '016x').lower();
3674 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
3675 checksumoutstr
= hashlib
.new(catfchecksumtype
);
3676 checksumoutstr
.update(catfcontents
);
3677 catnewfccs
= checksumoutstr
.hexdigest().lower();
3678 pyhascontents
= True;
3679 if(catfccs
!=catnewfccs
and skipchecksum
):
3680 VerbosePrintOut("File Content Checksum Error with file " + catfname
+ " at offset " + str(catfcontentstart
));
3682 if(catfsize
>0 and listonly
):
3683 catfp
.seek(catfsize
, 1);
3684 pyhascontents
= False;
3686 catfcontentend
= catfp
.tell() - 1;
3687 catlist
['ffilelist'].update({realidnum
: {'fid': realidnum
, 'fidalt': fileidnum
, 'fheadersize': catfheadsize
, 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
, 'fatime': catfatime
, 'fmtime': catfmtime
, 'fctime': catfctime
, 'fbtime': catfbtime
, 'fmode': catfmode
, 'fchmode': catfchmode
, 'ftypemod': catftypemod
, 'fuid': catfuid
, 'funame': catfuname
, 'fgid': catfgid
, 'fgname': catfgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': catfdev_minor
, 'fmajor': catfdev_major
, 'frminor': catfrdev_minor
, 'frmajor': catfrdev_major
, 'fchecksumtype': catfchecksumtype
, 'fnumfields': catfnumfields
, 'fextrafields': catfextrafields
, 'fextrafieldsize': catfextrasize
, 'fextralist': extrafieldslist
, 'fheaderchecksum': catfcs
, 'fcontentchecksum': catfccs
, 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': catfcontents
} });
3688 fileidnum
= fileidnum
+ 1;
3689 realidnum
= realidnum
+ 1;
3691 catlist
.update({'catfp': catfp
});
3696 create_alias_function("", __file_format_name__
, "ToArray", ArchiveFileToArray
);
3698 def ArchiveFileStringToArray(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3699 catfp
= BytesIO(catstr
);
3700 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3701 return listcatfiles
;
3703 create_alias_function("", __file_format_name__
, "StringToArray", ArchiveFileStringToArray
);
3705 def TarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3707 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
3708 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3709 return listcatfiles
;
3711 def ZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3713 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
3714 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3715 return listcatfiles
;
3717 if(not rarfile_support
):
3718 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3721 if(rarfile_support
):
3722 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3724 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
3725 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3726 return listcatfiles
;
3728 def ListDirToArrayAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
3729 catver
= formatspecs
[6];
3730 fileheaderver
= str(int(catver
.replace(".", "")));
3731 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
3732 advancedlist
= True;
3735 for line
in sys
.stdin
:
3736 infilelist
.append(line
.strip());
3737 infilelist
= list(filter(None, infilelist
));
3738 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
3739 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
3741 with
open(infiles
, "r") as finfile
:
3742 for line
in finfile
:
3743 infilelist
.append(line
.strip());
3744 infilelist
= list(filter(None, infilelist
));
3746 if(isinstance(infiles
, (list, tuple, ))):
3747 infilelist
= list(filter(None, infiles
));
3748 elif(isinstance(infiles
, (str, ))):
3749 infilelist
= list(filter(None, [infiles
]));
3751 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
3753 GetDirList
= ListDir(infilelist
, followlink
, False);
3761 inodetocatinode
= {};
3763 fnumfiles
= int(len(GetDirList
));
3764 catver
= formatspecs
[6];
3765 fileheaderver
= str(int(catver
.replace(".", "")));
3766 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
3767 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3768 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
3769 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
3770 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
3771 if(checksumtype
=="none" or checksumtype
==""):
3772 catfileheadercshex
= format(0, 'x').lower();
3773 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3774 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3775 elif(checksumtype
=="crc16_ccitt"):
3776 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3777 elif(checksumtype
=="adler32"):
3778 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3779 elif(checksumtype
=="crc32"):
3780 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3781 elif(checksumtype
=="crc64_ecma"):
3782 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3783 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3784 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3785 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3786 checksumoutstr
= hashlib
.new(checksumtype
);
3787 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3788 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3790 catfileheadercshex
= format(0, 'x').lower();
3791 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3792 fheadtell
= len(fileheader
);
3793 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
3794 for curfname
in GetDirList
:
3795 if(re
.findall("^[.|/]", curfname
)):
3798 fname
= "./"+curfname
;
3800 VerbosePrintOut(fname
);
3801 if(not followlink
or followlink
is None):
3802 fstatinfo
= os
.lstat(fname
);
3804 fstatinfo
= os
.stat(fname
);
3805 fpremode
= fstatinfo
.st_mode
;
3806 finode
= fstatinfo
.st_ino
;
3807 flinkcount
= fstatinfo
.st_nlink
;
3809 if(stat
.S_ISREG(fpremode
)):
3811 elif(stat
.S_ISLNK(fpremode
)):
3813 elif(stat
.S_ISCHR(fpremode
)):
3815 elif(stat
.S_ISBLK(fpremode
)):
3817 elif(stat
.S_ISDIR(fpremode
)):
3819 elif(stat
.S_ISFIFO(fpremode
)):
3821 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
3823 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
3825 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
3830 fbasedir
= os
.path
.dirname(fname
);
3832 if(not followlink
and finode
!=0):
3834 if(finode
in inodelist
):
3836 flinkname
= inodetofile
[finode
];
3837 fcurinode
= inodetocatinode
[finode
];
3838 if(finode
not in inodelist
):
3839 inodelist
.append(finode
);
3840 inodetofile
.update({finode
: fname
});
3841 inodetocatinode
.update({finode
: curinode
});
3842 fcurinode
= curinode
;
3843 curinode
= curinode
+ 1;
3845 fcurinode
= curinode
;
3846 curinode
= curinode
+ 1;
3847 curfid
= curfid
+ 1;
3849 flinkname
= os
.readlink(fname
);
3850 fdev
= fstatinfo
.st_dev
;
3851 getfdev
= GetDevMajorMinor(fdev
);
3852 fdev_minor
= getfdev
[0];
3853 fdev_major
= getfdev
[1];
3854 frdev
= fstatinfo
.st_dev
;
3855 if(hasattr(fstatinfo
, "st_rdev")):
3856 frdev
= fstatinfo
.st_rdev
;
3858 frdev
= fstatinfo
.st_dev
;
3859 getfrdev
= GetDevMajorMinor(frdev
);
3860 frdev_minor
= getfrdev
[0];
3861 frdev_major
= getfrdev
[1];
3862 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
3864 if(ftype
==0 or ftype
==7):
3865 fsize
= fstatinfo
.st_size
;
3866 fatime
= fstatinfo
.st_atime
;
3867 fmtime
= fstatinfo
.st_mtime
;
3868 fctime
= fstatinfo
.st_ctime
;
3869 if(hasattr(fstatinfo
, "st_birthtime")):
3870 fbtime
= fstatinfo
.st_birthtime
;
3872 fbtime
= fstatinfo
.st_ctime
;
3873 fmode
= fstatinfo
.st_mode
;
3874 fchmode
= stat
.S_IMODE(fstatinfo
.st_mode
);
3875 ftypemod
= stat
.S_IFMT(fstatinfo
.st_mode
);
3876 fuid
= fstatinfo
.st_uid
;
3877 fgid
= fstatinfo
.st_gid
;
3882 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
3883 funame
= userinfo
.pw_name
;
3892 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
3893 fgname
= groupinfo
.gr_name
;
3898 fdev_minor
= fdev_minor
;
3899 fdev_major
= fdev_major
;
3900 frdev_minor
= frdev_minor
;
3901 frdev_major
= frdev_major
;
3903 flinkcount
= flinkcount
;
3904 if(hasattr(fstatinfo
, "st_file_attributes")):
3905 fwinattributes
= fstatinfo
.st_file_attributes
;
3908 fcontents
= "".encode('UTF-8');
3910 if(ftype
== 0 or ftype
== 7):
3911 with
open(fname
, "rb") as fpc
:
3913 chunk
= fpc
.read(chunk_size
);
3917 if(followlink
and (ftype
== 1 or ftype
== 2)):
3918 flstatinfo
= os
.stat(flinkname
);
3919 with
open(flinkname
, "rb") as fpc
:
3921 chunk
= fpc
.read(chunk_size
);
3925 ftypehex
= format(ftype
, 'x').lower();
3926 extrafields
= len(extradata
);
3927 extrafieldslist
= extradata
;
3928 catfextrafields
= extrafields
;
3929 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
3930 if(len(extradata
)>0):
3931 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3932 extrasizelen
= len(extrasizestr
);
3933 extrasizelenhex
= format(extrasizelen
, 'x').lower();
3934 catfileoutstr
= AppendNullBytes([ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fuid
), 'x').lower(), funame
, format(int(fgid
), 'x').lower(), fgname
, format(int(fcurfid
), 'x').lower(), format(int(fcurinode
), 'x').lower(), format(int(flinkcount
), 'x').lower(), format(int(fdev_minor
), 'x').lower(), format(int(fdev_major
), 'x').lower(), format(int(frdev_minor
), 'x').lower(), format(int(frdev_major
), 'x').lower(), extrasizelenhex
, format(catfextrafields
, 'x').lower()], formatspecs
[5]);
3935 if(len(extradata
)>0):
3936 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3937 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
3938 catfnumfields
= 24 + catfextrafields
;
3939 if(checksumtype
=="none" or checksumtype
==""):
3940 catfileheadercshex
= format(0, 'x').lower();
3941 catfilecontentcshex
= format(0, 'x').lower();
3942 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3943 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3944 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
3945 elif(checksumtype
=="crc16_ccitt"):
3946 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3947 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
3948 elif(checksumtype
=="adler32"):
3949 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3950 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
3951 elif(checksumtype
=="crc32"):
3952 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3953 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
3954 elif(checksumtype
=="crc64_ecma"):
3955 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3956 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
3957 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3958 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3959 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
3960 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3961 checksumoutstr
= hashlib
.new(checksumtype
);
3962 checksumoutstr
.update("".encode('UTF-8'));
3963 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3964 checksumoutstr
= hashlib
.new(checksumtype
);
3965 checksumoutstr
.update(fcontents
);
3966 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
3968 catfileheadercshex
= format(0, 'x').lower();
3969 catfilecontentcshex
= format(0, 'x').lower();
3970 catfhstart
= fheadtell
;
3971 fheadtell
+= len(catfileoutstr
);
3972 catfhend
= fheadtell
- 1;
3973 catfcontentstart
= fheadtell
;
3974 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
3975 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
3976 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
3977 if(checksumtype
=="none" or checksumtype
==""):
3978 catfileheadercshex
= format(0, 'x').lower();
3979 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3980 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3981 elif(checksumtype
=="crc16_ccitt"):
3982 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3983 elif(checksumtype
=="adler32"):
3984 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3985 elif(checksumtype
=="crc32"):
3986 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3987 elif(checksumtype
=="crc64_ecma"):
3988 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3989 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3990 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3991 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3992 checksumoutstr
= hashlib
.new(checksumtype
);
3993 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
3994 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3996 catfileheadercshex
= format(0, 'x').lower();
3997 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
3998 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
3999 nullstrecd
= formatspecs
[5].encode('UTF-8');
4000 fheadtell
+= len(catfileoutstr
) + 1;
4001 catfcontentend
= fheadtell
- 1;
4002 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
4003 pyhascontents
= False;
4004 if(int(fsize
)>0 and not listonly
):
4005 pyhascontents
= True;
4006 if(int(fsize
)>0 and listonly
):
4008 pyhascontents
= False;
4009 catlist
['ffilelist'].update({fileidnum
: {'fid': fileidnum
, 'fidalt': fileidnum
, 'fheadersize': int(catheaersize
, 16), 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': checksumtype
, 'fnumfields': catfnumfields
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
4010 fileidnum
= fileidnum
+ 1;
4013 def TarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4019 inodetocatinode
= {};
4023 if(hasattr(sys
.stdin
, "buffer")):
4024 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
4026 shutil
.copyfileobj(sys
.stdin
, infile
);
4031 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
4032 infile
= download_file_from_internet_file(infile
);
4037 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
4039 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
4041 if(not tarfile
.is_tarfile(infile
)):
4043 except AttributeError:
4044 if(not is_tarfile(infile
)):
4049 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
4050 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
4052 tarfp
= tarfile
.open(infile
, "r");
4053 except FileNotFoundError
:
4055 fnumfiles
= int(len(tarfp
.getmembers()));
4056 catver
= formatspecs
[6];
4057 fileheaderver
= str(int(catver
.replace(".", "")));
4058 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4059 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4060 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4061 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4062 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4063 if(checksumtype
=="none" or checksumtype
==""):
4064 catfileheadercshex
= format(0, 'x').lower();
4065 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4066 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4067 elif(checksumtype
=="crc16_ccitt"):
4068 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4069 elif(checksumtype
=="adler32"):
4070 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4071 elif(checksumtype
=="crc32"):
4072 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4073 elif(checksumtype
=="crc64_ecma"):
4074 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4075 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4076 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4077 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4078 checksumoutstr
= hashlib
.new(checksumtype
);
4079 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4080 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4082 catfileheadercshex
= format(0, 'x').lower();
4083 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4084 fheadtell
= len(fileheader
);
4085 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4086 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
4087 if(re
.findall("^[.|/]", member
.name
)):
4088 fname
= member
.name
;
4090 fname
= "./"+member
.name
;
4092 VerbosePrintOut(fname
);
4093 fpremode
= member
.mode
;
4094 ffullmode
= member
.mode
;
4098 ffullmode
= member
.mode
+ stat
.S_IFREG
;
4100 elif(member
.isdev()):
4101 ffullmode
= member
.mode
;
4103 elif(member
.islnk()):
4104 ffullmode
= member
.mode
+ stat
.S_IFREG
;
4106 elif(member
.issym()):
4107 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
4109 elif(member
.ischr()):
4110 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
4112 elif(member
.isblk()):
4113 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
4115 elif(member
.isdir()):
4116 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
4118 elif(member
.isfifo()):
4119 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
4121 elif(member
.issparse()):
4122 ffullmode
= member
.mode
;
4125 ffullmode
= member
.mode
;
4128 fbasedir
= os
.path
.dirname(fname
);
4132 curfid
= curfid
+ 1;
4134 flinkname
= member
.linkname
;
4135 fdev_minor
= member
.devminor
;
4136 fdev_major
= member
.devmajor
;
4137 frdev_minor
= member
.devminor
;
4138 frdev_major
= member
.devmajor
;
4139 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
4141 elif(ftype
==0 or ftype
==7):
4142 fsize
= member
.size
;
4144 fsize
= member
.size
;
4145 fatime
= member
.mtime
;
4146 fmtime
= member
.mtime
;
4147 fctime
= member
.mtime
;
4148 fbtime
= member
.mtime
;
4150 fchmode
= stat
.S_IMODE(ffullmode
);
4151 ftypemod
= stat
.S_IFMT(ffullmode
);
4154 funame
= member
.uname
;
4155 fgname
= member
.gname
;
4156 flinkcount
= flinkcount
;
4157 fcontents
= "".encode('UTF-8');
4159 if(ftype
== 0 or ftype
== 7):
4160 with tarfp
.extractfile(member
) as fpc
:
4162 chunk
= fpc
.read(chunk_size
);
4166 ftypehex
= format(ftype
, 'x').lower();
4167 extrafields
= len(extradata
);
4168 extrafieldslist
= extradata
;
4169 catfextrafields
= extrafields
;
4170 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
4171 if(len(extradata
)>0):
4172 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4173 extrasizelen
= len(extrasizestr
);
4174 extrasizelenhex
= format(extrasizelen
, 'x').lower();
4175 catfileoutstr
= AppendNullBytes([ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fuid
), 'x').lower(), funame
, format(int(fgid
), 'x').lower(), fgname
, format(int(fcurfid
), 'x').lower(), format(int(fcurinode
), 'x').lower(), format(int(flinkcount
), 'x').lower(), format(int(fdev_minor
), 'x').lower(), format(int(fdev_major
), 'x').lower(), format(int(frdev_minor
), 'x').lower(), format(int(frdev_major
), 'x').lower(), extrasizelenhex
, format(catfextrafields
, 'x').lower()], formatspecs
[5]);
4176 if(len(extradata
)>0):
4177 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4178 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
4179 catfnumfields
= 24 + catfextrafields
;
4180 if(checksumtype
=="none" or checksumtype
==""):
4181 catfileheadercshex
= format(0, 'x').lower();
4182 catfilecontentcshex
= format(0, 'x').lower();
4183 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4184 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
4185 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
4186 elif(checksumtype
=="crc16_ccitt"):
4187 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
4188 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
4189 elif(checksumtype
=="adler32"):
4190 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4191 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
4192 elif(checksumtype
=="crc32"):
4193 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4194 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
4195 elif(checksumtype
=="crc64_ecma"):
4196 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4197 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
4198 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4199 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4200 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
4201 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4202 checksumoutstr
= hashlib
.new(checksumtype
);
4203 checksumoutstr
.update("".encode('UTF-8'));
4204 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4205 checksumoutstr
= hashlib
.new(checksumtype
);
4206 checksumoutstr
.update(fcontents
);
4207 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
4209 catfileheadercshex
= format(0, 'x').lower();
4210 catfilecontentcshex
= format(0, 'x').lower();
4211 catfhstart
= fheadtell
;
4212 fheadtell
+= len(catfileoutstr
);
4213 catfhend
= fheadtell
- 1;
4214 catfcontentstart
= fheadtell
;
4215 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4216 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
4217 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
4218 if(checksumtype
=="none" or checksumtype
==""):
4219 catfileheadercshex
= format(0, 'x').lower();
4220 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4221 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4222 elif(checksumtype
=="crc16_ccitt"):
4223 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4224 elif(checksumtype
=="adler32"):
4225 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4226 elif(checksumtype
=="crc32"):
4227 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4228 elif(checksumtype
=="crc64_ecma"):
4229 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4230 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4231 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4232 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4233 checksumoutstr
= hashlib
.new(checksumtype
);
4234 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
4235 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4237 catfileheadercshex
= format(0, 'x').lower();
4238 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4239 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
4240 nullstrecd
= formatspecs
[5].encode('UTF-8');
4241 fheadtell
+= len(catfileoutstr
) + 1;
4242 catfcontentend
= fheadtell
- 1;
4243 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
4244 pyhascontents
= False;
4245 if(int(fsize
)>0 and not listonly
):
4246 pyhascontents
= True;
4247 if(int(fsize
)>0 and listonly
):
4249 pyhascontents
= False;
4250 catlist
['ffilelist'].update({fileidnum
: {'fid': fileidnum
, 'fidalt': fileidnum
, 'fheadersize': int(catheaersize
, 16), 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': checksumtype
, 'fnumfields': catfnumfields
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
4251 fileidnum
= fileidnum
+ 1;
4254 def ZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4255 advancedlist
= True;
4261 inodetocatinode
= {};
4265 if(hasattr(sys
.stdin
, "buffer")):
4266 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
4268 shutil
.copyfileobj(sys
.stdin
, infile
);
4273 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
4274 infile
= download_file_from_internet_file(infile
);
4279 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
4283 if(not zipfile
.is_zipfile(infile
)):
4286 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
4287 except FileNotFoundError
:
4289 ziptest
= zipfp
.testzip();
4291 VerbosePrintOut("Bad file found!");
4292 fnumfiles
= int(len(zipfp
.infolist()));
4293 catver
= formatspecs
[6];
4294 fileheaderver
= str(int(catver
.replace(".", "")));
4295 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4296 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4297 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4298 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4299 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4300 if(checksumtype
=="none" or checksumtype
==""):
4301 catfileheadercshex
= format(0, 'x').lower();
4302 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4303 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4304 elif(checksumtype
=="crc16_ccitt"):
4305 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4306 elif(checksumtype
=="adler32"):
4307 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4308 elif(checksumtype
=="crc32"):
4309 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4310 elif(checksumtype
=="crc64_ecma"):
4311 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4312 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4313 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4314 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4315 checksumoutstr
= hashlib
.new(checksumtype
);
4316 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4317 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4319 catfileheadercshex
= format(0, 'x').lower();
4320 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4321 fheadtell
= len(fileheader
);
4322 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4323 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
4324 if(re
.findall("^[.|/]", member
.filename
)):
4325 fname
= member
.filename
;
4327 fname
= "./"+member
.filename
;
4328 zipinfo
= zipfp
.getinfo(member
.filename
);
4330 VerbosePrintOut(fname
);
4331 if(not member
.is_dir()):
4332 fpremode
= stat
.S_IFREG
+ 438;
4333 elif(member
.is_dir()):
4334 fpremode
= stat
.S_IFDIR
+ 511;
4337 if(not member
.is_dir()):
4339 elif(member
.is_dir()):
4342 fbasedir
= os
.path
.dirname(fname
);
4346 curfid
= curfid
+ 1;
4354 fsize
= member
.file_size
;
4356 fsize
= member
.file_size
;
4357 fatime
= time
.mktime(member
.date_time
+ (0, 0, -1));
4358 fmtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
4359 fctime
= time
.mktime(member
.date_time
+ (0, 0, -1));
4360 fbtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
4361 if(not member
.is_dir()):
4362 fmode
= stat
.S_IFREG
+ 438;
4363 fchmode
= stat
.S_IMODE(int(stat
.S_IFREG
+ 438));
4364 ftypemod
= stat
.S_IFMT(int(stat
.S_IFREG
+ 438));
4365 if(member
.is_dir()):
4366 fmode
= stat
.S_IFDIR
+ 511;
4367 fchmode
= stat
.S_IMODE(int(stat
.S_IFDIR
+ 511));
4368 ftypemod
= stat
.S_IFMT(int(stat
.S_IFDIR
+ 511));
4371 except AttributeError:
4377 except AttributeError:
4384 userinfo
= pwd
.getpwuid(os
.getuid());
4385 funame
= userinfo
.pw_name
;
4388 except AttributeError:
4396 groupinfo
= grp
.getgrgid(os
.getgid());
4397 fgname
= groupinfo
.gr_name
;
4400 except AttributeError:
4404 fcontents
= "".encode('UTF-8');
4406 fcontents
= zipfp
.read(member
.filename
);
4407 ftypehex
= format(ftype
, 'x').lower();
4408 extrafields
= len(extradata
);
4409 extrafieldslist
= extradata
;
4410 catfextrafields
= extrafields
;
4411 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
4412 if(len(extradata
)>0):
4413 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4414 extrasizelen
= len(extrasizestr
);
4415 extrasizelenhex
= format(extrasizelen
, 'x').lower();
4416 catfileoutstr
= AppendNullBytes([ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fuid
), 'x').lower(), funame
, format(int(fgid
), 'x').lower(), fgname
, format(int(fcurfid
), 'x').lower(), format(int(fcurinode
), 'x').lower(), format(int(flinkcount
), 'x').lower(), format(int(fdev_minor
), 'x').lower(), format(int(fdev_major
), 'x').lower(), format(int(frdev_minor
), 'x').lower(), format(int(frdev_major
), 'x').lower(), extrasizelenhex
, format(catfextrafields
, 'x').lower()], formatspecs
[5]);
4417 if(len(extradata
)>0):
4418 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4419 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
4420 catfnumfields
= 24 + catfextrafields
;
4421 if(checksumtype
=="none" or checksumtype
==""):
4422 catfileheadercshex
= format(0, 'x').lower();
4423 catfilecontentcshex
= format(0, 'x').lower();
4424 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4425 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
4426 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
4427 elif(checksumtype
=="crc16_ccitt"):
4428 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
4429 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
4430 elif(checksumtype
=="adler32"):
4431 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4432 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
4433 elif(checksumtype
=="crc32"):
4434 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4435 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
4436 elif(checksumtype
=="crc64_ecma"):
4437 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4438 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
4439 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4440 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4441 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
4442 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4443 checksumoutstr
= hashlib
.new(checksumtype
);
4444 checksumoutstr
.update("".encode('UTF-8'));
4445 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4446 checksumoutstr
= hashlib
.new(checksumtype
);
4447 checksumoutstr
.update(fcontents
);
4448 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
4450 catfileheadercshex
= format(0, 'x').lower();
4451 catfilecontentcshex
= format(0, 'x').lower();
4452 catfhstart
= fheadtell
;
4453 fheadtell
+= len(catfileoutstr
);
4454 catfhend
= fheadtell
- 1;
4455 catfcontentstart
= fheadtell
;
4456 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4457 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
4458 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
4459 if(checksumtype
=="none" or checksumtype
==""):
4460 catfileheadercshex
= format(0, 'x').lower();
4461 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4462 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4463 elif(checksumtype
=="crc16_ccitt"):
4464 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4465 elif(checksumtype
=="adler32"):
4466 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4467 elif(checksumtype
=="crc32"):
4468 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4469 elif(checksumtype
=="crc64_ecma"):
4470 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4471 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4472 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4473 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4474 checksumoutstr
= hashlib
.new(checksumtype
);
4475 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
4476 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4478 catfileheadercshex
= format(0, 'x').lower();
4479 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4480 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
4481 nullstrecd
= formatspecs
[5].encode('UTF-8');
4482 fheadtell
+= len(catfileoutstr
) + 1;
4483 catfcontentend
= fheadtell
- 1;
4484 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
4485 pyhascontents
= False;
4486 if(int(fsize
)>0 and not listonly
):
4487 pyhascontents
= True;
4488 if(int(fsize
)>0 and listonly
):
4490 pyhascontents
= False;
4491 catlist
['ffilelist'].update({fileidnum
: {'fid': fileidnum
, 'fidalt': fileidnum
, 'fheadersize': int(catheaersize
, 16), 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': checksumtype
, 'fnumfields': catfnumfields
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
4492 fileidnum
= fileidnum
+ 1;
4495 if(not rarfile_support
):
4496 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4499 if(rarfile_support
):
4500 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4501 advancedlist
= True;
4507 inodetocatinode
= {};
4509 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
4511 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
4513 rarfp
= rarfile
.RarFile(infile
, "r");
4514 rartest
= rarfp
.testrar();
4516 VerbosePrintOut("Bad file found!");
4517 fnumfiles
= int(len(rarfp
.infolist()));
4518 catver
= formatspecs
[6];
4519 fileheaderver
= str(int(catver
.replace(".", "")));
4520 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4521 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4522 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4523 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4524 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4525 if(checksumtype
=="none" or checksumtype
==""):
4526 catfileheadercshex
= format(0, 'x').lower();
4527 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4528 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4529 elif(checksumtype
=="crc16_ccitt"):
4530 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4531 elif(checksumtype
=="adler32"):
4532 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4533 elif(checksumtype
=="crc32"):
4534 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4535 elif(checksumtype
=="crc64_ecma"):
4536 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4537 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4538 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4539 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4540 checksumoutstr
= hashlib
.new(checksumtype
);
4541 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4542 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4544 catfileheadercshex
= format(0, 'x').lower();
4545 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4546 fheadtell
= len(fileheader
);
4547 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4548 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
4551 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
4554 member
.external_attr
4556 except AttributeError:
4558 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
4561 member
.external_attr
4563 except AttributeError:
4568 if(re
.findall("^[.|/]", member
.filename
)):
4569 fname
= member
.filename
;
4571 fname
= "./"+member
.filename
;
4572 rarinfo
= rarfp
.getinfo(member
.filename
);
4574 VerbosePrintOut(fname
);
4575 if(is_unix
and member
.external_attr
!=0):
4576 fpremode
= int(member
.external_attr
);
4577 elif(member
.is_file()):
4578 fpremode
= stat
.S_IFREG
+ 438;
4579 elif(member
.is_symlink()):
4580 fpremode
= stat
.S_IFLNK
+ 438;
4581 elif(member
.is_dir()):
4582 fpremode
= stat
.S_IFDIR
+ 511;
4583 if(is_windows
and member
.external_attr
!=0):
4584 fwinattributes
= int(member
.external_attr
);
4586 fwinattributes
= int(0);
4589 if(member
.is_file()):
4591 elif(member
.is_symlink()):
4593 elif(member
.is_dir()):
4597 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
4598 fbasedir
= os
.path
.dirname(fname
);
4602 curfid
= curfid
+ 1;
4610 fsize
= member
.file_size
;
4613 fatime
= int(member
.atime
.timestamp());
4615 fatime
= int(member
.mtime
.timestamp());
4616 except AttributeError:
4617 fatime
= int(member
.mtime
.timestamp());
4618 fmtime
= int(member
.mtime
.timestamp());
4621 fctime
= int(member
.ctime
.timestamp());
4623 fctime
= int(member
.mtime
.timestamp());
4624 except AttributeError:
4625 fctime
= int(member
.mtime
.timestamp());
4626 fbtime
= int(member
.mtime
.timestamp());
4627 if(is_unix
and member
.external_attr
!=0):
4628 fmode
= format(int(member
.external_attr
), 'x').lower();
4629 fchmode
= format(int(stat
.S_IMODE(member
.external_attr
)), 'x').lower();
4630 ftypemod
= format(int(stat
.S_IFMT(member
.external_attr
)), 'x').lower();
4631 elif(member
.is_file()):
4632 fmode
= int(stat
.S_IFREG
+ 438)
4633 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
4634 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
4635 elif(member
.is_symlink()):
4636 fmode
= int(stat
.S_IFLNK
+ 438)
4637 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
4638 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
4639 elif(member
.is_dir()):
4640 fmode
= int(stat
.S_IFDIR
+ 511)
4641 fchmode
= int(stat
.S_IMODE(stat
.S_IFDIR
+ 511));
4642 ftypemod
= int(stat
.S_IFMT(stat
.S_IFDIR
+ 511));
4645 except AttributeError:
4651 except AttributeError:
4658 userinfo
= pwd
.getpwuid(os
.getuid());
4659 funame
= userinfo
.pw_name
;
4662 except AttributeError:
4670 groupinfo
= grp
.getgrgid(os
.getgid());
4671 fgname
= groupinfo
.gr_name
;
4674 except AttributeError:
4678 fcontents
= "".encode('UTF-8');
4680 fcontents
= rarfp
.read(member
.filename
);
4681 ftypehex
= format(ftype
, 'x').lower();
4682 extrafields
= len(extradata
);
4683 extrafieldslist
= extradata
;
4684 catfextrafields
= extrafields
;
4685 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
4686 if(len(extradata
)>0):
4687 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4688 extrasizelen
= len(extrasizestr
);
4689 extrasizelenhex
= format(extrasizelen
, 'x').lower();
4690 catfileoutstr
= AppendNullBytes([ftypehex
, fname
, flinkname
, format(int(fsize
), 'x').lower(), format(int(fatime
), 'x').lower(), format(int(fmtime
), 'x').lower(), format(int(fctime
), 'x').lower(), format(int(fbtime
), 'x').lower(), format(int(fmode
), 'x').lower(), format(int(fuid
), 'x').lower(), funame
, format(int(fgid
), 'x').lower(), fgname
, format(int(fcurfid
), 'x').lower(), format(int(fcurinode
), 'x').lower(), format(int(flinkcount
), 'x').lower(), format(int(fdev_minor
), 'x').lower(), format(int(fdev_major
), 'x').lower(), format(int(frdev_minor
), 'x').lower(), format(int(frdev_major
), 'x').lower(), extrasizelenhex
, format(catfextrafields
, 'x').lower()], formatspecs
[5]);
4691 if(len(extradata
)>0):
4692 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4693 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
4694 catfnumfields
= 24 + catfextrafields
;
4695 if(checksumtype
=="none" or checksumtype
==""):
4696 catfileheadercshex
= format(0, 'x').lower();
4697 catfilecontentcshex
= format(0, 'x').lower();
4698 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4699 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4700 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
4701 elif(checksumtype
=="crc16_ccitt"):
4702 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4703 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
4704 elif(checksumtype
=="adler32"):
4705 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4706 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
4707 elif(checksumtype
=="crc32"):
4708 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4709 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
4710 elif(checksumtype
=="crc64_ecma"):
4711 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4712 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
4713 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4714 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4715 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
4716 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4717 checksumoutstr
= hashlib
.new(checksumtype
);
4718 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
4719 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4721 catfileheadercshex
= format(0, 'x').lower();
4722 catfilecontentcshex
= format(0, 'x').lower();
4723 catfhstart
= fheadtell
;
4724 fheadtell
+= len(catfileoutstr
);
4725 catfhend
= fheadtell
- 1;
4726 catfcontentstart
= fheadtell
;
4727 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4728 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
4729 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
4730 if(checksumtype
=="none" or checksumtype
==""):
4731 catfileheadercshex
= format(0, 'x').lower();
4732 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4733 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4734 elif(checksumtype
=="crc16_ccitt"):
4735 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4736 elif(checksumtype
=="adler32"):
4737 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4738 elif(checksumtype
=="crc32"):
4739 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4740 elif(checksumtype
=="crc64_ecma"):
4741 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4742 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4743 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4744 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4745 checksumoutstr
= hashlib
.new(checksumtype
);
4746 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
4747 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4749 catfileheadercshex
= format(0, 'x').lower();
4750 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4751 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
4752 nullstrecd
= formatspecs
[5].encode('UTF-8');
4753 fheadtell
+= len(catfileoutstr
) + 1;
4754 catfcontentend
= fheadtell
- 1;
4755 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
4756 pyhascontents
= False;
4757 if(int(fsize
)>0 and not listonly
):
4758 pyhascontents
= True;
4759 if(int(fsize
)>0 and listonly
):
4761 pyhascontents
= False;
4762 catlist
['ffilelist'].update({fileidnum
: {'fid': fileidnum
, 'fidalt': fileidnum
, 'fheadersize': int(catheaersize
, 16), 'fhstart': catfhstart
, 'fhend': catfhend
, 'ftype': ftype
, 'fname': fname
, 'fbasedir': fbasedir
, 'flinkname': flinkname
, 'fsize': fsize
, 'fatime': fatime
, 'fmtime': fmtime
, 'fctime': fctime
, 'fbtime': fbtime
, 'fmode': fmode
, 'fchmode': fchmode
, 'ftypemod': ftypemod
, 'fuid': fuid
, 'funame': funame
, 'fgid': fgid
, 'fgname': fgname
, 'finode': finode
, 'flinkcount': flinkcount
, 'fminor': fdev_minor
, 'fmajor': fdev_major
, 'frminor': frdev_minor
, 'frmajor': frdev_major
, 'fchecksumtype': checksumtype
, 'fnumfields': catfnumfields
, 'fextrafields': catfextrafields
, 'fextrafieldsize': extrasizelen
, 'fextralist': extrafieldslist
, 'fheaderchecksum': int(catfileheadercshex
, 16), 'fcontentchecksum': int(catfilecontentcshex
, 16), 'fhascontents': pyhascontents
, 'fcontentstart': catfcontentstart
, 'fcontentend': catfcontentend
, 'fcontents': fcontents
} });
4763 fileidnum
= fileidnum
+ 1;
4766 def ListDirToArray(infiles
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
4767 outarray
= BytesIO();
4768 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
4769 listcatfiles
= ArchiveFileToArray(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4770 return listcatfiles
;
4772 def ArchiveFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4773 if(isinstance(infile
, dict)):
4774 listcatfiles
= infile
;
4776 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
4777 infile
= RemoveWindowsPath(infile
);
4778 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4779 if(not listcatfiles
):
4781 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': {}}}};
4783 catarray
.update({'catfp': listcatfiles
['catfp']});
4784 lenlist
= len(listcatfiles
['ffilelist']);
4789 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4792 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4793 lcfx
= listcatfiles
['fnumfiles'];
4795 lcfx
= int(listcatfiles
['fnumfiles']);
4797 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
4798 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
4799 catarray
['filetoid'].update(filetoidarray
);
4800 catarray
['idtofile'].update(idtofilearray
);
4801 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4802 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
4803 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
4804 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4805 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
4806 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
4807 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4808 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4809 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4810 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4811 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4812 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4813 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4814 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
4815 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
4816 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
4817 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4818 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4819 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
4820 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
4821 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
4822 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4823 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4824 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4825 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
4826 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
4827 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
4828 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4829 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4830 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4831 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4835 create_alias_function("", __file_format_name__
, "ToArrayIndex", ArchiveFileToArrayIndex
);
4837 def ListDirToArrayIndexAlt(infiles
, dirlistfromtxt
=False, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4838 listcatfiles
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
4839 if(not listcatfiles
):
4841 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': {}}}};
4842 lenlist
= len(listcatfiles
['ffilelist']);
4847 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4850 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4851 lcfx
= listcatfiles
['fnumfiles'];
4853 lcfx
= int(listcatfiles
['fnumfiles']);
4855 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
4856 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
4857 catarray
['filetoid'].update(filetoidarray
);
4858 catarray
['idtofile'].update(idtofilearray
);
4859 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4860 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
4861 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
4862 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4863 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
4864 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
4865 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4866 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4867 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4868 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4869 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4870 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4871 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4872 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
4873 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
4874 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
4875 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4876 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4877 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
4878 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
4879 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
4880 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4881 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4882 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4883 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
4884 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
4885 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
4886 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4887 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4888 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4889 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4893 def TarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4894 listcatfiles
= TarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
4895 if(not listcatfiles
):
4897 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': {}}}};
4898 lenlist
= len(listcatfiles
['ffilelist']);
4903 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4906 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4907 lcfx
= listcatfiles
['fnumfiles'];
4909 lcfx
= int(listcatfiles
['fnumfiles']);
4911 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
4912 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
4913 catarray
['filetoid'].update(filetoidarray
);
4914 catarray
['idtofile'].update(idtofilearray
);
4915 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4916 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
4917 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
4918 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4919 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
4920 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
4921 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4922 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4923 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4924 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4925 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4926 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4927 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4928 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
4929 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
4930 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
4931 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4932 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4933 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
4934 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
4935 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
4936 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4937 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4938 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4939 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
4940 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
4941 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
4942 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4943 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4944 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4945 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4949 def ZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4950 listcatfiles
= ZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
4951 if(not listcatfiles
):
4953 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': {}}}};
4954 lenlist
= len(listcatfiles
['ffilelist']);
4959 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4962 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4963 lcfx
= listcatfiles
['fnumfiles'];
4965 lcfx
= int(listcatfiles
['fnumfiles']);
4967 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
4968 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
4969 catarray
['filetoid'].update(filetoidarray
);
4970 catarray
['idtofile'].update(idtofilearray
);
4971 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4972 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
4973 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
4974 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4975 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
4976 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
4977 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4978 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4979 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4980 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4981 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4982 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4983 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4984 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
4985 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
4986 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
4987 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4988 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4989 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
4990 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
4991 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
4992 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4993 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4994 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4995 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
4996 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
4997 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
4998 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4999 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5000 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5001 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5005 if(not rarfile_support
):
5006 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5009 if(rarfile_support
):
5010 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
5011 listcatfiles
= RarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
5012 if(not listcatfiles
):
5014 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': {}}}};
5015 lenlist
= len(listcatfiles
['ffilelist']);
5020 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
5023 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5024 lcfx
= listcatfiles
['fnumfiles'];
5026 lcfx
= int(listcatfiles
['fnumfiles']);
5028 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
5029 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
5030 catarray
['filetoid'].update(filetoidarray
);
5031 catarray
['idtofile'].update(idtofilearray
);
5032 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5033 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
5034 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
5035 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5036 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
5037 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
5038 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5039 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5040 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5041 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5042 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5043 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
5044 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
5045 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
5046 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
5047 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
5048 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5049 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5050 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
5051 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
5052 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
5053 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5054 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5055 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5056 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
5057 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
5058 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
5059 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
5060 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
5061 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
5062 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
5066 def ArchiveFileStringToArrayIndex(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5067 catfp
= BytesIO(catstr
);
5068 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5069 return listcatfiles
;
5071 create_alias_function("", __file_format_name__
, "StringToArrayIndex", ArchiveFileStringToArrayIndex
);
5073 def TarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5075 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5076 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5077 return listcatfiles
;
5079 def ZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5081 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5082 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5083 return listcatfiles
;
5085 if(not rarfile_support
):
5086 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5089 if(rarfile_support
):
5090 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
5092 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
5093 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
5094 return listcatfiles
;
5096 def ListDirToArrayIndex(infiles
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5097 outarray
= BytesIO();
5098 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, verbose
, True);
5099 listcatfiles
= ArchiveFileToArrayIndex(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
)
5100 return listcatfiles
;
5102 def RePackArchiveFile(infile
, outfile
, compression
="auto", compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5103 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
5104 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzop', 'lzo', 'lzma', 'xz'];
5105 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', 'lzop', '.lzo', '.lzma', '.xz'];
5106 if(isinstance(infile
, dict)):
5107 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5108 listcatfiles
= prelistcatfiles
['list'];
5110 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5111 infile
= RemoveWindowsPath(infile
);
5113 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5114 listcatfiles
= prelistcatfiles
['list'];
5116 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5117 if(outfile
!="-" and not hasattr(infile
, "read") and not hasattr(outfile
, "write")):
5118 outfile
= RemoveWindowsPath(outfile
);
5119 checksumtype
= checksumtype
.lower();
5120 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
5121 checksumtype
="crc32";
5122 if(checksumtype
=="none"):
5124 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
5126 if(compression
not in compressionlist
and compression
is None):
5127 compression
= "auto";
5129 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5130 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
5131 if(os
.path
.exists(outfile
)):
5133 if(not listcatfiles
):
5138 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
5140 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
5143 fbasename
= os
.path
.splitext(outfile
)[0];
5144 fextname
= os
.path
.splitext(outfile
)[1];
5145 catfp
= CompressOpenFile(outfile
, compressionlevel
);
5146 catver
= formatspecs
[6];
5147 fileheaderver
= str(int(catver
.replace(".", "")));
5148 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
5149 catfp
.write(fileheader
.encode('UTF-8'));
5150 lenlist
= len(listcatfiles
['ffilelist']);
5151 fnumfiles
= int(listcatfiles
['fnumfiles']);
5152 if(lenlist
>fnumfiles
or lenlist
<fnumfiles
):
5153 fnumfiles
= lenlist
;
5154 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
5155 fnumfilesa
= AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
5156 if(checksumtype
=="none" or checksumtype
==""):
5157 catfileheadercshex
= format(0, 'x').lower();
5158 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5159 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
5160 elif(checksumtype
=="crc16_ccitt"):
5161 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
5162 elif(checksumtype
=="adler32"):
5163 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
5164 elif(checksumtype
=="crc32"):
5165 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
5166 elif(checksumtype
=="crc64_ecma"):
5167 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5168 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5169 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5170 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5171 checksumoutstr
= hashlib
.new(checksumtype
);
5172 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
5173 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5175 catfileheadercshex
= format(0, 'x').lower();
5176 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
5177 catfp
.write(fnumfilesa
.encode('UTF-8'));
5180 os
.fsync(catfp
.fileno());
5181 except io
.UnsupportedOperation
:
5183 except AttributeError:
5189 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
5192 lcfx
= int(listcatfiles
['fnumfiles']);
5200 catfhstart
= catfp
.tell();
5201 if(re
.findall("^[.|/]", listcatfiles
['ffilelist'][reallcfi
]['fname'])):
5202 fname
= listcatfiles
['ffilelist'][reallcfi
]['fname'];
5204 fname
= "./"+listcatfiles
['ffilelist'][reallcfi
]['fname'];
5206 VerbosePrintOut(fname
);
5207 fheadersize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fheadersize']), 'x').lower();
5208 fsize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fsize']), 'x').lower();
5209 flinkname
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
5210 fatime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fatime']), 'x').lower();
5211 fmtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmtime']), 'x').lower();
5212 fctime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fctime']), 'x').lower();
5213 fbtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fbtime']), 'x').lower();
5214 fmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmode']), 'x').lower();
5215 fchmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fchmode']), 'x').lower();
5216 fuid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fuid']), 'x').lower();
5217 funame
= listcatfiles
['ffilelist'][reallcfi
]['funame'];
5218 fgid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fgid']), 'x').lower();
5219 fgname
= listcatfiles
['ffilelist'][reallcfi
]['fgname'];
5220 finode
= listcatfiles
['ffilelist'][reallcfi
]['finode'];
5221 flinkcount
= listcatfiles
['ffilelist'][reallcfi
]['flinkcount'];
5222 fdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fminor']), 'x').lower();
5223 fdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmajor']), 'x').lower();
5224 frdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frminor']), 'x').lower();
5225 frdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frmajor']), 'x').lower();
5226 if(len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] and len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>0):
5227 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
5228 if(len(extradata
) > 0):
5229 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(extradata
);
5230 listcatfiles
['ffilelist'][reallcfi
]['fextralist'] = extradata
;
5231 extrafields
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']), 'x').lower();
5232 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5233 if(len(extradata
)>0):
5234 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5235 extrasizelen
= format(len(extrasizestr
), 'x').lower();
5236 fcontents
= listcatfiles
['ffilelist'][reallcfi
]['fcontents'];
5238 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']==1 or listcatfiles
['ffilelist'][reallcfi
]['ftype']==2):
5239 getflinkpath
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
5240 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
5241 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
5242 fheadersize
= format(int(flinkinfo
['fheadersize']), 'x').lower();
5243 fsize
= format(int(flinkinfo
['fsize']), 'x').lower();
5244 flinkname
= flinkinfo
['flinkname'];
5245 fatime
= format(int(flinkinfo
['fatime']), 'x').lower();
5246 fmtime
= format(int(flinkinfo
['fmtime']), 'x').lower();
5247 fctime
= format(int(flinkinfo
['fctime']), 'x').lower();
5248 fbtime
= format(int(flinkinfo
['fbtime']), 'x').lower();
5249 fmode
= format(int(flinkinfo
['fmode']), 'x').lower();
5250 fchmode
= format(int(flinkinfo
['fchmode']), 'x').lower();
5251 fuid
= format(int(flinkinfo
['fuid']), 'x').lower();
5252 funame
= flinkinfo
['funame'];
5253 fgid
= format(int(flinkinfo
['fgid']), 'x').lower();
5254 fgname
= flinkinfo
['fgname'];
5255 finode
= flinkinfo
['finode'];
5256 flinkcount
= flinkinfo
['flinkcount'];
5257 fdev_minor
= format(int(flinkinfo
['fminor']), 'x').lower();
5258 fdev_major
= format(int(flinkinfo
['fmajor']), 'x').lower();
5259 frdev_minor
= format(int(flinkinfo
['frminor']), 'x').lower();
5260 frdev_major
= format(int(flinkinfo
['frmajor']), 'x').lower();
5261 if(len(flinkinfo
['fextralist'])>flinkinfo
['fextrafields'] and len(flinkinfo
['fextralist'])>0):
5262 flinkinfo
['fextrafields'] = len(flinkinfo
['fextralist']);
5263 if(len(extradata
) > 0):
5264 flinkinfo
['fextrafields'] = len(extradata
);
5265 flinkinfo
['fextralist'] = extradata
;
5266 extrafields
= format(int(flinkinfo
['fextrafields']), 'x').lower();
5267 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5268 if(len(extradata
)>0):
5269 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5270 extrasizelen
= format(len(extrasizestr
), 'x').lower();
5271 fcontents
= flinkinfo
['fcontents'];
5272 if(flinkinfo
['ftype']!=0 and flinkinfo
['ftype']!=7):
5274 fcontents
= fcontents
.encode('UTF-8');
5275 except AttributeError:
5277 ftypehex
= format(flinkinfo
['ftype'], 'x').lower();
5279 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=0 and listcatfiles
['ffilelist'][reallcfi
]['ftype']!=7):
5281 fcontents
= fcontents
.encode('UTF-8');
5282 except AttributeError:
5284 ftypehex
= format(listcatfiles
['ffilelist'][reallcfi
]['ftype'], 'x').lower();
5285 fcurfid
= format(curfid
, 'x').lower();
5286 if(not followlink
and finode
!=0):
5287 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=1):
5288 fcurinode
= format(int(curinode
), 'x').lower();
5289 inodetofile
.update({curinode
: fname
});
5290 filetoinode
.update({fname
: curinode
});
5291 curinode
= curinode
+ 1;
5293 fcurinode
= format(int(filetoinode
[flinkname
]), 'x').lower();
5295 fcurinode
= format(int(curinode
), 'x').lower();
5296 curinode
= curinode
+ 1;
5297 curfid
= curfid
+ 1;
5298 catfileoutstr
= AppendNullBytes([ftypehex
, fname
, flinkname
, fsize
, fatime
, fmtime
, fctime
, fbtime
, fmode
, fuid
, funame
, fgid
, fgname
, fcurfid
, fcurinode
, flinkcount
, fdev_minor
, fdev_major
, frdev_minor
, frdev_major
, extrasizelen
, extrafields
], formatspecs
[5]);
5299 if(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']>0):
5300 extrafieldslist
= [];
5302 exil
= listcatfiles
['ffilelist'][reallcfi
]['fextrafields'];
5304 extrafieldslist
.append(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
5306 catfileoutstr
+= AppendNullBytes([extrafieldslist
], formatspecs
[5]);
5307 catfileoutstr
+= AppendNullBytes([checksumtype
], formatspecs
[5]);
5308 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
5309 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
5310 if(checksumtype
=="none" or checksumtype
==""):
5311 catfileheadercshex
= format(0, 'x').lower();
5312 catfilecontentcshex
= format(0, 'x').lower();
5313 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5314 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5315 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
5316 elif(checksumtype
=="crc16_ccitt"):
5317 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5318 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
5319 elif(checksumtype
=="adler32"):
5320 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5321 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
5322 elif(checksumtype
=="crc32"):
5323 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5324 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
5325 elif(checksumtype
=="crc64_ecma"):
5326 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5327 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
5328 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5329 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5330 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
5331 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5332 checksumoutstr
= hashlib
.new(checksumtype
);
5333 checksumoutstr
.update("".encode('UTF-8'));
5334 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5335 checksumoutstr
= hashlib
.new(checksumtype
);
5336 checksumoutstr
.update(fcontents
);
5337 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
5339 catfileheadercshex
= format(0, 'x').lower();
5340 catfilecontentcshex
= format(0, 'x').lower();
5341 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5342 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
5343 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5344 if(checksumtype
=="none" or checksumtype
==""):
5345 catfileheadercshex
= format(0, 'x').lower();
5346 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5347 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5348 elif(checksumtype
=="crc16_ccitt"):
5349 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5350 elif(checksumtype
=="adler32"):
5351 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5352 elif(checksumtype
=="crc32"):
5353 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5354 elif(checksumtype
=="crc64_ecma"):
5355 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5356 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5357 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5358 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5359 checksumoutstr
= hashlib
.new(checksumtype
);
5360 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5361 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5363 catfileheadercshex
= format(0, 'x').lower();
5364 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5365 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5366 nullstrecd
= formatspecs
[5].encode('UTF-8');
5367 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
5368 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
5369 catfp
.write(catfileout
);
5372 os
.fsync(catfp
.fileno());
5373 except io
.UnsupportedOperation
:
5375 except AttributeError:
5378 reallcfi
= reallcfi
+ 1;
5379 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
5380 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
5383 os
.fsync(catfp
.fileno());
5384 except io
.UnsupportedOperation
:
5386 except AttributeError:
5390 if(hasattr(sys
.stdout
, "buffer")):
5391 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
5393 shutil
.copyfileobj(catfp
, sys
.stdout
);
5394 elif(re
.findall(r
"^(ftp|ftps|sftp)\:\/\/", outfile
)):
5395 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
5397 upload_file_to_internet_file(catfp
, outfile
);
5405 create_alias_function("RePack", __file_format_name__
, "", RePackArchiveFile
);
5407 def RePackArchiveFileFromString(catstr
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5408 catfp
= BytesIO(catstr
);
5409 listcatfiles
= RePackArchiveFile(catfp
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
5410 return listcatfiles
;
5412 create_alias_function("RePack", __file_format_name__
, "FromString", RePackArchiveFileFromString
);
5414 def PackArchiveFileFromListDir(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, skipchecksum
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5415 outarray
= BytesIO();
5416 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
5417 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
5418 return listcatfiles
;
5420 create_alias_function("Pack", __file_format_name__
, "FromListDir", PackArchiveFileFromListDir
);
5422 def ArchiveFileArrayBase64Encode(infile
, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5424 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5425 if(isinstance(infile
, dict)):
5426 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5427 listcatfiles
= prelistcatfiles
['list'];
5429 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5430 infile
= RemoveWindowsPath(infile
);
5432 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5433 listcatfiles
= prelistcatfiles
['list'];
5435 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5436 if(not listcatfiles
):
5438 lenlist
= len(listcatfiles
['ffilelist']);
5443 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
5446 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5447 lcfx
= listcatfiles
['fnumfiles'];
5449 lcfx
= int(listcatfiles
['fnumfiles']);
5450 if(lenlist
>lcfx
or lenlist
<lcfx
):
5453 if(listcatfiles
['ffilelist'][lcfi
]['fhascontents']):
5454 listcatfiles
['ffilelist'][lcfi
]['fcontents'] = base64
.b64encode(listcatfiles
['ffilelist'][lcfi
]['fcontents']).decode("UTF-8");
5456 return listcatfiles
;
5458 create_alias_function("", __file_format_name__
, "ArrayBase64Encode", ArchiveFileArrayBase64Encode
);
5460 def ArchiveFileArrayBase64Decode(infile
, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5462 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5463 if(isinstance(infile
, dict)):
5464 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5465 listcatfiles
= prelistcatfiles
['list'];
5467 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5468 infile
= RemoveWindowsPath(infile
);
5470 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5471 listcatfiles
= prelistcatfiles
['list'];
5473 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5474 if(not listcatfiles
):
5476 lenlist
= len(listcatfiles
['ffilelist']);
5481 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
5484 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5485 lcfx
= listcatfiles
['fnumfiles'];
5487 lcfx
= int(listcatfiles
['fnumfiles']);
5488 if(lenlist
>lcfx
or lenlist
<lcfx
):
5491 if(listcatfiles
['ffilelist'][lcfi
]['fhascontents']):
5492 listcatfiles
['ffilelist'][lcfi
]['fcontents'] = base64
.b64decode(listcatfiles
['ffilelist'][lcfi
]['fcontents'].encode("UTF-8"));
5494 return listcatfiles
;
5496 create_alias_function("", __file_format_name__
, "ArrayBase64Decode", ArchiveFileArrayBase64Decode
);
5498 def UnPackArchiveFile(infile
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, preservepermissions
=True, preservetime
=True, verbose
=False, returnfp
=False):
5499 if(outdir
is not None):
5500 outdir
= RemoveWindowsPath(outdir
);
5502 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5503 if(isinstance(infile
, dict)):
5504 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5505 listcatfiles
= prelistcatfiles
['list'];
5507 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5508 infile
= RemoveWindowsPath(infile
);
5510 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5511 listcatfiles
= prelistcatfiles
['list'];
5513 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5514 if(not listcatfiles
):
5516 lenlist
= len(listcatfiles
['ffilelist']);
5521 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
5524 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5525 lcfx
= listcatfiles
['fnumfiles'];
5527 lcfx
= int(listcatfiles
['fnumfiles']);
5528 if(lenlist
>lcfx
or lenlist
<lcfx
):
5535 userinfo
= pwd
.getpwuid(listcatfiles
['ffilelist'][lcfi
]['fuid']);
5536 funame
= userinfo
.pw_name
;
5545 groupinfo
= grp
.getgrgid(listcatfiles
['ffilelist'][lcfi
]['fgid']);
5546 fgname
= groupinfo
.gr_name
;
5552 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
5553 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5554 with
open(listcatfiles
['ffilelist'][lcfi
]['fname'], "wb") as fpc
:
5555 fpc
.write(listcatfiles
['ffilelist'][lcfi
]['fcontents'])
5558 os
.fsync(fpc
.fileno())
5559 except io
.UnsupportedOperation
:
5561 except AttributeError:
5563 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
5564 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
5565 if(preservepermissions
):
5566 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fchmode']);
5568 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
5569 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5571 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
5572 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
5573 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
5578 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
5579 funame
= userinfo
.pw_name
;
5588 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
5589 fgname
= groupinfo
.gr_name
;
5594 if(flinkinfo
['ftype'] == 0 or flinkinfo
['ftype'] == 7):
5595 with
open(listcatfiles
['ffilelist'][lcfi
]['fname'], "wb") as fpc
:
5596 fpc
.write(flinkinfo
['fcontents'])
5599 os
.fsync(fpc
.fileno())
5600 except io
.UnsupportedOperation
:
5602 except AttributeError:
5604 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
5605 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
5606 if(preservepermissions
):
5607 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5609 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
5610 if(flinkinfo
['ftype']==1):
5611 os
.link(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
5612 if(flinkinfo
['ftype']==2):
5613 os
.symlink(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
5614 if(flinkinfo
['ftype']==5):
5615 if(preservepermissions
):
5616 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5618 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname']);
5619 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
5620 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
5621 if(preservepermissions
):
5622 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5624 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
5625 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
5626 os
.mkfifo(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5628 os
.link(listcatfiles
['ffilelist'][lcfi
]['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
5629 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5631 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
5632 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
5633 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
5638 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
5639 funame
= userinfo
.pw_name
;
5648 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
5649 fgname
= groupinfo
.gr_name
;
5654 if(flinkinfo
['ftype']==0 or flinkinfo
['ftype']==7):
5655 with
open(listcatfiles
['ffilelist'][lcfi
]['fname'], "wb") as fpc
:
5656 fpc
.write(flinkinfo
['fcontents'])
5659 os
.fsync(fpc
.fileno())
5660 except io
.UnsupportedOperation
:
5662 except AttributeError:
5664 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
5665 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
5666 if(preservepermissions
):
5667 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5669 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
5670 if(flinkinfo
['ftype']==1):
5671 os
.link(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
5672 if(flinkinfo
['ftype']==2):
5673 os
.symlink(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
5674 if(flinkinfo
['ftype']==5):
5675 if(preservepermissions
):
5676 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5678 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname']);
5679 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
5680 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
5681 if(preservepermissions
):
5682 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5684 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
5685 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
5686 os
.mkfifo(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5688 os
.symlink(listcatfiles
['ffilelist'][lcfi
]['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
5689 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5690 if(preservepermissions
):
5691 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fchmode']);
5693 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname']);
5694 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
5695 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
5696 if(preservepermissions
):
5697 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fchmode']);
5699 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
5700 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6 and hasattr(os
, "mkfifo")):
5701 os
.mkfifo(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fchmode']);
5704 return listcatfiles
['ffilelist']['catfp'];
5708 create_alias_function("UnPack", __file_format_name__
, "", UnPackArchiveFile
);
5710 if(hasattr(shutil
, "register_unpack_format")):
5711 def UnPackArchiveFileFunc(archive_name
, extract_dir
=None, **kwargs
):
5712 return UnPackArchiveFile(archive_name
, extract_dir
, False, 0, 0, False, __file_format_delimiter__
, False, False);
5713 create_alias_function("UnPack", __file_format_name__
, "Func", UnPackArchiveFileFunc
);
5715 def UnPackArchiveFileString(catstr
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5716 catfp
= BytesIO(catstr
);
5717 listcatfiles
= UnPackArchiveFile(catfp
, outdir
, followlink
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
5718 return listcatfiles
;
5720 create_alias_function("UnPack", __file_format_name__
, "String", UnPackArchiveFileString
);
5722 def ArchiveFileListFiles(infile
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5723 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5724 if(isinstance(infile
, dict)):
5725 listcatfiles
= infile
;
5727 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5728 infile
= RemoveWindowsPath(infile
);
5729 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, True, skipchecksum
, formatspecs
, returnfp
);
5730 if(not listcatfiles
):
5732 lenlist
= len(listcatfiles
['ffilelist']);
5737 returnval
.update({lcfi
: listcatfiles
['ffilelist'][lcfi
]['fname']});
5739 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
5741 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' } };
5742 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'];
5743 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5744 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " link to " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
5745 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5746 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " -> " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
5747 fuprint
= listcatfiles
['ffilelist'][lcfi
]['funame'];
5748 if(len(fuprint
)<=0):
5749 fuprint
= listcatfiles
['ffilelist'][lcfi
]['fuid'];
5750 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgname'];
5751 if(len(fgprint
)<=0):
5752 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgid'];
5753 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
));
5756 return listcatfiles
['catfp'];
5760 create_alias_function("", __file_format_name__
, "ListFiles", ArchiveFileListFiles
);
5762 def ArchiveFileStringListFiles(catstr
, followlink
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5763 catfp
= BytesIO(catstr
);
5764 listcatfiles
= UnPackArchiveFile(catfp
, None, followlink
, skipchecksum
, formatspecs
, verbose
, returnfp
);
5765 return listcatfiles
;
5767 create_alias_function("", __file_format_name__
, "StringListFiles", ArchiveFileListFiles
);
5769 def TarFileListFiles(infile
, verbose
=False, returnfp
=False):
5770 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5773 if(hasattr(sys
.stdin
, "buffer")):
5774 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
5776 shutil
.copyfileobj(sys
.stdin
, infile
);
5781 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
5782 infile
= download_file_from_internet_file(infile
);
5787 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5789 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
5791 if(not tarfile
.is_tarfile(infile
)):
5793 except AttributeError:
5794 if(not is_tarfile(infile
)):
5799 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
5800 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
5802 tarfp
= tarfile
.open(infile
, "r");
5803 except FileNotFoundError
:
5805 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
5806 returnval
.update({lcfi
: member
.name
});
5807 fpremode
= member
.mode
;
5808 ffullmode
= member
.mode
;
5812 ffullmode
= member
.mode
+ stat
.S_IFREG
;
5814 elif(member
.isdev()):
5815 ffullmode
= member
.mode
;
5817 elif(member
.islnk()):
5818 ffullmode
= member
.mode
+ stat
.S_IFREG
;
5820 elif(member
.issym()):
5821 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
5823 elif(member
.ischr()):
5824 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
5826 elif(member
.isblk()):
5827 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
5829 elif(member
.isdir()):
5830 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
5832 elif(member
.isfifo()):
5833 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
5835 elif(member
.issparse()):
5836 ffullmode
= member
.mode
;
5839 VerbosePrintOut(member
.name
);
5841 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' } };
5842 printfname
= member
.name
;
5844 printfname
= member
.name
+ " link to " + member
.linkname
;
5845 elif(member
.issym()):
5846 printfname
= member
.name
+ " -> " + member
.linkname
;
5847 fuprint
= member
.uname
;
5848 if(len(fuprint
)<=0):
5849 fuprint
= member
.uid
;
5850 fgprint
= member
.gname
;
5851 if(len(fgprint
)<=0):
5852 fgprint
= member
.gid
;
5853 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
));
5856 return listcatfiles
['catfp'];
5860 def ZipFileListFiles(infile
, verbose
=False, returnfp
=False):
5861 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5864 if(hasattr(sys
.stdin
, "buffer")):
5865 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
5867 shutil
.copyfileobj(sys
.stdin
, infile
);
5872 elif(re
.findall(r
"^(http|https|ftp|ftps|sftp)\:\/\/", infile
)):
5873 infile
= download_file_from_internet_file(infile
);
5878 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5882 if(not zipfile
.is_zipfile(infile
)):
5885 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
5886 except FileNotFoundError
:
5890 ziptest
= zipfp
.testzip();
5892 VerbosePrintOut("Bad file found!");
5893 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
5894 if(not member
.is_dir()):
5895 fpremode
= int(stat
.S_IFREG
+ 438);
5896 elif(member
.is_dir()):
5897 fpremode
= int(stat
.S_IFDIR
+ 511);
5898 if(not member
.is_dir()):
5899 fmode
= int(stat
.S_IFREG
+ 438);
5900 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
5901 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
5902 elif(member
.is_dir()):
5903 fmode
= int(stat
.S_IFDIR
+ 511);
5904 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
5905 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
5906 returnval
.update({lcfi
: member
.filename
});
5908 VerbosePrintOut(member
.filename
);
5910 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' } };
5912 for fmodval
in str(oct(fmode
))[-3:]:
5913 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
5914 if(not member
.is_dir()):
5916 permissionstr
= "-" + permissionstr
;
5917 elif(member
.is_dir()):
5919 permissionstr
= "d" + permissionstr
;
5920 printfname
= member
.filename
;
5922 fuid
= int(os
.getuid());
5923 except AttributeError:
5928 fgid
= int(os
.getgid());
5929 except AttributeError:
5936 userinfo
= pwd
.getpwuid(os
.getuid());
5937 funame
= userinfo
.pw_name
;
5940 except AttributeError:
5948 groupinfo
= grp
.getgrgid(os
.getgid());
5949 fgname
= groupinfo
.gr_name
;
5952 except AttributeError:
5957 if(len(fuprint
)<=0):
5958 fuprint
= str(fuid
);
5960 if(len(fgprint
)<=0):
5961 fgprint
= str(fgid
);
5962 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
));
5965 return listcatfiles
['catfp'];
5969 if(not rarfile_support
):
5970 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
5971 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5972 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5975 if(rarfile_support
):
5976 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
5977 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5978 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5980 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
5984 rarfp
= rarfile
.RarFile(infile
, "r");
5985 rartest
= rarfp
.testrar();
5987 VerbosePrintOut("Bad file found!");
5988 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
5991 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
5994 member
.external_attr
5996 except AttributeError:
5998 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
6001 member
.external_attr
6003 except AttributeError:
6008 if(is_unix
and member
.external_attr
!=0):
6009 fpremode
= int(member
.external_attr
);
6010 elif(member
.is_file()):
6011 fpremode
= int(stat
.S_IFREG
+ 438);
6012 elif(member
.is_symlink()):
6013 fpremode
= int(stat
.S_IFLNK
+ 438);
6014 elif(member
.is_dir()):
6015 fpremode
= int(stat
.S_IFDIR
+ 511);
6016 if(is_windows
and member
.external_attr
!=0):
6017 fwinattributes
= int(member
.external_attr
);
6019 fwinattributes
= int(0);
6020 if(is_unix
and member
.external_attr
!=0):
6021 fmode
= int(member
.external_attr
);
6022 fchmode
= int(stat
.S_IMODE(member
.external_attr
));
6023 ftypemod
= int(stat
.S_IFMT(member
.external_attr
));
6024 elif(member
.is_file()):
6025 fmode
= int(stat
.S_IFREG
+ 438);
6026 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
6027 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
6028 elif(member
.is_symlink()):
6029 fmode
= int(stat
.S_IFLNK
+ 438);
6030 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFLNK
+ 438)));
6031 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFLNK
+ 438)));
6032 elif(member
.is_dir()):
6033 fmode
= int(stat
.S_IFDIR
+ 511);
6034 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
6035 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
6036 returnval
.update({lcfi
: member
.filename
});
6038 VerbosePrintOut(member
.filename
);
6040 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' } };
6042 for fmodval
in str(oct(fmode
))[-3:]:
6043 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
6044 if(member
.is_file()):
6046 permissionstr
= "-" + permissionstr
;
6047 printfname
= member
.filename
;
6048 elif(member
.is_symlink()):
6050 permissionstr
= "l" + permissionstr
;
6051 printfname
= member
.name
+ " -> " + member
.read().decode("UTF-8");
6052 elif(member
.is_dir()):
6054 permissionstr
= "d" + permissionstr
;
6055 printfname
= member
.filename
;
6057 fuid
= int(os
.getuid());
6058 except AttributeError:
6063 fgid
= int(os
.getgid());
6064 except AttributeError:
6071 userinfo
= pwd
.getpwuid(os
.getuid());
6072 funame
= userinfo
.pw_name
;
6075 except AttributeError:
6083 groupinfo
= grp
.getgrgid(os
.getgid());
6084 fgname
= groupinfo
.gr_name
;
6087 except AttributeError:
6092 if(len(fuprint
)<=0):
6093 fuprint
= str(fuid
);
6095 if(len(fgprint
)<=0):
6096 fgprint
= str(fgid
);
6097 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(member
.file_size
).rjust(15) + " " + member
.mtime
.strftime('%Y-%m-%d %H:%M') + " " + printfname
));
6100 return listcatfiles
['catfp'];
6104 def ListDirListFiles(infiles
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6105 outarray
= BytesIO();
6106 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, False, True);
6107 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6108 return listcatfiles
;
6110 def ListDirListFilesAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=True, seekstart
=0, seekend
=0, skipchecksum
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6111 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, formatspecs
, verbose
);
6112 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
6113 return listcatfiles
;
6115 def PackArchiveFileFromListDirAlt(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, skipchecksum
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6116 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, False, checksumtype
, extradata
, formatspecs
, False);
6117 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, followlink
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
6118 return listcatfiles
;
6120 create_alias_function("Pack", __file_format_name__
, "FromListDirAlt", PackArchiveFileFromListDirAlt
);
6122 def PackArchiveFileFromTarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6123 outarray
= TarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
6124 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
6125 return listcatfiles
;
6127 create_alias_function("Pack", __file_format_name__
, "FromTarFileAlt", PackArchiveFileFromTarFileAlt
);
6129 def PackArchiveFileFromZipFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6130 outarray
= ZipFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
6131 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
6132 return listcatfiles
;
6134 create_alias_function("Pack", __file_format_name__
, "FromZipFileAlt", PackArchiveFileFromZipFileAlt
);
6136 if(not rarfile_support
):
6137 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6140 if(rarfile_support
):
6141 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
6142 outarray
= RarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
6143 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
6144 return listcatfiles
;
6146 create_alias_function("Pack", __file_format_name__
, "FromRarFileAlt", PackArchiveFileFromRarFileAlt
);
6148 def download_file_from_ftp_file(url
):
6149 urlparts
= urlparse(url
);
6150 file_name
= os
.path
.basename(urlparts
.path
);
6151 file_dir
= os
.path
.dirname(urlparts
.path
);
6152 if(urlparts
.username
is not None):
6153 ftp_username
= urlparts
.username
;
6155 ftp_username
= "anonymous";
6156 if(urlparts
.password
is not None):
6157 ftp_password
= urlparts
.password
;
6158 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6159 ftp_password
= "anonymous";
6162 if(urlparts
.scheme
=="ftp"):
6164 elif(urlparts
.scheme
=="ftps"):
6168 if(urlparts
.scheme
=="sftp"):
6170 return download_file_from_pysftp_file(url
);
6172 return download_file_from_sftp_file(url
);
6173 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6174 return download_file_from_http_file(url
);
6175 ftp_port
= urlparts
.port
;
6176 if(urlparts
.port
is None):
6179 ftp
.connect(urlparts
.hostname
, ftp_port
);
6180 except socket
.gaierror
:
6181 log
.info("Error With URL "+url
);
6183 except socket
.timeout
:
6184 log
.info("Error With URL "+url
);
6186 ftp
.login(urlparts
.username
, urlparts
.password
);
6187 if(urlparts
.scheme
=="ftps"):
6189 ftpfile
= BytesIO();
6190 ftp
.retrbinary("RETR "+urlparts
.path
, ftpfile
.write
);
6191 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
6196 def download_file_from_ftp_string(url
):
6197 ftpfile
= download_file_from_ftp_file(url
);
6198 return ftpfile
.read();
6200 def upload_file_to_ftp_file(ftpfile
, url
):
6201 urlparts
= urlparse(url
);
6202 file_name
= os
.path
.basename(urlparts
.path
);
6203 file_dir
= os
.path
.dirname(urlparts
.path
);
6204 if(urlparts
.username
is not None):
6205 ftp_username
= urlparts
.username
;
6207 ftp_username
= "anonymous";
6208 if(urlparts
.password
is not None):
6209 ftp_password
= urlparts
.password
;
6210 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6211 ftp_password
= "anonymous";
6214 if(urlparts
.scheme
=="ftp"):
6216 elif(urlparts
.scheme
=="ftps"):
6220 if(urlparts
.scheme
=="sftp"):
6222 return upload_file_from_pysftp_file(url
);
6224 return upload_file_from_sftp_file(url
);
6225 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6227 ftp_port
= urlparts
.port
;
6228 if(urlparts
.port
is None):
6231 ftp
.connect(urlparts
.hostname
, ftp_port
);
6232 except socket
.gaierror
:
6233 log
.info("Error With URL "+url
);
6235 except socket
.timeout
:
6236 log
.info("Error With URL "+url
);
6238 ftp
.login(urlparts
.username
, urlparts
.password
);
6239 if(urlparts
.scheme
=="ftps"):
6241 ftp
.storbinary("STOR "+urlparts
.path
, ftpfile
);
6246 def upload_file_to_ftp_string(ftpstring
, url
):
6247 ftpfileo
= BytesIO(ftpstring
);
6248 ftpfile
= upload_file_to_ftp_file(ftpfileo
, url
);
6252 def download_file_from_http_file(url
, headers
=geturls_headers_pycatfile_python_alt
):
6253 # Parse the URL to extract username and password if present
6254 urlparts
= urlparse(url
);
6255 username
= urlparts
.username
;
6256 password
= urlparts
.password
;
6257 # Rebuild the URL without the username and password
6258 netloc
= urlparts
.hostname
;
6259 if(urlparts
.scheme
=="sftp"):
6261 return download_file_from_pysftp_file(url
);
6263 return download_file_from_sftp_file(url
);
6264 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
6265 return download_file_from_ftp_file(url
);
6267 netloc
+= ':' + str(urlparts
.port
);
6268 rebuilt_url
= urlunparse((urlparts
.scheme
, netloc
, urlparts
.path
, urlparts
.params
, urlparts
.query
, urlparts
.fragment
));
6269 # Create a temporary file object
6270 temp_file
= BytesIO();
6272 # Use the requests library if available
6273 if username
and password
:
6274 response
= requests
.get(rebuilt_url
, headers
=headers
, auth
=(username
, password
), stream
=True);
6276 response
= requests
.get(rebuilt_url
, headers
=headers
, stream
=True);
6277 response
.raw
.decode_content
= True
6278 shutil
.copyfileobj(response
.raw
, temp_file
);
6280 # Build a Request object for urllib
6281 request
= Request(rebuilt_url
, headers
=headers
);
6282 # Create an opener object for handling URLs
6283 if username
and password
:
6284 # Create a password manager
6285 password_mgr
= HTTPPasswordMgrWithDefaultRealm();
6286 # Add the username and password
6287 password_mgr
.add_password(None, rebuilt_url
, username
, password
);
6288 # Create an authentication handler using the password manager
6289 auth_handler
= HTTPBasicAuthHandler(password_mgr
);
6290 # Build the opener with the authentication handler
6291 opener
= build_opener(auth_handler
);
6293 opener
= build_opener();
6294 with opener
.open(request
) as response
:
6295 shutil
.copyfileobj(response
, temp_file
);
6296 # Reset file pointer to the start
6298 # Return the temporary file object
6301 def download_file_from_http_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
6302 httpfile
= download_file_from_http_file(url
, headers
);
6303 return ftpfile
.read();
6306 def download_file_from_sftp_file(url
):
6307 urlparts
= urlparse(url
);
6308 file_name
= os
.path
.basename(urlparts
.path
);
6309 file_dir
= os
.path
.dirname(urlparts
.path
);
6310 sftp_port
= urlparts
.port
;
6311 if(urlparts
.port
is None):
6314 sftp_port
= urlparts
.port
;
6315 if(urlparts
.username
is not None):
6316 sftp_username
= urlparts
.username
;
6318 sftp_username
= "anonymous";
6319 if(urlparts
.password
is not None):
6320 sftp_password
= urlparts
.password
;
6321 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6322 sftp_password
= "anonymous";
6325 if(urlparts
.scheme
=="ftp"):
6326 return download_file_from_ftp_file(url
);
6327 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6328 return download_file_from_http_file(url
);
6329 if(urlparts
.scheme
!="sftp"):
6331 ssh
= paramiko
.SSHClient();
6332 ssh
.load_system_host_keys();
6333 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
6335 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
6336 except paramiko
.ssh_exception
.SSHException
:
6338 except socket
.gaierror
:
6339 log
.info("Error With URL "+url
);
6341 except socket
.timeout
:
6342 log
.info("Error With URL "+url
);
6344 sftp
= ssh
.open_sftp();
6345 sftpfile
= BytesIO();
6346 sftp
.getfo(urlparts
.path
, sftpfile
);
6349 sftpfile
.seek(0, 0);
6352 def download_file_from_sftp_file(url
):
6356 def download_file_from_sftp_string(url
):
6357 sftpfile
= download_file_from_sftp_file(url
);
6358 return sftpfile
.read();
6360 def download_file_from_ftp_string(url
):
6364 def upload_file_to_sftp_file(sftpfile
, url
):
6365 urlparts
= urlparse(url
);
6366 file_name
= os
.path
.basename(urlparts
.path
);
6367 file_dir
= os
.path
.dirname(urlparts
.path
);
6368 sftp_port
= urlparts
.port
;
6369 if(urlparts
.port
is None):
6372 sftp_port
= urlparts
.port
;
6373 if(urlparts
.username
is not None):
6374 sftp_username
= urlparts
.username
;
6376 sftp_username
= "anonymous";
6377 if(urlparts
.password
is not None):
6378 sftp_password
= urlparts
.password
;
6379 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6380 sftp_password
= "anonymous";
6383 if(urlparts
.scheme
=="ftp"):
6384 return upload_file_from_ftp_file(url
);
6385 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6387 if(urlparts
.scheme
!="sftp"):
6389 ssh
= paramiko
.SSHClient();
6390 ssh
.load_system_host_keys();
6391 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
6393 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
6394 except paramiko
.ssh_exception
.SSHException
:
6396 except socket
.gaierror
:
6397 log
.info("Error With URL "+url
);
6399 except socket
.timeout
:
6400 log
.info("Error With URL "+url
);
6402 sftp
= ssh
.open_sftp();
6403 sftp
.putfo(sftpfile
, urlparts
.path
);
6406 sftpfile
.seek(0, 0);
6409 def upload_file_to_sftp_file(sftpfile
, url
):
6413 def upload_file_to_sftp_string(sftpstring
, url
):
6414 sftpfileo
= BytesIO(sftpstring
);
6415 sftpfile
= upload_file_to_sftp_files(ftpfileo
, url
);
6419 def upload_file_to_sftp_string(url
):
6423 def download_file_from_pysftp_file(url
):
6424 urlparts
= urlparse(url
);
6425 file_name
= os
.path
.basename(urlparts
.path
);
6426 file_dir
= os
.path
.dirname(urlparts
.path
);
6427 sftp_port
= urlparts
.port
;
6428 if(urlparts
.port
is None):
6431 sftp_port
= urlparts
.port
;
6432 if(urlparts
.username
is not None):
6433 sftp_username
= urlparts
.username
;
6435 sftp_username
= "anonymous";
6436 if(urlparts
.password
is not None):
6437 sftp_password
= urlparts
.password
;
6438 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6439 sftp_password
= "anonymous";
6442 if(urlparts
.scheme
=="ftp"):
6443 return download_file_from_ftp_file(url
);
6444 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6445 return download_file_from_http_file(url
);
6446 if(urlparts
.scheme
!="sftp"):
6449 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
6450 except paramiko
.ssh_exception
.SSHException
:
6452 except socket
.gaierror
:
6453 log
.info("Error With URL "+url
);
6455 except socket
.timeout
:
6456 log
.info("Error With URL "+url
);
6458 sftp
= ssh
.open_sftp();
6459 sftpfile
= BytesIO();
6460 sftp
.getfo(urlparts
.path
, sftpfile
);
6463 sftpfile
.seek(0, 0);
6466 def download_file_from_pysftp_file(url
):
6470 def download_file_from_pysftp_string(url
):
6471 sftpfile
= download_file_from_pysftp_file(url
);
6472 return sftpfile
.read();
6474 def download_file_from_ftp_string(url
):
6478 def upload_file_to_pysftp_file(sftpfile
, url
):
6479 urlparts
= urlparse(url
);
6480 file_name
= os
.path
.basename(urlparts
.path
);
6481 file_dir
= os
.path
.dirname(urlparts
.path
);
6482 sftp_port
= urlparts
.port
;
6483 if(urlparts
.port
is None):
6486 sftp_port
= urlparts
.port
;
6487 if(urlparts
.username
is not None):
6488 sftp_username
= urlparts
.username
;
6490 sftp_username
= "anonymous";
6491 if(urlparts
.password
is not None):
6492 sftp_password
= urlparts
.password
;
6493 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6494 sftp_password
= "anonymous";
6497 if(urlparts
.scheme
=="ftp"):
6498 return upload_file_from_ftp_file(url
);
6499 elif(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6501 if(urlparts
.scheme
!="sftp"):
6504 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
6505 except paramiko
.ssh_exception
.SSHException
:
6507 except socket
.gaierror
:
6508 log
.info("Error With URL "+url
);
6510 except socket
.timeout
:
6511 log
.info("Error With URL "+url
);
6513 sftp
= ssh
.open_sftp();
6514 sftp
.putfo(sftpfile
, urlparts
.path
);
6517 sftpfile
.seek(0, 0);
6520 def upload_file_to_pysftp_file(sftpfile
, url
):
6524 def upload_file_to_pysftp_string(sftpstring
, url
):
6525 sftpfileo
= BytesIO(sftpstring
);
6526 sftpfile
= upload_file_to_pysftp_files(ftpfileo
, url
);
6530 def upload_file_to_pysftp_string(url
):
6533 def download_file_from_internet_file(url
, headers
=geturls_headers_pycatfile_python_alt
):
6534 urlparts
= urlparse(url
);
6535 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6536 return download_file_from_http_file(url
, headers
);
6537 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
6538 return download_file_from_ftp_file(url
);
6539 elif(urlparts
.scheme
=="sftp"):
6540 if(__use_pysftp__
and havepysftp
):
6541 return download_file_from_pysftp_file(url
);
6543 return download_file_from_sftp_file(url
);
6548 def download_file_from_internet_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
6549 urlparts
= urlparse(url
);
6550 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6551 return download_file_from_http_string(url
, headers
);
6552 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
6553 return download_file_from_ftp_string(url
);
6554 elif(urlparts
.scheme
=="sftp"):
6555 if(__use_pysftp__
and havepysftp
):
6556 return download_file_from_pysftp_string(url
);
6558 return download_file_from_sftp_string(url
);
6563 def upload_file_to_internet_file(ifp
, url
):
6564 urlparts
= urlparse(url
);
6565 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6567 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
6568 return upload_file_from_ftp_file(ifp
, url
);
6569 elif(urlparts
.scheme
=="sftp"):
6570 if(__use_pysftp__
and havepysftp
):
6571 return upload_file_from_pysftp_file(ifp
, url
);
6573 return download_file_from_sftp_file(ifp
, url
);
6578 def upload_file_to_internet_string(ifp
, url
):
6579 urlparts
= urlparse(url
);
6580 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6582 elif(urlparts
.scheme
=="ftp" or urlparts
.scheme
=="ftps"):
6583 return upload_file_from_ftp_string(ifp
, url
);
6584 elif(urlparts
.scheme
=="sftp"):
6585 if(__use_pysftp__
and havepysftp
):
6586 return upload_file_from_pysftp_string(ifp
, url
);
6588 return download_file_from_sftp_string(ifp
, url
);
6594 if(hasattr(shutil
, "register_archive_format")):
6595 # Register the packing format
6596 shutil
.register_archive_format(__file_format_name__
, PackArchiveFileFunc
, description
='Pack concatenated files');
6597 except shutil
.RegistryError
:
6601 if(hasattr(shutil
, "register_unpack_format")):
6602 # Register the unpacking format
6603 shutil
.register_unpack_format(__file_format_name__
, archivefile_extensions
, UnPackArchiveFileFunc
, description
='UnPack concatenated files');
6604 except shutil
.RegistryError
: