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/8/2024 Ver. 0.3.4 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
, hashlib
, datetime
, logging
, binascii
, tempfile
, zipfile
, ftplib
;
23 if os
.name
== 'nt': # Only modify if on Windows
24 if sys
.version
[0] == "2":
26 sys
.stdout
= codecs
.getwriter('utf-8')(sys
.stdout
);
27 sys
.stderr
= codecs
.getwriter('utf-8')(sys
.stderr
);
30 sys
.stdout
= io
.TextIOWrapper(sys
.stdout
.buffer, encoding
='utf-8', errors
='replace', line_buffering
=True);
31 sys
.stderr
= io
.TextIOWrapper(sys
.stderr
.buffer, encoding
='utf-8', errors
='replace', line_buffering
=True);
33 hashlib_guaranteed
= False;
34 os
.environ
["PYTHONIOENCODING"] = "UTF-8";
35 os
.environ
["LC_CTYPE"] = "UTF-8";
39 sys
.setdefaultencoding('UTF-8');
42 except AttributeError:
46 except AttributeError:
50 import simplejson
as json
;
55 from zlib
import crc32
;
57 from binascii
import crc32
;
59 rarfile_support
= False;
62 rarfile_support
= True;
64 rarfile_support
= False;
67 from safetar
import is_tarfile
;
70 from xtarfile
import is_tarfile
;
72 from tarfile
import is_tarfile
;
75 import safetar
as tarfile
;
78 import xtarfile
as tarfile
;
82 if(sys
.version
[0]=="2"):
84 from io
import StringIO
, BytesIO
;
87 from cStringIO
import StringIO
;
88 from cStringIO
import StringIO
as BytesIO
;
90 from StringIO
import StringIO
;
91 from StringIO
import StringIO
as BytesIO
;
92 elif(sys
.version
[0]>="3"):
93 from io
import StringIO
, BytesIO
;
98 from cStringIO
import StringIO
as BytesIO
;
104 from StringIO
import StringIO
as BytesIO
;
110 from io
import BytesIO
;
115 __file_format_name__
= "CatFile";
116 __program_name__
= "Py"+__file_format_name__
;
117 __file_format_lower__
= __file_format_name__
.lower();
118 __file_format_len__
= len(__file_format_name__
);
119 __file_format_hex__
= binascii
.hexlify(__file_format_name__
.encode("UTF-8")).decode("UTF-8");
120 __file_format_delimiter__
= "\x00";
121 __file_format_ver__
= "001";
122 __file_format_list__
= [__file_format_name__
, __file_format_lower__
, __file_format_len__
, __file_format_hex__
, __file_format_delimiter__
, __file_format_ver__
];
123 __project__
= __program_name__
;
124 __project_url__
= "https://github.com/GameMaker2k/PyCatFile";
125 __version_info__
= (0, 3, 4, "RC 1", 1);
126 __version_date_info__
= (2024, 3, 3, "RC 1", 1);
127 __version_date__
= str(__version_date_info__
[0]) + "." + str(__version_date_info__
[1]).zfill(2) + "." + str(__version_date_info__
[2]).zfill(2);
128 __revision__
= __version_info__
[3];
129 __revision_id__
= "$Id$";
130 if(__version_info__
[4] is not None):
131 __version_date_plusrc__
= __version_date__
+ "-" + str(__version_date_info__
[4]);
132 if(__version_info__
[4] is None):
133 __version_date_plusrc__
= __version_date__
;
134 if(__version_info__
[3] is not None):
135 __version__
= str(__version_info__
[0]) + "." + str(__version_info__
[1]) + "." + str(__version_info__
[2]) + " " + str(__version_info__
[3]);
136 if(__version_info__
[3] is None):
137 __version__
= str(__version_info__
[0]) + "." + str(__version_info__
[1]) + "." + str(__version_info__
[2]);
139 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'xz', 'lz4', 'lzo', 'lzma'];
140 outextlist
= ['gz', 'bz2', 'zst', 'xz', 'lz4', 'lzo', 'lzma'];
141 outextlistwd
= ['.gz', '.bz2', '.zst', '.xz', '.lz4', '.lzo', '.lzma'];
143 tarfile_mimetype
= "application/tar";
144 tarfile_tar_mimetype
= tarfile_mimetype
;
145 zipfile_mimetype
= "application/zip";
146 zipfile_zip_mimetype
= zipfile_mimetype
;
147 rarfile_mimetype
= "application/rar";
148 rarfile_rar_mimetype
= rarfile_mimetype
;
149 archivefile_mimetype
= "application/x-"+__file_format_list__
[1]+"";
150 archivefile_cat_mimetype
= archivefile_mimetype
;
151 archivefile_gzip_mimetype
= "application/x-"+__file_format_list__
[1]+"+gzip";
152 archivefile_gz_mimetype
= archivefile_gzip_mimetype
;
153 archivefile_bzip2_mimetype
= "application/x-"+__file_format_list__
[1]+"+bzip2";
154 archivefile_bz2_mimetype
= archivefile_bzip2_mimetype
;
155 archivefile_lz4_mimetype
= "application/x-"+__file_format_list__
[1]+"+lz4";
156 archivefile_lzop_mimetype
= "application/x-"+__file_format_list__
[1]+"+lzop";
157 archivefile_lzo_mimetype
= archivefile_lzop_mimetype
;
158 archivefile_zstandard_mimetype
= "application/x-"+__file_format_list__
[1]+"+zstandard";
159 archivefile_zstd_mimetype
= archivefile_zstandard_mimetype
;
160 archivefile_lzma_mimetype
= "application/x-"+__file_format_list__
[1]+"+lzma";
161 archivefile_xz_mimetype
= "application/x-"+__file_format_list__
[1]+"+xz";
162 archivefile_extensions
= ['.cat', '.cat.gz', '.cat.bz2', '.cat.zst', '.cat.lz4', '.cat.lzo', '.cat.lzop', '.cat.lzma', '.cat.xz'];
164 if __name__
== "__main__":
166 curscrpath
= os
.path
.dirname(sys
.argv
[0]);
170 curscrpath
= curscrpath
.replace(os
.sep
, "/");
171 curscrpath
= curscrpath
+ "/";
172 scrfile
= curscrpath
+ "catfile.py";
173 if(os
.path
.exists(scrfile
) and os
.path
.isfile(scrfile
)):
174 scrcmd
= subprocess
.Popen([sys
.executable
, scrfile
] + sys
.argv
[1:]);
177 def VerbosePrintOut(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
183 "warning": logging
.warning
,
184 "error": logging
.error
,
185 "critical": logging
.critical
,
186 "exception": logging
.exception
,
187 "logalt": lambda x
: logging
.log(dgblevel
, x
),
188 "debug": logging
.debug
190 log_function
= log_functions
.get(outtype
);
192 log_function(dbgtxt
);
196 def VerbosePrintOutReturn(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
197 VerbosePrintOut(dbgtxt
, outtype
, dbgenable
, dgblevel
);
200 def RemoveWindowsPath(dpath
):
204 dpath
= dpath
.replace(os
.path
.sep
, "/");
205 dpath
= dpath
.rstrip("/");
206 if(dpath
=="." or dpath
==".."):
210 def NormalizeRelativePath(inpath
):
211 inpath
= RemoveWindowsPath(inpath
);
212 if(os
.path
.isabs(inpath
)):
215 if(inpath
.startswith("./") or inpath
.startswith("../")):
218 outpath
= "./" + inpath
;
221 def ListDir(dirpath
, followlink
=False, duplicates
=False):
222 if(isinstance(dirpath
, (list, tuple, ))):
223 dirpath
= list(filter(None, dirpath
));
224 elif(isinstance(dirpath
, (str, ))):
225 dirpath
= list(filter(None, [dirpath
]));
227 for mydirfile
in dirpath
:
228 if(not os
.path
.exists(mydirfile
)):
230 mydirfile
= NormalizeRelativePath(mydirfile
);
231 if(os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
)):
232 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
));
233 if(os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
)):
234 for root
, dirs
, filenames
in os
.walk(mydirfile
):
236 dpath
= RemoveWindowsPath(dpath
);
237 if(dpath
not in retlist
and not duplicates
):
238 retlist
.append(dpath
);
240 retlist
.append(dpath
);
241 for file in filenames
:
242 fpath
= os
.path
.join(root
, file);
243 fpath
= RemoveWindowsPath(fpath
);
244 if(fpath
not in retlist
and not duplicates
):
245 retlist
.append(fpath
);
247 retlist
.append(fpath
);
249 retlist
.append(RemoveWindowsPath(mydirfile
));
252 def ListDirAdvanced(dirpath
, followlink
=False, duplicates
=False):
253 if isinstance(dirpath
, (list, tuple)):
254 dirpath
= list(filter(None, dirpath
));
255 elif isinstance(dirpath
, str):
256 dirpath
= list(filter(None, [dirpath
]));
258 for mydirfile
in dirpath
:
259 if not os
.path
.exists(mydirfile
):
261 mydirfile
= NormalizeRelativePath(mydirfile
);
262 if os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
) and followlink
:
263 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
))
264 if os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
):
265 for root
, dirs
, filenames
in os
.walk(mydirfile
):
266 # Sort dirs and filenames alphabetically in place
267 dirs
.sort(key
=lambda x
: x
.lower());
268 filenames
.sort(key
=lambda x
: x
.lower());
269 dpath
= RemoveWindowsPath(root
);
270 if not duplicates
and dpath
not in retlist
:
271 retlist
.append(dpath
);
273 retlist
.append(dpath
);
274 for file in filenames
:
275 fpath
= os
.path
.join(root
, file);
276 fpath
= RemoveWindowsPath(fpath
);
277 if not duplicates
and fpath
not in retlist
:
278 retlist
.append(fpath
);
280 retlist
.append(fpath
);
282 retlist
.append(RemoveWindowsPath(mydirfile
));
285 def create_alias_function(prefix
, base_name
, suffix
, target_function
):
286 # Define a new function that wraps the target function
287 def alias_function(*args
, **kwargs
):
288 return target_function(*args
, **kwargs
);
290 # Create the function name by combining the prefix, base name, and the suffix
291 function_name
= "{}{}{}".format(prefix
, base_name
, suffix
);
293 # Add the new function to the global namespace
294 globals()[function_name
] = alias_function
;
296 # initial_value can be 0xFFFF or 0x0000
297 def crc16_ansi(msg
, initial_value
=0xFFFF):
298 # CRC-16-IBM / CRC-16-ANSI polynomial and initial value
299 poly
= 0x8005; # Polynomial for CRC-16-IBM / CRC-16-ANSI
300 crc
= initial_value
; # Initial value
302 crc ^
= b
<< 8; # XOR byte into CRC top byte
303 for _
in range(8): # Process each bit
304 if crc
& 0x8000: # If the top bit is set
305 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
307 crc
= crc
<< 1; # Just shift left
308 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
311 # initial_value can be 0xFFFF or 0x0000
312 def crc16_ibm(msg
, initial_value
=0xFFFF):
313 return crc16_ansi(msg
, initial_value
);
315 # initial_value is 0xFFFF
317 return crc16_ansi(msg
, 0xFFFF);
319 # initial_value can be 0xFFFF, 0x1D0F or 0x0000
320 def crc16_ccitt(msg
, initial_value
=0xFFFF):
321 # CRC-16-CCITT polynomial
322 poly
= 0x1021; # Polynomial for CRC-16-CCITT
323 # Use the specified initial value
326 crc ^
= b
<< 8; # XOR byte into CRC top byte
327 for _
in range(8): # Process each bit
328 if crc
& 0x8000: # If the top bit is set
329 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
331 crc
= crc
<< 1; # Just shift left
332 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
335 # initial_value can be 0x42F0E1EBA9EA3693 or 0x0000000000000000
336 def crc64_ecma(msg
, initial_value
=0x0000000000000000):
337 # CRC-64-ECMA polynomial and initial value
338 poly
= 0x42F0E1EBA9EA3693;
339 crc
= initial_value
; # Initial value for CRC-64-ECMA
341 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
342 for _
in range(8): # Process each bit
343 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
344 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
346 crc
<<= 1; # Just shift left if the MSB is 0
347 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
350 # initial_value can be 0x000000000000001B or 0xFFFFFFFFFFFFFFFF
351 def crc64_iso(msg
, initial_value
=0xFFFFFFFFFFFFFFFF):
352 # CRC-64-ISO polynomial and initial value
353 poly
= 0x000000000000001B;
354 crc
= initial_value
; # Common initial value for CRC-64-ISO
356 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
357 for _
in range(8): # Process each bit
358 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
359 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
361 crc
<<= 1; # Just shift left if the MSB is 0
362 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
365 def ReadTillNullByte(fp
, delimiter
=__file_format_delimiter__
):
368 nullbyte
= delimiter
.encode("UTF-8");
370 curbyte
= fp
.read(1);
371 if(curbyte
==nullbyte
or not curbyte
):
373 curfullbyte
= curfullbyte
+ curbyte
;
374 return curfullbyte
.decode('UTF-8');
376 def ReadUntilNullByte(fp
, delimiter
=__file_format_delimiter__
):
377 return ReadTillNullByte(fp
, delimiter
);
379 def SeekToEndOfFile(fp
):
383 if(lasttell
==fp
.tell()):
385 lasttell
= fp
.tell();
388 def ReadFileHeaderData(fp
, rounds
=0, delimiter
=__file_format_delimiter__
):
392 while(rocount
<roend
):
393 HeaderOut
.append(ReadTillNullByte(fp
, delimiter
));
394 rocount
= rocount
+ 1;
397 def ReadFileHeaderDataBySize(fp
, delimiter
=__file_format_delimiter__
):
398 headerpresize
= ReadTillNullByte(fp
, delimiter
);
399 headersize
= int(headerpresize
, 16);
400 headercontent
= str(fp
.read(headersize
).decode('UTF-8')).split(delimiter
);
403 roend
= int(len(headercontent
));
404 HeaderOut
= [headerpresize
];
405 while(rocount
<roend
):
406 HeaderOut
.append(headercontent
[rocount
]);
407 rocount
= rocount
+ 1;
410 def ReadFileHeaderDataByList(fp
, listval
=[], delimiter
=__file_format_delimiter__
):
412 roend
= int(len(listval
));
414 while(rocount
<roend
):
415 RoundArray
= {listval
[rocount
]: ReadTillNullByte(fp
, delimiter
)};
416 HeaderOut
.update(RoundArray
);
417 rocount
= rocount
+ 1;
420 def ReadFileHeaderDataByListSize(fp
, listval
=[], delimiter
=__file_format_delimiter__
):
421 headerpresize
= ReadTillNullByte(fp
, delimiter
);
422 headersize
= int(headerpresize
, 16);
423 headercontent
= str(fp
.read(headersize
).decode('UTF-8')).split(delimiter
);
427 roend
= int(len(headercontent
));
428 HeaderOut
= {listval
[0]: headerpresize
};
429 while(rocount
<roend
):
430 RoundArray
= {listval
[rocount
]: headercontent
[rocount
]};
431 HeaderOut
.update(RoundArray
);
432 rocount
= rocount
+ 1;
433 listcount
= listcount
+ 1;
436 def AppendNullByte(indata
, delimiter
=__file_format_delimiter__
):
437 outdata
= str(indata
) + delimiter
;
440 def AppendNullBytes(indata
=[], delimiter
=__file_format_delimiter__
):
445 outdata
= outdata
+ AppendNullByte(indata
[inum
], delimiter
);
449 def ReadTillNullByteAlt(fp
, delimiter
=__file_format_delimiter__
):
450 """Read bytes from file pointer until a null byte is encountered."""
451 bytes_list
= [] # Use list for efficient append operation.
453 cur_byte
= fp
.read(1);
454 if cur_byte
== delimiter
.encode() or not cur_byte
:
456 bytes_list
.append(cur_byte
);
457 return b
''.join(bytes_list
).decode('UTF-8');
459 def ReadUntilNullByteAlt(fp
, delimiter
=__file_format_delimiter__
):
460 return ReadTillNullByteAlt(fp
, delimiter
);
462 def ReadFileHeaderDataAlt(fp
, rounds
=0, delimiter
=__file_format_delimiter__
):
463 """Read multiple null-byte terminated strings from a file."""
465 for round_count
in range(rounds
):
466 header_out
[round_count
] = ReadTillNullByteAlt(fp
, delimiter
);
469 def ReadFileHeaderDataBySizeAlt(fp
, delimiter
=__file_format_delimiter__
):
470 # Read and convert header size from hexadecimal to integer
471 header_pre_size
= ReadTillNullByte(fp
, delimiter
);
472 header_size
= int(header_pre_size
, 16);
473 # Read and split the header content
474 header_content
= str(fp
.read(header_size
).decode('UTF-8')).split(delimiter
);
476 # Prepend the pre-size and return the combined list
477 return [header_pre_size
] + header_content
;
479 def ReadFileHeaderDataByListSizeAlt(fp
, listval
=[], delimiter
=__file_format_delimiter__
):
480 # Read the size and content from the header
481 header_pre_size
= ReadTillNullByte(fp
, delimiter
);
482 header_size
= int(header_pre_size
, 16);
483 header_content
= str(fp
.read(header_size
).decode('UTF-8')).split(delimiter
);
485 # Initialize HeaderOut with the header pre-size if listval is not empty
486 HeaderOut
= {listval
[0]: header_pre_size
} if listval
else {};
487 # Map the remaining listval items to their corresponding header content, starting from the second item
488 for i
in range(1, min(len(header_content
) + 1, len(listval
))):
489 HeaderOut
[listval
[i
]] = header_content
[i
- 1]; # -1 because header_content is 0-indexed
492 def ReadFileHeaderDataByListAlt(fp
, listval
=[], delimiter
=__file_format_delimiter__
):
493 """Read multiple null-byte terminated strings from a file."""
495 for round_count
in listval
:
496 header_out
.append(ReadTillNullByteAlt(fp
, delimiter
));
499 def AppendNullByteAlt(indata
, delimiter
=__file_format_delimiter__
):
500 """Append a null byte to the given data."""
501 return str(indata
) + delimiter
;
503 def AppendNullBytesAlt(indata
=[], delimiter
=__file_format_delimiter__
):
504 """Append a null byte to each element in the list and concatenate."""
505 return delimiter
.join(map(str, indata
)) + delimiter
; # Efficient concatenation with null byte.
507 def PrintPermissionString(fchmode
, ftype
):
508 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' } };
510 for fmodval
in str(oct(fchmode
))[-3:]:
511 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
512 if(ftype
==0 or ftype
==7):
513 permissionstr
= "-" + permissionstr
;
515 permissionstr
= "h" + permissionstr
;
517 permissionstr
= "l" + permissionstr
;
519 permissionstr
= "c" + permissionstr
;
521 permissionstr
= "b" + permissionstr
;
523 permissionstr
= "d" + permissionstr
;
525 permissionstr
= "f" + permissionstr
;
527 permissionstr
= "D" + permissionstr
;
529 permissionstr
= "p" + permissionstr
;
531 permissionstr
= "w" + permissionstr
;
533 permissionoutstr
= stat
.filemode(fchmode
);
534 except AttributeError:
535 permissionoutstr
= permissionstr
;
537 permissionoutstr
= permissionstr
;
538 return permissionoutstr
;
540 def PrintPermissionStringAlt(fchmode
, ftype
):
542 '0': '---', '1': '--x', '2': '-w-', '3': '-wx',
543 '4': 'r--', '5': 'r-x', '6': 'rw-', '7': 'rwx'
545 # Translate file mode into permission string
546 permissionstr
= ''.join([permissions
[i
] for i
in str(oct(fchmode
))[-3:]]);
547 # Append file type indicator
549 0: '-', 1: 'h', 2: 'l', 3: 'c', 4: 'b',
550 5: 'd', 6: 'f', 8: 'D', 9: 'p', 10: 'w'
552 file_type
= type_indicators
.get(ftype
, '-');
553 permissionstr
= file_type
+ permissionstr
;
555 permissionoutstr
= stat
.filemode(fchmode
);
556 except AttributeError:
557 permissionoutstr
= permissionstr
;
558 return permissionoutstr
;
560 def CompressionSupport():
561 compression_list
= [];
564 compression_list
.append("gz");
565 compression_list
.append("gzip");
570 compression_list
.append("bz2");
571 compression_list
.append("bzip2");
576 compression_list
.append("lz4");
581 compression_list
.append("lzo");
582 compression_list
.append("lzop");
587 compression_list
.append("zstd");
588 compression_list
.append("zstandard");
593 compression_list
.append("lzma");
594 compression_list
.append("xz");
597 return compression_list
;
599 def CheckCompressionType(infile
, formatspecs
=__file_format_list__
, closefp
=True):
600 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
604 catfp
= open(infile
, "rb");
605 except FileNotFoundError
:
608 prefp
= catfp
.read(2);
610 if(prefp
==binascii
.unhexlify("1f8b")):
613 prefp
= catfp
.read(3);
614 if(prefp
==binascii
.unhexlify("425a68")):
616 if(prefp
==binascii
.unhexlify("5d0000")):
619 prefp
= catfp
.read(4);
620 if(prefp
==binascii
.unhexlify("28b52ffd")):
622 if(prefp
==binascii
.unhexlify("04224d18")):
624 if(prefp
==binascii
.unhexlify("504B0304")):
625 filetype
= "zipfile";
627 prefp
= catfp
.read(5);
628 if(prefp
==binascii
.unhexlify("7573746172")):
629 filetype
= "tarfile";
631 prefp
= catfp
.read(6);
632 if(prefp
==binascii
.unhexlify("fd377a585a00")):
635 prefp
= catfp
.read(7);
636 if(prefp
==binascii
.unhexlify("526172211a0700")):
637 filetype
= "rarfile";
638 if(prefp
==binascii
.unhexlify("43617446696c65")):
639 filetype
= "catfile";
641 prefp
= catfp
.read(8);
642 if(prefp
==binascii
.unhexlify("526172211a070100")):
643 filetype
= "rarfile";
645 prefp
= catfp
.read(formatspecs
[2]);
646 if(prefp
==binascii
.unhexlify(formatspecs
[3])):
647 filetype
= formatspecs
[1];
649 prefp
= catfp
.read(9);
650 if(prefp
==binascii
.unhexlify("894c5a4f000d0a1a0a")):
653 prefp
= catfp
.read(10);
654 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
655 filetype
= "tarfile";
661 def CheckCompressionTypeFromString(instring
, formatspecs
=__file_format_list__
, closefp
=True):
663 instringsfile
= BytesIO(instring
);
665 instringsfile
= BytesIO(instring
.encode("UTF-8"));
666 return CheckCompressionType(instringsfile
, formatspecs
, closefp
);
668 def GetCompressionMimeType(infile
, formatspecs
=__file_format_list__
):
669 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
670 if(compresscheck
=="gzip" or compresscheck
=="gz"):
671 return archivefile_gzip_mimetype
;
672 if(compresscheck
=="bzip2" or compresscheck
=="bz2"):
673 return archivefile_bzip2_mimetype
;
674 if(compresscheck
=="zstd" or compresscheck
=="zstandard"):
675 return archivefile_zstandard_mimetype
;
676 if(compresscheck
=="lz4"):
677 return archivefile_lz4_mimetype
;
678 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
679 return archivefile_lzop_mimetype
;
680 if(compresscheck
=="lzma"):
681 return archivefile_lzma_mimetype
;
682 if(compresscheck
=="xz"):
683 return archivefile_xz_mimetype
;
684 if(compresscheck
=="catfile" or compresscheck
=="cat" or compresscheck
==formatspecs
[1]):
685 return archivefile_cat_mimetype
;
686 if(not compresscheck
):
690 def UncompressArchiveFile(fp
, formatspecs
=__file_format_list__
):
691 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
693 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
694 if(compresscheck
=="gzip"):
699 catfp
= gzip
.GzipFile(fileobj
=fp
, mode
="rb");
700 if(compresscheck
=="bzip2"):
706 catfp
.write(bz2
.decompress(fp
.read()));
707 if(compresscheck
=="zstd"):
713 catfp
.write(zstandard
.decompress(fp
.read()));
714 if(compresscheck
=="lz4"):
720 catfp
.write(lz4
.frame
.decompress(fp
.read()));
721 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
727 catfp
.write(lzo
.decompress(fp
.read()));
728 if(compresscheck
=="lzma" or compresscheck
=="xz"):
734 catfp
.write(lzma
.decompress(fp
.read()));
735 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[1]):
737 if(not compresscheck
):
743 with fp
as fpcontent
:
745 catfp
.write(lzma
.decompress(fp
.read()));
746 except lzma
.LZMAError
:
750 create_alias_function("Uncompress", __file_format_name__
, "", UncompressArchiveFile
);
752 def UncompressFile(infile
, formatspecs
=__file_format_list__
, mode
="rb"):
753 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
754 if(sys
.version_info
[0]==2 and compresscheck
):
760 if(compresscheck
=="gzip"):
766 filefp
= gzip
.open(infile
, mode
, encoding
="UTF-8");
767 except (ValueError, TypeError) as e
:
768 filefp
= gzip
.open(infile
, mode
);
769 if(compresscheck
=="bzip2"):
775 filefp
= bz2
.open(infile
, mode
, encoding
="UTF-8");
776 except (ValueError, TypeError) as e
:
777 filefp
= bz2
.open(infile
, mode
);
778 if(compresscheck
=="zstd"):
784 filefp
= zstandard
.open(infile
, mode
, encoding
="UTF-8");
785 except (ValueError, TypeError) as e
:
786 filefp
= zstandard
.open(infile
, mode
);
787 if(compresscheck
=="lz4"):
793 filefp
= lz4
.frame
.open(infile
, mode
, encoding
="UTF-8");
794 except (ValueError, TypeError) as e
:
795 filefp
= lz4
.frame
.open(infile
, mode
);
796 if(compresscheck
=="lzo"):
802 filefp
= lzo
.open(infile
, mode
, encoding
="UTF-8");
803 except (ValueError, TypeError) as e
:
804 filefp
= lzo
.open(infile
, mode
);
805 if(compresscheck
=="lzma"):
811 filefp
= lzma
.open(infile
, mode
, encoding
="UTF-8");
812 except (ValueError, TypeError) as e
:
813 filefp
= lzma
.open(infile
, mode
);
814 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[1]):
816 filefp
= open(infile
, mode
, encoding
="UTF-8");
817 except (ValueError, TypeError) as e
:
818 filefp
= open(infile
, mode
);
819 if(not compresscheck
):
821 filefp
= open(infile
, mode
, encoding
="UTF-8");
822 except (ValueError, TypeError) as e
:
823 filefp
= open(infile
, mode
);
824 except FileNotFoundError
:
828 def UncompressString(infile
):
829 compresscheck
= CheckCompressionTypeFromString(infile
, formatspecs
, False);
830 if(compresscheck
=="gzip"):
835 fileuz
= gzip
.decompress(infile
);
836 if(compresscheck
=="bzip2"):
841 fileuz
= bz2
.decompress(infile
);
842 if(compresscheck
=="zstd"):
847 fileuz
= zstandard
.decompress(infile
);
848 if(compresscheck
=="lz4"):
853 fileuz
= lz4
.frame
.decompress(infile
);
854 if(compresscheck
=="lzo"):
859 fileuz
= lzo
.decompress(infile
);
860 if(compresscheck
=="lzma"):
865 fileuz
= lzma
.decompress(infile
);
866 if(not compresscheck
):
868 if(hasattr(fileuz
, 'decode')):
869 fileuz
= fileuz
.decode("UTF-8");
872 def UncompressStringAlt(infile
):
874 outstring
= UncompressString(infile
);
875 filefp
.write(outstring
);
879 def CheckCompressionSubType(infile
, formatspecs
=__file_format_list__
):
880 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
881 if(not compresscheck
):
882 fextname
= os
.path
.splitext(infile
)[1];
884 compresscheck
= "gzip";
885 if(fextname
==".bz2"):
886 compresscheck
= "bzip2";
887 if(fextname
==".zst"):
888 compresscheck
= "zstd";
889 if(fextname
==".lz4"):
890 compresscheck
= "lz4";
891 if(fextname
==".lzo" or fextname
==".lzop"):
892 compresscheck
= "lzo";
893 if(fextname
==".lzma" or fextname
==".xz"):
894 compresscheck
= "lzma";
895 if(not compresscheck
):
897 if(compresscheck
=="catfile"):
899 if(compresscheck
==formatspecs
[1]):
900 return formatspecs
[1];
901 if(compresscheck
=="tarfile"):
903 if(compresscheck
=="zipfile"):
905 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
906 catfp
= UncompressArchiveFile(infile
, formatspecs
[1]);
909 if(compresscheck
=="gzip"):
914 catfp
= gzip
.GzipFile(infile
, "rb");
915 if(compresscheck
=="bzip2"):
920 catfp
= bz2
.BZ2File(infile
, "rb");
921 if(compresscheck
=="lz4"):
926 catfp
= lz4
.frame
.open(infile
, "rb");
927 if(compresscheck
=="zstd"):
932 catfp
= zstandard
.open(infile
, "rb");
933 if(compresscheck
=="lzma" or compresscheck
=="xz"):
938 catfp
= lzma
.open(infile
, "rb");
939 except FileNotFoundError
:
942 prefp
= catfp
.read(5);
943 if(prefp
==binascii
.unhexlify("7573746172")):
944 filetype
= "tarfile";
946 prefp
= catfp
.read(7);
947 if(prefp
==binascii
.unhexlify("43617446696c65")):
948 filetype
= "catfile";
950 prefp
= catfp
.read(formatspecs
[2]);
951 if(prefp
==binascii
.unhexlify(formatspecs
[3])):
952 filetype
= formatspecs
[1];
954 prefp
= catfp
.read(10);
955 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
956 filetype
= "tarfile";
961 def GZipCompress(data
, compresslevel
=9):
966 tmpfp
= tempfile
.NamedTemporaryFile("wb", delete
=False);
968 tmpfp
= gzip
.GzipFile(tmpfp
.name
, mode
="wb", compresslevel
=compresslevel
);
972 catfp
= open(tmpfp
.name
, "rb");
973 except FileNotFoundError
:
975 catdata
= catfp
.read();
979 def CompressArchiveFile(fp
, compression
="auto", compressionlevel
=None, formatspecs
=__file_format_list__
):
980 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
981 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
984 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[1]):
986 if(compression
not in compressionlist
and compression
is None):
987 compression
= "auto";
988 if(compression
=="gzip"):
994 if(compressionlevel
is None):
995 compressionlevel
= 9;
997 compressionlevel
= int(compressionlevel
);
998 catfp
.write(gzip
.compress(fp
.read(), compresslevel
=compressionlevel
));
999 if(compression
=="bzip2"):
1005 if(compressionlevel
is None):
1006 compressionlevel
= 9;
1008 compressionlevel
= int(compressionlevel
);
1009 catfp
.write(bz2
.compress(fp
.read(), compresslevel
=compressionlevel
));
1010 if(compression
=="lz4"):
1016 if(compressionlevel
is None):
1017 compressionlevel
= 9;
1019 compressionlevel
= int(compressionlevel
);
1020 catfp
.write(lz4
.frame
.compress(fp
.read(), compression_level
=compressionlevel
));
1021 if(compression
=="lzo" or compression
=="lzop"):
1027 if(compressionlevel
is None):
1028 compressionlevel
= 9;
1030 compressionlevel
= int(compressionlevel
);
1031 catfp
.write(lzo
.compress(fp
.read(), compresslevel
=compressionlevel
));
1032 if(compression
=="zstd"):
1038 if(compressionlevel
is None):
1039 compressionlevel
= 10;
1041 compressionlevel
= int(compressionlevel
);
1042 catfp
.write(zstandard
.compress(fp
.read(), level
=compressionlevel
));
1043 if(compression
=="lzma"):
1049 if(compressionlevel
is None):
1050 compressionlevel
= 9;
1052 compressionlevel
= int(compressionlevel
);
1053 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]));
1054 if(compression
=="xz"):
1060 if(compressionlevel
is None):
1061 compressionlevel
= 9;
1063 compressionlevel
= int(compressionlevel
);
1064 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]));
1065 if(compression
=="auto" or compression
is None):
1070 create_alias_function("Compress", __file_format_name__
, "", CompressArchiveFile
);
1072 def CompressOpenFile(outfile
, compressionlevel
=None):
1073 if(outfile
is None):
1075 fbasename
= os
.path
.splitext(outfile
)[0];
1076 fextname
= os
.path
.splitext(outfile
)[1];
1077 if(compressionlevel
is None and fextname
!=".zst"):
1078 compressionlevel
= 9;
1079 elif(compressionlevel
is None and fextname
==".zst"):
1080 compressionlevel
= 10;
1082 compressionlevel
= int(compressionlevel
);
1083 if(sys
.version_info
[0]==2):
1088 if(fextname
not in outextlistwd
):
1090 outfp
= open(outfile
, "wb", encoding
="UTF-8");
1091 except (ValueError, TypeError) as e
:
1092 outfp
= open(outfile
, "wb");
1093 elif(fextname
==".gz"):
1099 outfp
= gzip
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
1100 except (ValueError, TypeError) as e
:
1101 outfp
= gzip
.open(outfile
, mode
, compressionlevel
);
1102 elif(fextname
==".bz2"):
1108 outfp
= bz2
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
1109 except (ValueError, TypeError) as e
:
1110 outfp
= bz2
.open(outfile
, mode
, compressionlevel
);
1111 elif(fextname
==".zst"):
1117 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
), encoding
="UTF-8");
1118 except (ValueError, TypeError) as e
:
1119 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
));
1120 elif(fextname
==".xz"):
1126 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}], encoding
="UTF-8");
1127 except (ValueError, TypeError) as e
:
1128 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]);
1129 elif(fextname
==".lz4"):
1135 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
, encoding
="UTF-8");
1136 except (ValueError, TypeError) as e
:
1137 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
);
1138 elif(fextname
==".lzo"):
1144 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
, encoding
="UTF-8");
1145 except (ValueError, TypeError) as e
:
1146 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
);
1147 elif(fextname
==".lzma"):
1153 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}], encoding
="UTF-8");
1154 except (ValueError, TypeError) as e
:
1155 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]);
1156 except FileNotFoundError
:
1160 def GetDevMajorMinor(fdev
):
1162 if(hasattr(os
, "minor")):
1163 retdev
.append(os
.minor(fdev
));
1166 if(hasattr(os
, "major")):
1167 retdev
.append(os
.major(fdev
));
1172 def CheckSumSupport(checkfor
, guaranteed
=True):
1174 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
1176 hash_list
= sorted(list(hashlib
.algorithms_available
));
1177 checklistout
= sorted(hash_list
+ ['adler32', 'crc16', 'crc16_ansi', 'crc16_ibm', 'crc16_ccitt', 'crc32', 'crc64', 'crc64_ecma', 'crc64_iso', 'none']);
1178 if(checkfor
in checklistout
):
1183 def CheckSumSupportAlt(checkfor
, guaranteed
=True):
1185 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
1187 hash_list
= sorted(list(hashlib
.algorithms_available
));
1188 checklistout
= hash_list
;
1189 if(checkfor
in checklistout
):
1194 def PackArchiveFile(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1195 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1196 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1197 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1198 advancedlist
= True;
1199 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1200 outfile
= RemoveWindowsPath(outfile
);
1201 checksumtype
= checksumtype
.lower();
1202 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
1203 checksumtype
="crc32";
1204 if(checksumtype
=="none"):
1206 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[1]):
1208 if(compression
not in compressionlist
and compression
is None):
1209 compression
= "auto";
1211 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1212 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1213 if(os
.path
.exists(outfile
)):
1218 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1221 fbasename
= os
.path
.splitext(outfile
)[0];
1222 fextname
= os
.path
.splitext(outfile
)[1];
1223 catfp
= CompressOpenFile(outfile
, compressionlevel
);
1224 catver
= formatspecs
[5];
1225 fileheaderver
= str(int(catver
.replace(".", "")));
1226 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
1227 catfp
.write(fileheader
.encode('UTF-8'));
1230 for line
in sys
.stdin
:
1231 infilelist
.append(line
.strip());
1232 infilelist
= list(filter(None, infilelist
));
1233 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
1234 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
1236 with
open(infiles
, "r") as finfile
:
1237 for line
in finfile
:
1238 infilelist
.append(line
.strip());
1239 infilelist
= list(filter(None, infilelist
));
1241 if(isinstance(infiles
, (list, tuple, ))):
1242 infilelist
= list(filter(None, infiles
));
1243 elif(isinstance(infiles
, (str, ))):
1244 infilelist
= list(filter(None, [infiles
]));
1246 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
1248 GetDirList
= ListDir(infilelist
, followlink
, False);
1256 inodetocatinode
= {};
1257 fnumfiles
= format(int(len(GetDirList
)), 'x').lower();
1258 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[4]);
1259 if(checksumtype
=="none" or checksumtype
==""):
1260 catfileheadercshex
= format(0, 'x').lower();
1261 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1262 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1263 elif(checksumtype
=="crc16_ccitt"):
1264 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1265 elif(checksumtype
=="adler32"):
1266 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1267 elif(checksumtype
=="crc32"):
1268 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1269 elif(checksumtype
=="crc64_ecma"):
1270 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1271 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1272 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1273 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1274 checksumoutstr
= hashlib
.new(checksumtype
);
1275 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
1276 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1278 catfileheadercshex
= format(0, 'x').lower
1279 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
1280 catfp
.write(fnumfilesa
.encode('UTF-8'));
1283 os
.fsync(catfp
.fileno());
1284 except io
.UnsupportedOperation
:
1286 except AttributeError:
1288 for curfname
in GetDirList
:
1289 catfhstart
= catfp
.tell();
1290 if(re
.findall("^[.|/]", curfname
)):
1293 fname
= "./"+curfname
;
1295 VerbosePrintOut(fname
);
1296 if(not followlink
or followlink
is None):
1297 fstatinfo
= os
.lstat(fname
);
1299 fstatinfo
= os
.stat(fname
);
1300 fpremode
= fstatinfo
.st_mode
;
1301 finode
= fstatinfo
.st_ino
;
1302 flinkcount
= fstatinfo
.st_nlink
;
1304 if(stat
.S_ISREG(fpremode
)):
1306 elif(stat
.S_ISLNK(fpremode
)):
1308 elif(stat
.S_ISCHR(fpremode
)):
1310 elif(stat
.S_ISBLK(fpremode
)):
1312 elif(stat
.S_ISDIR(fpremode
)):
1314 elif(stat
.S_ISFIFO(fpremode
)):
1316 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
1318 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
1320 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
1325 fcurfid
= format(int(curfid
), 'x').lower();
1326 if(not followlink
and finode
!=0):
1328 if(finode
in inodelist
):
1330 flinkname
= inodetofile
[finode
];
1331 fcurinode
= format(int(inodetocatinode
[finode
]), 'x').lower();
1332 if(finode
not in inodelist
):
1333 inodelist
.append(finode
);
1334 inodetofile
.update({finode
: fname
});
1335 inodetocatinode
.update({finode
: curinode
});
1336 fcurinode
= format(int(curinode
), 'x').lower();
1337 curinode
= curinode
+ 1;
1339 fcurinode
= format(int(curinode
), 'x').lower();
1340 curinode
= curinode
+ 1;
1341 curfid
= curfid
+ 1;
1343 flinkname
= os
.readlink(fname
);
1344 fdev
= fstatinfo
.st_dev
;
1345 getfdev
= GetDevMajorMinor(fdev
);
1346 fdev_minor
= getfdev
[0];
1347 fdev_major
= getfdev
[1];
1348 frdev
= fstatinfo
.st_dev
;
1349 if(hasattr(fstatinfo
, "st_rdev")):
1350 frdev
= fstatinfo
.st_rdev
;
1352 frdev
= fstatinfo
.st_dev
;
1353 getfrdev
= GetDevMajorMinor(frdev
);
1354 frdev_minor
= getfrdev
[0];
1355 frdev_major
= getfrdev
[1];
1356 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
1357 fsize
= format(int("0"), 'x').lower();
1358 elif(ftype
==0 or ftype
==7):
1359 fsize
= format(int(fstatinfo
.st_size
), 'x').lower();
1361 fsize
= format(int(fstatinfo
.st_size
)).lower();
1362 fatime
= format(int(fstatinfo
.st_atime
), 'x').lower();
1363 fmtime
= format(int(fstatinfo
.st_mtime
), 'x').lower();
1364 fctime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1365 if(hasattr(fstatinfo
, "st_birthtime")):
1366 fbtime
= format(int(fstatinfo
.st_birthtime
), 'x').lower();
1368 fbtime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1369 fmode
= format(int(fstatinfo
.st_mode
), 'x').lower();
1370 fchmode
= format(int(stat
.S_IMODE(fstatinfo
.st_mode
)), 'x').lower();
1371 ftypemod
= format(int(stat
.S_IFMT(fstatinfo
.st_mode
)), 'x').lower();
1372 fuid
= format(int(fstatinfo
.st_uid
), 'x').lower();
1373 fgid
= format(int(fstatinfo
.st_gid
), 'x').lower();
1378 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
1379 funame
= userinfo
.pw_name
;
1388 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
1389 fgname
= groupinfo
.gr_name
;
1394 fdev_minor
= format(int(fdev_minor
), 'x').lower();
1395 fdev_major
= format(int(fdev_major
), 'x').lower();
1396 frdev_minor
= format(int(frdev_minor
), 'x').lower();
1397 frdev_major
= format(int(frdev_major
), 'x').lower();
1398 finode
= format(int(finode
), 'x').lower();
1399 flinkcount
= format(int(flinkcount
), 'x').lower();
1400 if(hasattr(fstatinfo
, "st_file_attributes")):
1401 fwinattributes
= format(int(fstatinfo
.st_file_attributes
), 'x').lower();
1403 fwinattributes
= format(int(0), 'x').lower();
1404 fcontents
= "".encode('UTF-8');
1406 if(ftype
== 0 or ftype
== 7):
1407 with
open(fname
, "rb") as fpc
:
1409 chunk
= fpc
.read(chunk_size
);
1413 if(followlink
and (ftype
== 1 or ftype
== 2)):
1414 flstatinfo
= os
.stat(flinkname
);
1415 with
open(flinkname
, "rb") as fpc
:
1417 chunk
= fpc
.read(chunk_size
);
1421 ftypehex
= format(ftype
, 'x').lower();
1422 extrafields
= format(len(extradata
), 'x').lower();
1423 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
1424 if(len(extradata
)>0):
1425 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
1426 extrasizelen
= format(len(extrasizestr
), 'x').lower();
1427 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
[4]);
1428 if(len(extradata
)>0):
1429 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[4]);
1430 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[4]);
1431 if(checksumtype
=="none" or checksumtype
==""):
1432 catfileheadercshex
= format(0, 'x').lower();
1433 catfilecontentcshex
= format(0, 'x').lower();
1434 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1435 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
1436 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
1437 elif(checksumtype
=="crc16_ccitt"):
1438 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
1439 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
1440 elif(checksumtype
=="adler32"):
1441 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1442 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
1443 elif(checksumtype
=="crc32"):
1444 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1445 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
1446 elif(checksumtype
=="crc64_ecma"):
1447 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1448 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
1449 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1450 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1451 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
1452 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1453 checksumoutstr
= hashlib
.new(checksumtype
);
1454 checksumoutstr
.update("".encode('UTF-8'));
1455 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1456 checksumoutstr
= hashlib
.new(checksumtype
);
1457 checksumoutstr
.update(fcontents
);
1458 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
1460 catfileheadercshex
= format(0, 'x').lower();
1461 catfilecontentcshex
= format(0, 'x').lower();
1462 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
1463 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
1464 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[4]) + catfileoutstr
;
1465 if(checksumtype
=="none" or checksumtype
==""):
1466 catfileheadercshex
= format(0, 'x').lower();
1467 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1468 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
1469 elif(checksumtype
=="crc16_ccitt"):
1470 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
1471 elif(checksumtype
=="adler32"):
1472 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
1473 elif(checksumtype
=="crc32"):
1474 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
1475 elif(checksumtype
=="crc64_ecma"):
1476 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1477 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1478 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1479 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1480 checksumoutstr
= hashlib
.new(checksumtype
);
1481 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
1482 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1484 catfileheadercshex
= format(0, 'x').lower();
1485 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
1486 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
1487 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
1488 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
1489 nullstrecd
= formatspecs
[4].encode('UTF-8');
1490 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
1491 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
1492 catfp
.write(catfileout
);
1495 os
.fsync(catfp
.fileno());
1496 except io
.UnsupportedOperation
:
1498 except AttributeError:
1500 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1501 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1504 os
.fsync(catfp
.fileno());
1505 except io
.UnsupportedOperation
:
1507 except AttributeError:
1511 if(hasattr(sys
.stdout
, "buffer")):
1512 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1514 shutil
.copyfileobj(catfp
, sys
.stdout
);
1522 create_alias_function("Pack", __file_format_name__
, "", PackArchiveFile
);
1524 if(hasattr(shutil
, "register_archive_format")):
1525 def PackArchiveFileFunc(archive_name
, source_dir
, **kwargs
):
1526 return PackArchiveFile(source_dir
, archive_name
, False, "auto", None, False, "crc32", [], __file_format_delimiter__
, False, False);
1527 create_alias_function("Pack", __file_format_name__
, "Func", PackArchiveFileFunc
);
1529 def PackArchiveFileFromDirList(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1530 return PackArchiveFile(infiles
, outfile
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
1532 def PackArchiveFileFromTarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1533 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1534 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1535 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1536 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1537 outfile
= RemoveWindowsPath(outfile
);
1538 checksumtype
= checksumtype
.lower();
1539 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
1540 checksumtype
="crc32";
1541 if(checksumtype
=="none"):
1543 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[1]):
1545 if(compression
not in compressionlist
and compression
is None):
1546 compression
= "auto";
1548 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1549 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1550 if(os
.path
.exists(outfile
)):
1555 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1558 fbasename
= os
.path
.splitext(outfile
)[0];
1559 fextname
= os
.path
.splitext(outfile
)[1];
1560 catfp
= CompressOpenFile(outfile
, compressionlevel
);
1561 catver
= formatspecs
[5];
1562 fileheaderver
= str(int(catver
.replace(".", "")));
1563 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
1564 catfp
.write(fileheader
.encode('UTF-8'));
1570 inodetocatinode
= {};
1571 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
1574 if(not tarfile
.is_tarfile(infile
)):
1576 except AttributeError:
1577 if(not is_tarfile(infile
)):
1580 tarfp
= tarfile
.open(infile
, "r");
1581 except FileNotFoundError
:
1583 fnumfiles
= format(int(len(tarfp
.getmembers())), 'x').lower();
1584 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[4]);
1585 if(checksumtype
=="none" or checksumtype
==""):
1586 catfileheadercshex
= format(0, 'x').lower();
1587 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1588 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1589 elif(checksumtype
=="crc16_ccitt"):
1590 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1591 elif(checksumtype
=="adler32"):
1592 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1593 elif(checksumtype
=="crc32"):
1594 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1595 elif(checksumtype
=="crc64_ecma"):
1596 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1597 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1598 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1599 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1600 checksumoutstr
= hashlib
.new(checksumtype
);
1601 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
1602 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1604 catfileheadercshex
= format(0, 'x').lower();
1605 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
1606 catfp
.write(fnumfilesa
.encode('UTF-8'));
1609 os
.fsync(catfp
.fileno());
1610 except io
.UnsupportedOperation
:
1612 except AttributeError:
1614 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
1615 catfhstart
= catfp
.tell();
1616 if(re
.findall("^[.|/]", member
.name
)):
1617 fname
= member
.name
;
1619 fname
= "./"+member
.name
;
1621 VerbosePrintOut(fname
);
1622 fpremode
= member
.mode
;
1623 ffullmode
= member
.mode
;
1627 ffullmode
= member
.mode
+ stat
.S_IFREG
;
1629 elif(member
.isdev()):
1630 ffullmode
= member
.mode
;
1632 elif(member
.islnk()):
1633 ffullmode
= member
.mode
+ stat
.S_IFREG
;
1635 elif(member
.issym()):
1636 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
1638 elif(member
.ischr()):
1639 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
1641 elif(member
.isblk()):
1642 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
1644 elif(member
.isdir()):
1645 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
1647 elif(member
.isfifo()):
1648 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
1650 elif(member
.issparse()):
1651 ffullmode
= member
.mode
;
1654 ffullmode
= member
.mode
;
1657 fcurfid
= format(int(curfid
), 'x').lower();
1658 fcurinode
= format(int(0), 'x').lower();
1659 curfid
= curfid
+ 1;
1661 flinkname
= member
.linkname
;
1662 fdev_minor
= format(int(member
.devminor
), 'x').lower();
1663 fdev_major
= format(int(member
.devmajor
), 'x').lower();
1664 frdev_minor
= format(int(member
.devminor
), 'x').lower();
1665 frdev_major
= format(int(member
.devmajor
), 'x').lower();
1666 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
1667 fsize
= format(int("0"), 'x').lower();
1668 elif(ftype
==0 or ftype
==7):
1669 fsize
= format(int(member
.size
), 'x').lower();
1671 fsize
= format(int(member
.size
), 'x').lower();
1672 fatime
= format(int(member
.mtime
), 'x').lower();
1673 fmtime
= format(int(member
.mtime
), 'x').lower();
1674 fctime
= format(int(member
.mtime
), 'x').lower();
1675 fbtime
= format(int(member
.mtime
), 'x').lower();
1676 fmode
= format(int(ffullmode
), 'x').lower();
1677 fchmode
= format(int(stat
.S_IMODE(ffullmode
)), 'x').lower();
1678 ftypemod
= format(int(stat
.S_IFMT(ffullmode
)), 'x').lower();
1679 fuid
= format(int(member
.uid
), 'x').lower();
1680 fgid
= format(int(member
.gid
), 'x').lower();
1681 funame
= member
.uname
;
1682 fgname
= member
.gname
;
1683 flinkcount
= format(int(flinkcount
), 'x').lower();
1684 fcontents
= "".encode('UTF-8');
1686 if(ftype
== 0 or ftype
== 7):
1687 with tarfp
.extractfile(member
) as fpc
:
1689 chunk
= fpc
.read(chunk_size
);
1693 ftypehex
= format(ftype
, 'x').lower();
1694 extrafields
= format(len(extradata
), 'x').lower();
1695 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
1696 if(len(extradata
)>0):
1697 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
1698 extrasizelen
= format(len(extrasizestr
), 'x').lower();
1699 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
[4]);
1700 if(len(extradata
)>0):
1701 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[4]);
1702 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[4]);
1703 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
1704 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
1705 if(checksumtype
=="none" or checksumtype
==""):
1706 catfileheadercshex
= format(0, 'x').lower();
1707 catfilecontentcshex
= format(0, 'x').lower();
1708 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1709 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
1710 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
1711 elif(checksumtype
=="crc16_ccitt"):
1712 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
1713 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
1714 elif(checksumtype
=="adler32"):
1715 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1716 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
1717 elif(checksumtype
=="crc32"):
1718 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1719 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
1720 elif(checksumtype
=="crc64_ecma"):
1721 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1722 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
1723 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1724 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1725 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
1726 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1727 checksumoutstr
= hashlib
.new(checksumtype
);
1728 checksumoutstr
.update("".encode('UTF-8'));
1729 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1730 checksumoutstr
= hashlib
.new(checksumtype
);
1731 checksumoutstr
.update(fcontents
);
1732 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
1734 catfileheadercshex
= format(0, 'x').lower();
1735 catfilecontentcshex
= format(0, 'x').lower();
1736 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
1737 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
1738 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[4]) + catfileoutstr
;
1739 if(checksumtype
=="none" or checksumtype
==""):
1740 catfileheadercshex
= format(0, 'x').lower();
1741 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1742 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
1743 elif(checksumtype
=="crc16_ccitt"):
1744 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
1745 elif(checksumtype
=="adler32"):
1746 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
1747 elif(checksumtype
=="crc32"):
1748 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
1749 elif(checksumtype
=="crc64_ecma"):
1750 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1751 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1752 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1753 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1754 checksumoutstr
= hashlib
.new(checksumtype
);
1755 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
1756 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1758 catfileheadercshex
= format(0, 'x').lower();
1759 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
1760 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
1761 nullstrecd
= formatspecs
[4].encode('UTF-8');
1762 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
1763 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
1764 catfp
.write(catfileout
);
1767 os
.fsync(catfp
.fileno());
1768 except io
.UnsupportedOperation
:
1770 except AttributeError:
1772 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1773 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1776 os
.fsync(catfp
.fileno());
1777 except io
.UnsupportedOperation
:
1779 except AttributeError:
1783 if(hasattr(sys
.stdout
, "buffer")):
1784 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1786 shutil
.copyfileobj(catfp
, sys
.stdout
);
1794 create_alias_function("Pack", __file_format_name__
, "FromTarFile", PackArchiveFileFromTarFile
);
1796 def PackArchiveFileFromZipFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1797 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1798 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1799 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1800 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1801 outfile
= RemoveWindowsPath(outfile
);
1802 checksumtype
= checksumtype
.lower();
1803 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
1804 checksumtype
="crc32";
1805 if(checksumtype
=="none"):
1807 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[1]):
1809 if(compression
not in compressionlist
and compression
is None):
1810 compression
= "auto";
1812 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1813 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1814 if(os
.path
.exists(outfile
)):
1819 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1822 fbasename
= os
.path
.splitext(outfile
)[0];
1823 fextname
= os
.path
.splitext(outfile
)[1];
1824 catfp
= CompressOpenFile(outfile
, compressionlevel
);
1825 catver
= formatspecs
[5];
1826 fileheaderver
= str(int(catver
.replace(".", "")));
1827 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
1828 catfp
.write(fileheader
.encode('UTF-8'));
1834 inodetocatinode
= {};
1835 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
1837 if(not zipfile
.is_zipfile(infile
)):
1839 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
1840 ziptest
= zipfp
.testzip();
1842 VerbosePrintOut("Bad file found!");
1843 fnumfiles
= format(int(len(zipfp
.infolist())), 'x').lower();
1844 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[4]);
1845 if(checksumtype
=="none" or checksumtype
==""):
1846 catfileheadercshex
= format(0, 'x').lower();
1847 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1848 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1849 elif(checksumtype
=="crc16_ccitt"):
1850 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1851 elif(checksumtype
=="adler32"):
1852 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1853 elif(checksumtype
=="crc32"):
1854 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1855 elif(checksumtype
=="crc64_ecma"):
1856 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1857 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1858 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1859 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1860 checksumoutstr
= hashlib
.new(checksumtype
);
1861 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
1862 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1864 catfileheadercshex
= format(0, 'x').lower();
1865 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
1866 catfp
.write(fnumfilesa
.encode('UTF-8'));
1869 os
.fsync(catfp
.fileno());
1870 except io
.UnsupportedOperation
:
1872 except AttributeError:
1874 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
1875 catfhstart
= catfp
.tell();
1876 if(re
.findall("^[.|/]", member
.filename
)):
1877 fname
= member
.filename
;
1879 fname
= "./"+member
.filename
;
1880 zipinfo
= zipfp
.getinfo(member
.filename
);
1882 VerbosePrintOut(fname
);
1883 if(not member
.is_dir()):
1884 fpremode
= int(stat
.S_IFREG
+ 438);
1885 elif(member
.is_dir()):
1886 fpremode
= int(stat
.S_IFDIR
+ 511);
1889 if(not member
.is_dir()):
1891 elif(member
.is_dir()):
1894 fcurfid
= format(int(curfid
), 'x').lower();
1895 fcurinode
= format(int(0), 'x').lower();
1896 curfid
= curfid
+ 1;
1897 fdev_minor
= format(int(0), 'x').lower();
1898 fdev_major
= format(int(0), 'x').lower();
1899 frdev_minor
= format(int(0), 'x').lower();
1900 frdev_major
= format(int(0), 'x').lower();
1902 fsize
= format(int("0"), 'x').lower();
1904 fsize
= format(int(member
.file_size
), 'x').lower();
1906 fsize
= format(int(member
.file_size
), 'x').lower();
1907 fatime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
1908 fmtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
1909 fctime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
1910 fbtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
1911 if(not member
.is_dir()):
1912 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
1913 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
1914 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
1915 if(member
.is_dir()):
1916 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
1917 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
1918 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
1920 fuid
= format(int(os
.getuid()), 'x').lower();
1921 except AttributeError:
1922 fuid
= format(int(0), 'x').lower();
1924 fuid
= format(int(0), 'x').lower();
1926 fgid
= format(int(os
.getgid()), 'x').lower();
1927 except AttributeError:
1928 fgid
= format(int(0), 'x').lower();
1930 fgid
= format(int(0), 'x').lower();
1934 userinfo
= pwd
.getpwuid(os
.getuid());
1935 funame
= userinfo
.pw_name
;
1938 except AttributeError:
1946 groupinfo
= grp
.getgrgid(os
.getgid());
1947 fgname
= groupinfo
.gr_name
;
1950 except AttributeError:
1954 fcontents
= "".encode('UTF-8');
1956 fcontents
= zipfp
.read(member
.filename
);
1957 ftypehex
= format(ftype
, 'x').lower();
1958 extrafields
= format(len(extradata
), 'x').lower();
1959 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
1960 if(len(extradata
)>0):
1961 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
1962 extrasizelen
= format(len(extrasizestr
), 'x').lower();
1963 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
[4]);
1964 if(len(extradata
)>0):
1965 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[4]);
1966 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[4]);
1967 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
1968 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
1969 if(checksumtype
=="none" or checksumtype
==""):
1970 catfileheadercshex
= format(0, 'x').lower();
1971 catfilecontentcshex
= format(0, 'x').lower();
1972 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1973 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
1974 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
1975 elif(checksumtype
=="crc16_ccitt"):
1976 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
1977 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
1978 elif(checksumtype
=="adler32"):
1979 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1980 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
1981 elif(checksumtype
=="crc32"):
1982 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1983 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
1984 elif(checksumtype
=="crc64_ecma"):
1985 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1986 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
1987 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1988 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1989 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
1990 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1991 checksumoutstr
= hashlib
.new(checksumtype
);
1992 checksumoutstr
.update("".encode('UTF-8'));
1993 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1994 checksumoutstr
= hashlib
.new(checksumtype
);
1995 checksumoutstr
.update(fcontents
);
1996 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
1998 catfileheadercshex
= format(0, 'x').lower();
1999 catfilecontentcshex
= format(0, 'x').lower();
2000 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
2001 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2002 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[4]) + catfileoutstr
;
2003 if(checksumtype
=="none" or checksumtype
==""):
2004 catfileheadercshex
= format(0, 'x').lower();
2005 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2006 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2007 elif(checksumtype
=="crc16_ccitt"):
2008 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2009 elif(checksumtype
=="adler32"):
2010 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2011 elif(checksumtype
=="crc32"):
2012 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2013 elif(checksumtype
=="crc64_ecma"):
2014 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2015 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2016 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2017 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2018 checksumoutstr
= hashlib
.new(checksumtype
);
2019 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
2020 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2022 catfileheadercshex
= format(0, 'x').lower();
2023 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
2024 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
2025 nullstrecd
= formatspecs
[4].encode('UTF-8');
2026 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
2027 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
2028 catfp
.write(catfileout
);
2031 os
.fsync(catfp
.fileno());
2032 except io
.UnsupportedOperation
:
2034 except AttributeError:
2036 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2037 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2040 os
.fsync(catfp
.fileno());
2041 except io
.UnsupportedOperation
:
2043 except AttributeError:
2047 if(hasattr(sys
.stdout
, "buffer")):
2048 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2050 shutil
.copyfileobj(catfp
, sys
.stdout
);
2058 create_alias_function("Pack", __file_format_name__
, "FromZipFile", PackArchiveFileFromZipFile
);
2060 if(not rarfile_support
):
2061 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2064 if(rarfile_support
):
2065 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2066 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2067 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2068 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
2069 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2070 outfile
= RemoveWindowsPath(outfile
);
2071 checksumtype
= checksumtype
.lower();
2072 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2073 checksumtype
="crc32";
2074 if(checksumtype
=="none"):
2076 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[1]):
2078 if(compression
not in compressionlist
and compression
is None):
2079 compression
= "auto";
2081 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2082 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2083 if(os
.path
.exists(outfile
)):
2088 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2091 fbasename
= os
.path
.splitext(outfile
)[0];
2092 fextname
= os
.path
.splitext(outfile
)[1];
2093 catfp
= CompressOpenFile(outfile
, compressionlevel
);
2094 catver
= formatspecs
[5];
2095 fileheaderver
= str(int(catver
.replace(".", "")));
2096 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
2097 catfp
.write(fileheader
.encode('UTF-8'));
2103 inodetocatinode
= {};
2104 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2106 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
2108 rarfp
= rarfile
.RarFile(infile
, "r");
2109 rartest
= rarfp
.testrar();
2111 VerbosePrintOut("Bad file found!");
2112 fnumfiles
= format(int(len(rarfp
.infolist())), 'x').lower();
2113 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[4]);
2114 if(checksumtype
=="none" or checksumtype
==""):
2115 catfileheadercshex
= format(0, 'x').lower();
2116 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2117 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2118 elif(checksumtype
=="crc16_ccitt"):
2119 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2120 elif(checksumtype
=="adler32"):
2121 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2122 elif(checksumtype
=="crc32"):
2123 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2124 elif(checksumtype
=="crc64_ecma"):
2125 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2126 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2127 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2128 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2129 checksumoutstr
= hashlib
.new(checksumtype
);
2130 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
2131 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2133 catfileheadercshex
= format(0, 'x').lower();
2134 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
2135 catfp
.write(fnumfilesa
.encode('UTF-8'));
2138 os
.fsync(catfp
.fileno());
2139 except io
.UnsupportedOperation
:
2141 except AttributeError:
2143 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
2146 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
2149 member
.external_attr
2151 except AttributeError:
2153 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
2156 member
.external_attr
2158 except AttributeError:
2163 catfhstart
= catfp
.tell();
2164 if(re
.findall("^[.|/]", member
.filename
)):
2165 fname
= member
.filename
;
2167 fname
= "./"+member
.filename
;
2168 rarinfo
= rarfp
.getinfo(member
.filename
);
2170 VerbosePrintOut(fname
);
2171 if(is_unix
and member
.external_attr
!=0):
2172 fpremode
= int(member
.external_attr
);
2173 elif(member
.is_file()):
2174 fpremode
= int(stat
.S_IFREG
+ 438);
2175 elif(member
.is_symlink()):
2176 fpremode
= int(stat
.S_IFLNK
+ 438);
2177 elif(member
.is_dir()):
2178 fpremode
= int(stat
.S_IFDIR
+ 511);
2179 if(is_windows
and member
.external_attr
!=0):
2180 fwinattributes
= format(int(member
.external_attr
), 'x').lower();
2182 fwinattributes
= format(int(0), 'x').lower();
2185 if(member
.is_file()):
2187 elif(member
.is_symlink()):
2189 elif(member
.is_dir()):
2193 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
2194 fcurfid
= format(int(curfid
), 'x').lower();
2195 fcurinode
= format(int(0), 'x').lower();
2196 curfid
= curfid
+ 1;
2197 fdev_minor
= format(int(0), 'x').lower();
2198 fdev_major
= format(int(0), 'x').lower();
2199 frdev_minor
= format(int(0), 'x').lower();
2200 frdev_major
= format(int(0), 'x').lower();
2202 fsize
= format(int("0"), 'x').lower();
2204 fsize
= format(int(member
.file_size
), 'x').lower();
2206 fsize
= format(int(member
.file_size
), 'x').lower();
2209 fatime
= format(int(member
.atime
.timestamp()), 'x').lower();
2211 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2212 except AttributeError:
2213 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2214 fmtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2217 fctime
= format(int(member
.ctime
.timestamp()), 'x').lower();
2219 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2220 except AttributeError:
2221 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2222 fbtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2223 if(is_unix
and member
.external_attr
!=0):
2224 fmode
= format(int(member
.external_attr
), 'x').lower();
2225 fchmode
= format(int(stat
.S_IMODE(member
.external_attr
)), 'x').lower();
2226 ftypemod
= format(int(stat
.S_IFMT(member
.external_attr
)), 'x').lower();
2227 elif(member
.is_file()):
2228 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
2229 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
2230 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
2231 elif(member
.is_symlink()):
2232 fmode
= format(int(stat
.S_IFLNK
+ 438), 'x').lower();
2233 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
2234 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
2235 elif(member
.is_dir()):
2236 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
2237 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
2238 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
2240 fuid
= format(int(os
.getuid()), 'x').lower();
2241 except AttributeError:
2242 fuid
= format(int(0), 'x').lower();
2244 fuid
= format(int(0), 'x').lower();
2246 fgid
= format(int(os
.getgid()), 'x').lower();
2247 except AttributeError:
2248 fgid
= format(int(0), 'x').lower();
2250 fgid
= format(int(0), 'x').lower();
2254 userinfo
= pwd
.getpwuid(os
.getuid());
2255 funame
= userinfo
.pw_name
;
2258 except AttributeError:
2266 groupinfo
= grp
.getgrgid(os
.getgid());
2267 fgname
= groupinfo
.gr_name
;
2270 except AttributeError:
2274 fcontents
= "".encode('UTF-8');
2276 fcontents
= rarfp
.read(member
.filename
);
2277 ftypehex
= format(ftype
, 'x').lower();
2278 extrafields
= format(len(extradata
), 'x').lower();
2279 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
2280 if(len(extradata
)>0):
2281 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
2282 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2283 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
[4]);
2284 if(len(extradata
)>0):
2285 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[4]);
2286 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[4]);
2287 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
2288 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
2289 if(checksumtype
=="none" or checksumtype
==""):
2290 catfileheadercshex
= format(0, 'x').lower();
2291 catfilecontentcshex
= format(0, 'x').lower();
2292 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2293 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2294 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
2295 elif(checksumtype
=="crc16_ccitt"):
2296 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2297 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
2298 elif(checksumtype
=="adler32"):
2299 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2300 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
2301 elif(checksumtype
=="crc32"):
2302 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2303 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
2304 elif(checksumtype
=="crc64_ecma"):
2305 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2306 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
2307 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2308 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2309 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
2310 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2311 checksumoutstr
= hashlib
.new(checksumtype
);
2312 checksumoutstr
.update("".encode('UTF-8'));
2313 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2314 checksumoutstr
= hashlib
.new(checksumtype
);
2315 checksumoutstr
.update(fcontents
);
2316 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
2318 catfileheadercshex
= format(0, 'x').lower();
2319 catfilecontentcshex
= format(0, 'x').lower();
2320 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
2321 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2322 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[4]) + catfileoutstr
2323 if(checksumtype
=="none" or checksumtype
==""):
2324 catfileheadercshex
= format(0, 'x').lower()
2325 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2326 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower()
2327 elif(checksumtype
=="crc16_ccitt"):
2328 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower()
2329 elif(checksumtype
=="adler32"):
2330 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower()
2331 elif(checksumtype
=="crc32"):
2332 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower()
2333 elif(checksumtype
=="crc64_ecma"):
2334 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
2335 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2336 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
2337 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2338 checksumoutstr
= hashlib
.new(checksumtype
)
2339 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'))
2340 catfileheadercshex
= checksumoutstr
.hexdigest().lower()
2342 catfileheadercshex
= format(0, 'x').lower()
2343 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4])
2344 catfileoutstrecd
= catfileoutstr
.encode('UTF-8')
2345 nullstrecd
= formatspecs
[4].encode('UTF-8')
2346 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
2347 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
)
2348 catfp
.write(catfileout
)
2351 os
.fsync(catfp
.fileno())
2352 except io
.UnsupportedOperation
:
2354 except AttributeError:
2356 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2357 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
)
2360 os
.fsync(catfp
.fileno())
2361 except io
.UnsupportedOperation
:
2363 except AttributeError:
2367 if(hasattr(sys
.stdout
, "buffer")):
2368 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer)
2370 shutil
.copyfileobj(catfp
, sys
.stdout
)
2378 create_alias_function("Pack", __file_format_name__
, "FromRarFile", PackArchiveFileFromRarFile
);
2380 def ArchiveFileSeekToFileNum(infile
, seekto
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
2381 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2384 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2385 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
2386 if(checkcompressfile
=="tarfile"):
2387 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2388 if(checkcompressfile
=="zipfile"):
2389 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2390 if(rarfile_support
and checkcompressfile
=="rarfile"):
2391 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2392 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[1]):
2399 if(hasattr(sys
.stdin
, "buffer")):
2400 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
2402 shutil
.copyfileobj(sys
.stdin
, catfp
);
2404 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2409 infile
= RemoveWindowsPath(infile
);
2410 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
2411 if(checkcompressfile
=="tarfile"):
2412 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2413 if(checkcompressfile
=="zipfile"):
2414 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2415 if(rarfile_support
and checkcompressfile
=="rarfile"):
2416 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2417 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[1]):
2419 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
2420 if(not compresscheck
):
2421 fextname
= os
.path
.splitext(infile
)[1];
2422 if(fextname
==".gz"):
2423 compresscheck
= "gzip";
2424 if(fextname
==".bz2"):
2425 compresscheck
= "bzip2";
2426 if(fextname
==".zst"):
2427 compresscheck
= "zstd";
2428 if(fextname
==".lz4" or fextname
==".clz4"):
2429 compresscheck
= "lz4";
2430 if(fextname
==".lzo" or fextname
==".lzop"):
2431 compresscheck
= "lzo";
2432 if(fextname
==".lzma" or fextname
==".xz"):
2433 compresscheck
= "lzma";
2434 if(not compresscheck
):
2436 catfp
= UncompressFile(infile
, formatspecs
, "rb");
2441 SeekToEndOfFile(catfp);
2443 SeekToEndOfFile(catfp);
2444 CatSize = catfp.tell();
2445 CatSizeEnd = CatSize;
2453 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[4]);
2454 catstring
= catheader
[0];
2455 catversion
= re
.findall(r
"([\d]+)$", catstring
);
2456 fprenumfiles
= catheader
[1];
2457 fnumfiles
= int(fprenumfiles
, 16);
2458 fprechecksumtype
= catheader
[2];
2459 fprechecksum
= catheader
[3];
2460 fileheader
= AppendNullByte(catstring
, formatspecs
[4]);
2461 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
2462 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[4]);
2463 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
2464 catfileheadercshex
= format(0, 'x').lower();
2465 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
2466 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2467 elif(fprechecksumtype
=="crc16_ccitt"):
2468 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2469 elif(fprechecksumtype
=="adler32"):
2470 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2471 elif(fprechecksumtype
=="crc32"):
2472 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2473 elif(fprechecksumtype
=="crc64_ecma"):
2474 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2475 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
2476 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2477 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
2478 checksumoutstr
= hashlib
.new(fprechecksumtype
);
2479 checksumoutstr
.update(fileheader
.encode('UTF-8'));
2480 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2482 catfileheadercshex
= format(0, 'x').lower();
2483 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
2484 fheadtell
= len(fileheader
);
2485 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
2486 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(catfp
.tell()));
2488 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
2489 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
2490 if(seekto
>=fnumfiles
):
2491 seekto
= fnumfiles
- 1;
2497 seekstart
= catfp
.tell();
2498 preheaderdata
= ReadFileHeaderData(catfp
, 5, formatspecs
[4]);
2499 prefheadsize
= int(preheaderdata
[0], 16);
2500 prefseek
= prefheadsize
- (int(len(preheaderdata
[1]) + 1) + int(len(preheaderdata
[2]) + 1) + int(len(preheaderdata
[3]) + 1) + int(len(preheaderdata
[4]) + 1));
2501 preftype
= int(preheaderdata
[1], 16);
2502 prefsize
= int(preheaderdata
[4], 16);
2503 catfp
.seek(prefseek
, 1);
2505 catfp
.seek(prefsize
, 1);
2508 catfp
.seek(seekstart
, 0);
2510 catfheadsize
= int(preheaderdata
[0], 16);
2511 catftype
= int(preheaderdata
[1], 16);
2512 if(re
.findall("^[.|/]", preheaderdata
[2])):
2513 catfname
= preheaderdata
[2];
2515 catfname
= "./"+preheaderdata
[2];
2516 catflinkname
= preheaderdata
[3];
2517 catfsize
= int(preheaderdata
[4], 16);
2518 catfbasedir
= os
.path
.dirname(catfname
);
2519 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
2521 catlist
.update({'catfp': catfp
});
2526 create_alias_function("", __file_format_name__
, "SeekToFileNum", ArchiveFileSeekToFileNum
);
2528 def ArchiveFileSeekToFileName(infile
, seekfile
=None, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
2529 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2532 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2533 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
2534 if(checkcompressfile
=="tarfile"):
2535 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2536 if(checkcompressfile
=="zipfile"):
2537 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2538 if(rarfile_support
and checkcompressfile
=="rarfile"):
2539 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2540 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[1]):
2547 if(hasattr(sys
.stdin
, "buffer")):
2548 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
2550 shutil
.copyfileobj(sys
.stdin
, catfp
);
2552 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2557 infile
= RemoveWindowsPath(infile
);
2558 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
2559 if(checkcompressfile
=="tarfile"):
2560 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2561 if(checkcompressfile
=="zipfile"):
2562 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2563 if(rarfile_support
and checkcompressfile
=="rarfile"):
2564 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2565 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[1]):
2567 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
2568 if(not compresscheck
):
2569 fextname
= os
.path
.splitext(infile
)[1];
2570 if(fextname
==".gz"):
2571 compresscheck
= "gzip";
2572 if(fextname
==".bz2"):
2573 compresscheck
= "bzip2";
2574 if(fextname
==".zst"):
2575 compresscheck
= "zstd";
2576 if(fextname
==".lz4" or fextname
==".clz4"):
2577 compresscheck
= "lz4";
2578 if(fextname
==".lzo" or fextname
==".lzop"):
2579 compresscheck
= "lzo";
2580 if(fextname
==".lzma" or fextname
==".xz"):
2581 compresscheck
= "lzma";
2582 if(not compresscheck
):
2584 catfp
= UncompressFile(infile
, formatspecs
, "rb");
2589 SeekToEndOfFile(catfp);
2591 SeekToEndOfFile(catfp);
2592 CatSize = catfp.tell();
2593 CatSizeEnd = CatSize;
2601 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[4]);
2602 catstring
= catheader
[0];
2603 catversion
= re
.findall(r
"([\d]+)$", catstring
);
2604 fprenumfiles
= catheader
[1];
2605 fnumfiles
= int(fprenumfiles
, 16);
2606 fprechecksumtype
= catheader
[2];
2607 fprechecksum
= catheader
[3];
2608 fileheader
= AppendNullByte(catstring
, formatspecs
[4]);
2609 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
2610 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[4]);
2611 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
2612 catfileheadercshex
= format(0, 'x').lower();
2613 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
2614 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2615 elif(fprechecksumtype
=="crc16_ccitt"):
2616 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2617 elif(fprechecksumtype
=="adler32"):
2618 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2619 elif(fprechecksumtype
=="crc32"):
2620 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2621 elif(fprechecksumtype
=="crc64_ecma"):
2622 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2623 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
2624 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2625 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
2626 checksumoutstr
= hashlib
.new(fprechecksumtype
);
2627 checksumoutstr
.update(fileheader
.encode('UTF-8'));
2628 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2630 catfileheadercshex
= format(0, 'x').lower();
2631 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
2632 fheadtell
= len(fileheader
);
2633 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
2634 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(catfp
.tell()));
2636 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
2637 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
2638 seekto
= fnumfiles
- 1
2643 seekstart
= catfp
.tell();
2644 preheaderdata
= ReadFileHeaderData(catfp
, 5, formatspecs
[4]);
2645 prefheadsize
= int(preheaderdata
[0], 16);
2646 prefseek
= prefheadsize
- (int(len(preheaderdata
[1]) + 1) + int(len(preheaderdata
[2]) + 1) + int(len(preheaderdata
[3]) + 1) + int(len(preheaderdata
[4]) + 1));
2647 preftype
= int(preheaderdata
[1], 16);
2648 prefsize
= int(preheaderdata
[4], 16);
2649 catfp
.seek(prefseek
, 1);
2651 catfp
.seek(prefsize
, 1);
2655 prefname
= preheaderdata
[2];
2656 if(re
.findall("^[.|/]", preheaderdata
[2])):
2657 prefname
= preheaderdata
[2];
2659 prefname
= "./"+preheaderdata
[2];
2660 if(prefname
==seekfile
):
2663 catfp
.seek(seekstart
, 0);
2665 catfheadsize
= int(preheaderdata
[0], 16);
2666 catftype
= int(preheaderdata
[1], 16);
2667 if(re
.findall("^[.|/]", preheaderdata
[2])):
2668 catfname
= preheaderdata
[2];
2670 catfname
= "./"+preheaderdata
[2];
2671 catflinkname
= preheaderdata
[3];
2672 catfsize
= int(preheaderdata
[4], 16);
2673 catfbasedir
= os
.path
.dirname(catfname
);
2675 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
2679 catlist
.update({'catfp': catfp
});
2684 create_alias_function("", __file_format_name__
, "SeekToFileName", ArchiveFileSeekToFileName
);
2686 def ArchiveFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, usenewstyle
=True, returnfp
=False):
2687 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2690 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2691 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
2692 if(checkcompressfile
=="tarfile"):
2693 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
2694 if(checkcompressfile
=="zipfile"):
2695 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
2696 if(rarfile_support
and checkcompressfile
=="rarfile"):
2697 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
2698 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[1]):
2705 if(hasattr(sys
.stdin
, "buffer")):
2706 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
2708 shutil
.copyfileobj(sys
.stdin
, catfp
);
2710 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2715 infile
= RemoveWindowsPath(infile
);
2716 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
2717 if(checkcompressfile
=="tarfile"):
2718 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
2719 if(checkcompressfile
=="zipfile"):
2720 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
2721 if(rarfile_support
and checkcompressfile
=="rarfile"):
2722 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
2723 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[1]):
2725 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
2726 if(not compresscheck
):
2727 fextname
= os
.path
.splitext(infile
)[1];
2728 if(fextname
==".gz"):
2729 compresscheck
= "gzip";
2730 if(fextname
==".bz2"):
2731 compresscheck
= "bzip2";
2732 if(fextname
==".zst"):
2733 compresscheck
= "zstd";
2734 if(fextname
==".lz4" or fextname
==".clz4"):
2735 compresscheck
= "lz4";
2736 if(fextname
==".lzo" or fextname
==".lzop"):
2737 compresscheck
= "lzo";
2738 if(fextname
==".lzma" or fextname
==".xz"):
2739 compresscheck
= "lzma";
2740 if(not compresscheck
):
2742 catfp
= UncompressFile(infile
, formatspecs
, "rb");
2747 SeekToEndOfFile(catfp);
2749 SeekToEndOfFile(catfp);
2750 CatSize = catfp.tell();
2751 CatSizeEnd = CatSize;
2759 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[4]);
2760 catstring
= catheader
[0];
2761 catversion
= re
.findall(r
"([\d]+)$", catstring
);
2762 fprenumfiles
= catheader
[1];
2763 fnumfiles
= int(fprenumfiles
, 16);
2764 fprechecksumtype
= catheader
[2];
2765 fprechecksum
= catheader
[3];
2766 fileheader
= AppendNullByte(catstring
, formatspecs
[4]);
2767 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
2768 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[4]);
2769 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
2770 catfileheadercshex
= format(0, 'x').lower();
2771 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
2772 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2773 elif(fprechecksumtype
=="crc16_ccitt"):
2774 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2775 elif(fprechecksumtype
=="adler32"):
2776 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2777 elif(fprechecksumtype
=="crc32"):
2778 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2779 elif(fprechecksumtype
=="crc64_ecma"):
2780 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2781 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
2782 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2783 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
2784 checksumoutstr
= hashlib
.new(fprechecksumtype
);
2785 checksumoutstr
.update(fileheader
.encode('UTF-8'));
2786 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2788 catfileheadercshex
= format(0, 'x').lower();
2789 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
2790 fheadtell
= len(fileheader
);
2791 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
2792 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(catfp
.tell()));
2794 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
2795 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
2796 if(seekstart
<0 and seekstart
>fnumfiles
):
2798 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
2799 seekend
= fnumfiles
;
2800 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
2801 seekend
= fnumfiles
- abs(seekend
);
2803 seekend
= fnumfiles
;
2806 while(il
< seekstart
):
2807 preheaderdata
= ReadFileHeaderData(catfp
, 5, formatspecs
[4]);
2808 prefheadsize
= int(preheaderdata
[0], 16);
2809 prefseek
= prefheadsize
- (int(len(preheaderdata
[1]) + 1) + int(len(preheaderdata
[2]) + 1) + int(len(preheaderdata
[3]) + 1) + int(len(preheaderdata
[4]) + 1));
2810 preftype
= int(preheaderdata
[1], 16);
2811 prefsize
= int(preheaderdata
[4], 16);
2812 catfp
.seek(prefseek
, 1);
2814 catfp
.seek(prefsize
, 1);
2817 fileidnum
= seekstart
;
2819 while(fileidnum
<seekend
):
2820 catfhstart
= catfp
.tell();
2822 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[4]);
2824 catheaderdata
= ReadFileHeaderData(catfp
, 23, formatspecs
[4]);
2825 catfheadsize
= int(catheaderdata
[0], 16);
2826 catftype
= int(catheaderdata
[1], 16);
2827 if(re
.findall("^[.|/]", catheaderdata
[2])):
2828 catfname
= catheaderdata
[2];
2830 catfname
= "./"+catheaderdata
[2];
2831 catfbasedir
= os
.path
.dirname(catfname
);
2832 catflinkname
= catheaderdata
[3];
2833 catfsize
= int(catheaderdata
[4], 16);
2834 catfatime
= int(catheaderdata
[5], 16);
2835 catfmtime
= int(catheaderdata
[6], 16);
2836 catfctime
= int(catheaderdata
[7], 16);
2837 catfbtime
= int(catheaderdata
[8], 16);
2838 catfmode
= int(catheaderdata
[9], 16);
2839 catfchmode
= stat
.S_IMODE(catfmode
);
2840 catftypemod
= stat
.S_IFMT(catfmode
);
2841 catfuid
= int(catheaderdata
[10], 16);
2842 catfuname
= catheaderdata
[11];
2843 catfgid
= int(catheaderdata
[12], 16);
2844 catfgname
= catheaderdata
[13];
2845 fid
= int(catheaderdata
[14], 16);
2846 finode
= int(catheaderdata
[15], 16);
2847 flinkcount
= int(catheaderdata
[16], 16);
2848 catfdev_minor
= int(catheaderdata
[17], 16);
2849 catfdev_major
= int(catheaderdata
[18], 16);
2850 catfrdev_minor
= int(catheaderdata
[19], 16);
2851 catfrdev_major
= int(catheaderdata
[20], 16);
2852 catfextrasize
= int(catheaderdata
[21], 16);
2853 catfextrafields
= int(catheaderdata
[22], 16);
2854 extrafieldslist
= [];
2857 extraend
= extrastart
+ catfextrafields
;
2858 extrafieldslist
= [];
2859 if(extrastart
<extraend
):
2860 extrafieldslist
.append(catheaderdata
[extrastart
]);
2861 extrastart
= extrastart
+ 1;
2862 catfchecksumtype
= catheaderdata
[extrastart
].lower();
2863 catfcs
= catheaderdata
[extrastart
+ 1].lower();
2864 catfccs
= catheaderdata
[extrastart
+ 2].lower();
2866 extrafieldslist
= [];
2867 if(catfextrafields
>0):
2868 extrafieldslist
= ReadFileHeaderData(catfp
, catfextrafields
, formatspecs
[4]);
2869 checksumsval
= ReadFileHeaderData(catfp
, 3, formatspecs
[4]);
2870 catfchecksumtype
= checksumsval
[0].lower();
2871 catfcs
= checksumsval
[1].lower();
2872 catfccs
= checksumsval
[2].lower();
2875 hcmax
= len(catheaderdata
) - 2;
2877 hcmax
= len(catheaderdata
);
2880 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[4]);
2882 catfnumfields
= 24 + catfextrafields
;
2883 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
2885 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
2886 catnewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
2887 elif(catfchecksumtype
=="adler32"):
2888 catnewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2889 elif(catfchecksumtype
=="crc32"):
2890 catnewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2891 elif(catfchecksumtype
=="crc64_ecma"):
2892 catnewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2893 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
2894 catnewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2895 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
2896 checksumoutstr
= hashlib
.new(catfchecksumtype
);
2897 checksumoutstr
.update(hout
.encode('UTF-8'));
2898 catnewfcs
= checksumoutstr
.hexdigest().lower();
2899 if(catfcs
!=catnewfcs
and not skipchecksum
):
2900 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfhstart
));
2902 catfhend
= catfp
.tell() - 1;
2903 catfcontentstart
= catfp
.tell();
2905 pyhascontents
= False;
2906 if(catfsize
>0 and not listonly
):
2907 catfcontents
= catfp
.read(catfsize
);
2908 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
2910 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
2911 catnewfccs
= format(crc16(catfcontents
) & 0xffff, '04x').lower();
2912 elif(catfchecksumtype
=="crc16_ccitt"):
2913 catnewfcs
= format(crc16_ccitt(catfcontents
) & 0xffff, '04x').lower();
2914 elif(catfchecksumtype
=="adler32"):
2915 catnewfccs
= format(zlib
.adler32(catfcontents
) & 0xffffffff, '08x').lower();
2916 elif(catfchecksumtype
=="crc32"):
2917 catnewfccs
= format(crc32(catfcontents
) & 0xffffffff, '08x').lower();
2918 elif(catfchecksumtype
=="crc64_ecma"):
2919 catnewfcs
= format(crc64_ecma(catfcontents
) & 0xffffffffffffffff, '016x').lower();
2920 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
2921 catnewfcs
= format(crc64_iso(catfcontents
) & 0xffffffffffffffff, '016x').lower();
2922 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
2923 checksumoutstr
= hashlib
.new(catfchecksumtype
);
2924 checksumoutstr
.update(catfcontents
);
2925 catnewfccs
= checksumoutstr
.hexdigest().lower();
2926 pyhascontents
= True;
2927 if(catfccs
!=catnewfccs
and skipchecksum
):
2928 VerbosePrintOut("File Content Checksum Error with file " + catfname
+ " at offset " + str(catfhstart
));
2930 if(catfsize
>0 and listonly
):
2931 catfp
.seek(catfsize
, 1);
2932 pyhascontents
= False;
2934 catfcontentend
= catfp
.tell() - 1;
2935 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
} });
2936 fileidnum
= fileidnum
+ 1;
2937 realidnum
= realidnum
+ 1;
2939 catlist
.update({'catfp': catfp
});
2944 create_alias_function("", __file_format_name__
, "ToArray", ArchiveFileToArray
);
2946 def ArchiveFileStringToArray(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, usenewstyle
=True, returnfp
=False):
2947 catfp
= BytesIO(catstr
);
2948 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
2949 return listcatfiles
;
2951 create_alias_function("", __file_format_name__
, "StringToArray", ArchiveFileStringToArray
);
2953 def TarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, usenewstyle
=True, returnfp
=False):
2955 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
2956 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
2957 return listcatfiles
;
2959 def ZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, usenewstyle
=True, returnfp
=False):
2961 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
2962 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
2963 return listcatfiles
;
2965 if(not rarfile_support
):
2966 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
2969 if(rarfile_support
):
2970 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, usenewstyle
=True, returnfp
=False):
2972 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
2973 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
2974 return listcatfiles
;
2976 def ListDirToArrayAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
2977 catver
= formatspecs
[5];
2978 fileheaderver
= str(int(catver
.replace(".", "")));
2979 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
2980 advancedlist
= True;
2983 for line
in sys
.stdin
:
2984 infilelist
.append(line
.strip());
2985 infilelist
= list(filter(None, infilelist
));
2986 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
2987 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
2989 with
open(infiles
, "r") as finfile
:
2990 for line
in finfile
:
2991 infilelist
.append(line
.strip());
2992 infilelist
= list(filter(None, infilelist
));
2994 if(isinstance(infiles
, (list, tuple, ))):
2995 infilelist
= list(filter(None, infiles
));
2996 elif(isinstance(infiles
, (str, ))):
2997 infilelist
= list(filter(None, [infiles
]));
2999 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
3001 GetDirList
= ListDir(infilelist
, followlink
, False);
3009 inodetocatinode
= {};
3011 fnumfiles
= int(len(GetDirList
));
3012 catver
= formatspecs
[5];
3013 fileheaderver
= str(int(catver
.replace(".", "")));
3014 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
3015 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3016 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[4]);
3017 catversion
= fileheaderver
;
3018 if(checksumtype
=="none" or checksumtype
==""):
3019 catfileheadercshex
= format(0, 'x').lower();
3020 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3021 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3022 elif(checksumtype
=="crc16_ccitt"):
3023 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3024 elif(checksumtype
=="adler32"):
3025 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3026 elif(checksumtype
=="crc32"):
3027 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3028 elif(checksumtype
=="crc64_ecma"):
3029 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3030 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3031 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3032 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3033 checksumoutstr
= hashlib
.new(checksumtype
);
3034 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3035 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3037 catfileheadercshex
= format(0, 'x').lower();
3038 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
3039 fheadtell
= len(fileheader
);
3040 catlist
= {'fnumfiles': fnumfiles
, 'fformat': formatspecs
[0], 'fversion': catversion
, 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
3041 for curfname
in GetDirList
:
3042 if(re
.findall("^[.|/]", curfname
)):
3045 fname
= "./"+curfname
;
3047 VerbosePrintOut(fname
);
3048 if(not followlink
or followlink
is None):
3049 fstatinfo
= os
.lstat(fname
);
3051 fstatinfo
= os
.stat(fname
);
3052 fpremode
= fstatinfo
.st_mode
;
3053 finode
= fstatinfo
.st_ino
;
3054 flinkcount
= fstatinfo
.st_nlink
;
3056 if(stat
.S_ISREG(fpremode
)):
3058 elif(stat
.S_ISLNK(fpremode
)):
3060 elif(stat
.S_ISCHR(fpremode
)):
3062 elif(stat
.S_ISBLK(fpremode
)):
3064 elif(stat
.S_ISDIR(fpremode
)):
3066 elif(stat
.S_ISFIFO(fpremode
)):
3068 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
3070 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
3072 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
3077 fbasedir
= os
.path
.dirname(fname
);
3079 if(not followlink
and finode
!=0):
3081 if(finode
in inodelist
):
3083 flinkname
= inodetofile
[finode
];
3084 fcurinode
= inodetocatinode
[finode
];
3085 if(finode
not in inodelist
):
3086 inodelist
.append(finode
);
3087 inodetofile
.update({finode
: fname
});
3088 inodetocatinode
.update({finode
: curinode
});
3089 fcurinode
= curinode
;
3090 curinode
= curinode
+ 1;
3092 fcurinode
= curinode
;
3093 curinode
= curinode
+ 1;
3094 curfid
= curfid
+ 1;
3096 flinkname
= os
.readlink(fname
);
3097 fdev
= fstatinfo
.st_dev
;
3098 getfdev
= GetDevMajorMinor(fdev
);
3099 fdev_minor
= getfdev
[0];
3100 fdev_major
= getfdev
[1];
3101 frdev
= fstatinfo
.st_dev
;
3102 if(hasattr(fstatinfo
, "st_rdev")):
3103 frdev
= fstatinfo
.st_rdev
;
3105 frdev
= fstatinfo
.st_dev
;
3106 getfrdev
= GetDevMajorMinor(frdev
);
3107 frdev_minor
= getfrdev
[0];
3108 frdev_major
= getfrdev
[1];
3109 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
3111 if(ftype
==0 or ftype
==7):
3112 fsize
= fstatinfo
.st_size
;
3113 fatime
= fstatinfo
.st_atime
;
3114 fmtime
= fstatinfo
.st_mtime
;
3115 fctime
= fstatinfo
.st_ctime
;
3116 if(hasattr(fstatinfo
, "st_birthtime")):
3117 fbtime
= fstatinfo
.st_birthtime
;
3119 fbtime
= fstatinfo
.st_ctime
;
3120 fmode
= fstatinfo
.st_mode
;
3121 fchmode
= stat
.S_IMODE(fstatinfo
.st_mode
);
3122 ftypemod
= stat
.S_IFMT(fstatinfo
.st_mode
);
3123 fuid
= fstatinfo
.st_uid
;
3124 fgid
= fstatinfo
.st_gid
;
3129 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
3130 funame
= userinfo
.pw_name
;
3139 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
3140 fgname
= groupinfo
.gr_name
;
3145 fdev_minor
= fdev_minor
;
3146 fdev_major
= fdev_major
;
3147 frdev_minor
= frdev_minor
;
3148 frdev_major
= frdev_major
;
3150 flinkcount
= flinkcount
;
3151 if(hasattr(fstatinfo
, "st_file_attributes")):
3152 fwinattributes
= fstatinfo
.st_file_attributes
;
3155 fcontents
= "".encode('UTF-8');
3157 if(ftype
== 0 or ftype
== 7):
3158 with
open(fname
, "rb") as fpc
:
3160 chunk
= fpc
.read(chunk_size
);
3164 if(followlink
and (ftype
== 1 or ftype
== 2)):
3165 flstatinfo
= os
.stat(flinkname
);
3166 with
open(flinkname
, "rb") as fpc
:
3168 chunk
= fpc
.read(chunk_size
);
3172 ftypehex
= format(ftype
, 'x').lower();
3173 extrafields
= len(extradata
);
3174 extrafieldslist
= extradata
;
3175 catfextrafields
= extrafields
;
3176 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
3177 if(len(extradata
)>0):
3178 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
3179 extrasizelen
= len(extrasizestr
);
3180 extrasizelenhex
= format(extrasizelen
, 'x').lower();
3181 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
[4]);
3182 if(len(extradata
)>0):
3183 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[4]);
3184 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[4]);
3185 catfnumfields
= 24 + catfextrafields
;
3186 if(checksumtype
=="none" or checksumtype
==""):
3187 catfileheadercshex
= format(0, 'x').lower();
3188 catfilecontentcshex
= format(0, 'x').lower();
3189 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3190 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3191 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
3192 elif(checksumtype
=="crc16_ccitt"):
3193 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3194 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
3195 elif(checksumtype
=="adler32"):
3196 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3197 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
3198 elif(checksumtype
=="crc32"):
3199 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3200 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
3201 elif(checksumtype
=="crc64_ecma"):
3202 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3203 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
3204 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3205 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3206 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
3207 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3208 checksumoutstr
= hashlib
.new(checksumtype
);
3209 checksumoutstr
.update("".encode('UTF-8'));
3210 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3211 checksumoutstr
= hashlib
.new(checksumtype
);
3212 checksumoutstr
.update(fcontents
);
3213 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
3215 catfileheadercshex
= format(0, 'x').lower();
3216 catfilecontentcshex
= format(0, 'x').lower();
3217 catfhstart
= fheadtell
;
3218 fheadtell
+= len(catfileoutstr
);
3219 catfhend
= fheadtell
- 1;
3220 catfcontentstart
= fheadtell
;
3221 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
3222 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
3223 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[4]) + catfileoutstr
;
3224 if(checksumtype
=="none" or checksumtype
==""):
3225 catfileheadercshex
= format(0, 'x').lower();
3226 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3227 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3228 elif(checksumtype
=="crc16_ccitt"):
3229 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3230 elif(checksumtype
=="adler32"):
3231 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3232 elif(checksumtype
=="crc32"):
3233 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3234 elif(checksumtype
=="crc64_ecma"):
3235 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3236 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3237 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3238 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3239 checksumoutstr
= hashlib
.new(checksumtype
);
3240 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
3241 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3243 catfileheadercshex
= format(0, 'x').lower();
3244 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
3245 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
3246 nullstrecd
= formatspecs
[4].encode('UTF-8');
3247 fheadtell
+= len(catfileoutstr
) + 1;
3248 catfcontentend
= fheadtell
- 1;
3249 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
3250 pyhascontents
= False;
3251 if(int(fsize
)>0 and not listonly
):
3252 pyhascontents
= True;
3253 if(int(fsize
)>0 and listonly
):
3255 pyhascontents
= False;
3256 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
} });
3257 fileidnum
= fileidnum
+ 1;
3260 def TarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
3266 inodetocatinode
= {};
3268 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
3271 if(not tarfile
.is_tarfile(infiles
)):
3273 except AttributeError:
3274 if(not is_tarfile(infiles
)):
3277 tarfp
= tarfile
.open(infiles
, "r");
3278 except FileNotFoundError
:
3280 fnumfiles
= int(len(tarfp
.getmembers()));
3281 catver
= formatspecs
[5];
3282 fileheaderver
= str(int(catver
.replace(".", "")));
3283 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
3284 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3285 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[4]);
3286 catversion
= fileheaderver
;
3287 if(checksumtype
=="none" or checksumtype
==""):
3288 catfileheadercshex
= format(0, 'x').lower();
3289 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3290 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3291 elif(checksumtype
=="crc16_ccitt"):
3292 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3293 elif(checksumtype
=="adler32"):
3294 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3295 elif(checksumtype
=="crc32"):
3296 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3297 elif(checksumtype
=="crc64_ecma"):
3298 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3299 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3300 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3301 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3302 checksumoutstr
= hashlib
.new(checksumtype
);
3303 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3304 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3306 catfileheadercshex
= format(0, 'x').lower();
3307 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
3308 fheadtell
= len(fileheader
);
3309 catlist
= {'fnumfiles': fnumfiles
, 'fformat': formatspecs
[0], 'fversion': catversion
, 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
3310 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
3311 if(re
.findall("^[.|/]", member
.name
)):
3312 fname
= member
.name
;
3314 fname
= "./"+member
.name
;
3316 VerbosePrintOut(fname
);
3317 fpremode
= member
.mode
;
3318 ffullmode
= member
.mode
;
3322 ffullmode
= member
.mode
+ stat
.S_IFREG
;
3324 elif(member
.isdev()):
3325 ffullmode
= member
.mode
;
3327 elif(member
.islnk()):
3328 ffullmode
= member
.mode
+ stat
.S_IFREG
;
3330 elif(member
.issym()):
3331 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
3333 elif(member
.ischr()):
3334 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
3336 elif(member
.isblk()):
3337 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
3339 elif(member
.isdir()):
3340 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
3342 elif(member
.isfifo()):
3343 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
3345 elif(member
.issparse()):
3346 ffullmode
= member
.mode
;
3349 ffullmode
= member
.mode
;
3352 fbasedir
= os
.path
.dirname(fname
);
3356 curfid
= curfid
+ 1;
3358 flinkname
= member
.linkname
;
3359 fdev_minor
= member
.devminor
;
3360 fdev_major
= member
.devmajor
;
3361 frdev_minor
= member
.devminor
;
3362 frdev_major
= member
.devmajor
;
3363 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
3365 elif(ftype
==0 or ftype
==7):
3366 fsize
= member
.size
;
3368 fsize
= member
.size
;
3369 fatime
= member
.mtime
;
3370 fmtime
= member
.mtime
;
3371 fctime
= member
.mtime
;
3372 fbtime
= member
.mtime
;
3374 fchmode
= stat
.S_IMODE(ffullmode
);
3375 ftypemod
= stat
.S_IFMT(ffullmode
);
3378 funame
= member
.uname
;
3379 fgname
= member
.gname
;
3380 flinkcount
= flinkcount
;
3381 fcontents
= "".encode('UTF-8');
3383 if(ftype
== 0 or ftype
== 7):
3384 with tarfp
.extractfile(member
) as fpc
:
3386 chunk
= fpc
.read(chunk_size
);
3390 ftypehex
= format(ftype
, 'x').lower();
3391 extrafields
= len(extradata
);
3392 extrafieldslist
= extradata
;
3393 catfextrafields
= extrafields
;
3394 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
3395 if(len(extradata
)>0):
3396 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
3397 extrasizelen
= len(extrasizestr
);
3398 extrasizelenhex
= format(extrasizelen
, 'x').lower();
3399 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
[4]);
3400 if(len(extradata
)>0):
3401 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[4]);
3402 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[4]);
3403 catfnumfields
= 24 + catfextrafields
;
3404 if(checksumtype
=="none" or checksumtype
==""):
3405 catfileheadercshex
= format(0, 'x').lower();
3406 catfilecontentcshex
= format(0, 'x').lower();
3407 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3408 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3409 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
3410 elif(checksumtype
=="crc16_ccitt"):
3411 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3412 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
3413 elif(checksumtype
=="adler32"):
3414 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3415 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
3416 elif(checksumtype
=="crc32"):
3417 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3418 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
3419 elif(checksumtype
=="crc64_ecma"):
3420 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3421 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
3422 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3423 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3424 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
3425 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3426 checksumoutstr
= hashlib
.new(checksumtype
);
3427 checksumoutstr
.update("".encode('UTF-8'));
3428 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3429 checksumoutstr
= hashlib
.new(checksumtype
);
3430 checksumoutstr
.update(fcontents
);
3431 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
3433 catfileheadercshex
= format(0, 'x').lower();
3434 catfilecontentcshex
= format(0, 'x').lower();
3435 catfhstart
= fheadtell
;
3436 fheadtell
+= len(catfileoutstr
);
3437 catfhend
= fheadtell
- 1;
3438 catfcontentstart
= fheadtell
;
3439 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
3440 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
3441 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[4]) + catfileoutstr
;
3442 if(checksumtype
=="none" or checksumtype
==""):
3443 catfileheadercshex
= format(0, 'x').lower();
3444 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3445 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3446 elif(checksumtype
=="crc16_ccitt"):
3447 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3448 elif(checksumtype
=="adler32"):
3449 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3450 elif(checksumtype
=="crc32"):
3451 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3452 elif(checksumtype
=="crc64_ecma"):
3453 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3454 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3455 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3456 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3457 checksumoutstr
= hashlib
.new(checksumtype
);
3458 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
3459 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3461 catfileheadercshex
= format(0, 'x').lower();
3462 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
3463 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
3464 nullstrecd
= formatspecs
[4].encode('UTF-8');
3465 fheadtell
+= len(catfileoutstr
) + 1;
3466 catfcontentend
= fheadtell
- 1;
3467 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
3468 pyhascontents
= False;
3469 if(int(fsize
)>0 and not listonly
):
3470 pyhascontents
= True;
3471 if(int(fsize
)>0 and listonly
):
3473 pyhascontents
= False;
3474 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
} });
3475 fileidnum
= fileidnum
+ 1;
3478 def ZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
3479 advancedlist
= True;
3485 inodetocatinode
= {};
3487 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
3489 if(not zipfile
.is_zipfile(infiles
)):
3491 zipfp
= zipfile
.ZipFile(infiles
, "r", allowZip64
=True);
3492 ziptest
= zipfp
.testzip();
3494 VerbosePrintOut("Bad file found!");
3495 fnumfiles
= int(len(zipfp
.infolist()));
3496 catver
= formatspecs
[5];
3497 fileheaderver
= str(int(catver
.replace(".", "")));
3498 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
3499 catversion
= fileheaderver
;
3500 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3501 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[4]);
3502 if(checksumtype
=="none" or checksumtype
==""):
3503 catfileheadercshex
= format(0, 'x').lower();
3504 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3505 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3506 elif(checksumtype
=="crc16_ccitt"):
3507 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3508 elif(checksumtype
=="adler32"):
3509 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3510 elif(checksumtype
=="crc32"):
3511 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3512 elif(checksumtype
=="crc64_ecma"):
3513 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3514 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3515 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3516 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3517 checksumoutstr
= hashlib
.new(checksumtype
);
3518 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3519 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3521 catfileheadercshex
= format(0, 'x').lower();
3522 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
3523 fheadtell
= len(fileheader
);
3524 catlist
= {'fnumfiles': fnumfiles
, 'fformat': formatspecs
[0], 'fversion': catversion
, 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
3525 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
3526 if(re
.findall("^[.|/]", member
.filename
)):
3527 fname
= member
.filename
;
3529 fname
= "./"+member
.filename
;
3530 zipinfo
= zipfp
.getinfo(member
.filename
);
3532 VerbosePrintOut(fname
);
3533 if(not member
.is_dir()):
3534 fpremode
= stat
.S_IFREG
+ 438;
3535 elif(member
.is_dir()):
3536 fpremode
= stat
.S_IFDIR
+ 511;
3539 if(not member
.is_dir()):
3541 elif(member
.is_dir()):
3544 fbasedir
= os
.path
.dirname(fname
);
3548 curfid
= curfid
+ 1;
3556 fsize
= member
.file_size
;
3558 fsize
= member
.file_size
;
3559 fatime
= time
.mktime(member
.date_time
+ (0, 0, -1));
3560 fmtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
3561 fctime
= time
.mktime(member
.date_time
+ (0, 0, -1));
3562 fbtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
3563 if(not member
.is_dir()):
3564 fmode
= stat
.S_IFREG
+ 438;
3565 fchmode
= stat
.S_IMODE(int(stat
.S_IFREG
+ 438));
3566 ftypemod
= stat
.S_IFMT(int(stat
.S_IFREG
+ 438));
3567 if(member
.is_dir()):
3568 fmode
= stat
.S_IFDIR
+ 511;
3569 fchmode
= stat
.S_IMODE(int(stat
.S_IFDIR
+ 511));
3570 ftypemod
= stat
.S_IFMT(int(stat
.S_IFDIR
+ 511));
3573 except AttributeError:
3579 except AttributeError:
3586 userinfo
= pwd
.getpwuid(os
.getuid());
3587 funame
= userinfo
.pw_name
;
3590 except AttributeError:
3598 groupinfo
= grp
.getgrgid(os
.getgid());
3599 fgname
= groupinfo
.gr_name
;
3602 except AttributeError:
3606 fcontents
= "".encode('UTF-8');
3608 fcontents
= zipfp
.read(member
.filename
);
3609 ftypehex
= format(ftype
, 'x').lower();
3610 extrafields
= len(extradata
);
3611 extrafieldslist
= extradata
;
3612 catfextrafields
= extrafields
;
3613 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
3614 if(len(extradata
)>0):
3615 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
3616 extrasizelen
= len(extrasizestr
);
3617 extrasizelenhex
= format(extrasizelen
, 'x').lower();
3618 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
[4]);
3619 if(len(extradata
)>0):
3620 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[4]);
3621 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[4]);
3622 catfnumfields
= 24 + catfextrafields
;
3623 if(checksumtype
=="none" or checksumtype
==""):
3624 catfileheadercshex
= format(0, 'x').lower();
3625 catfilecontentcshex
= format(0, 'x').lower();
3626 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3627 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3628 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
3629 elif(checksumtype
=="crc16_ccitt"):
3630 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3631 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
3632 elif(checksumtype
=="adler32"):
3633 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3634 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
3635 elif(checksumtype
=="crc32"):
3636 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3637 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
3638 elif(checksumtype
=="crc64_ecma"):
3639 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3640 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
3641 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3642 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3643 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
3644 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3645 checksumoutstr
= hashlib
.new(checksumtype
);
3646 checksumoutstr
.update("".encode('UTF-8'));
3647 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3648 checksumoutstr
= hashlib
.new(checksumtype
);
3649 checksumoutstr
.update(fcontents
);
3650 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
3652 catfileheadercshex
= format(0, 'x').lower();
3653 catfilecontentcshex
= format(0, 'x').lower();
3654 catfhstart
= fheadtell
;
3655 fheadtell
+= len(catfileoutstr
);
3656 catfhend
= fheadtell
- 1;
3657 catfcontentstart
= fheadtell
;
3658 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
3659 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
3660 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[4]) + catfileoutstr
;
3661 if(checksumtype
=="none" or checksumtype
==""):
3662 catfileheadercshex
= format(0, 'x').lower();
3663 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3664 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3665 elif(checksumtype
=="crc16_ccitt"):
3666 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3667 elif(checksumtype
=="adler32"):
3668 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3669 elif(checksumtype
=="crc32"):
3670 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3671 elif(checksumtype
=="crc64_ecma"):
3672 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3673 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3674 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3675 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3676 checksumoutstr
= hashlib
.new(checksumtype
);
3677 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
3678 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3680 catfileheadercshex
= format(0, 'x').lower();
3681 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
3682 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
3683 nullstrecd
= formatspecs
[4].encode('UTF-8');
3684 fheadtell
+= len(catfileoutstr
) + 1;
3685 catfcontentend
= fheadtell
- 1;
3686 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
3687 pyhascontents
= False;
3688 if(int(fsize
)>0 and not listonly
):
3689 pyhascontents
= True;
3690 if(int(fsize
)>0 and listonly
):
3692 pyhascontents
= False;
3693 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
} });
3694 fileidnum
= fileidnum
+ 1;
3697 if(not rarfile_support
):
3698 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
3701 if(rarfile_support
):
3702 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
3703 advancedlist
= True;
3709 inodetocatinode
= {};
3711 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
3713 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
3715 rarfp
= rarfile
.RarFile(infile
, "r");
3716 rartest
= rarfp
.testrar();
3718 VerbosePrintOut("Bad file found!");
3719 fnumfiles
= int(len(rarfp
.infolist()));
3720 catver
= formatspecs
[5];
3721 fileheaderver
= str(int(catver
.replace(".", "")));
3722 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
3723 catversion
= fileheaderver
;
3724 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3725 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[4]);
3726 if(checksumtype
=="none" or checksumtype
==""):
3727 catfileheadercshex
= format(0, 'x').lower();
3728 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3729 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3730 elif(checksumtype
=="crc16_ccitt"):
3731 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3732 elif(checksumtype
=="adler32"):
3733 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3734 elif(checksumtype
=="crc32"):
3735 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3736 elif(checksumtype
=="crc64_ecma"):
3737 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3738 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3739 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3740 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3741 checksumoutstr
= hashlib
.new(checksumtype
);
3742 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3743 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3745 catfileheadercshex
= format(0, 'x').lower();
3746 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
3747 fheadtell
= len(fileheader
);
3748 catlist
= {'fnumfiles': fnumfiles
, 'fformat': formatspecs
[0], 'fversion': catversion
, 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
3749 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
3752 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
3755 member
.external_attr
3757 except AttributeError:
3759 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
3762 member
.external_attr
3764 except AttributeError:
3769 if(re
.findall("^[.|/]", member
.filename
)):
3770 fname
= member
.filename
;
3772 fname
= "./"+member
.filename
;
3773 rarinfo
= rarfp
.getinfo(member
.filename
);
3775 VerbosePrintOut(fname
);
3776 if(is_unix
and member
.external_attr
!=0):
3777 fpremode
= int(member
.external_attr
);
3778 elif(member
.is_file()):
3779 fpremode
= stat
.S_IFREG
+ 438;
3780 elif(member
.is_symlink()):
3781 fpremode
= stat
.S_IFLNK
+ 438;
3782 elif(member
.is_dir()):
3783 fpremode
= stat
.S_IFDIR
+ 511;
3784 if(is_windows
and member
.external_attr
!=0):
3785 fwinattributes
= int(member
.external_attr
);
3787 fwinattributes
= int(0);
3790 if(member
.is_file()):
3792 elif(member
.is_symlink()):
3794 elif(member
.is_dir()):
3798 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
3799 fbasedir
= os
.path
.dirname(fname
);
3803 curfid
= curfid
+ 1;
3811 fsize
= member
.file_size
;
3814 fatime
= int(member
.atime
.timestamp());
3816 fatime
= int(member
.mtime
.timestamp());
3817 except AttributeError:
3818 fatime
= int(member
.mtime
.timestamp());
3819 fmtime
= int(member
.mtime
.timestamp());
3822 fctime
= int(member
.ctime
.timestamp());
3824 fctime
= int(member
.mtime
.timestamp());
3825 except AttributeError:
3826 fctime
= int(member
.mtime
.timestamp());
3827 fbtime
= int(member
.mtime
.timestamp());
3828 if(is_unix
and member
.external_attr
!=0):
3829 fmode
= format(int(member
.external_attr
), 'x').lower();
3830 fchmode
= format(int(stat
.S_IMODE(member
.external_attr
)), 'x').lower();
3831 ftypemod
= format(int(stat
.S_IFMT(member
.external_attr
)), 'x').lower();
3832 elif(member
.is_file()):
3833 fmode
= int(stat
.S_IFREG
+ 438)
3834 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
3835 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
3836 elif(member
.is_symlink()):
3837 fmode
= int(stat
.S_IFLNK
+ 438)
3838 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
3839 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
3840 elif(member
.is_dir()):
3841 fmode
= int(stat
.S_IFDIR
+ 511)
3842 fchmode
= int(stat
.S_IMODE(stat
.S_IFDIR
+ 511));
3843 ftypemod
= int(stat
.S_IFMT(stat
.S_IFDIR
+ 511));
3846 except AttributeError:
3852 except AttributeError:
3859 userinfo
= pwd
.getpwuid(os
.getuid());
3860 funame
= userinfo
.pw_name
;
3863 except AttributeError:
3871 groupinfo
= grp
.getgrgid(os
.getgid());
3872 fgname
= groupinfo
.gr_name
;
3875 except AttributeError:
3879 fcontents
= "".encode('UTF-8');
3881 fcontents
= rarfp
.read(member
.filename
);
3882 ftypehex
= format(ftype
, 'x').lower();
3883 extrafields
= len(extradata
);
3884 extrafieldslist
= extradata
;
3885 catfextrafields
= extrafields
;
3886 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
3887 if(len(extradata
)>0):
3888 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
3889 extrasizelen
= len(extrasizestr
);
3890 extrasizelenhex
= format(extrasizelen
, 'x').lower();
3891 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
[4]);
3892 if(len(extradata
)>0):
3893 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[4]);
3894 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[4]);
3895 catfnumfields
= 24 + catfextrafields
;
3896 if(checksumtype
=="none" or checksumtype
==""):
3897 catfileheadercshex
= format(0, 'x').lower();
3898 catfilecontentcshex
= format(0, 'x').lower();
3899 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3900 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3901 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
3902 elif(checksumtype
=="crc16_ccitt"):
3903 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3904 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
3905 elif(checksumtype
=="adler32"):
3906 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3907 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
3908 elif(checksumtype
=="crc32"):
3909 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3910 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
3911 elif(checksumtype
=="crc64_ecma"):
3912 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3913 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
3914 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3915 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3916 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
3917 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3918 checksumoutstr
= hashlib
.new(checksumtype
);
3919 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
3920 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3922 catfileheadercshex
= format(0, 'x').lower();
3923 catfilecontentcshex
= format(0, 'x').lower();
3924 catfhstart
= fheadtell
;
3925 fheadtell
+= len(catfileoutstr
);
3926 catfhend
= fheadtell
- 1;
3927 catfcontentstart
= fheadtell
;
3928 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
3929 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
3930 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[4]) + catfileoutstr
;
3931 if(checksumtype
=="none" or checksumtype
==""):
3932 catfileheadercshex
= format(0, 'x').lower();
3933 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3934 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3935 elif(checksumtype
=="crc16_ccitt"):
3936 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3937 elif(checksumtype
=="adler32"):
3938 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3939 elif(checksumtype
=="crc32"):
3940 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3941 elif(checksumtype
=="crc64_ecma"):
3942 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3943 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3944 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3945 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3946 checksumoutstr
= hashlib
.new(checksumtype
);
3947 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
3948 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3950 catfileheadercshex
= format(0, 'x').lower();
3951 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
3952 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
3953 nullstrecd
= formatspecs
[4].encode('UTF-8');
3954 fheadtell
+= len(catfileoutstr
) + 1;
3955 catfcontentend
= fheadtell
- 1;
3956 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
3957 pyhascontents
= False;
3958 if(int(fsize
)>0 and not listonly
):
3959 pyhascontents
= True;
3960 if(int(fsize
)>0 and listonly
):
3962 pyhascontents
= False;
3963 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
} });
3964 fileidnum
= fileidnum
+ 1;
3967 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, usenewstyle
=True, returnfp
=False):
3968 outarray
= BytesIO();
3969 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
3970 listcatfiles
= ArchiveFileToArray(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
3971 return listcatfiles
;
3973 def ArchiveFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, usenewstyle
=True, returnfp
=False):
3974 if(isinstance(infile
, dict)):
3975 listcatfiles
= infile
;
3977 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
3978 infile
= RemoveWindowsPath(infile
);
3979 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
3980 if(not listcatfiles
):
3982 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': {}}}};
3984 catarray
.update({'catfp': listcatfiles
['catfp']});
3985 lenlist
= len(listcatfiles
['ffilelist']);
3990 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
3993 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
3994 lcfx
= listcatfiles
['fnumfiles'];
3996 lcfx
= int(listcatfiles
['fnumfiles']);
3998 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
3999 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
4000 catarray
['filetoid'].update(filetoidarray
);
4001 catarray
['idtofile'].update(idtofilearray
);
4002 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4003 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
4004 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
4005 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4006 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
4007 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
4008 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4009 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4010 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4011 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4012 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4013 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4014 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4015 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
4016 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
4017 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
4018 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4019 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4020 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
4021 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
4022 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
4023 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4024 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4025 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4026 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
4027 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
4028 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
4029 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4030 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4031 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4032 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4036 create_alias_function("", __file_format_name__
, "ToArrayIndex", ArchiveFileToArrayIndex
);
4038 def ListDirToArrayIndexAlt(infiles
, dirlistfromtxt
=False, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4039 listcatfiles
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
4040 if(not listcatfiles
):
4042 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': {}}}};
4043 lenlist
= len(listcatfiles
['ffilelist']);
4048 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4051 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4052 lcfx
= listcatfiles
['fnumfiles'];
4054 lcfx
= int(listcatfiles
['fnumfiles']);
4056 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
4057 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
4058 catarray
['filetoid'].update(filetoidarray
);
4059 catarray
['idtofile'].update(idtofilearray
);
4060 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4061 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
4062 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
4063 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4064 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
4065 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
4066 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4067 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4068 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4069 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4070 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4071 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4072 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4073 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
4074 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
4075 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
4076 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4077 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4078 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
4079 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
4080 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
4081 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4082 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4083 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4084 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
4085 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
4086 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
4087 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4088 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4089 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4090 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4094 def TarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4095 listcatfiles
= TarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
4096 if(not listcatfiles
):
4098 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': {}}}};
4099 lenlist
= len(listcatfiles
['ffilelist']);
4104 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4107 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4108 lcfx
= listcatfiles
['fnumfiles'];
4110 lcfx
= int(listcatfiles
['fnumfiles']);
4112 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
4113 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
4114 catarray
['filetoid'].update(filetoidarray
);
4115 catarray
['idtofile'].update(idtofilearray
);
4116 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4117 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
4118 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
4119 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4120 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
4121 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
4122 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4123 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4124 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4125 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4126 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4127 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4128 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4129 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
4130 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
4131 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
4132 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4133 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4134 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
4135 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
4136 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
4137 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4138 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4139 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4140 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
4141 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
4142 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
4143 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4144 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4145 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4146 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4150 def ZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4151 listcatfiles
= ZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
4152 if(not listcatfiles
):
4154 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': {}}}};
4155 lenlist
= len(listcatfiles
['ffilelist']);
4160 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4163 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4164 lcfx
= listcatfiles
['fnumfiles'];
4166 lcfx
= int(listcatfiles
['fnumfiles']);
4168 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
4169 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
4170 catarray
['filetoid'].update(filetoidarray
);
4171 catarray
['idtofile'].update(idtofilearray
);
4172 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4173 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
4174 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
4175 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4176 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
4177 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
4178 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4179 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4180 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4181 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4182 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4183 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4184 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4185 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
4186 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
4187 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
4188 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4189 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4190 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
4191 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
4192 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
4193 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4194 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4195 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4196 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
4197 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
4198 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
4199 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4200 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4201 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4202 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4206 if(not rarfile_support
):
4207 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4210 if(rarfile_support
):
4211 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4212 listcatfiles
= RarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
4213 if(not listcatfiles
):
4215 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': {}}}};
4216 lenlist
= len(listcatfiles
['ffilelist']);
4221 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4224 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4225 lcfx
= listcatfiles
['fnumfiles'];
4227 lcfx
= int(listcatfiles
['fnumfiles']);
4229 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
4230 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
4231 catarray
['filetoid'].update(filetoidarray
);
4232 catarray
['idtofile'].update(idtofilearray
);
4233 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4234 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
4235 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
4236 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4237 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
4238 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
4239 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4240 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4241 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4242 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4243 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4244 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4245 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4246 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
4247 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
4248 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
4249 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4250 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4251 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
4252 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
4253 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
4254 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4255 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4256 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4257 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
4258 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
4259 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
4260 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4261 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4262 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4263 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4267 def ArchiveFileStringToArrayIndex(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, usenewstyle
=True, returnfp
=False):
4268 catfp
= BytesIO(catstr
);
4269 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
4270 return listcatfiles
;
4272 create_alias_function("", __file_format_name__
, "StringToArrayIndex", ArchiveFileStringToArrayIndex
);
4274 def TarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, usenewstyle
=True, returnfp
=False):
4276 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4277 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
4278 return listcatfiles
;
4280 def ZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, usenewstyle
=True, returnfp
=False):
4282 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4283 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
4284 return listcatfiles
;
4286 if(not rarfile_support
):
4287 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4290 if(rarfile_support
):
4291 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, usenewstyle
=True, returnfp
=False):
4293 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4294 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
4295 return listcatfiles
;
4297 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, usenewstyle
=True, returnfp
=False):
4298 outarray
= BytesIO();
4299 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, verbose
, True);
4300 listcatfiles
= ArchiveFileToArrayIndex(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, usenewstyle
, returnfp
)
4301 return listcatfiles
;
4303 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, usenewstyle
=True, returnfp
=False):
4304 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
4305 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzop', 'lzo', 'lzma', 'xz'];
4306 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', 'lzop', '.lzo', '.lzma', '.xz'];
4307 if(isinstance(infile
, dict)):
4308 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
4309 listcatfiles
= prelistcatfiles
['list'];
4311 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
4312 infile
= RemoveWindowsPath(infile
);
4314 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
4315 listcatfiles
= prelistcatfiles
['list'];
4317 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
4318 if(outfile
!="-" and not hasattr(infile
, "read") and not hasattr(outfile
, "write")):
4319 outfile
= RemoveWindowsPath(outfile
);
4320 checksumtype
= checksumtype
.lower();
4321 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
4322 checksumtype
="crc32";
4323 if(checksumtype
=="none"):
4325 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[1]):
4327 if(compression
not in compressionlist
and compression
is None):
4328 compression
= "auto";
4330 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
4331 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
4332 if(os
.path
.exists(outfile
)):
4334 if(not listcatfiles
):
4339 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
4342 fbasename
= os
.path
.splitext(outfile
)[0];
4343 fextname
= os
.path
.splitext(outfile
)[1];
4344 catfp
= CompressOpenFile(outfile
, compressionlevel
);
4345 catver
= formatspecs
[5];
4346 fileheaderver
= str(int(catver
.replace(".", "")));
4347 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
4348 catfp
.write(fileheader
.encode('UTF-8'));
4349 lenlist
= len(listcatfiles
['ffilelist']);
4350 fnumfiles
= int(listcatfiles
['fnumfiles']);
4351 if(lenlist
>fnumfiles
or lenlist
<fnumfiles
):
4352 fnumfiles
= lenlist
;
4353 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4354 fnumfilesa
= AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[4]);
4355 if(checksumtype
=="none" or checksumtype
==""):
4356 catfileheadercshex
= format(0, 'x').lower();
4357 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4358 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
4359 elif(checksumtype
=="crc16_ccitt"):
4360 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
4361 elif(checksumtype
=="adler32"):
4362 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
4363 elif(checksumtype
=="crc32"):
4364 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
4365 elif(checksumtype
=="crc64_ecma"):
4366 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4367 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4368 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4369 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4370 checksumoutstr
= hashlib
.new(checksumtype
);
4371 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
4372 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4374 catfileheadercshex
= format(0, 'x').lower();
4375 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
4376 catfp
.write(fnumfilesa
.encode('UTF-8'));
4379 os
.fsync(catfp
.fileno());
4380 except io
.UnsupportedOperation
:
4382 except AttributeError:
4388 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4391 lcfx
= int(listcatfiles
['fnumfiles']);
4399 catfhstart
= catfp
.tell();
4400 if(re
.findall("^[.|/]", listcatfiles
['ffilelist'][reallcfi
]['fname'])):
4401 fname
= listcatfiles
['ffilelist'][reallcfi
]['fname'];
4403 fname
= "./"+listcatfiles
['ffilelist'][reallcfi
]['fname'];
4405 VerbosePrintOut(fname
);
4406 fheadersize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fheadersize']), 'x').lower();
4407 fsize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fsize']), 'x').lower();
4408 flinkname
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
4409 fatime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fatime']), 'x').lower();
4410 fmtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmtime']), 'x').lower();
4411 fctime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fctime']), 'x').lower();
4412 fbtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fbtime']), 'x').lower();
4413 fmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmode']), 'x').lower();
4414 fchmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fchmode']), 'x').lower();
4415 fuid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fuid']), 'x').lower();
4416 funame
= listcatfiles
['ffilelist'][reallcfi
]['funame'];
4417 fgid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fgid']), 'x').lower();
4418 fgname
= listcatfiles
['ffilelist'][reallcfi
]['fgname'];
4419 finode
= listcatfiles
['ffilelist'][reallcfi
]['finode'];
4420 flinkcount
= listcatfiles
['ffilelist'][reallcfi
]['flinkcount'];
4421 fdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fminor']), 'x').lower();
4422 fdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmajor']), 'x').lower();
4423 frdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frminor']), 'x').lower();
4424 frdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frmajor']), 'x').lower();
4425 if(len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] and len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>0):
4426 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
4427 if(len(extradata
) > 0):
4428 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(extradata
);
4429 listcatfiles
['ffilelist'][reallcfi
]['fextralist'] = extradata
;
4430 extrafields
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']), 'x').lower();
4431 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
4432 if(len(extradata
)>0):
4433 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
4434 extrasizelen
= format(len(extrasizestr
), 'x').lower();
4435 fcontents
= listcatfiles
['ffilelist'][reallcfi
]['fcontents'];
4437 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']==1 or listcatfiles
['ffilelist'][reallcfi
]['ftype']==2):
4438 getflinkpath
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
4439 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
4440 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
4441 fheadersize
= format(int(flinkinfo
['fheadersize']), 'x').lower();
4442 fsize
= format(int(flinkinfo
['fsize']), 'x').lower();
4443 flinkname
= flinkinfo
['flinkname'];
4444 fatime
= format(int(flinkinfo
['fatime']), 'x').lower();
4445 fmtime
= format(int(flinkinfo
['fmtime']), 'x').lower();
4446 fctime
= format(int(flinkinfo
['fctime']), 'x').lower();
4447 fbtime
= format(int(flinkinfo
['fbtime']), 'x').lower();
4448 fmode
= format(int(flinkinfo
['fmode']), 'x').lower();
4449 fchmode
= format(int(flinkinfo
['fchmode']), 'x').lower();
4450 fuid
= format(int(flinkinfo
['fuid']), 'x').lower();
4451 funame
= flinkinfo
['funame'];
4452 fgid
= format(int(flinkinfo
['fgid']), 'x').lower();
4453 fgname
= flinkinfo
['fgname'];
4454 finode
= flinkinfo
['finode'];
4455 flinkcount
= flinkinfo
['flinkcount'];
4456 fdev_minor
= format(int(flinkinfo
['fminor']), 'x').lower();
4457 fdev_major
= format(int(flinkinfo
['fmajor']), 'x').lower();
4458 frdev_minor
= format(int(flinkinfo
['frminor']), 'x').lower();
4459 frdev_major
= format(int(flinkinfo
['frmajor']), 'x').lower();
4460 if(len(flinkinfo
['fextralist'])>flinkinfo
['fextrafields'] and len(flinkinfo
['fextralist'])>0):
4461 flinkinfo
['fextrafields'] = len(flinkinfo
['fextralist']);
4462 if(len(extradata
) > 0):
4463 flinkinfo
['fextrafields'] = len(extradata
);
4464 flinkinfo
['fextralist'] = extradata
;
4465 extrafields
= format(int(flinkinfo
['fextrafields']), 'x').lower();
4466 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
4467 if(len(extradata
)>0):
4468 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
4469 extrasizelen
= format(len(extrasizestr
), 'x').lower();
4470 fcontents
= flinkinfo
['fcontents'];
4471 if(flinkinfo
['ftype']!=0 and flinkinfo
['ftype']!=7):
4473 fcontents
= fcontents
.encode('UTF-8');
4474 except AttributeError:
4476 ftypehex
= format(flinkinfo
['ftype'], 'x').lower();
4478 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=0 and listcatfiles
['ffilelist'][reallcfi
]['ftype']!=7):
4480 fcontents
= fcontents
.encode('UTF-8');
4481 except AttributeError:
4483 ftypehex
= format(listcatfiles
['ffilelist'][reallcfi
]['ftype'], 'x').lower();
4484 fcurfid
= format(curfid
, 'x').lower();
4485 if(not followlink
and finode
!=0):
4486 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=1):
4487 fcurinode
= format(int(curinode
), 'x').lower();
4488 inodetofile
.update({curinode
: fname
});
4489 filetoinode
.update({fname
: curinode
});
4490 curinode
= curinode
+ 1;
4492 fcurinode
= format(int(filetoinode
[flinkname
]), 'x').lower();
4494 fcurinode
= format(int(curinode
), 'x').lower();
4495 curinode
= curinode
+ 1;
4496 curfid
= curfid
+ 1;
4497 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
[4]);
4498 if(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']>0):
4499 extrafieldslist
= [];
4501 exil
= listcatfiles
['ffilelist'][reallcfi
]['fextrafields'];
4503 extrafieldslist
.append(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
4505 catfileoutstr
+= AppendNullBytes([extrafieldslist
], formatspecs
[4]);
4506 catfileoutstr
+= AppendNullBytes([checksumtype
], formatspecs
[4]);
4507 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
4508 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
4509 if(checksumtype
=="none" or checksumtype
==""):
4510 catfileheadercshex
= format(0, 'x').lower();
4511 catfilecontentcshex
= format(0, 'x').lower();
4512 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4513 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
4514 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
4515 elif(checksumtype
=="crc16_ccitt"):
4516 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
4517 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
4518 elif(checksumtype
=="adler32"):
4519 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4520 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
4521 elif(checksumtype
=="crc32"):
4522 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4523 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
4524 elif(checksumtype
=="crc64_ecma"):
4525 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4526 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
4527 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4528 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4529 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
4530 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4531 checksumoutstr
= hashlib
.new(checksumtype
);
4532 checksumoutstr
.update("".encode('UTF-8'));
4533 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4534 checksumoutstr
= hashlib
.new(checksumtype
);
4535 checksumoutstr
.update(fcontents
);
4536 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
4538 catfileheadercshex
= format(0, 'x').lower();
4539 catfilecontentcshex
= format(0, 'x').lower();
4540 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
4541 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
4542 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[4]) + catfileoutstr
;
4543 if(checksumtype
=="none" or checksumtype
==""):
4544 catfileheadercshex
= format(0, 'x').lower();
4545 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4546 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4547 elif(checksumtype
=="crc16_ccitt"):
4548 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4549 elif(checksumtype
=="adler32"):
4550 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4551 elif(checksumtype
=="crc32"):
4552 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4553 elif(checksumtype
=="crc64_ecma"):
4554 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4555 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4556 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4557 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4558 checksumoutstr
= hashlib
.new(checksumtype
);
4559 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
4560 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4562 catfileheadercshex
= format(0, 'x').lower();
4563 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
4564 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
4565 nullstrecd
= formatspecs
[4].encode('UTF-8');
4566 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
4567 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
4568 catfp
.write(catfileout
);
4571 os
.fsync(catfp
.fileno());
4572 except io
.UnsupportedOperation
:
4574 except AttributeError:
4577 reallcfi
= reallcfi
+ 1;
4578 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
4579 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
4582 os
.fsync(catfp
.fileno());
4583 except io
.UnsupportedOperation
:
4585 except AttributeError:
4589 if(hasattr(sys
.stdout
, "buffer")):
4590 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
4592 shutil
.copyfileobj(catfp
, sys
.stdout
);
4600 create_alias_function("RePack", __file_format_name__
, "", RePackArchiveFile
);
4602 def RePackArchiveFileFromString(catstr
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
4603 catfp
= BytesIO(catstr
);
4604 listcatfiles
= RePackArchiveFile(catfp
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
4605 return listcatfiles
;
4607 create_alias_function("RePack", __file_format_name__
, "FromString", RePackArchiveFileFromString
);
4609 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):
4610 outarray
= BytesIO();
4611 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
4612 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
4613 return listcatfiles
;
4615 create_alias_function("Pack", __file_format_name__
, "FromListDir", PackArchiveFileFromListDir
);
4617 def ArchiveFileArrayBase64Encode(infile
, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, usenewstyle
=True, returnfp
=False):
4619 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
4620 if(isinstance(infile
, dict)):
4621 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
4622 listcatfiles
= prelistcatfiles
['list'];
4624 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
4625 infile
= RemoveWindowsPath(infile
);
4627 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
4628 listcatfiles
= prelistcatfiles
['list'];
4630 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
4631 if(not listcatfiles
):
4633 lenlist
= len(listcatfiles
['ffilelist']);
4638 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4641 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4642 lcfx
= listcatfiles
['fnumfiles'];
4644 lcfx
= int(listcatfiles
['fnumfiles']);
4645 if(lenlist
>lcfx
or lenlist
<lcfx
):
4648 if(listcatfiles
['ffilelist'][lcfi
]['fhascontents']):
4649 listcatfiles
['ffilelist'][lcfi
]['fcontents'] = base64
.b64encode(listcatfiles
['ffilelist'][lcfi
]['fcontents']).decode("UTF-8");
4651 return listcatfiles
;
4653 create_alias_function("", __file_format_name__
, "ArrayBase64Encode", ArchiveFileArrayBase64Encode
);
4655 def ArchiveFileArrayBase64Decode(infile
, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, usenewstyle
=True, returnfp
=False):
4657 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
4658 if(isinstance(infile
, dict)):
4659 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
4660 listcatfiles
= prelistcatfiles
['list'];
4662 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
4663 infile
= RemoveWindowsPath(infile
);
4665 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
4666 listcatfiles
= prelistcatfiles
['list'];
4668 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
4669 if(not listcatfiles
):
4671 lenlist
= len(listcatfiles
['ffilelist']);
4676 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4679 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4680 lcfx
= listcatfiles
['fnumfiles'];
4682 lcfx
= int(listcatfiles
['fnumfiles']);
4683 if(lenlist
>lcfx
or lenlist
<lcfx
):
4686 if(listcatfiles
['ffilelist'][lcfi
]['fhascontents']):
4687 listcatfiles
['ffilelist'][lcfi
]['fcontents'] = base64
.b64decode(listcatfiles
['ffilelist'][lcfi
]['fcontents'].encode("UTF-8"));
4689 return listcatfiles
;
4691 create_alias_function("", __file_format_name__
, "ArrayBase64Decode", ArchiveFileArrayBase64Decode
);
4693 def UnPackArchiveFile(infile
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, usenewstyle
=True, returnfp
=False):
4694 if(outdir
is not None):
4695 outdir
= RemoveWindowsPath(outdir
);
4697 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
4698 if(isinstance(infile
, dict)):
4699 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
4700 listcatfiles
= prelistcatfiles
['list'];
4702 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
4703 infile
= RemoveWindowsPath(infile
);
4705 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
4706 listcatfiles
= prelistcatfiles
['list'];
4708 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
4709 if(not listcatfiles
):
4711 lenlist
= len(listcatfiles
['ffilelist']);
4716 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4719 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4720 lcfx
= listcatfiles
['fnumfiles'];
4722 lcfx
= int(listcatfiles
['fnumfiles']);
4723 if(lenlist
>lcfx
or lenlist
<lcfx
):
4730 userinfo
= pwd
.getpwuid(listcatfiles
['ffilelist'][lcfi
]['fuid']);
4731 funame
= userinfo
.pw_name
;
4740 groupinfo
= grp
.getgrgid(listcatfiles
['ffilelist'][lcfi
]['fgid']);
4741 fgname
= groupinfo
.gr_name
;
4747 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
4748 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4749 with
open(listcatfiles
['ffilelist'][lcfi
]['fname'], "wb") as fpc
:
4750 fpc
.write(listcatfiles
['ffilelist'][lcfi
]['fcontents'])
4753 os
.fsync(fpc
.fileno())
4754 except io
.UnsupportedOperation
:
4756 except AttributeError:
4758 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname']):
4759 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
4760 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], int(listcatfiles
['ffilelist'][lcfi
]['fchmode'], 8));
4761 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
4762 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4764 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
4765 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
4766 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
4771 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
4772 funame
= userinfo
.pw_name
;
4781 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
4782 fgname
= groupinfo
.gr_name
;
4787 if(flinkinfo
['ftype'] == 0 or flinkinfo
['ftype'] == 7):
4788 with
open(listcatfiles
['ffilelist'][lcfi
]['fname'], "wb") as fpc
:
4789 fpc
.write(flinkinfo
['fcontents'])
4792 os
.fsync(fpc
.fileno())
4793 except io
.UnsupportedOperation
:
4795 except AttributeError:
4797 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname']):
4798 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
4799 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], int(flinkinfo
['fchmode'], 8));
4800 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
4801 if(flinkinfo
['ftype']==1):
4802 os
.link(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
4803 if(flinkinfo
['ftype']==2):
4804 os
.symlink(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
4805 if(flinkinfo
['ftype']==5):
4806 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname'], int(flinkinfo
['fchmode'], 8));
4807 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname']):
4808 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
4809 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], int(flinkinfo
['fchmode'], 8));
4810 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
4811 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
4812 os
.mkfifo(listcatfiles
['ffilelist'][lcfi
]['fname'], int(flinkinfo
['fchmode'], 8));
4814 os
.link(listcatfiles
['ffilelist'][lcfi
]['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
4815 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4817 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
4818 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
4819 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
4824 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
4825 funame
= userinfo
.pw_name
;
4834 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
4835 fgname
= groupinfo
.gr_name
;
4840 if(flinkinfo
['ftype']==0 or flinkinfo
['ftype']==7):
4841 with
open(listcatfiles
['ffilelist'][lcfi
]['fname'], "wb") as fpc
:
4842 fpc
.write(flinkinfo
['fcontents'])
4845 os
.fsync(fpc
.fileno())
4846 except io
.UnsupportedOperation
:
4848 except AttributeError:
4850 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname']):
4851 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
4852 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], int(flinkinfo
['fchmode'], 8));
4853 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
4854 if(flinkinfo
['ftype']==1):
4855 os
.link(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
4856 if(flinkinfo
['ftype']==2):
4857 os
.symlink(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
4858 if(flinkinfo
['ftype']==5):
4859 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname'], int(flinkinfo
['fchmode'], 8));
4860 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname']):
4861 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
4862 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], int(flinkinfo
['fchmode'], 8));
4863 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
4864 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
4865 os
.mkfifo(listcatfiles
['ffilelist'][lcfi
]['fname'], int(flinkinfo
['fchmode'], 8));
4867 os
.symlink(listcatfiles
['ffilelist'][lcfi
]['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
4868 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4869 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname'], int(listcatfiles
['ffilelist'][lcfi
]['fchmode'], 8));
4870 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname']):
4871 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
4872 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], int(listcatfiles
['ffilelist'][lcfi
]['fchmode'], 8));
4873 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
4874 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6 and hasattr(os
, "mkfifo")):
4875 os
.mkfifo(listcatfiles
['ffilelist'][lcfi
]['fname'], int(listcatfiles
['ffilelist'][lcfi
]['fchmode'], 8));
4878 return listcatfiles
['ffilelist']['catfp'];
4882 create_alias_function("UnPack", __file_format_name__
, "", UnPackArchiveFile
);
4884 if(hasattr(shutil
, "register_unpack_format")):
4885 def UnPackArchiveFileFunc(archive_name
, extract_dir
=None, **kwargs
):
4886 return UnPackArchiveFile(archive_name
, extract_dir
, False, 0, 0, False, __file_format_delimiter__
, False, False);
4887 create_alias_function("UnPack", __file_format_name__
, "Func", UnPackArchiveFileFunc
);
4889 def UnPackArchiveFileString(catstr
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
4890 catfp
= BytesIO(catstr
);
4891 listcatfiles
= UnPackArchiveFile(catfp
, outdir
, followlink
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
4892 return listcatfiles
;
4894 create_alias_function("UnPack", __file_format_name__
, "String", UnPackArchiveFileString
);
4896 def ArchiveFileListFiles(infile
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, usenewstyle
=True, returnfp
=False):
4897 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
4898 if(isinstance(infile
, dict)):
4899 listcatfiles
= infile
;
4901 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
4902 infile
= RemoveWindowsPath(infile
);
4903 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, True, skipchecksum
, formatspecs
, usenewstyle
, returnfp
);
4904 if(not listcatfiles
):
4906 lenlist
= len(listcatfiles
['ffilelist']);
4911 returnval
.update({lcfi
: listcatfiles
['ffilelist'][lcfi
]['fname']});
4913 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
4915 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' } };
4916 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'];
4917 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4918 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " link to " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
4919 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4920 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " -> " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
4921 fuprint
= listcatfiles
['ffilelist'][lcfi
]['funame'];
4922 if(len(fuprint
)<=0):
4923 fuprint
= listcatfiles
['ffilelist'][lcfi
]['fuid'];
4924 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgname'];
4925 if(len(fgprint
)<=0):
4926 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgid'];
4927 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
));
4930 return listcatfiles
['catfp'];
4934 create_alias_function("", __file_format_name__
, "ListFiles", ArchiveFileListFiles
);
4936 def ArchiveFileStringListFiles(catstr
, followlink
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
4937 catfp
= BytesIO(catstr
);
4938 listcatfiles
= UnPackArchiveFile(catfp
, None, followlink
, skipchecksum
, formatspecs
, verbose
, returnfp
);
4939 return listcatfiles
;
4941 create_alias_function("", __file_format_name__
, "StringListFiles", ArchiveFileListFiles
);
4943 def TarFileListFiles(infile
, verbose
=False, returnfp
=False):
4944 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
4945 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
4948 if(not tarfile
.is_tarfile(infile
)):
4950 except AttributeError:
4951 if(not is_tarfile(infile
)):
4956 tarfp
= tarfile
.open(infiles
, "r");
4957 except FileNotFoundError
:
4959 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
4960 returnval
.update({lcfi
: member
.name
});
4961 fpremode
= member
.mode
;
4962 ffullmode
= member
.mode
;
4966 ffullmode
= member
.mode
+ stat
.S_IFREG
;
4968 elif(member
.isdev()):
4969 ffullmode
= member
.mode
;
4971 elif(member
.islnk()):
4972 ffullmode
= member
.mode
+ stat
.S_IFREG
;
4974 elif(member
.issym()):
4975 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
4977 elif(member
.ischr()):
4978 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
4980 elif(member
.isblk()):
4981 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
4983 elif(member
.isdir()):
4984 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
4986 elif(member
.isfifo()):
4987 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
4989 elif(member
.issparse()):
4990 ffullmode
= member
.mode
;
4993 VerbosePrintOut(member
.name
);
4995 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' } };
4996 printfname
= member
.name
;
4998 printfname
= member
.name
+ " link to " + member
.linkname
;
4999 elif(member
.issym()):
5000 printfname
= member
.name
+ " -> " + member
.linkname
;
5001 fuprint
= member
.uname
;
5002 if(len(fuprint
)<=0):
5003 fuprint
= member
.uid
;
5004 fgprint
= member
.gname
;
5005 if(len(fgprint
)<=0):
5006 fgprint
= member
.gid
;
5007 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
));
5010 return listcatfiles
['catfp'];
5014 def ZipFileListFiles(infile
, verbose
=False, returnfp
=False):
5015 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5016 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5018 if(not zipfile
.is_zipfile(infile
)):
5022 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
5023 ziptest
= zipfp
.testzip();
5025 VerbosePrintOut("Bad file found!");
5026 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
5027 if(not member
.is_dir()):
5028 fpremode
= int(stat
.S_IFREG
+ 438);
5029 elif(member
.is_dir()):
5030 fpremode
= int(stat
.S_IFDIR
+ 511);
5031 if(not member
.is_dir()):
5032 fmode
= int(stat
.S_IFREG
+ 438);
5033 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
5034 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
5035 elif(member
.is_dir()):
5036 fmode
= int(stat
.S_IFDIR
+ 511);
5037 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
5038 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
5039 returnval
.update({lcfi
: member
.filename
});
5041 VerbosePrintOut(member
.filename
);
5043 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' } };
5045 for fmodval
in str(oct(fmode
))[-3:]:
5046 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
5047 if(not member
.is_dir()):
5049 permissionstr
= "-" + permissionstr
;
5050 elif(member
.is_dir()):
5052 permissionstr
= "d" + permissionstr
;
5053 printfname
= member
.filename
;
5055 fuid
= int(os
.getuid());
5056 except AttributeError:
5061 fgid
= int(os
.getgid());
5062 except AttributeError:
5069 userinfo
= pwd
.getpwuid(os
.getuid());
5070 funame
= userinfo
.pw_name
;
5073 except AttributeError:
5081 groupinfo
= grp
.getgrgid(os
.getgid());
5082 fgname
= groupinfo
.gr_name
;
5085 except AttributeError:
5090 if(len(fuprint
)<=0):
5091 fuprint
= str(fuid
);
5093 if(len(fgprint
)<=0):
5094 fgprint
= str(fgid
);
5095 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
));
5098 return listcatfiles
['catfp'];
5102 if(not rarfile_support
):
5103 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
5104 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5105 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5108 if(rarfile_support
):
5109 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
5110 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5111 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5113 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
5117 rarfp
= rarfile
.RarFile(infile
, "r");
5118 rartest
= rarfp
.testrar();
5120 VerbosePrintOut("Bad file found!");
5121 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
5124 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
5127 member
.external_attr
5129 except AttributeError:
5131 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
5134 member
.external_attr
5136 except AttributeError:
5141 if(is_unix
and member
.external_attr
!=0):
5142 fpremode
= int(member
.external_attr
);
5143 elif(member
.is_file()):
5144 fpremode
= int(stat
.S_IFREG
+ 438);
5145 elif(member
.is_symlink()):
5146 fpremode
= int(stat
.S_IFLNK
+ 438);
5147 elif(member
.is_dir()):
5148 fpremode
= int(stat
.S_IFDIR
+ 511);
5149 if(is_windows
and member
.external_attr
!=0):
5150 fwinattributes
= int(member
.external_attr
);
5152 fwinattributes
= int(0);
5153 if(is_unix
and member
.external_attr
!=0):
5154 fmode
= int(member
.external_attr
);
5155 fchmode
= int(stat
.S_IMODE(member
.external_attr
));
5156 ftypemod
= int(stat
.S_IFMT(member
.external_attr
));
5157 elif(member
.is_file()):
5158 fmode
= int(stat
.S_IFREG
+ 438);
5159 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
5160 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
5161 elif(member
.is_symlink()):
5162 fmode
= int(stat
.S_IFLNK
+ 438);
5163 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFLNK
+ 438)));
5164 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFLNK
+ 438)));
5165 elif(member
.is_dir()):
5166 fmode
= int(stat
.S_IFDIR
+ 511);
5167 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
5168 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
5169 returnval
.update({lcfi
: member
.filename
});
5171 VerbosePrintOut(member
.filename
);
5173 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' } };
5175 for fmodval
in str(oct(fmode
))[-3:]:
5176 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
5177 if(member
.is_file()):
5179 permissionstr
= "-" + permissionstr
;
5180 printfname
= member
.filename
;
5181 elif(member
.is_symlink()):
5183 permissionstr
= "l" + permissionstr
;
5184 printfname
= member
.name
+ " -> " + member
.read().decode("UTF-8");
5185 elif(member
.is_dir()):
5187 permissionstr
= "d" + permissionstr
;
5188 printfname
= member
.filename
;
5190 fuid
= int(os
.getuid());
5191 except AttributeError:
5196 fgid
= int(os
.getgid());
5197 except AttributeError:
5204 userinfo
= pwd
.getpwuid(os
.getuid());
5205 funame
= userinfo
.pw_name
;
5208 except AttributeError:
5216 groupinfo
= grp
.getgrgid(os
.getgid());
5217 fgname
= groupinfo
.gr_name
;
5220 except AttributeError:
5225 if(len(fuprint
)<=0):
5226 fuprint
= str(fuid
);
5228 if(len(fgprint
)<=0):
5229 fgprint
= str(fgid
);
5230 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(member
.file_size
).rjust(15) + " " + member
.mtime
.strftime('%Y-%m-%d %H:%M') + " " + printfname
));
5233 return listcatfiles
['catfp'];
5237 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):
5238 outarray
= BytesIO();
5239 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, False, True);
5240 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
5241 return listcatfiles
;
5243 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):
5244 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, formatspecs
, verbose
);
5245 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
5246 return listcatfiles
;
5248 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):
5249 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, False, checksumtype
, extradata
, formatspecs
, False);
5250 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, followlink
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
5251 return listcatfiles
;
5253 create_alias_function("Pack", __file_format_name__
, "FromListDirAlt", PackArchiveFileFromListDirAlt
);
5255 def PackArchiveFileFromTarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5256 outarray
= TarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
5257 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
5258 return listcatfiles
;
5260 create_alias_function("Pack", __file_format_name__
, "FromTarFileAlt", PackArchiveFileFromTarFileAlt
);
5262 def PackArchiveFileFromZipFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5263 outarray
= ZipFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
5264 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
5265 return listcatfiles
;
5267 create_alias_function("Pack", __file_format_name__
, "FromZipFileAlt", PackArchiveFileFromZipFileAlt
);
5269 if(not rarfile_support
):
5270 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5273 if(rarfile_support
):
5274 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5275 outarray
= RarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
5276 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
5277 return listcatfiles
;
5279 create_alias_function("Pack", __file_format_name__
, "FromRarFileAlt", PackArchiveFileFromRarFileAlt
);
5281 def download_file_from_ftp_file(url
):
5282 urlparts
= urlparse
.urlparse(url
);
5283 file_name
= os
.path
.basename(urlparts
.path
);
5284 file_dir
= os
.path
.dirname(urlparts
.path
);
5285 if(urlparts
.username
is not None):
5286 ftp_username
= urlparts
.username
;
5288 ftp_username
= "anonymous";
5289 if(urlparts
.password
is not None):
5290 ftp_password
= urlparts
.password
;
5291 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
5292 ftp_password
= "anonymous";
5295 if(urlparts
.scheme
=="ftp"):
5297 elif(urlparts
.scheme
=="ftps"):
5301 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
5303 ftp_port
= urlparts
.port
;
5304 if(urlparts
.port
is None):
5307 ftp
.connect(urlparts
.hostname
, ftp_port
);
5308 except socket
.gaierror
:
5309 log
.info("Error With URL "+httpurl
);
5311 except socket
.timeout
:
5312 log
.info("Error With URL "+httpurl
);
5314 ftp
.login(urlparts
.username
, urlparts
.password
);
5315 if(urlparts
.scheme
=="ftps"):
5317 ftpfile
= BytesIO();
5318 ftp
.retrbinary("RETR "+urlparts
.path
, ftpfile
.write
);
5319 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
5324 def download_file_from_ftp_string(url
):
5325 ftpfile
= download_file_from_ftp_file(url
);
5326 return ftpfile
.read();
5328 def upload_file_to_ftp_file(ftpfile
, url
):
5329 urlparts
= urlparse
.urlparse(url
);
5330 file_name
= os
.path
.basename(urlparts
.path
);
5331 file_dir
= os
.path
.dirname(urlparts
.path
);
5332 if(urlparts
.username
is not None):
5333 ftp_username
= urlparts
.username
;
5335 ftp_username
= "anonymous";
5336 if(urlparts
.password
is not None):
5337 ftp_password
= urlparts
.password
;
5338 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
5339 ftp_password
= "anonymous";
5342 if(urlparts
.scheme
=="ftp"):
5344 elif(urlparts
.scheme
=="ftps"):
5348 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
5350 ftp_port
= urlparts
.port
;
5351 if(urlparts
.port
is None):
5354 ftp
.connect(urlparts
.hostname
, ftp_port
);
5355 except socket
.gaierror
:
5356 log
.info("Error With URL "+httpurl
);
5358 except socket
.timeout
:
5359 log
.info("Error With URL "+httpurl
);
5361 ftp
.login(urlparts
.username
, urlparts
.password
);
5362 if(urlparts
.scheme
=="ftps"):
5364 ftp
.storbinary("STOR "+urlparts
.path
, ftpfile
);
5369 def upload_file_to_ftp_string(ftpstring
, url
):
5370 ftpfileo
= BytesIO(ftpstring
);
5371 ftpfile
= upload_file_to_ftp_file(ftpfileo
, url
);
5376 if(hasattr(shutil
, "register_archive_format")):
5377 # Register the packing format
5378 shutil
.register_archive_format(__file_format_name__
, PackArchiveFileFunc
, description
='Pack concatenated files');
5379 except shutil
.RegistryError
:
5383 if(hasattr(shutil
, "register_unpack_format")):
5384 # Register the unpacking format
5385 shutil
.register_unpack_format(__file_format_name__
, archivefile_extensions
, UnPackArchiveFileFunc
, description
='UnPack concatenated files');
5386 except shutil
.RegistryError
: