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 from urllib
.request
import Request
, build_opener
, HTTPBasicAuthHandler
;
112 from urllib
.parse
import urlparse
;
115 from urllib2
import Request
, build_opener
, HTTPBasicAuthHandler
;
116 from urlparse
import urlparse
;
118 if(sys
.version
[0]=="2"):
120 from io
import StringIO
, BytesIO
;
123 from cStringIO
import StringIO
;
124 from cStringIO
import StringIO
as BytesIO
;
126 from StringIO
import StringIO
;
127 from StringIO
import StringIO
as BytesIO
;
128 elif(sys
.version
[0]>="3"):
129 from io
import StringIO
, BytesIO
;
134 from cStringIO
import StringIO
as BytesIO
;
140 from StringIO
import StringIO
as BytesIO
;
146 from io
import BytesIO
;
151 __use_pysftp__
= False;
153 __use_pysftp__
= False;
154 __file_format_name__
= "CatFile";
155 __program_name__
= "Py"+__file_format_name__
;
156 __file_format_lower__
= __file_format_name__
.lower();
157 __file_format_magic__
= __file_format_name__
;
158 __file_format_len__
= len(__file_format_magic__
);
159 __file_format_hex__
= binascii
.hexlify(__file_format_magic__
.encode("UTF-8")).decode("UTF-8");
160 __file_format_delimiter__
= "\x00";
161 __file_format_ver__
= "001";
162 __use_new_style__
= True;
163 __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__
];
164 __project__
= __program_name__
;
165 __project_url__
= "https://github.com/GameMaker2k/PyCatFile";
166 __version_info__
= (0, 5, 0, "RC 1", 1);
167 __version_date_info__
= (2024, 3, 15, "RC 1", 1);
168 __version_date__
= str(__version_date_info__
[0]) + "." + str(__version_date_info__
[1]).zfill(2) + "." + str(__version_date_info__
[2]).zfill(2);
169 __revision__
= __version_info__
[3];
170 __revision_id__
= "$Id$";
171 if(__version_info__
[4] is not None):
172 __version_date_plusrc__
= __version_date__
+ "-" + str(__version_date_info__
[4]);
173 if(__version_info__
[4] is None):
174 __version_date_plusrc__
= __version_date__
;
175 if(__version_info__
[3] is not None):
176 __version__
= str(__version_info__
[0]) + "." + str(__version_info__
[1]) + "." + str(__version_info__
[2]) + " " + str(__version_info__
[3]);
177 if(__version_info__
[3] is None):
178 __version__
= str(__version_info__
[0]) + "." + str(__version_info__
[1]) + "." + str(__version_info__
[2]);
180 PyBitness
= platform
.architecture();
181 if(PyBitness
=="32bit" or PyBitness
=="32"):
183 elif(PyBitness
=="64bit" or PyBitness
=="64"):
188 geturls_ua_pycatfile_python
= "Mozilla/5.0 (compatible; {proname}/{prover}; +{prourl})".format(proname
=__project__
, prover
=__version__
, prourl
=__project_url__
);
189 if(platform
.python_implementation()!=""):
190 py_implementation
= platform
.python_implementation();
191 if(platform
.python_implementation()==""):
192 py_implementation
= "Python";
193 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__
);
194 geturls_ua_googlebot_google
= "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
195 geturls_ua_googlebot_google_old
= "Googlebot/2.1 (+http://www.google.com/bot.html)";
196 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
)};
197 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
)};
198 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"};
199 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"};
201 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'xz', 'lz4', 'lzo', 'lzma'];
202 outextlist
= ['gz', 'bz2', 'zst', 'xz', 'lz4', 'lzo', 'lzma'];
203 outextlistwd
= ['.gz', '.bz2', '.zst', '.xz', '.lz4', '.lzo', '.lzma'];
205 tarfile_mimetype
= "application/tar";
206 tarfile_tar_mimetype
= tarfile_mimetype
;
207 zipfile_mimetype
= "application/zip";
208 zipfile_zip_mimetype
= zipfile_mimetype
;
209 rarfile_mimetype
= "application/rar";
210 rarfile_rar_mimetype
= rarfile_mimetype
;
211 archivefile_mimetype
= "application/x-"+__file_format_list__
[1]+"";
212 archivefile_cat_mimetype
= archivefile_mimetype
;
213 archivefile_gzip_mimetype
= "application/x-"+__file_format_list__
[1]+"+gzip";
214 archivefile_gz_mimetype
= archivefile_gzip_mimetype
;
215 archivefile_bzip2_mimetype
= "application/x-"+__file_format_list__
[1]+"+bzip2";
216 archivefile_bz2_mimetype
= archivefile_bzip2_mimetype
;
217 archivefile_lz4_mimetype
= "application/x-"+__file_format_list__
[1]+"+lz4";
218 archivefile_lzop_mimetype
= "application/x-"+__file_format_list__
[1]+"+lzop";
219 archivefile_lzo_mimetype
= archivefile_lzop_mimetype
;
220 archivefile_zstandard_mimetype
= "application/x-"+__file_format_list__
[1]+"+zstandard";
221 archivefile_zstd_mimetype
= archivefile_zstandard_mimetype
;
222 archivefile_lzma_mimetype
= "application/x-"+__file_format_list__
[1]+"+lzma";
223 archivefile_xz_mimetype
= "application/x-"+__file_format_list__
[1]+"+xz";
224 archivefile_extensions
= ['.cat', '.cat.gz', '.cat.bz2', '.cat.zst', '.cat.lz4', '.cat.lzo', '.cat.lzop', '.cat.lzma', '.cat.xz'];
226 if __name__
== "__main__":
228 curscrpath
= os
.path
.dirname(sys
.argv
[0]);
232 curscrpath
= curscrpath
.replace(os
.sep
, "/");
233 curscrpath
= curscrpath
+ "/";
234 scrfile
= curscrpath
+ "catfile.py";
235 if(os
.path
.exists(scrfile
) and os
.path
.isfile(scrfile
)):
236 scrcmd
= subprocess
.Popen([sys
.executable
, scrfile
] + sys
.argv
[1:]);
239 def VerbosePrintOut(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
245 "warning": logging
.warning
,
246 "error": logging
.error
,
247 "critical": logging
.critical
,
248 "exception": logging
.exception
,
249 "logalt": lambda x
: logging
.log(dgblevel
, x
),
250 "debug": logging
.debug
252 log_function
= log_functions
.get(outtype
);
254 log_function(dbgtxt
);
258 def VerbosePrintOutReturn(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
259 VerbosePrintOut(dbgtxt
, outtype
, dbgenable
, dgblevel
);
262 def RemoveWindowsPath(dpath
):
266 dpath
= dpath
.replace(os
.path
.sep
, "/");
267 dpath
= dpath
.rstrip("/");
268 if(dpath
=="." or dpath
==".."):
272 def NormalizeRelativePath(inpath
):
273 inpath
= RemoveWindowsPath(inpath
);
274 if(os
.path
.isabs(inpath
)):
277 if(inpath
.startswith("./") or inpath
.startswith("../")):
280 outpath
= "./" + inpath
;
283 def ListDir(dirpath
, followlink
=False, duplicates
=False):
284 if(isinstance(dirpath
, (list, tuple, ))):
285 dirpath
= list(filter(None, dirpath
));
286 elif(isinstance(dirpath
, (str, ))):
287 dirpath
= list(filter(None, [dirpath
]));
289 for mydirfile
in dirpath
:
290 if(not os
.path
.exists(mydirfile
)):
292 mydirfile
= NormalizeRelativePath(mydirfile
);
293 if(os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
)):
294 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
));
295 if(os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
)):
296 for root
, dirs
, filenames
in os
.walk(mydirfile
):
298 dpath
= RemoveWindowsPath(dpath
);
299 if(dpath
not in retlist
and not duplicates
):
300 retlist
.append(dpath
);
302 retlist
.append(dpath
);
303 for file in filenames
:
304 fpath
= os
.path
.join(root
, file);
305 fpath
= RemoveWindowsPath(fpath
);
306 if(fpath
not in retlist
and not duplicates
):
307 retlist
.append(fpath
);
309 retlist
.append(fpath
);
311 retlist
.append(RemoveWindowsPath(mydirfile
));
314 def ListDirAdvanced(dirpath
, followlink
=False, duplicates
=False):
315 if isinstance(dirpath
, (list, tuple)):
316 dirpath
= list(filter(None, dirpath
));
317 elif isinstance(dirpath
, str):
318 dirpath
= list(filter(None, [dirpath
]));
320 for mydirfile
in dirpath
:
321 if not os
.path
.exists(mydirfile
):
323 mydirfile
= NormalizeRelativePath(mydirfile
);
324 if os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
) and followlink
:
325 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
))
326 if os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
):
327 for root
, dirs
, filenames
in os
.walk(mydirfile
):
328 # Sort dirs and filenames alphabetically in place
329 dirs
.sort(key
=lambda x
: x
.lower());
330 filenames
.sort(key
=lambda x
: x
.lower());
331 dpath
= RemoveWindowsPath(root
);
332 if not duplicates
and dpath
not in retlist
:
333 retlist
.append(dpath
);
335 retlist
.append(dpath
);
336 for file in filenames
:
337 fpath
= os
.path
.join(root
, file);
338 fpath
= RemoveWindowsPath(fpath
);
339 if not duplicates
and fpath
not in retlist
:
340 retlist
.append(fpath
);
342 retlist
.append(fpath
);
344 retlist
.append(RemoveWindowsPath(mydirfile
));
347 def create_alias_function(prefix
, base_name
, suffix
, target_function
):
348 # Define a new function that wraps the target function
349 def alias_function(*args
, **kwargs
):
350 return target_function(*args
, **kwargs
);
352 # Create the function name by combining the prefix, base name, and the suffix
353 function_name
= "{}{}{}".format(prefix
, base_name
, suffix
);
355 # Add the new function to the global namespace
356 globals()[function_name
] = alias_function
;
358 # initial_value can be 0xFFFF or 0x0000
359 def crc16_ansi(msg
, initial_value
=0xFFFF):
360 # CRC-16-IBM / CRC-16-ANSI polynomial and initial value
361 poly
= 0x8005; # Polynomial for CRC-16-IBM / CRC-16-ANSI
362 crc
= initial_value
; # Initial value
364 crc ^
= b
<< 8; # XOR byte into CRC top byte
365 for _
in range(8): # Process each bit
366 if crc
& 0x8000: # If the top bit is set
367 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
369 crc
= crc
<< 1; # Just shift left
370 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
373 # initial_value can be 0xFFFF or 0x0000
374 def crc16_ibm(msg
, initial_value
=0xFFFF):
375 return crc16_ansi(msg
, initial_value
);
377 # initial_value is 0xFFFF
379 return crc16_ansi(msg
, 0xFFFF);
381 # initial_value can be 0xFFFF, 0x1D0F or 0x0000
382 def crc16_ccitt(msg
, initial_value
=0xFFFF):
383 # CRC-16-CCITT polynomial
384 poly
= 0x1021; # Polynomial for CRC-16-CCITT
385 # Use the specified initial value
388 crc ^
= b
<< 8; # XOR byte into CRC top byte
389 for _
in range(8): # Process each bit
390 if crc
& 0x8000: # If the top bit is set
391 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
393 crc
= crc
<< 1; # Just shift left
394 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
397 # initial_value can be 0x42F0E1EBA9EA3693 or 0x0000000000000000
398 def crc64_ecma(msg
, initial_value
=0x0000000000000000):
399 # CRC-64-ECMA polynomial and initial value
400 poly
= 0x42F0E1EBA9EA3693;
401 crc
= initial_value
; # Initial value for CRC-64-ECMA
403 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
404 for _
in range(8): # Process each bit
405 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
406 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
408 crc
<<= 1; # Just shift left if the MSB is 0
409 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
412 # initial_value can be 0x000000000000001B or 0xFFFFFFFFFFFFFFFF
413 def crc64_iso(msg
, initial_value
=0xFFFFFFFFFFFFFFFF):
414 # CRC-64-ISO polynomial and initial value
415 poly
= 0x000000000000001B;
416 crc
= initial_value
; # Common initial value for CRC-64-ISO
418 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
419 for _
in range(8): # Process each bit
420 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
421 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
423 crc
<<= 1; # Just shift left if the MSB is 0
424 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
427 def GetDataFromArray(data
, path
, default
=None):
431 element
= element
[key
];
433 except (KeyError, TypeError, IndexError):
436 def GetDataFromArrayAlt(structure
, path
, default
=None):
439 if isinstance(element
, dict) and key
in element
:
440 element
= element
[key
];
441 elif isinstance(element
, list) and isinstance(key
, int) and -len(element
) <= key
< len(element
):
442 element
= element
[key
];
447 def ReadTillNullByte(fp
, delimiter
=__file_format_delimiter__
):
450 nullbyte
= delimiter
.encode("UTF-8");
452 curbyte
= fp
.read(1);
453 if(curbyte
==nullbyte
or not curbyte
):
455 curfullbyte
= curfullbyte
+ curbyte
;
456 return curfullbyte
.decode('UTF-8');
458 def ReadUntilNullByte(fp
, delimiter
=__file_format_delimiter__
):
459 return ReadTillNullByte(fp
, delimiter
);
461 def SeekToEndOfFile(fp
):
465 if(lasttell
==fp
.tell()):
467 lasttell
= fp
.tell();
470 def ReadFileHeaderData(fp
, rounds
=0, delimiter
=__file_format_delimiter__
):
474 while(rocount
<roend
):
475 HeaderOut
.append(ReadTillNullByte(fp
, delimiter
));
476 rocount
= rocount
+ 1;
479 def ReadFileHeaderDataBySize(fp
, delimiter
=__file_format_delimiter__
):
480 headerpresize
= ReadTillNullByte(fp
, delimiter
);
481 headersize
= int(headerpresize
, 16);
482 headercontent
= str(fp
.read(headersize
).decode('UTF-8')).split(delimiter
);
485 roend
= int(len(headercontent
));
486 HeaderOut
= [headerpresize
];
487 while(rocount
<roend
):
488 HeaderOut
.append(headercontent
[rocount
]);
489 rocount
= rocount
+ 1;
492 def ReadFileHeaderDataByList(fp
, listval
=[], delimiter
=__file_format_delimiter__
):
494 roend
= int(len(listval
));
496 while(rocount
<roend
):
497 RoundArray
= {listval
[rocount
]: ReadTillNullByte(fp
, delimiter
)};
498 HeaderOut
.update(RoundArray
);
499 rocount
= rocount
+ 1;
502 def ReadFileHeaderDataByListSize(fp
, listval
=[], delimiter
=__file_format_delimiter__
):
503 headerpresize
= ReadTillNullByte(fp
, delimiter
);
504 headersize
= int(headerpresize
, 16);
505 headercontent
= str(fp
.read(headersize
).decode('UTF-8')).split(delimiter
);
509 roend
= int(len(headercontent
));
510 HeaderOut
= {listval
[0]: headerpresize
};
511 while(rocount
<roend
):
512 RoundArray
= {listval
[rocount
]: headercontent
[rocount
]};
513 HeaderOut
.update(RoundArray
);
514 rocount
= rocount
+ 1;
515 listcount
= listcount
+ 1;
518 def AppendNullByte(indata
, delimiter
=__file_format_delimiter__
):
519 outdata
= str(indata
) + delimiter
;
522 def AppendNullBytes(indata
=[], delimiter
=__file_format_delimiter__
):
527 outdata
= outdata
+ AppendNullByte(indata
[inum
], delimiter
);
531 def ReadTillNullByteAlt(fp
, delimiter
=__file_format_delimiter__
):
532 """Read bytes from file pointer until a null byte is encountered."""
533 bytes_list
= [] # Use list for efficient append operation.
535 cur_byte
= fp
.read(1);
536 if cur_byte
== delimiter
.encode() or not cur_byte
:
538 bytes_list
.append(cur_byte
);
539 return b
''.join(bytes_list
).decode('UTF-8');
541 def ReadUntilNullByteAlt(fp
, delimiter
=__file_format_delimiter__
):
542 return ReadTillNullByteAlt(fp
, delimiter
);
544 def ReadFileHeaderDataAlt(fp
, rounds
=0, delimiter
=__file_format_delimiter__
):
545 """Read multiple null-byte terminated strings from a file."""
547 for round_count
in range(rounds
):
548 header_out
[round_count
] = ReadTillNullByteAlt(fp
, delimiter
);
551 def ReadFileHeaderDataBySizeAlt(fp
, delimiter
=__file_format_delimiter__
):
552 # Read and convert header size from hexadecimal to integer
553 header_pre_size
= ReadTillNullByte(fp
, delimiter
);
554 header_size
= int(header_pre_size
, 16);
555 # Read and split the header content
556 header_content
= str(fp
.read(header_size
).decode('UTF-8')).split(delimiter
);
558 # Prepend the pre-size and return the combined list
559 return [header_pre_size
] + header_content
;
561 def ReadFileHeaderDataByListSizeAlt(fp
, listval
=[], delimiter
=__file_format_delimiter__
):
562 # Read the size and content from the header
563 header_pre_size
= ReadTillNullByte(fp
, delimiter
);
564 header_size
= int(header_pre_size
, 16);
565 header_content
= str(fp
.read(header_size
).decode('UTF-8')).split(delimiter
);
567 # Initialize HeaderOut with the header pre-size if listval is not empty
568 HeaderOut
= {listval
[0]: header_pre_size
} if listval
else {};
569 # Map the remaining listval items to their corresponding header content, starting from the second item
570 for i
in range(1, min(len(header_content
) + 1, len(listval
))):
571 HeaderOut
[listval
[i
]] = header_content
[i
- 1]; # -1 because header_content is 0-indexed
574 def ReadFileHeaderDataByListAlt(fp
, listval
=[], delimiter
=__file_format_delimiter__
):
575 """Read multiple null-byte terminated strings from a file."""
577 for round_count
in listval
:
578 header_out
.append(ReadTillNullByteAlt(fp
, delimiter
));
581 def AppendNullByteAlt(indata
, delimiter
=__file_format_delimiter__
):
582 """Append a null byte to the given data."""
583 return str(indata
) + delimiter
;
585 def AppendNullBytesAlt(indata
=[], delimiter
=__file_format_delimiter__
):
586 """Append a null byte to each element in the list and concatenate."""
587 return delimiter
.join(map(str, indata
)) + delimiter
; # Efficient concatenation with null byte.
589 def PrintPermissionString(fchmode
, ftype
):
590 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' } };
592 for fmodval
in str(oct(fchmode
))[-3:]:
593 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
594 if(ftype
==0 or ftype
==7):
595 permissionstr
= "-" + permissionstr
;
597 permissionstr
= "h" + permissionstr
;
599 permissionstr
= "l" + permissionstr
;
601 permissionstr
= "c" + permissionstr
;
603 permissionstr
= "b" + permissionstr
;
605 permissionstr
= "d" + permissionstr
;
607 permissionstr
= "f" + permissionstr
;
609 permissionstr
= "D" + permissionstr
;
611 permissionstr
= "p" + permissionstr
;
613 permissionstr
= "w" + permissionstr
;
615 permissionoutstr
= stat
.filemode(fchmode
);
616 except AttributeError:
617 permissionoutstr
= permissionstr
;
619 permissionoutstr
= permissionstr
;
620 return permissionoutstr
;
622 def PrintPermissionStringAlt(fchmode
, ftype
):
624 '0': '---', '1': '--x', '2': '-w-', '3': '-wx',
625 '4': 'r--', '5': 'r-x', '6': 'rw-', '7': 'rwx'
627 # Translate file mode into permission string
628 permissionstr
= ''.join([permissions
[i
] for i
in str(oct(fchmode
))[-3:]]);
629 # Append file type indicator
631 0: '-', 1: 'h', 2: 'l', 3: 'c', 4: 'b',
632 5: 'd', 6: 'f', 8: 'D', 9: 'p', 10: 'w'
634 file_type
= type_indicators
.get(ftype
, '-');
635 permissionstr
= file_type
+ permissionstr
;
637 permissionoutstr
= stat
.filemode(fchmode
);
638 except AttributeError:
639 permissionoutstr
= permissionstr
;
640 return permissionoutstr
;
642 def CompressionSupport():
643 compression_list
= [];
646 compression_list
.append("gz");
647 compression_list
.append("gzip");
652 compression_list
.append("bz2");
653 compression_list
.append("bzip2");
658 compression_list
.append("lz4");
663 compression_list
.append("lzo");
664 compression_list
.append("lzop");
669 compression_list
.append("zstd");
670 compression_list
.append("zstandard");
675 compression_list
.append("lzma");
676 compression_list
.append("xz");
679 return compression_list
;
681 def CheckCompressionType(infile
, formatspecs
=__file_format_list__
, closefp
=True):
682 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
686 catfp
= open(infile
, "rb");
687 except FileNotFoundError
:
690 prefp
= catfp
.read(2);
692 if(prefp
==binascii
.unhexlify("1f8b")):
695 prefp
= catfp
.read(3);
696 if(prefp
==binascii
.unhexlify("425a68")):
698 if(prefp
==binascii
.unhexlify("5d0000")):
701 prefp
= catfp
.read(4);
702 if(prefp
==binascii
.unhexlify("28b52ffd")):
704 if(prefp
==binascii
.unhexlify("04224d18")):
706 if(prefp
==binascii
.unhexlify("504B0304")):
707 filetype
= "zipfile";
709 prefp
= catfp
.read(5);
710 if(prefp
==binascii
.unhexlify("7573746172")):
711 filetype
= "tarfile";
713 prefp
= catfp
.read(6);
714 if(prefp
==binascii
.unhexlify("fd377a585a00")):
717 prefp
= catfp
.read(7);
718 if(prefp
==binascii
.unhexlify("526172211a0700")):
719 filetype
= "rarfile";
720 if(prefp
==binascii
.unhexlify("43617446696c65")):
721 filetype
= "catfile";
723 prefp
= catfp
.read(8);
724 if(prefp
==binascii
.unhexlify("526172211a070100")):
725 filetype
= "rarfile";
727 prefp
= catfp
.read(formatspecs
[3]);
728 if(prefp
==binascii
.unhexlify(formatspecs
[4])):
729 filetype
= formatspecs
[2];
731 prefp
= catfp
.read(9);
732 if(prefp
==binascii
.unhexlify("894c5a4f000d0a1a0a")):
735 prefp
= catfp
.read(10);
736 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
737 filetype
= "tarfile";
743 def CheckCompressionTypeFromString(instring
, formatspecs
=__file_format_list__
, closefp
=True):
745 instringsfile
= BytesIO(instring
);
747 instringsfile
= BytesIO(instring
.encode("UTF-8"));
748 return CheckCompressionType(instringsfile
, formatspecs
, closefp
);
750 def GetCompressionMimeType(infile
, formatspecs
=__file_format_list__
):
751 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
752 if(compresscheck
=="gzip" or compresscheck
=="gz"):
753 return archivefile_gzip_mimetype
;
754 if(compresscheck
=="bzip2" or compresscheck
=="bz2"):
755 return archivefile_bzip2_mimetype
;
756 if(compresscheck
=="zstd" or compresscheck
=="zstandard"):
757 return archivefile_zstandard_mimetype
;
758 if(compresscheck
=="lz4"):
759 return archivefile_lz4_mimetype
;
760 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
761 return archivefile_lzop_mimetype
;
762 if(compresscheck
=="lzma"):
763 return archivefile_lzma_mimetype
;
764 if(compresscheck
=="xz"):
765 return archivefile_xz_mimetype
;
766 if(compresscheck
=="catfile" or compresscheck
=="cat" or compresscheck
==formatspecs
[2]):
767 return archivefile_cat_mimetype
;
768 if(not compresscheck
):
772 def UncompressArchiveFile(fp
, formatspecs
=__file_format_list__
):
773 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
775 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
776 if(compresscheck
=="gzip"):
781 catfp
= gzip
.GzipFile(fileobj
=fp
, mode
="rb");
782 if(compresscheck
=="bzip2"):
788 catfp
.write(bz2
.decompress(fp
.read()));
789 if(compresscheck
=="zstd"):
795 catfp
.write(zstandard
.decompress(fp
.read()));
796 if(compresscheck
=="lz4"):
802 catfp
.write(lz4
.frame
.decompress(fp
.read()));
803 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
809 catfp
.write(lzo
.decompress(fp
.read()));
810 if(compresscheck
=="lzma" or compresscheck
=="xz"):
816 catfp
.write(lzma
.decompress(fp
.read()));
817 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[2]):
819 if(not compresscheck
):
825 with fp
as fpcontent
:
827 catfp
.write(lzma
.decompress(fp
.read()));
828 except lzma
.LZMAError
:
832 create_alias_function("Uncompress", __file_format_name__
, "", UncompressArchiveFile
);
834 def UncompressFile(infile
, formatspecs
=__file_format_list__
, mode
="rb"):
835 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
836 if(sys
.version_info
[0]==2 and compresscheck
):
842 if(compresscheck
=="gzip"):
848 filefp
= gzip
.open(infile
, mode
, encoding
="UTF-8");
849 except (ValueError, TypeError) as e
:
850 filefp
= gzip
.open(infile
, mode
);
851 if(compresscheck
=="bzip2"):
857 filefp
= bz2
.open(infile
, mode
, encoding
="UTF-8");
858 except (ValueError, TypeError) as e
:
859 filefp
= bz2
.open(infile
, mode
);
860 if(compresscheck
=="zstd"):
866 filefp
= zstandard
.open(infile
, mode
, encoding
="UTF-8");
867 except (ValueError, TypeError) as e
:
868 filefp
= zstandard
.open(infile
, mode
);
869 if(compresscheck
=="lz4"):
875 filefp
= lz4
.frame
.open(infile
, mode
, encoding
="UTF-8");
876 except (ValueError, TypeError) as e
:
877 filefp
= lz4
.frame
.open(infile
, mode
);
878 if(compresscheck
=="lzo"):
884 filefp
= lzo
.open(infile
, mode
, encoding
="UTF-8");
885 except (ValueError, TypeError) as e
:
886 filefp
= lzo
.open(infile
, mode
);
887 if(compresscheck
=="lzma"):
893 filefp
= lzma
.open(infile
, mode
, encoding
="UTF-8");
894 except (ValueError, TypeError) as e
:
895 filefp
= lzma
.open(infile
, mode
);
896 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[2]):
898 filefp
= open(infile
, mode
, encoding
="UTF-8");
899 except (ValueError, TypeError) as e
:
900 filefp
= open(infile
, mode
);
901 if(not compresscheck
):
903 filefp
= open(infile
, mode
, encoding
="UTF-8");
904 except (ValueError, TypeError) as e
:
905 filefp
= open(infile
, mode
);
906 except FileNotFoundError
:
910 def UncompressString(infile
):
911 compresscheck
= CheckCompressionTypeFromString(infile
, formatspecs
, False);
912 if(compresscheck
=="gzip"):
917 fileuz
= gzip
.decompress(infile
);
918 if(compresscheck
=="bzip2"):
923 fileuz
= bz2
.decompress(infile
);
924 if(compresscheck
=="zstd"):
929 fileuz
= zstandard
.decompress(infile
);
930 if(compresscheck
=="lz4"):
935 fileuz
= lz4
.frame
.decompress(infile
);
936 if(compresscheck
=="lzo"):
941 fileuz
= lzo
.decompress(infile
);
942 if(compresscheck
=="lzma"):
947 fileuz
= lzma
.decompress(infile
);
948 if(not compresscheck
):
950 if(hasattr(fileuz
, 'decode')):
951 fileuz
= fileuz
.decode("UTF-8");
954 def UncompressStringAlt(infile
):
956 outstring
= UncompressString(infile
);
957 filefp
.write(outstring
);
961 def CheckCompressionSubType(infile
, formatspecs
=__file_format_list__
):
962 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
963 if(not compresscheck
):
964 fextname
= os
.path
.splitext(infile
)[1];
966 compresscheck
= "gzip";
967 if(fextname
==".bz2"):
968 compresscheck
= "bzip2";
969 if(fextname
==".zst"):
970 compresscheck
= "zstd";
971 if(fextname
==".lz4"):
972 compresscheck
= "lz4";
973 if(fextname
==".lzo" or fextname
==".lzop"):
974 compresscheck
= "lzo";
975 if(fextname
==".lzma" or fextname
==".xz"):
976 compresscheck
= "lzma";
977 if(not compresscheck
):
979 if(compresscheck
=="catfile"):
981 if(compresscheck
==formatspecs
[2]):
982 return formatspecs
[2];
983 if(compresscheck
=="tarfile"):
985 if(compresscheck
=="zipfile"):
987 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
988 catfp
= UncompressArchiveFile(infile
, formatspecs
[2]);
991 if(compresscheck
=="gzip"):
996 catfp
= gzip
.GzipFile(infile
, "rb");
997 if(compresscheck
=="bzip2"):
1002 catfp
= bz2
.BZ2File(infile
, "rb");
1003 if(compresscheck
=="lz4"):
1008 catfp
= lz4
.frame
.open(infile
, "rb");
1009 if(compresscheck
=="zstd"):
1014 catfp
= zstandard
.open(infile
, "rb");
1015 if(compresscheck
=="lzma" or compresscheck
=="xz"):
1020 catfp
= lzma
.open(infile
, "rb");
1021 except FileNotFoundError
:
1024 prefp
= catfp
.read(5);
1025 if(prefp
==binascii
.unhexlify("7573746172")):
1026 filetype
= "tarfile";
1028 prefp
= catfp
.read(7);
1029 if(prefp
==binascii
.unhexlify("43617446696c65")):
1030 filetype
= "catfile";
1032 prefp
= catfp
.read(formatspecs
[3]);
1033 if(prefp
==binascii
.unhexlify(formatspecs
[4])):
1034 filetype
= formatspecs
[2];
1036 prefp
= catfp
.read(10);
1037 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
1038 filetype
= "tarfile";
1043 def GZipCompress(data
, compresslevel
=9):
1048 tmpfp
= tempfile
.NamedTemporaryFile("wb", delete
=False);
1050 tmpfp
= gzip
.GzipFile(tmpfp
.name
, mode
="wb", compresslevel
=compresslevel
);
1054 catfp
= open(tmpfp
.name
, "rb");
1055 except FileNotFoundError
:
1057 catdata
= catfp
.read();
1061 def CompressArchiveFile(fp
, compression
="auto", compressionlevel
=None, formatspecs
=__file_format_list__
):
1062 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1063 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
1066 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
1068 if(compression
not in compressionlist
and compression
is None):
1069 compression
= "auto";
1070 if(compression
=="gzip"):
1076 if(compressionlevel
is None):
1077 compressionlevel
= 9;
1079 compressionlevel
= int(compressionlevel
);
1080 catfp
.write(gzip
.compress(fp
.read(), compresslevel
=compressionlevel
));
1081 if(compression
=="bzip2"):
1087 if(compressionlevel
is None):
1088 compressionlevel
= 9;
1090 compressionlevel
= int(compressionlevel
);
1091 catfp
.write(bz2
.compress(fp
.read(), compresslevel
=compressionlevel
));
1092 if(compression
=="lz4"):
1098 if(compressionlevel
is None):
1099 compressionlevel
= 9;
1101 compressionlevel
= int(compressionlevel
);
1102 catfp
.write(lz4
.frame
.compress(fp
.read(), compression_level
=compressionlevel
));
1103 if(compression
=="lzo" or compression
=="lzop"):
1109 if(compressionlevel
is None):
1110 compressionlevel
= 9;
1112 compressionlevel
= int(compressionlevel
);
1113 catfp
.write(lzo
.compress(fp
.read(), compresslevel
=compressionlevel
));
1114 if(compression
=="zstd"):
1120 if(compressionlevel
is None):
1121 compressionlevel
= 10;
1123 compressionlevel
= int(compressionlevel
);
1124 catfp
.write(zstandard
.compress(fp
.read(), level
=compressionlevel
));
1125 if(compression
=="lzma"):
1131 if(compressionlevel
is None):
1132 compressionlevel
= 9;
1134 compressionlevel
= int(compressionlevel
);
1135 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]));
1136 if(compression
=="xz"):
1142 if(compressionlevel
is None):
1143 compressionlevel
= 9;
1145 compressionlevel
= int(compressionlevel
);
1146 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]));
1147 if(compression
=="auto" or compression
is None):
1152 create_alias_function("Compress", __file_format_name__
, "", CompressArchiveFile
);
1154 def CompressOpenFile(outfile
, compressionlevel
=None):
1155 if(outfile
is None):
1157 fbasename
= os
.path
.splitext(outfile
)[0];
1158 fextname
= os
.path
.splitext(outfile
)[1];
1159 if(compressionlevel
is None and fextname
!=".zst"):
1160 compressionlevel
= 9;
1161 elif(compressionlevel
is None and fextname
==".zst"):
1162 compressionlevel
= 10;
1164 compressionlevel
= int(compressionlevel
);
1165 if(sys
.version_info
[0]==2):
1170 if(fextname
not in outextlistwd
):
1172 outfp
= open(outfile
, "wb", encoding
="UTF-8");
1173 except (ValueError, TypeError) as e
:
1174 outfp
= open(outfile
, "wb");
1175 elif(fextname
==".gz"):
1181 outfp
= gzip
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
1182 except (ValueError, TypeError) as e
:
1183 outfp
= gzip
.open(outfile
, mode
, compressionlevel
);
1184 elif(fextname
==".bz2"):
1190 outfp
= bz2
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
1191 except (ValueError, TypeError) as e
:
1192 outfp
= bz2
.open(outfile
, mode
, compressionlevel
);
1193 elif(fextname
==".zst"):
1199 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
), encoding
="UTF-8");
1200 except (ValueError, TypeError) as e
:
1201 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
));
1202 elif(fextname
==".xz"):
1208 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}], encoding
="UTF-8");
1209 except (ValueError, TypeError) as e
:
1210 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]);
1211 elif(fextname
==".lz4"):
1217 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
, encoding
="UTF-8");
1218 except (ValueError, TypeError) as e
:
1219 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
);
1220 elif(fextname
==".lzo"):
1226 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
, encoding
="UTF-8");
1227 except (ValueError, TypeError) as e
:
1228 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
);
1229 elif(fextname
==".lzma"):
1235 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}], encoding
="UTF-8");
1236 except (ValueError, TypeError) as e
:
1237 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]);
1238 except FileNotFoundError
:
1242 def GetDevMajorMinor(fdev
):
1244 if(hasattr(os
, "minor")):
1245 retdev
.append(os
.minor(fdev
));
1248 if(hasattr(os
, "major")):
1249 retdev
.append(os
.major(fdev
));
1254 def CheckSumSupport(checkfor
, guaranteed
=True):
1256 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
1258 hash_list
= sorted(list(hashlib
.algorithms_available
));
1259 checklistout
= sorted(hash_list
+ ['adler32', 'crc16', 'crc16_ansi', 'crc16_ibm', 'crc16_ccitt', 'crc32', 'crc64', 'crc64_ecma', 'crc64_iso', 'none']);
1260 if(checkfor
in checklistout
):
1265 def CheckSumSupportAlt(checkfor
, guaranteed
=True):
1267 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
1269 hash_list
= sorted(list(hashlib
.algorithms_available
));
1270 checklistout
= hash_list
;
1271 if(checkfor
in checklistout
):
1276 def PackArchiveFile(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1277 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1278 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1279 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1280 advancedlist
= True;
1281 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1282 outfile
= RemoveWindowsPath(outfile
);
1283 checksumtype
= checksumtype
.lower();
1284 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
1285 checksumtype
="crc32";
1286 if(checksumtype
=="none"):
1288 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
1290 if(compression
not in compressionlist
and compression
is None):
1291 compression
= "auto";
1293 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1294 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1295 if(os
.path
.exists(outfile
)):
1300 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1302 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
1304 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
1307 fbasename
= os
.path
.splitext(outfile
)[0];
1308 fextname
= os
.path
.splitext(outfile
)[1];
1309 catfp
= CompressOpenFile(outfile
, compressionlevel
);
1310 catver
= formatspecs
[6];
1311 fileheaderver
= str(int(catver
.replace(".", "")));
1312 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
1313 catfp
.write(fileheader
.encode('UTF-8'));
1316 for line
in sys
.stdin
:
1317 infilelist
.append(line
.strip());
1318 infilelist
= list(filter(None, infilelist
));
1319 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
1320 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
1322 with
open(infiles
, "r") as finfile
:
1323 for line
in finfile
:
1324 infilelist
.append(line
.strip());
1325 infilelist
= list(filter(None, infilelist
));
1327 if(isinstance(infiles
, (list, tuple, ))):
1328 infilelist
= list(filter(None, infiles
));
1329 elif(isinstance(infiles
, (str, ))):
1330 infilelist
= list(filter(None, [infiles
]));
1332 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
1334 GetDirList
= ListDir(infilelist
, followlink
, False);
1342 inodetocatinode
= {};
1343 fnumfiles
= format(int(len(GetDirList
)), 'x').lower();
1344 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
1345 if(checksumtype
=="none" or checksumtype
==""):
1346 catfileheadercshex
= format(0, 'x').lower();
1347 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1348 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1349 elif(checksumtype
=="crc16_ccitt"):
1350 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1351 elif(checksumtype
=="adler32"):
1352 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1353 elif(checksumtype
=="crc32"):
1354 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1355 elif(checksumtype
=="crc64_ecma"):
1356 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1357 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1358 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1359 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1360 checksumoutstr
= hashlib
.new(checksumtype
);
1361 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
1362 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1364 catfileheadercshex
= format(0, 'x').lower
1365 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
1366 catfp
.write(fnumfilesa
.encode('UTF-8'));
1369 os
.fsync(catfp
.fileno());
1370 except io
.UnsupportedOperation
:
1372 except AttributeError:
1374 for curfname
in GetDirList
:
1375 catfhstart
= catfp
.tell();
1376 if(re
.findall("^[.|/]", curfname
)):
1379 fname
= "./"+curfname
;
1381 VerbosePrintOut(fname
);
1382 if(not followlink
or followlink
is None):
1383 fstatinfo
= os
.lstat(fname
);
1385 fstatinfo
= os
.stat(fname
);
1386 fpremode
= fstatinfo
.st_mode
;
1387 finode
= fstatinfo
.st_ino
;
1388 flinkcount
= fstatinfo
.st_nlink
;
1390 if(stat
.S_ISREG(fpremode
)):
1392 elif(stat
.S_ISLNK(fpremode
)):
1394 elif(stat
.S_ISCHR(fpremode
)):
1396 elif(stat
.S_ISBLK(fpremode
)):
1398 elif(stat
.S_ISDIR(fpremode
)):
1400 elif(stat
.S_ISFIFO(fpremode
)):
1402 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
1404 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
1406 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
1411 fcurfid
= format(int(curfid
), 'x').lower();
1412 if(not followlink
and finode
!=0):
1414 if(finode
in inodelist
):
1416 flinkname
= inodetofile
[finode
];
1417 fcurinode
= format(int(inodetocatinode
[finode
]), 'x').lower();
1418 if(finode
not in inodelist
):
1419 inodelist
.append(finode
);
1420 inodetofile
.update({finode
: fname
});
1421 inodetocatinode
.update({finode
: curinode
});
1422 fcurinode
= format(int(curinode
), 'x').lower();
1423 curinode
= curinode
+ 1;
1425 fcurinode
= format(int(curinode
), 'x').lower();
1426 curinode
= curinode
+ 1;
1427 curfid
= curfid
+ 1;
1429 flinkname
= os
.readlink(fname
);
1430 fdev
= fstatinfo
.st_dev
;
1431 getfdev
= GetDevMajorMinor(fdev
);
1432 fdev_minor
= getfdev
[0];
1433 fdev_major
= getfdev
[1];
1434 frdev
= fstatinfo
.st_dev
;
1435 if(hasattr(fstatinfo
, "st_rdev")):
1436 frdev
= fstatinfo
.st_rdev
;
1438 frdev
= fstatinfo
.st_dev
;
1439 getfrdev
= GetDevMajorMinor(frdev
);
1440 frdev_minor
= getfrdev
[0];
1441 frdev_major
= getfrdev
[1];
1442 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
1443 fsize
= format(int("0"), 'x').lower();
1444 elif(ftype
==0 or ftype
==7):
1445 fsize
= format(int(fstatinfo
.st_size
), 'x').lower();
1447 fsize
= format(int(fstatinfo
.st_size
)).lower();
1448 fatime
= format(int(fstatinfo
.st_atime
), 'x').lower();
1449 fmtime
= format(int(fstatinfo
.st_mtime
), 'x').lower();
1450 fctime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1451 if(hasattr(fstatinfo
, "st_birthtime")):
1452 fbtime
= format(int(fstatinfo
.st_birthtime
), 'x').lower();
1454 fbtime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1455 fmode
= format(int(fstatinfo
.st_mode
), 'x').lower();
1456 fchmode
= format(int(stat
.S_IMODE(fstatinfo
.st_mode
)), 'x').lower();
1457 ftypemod
= format(int(stat
.S_IFMT(fstatinfo
.st_mode
)), 'x').lower();
1458 fuid
= format(int(fstatinfo
.st_uid
), 'x').lower();
1459 fgid
= format(int(fstatinfo
.st_gid
), 'x').lower();
1464 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
1465 funame
= userinfo
.pw_name
;
1474 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
1475 fgname
= groupinfo
.gr_name
;
1480 fdev_minor
= format(int(fdev_minor
), 'x').lower();
1481 fdev_major
= format(int(fdev_major
), 'x').lower();
1482 frdev_minor
= format(int(frdev_minor
), 'x').lower();
1483 frdev_major
= format(int(frdev_major
), 'x').lower();
1484 finode
= format(int(finode
), 'x').lower();
1485 flinkcount
= format(int(flinkcount
), 'x').lower();
1486 if(hasattr(fstatinfo
, "st_file_attributes")):
1487 fwinattributes
= format(int(fstatinfo
.st_file_attributes
), 'x').lower();
1489 fwinattributes
= format(int(0), 'x').lower();
1490 fcontents
= "".encode('UTF-8');
1492 if(ftype
== 0 or ftype
== 7):
1493 with
open(fname
, "rb") as fpc
:
1495 chunk
= fpc
.read(chunk_size
);
1499 if(followlink
and (ftype
== 1 or ftype
== 2)):
1500 flstatinfo
= os
.stat(flinkname
);
1501 with
open(flinkname
, "rb") as fpc
:
1503 chunk
= fpc
.read(chunk_size
);
1507 ftypehex
= format(ftype
, 'x').lower();
1508 extrafields
= format(len(extradata
), 'x').lower();
1509 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
1510 if(len(extradata
)>0):
1511 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
1512 extrasizelen
= format(len(extrasizestr
), 'x').lower();
1513 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]);
1514 if(len(extradata
)>0):
1515 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
1516 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
1517 if(checksumtype
=="none" or checksumtype
==""):
1518 catfileheadercshex
= format(0, 'x').lower();
1519 catfilecontentcshex
= format(0, 'x').lower();
1520 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1521 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
1522 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
1523 elif(checksumtype
=="crc16_ccitt"):
1524 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
1525 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
1526 elif(checksumtype
=="adler32"):
1527 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1528 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
1529 elif(checksumtype
=="crc32"):
1530 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1531 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
1532 elif(checksumtype
=="crc64_ecma"):
1533 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1534 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
1535 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1536 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1537 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
1538 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1539 checksumoutstr
= hashlib
.new(checksumtype
);
1540 checksumoutstr
.update("".encode('UTF-8'));
1541 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1542 checksumoutstr
= hashlib
.new(checksumtype
);
1543 checksumoutstr
.update(fcontents
);
1544 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
1546 catfileheadercshex
= format(0, 'x').lower();
1547 catfilecontentcshex
= format(0, 'x').lower();
1548 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1549 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
1550 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
1551 if(checksumtype
=="none" or checksumtype
==""):
1552 catfileheadercshex
= format(0, 'x').lower();
1553 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1554 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
1555 elif(checksumtype
=="crc16_ccitt"):
1556 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
1557 elif(checksumtype
=="adler32"):
1558 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
1559 elif(checksumtype
=="crc32"):
1560 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
1561 elif(checksumtype
=="crc64_ecma"):
1562 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1563 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1564 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1565 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1566 checksumoutstr
= hashlib
.new(checksumtype
);
1567 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
1568 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1570 catfileheadercshex
= format(0, 'x').lower();
1571 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1572 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
1573 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
1574 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
1575 nullstrecd
= formatspecs
[5].encode('UTF-8');
1576 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
1577 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
1578 catfp
.write(catfileout
);
1581 os
.fsync(catfp
.fileno());
1582 except io
.UnsupportedOperation
:
1584 except AttributeError:
1586 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1587 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1590 os
.fsync(catfp
.fileno());
1591 except io
.UnsupportedOperation
:
1593 except AttributeError:
1597 if(hasattr(sys
.stdout
, "buffer")):
1598 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1600 shutil
.copyfileobj(catfp
, sys
.stdout
);
1601 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
1602 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1604 upload_file_to_ftp_file(catfp
, outfile
);
1605 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
1606 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1609 upload_file_to_pysftp_file(catfp
, outfile
);
1611 upload_file_to_sftp_file(catfp
, outfile
);
1619 create_alias_function("Pack", __file_format_name__
, "", PackArchiveFile
);
1621 if(hasattr(shutil
, "register_archive_format")):
1622 def PackArchiveFileFunc(archive_name
, source_dir
, **kwargs
):
1623 return PackArchiveFile(source_dir
, archive_name
, False, "auto", None, False, "crc32", [], __file_format_delimiter__
, False, False);
1624 create_alias_function("Pack", __file_format_name__
, "Func", PackArchiveFileFunc
);
1626 def PackArchiveFileFromDirList(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1627 return PackArchiveFile(infiles
, outfile
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
1629 def PackArchiveFileFromTarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1630 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1631 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1632 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1633 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1634 outfile
= RemoveWindowsPath(outfile
);
1635 checksumtype
= checksumtype
.lower();
1636 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
1637 checksumtype
="crc32";
1638 if(checksumtype
=="none"):
1640 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
1642 if(compression
not in compressionlist
and compression
is None):
1643 compression
= "auto";
1645 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1646 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1647 if(os
.path
.exists(outfile
)):
1652 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1654 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
1656 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
1659 fbasename
= os
.path
.splitext(outfile
)[0];
1660 fextname
= os
.path
.splitext(outfile
)[1];
1661 catfp
= CompressOpenFile(outfile
, compressionlevel
);
1662 catver
= formatspecs
[6];
1663 fileheaderver
= str(int(catver
.replace(".", "")));
1664 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
1665 catfp
.write(fileheader
.encode('UTF-8'));
1671 inodetocatinode
= {};
1674 if(hasattr(sys
.stdin
, "buffer")):
1675 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
1677 shutil
.copyfileobj(sys
.stdin
, infile
);
1682 elif(re
.findall(r
"^(http|https)\:\/\/", infile
)):
1683 infile
= download_file_from_http_file(infile
);
1688 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", infile
)):
1689 infile
= download_file_from_ftp_file(infile
);
1694 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
1696 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
1698 if(not tarfile
.is_tarfile(infile
)):
1700 except AttributeError:
1701 if(not is_tarfile(infile
)):
1706 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
1707 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
1709 tarfp
= tarfile
.open(infile
, "r");
1710 except FileNotFoundError
:
1712 fnumfiles
= format(int(len(tarfp
.getmembers())), 'x').lower();
1713 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
1714 if(checksumtype
=="none" or checksumtype
==""):
1715 catfileheadercshex
= format(0, 'x').lower();
1716 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1717 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1718 elif(checksumtype
=="crc16_ccitt"):
1719 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1720 elif(checksumtype
=="adler32"):
1721 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1722 elif(checksumtype
=="crc32"):
1723 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1724 elif(checksumtype
=="crc64_ecma"):
1725 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1726 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1727 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1728 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1729 checksumoutstr
= hashlib
.new(checksumtype
);
1730 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
1731 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1733 catfileheadercshex
= format(0, 'x').lower();
1734 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
1735 catfp
.write(fnumfilesa
.encode('UTF-8'));
1738 os
.fsync(catfp
.fileno());
1739 except io
.UnsupportedOperation
:
1741 except AttributeError:
1743 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
1744 catfhstart
= catfp
.tell();
1745 if(re
.findall("^[.|/]", member
.name
)):
1746 fname
= member
.name
;
1748 fname
= "./"+member
.name
;
1750 VerbosePrintOut(fname
);
1751 fpremode
= member
.mode
;
1752 ffullmode
= member
.mode
;
1756 ffullmode
= member
.mode
+ stat
.S_IFREG
;
1758 elif(member
.isdev()):
1759 ffullmode
= member
.mode
;
1761 elif(member
.islnk()):
1762 ffullmode
= member
.mode
+ stat
.S_IFREG
;
1764 elif(member
.issym()):
1765 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
1767 elif(member
.ischr()):
1768 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
1770 elif(member
.isblk()):
1771 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
1773 elif(member
.isdir()):
1774 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
1776 elif(member
.isfifo()):
1777 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
1779 elif(member
.issparse()):
1780 ffullmode
= member
.mode
;
1783 ffullmode
= member
.mode
;
1786 fcurfid
= format(int(curfid
), 'x').lower();
1787 fcurinode
= format(int(0), 'x').lower();
1788 curfid
= curfid
+ 1;
1790 flinkname
= member
.linkname
;
1791 fdev_minor
= format(int(member
.devminor
), 'x').lower();
1792 fdev_major
= format(int(member
.devmajor
), 'x').lower();
1793 frdev_minor
= format(int(member
.devminor
), 'x').lower();
1794 frdev_major
= format(int(member
.devmajor
), 'x').lower();
1795 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
1796 fsize
= format(int("0"), 'x').lower();
1797 elif(ftype
==0 or ftype
==7):
1798 fsize
= format(int(member
.size
), 'x').lower();
1800 fsize
= format(int(member
.size
), 'x').lower();
1801 fatime
= format(int(member
.mtime
), 'x').lower();
1802 fmtime
= format(int(member
.mtime
), 'x').lower();
1803 fctime
= format(int(member
.mtime
), 'x').lower();
1804 fbtime
= format(int(member
.mtime
), 'x').lower();
1805 fmode
= format(int(ffullmode
), 'x').lower();
1806 fchmode
= format(int(stat
.S_IMODE(ffullmode
)), 'x').lower();
1807 ftypemod
= format(int(stat
.S_IFMT(ffullmode
)), 'x').lower();
1808 fuid
= format(int(member
.uid
), 'x').lower();
1809 fgid
= format(int(member
.gid
), 'x').lower();
1810 funame
= member
.uname
;
1811 fgname
= member
.gname
;
1812 flinkcount
= format(int(flinkcount
), 'x').lower();
1813 fcontents
= "".encode('UTF-8');
1815 if(ftype
== 0 or ftype
== 7):
1816 with tarfp
.extractfile(member
) as fpc
:
1818 chunk
= fpc
.read(chunk_size
);
1822 ftypehex
= format(ftype
, 'x').lower();
1823 extrafields
= format(len(extradata
), 'x').lower();
1824 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
1825 if(len(extradata
)>0):
1826 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
1827 extrasizelen
= format(len(extrasizestr
), 'x').lower();
1828 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]);
1829 if(len(extradata
)>0):
1830 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
1831 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
1832 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
1833 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
1834 if(checksumtype
=="none" or checksumtype
==""):
1835 catfileheadercshex
= format(0, 'x').lower();
1836 catfilecontentcshex
= format(0, 'x').lower();
1837 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1838 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
1839 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
1840 elif(checksumtype
=="crc16_ccitt"):
1841 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
1842 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
1843 elif(checksumtype
=="adler32"):
1844 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1845 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
1846 elif(checksumtype
=="crc32"):
1847 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1848 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
1849 elif(checksumtype
=="crc64_ecma"):
1850 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1851 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
1852 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1853 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1854 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
1855 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1856 checksumoutstr
= hashlib
.new(checksumtype
);
1857 checksumoutstr
.update("".encode('UTF-8'));
1858 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1859 checksumoutstr
= hashlib
.new(checksumtype
);
1860 checksumoutstr
.update(fcontents
);
1861 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
1863 catfileheadercshex
= format(0, 'x').lower();
1864 catfilecontentcshex
= format(0, 'x').lower();
1865 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1866 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
1867 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
1868 if(checksumtype
=="none" or checksumtype
==""):
1869 catfileheadercshex
= format(0, 'x').lower();
1870 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1871 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
1872 elif(checksumtype
=="crc16_ccitt"):
1873 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
1874 elif(checksumtype
=="adler32"):
1875 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
1876 elif(checksumtype
=="crc32"):
1877 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
1878 elif(checksumtype
=="crc64_ecma"):
1879 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1880 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1881 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1882 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1883 checksumoutstr
= hashlib
.new(checksumtype
);
1884 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
1885 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1887 catfileheadercshex
= format(0, 'x').lower();
1888 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
1889 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
1890 nullstrecd
= formatspecs
[5].encode('UTF-8');
1891 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
1892 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
1893 catfp
.write(catfileout
);
1896 os
.fsync(catfp
.fileno());
1897 except io
.UnsupportedOperation
:
1899 except AttributeError:
1901 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1902 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1905 os
.fsync(catfp
.fileno());
1906 except io
.UnsupportedOperation
:
1908 except AttributeError:
1912 if(hasattr(sys
.stdout
, "buffer")):
1913 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1915 shutil
.copyfileobj(catfp
, sys
.stdout
);
1916 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
1917 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1919 upload_file_to_ftp_file(catfp
, outfile
);
1920 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
1921 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1924 upload_file_to_pysftp_file(catfp
, outfile
);
1926 upload_file_to_sftp_file(catfp
, outfile
);
1934 create_alias_function("Pack", __file_format_name__
, "FromTarFile", PackArchiveFileFromTarFile
);
1936 def PackArchiveFileFromZipFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1937 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1938 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1939 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1940 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1941 outfile
= RemoveWindowsPath(outfile
);
1942 checksumtype
= checksumtype
.lower();
1943 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
1944 checksumtype
="crc32";
1945 if(checksumtype
=="none"):
1947 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
1949 if(compression
not in compressionlist
and compression
is None):
1950 compression
= "auto";
1952 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1953 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1954 if(os
.path
.exists(outfile
)):
1959 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1961 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
1963 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
1966 fbasename
= os
.path
.splitext(outfile
)[0];
1967 fextname
= os
.path
.splitext(outfile
)[1];
1968 catfp
= CompressOpenFile(outfile
, compressionlevel
);
1969 catver
= formatspecs
[6];
1970 fileheaderver
= str(int(catver
.replace(".", "")));
1971 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
1972 catfp
.write(fileheader
.encode('UTF-8'));
1978 inodetocatinode
= {};
1981 if(hasattr(sys
.stdin
, "buffer")):
1982 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
1984 shutil
.copyfileobj(sys
.stdin
, infile
);
1989 elif(re
.findall(r
"^(http|https)\:\/\/", infile
)):
1990 infile
= download_file_from_http_file(infile
);
1995 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", infile
)):
1996 infile
= download_file_from_ftp_file(infile
);
2001 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2005 if(not zipfile
.is_zipfile(infile
)):
2008 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
2009 except FileNotFoundError
:
2011 ziptest
= zipfp
.testzip();
2013 VerbosePrintOut("Bad file found!");
2014 fnumfiles
= format(int(len(zipfp
.infolist())), 'x').lower();
2015 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2016 if(checksumtype
=="none" or checksumtype
==""):
2017 catfileheadercshex
= format(0, 'x').lower();
2018 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2019 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2020 elif(checksumtype
=="crc16_ccitt"):
2021 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2022 elif(checksumtype
=="adler32"):
2023 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2024 elif(checksumtype
=="crc32"):
2025 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2026 elif(checksumtype
=="crc64_ecma"):
2027 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2028 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2029 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2030 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2031 checksumoutstr
= hashlib
.new(checksumtype
);
2032 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
2033 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2035 catfileheadercshex
= format(0, 'x').lower();
2036 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2037 catfp
.write(fnumfilesa
.encode('UTF-8'));
2040 os
.fsync(catfp
.fileno());
2041 except io
.UnsupportedOperation
:
2043 except AttributeError:
2045 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
2046 catfhstart
= catfp
.tell();
2047 if(re
.findall("^[.|/]", member
.filename
)):
2048 fname
= member
.filename
;
2050 fname
= "./"+member
.filename
;
2051 zipinfo
= zipfp
.getinfo(member
.filename
);
2053 VerbosePrintOut(fname
);
2054 if(not member
.is_dir()):
2055 fpremode
= int(stat
.S_IFREG
+ 438);
2056 elif(member
.is_dir()):
2057 fpremode
= int(stat
.S_IFDIR
+ 511);
2060 if(not member
.is_dir()):
2062 elif(member
.is_dir()):
2065 fcurfid
= format(int(curfid
), 'x').lower();
2066 fcurinode
= format(int(0), 'x').lower();
2067 curfid
= curfid
+ 1;
2068 fdev_minor
= format(int(0), 'x').lower();
2069 fdev_major
= format(int(0), 'x').lower();
2070 frdev_minor
= format(int(0), 'x').lower();
2071 frdev_major
= format(int(0), 'x').lower();
2073 fsize
= format(int("0"), 'x').lower();
2075 fsize
= format(int(member
.file_size
), 'x').lower();
2077 fsize
= format(int(member
.file_size
), 'x').lower();
2078 fatime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2079 fmtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2080 fctime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2081 fbtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
2082 if(not member
.is_dir()):
2083 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
2084 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
2085 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
2086 if(member
.is_dir()):
2087 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
2088 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
2089 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
2091 fuid
= format(int(os
.getuid()), 'x').lower();
2092 except AttributeError:
2093 fuid
= format(int(0), 'x').lower();
2095 fuid
= format(int(0), 'x').lower();
2097 fgid
= format(int(os
.getgid()), 'x').lower();
2098 except AttributeError:
2099 fgid
= format(int(0), 'x').lower();
2101 fgid
= format(int(0), 'x').lower();
2105 userinfo
= pwd
.getpwuid(os
.getuid());
2106 funame
= userinfo
.pw_name
;
2109 except AttributeError:
2117 groupinfo
= grp
.getgrgid(os
.getgid());
2118 fgname
= groupinfo
.gr_name
;
2121 except AttributeError:
2125 fcontents
= "".encode('UTF-8');
2127 fcontents
= zipfp
.read(member
.filename
);
2128 ftypehex
= format(ftype
, 'x').lower();
2129 extrafields
= format(len(extradata
), 'x').lower();
2130 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
2131 if(len(extradata
)>0):
2132 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2133 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2134 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]);
2135 if(len(extradata
)>0):
2136 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2137 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
2138 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
2139 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
2140 if(checksumtype
=="none" or checksumtype
==""):
2141 catfileheadercshex
= format(0, 'x').lower();
2142 catfilecontentcshex
= format(0, 'x').lower();
2143 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2144 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2145 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
2146 elif(checksumtype
=="crc16_ccitt"):
2147 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2148 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
2149 elif(checksumtype
=="adler32"):
2150 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2151 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
2152 elif(checksumtype
=="crc32"):
2153 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2154 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
2155 elif(checksumtype
=="crc64_ecma"):
2156 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2157 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
2158 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2159 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2160 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
2161 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2162 checksumoutstr
= hashlib
.new(checksumtype
);
2163 checksumoutstr
.update("".encode('UTF-8'));
2164 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2165 checksumoutstr
= hashlib
.new(checksumtype
);
2166 checksumoutstr
.update(fcontents
);
2167 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
2169 catfileheadercshex
= format(0, 'x').lower();
2170 catfilecontentcshex
= format(0, 'x').lower();
2171 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2172 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2173 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
2174 if(checksumtype
=="none" or checksumtype
==""):
2175 catfileheadercshex
= format(0, 'x').lower();
2176 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2177 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2178 elif(checksumtype
=="crc16_ccitt"):
2179 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2180 elif(checksumtype
=="adler32"):
2181 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2182 elif(checksumtype
=="crc32"):
2183 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2184 elif(checksumtype
=="crc64_ecma"):
2185 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2186 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2187 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2188 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2189 checksumoutstr
= hashlib
.new(checksumtype
);
2190 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
2191 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2193 catfileheadercshex
= format(0, 'x').lower();
2194 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2195 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
2196 nullstrecd
= formatspecs
[5].encode('UTF-8');
2197 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
2198 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
2199 catfp
.write(catfileout
);
2202 os
.fsync(catfp
.fileno());
2203 except io
.UnsupportedOperation
:
2205 except AttributeError:
2207 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2208 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2211 os
.fsync(catfp
.fileno());
2212 except io
.UnsupportedOperation
:
2214 except AttributeError:
2218 if(hasattr(sys
.stdout
, "buffer")):
2219 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2221 shutil
.copyfileobj(catfp
, sys
.stdout
);
2222 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
2223 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2225 upload_file_to_ftp_file(catfp
, outfile
);
2226 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
2227 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2230 upload_file_to_pysftp_file(catfp
, outfile
);
2232 upload_file_to_sftp_file(catfp
, outfile
);
2240 create_alias_function("Pack", __file_format_name__
, "FromZipFile", PackArchiveFileFromZipFile
);
2242 if(not rarfile_support
):
2243 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2246 if(rarfile_support
):
2247 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2248 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2249 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2250 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
2251 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2252 outfile
= RemoveWindowsPath(outfile
);
2253 checksumtype
= checksumtype
.lower();
2254 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2255 checksumtype
="crc32";
2256 if(checksumtype
=="none"):
2258 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
2260 if(compression
not in compressionlist
and compression
is None):
2261 compression
= "auto";
2263 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2264 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2265 if(os
.path
.exists(outfile
)):
2270 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2272 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
2274 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
2277 fbasename
= os
.path
.splitext(outfile
)[0];
2278 fextname
= os
.path
.splitext(outfile
)[1];
2279 catfp
= CompressOpenFile(outfile
, compressionlevel
);
2280 catver
= formatspecs
[6];
2281 fileheaderver
= str(int(catver
.replace(".", "")));
2282 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
2283 catfp
.write(fileheader
.encode('UTF-8'));
2289 inodetocatinode
= {};
2290 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2292 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
2294 rarfp
= rarfile
.RarFile(infile
, "r");
2295 rartest
= rarfp
.testrar();
2297 VerbosePrintOut("Bad file found!");
2298 fnumfiles
= format(int(len(rarfp
.infolist())), 'x').lower();
2299 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[5]);
2300 if(checksumtype
=="none" or checksumtype
==""):
2301 catfileheadercshex
= format(0, 'x').lower();
2302 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2303 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2304 elif(checksumtype
=="crc16_ccitt"):
2305 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2306 elif(checksumtype
=="adler32"):
2307 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2308 elif(checksumtype
=="crc32"):
2309 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2310 elif(checksumtype
=="crc64_ecma"):
2311 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2312 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2313 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2314 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2315 checksumoutstr
= hashlib
.new(checksumtype
);
2316 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
2317 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2319 catfileheadercshex
= format(0, 'x').lower();
2320 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2321 catfp
.write(fnumfilesa
.encode('UTF-8'));
2324 os
.fsync(catfp
.fileno());
2325 except io
.UnsupportedOperation
:
2327 except AttributeError:
2329 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
2332 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
2335 member
.external_attr
2337 except AttributeError:
2339 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
2342 member
.external_attr
2344 except AttributeError:
2349 catfhstart
= catfp
.tell();
2350 if(re
.findall("^[.|/]", member
.filename
)):
2351 fname
= member
.filename
;
2353 fname
= "./"+member
.filename
;
2354 rarinfo
= rarfp
.getinfo(member
.filename
);
2356 VerbosePrintOut(fname
);
2357 if(is_unix
and member
.external_attr
!=0):
2358 fpremode
= int(member
.external_attr
);
2359 elif(member
.is_file()):
2360 fpremode
= int(stat
.S_IFREG
+ 438);
2361 elif(member
.is_symlink()):
2362 fpremode
= int(stat
.S_IFLNK
+ 438);
2363 elif(member
.is_dir()):
2364 fpremode
= int(stat
.S_IFDIR
+ 511);
2365 if(is_windows
and member
.external_attr
!=0):
2366 fwinattributes
= format(int(member
.external_attr
), 'x').lower();
2368 fwinattributes
= format(int(0), 'x').lower();
2371 if(member
.is_file()):
2373 elif(member
.is_symlink()):
2375 elif(member
.is_dir()):
2379 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
2380 fcurfid
= format(int(curfid
), 'x').lower();
2381 fcurinode
= format(int(0), 'x').lower();
2382 curfid
= curfid
+ 1;
2383 fdev_minor
= format(int(0), 'x').lower();
2384 fdev_major
= format(int(0), 'x').lower();
2385 frdev_minor
= format(int(0), 'x').lower();
2386 frdev_major
= format(int(0), 'x').lower();
2388 fsize
= format(int("0"), 'x').lower();
2390 fsize
= format(int(member
.file_size
), 'x').lower();
2392 fsize
= format(int(member
.file_size
), 'x').lower();
2395 fatime
= format(int(member
.atime
.timestamp()), 'x').lower();
2397 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2398 except AttributeError:
2399 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2400 fmtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2403 fctime
= format(int(member
.ctime
.timestamp()), 'x').lower();
2405 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2406 except AttributeError:
2407 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2408 fbtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2409 if(is_unix
and member
.external_attr
!=0):
2410 fmode
= format(int(member
.external_attr
), 'x').lower();
2411 fchmode
= format(int(stat
.S_IMODE(member
.external_attr
)), 'x').lower();
2412 ftypemod
= format(int(stat
.S_IFMT(member
.external_attr
)), 'x').lower();
2413 elif(member
.is_file()):
2414 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
2415 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
2416 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
2417 elif(member
.is_symlink()):
2418 fmode
= format(int(stat
.S_IFLNK
+ 438), 'x').lower();
2419 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
2420 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
2421 elif(member
.is_dir()):
2422 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
2423 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
2424 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
2426 fuid
= format(int(os
.getuid()), 'x').lower();
2427 except AttributeError:
2428 fuid
= format(int(0), 'x').lower();
2430 fuid
= format(int(0), 'x').lower();
2432 fgid
= format(int(os
.getgid()), 'x').lower();
2433 except AttributeError:
2434 fgid
= format(int(0), 'x').lower();
2436 fgid
= format(int(0), 'x').lower();
2440 userinfo
= pwd
.getpwuid(os
.getuid());
2441 funame
= userinfo
.pw_name
;
2444 except AttributeError:
2452 groupinfo
= grp
.getgrgid(os
.getgid());
2453 fgname
= groupinfo
.gr_name
;
2456 except AttributeError:
2460 fcontents
= "".encode('UTF-8');
2462 fcontents
= rarfp
.read(member
.filename
);
2463 ftypehex
= format(ftype
, 'x').lower();
2464 extrafields
= format(len(extradata
), 'x').lower();
2465 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
2466 if(len(extradata
)>0):
2467 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2468 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2469 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]);
2470 if(len(extradata
)>0):
2471 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
2472 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
2473 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
2474 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
2475 if(checksumtype
=="none" or checksumtype
==""):
2476 catfileheadercshex
= format(0, 'x').lower();
2477 catfilecontentcshex
= format(0, 'x').lower();
2478 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2479 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2480 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
2481 elif(checksumtype
=="crc16_ccitt"):
2482 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2483 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
2484 elif(checksumtype
=="adler32"):
2485 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2486 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
2487 elif(checksumtype
=="crc32"):
2488 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2489 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
2490 elif(checksumtype
=="crc64_ecma"):
2491 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2492 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
2493 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2494 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2495 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
2496 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2497 checksumoutstr
= hashlib
.new(checksumtype
);
2498 checksumoutstr
.update("".encode('UTF-8'));
2499 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2500 checksumoutstr
= hashlib
.new(checksumtype
);
2501 checksumoutstr
.update(fcontents
);
2502 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
2504 catfileheadercshex
= format(0, 'x').lower();
2505 catfilecontentcshex
= format(0, 'x').lower();
2506 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
2507 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2508 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
2509 if(checksumtype
=="none" or checksumtype
==""):
2510 catfileheadercshex
= format(0, 'x').lower()
2511 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2512 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower()
2513 elif(checksumtype
=="crc16_ccitt"):
2514 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower()
2515 elif(checksumtype
=="adler32"):
2516 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower()
2517 elif(checksumtype
=="crc32"):
2518 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower()
2519 elif(checksumtype
=="crc64_ecma"):
2520 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
2521 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2522 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
2523 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2524 checksumoutstr
= hashlib
.new(checksumtype
)
2525 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'))
2526 catfileheadercshex
= checksumoutstr
.hexdigest().lower()
2528 catfileheadercshex
= format(0, 'x').lower()
2529 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5])
2530 catfileoutstrecd
= catfileoutstr
.encode('UTF-8')
2531 nullstrecd
= formatspecs
[5].encode('UTF-8')
2532 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
2533 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
)
2534 catfp
.write(catfileout
)
2537 os
.fsync(catfp
.fileno())
2538 except io
.UnsupportedOperation
:
2540 except AttributeError:
2542 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2543 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
)
2546 os
.fsync(catfp
.fileno())
2547 except io
.UnsupportedOperation
:
2549 except AttributeError:
2553 if(hasattr(sys
.stdout
, "buffer")):
2554 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2556 shutil
.copyfileobj(catfp
, sys
.stdout
);
2557 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
2558 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2560 upload_file_to_ftp_file(catfp
, outfile
);
2561 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
2562 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2565 upload_file_to_pysftp_file(catfp
, outfile
);
2567 upload_file_to_sftp_file(catfp
, outfile
);
2575 create_alias_function("Pack", __file_format_name__
, "FromRarFile", PackArchiveFileFromRarFile
);
2577 def ArchiveFileSeekToFileNum(infile
, seekto
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
2578 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2581 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2582 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
2583 if(checkcompressfile
=="tarfile"):
2584 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2585 if(checkcompressfile
=="zipfile"):
2586 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2587 if(rarfile_support
and checkcompressfile
=="rarfile"):
2588 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2589 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
2596 if(hasattr(sys
.stdin
, "buffer")):
2597 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
2599 shutil
.copyfileobj(sys
.stdin
, catfp
);
2601 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2605 elif(re
.findall(r
"^(http|https)\:\/\/", infile
)):
2606 catfp
= download_file_from_http_file(infile
);
2608 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2612 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", infile
)):
2613 catfp
= download_file_from_ftp_file(infile
);
2615 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2619 elif(re
.findall(r
"^(sftp)\:\/\/", infile
) and haveparamiko
):
2621 catfp
= download_file_from_pysftp_file(infile
);
2623 catfp
= download_file_from_sftp_file(infile
);
2625 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2630 infile
= RemoveWindowsPath(infile
);
2631 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
2632 if(checkcompressfile
=="tarfile"):
2633 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2634 if(checkcompressfile
=="zipfile"):
2635 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2636 if(rarfile_support
and checkcompressfile
=="rarfile"):
2637 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2638 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
2640 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
2641 if(not compresscheck
):
2642 fextname
= os
.path
.splitext(infile
)[1];
2643 if(fextname
==".gz"):
2644 compresscheck
= "gzip";
2645 if(fextname
==".bz2"):
2646 compresscheck
= "bzip2";
2647 if(fextname
==".zst"):
2648 compresscheck
= "zstd";
2649 if(fextname
==".lz4" or fextname
==".clz4"):
2650 compresscheck
= "lz4";
2651 if(fextname
==".lzo" or fextname
==".lzop"):
2652 compresscheck
= "lzo";
2653 if(fextname
==".lzma" or fextname
==".xz"):
2654 compresscheck
= "lzma";
2655 if(not compresscheck
):
2657 catfp
= UncompressFile(infile
, formatspecs
, "rb");
2662 SeekToEndOfFile(catfp);
2664 SeekToEndOfFile(catfp);
2665 CatSize = catfp.tell();
2666 CatSizeEnd = CatSize;
2674 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
2675 catstring
= catheader
[0];
2676 catversion
= re
.findall(r
"([\d]+)$", catstring
);
2677 fprenumfiles
= catheader
[1];
2678 fnumfiles
= int(fprenumfiles
, 16);
2679 fprechecksumtype
= catheader
[2];
2680 fprechecksum
= catheader
[3];
2681 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
2682 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
2683 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
2684 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
2685 catfileheadercshex
= format(0, 'x').lower();
2686 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
2687 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2688 elif(fprechecksumtype
=="crc16_ccitt"):
2689 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2690 elif(fprechecksumtype
=="adler32"):
2691 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2692 elif(fprechecksumtype
=="crc32"):
2693 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2694 elif(fprechecksumtype
=="crc64_ecma"):
2695 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2696 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
2697 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2698 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
2699 checksumoutstr
= hashlib
.new(fprechecksumtype
);
2700 checksumoutstr
.update(fileheader
.encode('UTF-8'));
2701 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2703 catfileheadercshex
= format(0, 'x').lower();
2704 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2705 fheadtell
= len(fileheader
);
2706 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
2707 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(catfp
.tell()));
2709 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
2710 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
2711 if(seekto
>=fnumfiles
):
2712 seekto
= fnumfiles
- 1;
2718 seekstart
= catfp
.tell();
2719 preheaderdata
= ReadFileHeaderData(catfp
, 5, formatspecs
[5]);
2720 prefheadsize
= int(preheaderdata
[0], 16);
2721 prefseek
= prefheadsize
- (int(len(preheaderdata
[1]) + 1) + int(len(preheaderdata
[2]) + 1) + int(len(preheaderdata
[3]) + 1) + int(len(preheaderdata
[4]) + 1));
2722 preftype
= int(preheaderdata
[1], 16);
2723 prefsize
= int(preheaderdata
[4], 16);
2724 catfp
.seek(prefseek
, 1);
2726 catfp
.seek(prefsize
, 1);
2729 catfp
.seek(seekstart
, 0);
2731 catfheadsize
= int(preheaderdata
[0], 16);
2732 catftype
= int(preheaderdata
[1], 16);
2733 if(re
.findall("^[.|/]", preheaderdata
[2])):
2734 catfname
= preheaderdata
[2];
2736 catfname
= "./"+preheaderdata
[2];
2737 catflinkname
= preheaderdata
[3];
2738 catfsize
= int(preheaderdata
[4], 16);
2739 catfbasedir
= os
.path
.dirname(catfname
);
2740 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
2742 catlist
.update({'catfp': catfp
});
2747 create_alias_function("", __file_format_name__
, "SeekToFileNum", ArchiveFileSeekToFileNum
);
2749 def ArchiveFileSeekToFileName(infile
, seekfile
=None, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
2750 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2753 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2754 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
2755 if(checkcompressfile
=="tarfile"):
2756 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2757 if(checkcompressfile
=="zipfile"):
2758 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2759 if(rarfile_support
and checkcompressfile
=="rarfile"):
2760 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2761 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
2768 if(hasattr(sys
.stdin
, "buffer")):
2769 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
2771 shutil
.copyfileobj(sys
.stdin
, catfp
);
2773 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2777 elif(re
.findall(r
"^(http|https)\:\/\/", infile
)):
2778 catfp
= download_file_from_http_file(infile
);
2780 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2784 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", infile
)):
2785 catfp
= download_file_from_ftp_file(infile
);
2787 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2791 elif(re
.findall(r
"^(sftp)\:\/\/", infile
) and haveparamiko
):
2793 catfp
= download_file_from_pysftp_file(infile
);
2795 catfp
= download_file_from_sftp_file(infile
);
2797 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2802 infile
= RemoveWindowsPath(infile
);
2803 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
2804 if(checkcompressfile
=="tarfile"):
2805 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2806 if(checkcompressfile
=="zipfile"):
2807 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2808 if(rarfile_support
and checkcompressfile
=="rarfile"):
2809 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2810 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
2812 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
2813 if(not compresscheck
):
2814 fextname
= os
.path
.splitext(infile
)[1];
2815 if(fextname
==".gz"):
2816 compresscheck
= "gzip";
2817 if(fextname
==".bz2"):
2818 compresscheck
= "bzip2";
2819 if(fextname
==".zst"):
2820 compresscheck
= "zstd";
2821 if(fextname
==".lz4" or fextname
==".clz4"):
2822 compresscheck
= "lz4";
2823 if(fextname
==".lzo" or fextname
==".lzop"):
2824 compresscheck
= "lzo";
2825 if(fextname
==".lzma" or fextname
==".xz"):
2826 compresscheck
= "lzma";
2827 if(not compresscheck
):
2829 catfp
= UncompressFile(infile
, formatspecs
, "rb");
2834 SeekToEndOfFile(catfp);
2836 SeekToEndOfFile(catfp);
2837 CatSize = catfp.tell();
2838 CatSizeEnd = CatSize;
2846 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
2847 catstring
= catheader
[0];
2848 catversion
= re
.findall(r
"([\d]+)$", catstring
);
2849 fprenumfiles
= catheader
[1];
2850 fnumfiles
= int(fprenumfiles
, 16);
2851 fprechecksumtype
= catheader
[2];
2852 fprechecksum
= catheader
[3];
2853 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
2854 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
2855 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
2856 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
2857 catfileheadercshex
= format(0, 'x').lower();
2858 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
2859 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2860 elif(fprechecksumtype
=="crc16_ccitt"):
2861 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2862 elif(fprechecksumtype
=="adler32"):
2863 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2864 elif(fprechecksumtype
=="crc32"):
2865 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2866 elif(fprechecksumtype
=="crc64_ecma"):
2867 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2868 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
2869 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2870 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
2871 checksumoutstr
= hashlib
.new(fprechecksumtype
);
2872 checksumoutstr
.update(fileheader
.encode('UTF-8'));
2873 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2875 catfileheadercshex
= format(0, 'x').lower();
2876 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
2877 fheadtell
= len(fileheader
);
2878 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
2879 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(catfp
.tell()));
2881 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
2882 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
2883 seekto
= fnumfiles
- 1
2888 seekstart
= catfp
.tell();
2889 preheaderdata
= ReadFileHeaderData(catfp
, 5, formatspecs
[5]);
2890 prefheadsize
= int(preheaderdata
[0], 16);
2891 prefseek
= prefheadsize
- (int(len(preheaderdata
[1]) + 1) + int(len(preheaderdata
[2]) + 1) + int(len(preheaderdata
[3]) + 1) + int(len(preheaderdata
[4]) + 1));
2892 preftype
= int(preheaderdata
[1], 16);
2893 prefsize
= int(preheaderdata
[4], 16);
2894 catfp
.seek(prefseek
, 1);
2896 catfp
.seek(prefsize
, 1);
2900 prefname
= preheaderdata
[2];
2901 if(re
.findall("^[.|/]", preheaderdata
[2])):
2902 prefname
= preheaderdata
[2];
2904 prefname
= "./"+preheaderdata
[2];
2905 if(prefname
==seekfile
):
2908 catfp
.seek(seekstart
, 0);
2910 catfheadsize
= int(preheaderdata
[0], 16);
2911 catftype
= int(preheaderdata
[1], 16);
2912 if(re
.findall("^[.|/]", preheaderdata
[2])):
2913 catfname
= preheaderdata
[2];
2915 catfname
= "./"+preheaderdata
[2];
2916 catflinkname
= preheaderdata
[3];
2917 catfsize
= int(preheaderdata
[4], 16);
2918 catfbasedir
= os
.path
.dirname(catfname
);
2920 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
2924 catlist
.update({'catfp': catfp
});
2929 create_alias_function("", __file_format_name__
, "SeekToFileName", ArchiveFileSeekToFileName
);
2931 def ArchiveFileValidate(infile
, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2933 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2934 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2937 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2938 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
2939 if(checkcompressfile
=="tarfile"):
2940 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
2941 if(checkcompressfile
=="zipfile"):
2942 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
2943 if(rarfile_support
and checkcompressfile
=="rarfile"):
2944 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
2945 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
2952 if(hasattr(sys
.stdin
, "buffer")):
2953 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
2955 shutil
.copyfileobj(sys
.stdin
, catfp
);
2957 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2961 elif(re
.findall(r
"^(http|https)\:\/\/", infile
)):
2962 catfp
= download_file_from_http_file(infile
);
2964 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2968 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", infile
)):
2969 catfp
= download_file_from_ftp_file(infile
);
2971 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2975 elif(re
.findall(r
"^(sftp)\:\/\/", infile
) and haveparamiko
):
2977 catfp
= download_file_from_pysftp_file(infile
);
2979 catfp
= download_file_from_sftp_file(infile
);
2981 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2986 infile
= RemoveWindowsPath(infile
);
2987 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
2988 if(checkcompressfile
=="tarfile"):
2989 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
2990 if(checkcompressfile
=="zipfile"):
2991 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
2992 if(rarfile_support
and checkcompressfile
=="rarfile"):
2993 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
2994 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
2996 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
2997 if(not compresscheck
):
2998 fextname
= os
.path
.splitext(infile
)[1];
2999 if(fextname
==".gz"):
3000 compresscheck
= "gzip";
3001 if(fextname
==".bz2"):
3002 compresscheck
= "bzip2";
3003 if(fextname
==".zst"):
3004 compresscheck
= "zstd";
3005 if(fextname
==".lz4" or fextname
==".clz4"):
3006 compresscheck
= "lz4";
3007 if(fextname
==".lzo" or fextname
==".lzop"):
3008 compresscheck
= "lzo";
3009 if(fextname
==".lzma" or fextname
==".xz"):
3010 compresscheck
= "lzma";
3011 if(not compresscheck
):
3013 catfp
= UncompressFile(infile
, formatspecs
, "rb");
3018 SeekToEndOfFile(catfp);
3020 SeekToEndOfFile(catfp);
3021 CatSize = catfp.tell();
3022 CatSizeEnd = CatSize;
3030 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
3031 catstring
= catheader
[0];
3032 catversion
= re
.findall(r
"([\d]+)$", catstring
);
3033 fprenumfiles
= catheader
[1];
3034 fnumfiles
= int(fprenumfiles
, 16);
3035 fprechecksumtype
= catheader
[2];
3036 fprechecksum
= catheader
[3];
3038 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
3039 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3040 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
3041 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
3042 catfileheadercshex
= format(0, 'x').lower();
3043 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
3044 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3045 elif(fprechecksumtype
=="crc16_ccitt"):
3046 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3047 elif(fprechecksumtype
=="adler32"):
3048 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3049 elif(fprechecksumtype
=="crc32"):
3050 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3051 elif(fprechecksumtype
=="crc64_ecma"):
3052 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3053 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
3054 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3055 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
3056 checksumoutstr
= hashlib
.new(fprechecksumtype
);
3057 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3058 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3060 catfileheadercshex
= format(0, 'x').lower();
3061 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3063 VerbosePrintOut("Checking File Header Checksum of file " + infile
+ " at offset " + str(0));
3064 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
3065 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(0));
3067 while(il
<fnumfiles
):
3068 catfhstart
= catfp
.tell();
3070 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3072 catheaderdata
= ReadFileHeaderData(catfp
, 23, formatspecs
[5]);
3073 catfheadsize
= int(catheaderdata
[0], 16);
3074 catftype
= int(catheaderdata
[1], 16);
3075 if(re
.findall("^[.|/]", catheaderdata
[2])):
3076 catfname
= catheaderdata
[2];
3078 catfname
= "./"+catheaderdata
[2];
3079 catfbasedir
= os
.path
.dirname(catfname
);
3080 catflinkname
= catheaderdata
[3];
3081 catfsize
= int(catheaderdata
[4], 16);
3082 catfatime
= int(catheaderdata
[5], 16);
3083 catfmtime
= int(catheaderdata
[6], 16);
3084 catfctime
= int(catheaderdata
[7], 16);
3085 catfbtime
= int(catheaderdata
[8], 16);
3086 catfmode
= int(catheaderdata
[9], 16);
3087 catfchmode
= stat
.S_IMODE(catfmode
);
3088 catftypemod
= stat
.S_IFMT(catfmode
);
3089 catfuid
= int(catheaderdata
[10], 16);
3090 catfuname
= catheaderdata
[11];
3091 catfgid
= int(catheaderdata
[12], 16);
3092 catfgname
= catheaderdata
[13];
3093 fid
= int(catheaderdata
[14], 16);
3094 finode
= int(catheaderdata
[15], 16);
3095 flinkcount
= int(catheaderdata
[16], 16);
3096 catfdev_minor
= int(catheaderdata
[17], 16);
3097 catfdev_major
= int(catheaderdata
[18], 16);
3098 catfrdev_minor
= int(catheaderdata
[19], 16);
3099 catfrdev_major
= int(catheaderdata
[20], 16);
3100 catfextrasize
= int(catheaderdata
[21], 16);
3101 catfextrafields
= int(catheaderdata
[22], 16);
3102 extrafieldslist
= [];
3105 extraend
= extrastart
+ catfextrafields
;
3106 extrafieldslist
= [];
3107 if(extrastart
<extraend
):
3108 extrafieldslist
.append(catheaderdata
[extrastart
]);
3109 extrastart
= extrastart
+ 1;
3110 catfchecksumtype
= catheaderdata
[extrastart
].lower();
3111 catfcs
= catheaderdata
[extrastart
+ 1].lower();
3112 catfccs
= catheaderdata
[extrastart
+ 2].lower();
3114 extrafieldslist
= [];
3116 extrafieldslist
= ReadFileHeaderData(catfp
, catfextrafields
, formatspecs
[5]);
3117 checksumsval
= ReadFileHeaderData(catfp
, 3, formatspecs
[5]);
3118 catfchecksumtype
= checksumsval
[0].lower();
3119 catfcs
= checksumsval
[1].lower();
3120 catfccs
= checksumsval
[2].lower();
3123 hcmax
= len(catheaderdata
) - 2;
3125 hcmax
= len(catheaderdata
);
3128 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[5]);
3130 catfnumfields
= 24 + catfextrafields
;
3131 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
3133 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
3134 catnewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
3135 elif(catfchecksumtype
=="adler32"):
3136 catnewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3137 elif(catfchecksumtype
=="crc32"):
3138 catnewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3139 elif(catfchecksumtype
=="crc64_ecma"):
3140 catnewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3141 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
3142 catnewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3143 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
3144 checksumoutstr
= hashlib
.new(catfchecksumtype
);
3145 checksumoutstr
.update(hout
.encode('UTF-8'));
3146 catnewfcs
= checksumoutstr
.hexdigest().lower();
3148 VerbosePrintOut("Checking File Header Checksum of file " + catfname
+ " at offset " + str(catfhstart
));
3149 if(catfcs
!=catnewfcs
and not skipchecksum
):
3150 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfhstart
));
3152 catfhend
= catfp
.tell() - 1;
3153 catfcontentstart
= catfp
.tell();
3155 pyhascontents
= False;
3157 catfcontents
= catfp
.read(catfsize
);
3158 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
3160 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
3161 catnewfccs
= format(crc16(catfcontents
) & 0xffff, '04x').lower();
3162 elif(catfchecksumtype
=="crc16_ccitt"):
3163 catnewfcs
= format(crc16_ccitt(catfcontents
) & 0xffff, '04x').lower();
3164 elif(catfchecksumtype
=="adler32"):
3165 catnewfccs
= format(zlib
.adler32(catfcontents
) & 0xffffffff, '08x').lower();
3166 elif(catfchecksumtype
=="crc32"):
3167 catnewfccs
= format(crc32(catfcontents
) & 0xffffffff, '08x').lower();
3168 elif(catfchecksumtype
=="crc64_ecma"):
3169 catnewfcs
= format(crc64_ecma(catfcontents
) & 0xffffffffffffffff, '016x').lower();
3170 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
3171 catnewfcs
= format(crc64_iso(catfcontents
) & 0xffffffffffffffff, '016x').lower();
3172 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
3173 checksumoutstr
= hashlib
.new(catfchecksumtype
);
3174 checksumoutstr
.update(catfcontents
);
3175 catnewfccs
= checksumoutstr
.hexdigest().lower();
3176 pyhascontents
= True;
3178 VerbosePrintOut("Checking File Content Checksum of file " + catfname
+ " at offset " + str(catfcontentstart
));
3179 if(catfccs
!=catnewfccs
and skipchecksum
):
3180 VerbosePrintOut("File Content Checksum Error with file " + catfname
+ " at offset " + str(catfcontentstart
));
3190 create_alias_function("", __file_format_name__
, "Validate", ArchiveFileValidate
);
3192 def ArchiveFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3193 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3196 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3197 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
3198 if(checkcompressfile
=="tarfile"):
3199 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3200 if(checkcompressfile
=="zipfile"):
3201 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3202 if(rarfile_support
and checkcompressfile
=="rarfile"):
3203 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3204 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3211 if(hasattr(sys
.stdin
, "buffer")):
3212 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
3214 shutil
.copyfileobj(sys
.stdin
, catfp
);
3216 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3220 elif(re
.findall(r
"^(http|https)\:\/\/", infile
)):
3221 catfp
= download_file_from_http_file(infile
);
3223 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3227 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", infile
)):
3228 catfp
= download_file_from_ftp_file(infile
);
3230 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3234 elif(re
.findall(r
"^(sftp)\:\/\/", infile
) and haveparamiko
):
3236 catfp
= download_file_from_pysftp_file(infile
);
3238 catfp
= download_file_from_sftp_file(infile
);
3240 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
3245 infile
= RemoveWindowsPath(infile
);
3246 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
3247 if(checkcompressfile
=="tarfile"):
3248 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3249 if(checkcompressfile
=="zipfile"):
3250 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3251 if(rarfile_support
and checkcompressfile
=="rarfile"):
3252 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3253 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[2]):
3255 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
3256 if(not compresscheck
):
3257 fextname
= os
.path
.splitext(infile
)[1];
3258 if(fextname
==".gz"):
3259 compresscheck
= "gzip";
3260 if(fextname
==".bz2"):
3261 compresscheck
= "bzip2";
3262 if(fextname
==".zst"):
3263 compresscheck
= "zstd";
3264 if(fextname
==".lz4" or fextname
==".clz4"):
3265 compresscheck
= "lz4";
3266 if(fextname
==".lzo" or fextname
==".lzop"):
3267 compresscheck
= "lzo";
3268 if(fextname
==".lzma" or fextname
==".xz"):
3269 compresscheck
= "lzma";
3270 if(not compresscheck
):
3272 catfp
= UncompressFile(infile
, formatspecs
, "rb");
3277 SeekToEndOfFile(catfp);
3279 SeekToEndOfFile(catfp);
3280 CatSize = catfp.tell();
3281 CatSizeEnd = CatSize;
3289 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[5]);
3290 catstring
= catheader
[0];
3291 catversion
= re
.findall(r
"([\d]+)$", catstring
);
3292 fprenumfiles
= catheader
[1];
3293 fnumfiles
= int(fprenumfiles
, 16);
3294 fprechecksumtype
= catheader
[2];
3295 fprechecksum
= catheader
[3];
3296 fileheader
= AppendNullByte(catstring
, formatspecs
[5]);
3297 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3298 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[5]);
3299 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
3300 catfileheadercshex
= format(0, 'x').lower();
3301 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
3302 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3303 elif(fprechecksumtype
=="crc16_ccitt"):
3304 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3305 elif(fprechecksumtype
=="adler32"):
3306 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3307 elif(fprechecksumtype
=="crc32"):
3308 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3309 elif(fprechecksumtype
=="crc64_ecma"):
3310 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3311 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
3312 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3313 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
3314 checksumoutstr
= hashlib
.new(fprechecksumtype
);
3315 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3316 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3318 catfileheadercshex
= format(0, 'x').lower();
3319 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3320 fheadtell
= len(fileheader
);
3321 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
3322 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(catfp
.tell()));
3324 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
3325 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
3326 if(seekstart
<0 and seekstart
>fnumfiles
):
3328 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
3329 seekend
= fnumfiles
;
3330 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
3331 seekend
= fnumfiles
- abs(seekend
);
3334 while(il
< seekstart
):
3335 preheaderdata
= ReadFileHeaderData(catfp
, 5, formatspecs
[5]);
3336 prefheadsize
= int(preheaderdata
[0], 16);
3337 prefseek
= prefheadsize
- (int(len(preheaderdata
[1]) + 1) + int(len(preheaderdata
[2]) + 1) + int(len(preheaderdata
[3]) + 1) + int(len(preheaderdata
[4]) + 1));
3338 preftype
= int(preheaderdata
[1], 16);
3339 prefsize
= int(preheaderdata
[4], 16);
3340 catfp
.seek(prefseek
, 1);
3342 catfp
.seek(prefsize
, 1);
3345 fileidnum
= seekstart
;
3347 while(fileidnum
<seekend
):
3348 catfhstart
= catfp
.tell();
3350 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[5]);
3352 catheaderdata
= ReadFileHeaderData(catfp
, 23, formatspecs
[5]);
3353 catfheadsize
= int(catheaderdata
[0], 16);
3354 catftype
= int(catheaderdata
[1], 16);
3355 if(re
.findall("^[.|/]", catheaderdata
[2])):
3356 catfname
= catheaderdata
[2];
3358 catfname
= "./"+catheaderdata
[2];
3359 catfbasedir
= os
.path
.dirname(catfname
);
3360 catflinkname
= catheaderdata
[3];
3361 catfsize
= int(catheaderdata
[4], 16);
3362 catfatime
= int(catheaderdata
[5], 16);
3363 catfmtime
= int(catheaderdata
[6], 16);
3364 catfctime
= int(catheaderdata
[7], 16);
3365 catfbtime
= int(catheaderdata
[8], 16);
3366 catfmode
= int(catheaderdata
[9], 16);
3367 catfchmode
= stat
.S_IMODE(catfmode
);
3368 catftypemod
= stat
.S_IFMT(catfmode
);
3369 catfuid
= int(catheaderdata
[10], 16);
3370 catfuname
= catheaderdata
[11];
3371 catfgid
= int(catheaderdata
[12], 16);
3372 catfgname
= catheaderdata
[13];
3373 fid
= int(catheaderdata
[14], 16);
3374 finode
= int(catheaderdata
[15], 16);
3375 flinkcount
= int(catheaderdata
[16], 16);
3376 catfdev_minor
= int(catheaderdata
[17], 16);
3377 catfdev_major
= int(catheaderdata
[18], 16);
3378 catfrdev_minor
= int(catheaderdata
[19], 16);
3379 catfrdev_major
= int(catheaderdata
[20], 16);
3380 catfextrasize
= int(catheaderdata
[21], 16);
3381 catfextrafields
= int(catheaderdata
[22], 16);
3382 extrafieldslist
= [];
3385 extraend
= extrastart
+ catfextrafields
;
3386 extrafieldslist
= [];
3387 if(extrastart
<extraend
):
3388 extrafieldslist
.append(catheaderdata
[extrastart
]);
3389 extrastart
= extrastart
+ 1;
3390 catfchecksumtype
= catheaderdata
[extrastart
].lower();
3391 catfcs
= catheaderdata
[extrastart
+ 1].lower();
3392 catfccs
= catheaderdata
[extrastart
+ 2].lower();
3394 extrafieldslist
= [];
3396 extrafieldslist
= ReadFileHeaderData(catfp
, catfextrafields
, formatspecs
[5]);
3397 checksumsval
= ReadFileHeaderData(catfp
, 3, formatspecs
[5]);
3398 catfchecksumtype
= checksumsval
[0].lower();
3399 catfcs
= checksumsval
[1].lower();
3400 catfccs
= checksumsval
[2].lower();
3403 hcmax
= len(catheaderdata
) - 2;
3405 hcmax
= len(catheaderdata
);
3408 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[5]);
3410 catfnumfields
= 24 + catfextrafields
;
3411 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
3413 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
3414 catnewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
3415 elif(catfchecksumtype
=="adler32"):
3416 catnewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3417 elif(catfchecksumtype
=="crc32"):
3418 catnewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3419 elif(catfchecksumtype
=="crc64_ecma"):
3420 catnewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3421 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
3422 catnewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3423 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
3424 checksumoutstr
= hashlib
.new(catfchecksumtype
);
3425 checksumoutstr
.update(hout
.encode('UTF-8'));
3426 catnewfcs
= checksumoutstr
.hexdigest().lower();
3427 if(catfcs
!=catnewfcs
and not skipchecksum
):
3428 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfhstart
));
3430 catfhend
= catfp
.tell() - 1;
3431 catfcontentstart
= catfp
.tell();
3433 pyhascontents
= False;
3434 if(catfsize
>0 and not listonly
):
3435 catfcontents
= catfp
.read(catfsize
);
3436 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
3438 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
3439 catnewfccs
= format(crc16(catfcontents
) & 0xffff, '04x').lower();
3440 elif(catfchecksumtype
=="crc16_ccitt"):
3441 catnewfcs
= format(crc16_ccitt(catfcontents
) & 0xffff, '04x').lower();
3442 elif(catfchecksumtype
=="adler32"):
3443 catnewfccs
= format(zlib
.adler32(catfcontents
) & 0xffffffff, '08x').lower();
3444 elif(catfchecksumtype
=="crc32"):
3445 catnewfccs
= format(crc32(catfcontents
) & 0xffffffff, '08x').lower();
3446 elif(catfchecksumtype
=="crc64_ecma"):
3447 catnewfcs
= format(crc64_ecma(catfcontents
) & 0xffffffffffffffff, '016x').lower();
3448 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
3449 catnewfcs
= format(crc64_iso(catfcontents
) & 0xffffffffffffffff, '016x').lower();
3450 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
3451 checksumoutstr
= hashlib
.new(catfchecksumtype
);
3452 checksumoutstr
.update(catfcontents
);
3453 catnewfccs
= checksumoutstr
.hexdigest().lower();
3454 pyhascontents
= True;
3455 if(catfccs
!=catnewfccs
and skipchecksum
):
3456 VerbosePrintOut("File Content Checksum Error with file " + catfname
+ " at offset " + str(catfcontentstart
));
3458 if(catfsize
>0 and listonly
):
3459 catfp
.seek(catfsize
, 1);
3460 pyhascontents
= False;
3462 catfcontentend
= catfp
.tell() - 1;
3463 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
} });
3464 fileidnum
= fileidnum
+ 1;
3465 realidnum
= realidnum
+ 1;
3467 catlist
.update({'catfp': catfp
});
3472 create_alias_function("", __file_format_name__
, "ToArray", ArchiveFileToArray
);
3474 def ArchiveFileStringToArray(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3475 catfp
= BytesIO(catstr
);
3476 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3477 return listcatfiles
;
3479 create_alias_function("", __file_format_name__
, "StringToArray", ArchiveFileStringToArray
);
3481 def TarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3483 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
3484 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3485 return listcatfiles
;
3487 def ZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3489 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
3490 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3491 return listcatfiles
;
3493 if(not rarfile_support
):
3494 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3497 if(rarfile_support
):
3498 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3500 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
3501 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3502 return listcatfiles
;
3504 def ListDirToArrayAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
3505 catver
= formatspecs
[6];
3506 fileheaderver
= str(int(catver
.replace(".", "")));
3507 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
3508 advancedlist
= True;
3511 for line
in sys
.stdin
:
3512 infilelist
.append(line
.strip());
3513 infilelist
= list(filter(None, infilelist
));
3514 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
3515 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
3517 with
open(infiles
, "r") as finfile
:
3518 for line
in finfile
:
3519 infilelist
.append(line
.strip());
3520 infilelist
= list(filter(None, infilelist
));
3522 if(isinstance(infiles
, (list, tuple, ))):
3523 infilelist
= list(filter(None, infiles
));
3524 elif(isinstance(infiles
, (str, ))):
3525 infilelist
= list(filter(None, [infiles
]));
3527 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
3529 GetDirList
= ListDir(infilelist
, followlink
, False);
3537 inodetocatinode
= {};
3539 fnumfiles
= int(len(GetDirList
));
3540 catver
= formatspecs
[6];
3541 fileheaderver
= str(int(catver
.replace(".", "")));
3542 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
3543 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3544 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
3545 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
3546 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
3547 if(checksumtype
=="none" or checksumtype
==""):
3548 catfileheadercshex
= format(0, 'x').lower();
3549 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3550 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3551 elif(checksumtype
=="crc16_ccitt"):
3552 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3553 elif(checksumtype
=="adler32"):
3554 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3555 elif(checksumtype
=="crc32"):
3556 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3557 elif(checksumtype
=="crc64_ecma"):
3558 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3559 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3560 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3561 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3562 checksumoutstr
= hashlib
.new(checksumtype
);
3563 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3564 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3566 catfileheadercshex
= format(0, 'x').lower();
3567 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3568 fheadtell
= len(fileheader
);
3569 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
3570 for curfname
in GetDirList
:
3571 if(re
.findall("^[.|/]", curfname
)):
3574 fname
= "./"+curfname
;
3576 VerbosePrintOut(fname
);
3577 if(not followlink
or followlink
is None):
3578 fstatinfo
= os
.lstat(fname
);
3580 fstatinfo
= os
.stat(fname
);
3581 fpremode
= fstatinfo
.st_mode
;
3582 finode
= fstatinfo
.st_ino
;
3583 flinkcount
= fstatinfo
.st_nlink
;
3585 if(stat
.S_ISREG(fpremode
)):
3587 elif(stat
.S_ISLNK(fpremode
)):
3589 elif(stat
.S_ISCHR(fpremode
)):
3591 elif(stat
.S_ISBLK(fpremode
)):
3593 elif(stat
.S_ISDIR(fpremode
)):
3595 elif(stat
.S_ISFIFO(fpremode
)):
3597 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
3599 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
3601 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
3606 fbasedir
= os
.path
.dirname(fname
);
3608 if(not followlink
and finode
!=0):
3610 if(finode
in inodelist
):
3612 flinkname
= inodetofile
[finode
];
3613 fcurinode
= inodetocatinode
[finode
];
3614 if(finode
not in inodelist
):
3615 inodelist
.append(finode
);
3616 inodetofile
.update({finode
: fname
});
3617 inodetocatinode
.update({finode
: curinode
});
3618 fcurinode
= curinode
;
3619 curinode
= curinode
+ 1;
3621 fcurinode
= curinode
;
3622 curinode
= curinode
+ 1;
3623 curfid
= curfid
+ 1;
3625 flinkname
= os
.readlink(fname
);
3626 fdev
= fstatinfo
.st_dev
;
3627 getfdev
= GetDevMajorMinor(fdev
);
3628 fdev_minor
= getfdev
[0];
3629 fdev_major
= getfdev
[1];
3630 frdev
= fstatinfo
.st_dev
;
3631 if(hasattr(fstatinfo
, "st_rdev")):
3632 frdev
= fstatinfo
.st_rdev
;
3634 frdev
= fstatinfo
.st_dev
;
3635 getfrdev
= GetDevMajorMinor(frdev
);
3636 frdev_minor
= getfrdev
[0];
3637 frdev_major
= getfrdev
[1];
3638 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
3640 if(ftype
==0 or ftype
==7):
3641 fsize
= fstatinfo
.st_size
;
3642 fatime
= fstatinfo
.st_atime
;
3643 fmtime
= fstatinfo
.st_mtime
;
3644 fctime
= fstatinfo
.st_ctime
;
3645 if(hasattr(fstatinfo
, "st_birthtime")):
3646 fbtime
= fstatinfo
.st_birthtime
;
3648 fbtime
= fstatinfo
.st_ctime
;
3649 fmode
= fstatinfo
.st_mode
;
3650 fchmode
= stat
.S_IMODE(fstatinfo
.st_mode
);
3651 ftypemod
= stat
.S_IFMT(fstatinfo
.st_mode
);
3652 fuid
= fstatinfo
.st_uid
;
3653 fgid
= fstatinfo
.st_gid
;
3658 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
3659 funame
= userinfo
.pw_name
;
3668 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
3669 fgname
= groupinfo
.gr_name
;
3674 fdev_minor
= fdev_minor
;
3675 fdev_major
= fdev_major
;
3676 frdev_minor
= frdev_minor
;
3677 frdev_major
= frdev_major
;
3679 flinkcount
= flinkcount
;
3680 if(hasattr(fstatinfo
, "st_file_attributes")):
3681 fwinattributes
= fstatinfo
.st_file_attributes
;
3684 fcontents
= "".encode('UTF-8');
3686 if(ftype
== 0 or ftype
== 7):
3687 with
open(fname
, "rb") as fpc
:
3689 chunk
= fpc
.read(chunk_size
);
3693 if(followlink
and (ftype
== 1 or ftype
== 2)):
3694 flstatinfo
= os
.stat(flinkname
);
3695 with
open(flinkname
, "rb") as fpc
:
3697 chunk
= fpc
.read(chunk_size
);
3701 ftypehex
= format(ftype
, 'x').lower();
3702 extrafields
= len(extradata
);
3703 extrafieldslist
= extradata
;
3704 catfextrafields
= extrafields
;
3705 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
3706 if(len(extradata
)>0):
3707 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3708 extrasizelen
= len(extrasizestr
);
3709 extrasizelenhex
= format(extrasizelen
, 'x').lower();
3710 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]);
3711 if(len(extradata
)>0):
3712 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3713 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
3714 catfnumfields
= 24 + catfextrafields
;
3715 if(checksumtype
=="none" or checksumtype
==""):
3716 catfileheadercshex
= format(0, 'x').lower();
3717 catfilecontentcshex
= format(0, 'x').lower();
3718 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3719 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3720 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
3721 elif(checksumtype
=="crc16_ccitt"):
3722 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3723 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
3724 elif(checksumtype
=="adler32"):
3725 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3726 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
3727 elif(checksumtype
=="crc32"):
3728 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3729 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
3730 elif(checksumtype
=="crc64_ecma"):
3731 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3732 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
3733 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3734 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3735 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
3736 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3737 checksumoutstr
= hashlib
.new(checksumtype
);
3738 checksumoutstr
.update("".encode('UTF-8'));
3739 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3740 checksumoutstr
= hashlib
.new(checksumtype
);
3741 checksumoutstr
.update(fcontents
);
3742 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
3744 catfileheadercshex
= format(0, 'x').lower();
3745 catfilecontentcshex
= format(0, 'x').lower();
3746 catfhstart
= fheadtell
;
3747 fheadtell
+= len(catfileoutstr
);
3748 catfhend
= fheadtell
- 1;
3749 catfcontentstart
= fheadtell
;
3750 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
3751 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
3752 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
3753 if(checksumtype
=="none" or checksumtype
==""):
3754 catfileheadercshex
= format(0, 'x').lower();
3755 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3756 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3757 elif(checksumtype
=="crc16_ccitt"):
3758 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3759 elif(checksumtype
=="adler32"):
3760 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3761 elif(checksumtype
=="crc32"):
3762 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3763 elif(checksumtype
=="crc64_ecma"):
3764 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3765 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3766 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3767 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3768 checksumoutstr
= hashlib
.new(checksumtype
);
3769 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
3770 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3772 catfileheadercshex
= format(0, 'x').lower();
3773 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
3774 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
3775 nullstrecd
= formatspecs
[5].encode('UTF-8');
3776 fheadtell
+= len(catfileoutstr
) + 1;
3777 catfcontentend
= fheadtell
- 1;
3778 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
3779 pyhascontents
= False;
3780 if(int(fsize
)>0 and not listonly
):
3781 pyhascontents
= True;
3782 if(int(fsize
)>0 and listonly
):
3784 pyhascontents
= False;
3785 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
} });
3786 fileidnum
= fileidnum
+ 1;
3789 def TarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
3795 inodetocatinode
= {};
3799 if(hasattr(sys
.stdin
, "buffer")):
3800 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
3802 shutil
.copyfileobj(sys
.stdin
, infile
);
3807 elif(re
.findall(r
"^(http|https)\:\/\/", infile
)):
3808 infile
= download_file_from_http_file(infile
);
3813 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", infile
)):
3814 infile
= download_file_from_ftp_file(infile
);
3819 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
3821 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
3823 if(not tarfile
.is_tarfile(infile
)):
3825 except AttributeError:
3826 if(not is_tarfile(infile
)):
3831 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
3832 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
3834 tarfp
= tarfile
.open(infile
, "r");
3835 except FileNotFoundError
:
3837 fnumfiles
= int(len(tarfp
.getmembers()));
3838 catver
= formatspecs
[6];
3839 fileheaderver
= str(int(catver
.replace(".", "")));
3840 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
3841 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3842 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
3843 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
3844 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
3845 if(checksumtype
=="none" or checksumtype
==""):
3846 catfileheadercshex
= format(0, 'x').lower();
3847 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3848 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3849 elif(checksumtype
=="crc16_ccitt"):
3850 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3851 elif(checksumtype
=="adler32"):
3852 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3853 elif(checksumtype
=="crc32"):
3854 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3855 elif(checksumtype
=="crc64_ecma"):
3856 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3857 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3858 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3859 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3860 checksumoutstr
= hashlib
.new(checksumtype
);
3861 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3862 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3864 catfileheadercshex
= format(0, 'x').lower();
3865 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
3866 fheadtell
= len(fileheader
);
3867 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
3868 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
3869 if(re
.findall("^[.|/]", member
.name
)):
3870 fname
= member
.name
;
3872 fname
= "./"+member
.name
;
3874 VerbosePrintOut(fname
);
3875 fpremode
= member
.mode
;
3876 ffullmode
= member
.mode
;
3880 ffullmode
= member
.mode
+ stat
.S_IFREG
;
3882 elif(member
.isdev()):
3883 ffullmode
= member
.mode
;
3885 elif(member
.islnk()):
3886 ffullmode
= member
.mode
+ stat
.S_IFREG
;
3888 elif(member
.issym()):
3889 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
3891 elif(member
.ischr()):
3892 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
3894 elif(member
.isblk()):
3895 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
3897 elif(member
.isdir()):
3898 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
3900 elif(member
.isfifo()):
3901 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
3903 elif(member
.issparse()):
3904 ffullmode
= member
.mode
;
3907 ffullmode
= member
.mode
;
3910 fbasedir
= os
.path
.dirname(fname
);
3914 curfid
= curfid
+ 1;
3916 flinkname
= member
.linkname
;
3917 fdev_minor
= member
.devminor
;
3918 fdev_major
= member
.devmajor
;
3919 frdev_minor
= member
.devminor
;
3920 frdev_major
= member
.devmajor
;
3921 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
3923 elif(ftype
==0 or ftype
==7):
3924 fsize
= member
.size
;
3926 fsize
= member
.size
;
3927 fatime
= member
.mtime
;
3928 fmtime
= member
.mtime
;
3929 fctime
= member
.mtime
;
3930 fbtime
= member
.mtime
;
3932 fchmode
= stat
.S_IMODE(ffullmode
);
3933 ftypemod
= stat
.S_IFMT(ffullmode
);
3936 funame
= member
.uname
;
3937 fgname
= member
.gname
;
3938 flinkcount
= flinkcount
;
3939 fcontents
= "".encode('UTF-8');
3941 if(ftype
== 0 or ftype
== 7):
3942 with tarfp
.extractfile(member
) as fpc
:
3944 chunk
= fpc
.read(chunk_size
);
3948 ftypehex
= format(ftype
, 'x').lower();
3949 extrafields
= len(extradata
);
3950 extrafieldslist
= extradata
;
3951 catfextrafields
= extrafields
;
3952 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
3953 if(len(extradata
)>0):
3954 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3955 extrasizelen
= len(extrasizestr
);
3956 extrasizelenhex
= format(extrasizelen
, 'x').lower();
3957 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]);
3958 if(len(extradata
)>0):
3959 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
3960 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
3961 catfnumfields
= 24 + catfextrafields
;
3962 if(checksumtype
=="none" or checksumtype
==""):
3963 catfileheadercshex
= format(0, 'x').lower();
3964 catfilecontentcshex
= format(0, 'x').lower();
3965 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3966 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3967 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
3968 elif(checksumtype
=="crc16_ccitt"):
3969 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3970 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
3971 elif(checksumtype
=="adler32"):
3972 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3973 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
3974 elif(checksumtype
=="crc32"):
3975 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3976 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
3977 elif(checksumtype
=="crc64_ecma"):
3978 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3979 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
3980 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3981 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3982 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
3983 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3984 checksumoutstr
= hashlib
.new(checksumtype
);
3985 checksumoutstr
.update("".encode('UTF-8'));
3986 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3987 checksumoutstr
= hashlib
.new(checksumtype
);
3988 checksumoutstr
.update(fcontents
);
3989 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
3991 catfileheadercshex
= format(0, 'x').lower();
3992 catfilecontentcshex
= format(0, 'x').lower();
3993 catfhstart
= fheadtell
;
3994 fheadtell
+= len(catfileoutstr
);
3995 catfhend
= fheadtell
- 1;
3996 catfcontentstart
= fheadtell
;
3997 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
3998 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
3999 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
4000 if(checksumtype
=="none" or checksumtype
==""):
4001 catfileheadercshex
= format(0, 'x').lower();
4002 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4003 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4004 elif(checksumtype
=="crc16_ccitt"):
4005 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4006 elif(checksumtype
=="adler32"):
4007 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4008 elif(checksumtype
=="crc32"):
4009 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4010 elif(checksumtype
=="crc64_ecma"):
4011 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4012 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4013 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4014 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4015 checksumoutstr
= hashlib
.new(checksumtype
);
4016 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
4017 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4019 catfileheadercshex
= format(0, 'x').lower();
4020 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4021 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
4022 nullstrecd
= formatspecs
[5].encode('UTF-8');
4023 fheadtell
+= len(catfileoutstr
) + 1;
4024 catfcontentend
= fheadtell
- 1;
4025 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
4026 pyhascontents
= False;
4027 if(int(fsize
)>0 and not listonly
):
4028 pyhascontents
= True;
4029 if(int(fsize
)>0 and listonly
):
4031 pyhascontents
= False;
4032 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
} });
4033 fileidnum
= fileidnum
+ 1;
4036 def ZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4037 advancedlist
= True;
4043 inodetocatinode
= {};
4047 if(hasattr(sys
.stdin
, "buffer")):
4048 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
4050 shutil
.copyfileobj(sys
.stdin
, infile
);
4055 elif(re
.findall(r
"^(http|https)\:\/\/", infile
)):
4056 infile
= download_file_from_http_file(infile
);
4061 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", infile
)):
4062 infile
= download_file_from_ftp_file(infile
);
4067 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
4071 if(not zipfile
.is_zipfile(infile
)):
4074 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
4075 except FileNotFoundError
:
4077 ziptest
= zipfp
.testzip();
4079 VerbosePrintOut("Bad file found!");
4080 fnumfiles
= int(len(zipfp
.infolist()));
4081 catver
= formatspecs
[6];
4082 fileheaderver
= str(int(catver
.replace(".", "")));
4083 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4084 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4085 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4086 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4087 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4088 if(checksumtype
=="none" or checksumtype
==""):
4089 catfileheadercshex
= format(0, 'x').lower();
4090 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4091 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4092 elif(checksumtype
=="crc16_ccitt"):
4093 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4094 elif(checksumtype
=="adler32"):
4095 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4096 elif(checksumtype
=="crc32"):
4097 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4098 elif(checksumtype
=="crc64_ecma"):
4099 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4100 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4101 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4102 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4103 checksumoutstr
= hashlib
.new(checksumtype
);
4104 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4105 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4107 catfileheadercshex
= format(0, 'x').lower();
4108 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4109 fheadtell
= len(fileheader
);
4110 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4111 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
4112 if(re
.findall("^[.|/]", member
.filename
)):
4113 fname
= member
.filename
;
4115 fname
= "./"+member
.filename
;
4116 zipinfo
= zipfp
.getinfo(member
.filename
);
4118 VerbosePrintOut(fname
);
4119 if(not member
.is_dir()):
4120 fpremode
= stat
.S_IFREG
+ 438;
4121 elif(member
.is_dir()):
4122 fpremode
= stat
.S_IFDIR
+ 511;
4125 if(not member
.is_dir()):
4127 elif(member
.is_dir()):
4130 fbasedir
= os
.path
.dirname(fname
);
4134 curfid
= curfid
+ 1;
4142 fsize
= member
.file_size
;
4144 fsize
= member
.file_size
;
4145 fatime
= time
.mktime(member
.date_time
+ (0, 0, -1));
4146 fmtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
4147 fctime
= time
.mktime(member
.date_time
+ (0, 0, -1));
4148 fbtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
4149 if(not member
.is_dir()):
4150 fmode
= stat
.S_IFREG
+ 438;
4151 fchmode
= stat
.S_IMODE(int(stat
.S_IFREG
+ 438));
4152 ftypemod
= stat
.S_IFMT(int(stat
.S_IFREG
+ 438));
4153 if(member
.is_dir()):
4154 fmode
= stat
.S_IFDIR
+ 511;
4155 fchmode
= stat
.S_IMODE(int(stat
.S_IFDIR
+ 511));
4156 ftypemod
= stat
.S_IFMT(int(stat
.S_IFDIR
+ 511));
4159 except AttributeError:
4165 except AttributeError:
4172 userinfo
= pwd
.getpwuid(os
.getuid());
4173 funame
= userinfo
.pw_name
;
4176 except AttributeError:
4184 groupinfo
= grp
.getgrgid(os
.getgid());
4185 fgname
= groupinfo
.gr_name
;
4188 except AttributeError:
4192 fcontents
= "".encode('UTF-8');
4194 fcontents
= zipfp
.read(member
.filename
);
4195 ftypehex
= format(ftype
, 'x').lower();
4196 extrafields
= len(extradata
);
4197 extrafieldslist
= extradata
;
4198 catfextrafields
= extrafields
;
4199 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
4200 if(len(extradata
)>0):
4201 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4202 extrasizelen
= len(extrasizestr
);
4203 extrasizelenhex
= format(extrasizelen
, 'x').lower();
4204 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]);
4205 if(len(extradata
)>0):
4206 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4207 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
4208 catfnumfields
= 24 + catfextrafields
;
4209 if(checksumtype
=="none" or checksumtype
==""):
4210 catfileheadercshex
= format(0, 'x').lower();
4211 catfilecontentcshex
= format(0, 'x').lower();
4212 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4213 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
4214 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
4215 elif(checksumtype
=="crc16_ccitt"):
4216 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
4217 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
4218 elif(checksumtype
=="adler32"):
4219 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4220 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
4221 elif(checksumtype
=="crc32"):
4222 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4223 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
4224 elif(checksumtype
=="crc64_ecma"):
4225 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4226 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
4227 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4228 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4229 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
4230 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4231 checksumoutstr
= hashlib
.new(checksumtype
);
4232 checksumoutstr
.update("".encode('UTF-8'));
4233 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4234 checksumoutstr
= hashlib
.new(checksumtype
);
4235 checksumoutstr
.update(fcontents
);
4236 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
4238 catfileheadercshex
= format(0, 'x').lower();
4239 catfilecontentcshex
= format(0, 'x').lower();
4240 catfhstart
= fheadtell
;
4241 fheadtell
+= len(catfileoutstr
);
4242 catfhend
= fheadtell
- 1;
4243 catfcontentstart
= fheadtell
;
4244 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4245 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
4246 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
4247 if(checksumtype
=="none" or checksumtype
==""):
4248 catfileheadercshex
= format(0, 'x').lower();
4249 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4250 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4251 elif(checksumtype
=="crc16_ccitt"):
4252 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4253 elif(checksumtype
=="adler32"):
4254 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4255 elif(checksumtype
=="crc32"):
4256 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4257 elif(checksumtype
=="crc64_ecma"):
4258 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4259 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4260 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4261 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4262 checksumoutstr
= hashlib
.new(checksumtype
);
4263 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
4264 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4266 catfileheadercshex
= format(0, 'x').lower();
4267 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4268 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
4269 nullstrecd
= formatspecs
[5].encode('UTF-8');
4270 fheadtell
+= len(catfileoutstr
) + 1;
4271 catfcontentend
= fheadtell
- 1;
4272 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
4273 pyhascontents
= False;
4274 if(int(fsize
)>0 and not listonly
):
4275 pyhascontents
= True;
4276 if(int(fsize
)>0 and listonly
):
4278 pyhascontents
= False;
4279 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
} });
4280 fileidnum
= fileidnum
+ 1;
4283 if(not rarfile_support
):
4284 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4287 if(rarfile_support
):
4288 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4289 advancedlist
= True;
4295 inodetocatinode
= {};
4297 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
4299 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
4301 rarfp
= rarfile
.RarFile(infile
, "r");
4302 rartest
= rarfp
.testrar();
4304 VerbosePrintOut("Bad file found!");
4305 fnumfiles
= int(len(rarfp
.infolist()));
4306 catver
= formatspecs
[6];
4307 fileheaderver
= str(int(catver
.replace(".", "")));
4308 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4309 catversion
= re
.findall(r
"([\d]+)$", fileheader
);
4310 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
4311 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4312 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4313 if(checksumtype
=="none" or checksumtype
==""):
4314 catfileheadercshex
= format(0, 'x').lower();
4315 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4316 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4317 elif(checksumtype
=="crc16_ccitt"):
4318 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
4319 elif(checksumtype
=="adler32"):
4320 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4321 elif(checksumtype
=="crc32"):
4322 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4323 elif(checksumtype
=="crc64_ecma"):
4324 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4325 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4326 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4327 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4328 checksumoutstr
= hashlib
.new(checksumtype
);
4329 checksumoutstr
.update(fileheader
.encode('UTF-8'));
4330 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4332 catfileheadercshex
= format(0, 'x').lower();
4333 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4334 fheadtell
= len(fileheader
);
4335 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
4336 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
4339 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
4342 member
.external_attr
4344 except AttributeError:
4346 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
4349 member
.external_attr
4351 except AttributeError:
4356 if(re
.findall("^[.|/]", member
.filename
)):
4357 fname
= member
.filename
;
4359 fname
= "./"+member
.filename
;
4360 rarinfo
= rarfp
.getinfo(member
.filename
);
4362 VerbosePrintOut(fname
);
4363 if(is_unix
and member
.external_attr
!=0):
4364 fpremode
= int(member
.external_attr
);
4365 elif(member
.is_file()):
4366 fpremode
= stat
.S_IFREG
+ 438;
4367 elif(member
.is_symlink()):
4368 fpremode
= stat
.S_IFLNK
+ 438;
4369 elif(member
.is_dir()):
4370 fpremode
= stat
.S_IFDIR
+ 511;
4371 if(is_windows
and member
.external_attr
!=0):
4372 fwinattributes
= int(member
.external_attr
);
4374 fwinattributes
= int(0);
4377 if(member
.is_file()):
4379 elif(member
.is_symlink()):
4381 elif(member
.is_dir()):
4385 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
4386 fbasedir
= os
.path
.dirname(fname
);
4390 curfid
= curfid
+ 1;
4398 fsize
= member
.file_size
;
4401 fatime
= int(member
.atime
.timestamp());
4403 fatime
= int(member
.mtime
.timestamp());
4404 except AttributeError:
4405 fatime
= int(member
.mtime
.timestamp());
4406 fmtime
= int(member
.mtime
.timestamp());
4409 fctime
= int(member
.ctime
.timestamp());
4411 fctime
= int(member
.mtime
.timestamp());
4412 except AttributeError:
4413 fctime
= int(member
.mtime
.timestamp());
4414 fbtime
= int(member
.mtime
.timestamp());
4415 if(is_unix
and member
.external_attr
!=0):
4416 fmode
= format(int(member
.external_attr
), 'x').lower();
4417 fchmode
= format(int(stat
.S_IMODE(member
.external_attr
)), 'x').lower();
4418 ftypemod
= format(int(stat
.S_IFMT(member
.external_attr
)), 'x').lower();
4419 elif(member
.is_file()):
4420 fmode
= int(stat
.S_IFREG
+ 438)
4421 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
4422 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
4423 elif(member
.is_symlink()):
4424 fmode
= int(stat
.S_IFLNK
+ 438)
4425 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
4426 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
4427 elif(member
.is_dir()):
4428 fmode
= int(stat
.S_IFDIR
+ 511)
4429 fchmode
= int(stat
.S_IMODE(stat
.S_IFDIR
+ 511));
4430 ftypemod
= int(stat
.S_IFMT(stat
.S_IFDIR
+ 511));
4433 except AttributeError:
4439 except AttributeError:
4446 userinfo
= pwd
.getpwuid(os
.getuid());
4447 funame
= userinfo
.pw_name
;
4450 except AttributeError:
4458 groupinfo
= grp
.getgrgid(os
.getgid());
4459 fgname
= groupinfo
.gr_name
;
4462 except AttributeError:
4466 fcontents
= "".encode('UTF-8');
4468 fcontents
= rarfp
.read(member
.filename
);
4469 ftypehex
= format(ftype
, 'x').lower();
4470 extrafields
= len(extradata
);
4471 extrafieldslist
= extradata
;
4472 catfextrafields
= extrafields
;
4473 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
4474 if(len(extradata
)>0):
4475 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4476 extrasizelen
= len(extrasizestr
);
4477 extrasizelenhex
= format(extrasizelen
, 'x').lower();
4478 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]);
4479 if(len(extradata
)>0):
4480 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[5]);
4481 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[5]);
4482 catfnumfields
= 24 + catfextrafields
;
4483 if(checksumtype
=="none" or checksumtype
==""):
4484 catfileheadercshex
= format(0, 'x').lower();
4485 catfilecontentcshex
= format(0, 'x').lower();
4486 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4487 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4488 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
4489 elif(checksumtype
=="crc16_ccitt"):
4490 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4491 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
4492 elif(checksumtype
=="adler32"):
4493 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4494 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
4495 elif(checksumtype
=="crc32"):
4496 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4497 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
4498 elif(checksumtype
=="crc64_ecma"):
4499 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4500 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
4501 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4502 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4503 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
4504 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4505 checksumoutstr
= hashlib
.new(checksumtype
);
4506 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
4507 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4509 catfileheadercshex
= format(0, 'x').lower();
4510 catfilecontentcshex
= format(0, 'x').lower();
4511 catfhstart
= fheadtell
;
4512 fheadtell
+= len(catfileoutstr
);
4513 catfhend
= fheadtell
- 1;
4514 catfcontentstart
= fheadtell
;
4515 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4516 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
4517 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
4518 if(checksumtype
=="none" or checksumtype
==""):
4519 catfileheadercshex
= format(0, 'x').lower();
4520 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4521 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4522 elif(checksumtype
=="crc16_ccitt"):
4523 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4524 elif(checksumtype
=="adler32"):
4525 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4526 elif(checksumtype
=="crc32"):
4527 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4528 elif(checksumtype
=="crc64_ecma"):
4529 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4530 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4531 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4532 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4533 checksumoutstr
= hashlib
.new(checksumtype
);
4534 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
4535 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4537 catfileheadercshex
= format(0, 'x').lower();
4538 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
4539 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
4540 nullstrecd
= formatspecs
[5].encode('UTF-8');
4541 fheadtell
+= len(catfileoutstr
) + 1;
4542 catfcontentend
= fheadtell
- 1;
4543 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
4544 pyhascontents
= False;
4545 if(int(fsize
)>0 and not listonly
):
4546 pyhascontents
= True;
4547 if(int(fsize
)>0 and listonly
):
4549 pyhascontents
= False;
4550 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
} });
4551 fileidnum
= fileidnum
+ 1;
4554 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):
4555 outarray
= BytesIO();
4556 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
4557 listcatfiles
= ArchiveFileToArray(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4558 return listcatfiles
;
4560 def ArchiveFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4561 if(isinstance(infile
, dict)):
4562 listcatfiles
= infile
;
4564 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
4565 infile
= RemoveWindowsPath(infile
);
4566 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4567 if(not listcatfiles
):
4569 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': {}}}};
4571 catarray
.update({'catfp': listcatfiles
['catfp']});
4572 lenlist
= len(listcatfiles
['ffilelist']);
4577 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4580 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4581 lcfx
= listcatfiles
['fnumfiles'];
4583 lcfx
= int(listcatfiles
['fnumfiles']);
4585 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
4586 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
4587 catarray
['filetoid'].update(filetoidarray
);
4588 catarray
['idtofile'].update(idtofilearray
);
4589 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4590 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
4591 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
4592 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4593 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
4594 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
4595 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4596 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4597 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4598 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4599 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4600 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4601 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4602 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
4603 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
4604 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
4605 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4606 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4607 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
4608 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
4609 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
4610 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4611 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4612 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4613 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
4614 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
4615 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
4616 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4617 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4618 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4619 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4623 create_alias_function("", __file_format_name__
, "ToArrayIndex", ArchiveFileToArrayIndex
);
4625 def ListDirToArrayIndexAlt(infiles
, dirlistfromtxt
=False, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4626 listcatfiles
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
4627 if(not listcatfiles
):
4629 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': {}}}};
4630 lenlist
= len(listcatfiles
['ffilelist']);
4635 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4638 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4639 lcfx
= listcatfiles
['fnumfiles'];
4641 lcfx
= int(listcatfiles
['fnumfiles']);
4643 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
4644 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
4645 catarray
['filetoid'].update(filetoidarray
);
4646 catarray
['idtofile'].update(idtofilearray
);
4647 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4648 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
4649 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
4650 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4651 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
4652 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
4653 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4654 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4655 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4656 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4657 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4658 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4659 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4660 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
4661 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
4662 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
4663 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4664 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4665 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
4666 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
4667 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
4668 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4669 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4670 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4671 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
4672 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
4673 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
4674 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4675 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4676 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4677 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4681 def TarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4682 listcatfiles
= TarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
4683 if(not listcatfiles
):
4685 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': {}}}};
4686 lenlist
= len(listcatfiles
['ffilelist']);
4691 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4694 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4695 lcfx
= listcatfiles
['fnumfiles'];
4697 lcfx
= int(listcatfiles
['fnumfiles']);
4699 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
4700 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
4701 catarray
['filetoid'].update(filetoidarray
);
4702 catarray
['idtofile'].update(idtofilearray
);
4703 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4704 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
4705 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
4706 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4707 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
4708 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
4709 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4710 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4711 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4712 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4713 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4714 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4715 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4716 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
4717 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
4718 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
4719 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4720 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4721 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
4722 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
4723 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
4724 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4725 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4726 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4727 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
4728 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
4729 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
4730 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4731 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4732 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4733 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4737 def ZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4738 listcatfiles
= ZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
4739 if(not listcatfiles
):
4741 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': {}}}};
4742 lenlist
= len(listcatfiles
['ffilelist']);
4747 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4750 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4751 lcfx
= listcatfiles
['fnumfiles'];
4753 lcfx
= int(listcatfiles
['fnumfiles']);
4755 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
4756 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
4757 catarray
['filetoid'].update(filetoidarray
);
4758 catarray
['idtofile'].update(idtofilearray
);
4759 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4760 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
4761 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
4762 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4763 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
4764 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
4765 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4766 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4767 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4768 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4769 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4770 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4771 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4772 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
4773 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
4774 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
4775 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4776 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4777 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
4778 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
4779 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
4780 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4781 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4782 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4783 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
4784 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
4785 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
4786 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4787 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4788 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4789 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4793 if(not rarfile_support
):
4794 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4797 if(rarfile_support
):
4798 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4799 listcatfiles
= RarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
4800 if(not listcatfiles
):
4802 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': {}}}};
4803 lenlist
= len(listcatfiles
['ffilelist']);
4808 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4811 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4812 lcfx
= listcatfiles
['fnumfiles'];
4814 lcfx
= int(listcatfiles
['fnumfiles']);
4816 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
4817 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
4818 catarray
['filetoid'].update(filetoidarray
);
4819 catarray
['idtofile'].update(idtofilearray
);
4820 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4821 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
4822 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
4823 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4824 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
4825 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
4826 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4827 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4828 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4829 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4830 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4831 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4832 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4833 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
4834 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
4835 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
4836 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4837 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4838 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
4839 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
4840 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
4841 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4842 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4843 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4844 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
4845 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
4846 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
4847 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4848 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4849 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4850 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4854 def ArchiveFileStringToArrayIndex(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4855 catfp
= BytesIO(catstr
);
4856 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4857 return listcatfiles
;
4859 create_alias_function("", __file_format_name__
, "StringToArrayIndex", ArchiveFileStringToArrayIndex
);
4861 def TarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4863 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4864 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4865 return listcatfiles
;
4867 def ZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4869 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4870 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4871 return listcatfiles
;
4873 if(not rarfile_support
):
4874 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4877 if(rarfile_support
):
4878 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4880 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4881 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4882 return listcatfiles
;
4884 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):
4885 outarray
= BytesIO();
4886 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, verbose
, True);
4887 listcatfiles
= ArchiveFileToArrayIndex(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
)
4888 return listcatfiles
;
4890 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):
4891 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
4892 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzop', 'lzo', 'lzma', 'xz'];
4893 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', 'lzop', '.lzo', '.lzma', '.xz'];
4894 if(isinstance(infile
, dict)):
4895 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
4896 listcatfiles
= prelistcatfiles
['list'];
4898 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
4899 infile
= RemoveWindowsPath(infile
);
4901 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
4902 listcatfiles
= prelistcatfiles
['list'];
4904 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
4905 if(outfile
!="-" and not hasattr(infile
, "read") and not hasattr(outfile
, "write")):
4906 outfile
= RemoveWindowsPath(outfile
);
4907 checksumtype
= checksumtype
.lower();
4908 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
4909 checksumtype
="crc32";
4910 if(checksumtype
=="none"):
4912 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[2]):
4914 if(compression
not in compressionlist
and compression
is None):
4915 compression
= "auto";
4917 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
4918 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
4919 if(os
.path
.exists(outfile
)):
4921 if(not listcatfiles
):
4926 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
4928 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
4930 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
4933 fbasename
= os
.path
.splitext(outfile
)[0];
4934 fextname
= os
.path
.splitext(outfile
)[1];
4935 catfp
= CompressOpenFile(outfile
, compressionlevel
);
4936 catver
= formatspecs
[6];
4937 fileheaderver
= str(int(catver
.replace(".", "")));
4938 fileheader
= AppendNullByte(formatspecs
[1] + fileheaderver
, formatspecs
[5]);
4939 catfp
.write(fileheader
.encode('UTF-8'));
4940 lenlist
= len(listcatfiles
['ffilelist']);
4941 fnumfiles
= int(listcatfiles
['fnumfiles']);
4942 if(lenlist
>fnumfiles
or lenlist
<fnumfiles
):
4943 fnumfiles
= lenlist
;
4944 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4945 fnumfilesa
= AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[5]);
4946 if(checksumtype
=="none" or checksumtype
==""):
4947 catfileheadercshex
= format(0, 'x').lower();
4948 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4949 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
4950 elif(checksumtype
=="crc16_ccitt"):
4951 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
4952 elif(checksumtype
=="adler32"):
4953 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
4954 elif(checksumtype
=="crc32"):
4955 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
4956 elif(checksumtype
=="crc64_ecma"):
4957 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4958 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4959 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4960 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4961 checksumoutstr
= hashlib
.new(checksumtype
);
4962 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
4963 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4965 catfileheadercshex
= format(0, 'x').lower();
4966 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[5]);
4967 catfp
.write(fnumfilesa
.encode('UTF-8'));
4970 os
.fsync(catfp
.fileno());
4971 except io
.UnsupportedOperation
:
4973 except AttributeError:
4979 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4982 lcfx
= int(listcatfiles
['fnumfiles']);
4990 catfhstart
= catfp
.tell();
4991 if(re
.findall("^[.|/]", listcatfiles
['ffilelist'][reallcfi
]['fname'])):
4992 fname
= listcatfiles
['ffilelist'][reallcfi
]['fname'];
4994 fname
= "./"+listcatfiles
['ffilelist'][reallcfi
]['fname'];
4996 VerbosePrintOut(fname
);
4997 fheadersize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fheadersize']), 'x').lower();
4998 fsize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fsize']), 'x').lower();
4999 flinkname
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
5000 fatime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fatime']), 'x').lower();
5001 fmtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmtime']), 'x').lower();
5002 fctime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fctime']), 'x').lower();
5003 fbtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fbtime']), 'x').lower();
5004 fmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmode']), 'x').lower();
5005 fchmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fchmode']), 'x').lower();
5006 fuid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fuid']), 'x').lower();
5007 funame
= listcatfiles
['ffilelist'][reallcfi
]['funame'];
5008 fgid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fgid']), 'x').lower();
5009 fgname
= listcatfiles
['ffilelist'][reallcfi
]['fgname'];
5010 finode
= listcatfiles
['ffilelist'][reallcfi
]['finode'];
5011 flinkcount
= listcatfiles
['ffilelist'][reallcfi
]['flinkcount'];
5012 fdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fminor']), 'x').lower();
5013 fdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmajor']), 'x').lower();
5014 frdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frminor']), 'x').lower();
5015 frdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frmajor']), 'x').lower();
5016 if(len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] and len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>0):
5017 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
5018 if(len(extradata
) > 0):
5019 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(extradata
);
5020 listcatfiles
['ffilelist'][reallcfi
]['fextralist'] = extradata
;
5021 extrafields
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']), 'x').lower();
5022 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5023 if(len(extradata
)>0):
5024 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5025 extrasizelen
= format(len(extrasizestr
), 'x').lower();
5026 fcontents
= listcatfiles
['ffilelist'][reallcfi
]['fcontents'];
5028 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']==1 or listcatfiles
['ffilelist'][reallcfi
]['ftype']==2):
5029 getflinkpath
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
5030 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
5031 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
5032 fheadersize
= format(int(flinkinfo
['fheadersize']), 'x').lower();
5033 fsize
= format(int(flinkinfo
['fsize']), 'x').lower();
5034 flinkname
= flinkinfo
['flinkname'];
5035 fatime
= format(int(flinkinfo
['fatime']), 'x').lower();
5036 fmtime
= format(int(flinkinfo
['fmtime']), 'x').lower();
5037 fctime
= format(int(flinkinfo
['fctime']), 'x').lower();
5038 fbtime
= format(int(flinkinfo
['fbtime']), 'x').lower();
5039 fmode
= format(int(flinkinfo
['fmode']), 'x').lower();
5040 fchmode
= format(int(flinkinfo
['fchmode']), 'x').lower();
5041 fuid
= format(int(flinkinfo
['fuid']), 'x').lower();
5042 funame
= flinkinfo
['funame'];
5043 fgid
= format(int(flinkinfo
['fgid']), 'x').lower();
5044 fgname
= flinkinfo
['fgname'];
5045 finode
= flinkinfo
['finode'];
5046 flinkcount
= flinkinfo
['flinkcount'];
5047 fdev_minor
= format(int(flinkinfo
['fminor']), 'x').lower();
5048 fdev_major
= format(int(flinkinfo
['fmajor']), 'x').lower();
5049 frdev_minor
= format(int(flinkinfo
['frminor']), 'x').lower();
5050 frdev_major
= format(int(flinkinfo
['frmajor']), 'x').lower();
5051 if(len(flinkinfo
['fextralist'])>flinkinfo
['fextrafields'] and len(flinkinfo
['fextralist'])>0):
5052 flinkinfo
['fextrafields'] = len(flinkinfo
['fextralist']);
5053 if(len(extradata
) > 0):
5054 flinkinfo
['fextrafields'] = len(extradata
);
5055 flinkinfo
['fextralist'] = extradata
;
5056 extrafields
= format(int(flinkinfo
['fextrafields']), 'x').lower();
5057 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[5]);
5058 if(len(extradata
)>0):
5059 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[5]);
5060 extrasizelen
= format(len(extrasizestr
), 'x').lower();
5061 fcontents
= flinkinfo
['fcontents'];
5062 if(flinkinfo
['ftype']!=0 and flinkinfo
['ftype']!=7):
5064 fcontents
= fcontents
.encode('UTF-8');
5065 except AttributeError:
5067 ftypehex
= format(flinkinfo
['ftype'], 'x').lower();
5069 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=0 and listcatfiles
['ffilelist'][reallcfi
]['ftype']!=7):
5071 fcontents
= fcontents
.encode('UTF-8');
5072 except AttributeError:
5074 ftypehex
= format(listcatfiles
['ffilelist'][reallcfi
]['ftype'], 'x').lower();
5075 fcurfid
= format(curfid
, 'x').lower();
5076 if(not followlink
and finode
!=0):
5077 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=1):
5078 fcurinode
= format(int(curinode
), 'x').lower();
5079 inodetofile
.update({curinode
: fname
});
5080 filetoinode
.update({fname
: curinode
});
5081 curinode
= curinode
+ 1;
5083 fcurinode
= format(int(filetoinode
[flinkname
]), 'x').lower();
5085 fcurinode
= format(int(curinode
), 'x').lower();
5086 curinode
= curinode
+ 1;
5087 curfid
= curfid
+ 1;
5088 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]);
5089 if(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']>0):
5090 extrafieldslist
= [];
5092 exil
= listcatfiles
['ffilelist'][reallcfi
]['fextrafields'];
5094 extrafieldslist
.append(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
5096 catfileoutstr
+= AppendNullBytes([extrafieldslist
], formatspecs
[5]);
5097 catfileoutstr
+= AppendNullBytes([checksumtype
], formatspecs
[5]);
5098 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
5099 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
5100 if(checksumtype
=="none" or checksumtype
==""):
5101 catfileheadercshex
= format(0, 'x').lower();
5102 catfilecontentcshex
= format(0, 'x').lower();
5103 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5104 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5105 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
5106 elif(checksumtype
=="crc16_ccitt"):
5107 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5108 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
5109 elif(checksumtype
=="adler32"):
5110 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5111 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
5112 elif(checksumtype
=="crc32"):
5113 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5114 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
5115 elif(checksumtype
=="crc64_ecma"):
5116 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5117 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
5118 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5119 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5120 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
5121 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5122 checksumoutstr
= hashlib
.new(checksumtype
);
5123 checksumoutstr
.update("".encode('UTF-8'));
5124 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5125 checksumoutstr
= hashlib
.new(checksumtype
);
5126 checksumoutstr
.update(fcontents
);
5127 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
5129 catfileheadercshex
= format(0, 'x').lower();
5130 catfilecontentcshex
= format(0, 'x').lower();
5131 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5132 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
5133 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[5]) + catfileoutstr
;
5134 if(checksumtype
=="none" or checksumtype
==""):
5135 catfileheadercshex
= format(0, 'x').lower();
5136 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
5137 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5138 elif(checksumtype
=="crc16_ccitt"):
5139 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
5140 elif(checksumtype
=="adler32"):
5141 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5142 elif(checksumtype
=="crc32"):
5143 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
5144 elif(checksumtype
=="crc64_ecma"):
5145 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5146 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
5147 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5148 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
5149 checksumoutstr
= hashlib
.new(checksumtype
);
5150 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
5151 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
5153 catfileheadercshex
= format(0, 'x').lower();
5154 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[5]);
5155 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
5156 nullstrecd
= formatspecs
[5].encode('UTF-8');
5157 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
5158 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
5159 catfp
.write(catfileout
);
5162 os
.fsync(catfp
.fileno());
5163 except io
.UnsupportedOperation
:
5165 except AttributeError:
5168 reallcfi
= reallcfi
+ 1;
5169 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
5170 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
5173 os
.fsync(catfp
.fileno());
5174 except io
.UnsupportedOperation
:
5176 except AttributeError:
5180 if(hasattr(sys
.stdout
, "buffer")):
5181 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
5183 shutil
.copyfileobj(catfp
, sys
.stdout
);
5184 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
5185 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
5187 upload_file_to_ftp_file(catfp
, outfile
);
5188 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
5189 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
5192 upload_file_to_pysftp_file(catfp
, outfile
);
5194 upload_file_to_sftp_file(catfp
, outfile
);
5202 create_alias_function("RePack", __file_format_name__
, "", RePackArchiveFile
);
5204 def RePackArchiveFileFromString(catstr
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5205 catfp
= BytesIO(catstr
);
5206 listcatfiles
= RePackArchiveFile(catfp
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
5207 return listcatfiles
;
5209 create_alias_function("RePack", __file_format_name__
, "FromString", RePackArchiveFileFromString
);
5211 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):
5212 outarray
= BytesIO();
5213 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
5214 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
5215 return listcatfiles
;
5217 create_alias_function("Pack", __file_format_name__
, "FromListDir", PackArchiveFileFromListDir
);
5219 def ArchiveFileArrayBase64Encode(infile
, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5221 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5222 if(isinstance(infile
, dict)):
5223 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5224 listcatfiles
= prelistcatfiles
['list'];
5226 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5227 infile
= RemoveWindowsPath(infile
);
5229 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5230 listcatfiles
= prelistcatfiles
['list'];
5232 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5233 if(not listcatfiles
):
5235 lenlist
= len(listcatfiles
['ffilelist']);
5240 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
5243 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5244 lcfx
= listcatfiles
['fnumfiles'];
5246 lcfx
= int(listcatfiles
['fnumfiles']);
5247 if(lenlist
>lcfx
or lenlist
<lcfx
):
5250 if(listcatfiles
['ffilelist'][lcfi
]['fhascontents']):
5251 listcatfiles
['ffilelist'][lcfi
]['fcontents'] = base64
.b64encode(listcatfiles
['ffilelist'][lcfi
]['fcontents']).decode("UTF-8");
5253 return listcatfiles
;
5255 create_alias_function("", __file_format_name__
, "ArrayBase64Encode", ArchiveFileArrayBase64Encode
);
5257 def ArchiveFileArrayBase64Decode(infile
, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5259 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5260 if(isinstance(infile
, dict)):
5261 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5262 listcatfiles
= prelistcatfiles
['list'];
5264 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5265 infile
= RemoveWindowsPath(infile
);
5267 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5268 listcatfiles
= prelistcatfiles
['list'];
5270 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5271 if(not listcatfiles
):
5273 lenlist
= len(listcatfiles
['ffilelist']);
5278 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
5281 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5282 lcfx
= listcatfiles
['fnumfiles'];
5284 lcfx
= int(listcatfiles
['fnumfiles']);
5285 if(lenlist
>lcfx
or lenlist
<lcfx
):
5288 if(listcatfiles
['ffilelist'][lcfi
]['fhascontents']):
5289 listcatfiles
['ffilelist'][lcfi
]['fcontents'] = base64
.b64decode(listcatfiles
['ffilelist'][lcfi
]['fcontents'].encode("UTF-8"));
5291 return listcatfiles
;
5293 create_alias_function("", __file_format_name__
, "ArrayBase64Decode", ArchiveFileArrayBase64Decode
);
5295 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):
5296 if(outdir
is not None):
5297 outdir
= RemoveWindowsPath(outdir
);
5299 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5300 if(isinstance(infile
, dict)):
5301 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5302 listcatfiles
= prelistcatfiles
['list'];
5304 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5305 infile
= RemoveWindowsPath(infile
);
5307 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5308 listcatfiles
= prelistcatfiles
['list'];
5310 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
5311 if(not listcatfiles
):
5313 lenlist
= len(listcatfiles
['ffilelist']);
5318 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
5321 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
5322 lcfx
= listcatfiles
['fnumfiles'];
5324 lcfx
= int(listcatfiles
['fnumfiles']);
5325 if(lenlist
>lcfx
or lenlist
<lcfx
):
5332 userinfo
= pwd
.getpwuid(listcatfiles
['ffilelist'][lcfi
]['fuid']);
5333 funame
= userinfo
.pw_name
;
5342 groupinfo
= grp
.getgrgid(listcatfiles
['ffilelist'][lcfi
]['fgid']);
5343 fgname
= groupinfo
.gr_name
;
5349 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
5350 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
5351 with
open(listcatfiles
['ffilelist'][lcfi
]['fname'], "wb") as fpc
:
5352 fpc
.write(listcatfiles
['ffilelist'][lcfi
]['fcontents'])
5355 os
.fsync(fpc
.fileno())
5356 except io
.UnsupportedOperation
:
5358 except AttributeError:
5360 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
5361 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
5362 if(preservepermissions
):
5363 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fchmode']);
5365 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
5366 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5368 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
5369 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
5370 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
5375 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
5376 funame
= userinfo
.pw_name
;
5385 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
5386 fgname
= groupinfo
.gr_name
;
5391 if(flinkinfo
['ftype'] == 0 or flinkinfo
['ftype'] == 7):
5392 with
open(listcatfiles
['ffilelist'][lcfi
]['fname'], "wb") as fpc
:
5393 fpc
.write(flinkinfo
['fcontents'])
5396 os
.fsync(fpc
.fileno())
5397 except io
.UnsupportedOperation
:
5399 except AttributeError:
5401 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
5402 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
5403 if(preservepermissions
):
5404 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5406 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
5407 if(flinkinfo
['ftype']==1):
5408 os
.link(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
5409 if(flinkinfo
['ftype']==2):
5410 os
.symlink(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
5411 if(flinkinfo
['ftype']==5):
5412 if(preservepermissions
):
5413 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5415 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname']);
5416 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
5417 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
5418 if(preservepermissions
):
5419 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5421 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
5422 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
5423 os
.mkfifo(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5425 os
.link(listcatfiles
['ffilelist'][lcfi
]['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
5426 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5428 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
5429 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
5430 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
5435 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
5436 funame
= userinfo
.pw_name
;
5445 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
5446 fgname
= groupinfo
.gr_name
;
5451 if(flinkinfo
['ftype']==0 or flinkinfo
['ftype']==7):
5452 with
open(listcatfiles
['ffilelist'][lcfi
]['fname'], "wb") as fpc
:
5453 fpc
.write(flinkinfo
['fcontents'])
5456 os
.fsync(fpc
.fileno())
5457 except io
.UnsupportedOperation
:
5459 except AttributeError:
5461 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
5462 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
5463 if(preservepermissions
):
5464 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5466 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
5467 if(flinkinfo
['ftype']==1):
5468 os
.link(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
5469 if(flinkinfo
['ftype']==2):
5470 os
.symlink(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
5471 if(flinkinfo
['ftype']==5):
5472 if(preservepermissions
):
5473 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5475 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname']);
5476 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname'] and preservepermissions
):
5477 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
5478 if(preservepermissions
):
5479 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5481 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
5482 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
5483 os
.mkfifo(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fchmode']);
5485 os
.symlink(listcatfiles
['ffilelist'][lcfi
]['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
5486 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
5487 if(preservepermissions
):
5488 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fchmode']);
5490 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname']);
5491 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname'] and preservepermissions
):
5492 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
5493 if(preservepermissions
):
5494 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fchmode']);
5496 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
5497 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6 and hasattr(os
, "mkfifo")):
5498 os
.mkfifo(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fchmode']);
5501 return listcatfiles
['ffilelist']['catfp'];
5505 create_alias_function("UnPack", __file_format_name__
, "", UnPackArchiveFile
);
5507 if(hasattr(shutil
, "register_unpack_format")):
5508 def UnPackArchiveFileFunc(archive_name
, extract_dir
=None, **kwargs
):
5509 return UnPackArchiveFile(archive_name
, extract_dir
, False, 0, 0, False, __file_format_delimiter__
, False, False);
5510 create_alias_function("UnPack", __file_format_name__
, "Func", UnPackArchiveFileFunc
);
5512 def UnPackArchiveFileString(catstr
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5513 catfp
= BytesIO(catstr
);
5514 listcatfiles
= UnPackArchiveFile(catfp
, outdir
, followlink
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
5515 return listcatfiles
;
5517 create_alias_function("UnPack", __file_format_name__
, "String", UnPackArchiveFileString
);
5519 def ArchiveFileListFiles(infile
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5520 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5521 if(isinstance(infile
, dict)):
5522 listcatfiles
= infile
;
5524 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
5525 infile
= RemoveWindowsPath(infile
);
5526 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, True, skipchecksum
, formatspecs
, returnfp
);
5527 if(not listcatfiles
):
5529 lenlist
= len(listcatfiles
['ffilelist']);
5534 returnval
.update({lcfi
: listcatfiles
['ffilelist'][lcfi
]['fname']});
5536 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
5538 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' } };
5539 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'];
5540 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5541 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " link to " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
5542 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5543 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " -> " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
5544 fuprint
= listcatfiles
['ffilelist'][lcfi
]['funame'];
5545 if(len(fuprint
)<=0):
5546 fuprint
= listcatfiles
['ffilelist'][lcfi
]['fuid'];
5547 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgname'];
5548 if(len(fgprint
)<=0):
5549 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgid'];
5550 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
));
5553 return listcatfiles
['catfp'];
5557 create_alias_function("", __file_format_name__
, "ListFiles", ArchiveFileListFiles
);
5559 def ArchiveFileStringListFiles(catstr
, followlink
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5560 catfp
= BytesIO(catstr
);
5561 listcatfiles
= UnPackArchiveFile(catfp
, None, followlink
, skipchecksum
, formatspecs
, verbose
, returnfp
);
5562 return listcatfiles
;
5564 create_alias_function("", __file_format_name__
, "StringListFiles", ArchiveFileListFiles
);
5566 def TarFileListFiles(infile
, verbose
=False, returnfp
=False):
5567 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5570 if(hasattr(sys
.stdin
, "buffer")):
5571 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
5573 shutil
.copyfileobj(sys
.stdin
, infile
);
5578 elif(re
.findall(r
"^(http|https)\:\/\/", infile
)):
5579 infile
= download_file_from_http_file(infile
);
5584 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", infile
)):
5585 infile
= download_file_from_ftp_file(infile
);
5590 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5592 elif(os
.path
.exists(infile
) and os
.path
.isfile(infile
)):
5594 if(not tarfile
.is_tarfile(infile
)):
5596 except AttributeError:
5597 if(not is_tarfile(infile
)):
5602 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
5603 tarfp
= tarfile
.open(fileobj
=infile
, mode
="r");
5605 tarfp
= tarfile
.open(infile
, "r");
5606 except FileNotFoundError
:
5608 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
5609 returnval
.update({lcfi
: member
.name
});
5610 fpremode
= member
.mode
;
5611 ffullmode
= member
.mode
;
5615 ffullmode
= member
.mode
+ stat
.S_IFREG
;
5617 elif(member
.isdev()):
5618 ffullmode
= member
.mode
;
5620 elif(member
.islnk()):
5621 ffullmode
= member
.mode
+ stat
.S_IFREG
;
5623 elif(member
.issym()):
5624 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
5626 elif(member
.ischr()):
5627 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
5629 elif(member
.isblk()):
5630 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
5632 elif(member
.isdir()):
5633 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
5635 elif(member
.isfifo()):
5636 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
5638 elif(member
.issparse()):
5639 ffullmode
= member
.mode
;
5642 VerbosePrintOut(member
.name
);
5644 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' } };
5645 printfname
= member
.name
;
5647 printfname
= member
.name
+ " link to " + member
.linkname
;
5648 elif(member
.issym()):
5649 printfname
= member
.name
+ " -> " + member
.linkname
;
5650 fuprint
= member
.uname
;
5651 if(len(fuprint
)<=0):
5652 fuprint
= member
.uid
;
5653 fgprint
= member
.gname
;
5654 if(len(fgprint
)<=0):
5655 fgprint
= member
.gid
;
5656 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
));
5659 return listcatfiles
['catfp'];
5663 def ZipFileListFiles(infile
, verbose
=False, returnfp
=False):
5664 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5667 if(hasattr(sys
.stdin
, "buffer")):
5668 shutil
.copyfileobj(sys
.stdin
.buffer, infile
);
5670 shutil
.copyfileobj(sys
.stdin
, infile
);
5675 elif(re
.findall(r
"^(http|https)\:\/\/", infile
)):
5676 infile
= download_file_from_http_file(infile
);
5681 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", infile
)):
5682 infile
= download_file_from_ftp_file(infile
);
5687 elif(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5691 if(not zipfile
.is_zipfile(infile
)):
5694 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
5695 except FileNotFoundError
:
5699 ziptest
= zipfp
.testzip();
5701 VerbosePrintOut("Bad file found!");
5702 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
5703 if(not member
.is_dir()):
5704 fpremode
= int(stat
.S_IFREG
+ 438);
5705 elif(member
.is_dir()):
5706 fpremode
= int(stat
.S_IFDIR
+ 511);
5707 if(not member
.is_dir()):
5708 fmode
= int(stat
.S_IFREG
+ 438);
5709 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
5710 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
5711 elif(member
.is_dir()):
5712 fmode
= int(stat
.S_IFDIR
+ 511);
5713 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
5714 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
5715 returnval
.update({lcfi
: member
.filename
});
5717 VerbosePrintOut(member
.filename
);
5719 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' } };
5721 for fmodval
in str(oct(fmode
))[-3:]:
5722 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
5723 if(not member
.is_dir()):
5725 permissionstr
= "-" + permissionstr
;
5726 elif(member
.is_dir()):
5728 permissionstr
= "d" + permissionstr
;
5729 printfname
= member
.filename
;
5731 fuid
= int(os
.getuid());
5732 except AttributeError:
5737 fgid
= int(os
.getgid());
5738 except AttributeError:
5745 userinfo
= pwd
.getpwuid(os
.getuid());
5746 funame
= userinfo
.pw_name
;
5749 except AttributeError:
5757 groupinfo
= grp
.getgrgid(os
.getgid());
5758 fgname
= groupinfo
.gr_name
;
5761 except AttributeError:
5766 if(len(fuprint
)<=0):
5767 fuprint
= str(fuid
);
5769 if(len(fgprint
)<=0):
5770 fgprint
= str(fgid
);
5771 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
));
5774 return listcatfiles
['catfp'];
5778 if(not rarfile_support
):
5779 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
5780 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5781 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5784 if(rarfile_support
):
5785 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
5786 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5787 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5789 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
5793 rarfp
= rarfile
.RarFile(infile
, "r");
5794 rartest
= rarfp
.testrar();
5796 VerbosePrintOut("Bad file found!");
5797 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
5800 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
5803 member
.external_attr
5805 except AttributeError:
5807 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
5810 member
.external_attr
5812 except AttributeError:
5817 if(is_unix
and member
.external_attr
!=0):
5818 fpremode
= int(member
.external_attr
);
5819 elif(member
.is_file()):
5820 fpremode
= int(stat
.S_IFREG
+ 438);
5821 elif(member
.is_symlink()):
5822 fpremode
= int(stat
.S_IFLNK
+ 438);
5823 elif(member
.is_dir()):
5824 fpremode
= int(stat
.S_IFDIR
+ 511);
5825 if(is_windows
and member
.external_attr
!=0):
5826 fwinattributes
= int(member
.external_attr
);
5828 fwinattributes
= int(0);
5829 if(is_unix
and member
.external_attr
!=0):
5830 fmode
= int(member
.external_attr
);
5831 fchmode
= int(stat
.S_IMODE(member
.external_attr
));
5832 ftypemod
= int(stat
.S_IFMT(member
.external_attr
));
5833 elif(member
.is_file()):
5834 fmode
= int(stat
.S_IFREG
+ 438);
5835 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
5836 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
5837 elif(member
.is_symlink()):
5838 fmode
= int(stat
.S_IFLNK
+ 438);
5839 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFLNK
+ 438)));
5840 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFLNK
+ 438)));
5841 elif(member
.is_dir()):
5842 fmode
= int(stat
.S_IFDIR
+ 511);
5843 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
5844 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
5845 returnval
.update({lcfi
: member
.filename
});
5847 VerbosePrintOut(member
.filename
);
5849 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' } };
5851 for fmodval
in str(oct(fmode
))[-3:]:
5852 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
5853 if(member
.is_file()):
5855 permissionstr
= "-" + permissionstr
;
5856 printfname
= member
.filename
;
5857 elif(member
.is_symlink()):
5859 permissionstr
= "l" + permissionstr
;
5860 printfname
= member
.name
+ " -> " + member
.read().decode("UTF-8");
5861 elif(member
.is_dir()):
5863 permissionstr
= "d" + permissionstr
;
5864 printfname
= member
.filename
;
5866 fuid
= int(os
.getuid());
5867 except AttributeError:
5872 fgid
= int(os
.getgid());
5873 except AttributeError:
5880 userinfo
= pwd
.getpwuid(os
.getuid());
5881 funame
= userinfo
.pw_name
;
5884 except AttributeError:
5892 groupinfo
= grp
.getgrgid(os
.getgid());
5893 fgname
= groupinfo
.gr_name
;
5896 except AttributeError:
5901 if(len(fuprint
)<=0):
5902 fuprint
= str(fuid
);
5904 if(len(fgprint
)<=0):
5905 fgprint
= str(fgid
);
5906 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(member
.file_size
).rjust(15) + " " + member
.mtime
.strftime('%Y-%m-%d %H:%M') + " " + printfname
));
5909 return listcatfiles
['catfp'];
5913 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):
5914 outarray
= BytesIO();
5915 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, False, True);
5916 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
5917 return listcatfiles
;
5919 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):
5920 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, formatspecs
, verbose
);
5921 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
5922 return listcatfiles
;
5924 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):
5925 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, False, checksumtype
, extradata
, formatspecs
, False);
5926 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, followlink
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
5927 return listcatfiles
;
5929 create_alias_function("Pack", __file_format_name__
, "FromListDirAlt", PackArchiveFileFromListDirAlt
);
5931 def PackArchiveFileFromTarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5932 outarray
= TarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
5933 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
5934 return listcatfiles
;
5936 create_alias_function("Pack", __file_format_name__
, "FromTarFileAlt", PackArchiveFileFromTarFileAlt
);
5938 def PackArchiveFileFromZipFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5939 outarray
= ZipFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
5940 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
5941 return listcatfiles
;
5943 create_alias_function("Pack", __file_format_name__
, "FromZipFileAlt", PackArchiveFileFromZipFileAlt
);
5945 if(not rarfile_support
):
5946 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5949 if(rarfile_support
):
5950 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5951 outarray
= RarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
5952 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
5953 return listcatfiles
;
5955 create_alias_function("Pack", __file_format_name__
, "FromRarFileAlt", PackArchiveFileFromRarFileAlt
);
5957 def download_file_from_ftp_file(url
):
5958 urlparts
= urlparse(url
);
5959 file_name
= os
.path
.basename(urlparts
.path
);
5960 file_dir
= os
.path
.dirname(urlparts
.path
);
5961 if(urlparts
.username
is not None):
5962 ftp_username
= urlparts
.username
;
5964 ftp_username
= "anonymous";
5965 if(urlparts
.password
is not None):
5966 ftp_password
= urlparts
.password
;
5967 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
5968 ftp_password
= "anonymous";
5971 if(urlparts
.scheme
=="ftp"):
5973 elif(urlparts
.scheme
=="ftps"):
5977 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
5979 ftp_port
= urlparts
.port
;
5980 if(urlparts
.port
is None):
5983 ftp
.connect(urlparts
.hostname
, ftp_port
);
5984 except socket
.gaierror
:
5985 log
.info("Error With URL "+url
);
5987 except socket
.timeout
:
5988 log
.info("Error With URL "+url
);
5990 ftp
.login(urlparts
.username
, urlparts
.password
);
5991 if(urlparts
.scheme
=="ftps"):
5993 ftpfile
= BytesIO();
5994 ftp
.retrbinary("RETR "+urlparts
.path
, ftpfile
.write
);
5995 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
6000 def download_file_from_ftp_string(url
):
6001 ftpfile
= download_file_from_ftp_file(url
);
6002 return ftpfile
.read();
6004 def upload_file_to_ftp_file(ftpfile
, url
):
6005 urlparts
= urlparse(url
);
6006 file_name
= os
.path
.basename(urlparts
.path
);
6007 file_dir
= os
.path
.dirname(urlparts
.path
);
6008 if(urlparts
.username
is not None):
6009 ftp_username
= urlparts
.username
;
6011 ftp_username
= "anonymous";
6012 if(urlparts
.password
is not None):
6013 ftp_password
= urlparts
.password
;
6014 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6015 ftp_password
= "anonymous";
6018 if(urlparts
.scheme
=="ftp"):
6020 elif(urlparts
.scheme
=="ftps"):
6024 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6026 ftp_port
= urlparts
.port
;
6027 if(urlparts
.port
is None):
6030 ftp
.connect(urlparts
.hostname
, ftp_port
);
6031 except socket
.gaierror
:
6032 log
.info("Error With URL "+url
);
6034 except socket
.timeout
:
6035 log
.info("Error With URL "+url
);
6037 ftp
.login(urlparts
.username
, urlparts
.password
);
6038 if(urlparts
.scheme
=="ftps"):
6040 ftp
.storbinary("STOR "+urlparts
.path
, ftpfile
);
6045 def upload_file_to_ftp_string(ftpstring
, url
):
6046 ftpfileo
= BytesIO(ftpstring
);
6047 ftpfile
= upload_file_to_ftp_file(ftpfileo
, url
);
6051 def download_file_from_http_file(url
, headers
=geturls_headers_pycatfile_python_alt
):
6052 # Parse the URL to extract username and password if present
6053 parsed_url
= urlparse(url
);
6054 username
= parsed_url
.username
;
6055 password
= parsed_url
.password
;
6056 # Rebuild the URL without the username and password
6057 netloc
= parsed_url
.hostname
;
6059 netloc
+= ':' + str(parsed_url
.port
);
6060 rebuilt_url
= urlunparse((parsed_url
.scheme
, netloc
, parsed_url
.path
, parsed_url
.params
, parsed_url
.query
, parsed_url
.fragment
));
6062 # Use the requests library if available
6063 if username
and password
:
6064 response
= requests
.get(rebuilt_url
, headers
=headers
, auth
=(username
, password
));
6066 response
= requests
.get(rebuilt_url
, headers
=headers
);
6067 # Open a temporary file object and write the data
6068 temp_file
= BytesIO(response
.content
);
6070 # Build a Request object for urllib
6071 request
= Request(rebuilt_url
, headers
=headers
);
6072 # Create an opener object for handling URLs
6073 if username
and password
:
6074 # Create a password manager
6075 password_mgr
= HTTPPasswordMgrWithDefaultRealm();
6076 # Add the username and password
6077 password_mgr
.add_password(None, rebuilt_url
, username
, password
);
6078 # Create an authentication handler using the password manager
6079 auth_handler
= HTTPBasicAuthHandler(password_mgr
);
6080 # Build the opener with the authentication handler
6081 opener
= build_opener(auth_handler
);
6083 opener
= build_opener();
6084 # Open the URL using the custom opener
6085 response
= opener
.open(request
);
6086 data
= response
.read();
6087 # Write the data to a temporary file object
6088 temp_file
= BytesIO(data
);
6089 # Reset file pointer to the start
6091 # Return the temporary file object
6094 def download_file_from_http_string(url
, headers
=geturls_headers_pycatfile_python_alt
):
6095 httpfile
= download_file_from_http_file(url
, headers
);
6096 return ftpfile
.read();
6099 def download_file_from_sftp_file(url
):
6100 urlparts
= urlparse(url
);
6101 file_name
= os
.path
.basename(urlparts
.path
);
6102 file_dir
= os
.path
.dirname(urlparts
.path
);
6103 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6105 sftp_port
= urlparts
.port
;
6106 if(urlparts
.port
is None):
6109 sftp_port
= urlparts
.port
;
6110 if(urlparts
.username
is not None):
6111 sftp_username
= urlparts
.username
;
6113 sftp_username
= "anonymous";
6114 if(urlparts
.password
is not None):
6115 sftp_password
= urlparts
.password
;
6116 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6117 sftp_password
= "anonymous";
6120 if(urlparts
.scheme
!="sftp"):
6122 ssh
= paramiko
.SSHClient();
6123 ssh
.load_system_host_keys();
6124 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
6126 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
6127 except paramiko
.ssh_exception
.SSHException
:
6129 except socket
.gaierror
:
6130 log
.info("Error With URL "+url
);
6132 except socket
.timeout
:
6133 log
.info("Error With URL "+url
);
6135 sftp
= ssh
.open_sftp();
6136 sftpfile
= BytesIO();
6137 sftp
.getfo(urlparts
.path
, sftpfile
);
6140 sftpfile
.seek(0, 0);
6143 def download_file_from_sftp_file(url
):
6147 def download_file_from_sftp_string(url
):
6148 sftpfile
= download_file_from_sftp_file(url
);
6149 return sftpfile
.read();
6151 def download_file_from_ftp_string(url
):
6155 def upload_file_to_sftp_file(sftpfile
, url
):
6156 urlparts
= urlparse(url
);
6157 file_name
= os
.path
.basename(urlparts
.path
);
6158 file_dir
= os
.path
.dirname(urlparts
.path
);
6159 sftp_port
= urlparts
.port
;
6160 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6162 if(urlparts
.port
is None):
6165 sftp_port
= urlparts
.port
;
6166 if(urlparts
.username
is not None):
6167 sftp_username
= urlparts
.username
;
6169 sftp_username
= "anonymous";
6170 if(urlparts
.password
is not None):
6171 sftp_password
= urlparts
.password
;
6172 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6173 sftp_password
= "anonymous";
6176 if(urlparts
.scheme
!="sftp"):
6178 ssh
= paramiko
.SSHClient();
6179 ssh
.load_system_host_keys();
6180 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
6182 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
6183 except paramiko
.ssh_exception
.SSHException
:
6185 except socket
.gaierror
:
6186 log
.info("Error With URL "+url
);
6188 except socket
.timeout
:
6189 log
.info("Error With URL "+url
);
6191 sftp
= ssh
.open_sftp();
6192 sftp
.putfo(sftpfile
, urlparts
.path
);
6195 sftpfile
.seek(0, 0);
6198 def upload_file_to_sftp_file(sftpfile
, url
):
6202 def upload_file_to_sftp_string(sftpstring
, url
):
6203 sftpfileo
= BytesIO(sftpstring
);
6204 sftpfile
= upload_file_to_sftp_files(ftpfileo
, url
);
6208 def upload_file_to_sftp_string(url
):
6212 def download_file_from_pysftp_file(url
):
6213 urlparts
= urlparse(url
);
6214 file_name
= os
.path
.basename(urlparts
.path
);
6215 file_dir
= os
.path
.dirname(urlparts
.path
);
6216 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6218 sftp_port
= urlparts
.port
;
6219 if(urlparts
.port
is None):
6222 sftp_port
= urlparts
.port
;
6223 if(urlparts
.username
is not None):
6224 sftp_username
= urlparts
.username
;
6226 sftp_username
= "anonymous";
6227 if(urlparts
.password
is not None):
6228 sftp_password
= urlparts
.password
;
6229 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6230 sftp_password
= "anonymous";
6233 if(urlparts
.scheme
!="sftp"):
6236 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
6237 except paramiko
.ssh_exception
.SSHException
:
6239 except socket
.gaierror
:
6240 log
.info("Error With URL "+url
);
6242 except socket
.timeout
:
6243 log
.info("Error With URL "+url
);
6245 sftp
= ssh
.open_sftp();
6246 sftpfile
= BytesIO();
6247 sftp
.getfo(urlparts
.path
, sftpfile
);
6250 sftpfile
.seek(0, 0);
6253 def download_file_from_pysftp_file(url
):
6257 def download_file_from_pysftp_string(url
):
6258 sftpfile
= download_file_from_pysftp_file(url
);
6259 return sftpfile
.read();
6261 def download_file_from_ftp_string(url
):
6265 def upload_file_to_pysftp_file(sftpfile
, url
):
6266 urlparts
= urlparse(url
);
6267 file_name
= os
.path
.basename(urlparts
.path
);
6268 file_dir
= os
.path
.dirname(urlparts
.path
);
6269 sftp_port
= urlparts
.port
;
6270 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
6272 if(urlparts
.port
is None):
6275 sftp_port
= urlparts
.port
;
6276 if(urlparts
.username
is not None):
6277 sftp_username
= urlparts
.username
;
6279 sftp_username
= "anonymous";
6280 if(urlparts
.password
is not None):
6281 sftp_password
= urlparts
.password
;
6282 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
6283 sftp_password
= "anonymous";
6286 if(urlparts
.scheme
!="sftp"):
6289 pysftp
.Connection(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
6290 except paramiko
.ssh_exception
.SSHException
:
6292 except socket
.gaierror
:
6293 log
.info("Error With URL "+url
);
6295 except socket
.timeout
:
6296 log
.info("Error With URL "+url
);
6298 sftp
= ssh
.open_sftp();
6299 sftp
.putfo(sftpfile
, urlparts
.path
);
6302 sftpfile
.seek(0, 0);
6305 def upload_file_to_pysftp_file(sftpfile
, url
):
6309 def upload_file_to_pysftp_string(sftpstring
, url
):
6310 sftpfileo
= BytesIO(sftpstring
);
6311 sftpfile
= upload_file_to_pysftp_files(ftpfileo
, url
);
6315 def upload_file_to_pysftp_string(url
):
6319 if(hasattr(shutil
, "register_archive_format")):
6320 # Register the packing format
6321 shutil
.register_archive_format(__file_format_name__
, PackArchiveFileFunc
, description
='Pack concatenated files');
6322 except shutil
.RegistryError
:
6326 if(hasattr(shutil
, "register_unpack_format")):
6327 # Register the unpacking format
6328 shutil
.register_unpack_format(__file_format_name__
, archivefile_extensions
, UnPackArchiveFileFunc
, description
='UnPack concatenated files');
6329 except shutil
.RegistryError
: