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/13/2024 Ver. 0.4.0 RC 1 - Author: cooldude2k $
20 from __future__
import absolute_import
, division
, print_function
, unicode_literals
;
21 import io
, os
, re
, sys
, time
, stat
, zlib
, base64
, shutil
, hashlib
, datetime
, logging
, binascii
, tempfile
, zipfile
, ftplib
;
22 from ftplib
import FTP
, FTP_TLS
;
23 if(sys
.version
[0]=="2"):
24 from urlparse
import urlparse
, urlunparse
, urlsplit
, urlunsplit
, urljoin
;
25 elif(sys
.version
[0]>="3"):
26 from urllib
.parse
import urlunparse
, urlsplit
, urlunsplit
, urljoin
, urlencode
;
27 import urllib
.parse
as urlparse
;
29 if os
.name
== 'nt': # Only modify if on Windows
30 if sys
.version
[0] == "2":
32 sys
.stdout
= codecs
.getwriter('utf-8')(sys
.stdout
);
33 sys
.stderr
= codecs
.getwriter('utf-8')(sys
.stderr
);
36 sys
.stdout
= io
.TextIOWrapper(sys
.stdout
.buffer, encoding
='utf-8', errors
='replace', line_buffering
=True);
37 sys
.stderr
= io
.TextIOWrapper(sys
.stderr
.buffer, encoding
='utf-8', errors
='replace', line_buffering
=True);
39 hashlib_guaranteed
= False;
40 os
.environ
["PYTHONIOENCODING"] = "UTF-8";
41 os
.environ
["LC_CTYPE"] = "UTF-8";
45 sys
.setdefaultencoding('UTF-8');
48 except AttributeError:
52 except AttributeError:
56 import simplejson
as json
;
61 from zlib
import crc32
;
63 from binascii
import crc32
;
65 rarfile_support
= False;
68 rarfile_support
= True;
70 rarfile_support
= False;
73 from safetar
import is_tarfile
;
76 from xtarfile
import is_tarfile
;
78 from tarfile
import is_tarfile
;
81 import safetar
as tarfile
;
84 import xtarfile
as tarfile
;
95 if(sys
.version
[0]=="2"):
97 from io
import StringIO
, BytesIO
;
100 from cStringIO
import StringIO
;
101 from cStringIO
import StringIO
as BytesIO
;
103 from StringIO
import StringIO
;
104 from StringIO
import StringIO
as BytesIO
;
105 elif(sys
.version
[0]>="3"):
106 from io
import StringIO
, BytesIO
;
111 from cStringIO
import StringIO
as BytesIO
;
117 from StringIO
import StringIO
as BytesIO
;
123 from io
import BytesIO
;
128 __file_format_name__
= "CatFile";
129 __program_name__
= "Py"+__file_format_name__
;
130 __file_format_lower__
= __file_format_name__
.lower();
131 __file_format_len__
= len(__file_format_name__
);
132 __file_format_hex__
= binascii
.hexlify(__file_format_name__
.encode("UTF-8")).decode("UTF-8");
133 __file_format_delimiter__
= "\x00";
134 __file_format_ver__
= "001";
135 __use_new_style__
= True;
136 __file_format_list__
= [__file_format_name__
, __file_format_lower__
, __file_format_len__
, __file_format_hex__
, __file_format_delimiter__
, __file_format_ver__
, __use_new_style__
];
137 __project__
= __program_name__
;
138 __project_url__
= "https://github.com/GameMaker2k/PyCatFile";
139 __version_info__
= (0, 4, 0, "RC 1", 1);
140 __version_date_info__
= (2024, 3, 13, "RC 1", 1);
141 __version_date__
= str(__version_date_info__
[0]) + "." + str(__version_date_info__
[1]).zfill(2) + "." + str(__version_date_info__
[2]).zfill(2);
142 __revision__
= __version_info__
[3];
143 __revision_id__
= "$Id$";
144 if(__version_info__
[4] is not None):
145 __version_date_plusrc__
= __version_date__
+ "-" + str(__version_date_info__
[4]);
146 if(__version_info__
[4] is None):
147 __version_date_plusrc__
= __version_date__
;
148 if(__version_info__
[3] is not None):
149 __version__
= str(__version_info__
[0]) + "." + str(__version_info__
[1]) + "." + str(__version_info__
[2]) + " " + str(__version_info__
[3]);
150 if(__version_info__
[3] is None):
151 __version__
= str(__version_info__
[0]) + "." + str(__version_info__
[1]) + "." + str(__version_info__
[2]);
153 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'xz', 'lz4', 'lzo', 'lzma'];
154 outextlist
= ['gz', 'bz2', 'zst', 'xz', 'lz4', 'lzo', 'lzma'];
155 outextlistwd
= ['.gz', '.bz2', '.zst', '.xz', '.lz4', '.lzo', '.lzma'];
157 tarfile_mimetype
= "application/tar";
158 tarfile_tar_mimetype
= tarfile_mimetype
;
159 zipfile_mimetype
= "application/zip";
160 zipfile_zip_mimetype
= zipfile_mimetype
;
161 rarfile_mimetype
= "application/rar";
162 rarfile_rar_mimetype
= rarfile_mimetype
;
163 archivefile_mimetype
= "application/x-"+__file_format_list__
[1]+"";
164 archivefile_cat_mimetype
= archivefile_mimetype
;
165 archivefile_gzip_mimetype
= "application/x-"+__file_format_list__
[1]+"+gzip";
166 archivefile_gz_mimetype
= archivefile_gzip_mimetype
;
167 archivefile_bzip2_mimetype
= "application/x-"+__file_format_list__
[1]+"+bzip2";
168 archivefile_bz2_mimetype
= archivefile_bzip2_mimetype
;
169 archivefile_lz4_mimetype
= "application/x-"+__file_format_list__
[1]+"+lz4";
170 archivefile_lzop_mimetype
= "application/x-"+__file_format_list__
[1]+"+lzop";
171 archivefile_lzo_mimetype
= archivefile_lzop_mimetype
;
172 archivefile_zstandard_mimetype
= "application/x-"+__file_format_list__
[1]+"+zstandard";
173 archivefile_zstd_mimetype
= archivefile_zstandard_mimetype
;
174 archivefile_lzma_mimetype
= "application/x-"+__file_format_list__
[1]+"+lzma";
175 archivefile_xz_mimetype
= "application/x-"+__file_format_list__
[1]+"+xz";
176 archivefile_extensions
= ['.cat', '.cat.gz', '.cat.bz2', '.cat.zst', '.cat.lz4', '.cat.lzo', '.cat.lzop', '.cat.lzma', '.cat.xz'];
178 if __name__
== "__main__":
180 curscrpath
= os
.path
.dirname(sys
.argv
[0]);
184 curscrpath
= curscrpath
.replace(os
.sep
, "/");
185 curscrpath
= curscrpath
+ "/";
186 scrfile
= curscrpath
+ "catfile.py";
187 if(os
.path
.exists(scrfile
) and os
.path
.isfile(scrfile
)):
188 scrcmd
= subprocess
.Popen([sys
.executable
, scrfile
] + sys
.argv
[1:]);
191 def VerbosePrintOut(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
197 "warning": logging
.warning
,
198 "error": logging
.error
,
199 "critical": logging
.critical
,
200 "exception": logging
.exception
,
201 "logalt": lambda x
: logging
.log(dgblevel
, x
),
202 "debug": logging
.debug
204 log_function
= log_functions
.get(outtype
);
206 log_function(dbgtxt
);
210 def VerbosePrintOutReturn(dbgtxt
, outtype
="log", dbgenable
=True, dgblevel
=20):
211 VerbosePrintOut(dbgtxt
, outtype
, dbgenable
, dgblevel
);
214 def RemoveWindowsPath(dpath
):
218 dpath
= dpath
.replace(os
.path
.sep
, "/");
219 dpath
= dpath
.rstrip("/");
220 if(dpath
=="." or dpath
==".."):
224 def NormalizeRelativePath(inpath
):
225 inpath
= RemoveWindowsPath(inpath
);
226 if(os
.path
.isabs(inpath
)):
229 if(inpath
.startswith("./") or inpath
.startswith("../")):
232 outpath
= "./" + inpath
;
235 def ListDir(dirpath
, followlink
=False, duplicates
=False):
236 if(isinstance(dirpath
, (list, tuple, ))):
237 dirpath
= list(filter(None, dirpath
));
238 elif(isinstance(dirpath
, (str, ))):
239 dirpath
= list(filter(None, [dirpath
]));
241 for mydirfile
in dirpath
:
242 if(not os
.path
.exists(mydirfile
)):
244 mydirfile
= NormalizeRelativePath(mydirfile
);
245 if(os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
)):
246 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
));
247 if(os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
)):
248 for root
, dirs
, filenames
in os
.walk(mydirfile
):
250 dpath
= RemoveWindowsPath(dpath
);
251 if(dpath
not in retlist
and not duplicates
):
252 retlist
.append(dpath
);
254 retlist
.append(dpath
);
255 for file in filenames
:
256 fpath
= os
.path
.join(root
, file);
257 fpath
= RemoveWindowsPath(fpath
);
258 if(fpath
not in retlist
and not duplicates
):
259 retlist
.append(fpath
);
261 retlist
.append(fpath
);
263 retlist
.append(RemoveWindowsPath(mydirfile
));
266 def ListDirAdvanced(dirpath
, followlink
=False, duplicates
=False):
267 if isinstance(dirpath
, (list, tuple)):
268 dirpath
= list(filter(None, dirpath
));
269 elif isinstance(dirpath
, str):
270 dirpath
= list(filter(None, [dirpath
]));
272 for mydirfile
in dirpath
:
273 if not os
.path
.exists(mydirfile
):
275 mydirfile
= NormalizeRelativePath(mydirfile
);
276 if os
.path
.exists(mydirfile
) and os
.path
.islink(mydirfile
) and followlink
:
277 mydirfile
= RemoveWindowsPath(os
.path
.realpath(mydirfile
))
278 if os
.path
.exists(mydirfile
) and os
.path
.isdir(mydirfile
):
279 for root
, dirs
, filenames
in os
.walk(mydirfile
):
280 # Sort dirs and filenames alphabetically in place
281 dirs
.sort(key
=lambda x
: x
.lower());
282 filenames
.sort(key
=lambda x
: x
.lower());
283 dpath
= RemoveWindowsPath(root
);
284 if not duplicates
and dpath
not in retlist
:
285 retlist
.append(dpath
);
287 retlist
.append(dpath
);
288 for file in filenames
:
289 fpath
= os
.path
.join(root
, file);
290 fpath
= RemoveWindowsPath(fpath
);
291 if not duplicates
and fpath
not in retlist
:
292 retlist
.append(fpath
);
294 retlist
.append(fpath
);
296 retlist
.append(RemoveWindowsPath(mydirfile
));
299 def create_alias_function(prefix
, base_name
, suffix
, target_function
):
300 # Define a new function that wraps the target function
301 def alias_function(*args
, **kwargs
):
302 return target_function(*args
, **kwargs
);
304 # Create the function name by combining the prefix, base name, and the suffix
305 function_name
= "{}{}{}".format(prefix
, base_name
, suffix
);
307 # Add the new function to the global namespace
308 globals()[function_name
] = alias_function
;
310 # initial_value can be 0xFFFF or 0x0000
311 def crc16_ansi(msg
, initial_value
=0xFFFF):
312 # CRC-16-IBM / CRC-16-ANSI polynomial and initial value
313 poly
= 0x8005; # Polynomial for CRC-16-IBM / CRC-16-ANSI
314 crc
= initial_value
; # Initial value
316 crc ^
= b
<< 8; # XOR byte into CRC top byte
317 for _
in range(8): # Process each bit
318 if crc
& 0x8000: # If the top bit is set
319 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
321 crc
= crc
<< 1; # Just shift left
322 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
325 # initial_value can be 0xFFFF or 0x0000
326 def crc16_ibm(msg
, initial_value
=0xFFFF):
327 return crc16_ansi(msg
, initial_value
);
329 # initial_value is 0xFFFF
331 return crc16_ansi(msg
, 0xFFFF);
333 # initial_value can be 0xFFFF, 0x1D0F or 0x0000
334 def crc16_ccitt(msg
, initial_value
=0xFFFF):
335 # CRC-16-CCITT polynomial
336 poly
= 0x1021; # Polynomial for CRC-16-CCITT
337 # Use the specified initial value
340 crc ^
= b
<< 8; # XOR byte into CRC top byte
341 for _
in range(8): # Process each bit
342 if crc
& 0x8000: # If the top bit is set
343 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with the polynomial
345 crc
= crc
<< 1; # Just shift left
346 crc
&= 0xFFFF; # Ensure CRC remains 16-bit
349 # initial_value can be 0x42F0E1EBA9EA3693 or 0x0000000000000000
350 def crc64_ecma(msg
, initial_value
=0x0000000000000000):
351 # CRC-64-ECMA polynomial and initial value
352 poly
= 0x42F0E1EBA9EA3693;
353 crc
= initial_value
; # Initial value for CRC-64-ECMA
355 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
356 for _
in range(8): # Process each bit
357 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
358 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
360 crc
<<= 1; # Just shift left if the MSB is 0
361 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
364 # initial_value can be 0x000000000000001B or 0xFFFFFFFFFFFFFFFF
365 def crc64_iso(msg
, initial_value
=0xFFFFFFFFFFFFFFFF):
366 # CRC-64-ISO polynomial and initial value
367 poly
= 0x000000000000001B;
368 crc
= initial_value
; # Common initial value for CRC-64-ISO
370 crc ^
= b
<< 56; # XOR byte into the most significant byte of the CRC
371 for _
in range(8): # Process each bit
372 if crc
& (1 << 63): # Check if the leftmost (most significant) bit is set
373 crc
= (crc
<< 1) ^ poly
; # Shift left and XOR with poly if the MSB is 1
375 crc
<<= 1; # Just shift left if the MSB is 0
376 crc
&= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
379 def ReadTillNullByte(fp
, delimiter
=__file_format_delimiter__
):
382 nullbyte
= delimiter
.encode("UTF-8");
384 curbyte
= fp
.read(1);
385 if(curbyte
==nullbyte
or not curbyte
):
387 curfullbyte
= curfullbyte
+ curbyte
;
388 return curfullbyte
.decode('UTF-8');
390 def ReadUntilNullByte(fp
, delimiter
=__file_format_delimiter__
):
391 return ReadTillNullByte(fp
, delimiter
);
393 def SeekToEndOfFile(fp
):
397 if(lasttell
==fp
.tell()):
399 lasttell
= fp
.tell();
402 def ReadFileHeaderData(fp
, rounds
=0, delimiter
=__file_format_delimiter__
):
406 while(rocount
<roend
):
407 HeaderOut
.append(ReadTillNullByte(fp
, delimiter
));
408 rocount
= rocount
+ 1;
411 def ReadFileHeaderDataBySize(fp
, delimiter
=__file_format_delimiter__
):
412 headerpresize
= ReadTillNullByte(fp
, delimiter
);
413 headersize
= int(headerpresize
, 16);
414 headercontent
= str(fp
.read(headersize
).decode('UTF-8')).split(delimiter
);
417 roend
= int(len(headercontent
));
418 HeaderOut
= [headerpresize
];
419 while(rocount
<roend
):
420 HeaderOut
.append(headercontent
[rocount
]);
421 rocount
= rocount
+ 1;
424 def ReadFileHeaderDataByList(fp
, listval
=[], delimiter
=__file_format_delimiter__
):
426 roend
= int(len(listval
));
428 while(rocount
<roend
):
429 RoundArray
= {listval
[rocount
]: ReadTillNullByte(fp
, delimiter
)};
430 HeaderOut
.update(RoundArray
);
431 rocount
= rocount
+ 1;
434 def ReadFileHeaderDataByListSize(fp
, listval
=[], delimiter
=__file_format_delimiter__
):
435 headerpresize
= ReadTillNullByte(fp
, delimiter
);
436 headersize
= int(headerpresize
, 16);
437 headercontent
= str(fp
.read(headersize
).decode('UTF-8')).split(delimiter
);
441 roend
= int(len(headercontent
));
442 HeaderOut
= {listval
[0]: headerpresize
};
443 while(rocount
<roend
):
444 RoundArray
= {listval
[rocount
]: headercontent
[rocount
]};
445 HeaderOut
.update(RoundArray
);
446 rocount
= rocount
+ 1;
447 listcount
= listcount
+ 1;
450 def AppendNullByte(indata
, delimiter
=__file_format_delimiter__
):
451 outdata
= str(indata
) + delimiter
;
454 def AppendNullBytes(indata
=[], delimiter
=__file_format_delimiter__
):
459 outdata
= outdata
+ AppendNullByte(indata
[inum
], delimiter
);
463 def ReadTillNullByteAlt(fp
, delimiter
=__file_format_delimiter__
):
464 """Read bytes from file pointer until a null byte is encountered."""
465 bytes_list
= [] # Use list for efficient append operation.
467 cur_byte
= fp
.read(1);
468 if cur_byte
== delimiter
.encode() or not cur_byte
:
470 bytes_list
.append(cur_byte
);
471 return b
''.join(bytes_list
).decode('UTF-8');
473 def ReadUntilNullByteAlt(fp
, delimiter
=__file_format_delimiter__
):
474 return ReadTillNullByteAlt(fp
, delimiter
);
476 def ReadFileHeaderDataAlt(fp
, rounds
=0, delimiter
=__file_format_delimiter__
):
477 """Read multiple null-byte terminated strings from a file."""
479 for round_count
in range(rounds
):
480 header_out
[round_count
] = ReadTillNullByteAlt(fp
, delimiter
);
483 def ReadFileHeaderDataBySizeAlt(fp
, delimiter
=__file_format_delimiter__
):
484 # Read and convert header size from hexadecimal to integer
485 header_pre_size
= ReadTillNullByte(fp
, delimiter
);
486 header_size
= int(header_pre_size
, 16);
487 # Read and split the header content
488 header_content
= str(fp
.read(header_size
).decode('UTF-8')).split(delimiter
);
490 # Prepend the pre-size and return the combined list
491 return [header_pre_size
] + header_content
;
493 def ReadFileHeaderDataByListSizeAlt(fp
, listval
=[], delimiter
=__file_format_delimiter__
):
494 # Read the size and content from the header
495 header_pre_size
= ReadTillNullByte(fp
, delimiter
);
496 header_size
= int(header_pre_size
, 16);
497 header_content
= str(fp
.read(header_size
).decode('UTF-8')).split(delimiter
);
499 # Initialize HeaderOut with the header pre-size if listval is not empty
500 HeaderOut
= {listval
[0]: header_pre_size
} if listval
else {};
501 # Map the remaining listval items to their corresponding header content, starting from the second item
502 for i
in range(1, min(len(header_content
) + 1, len(listval
))):
503 HeaderOut
[listval
[i
]] = header_content
[i
- 1]; # -1 because header_content is 0-indexed
506 def ReadFileHeaderDataByListAlt(fp
, listval
=[], delimiter
=__file_format_delimiter__
):
507 """Read multiple null-byte terminated strings from a file."""
509 for round_count
in listval
:
510 header_out
.append(ReadTillNullByteAlt(fp
, delimiter
));
513 def AppendNullByteAlt(indata
, delimiter
=__file_format_delimiter__
):
514 """Append a null byte to the given data."""
515 return str(indata
) + delimiter
;
517 def AppendNullBytesAlt(indata
=[], delimiter
=__file_format_delimiter__
):
518 """Append a null byte to each element in the list and concatenate."""
519 return delimiter
.join(map(str, indata
)) + delimiter
; # Efficient concatenation with null byte.
521 def PrintPermissionString(fchmode
, ftype
):
522 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' } };
524 for fmodval
in str(oct(fchmode
))[-3:]:
525 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
526 if(ftype
==0 or ftype
==7):
527 permissionstr
= "-" + permissionstr
;
529 permissionstr
= "h" + permissionstr
;
531 permissionstr
= "l" + permissionstr
;
533 permissionstr
= "c" + permissionstr
;
535 permissionstr
= "b" + permissionstr
;
537 permissionstr
= "d" + permissionstr
;
539 permissionstr
= "f" + permissionstr
;
541 permissionstr
= "D" + permissionstr
;
543 permissionstr
= "p" + permissionstr
;
545 permissionstr
= "w" + permissionstr
;
547 permissionoutstr
= stat
.filemode(fchmode
);
548 except AttributeError:
549 permissionoutstr
= permissionstr
;
551 permissionoutstr
= permissionstr
;
552 return permissionoutstr
;
554 def PrintPermissionStringAlt(fchmode
, ftype
):
556 '0': '---', '1': '--x', '2': '-w-', '3': '-wx',
557 '4': 'r--', '5': 'r-x', '6': 'rw-', '7': 'rwx'
559 # Translate file mode into permission string
560 permissionstr
= ''.join([permissions
[i
] for i
in str(oct(fchmode
))[-3:]]);
561 # Append file type indicator
563 0: '-', 1: 'h', 2: 'l', 3: 'c', 4: 'b',
564 5: 'd', 6: 'f', 8: 'D', 9: 'p', 10: 'w'
566 file_type
= type_indicators
.get(ftype
, '-');
567 permissionstr
= file_type
+ permissionstr
;
569 permissionoutstr
= stat
.filemode(fchmode
);
570 except AttributeError:
571 permissionoutstr
= permissionstr
;
572 return permissionoutstr
;
574 def CompressionSupport():
575 compression_list
= [];
578 compression_list
.append("gz");
579 compression_list
.append("gzip");
584 compression_list
.append("bz2");
585 compression_list
.append("bzip2");
590 compression_list
.append("lz4");
595 compression_list
.append("lzo");
596 compression_list
.append("lzop");
601 compression_list
.append("zstd");
602 compression_list
.append("zstandard");
607 compression_list
.append("lzma");
608 compression_list
.append("xz");
611 return compression_list
;
613 def CheckCompressionType(infile
, formatspecs
=__file_format_list__
, closefp
=True):
614 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
618 catfp
= open(infile
, "rb");
619 except FileNotFoundError
:
622 prefp
= catfp
.read(2);
624 if(prefp
==binascii
.unhexlify("1f8b")):
627 prefp
= catfp
.read(3);
628 if(prefp
==binascii
.unhexlify("425a68")):
630 if(prefp
==binascii
.unhexlify("5d0000")):
633 prefp
= catfp
.read(4);
634 if(prefp
==binascii
.unhexlify("28b52ffd")):
636 if(prefp
==binascii
.unhexlify("04224d18")):
638 if(prefp
==binascii
.unhexlify("504B0304")):
639 filetype
= "zipfile";
641 prefp
= catfp
.read(5);
642 if(prefp
==binascii
.unhexlify("7573746172")):
643 filetype
= "tarfile";
645 prefp
= catfp
.read(6);
646 if(prefp
==binascii
.unhexlify("fd377a585a00")):
649 prefp
= catfp
.read(7);
650 if(prefp
==binascii
.unhexlify("526172211a0700")):
651 filetype
= "rarfile";
652 if(prefp
==binascii
.unhexlify("43617446696c65")):
653 filetype
= "catfile";
655 prefp
= catfp
.read(8);
656 if(prefp
==binascii
.unhexlify("526172211a070100")):
657 filetype
= "rarfile";
659 prefp
= catfp
.read(formatspecs
[2]);
660 if(prefp
==binascii
.unhexlify(formatspecs
[3])):
661 filetype
= formatspecs
[1];
663 prefp
= catfp
.read(9);
664 if(prefp
==binascii
.unhexlify("894c5a4f000d0a1a0a")):
667 prefp
= catfp
.read(10);
668 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
669 filetype
= "tarfile";
675 def CheckCompressionTypeFromString(instring
, formatspecs
=__file_format_list__
, closefp
=True):
677 instringsfile
= BytesIO(instring
);
679 instringsfile
= BytesIO(instring
.encode("UTF-8"));
680 return CheckCompressionType(instringsfile
, formatspecs
, closefp
);
682 def GetCompressionMimeType(infile
, formatspecs
=__file_format_list__
):
683 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
684 if(compresscheck
=="gzip" or compresscheck
=="gz"):
685 return archivefile_gzip_mimetype
;
686 if(compresscheck
=="bzip2" or compresscheck
=="bz2"):
687 return archivefile_bzip2_mimetype
;
688 if(compresscheck
=="zstd" or compresscheck
=="zstandard"):
689 return archivefile_zstandard_mimetype
;
690 if(compresscheck
=="lz4"):
691 return archivefile_lz4_mimetype
;
692 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
693 return archivefile_lzop_mimetype
;
694 if(compresscheck
=="lzma"):
695 return archivefile_lzma_mimetype
;
696 if(compresscheck
=="xz"):
697 return archivefile_xz_mimetype
;
698 if(compresscheck
=="catfile" or compresscheck
=="cat" or compresscheck
==formatspecs
[1]):
699 return archivefile_cat_mimetype
;
700 if(not compresscheck
):
704 def UncompressArchiveFile(fp
, formatspecs
=__file_format_list__
):
705 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
707 compresscheck
= CheckCompressionType(fp
, formatspecs
, False);
708 if(compresscheck
=="gzip"):
713 catfp
= gzip
.GzipFile(fileobj
=fp
, mode
="rb");
714 if(compresscheck
=="bzip2"):
720 catfp
.write(bz2
.decompress(fp
.read()));
721 if(compresscheck
=="zstd"):
727 catfp
.write(zstandard
.decompress(fp
.read()));
728 if(compresscheck
=="lz4"):
734 catfp
.write(lz4
.frame
.decompress(fp
.read()));
735 if(compresscheck
=="lzo" or compresscheck
=="lzop"):
741 catfp
.write(lzo
.decompress(fp
.read()));
742 if(compresscheck
=="lzma" or compresscheck
=="xz"):
748 catfp
.write(lzma
.decompress(fp
.read()));
749 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[1]):
751 if(not compresscheck
):
757 with fp
as fpcontent
:
759 catfp
.write(lzma
.decompress(fp
.read()));
760 except lzma
.LZMAError
:
764 create_alias_function("Uncompress", __file_format_name__
, "", UncompressArchiveFile
);
766 def UncompressFile(infile
, formatspecs
=__file_format_list__
, mode
="rb"):
767 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
768 if(sys
.version_info
[0]==2 and compresscheck
):
774 if(compresscheck
=="gzip"):
780 filefp
= gzip
.open(infile
, mode
, encoding
="UTF-8");
781 except (ValueError, TypeError) as e
:
782 filefp
= gzip
.open(infile
, mode
);
783 if(compresscheck
=="bzip2"):
789 filefp
= bz2
.open(infile
, mode
, encoding
="UTF-8");
790 except (ValueError, TypeError) as e
:
791 filefp
= bz2
.open(infile
, mode
);
792 if(compresscheck
=="zstd"):
798 filefp
= zstandard
.open(infile
, mode
, encoding
="UTF-8");
799 except (ValueError, TypeError) as e
:
800 filefp
= zstandard
.open(infile
, mode
);
801 if(compresscheck
=="lz4"):
807 filefp
= lz4
.frame
.open(infile
, mode
, encoding
="UTF-8");
808 except (ValueError, TypeError) as e
:
809 filefp
= lz4
.frame
.open(infile
, mode
);
810 if(compresscheck
=="lzo"):
816 filefp
= lzo
.open(infile
, mode
, encoding
="UTF-8");
817 except (ValueError, TypeError) as e
:
818 filefp
= lzo
.open(infile
, mode
);
819 if(compresscheck
=="lzma"):
825 filefp
= lzma
.open(infile
, mode
, encoding
="UTF-8");
826 except (ValueError, TypeError) as e
:
827 filefp
= lzma
.open(infile
, mode
);
828 if(compresscheck
=="catfile" or compresscheck
==formatspecs
[1]):
830 filefp
= open(infile
, mode
, encoding
="UTF-8");
831 except (ValueError, TypeError) as e
:
832 filefp
= open(infile
, mode
);
833 if(not compresscheck
):
835 filefp
= open(infile
, mode
, encoding
="UTF-8");
836 except (ValueError, TypeError) as e
:
837 filefp
= open(infile
, mode
);
838 except FileNotFoundError
:
842 def UncompressString(infile
):
843 compresscheck
= CheckCompressionTypeFromString(infile
, formatspecs
, False);
844 if(compresscheck
=="gzip"):
849 fileuz
= gzip
.decompress(infile
);
850 if(compresscheck
=="bzip2"):
855 fileuz
= bz2
.decompress(infile
);
856 if(compresscheck
=="zstd"):
861 fileuz
= zstandard
.decompress(infile
);
862 if(compresscheck
=="lz4"):
867 fileuz
= lz4
.frame
.decompress(infile
);
868 if(compresscheck
=="lzo"):
873 fileuz
= lzo
.decompress(infile
);
874 if(compresscheck
=="lzma"):
879 fileuz
= lzma
.decompress(infile
);
880 if(not compresscheck
):
882 if(hasattr(fileuz
, 'decode')):
883 fileuz
= fileuz
.decode("UTF-8");
886 def UncompressStringAlt(infile
):
888 outstring
= UncompressString(infile
);
889 filefp
.write(outstring
);
893 def CheckCompressionSubType(infile
, formatspecs
=__file_format_list__
):
894 compresscheck
= CheckCompressionType(infile
, formatspecs
, False);
895 if(not compresscheck
):
896 fextname
= os
.path
.splitext(infile
)[1];
898 compresscheck
= "gzip";
899 if(fextname
==".bz2"):
900 compresscheck
= "bzip2";
901 if(fextname
==".zst"):
902 compresscheck
= "zstd";
903 if(fextname
==".lz4"):
904 compresscheck
= "lz4";
905 if(fextname
==".lzo" or fextname
==".lzop"):
906 compresscheck
= "lzo";
907 if(fextname
==".lzma" or fextname
==".xz"):
908 compresscheck
= "lzma";
909 if(not compresscheck
):
911 if(compresscheck
=="catfile"):
913 if(compresscheck
==formatspecs
[1]):
914 return formatspecs
[1];
915 if(compresscheck
=="tarfile"):
917 if(compresscheck
=="zipfile"):
919 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
920 catfp
= UncompressArchiveFile(infile
, formatspecs
[1]);
923 if(compresscheck
=="gzip"):
928 catfp
= gzip
.GzipFile(infile
, "rb");
929 if(compresscheck
=="bzip2"):
934 catfp
= bz2
.BZ2File(infile
, "rb");
935 if(compresscheck
=="lz4"):
940 catfp
= lz4
.frame
.open(infile
, "rb");
941 if(compresscheck
=="zstd"):
946 catfp
= zstandard
.open(infile
, "rb");
947 if(compresscheck
=="lzma" or compresscheck
=="xz"):
952 catfp
= lzma
.open(infile
, "rb");
953 except FileNotFoundError
:
956 prefp
= catfp
.read(5);
957 if(prefp
==binascii
.unhexlify("7573746172")):
958 filetype
= "tarfile";
960 prefp
= catfp
.read(7);
961 if(prefp
==binascii
.unhexlify("43617446696c65")):
962 filetype
= "catfile";
964 prefp
= catfp
.read(formatspecs
[2]);
965 if(prefp
==binascii
.unhexlify(formatspecs
[3])):
966 filetype
= formatspecs
[1];
968 prefp
= catfp
.read(10);
969 if(prefp
==binascii
.unhexlify("7061785f676c6f62616c")):
970 filetype
= "tarfile";
975 def GZipCompress(data
, compresslevel
=9):
980 tmpfp
= tempfile
.NamedTemporaryFile("wb", delete
=False);
982 tmpfp
= gzip
.GzipFile(tmpfp
.name
, mode
="wb", compresslevel
=compresslevel
);
986 catfp
= open(tmpfp
.name
, "rb");
987 except FileNotFoundError
:
989 catdata
= catfp
.read();
993 def CompressArchiveFile(fp
, compression
="auto", compressionlevel
=None, formatspecs
=__file_format_list__
):
994 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
995 if(not hasattr(fp
, "read") and not hasattr(fp
, "write")):
998 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[1]):
1000 if(compression
not in compressionlist
and compression
is None):
1001 compression
= "auto";
1002 if(compression
=="gzip"):
1008 if(compressionlevel
is None):
1009 compressionlevel
= 9;
1011 compressionlevel
= int(compressionlevel
);
1012 catfp
.write(gzip
.compress(fp
.read(), compresslevel
=compressionlevel
));
1013 if(compression
=="bzip2"):
1019 if(compressionlevel
is None):
1020 compressionlevel
= 9;
1022 compressionlevel
= int(compressionlevel
);
1023 catfp
.write(bz2
.compress(fp
.read(), compresslevel
=compressionlevel
));
1024 if(compression
=="lz4"):
1030 if(compressionlevel
is None):
1031 compressionlevel
= 9;
1033 compressionlevel
= int(compressionlevel
);
1034 catfp
.write(lz4
.frame
.compress(fp
.read(), compression_level
=compressionlevel
));
1035 if(compression
=="lzo" or compression
=="lzop"):
1041 if(compressionlevel
is None):
1042 compressionlevel
= 9;
1044 compressionlevel
= int(compressionlevel
);
1045 catfp
.write(lzo
.compress(fp
.read(), compresslevel
=compressionlevel
));
1046 if(compression
=="zstd"):
1052 if(compressionlevel
is None):
1053 compressionlevel
= 10;
1055 compressionlevel
= int(compressionlevel
);
1056 catfp
.write(zstandard
.compress(fp
.read(), level
=compressionlevel
));
1057 if(compression
=="lzma"):
1063 if(compressionlevel
is None):
1064 compressionlevel
= 9;
1066 compressionlevel
= int(compressionlevel
);
1067 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]));
1068 if(compression
=="xz"):
1074 if(compressionlevel
is None):
1075 compressionlevel
= 9;
1077 compressionlevel
= int(compressionlevel
);
1078 catfp
.write(lzma
.compress(fp
.read(), format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]));
1079 if(compression
=="auto" or compression
is None):
1084 create_alias_function("Compress", __file_format_name__
, "", CompressArchiveFile
);
1086 def CompressOpenFile(outfile
, compressionlevel
=None):
1087 if(outfile
is None):
1089 fbasename
= os
.path
.splitext(outfile
)[0];
1090 fextname
= os
.path
.splitext(outfile
)[1];
1091 if(compressionlevel
is None and fextname
!=".zst"):
1092 compressionlevel
= 9;
1093 elif(compressionlevel
is None and fextname
==".zst"):
1094 compressionlevel
= 10;
1096 compressionlevel
= int(compressionlevel
);
1097 if(sys
.version_info
[0]==2):
1102 if(fextname
not in outextlistwd
):
1104 outfp
= open(outfile
, "wb", encoding
="UTF-8");
1105 except (ValueError, TypeError) as e
:
1106 outfp
= open(outfile
, "wb");
1107 elif(fextname
==".gz"):
1113 outfp
= gzip
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
1114 except (ValueError, TypeError) as e
:
1115 outfp
= gzip
.open(outfile
, mode
, compressionlevel
);
1116 elif(fextname
==".bz2"):
1122 outfp
= bz2
.open(outfile
, mode
, compressionlevel
, encoding
="UTF-8");
1123 except (ValueError, TypeError) as e
:
1124 outfp
= bz2
.open(outfile
, mode
, compressionlevel
);
1125 elif(fextname
==".zst"):
1131 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
), encoding
="UTF-8");
1132 except (ValueError, TypeError) as e
:
1133 outfp
= zstandard
.open(outfile
, mode
, zstandard
.ZstdCompressor(level
=compressionlevel
));
1134 elif(fextname
==".xz"):
1140 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}], encoding
="UTF-8");
1141 except (ValueError, TypeError) as e
:
1142 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_XZ
, filters
=[{"id": lzma
.FILTER_LZMA2
, "preset": compressionlevel
}]);
1143 elif(fextname
==".lz4"):
1149 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
, encoding
="UTF-8");
1150 except (ValueError, TypeError) as e
:
1151 outfp
= lz4
.frame
.open(outfile
, mode
, compression_level
=compressionlevel
);
1152 elif(fextname
==".lzo"):
1158 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
, encoding
="UTF-8");
1159 except (ValueError, TypeError) as e
:
1160 outfp
= lzo
.open(outfile
, mode
, compresslevel
=compressionlevel
);
1161 elif(fextname
==".lzma"):
1167 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}], encoding
="UTF-8");
1168 except (ValueError, TypeError) as e
:
1169 outfp
= lzma
.open(outfile
, mode
, format
=lzma
.FORMAT_ALONE
, filters
=[{"id": lzma
.FILTER_LZMA1
, "preset": compressionlevel
}]);
1170 except FileNotFoundError
:
1174 def GetDevMajorMinor(fdev
):
1176 if(hasattr(os
, "minor")):
1177 retdev
.append(os
.minor(fdev
));
1180 if(hasattr(os
, "major")):
1181 retdev
.append(os
.major(fdev
));
1186 def CheckSumSupport(checkfor
, guaranteed
=True):
1188 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
1190 hash_list
= sorted(list(hashlib
.algorithms_available
));
1191 checklistout
= sorted(hash_list
+ ['adler32', 'crc16', 'crc16_ansi', 'crc16_ibm', 'crc16_ccitt', 'crc32', 'crc64', 'crc64_ecma', 'crc64_iso', 'none']);
1192 if(checkfor
in checklistout
):
1197 def CheckSumSupportAlt(checkfor
, guaranteed
=True):
1199 hash_list
= sorted(list(hashlib
.algorithms_guaranteed
));
1201 hash_list
= sorted(list(hashlib
.algorithms_available
));
1202 checklistout
= hash_list
;
1203 if(checkfor
in checklistout
):
1208 def PackArchiveFile(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1209 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1210 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1211 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1212 advancedlist
= True;
1213 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1214 outfile
= RemoveWindowsPath(outfile
);
1215 checksumtype
= checksumtype
.lower();
1216 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
1217 checksumtype
="crc32";
1218 if(checksumtype
=="none"):
1220 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[1]):
1222 if(compression
not in compressionlist
and compression
is None):
1223 compression
= "auto";
1225 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1226 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1227 if(os
.path
.exists(outfile
)):
1232 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1234 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
1236 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
1239 fbasename
= os
.path
.splitext(outfile
)[0];
1240 fextname
= os
.path
.splitext(outfile
)[1];
1241 catfp
= CompressOpenFile(outfile
, compressionlevel
);
1242 catver
= formatspecs
[5];
1243 fileheaderver
= str(int(catver
.replace(".", "")));
1244 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
1245 catfp
.write(fileheader
.encode('UTF-8'));
1248 for line
in sys
.stdin
:
1249 infilelist
.append(line
.strip());
1250 infilelist
= list(filter(None, infilelist
));
1251 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
1252 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
1254 with
open(infiles
, "r") as finfile
:
1255 for line
in finfile
:
1256 infilelist
.append(line
.strip());
1257 infilelist
= list(filter(None, infilelist
));
1259 if(isinstance(infiles
, (list, tuple, ))):
1260 infilelist
= list(filter(None, infiles
));
1261 elif(isinstance(infiles
, (str, ))):
1262 infilelist
= list(filter(None, [infiles
]));
1264 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
1266 GetDirList
= ListDir(infilelist
, followlink
, False);
1274 inodetocatinode
= {};
1275 fnumfiles
= format(int(len(GetDirList
)), 'x').lower();
1276 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[4]);
1277 if(checksumtype
=="none" or checksumtype
==""):
1278 catfileheadercshex
= format(0, 'x').lower();
1279 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1280 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1281 elif(checksumtype
=="crc16_ccitt"):
1282 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1283 elif(checksumtype
=="adler32"):
1284 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1285 elif(checksumtype
=="crc32"):
1286 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1287 elif(checksumtype
=="crc64_ecma"):
1288 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1289 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1290 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1291 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1292 checksumoutstr
= hashlib
.new(checksumtype
);
1293 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
1294 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1296 catfileheadercshex
= format(0, 'x').lower
1297 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
1298 catfp
.write(fnumfilesa
.encode('UTF-8'));
1301 os
.fsync(catfp
.fileno());
1302 except io
.UnsupportedOperation
:
1304 except AttributeError:
1306 for curfname
in GetDirList
:
1307 catfhstart
= catfp
.tell();
1308 if(re
.findall("^[.|/]", curfname
)):
1311 fname
= "./"+curfname
;
1313 VerbosePrintOut(fname
);
1314 if(not followlink
or followlink
is None):
1315 fstatinfo
= os
.lstat(fname
);
1317 fstatinfo
= os
.stat(fname
);
1318 fpremode
= fstatinfo
.st_mode
;
1319 finode
= fstatinfo
.st_ino
;
1320 flinkcount
= fstatinfo
.st_nlink
;
1322 if(stat
.S_ISREG(fpremode
)):
1324 elif(stat
.S_ISLNK(fpremode
)):
1326 elif(stat
.S_ISCHR(fpremode
)):
1328 elif(stat
.S_ISBLK(fpremode
)):
1330 elif(stat
.S_ISDIR(fpremode
)):
1332 elif(stat
.S_ISFIFO(fpremode
)):
1334 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
1336 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
1338 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
1343 fcurfid
= format(int(curfid
), 'x').lower();
1344 if(not followlink
and finode
!=0):
1346 if(finode
in inodelist
):
1348 flinkname
= inodetofile
[finode
];
1349 fcurinode
= format(int(inodetocatinode
[finode
]), 'x').lower();
1350 if(finode
not in inodelist
):
1351 inodelist
.append(finode
);
1352 inodetofile
.update({finode
: fname
});
1353 inodetocatinode
.update({finode
: curinode
});
1354 fcurinode
= format(int(curinode
), 'x').lower();
1355 curinode
= curinode
+ 1;
1357 fcurinode
= format(int(curinode
), 'x').lower();
1358 curinode
= curinode
+ 1;
1359 curfid
= curfid
+ 1;
1361 flinkname
= os
.readlink(fname
);
1362 fdev
= fstatinfo
.st_dev
;
1363 getfdev
= GetDevMajorMinor(fdev
);
1364 fdev_minor
= getfdev
[0];
1365 fdev_major
= getfdev
[1];
1366 frdev
= fstatinfo
.st_dev
;
1367 if(hasattr(fstatinfo
, "st_rdev")):
1368 frdev
= fstatinfo
.st_rdev
;
1370 frdev
= fstatinfo
.st_dev
;
1371 getfrdev
= GetDevMajorMinor(frdev
);
1372 frdev_minor
= getfrdev
[0];
1373 frdev_major
= getfrdev
[1];
1374 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
1375 fsize
= format(int("0"), 'x').lower();
1376 elif(ftype
==0 or ftype
==7):
1377 fsize
= format(int(fstatinfo
.st_size
), 'x').lower();
1379 fsize
= format(int(fstatinfo
.st_size
)).lower();
1380 fatime
= format(int(fstatinfo
.st_atime
), 'x').lower();
1381 fmtime
= format(int(fstatinfo
.st_mtime
), 'x').lower();
1382 fctime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1383 if(hasattr(fstatinfo
, "st_birthtime")):
1384 fbtime
= format(int(fstatinfo
.st_birthtime
), 'x').lower();
1386 fbtime
= format(int(fstatinfo
.st_ctime
), 'x').lower();
1387 fmode
= format(int(fstatinfo
.st_mode
), 'x').lower();
1388 fchmode
= format(int(stat
.S_IMODE(fstatinfo
.st_mode
)), 'x').lower();
1389 ftypemod
= format(int(stat
.S_IFMT(fstatinfo
.st_mode
)), 'x').lower();
1390 fuid
= format(int(fstatinfo
.st_uid
), 'x').lower();
1391 fgid
= format(int(fstatinfo
.st_gid
), 'x').lower();
1396 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
1397 funame
= userinfo
.pw_name
;
1406 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
1407 fgname
= groupinfo
.gr_name
;
1412 fdev_minor
= format(int(fdev_minor
), 'x').lower();
1413 fdev_major
= format(int(fdev_major
), 'x').lower();
1414 frdev_minor
= format(int(frdev_minor
), 'x').lower();
1415 frdev_major
= format(int(frdev_major
), 'x').lower();
1416 finode
= format(int(finode
), 'x').lower();
1417 flinkcount
= format(int(flinkcount
), 'x').lower();
1418 if(hasattr(fstatinfo
, "st_file_attributes")):
1419 fwinattributes
= format(int(fstatinfo
.st_file_attributes
), 'x').lower();
1421 fwinattributes
= format(int(0), 'x').lower();
1422 fcontents
= "".encode('UTF-8');
1424 if(ftype
== 0 or ftype
== 7):
1425 with
open(fname
, "rb") as fpc
:
1427 chunk
= fpc
.read(chunk_size
);
1431 if(followlink
and (ftype
== 1 or ftype
== 2)):
1432 flstatinfo
= os
.stat(flinkname
);
1433 with
open(flinkname
, "rb") as fpc
:
1435 chunk
= fpc
.read(chunk_size
);
1439 ftypehex
= format(ftype
, 'x').lower();
1440 extrafields
= format(len(extradata
), 'x').lower();
1441 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
1442 if(len(extradata
)>0):
1443 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
1444 extrasizelen
= format(len(extrasizestr
), 'x').lower();
1445 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]);
1446 if(len(extradata
)>0):
1447 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[4]);
1448 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[4]);
1449 if(checksumtype
=="none" or checksumtype
==""):
1450 catfileheadercshex
= format(0, 'x').lower();
1451 catfilecontentcshex
= format(0, 'x').lower();
1452 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1453 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
1454 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
1455 elif(checksumtype
=="crc16_ccitt"):
1456 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
1457 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
1458 elif(checksumtype
=="adler32"):
1459 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1460 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
1461 elif(checksumtype
=="crc32"):
1462 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1463 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
1464 elif(checksumtype
=="crc64_ecma"):
1465 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1466 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
1467 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1468 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1469 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
1470 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1471 checksumoutstr
= hashlib
.new(checksumtype
);
1472 checksumoutstr
.update("".encode('UTF-8'));
1473 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1474 checksumoutstr
= hashlib
.new(checksumtype
);
1475 checksumoutstr
.update(fcontents
);
1476 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
1478 catfileheadercshex
= format(0, 'x').lower();
1479 catfilecontentcshex
= format(0, 'x').lower();
1480 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
1481 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
1482 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[4]) + catfileoutstr
;
1483 if(checksumtype
=="none" or checksumtype
==""):
1484 catfileheadercshex
= format(0, 'x').lower();
1485 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1486 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
1487 elif(checksumtype
=="crc16_ccitt"):
1488 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
1489 elif(checksumtype
=="adler32"):
1490 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
1491 elif(checksumtype
=="crc32"):
1492 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
1493 elif(checksumtype
=="crc64_ecma"):
1494 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1495 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1496 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1497 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1498 checksumoutstr
= hashlib
.new(checksumtype
);
1499 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
1500 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1502 catfileheadercshex
= format(0, 'x').lower();
1503 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
1504 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
1505 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
1506 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
1507 nullstrecd
= formatspecs
[4].encode('UTF-8');
1508 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
1509 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
1510 catfp
.write(catfileout
);
1513 os
.fsync(catfp
.fileno());
1514 except io
.UnsupportedOperation
:
1516 except AttributeError:
1518 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1519 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1522 os
.fsync(catfp
.fileno());
1523 except io
.UnsupportedOperation
:
1525 except AttributeError:
1529 if(hasattr(sys
.stdout
, "buffer")):
1530 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1532 shutil
.copyfileobj(catfp
, sys
.stdout
);
1533 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
1534 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1536 upload_file_to_ftp_file(catfp
, outfile
);
1537 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
1538 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1540 upload_file_to_sftp_file(catfp
, outfile
);
1548 create_alias_function("Pack", __file_format_name__
, "", PackArchiveFile
);
1550 if(hasattr(shutil
, "register_archive_format")):
1551 def PackArchiveFileFunc(archive_name
, source_dir
, **kwargs
):
1552 return PackArchiveFile(source_dir
, archive_name
, False, "auto", None, False, "crc32", [], __file_format_delimiter__
, False, False);
1553 create_alias_function("Pack", __file_format_name__
, "Func", PackArchiveFileFunc
);
1555 def PackArchiveFileFromDirList(infiles
, outfile
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1556 return PackArchiveFile(infiles
, outfile
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, returnfp
);
1558 def PackArchiveFileFromTarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1559 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1560 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1561 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1562 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1563 outfile
= RemoveWindowsPath(outfile
);
1564 checksumtype
= checksumtype
.lower();
1565 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
1566 checksumtype
="crc32";
1567 if(checksumtype
=="none"):
1569 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[1]):
1571 if(compression
not in compressionlist
and compression
is None):
1572 compression
= "auto";
1574 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1575 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1576 if(os
.path
.exists(outfile
)):
1581 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1583 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
1585 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
1588 fbasename
= os
.path
.splitext(outfile
)[0];
1589 fextname
= os
.path
.splitext(outfile
)[1];
1590 catfp
= CompressOpenFile(outfile
, compressionlevel
);
1591 catver
= formatspecs
[5];
1592 fileheaderver
= str(int(catver
.replace(".", "")));
1593 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
1594 catfp
.write(fileheader
.encode('UTF-8'));
1600 inodetocatinode
= {};
1601 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
1604 if(not tarfile
.is_tarfile(infile
)):
1606 except AttributeError:
1607 if(not is_tarfile(infile
)):
1610 tarfp
= tarfile
.open(infile
, "r");
1611 except FileNotFoundError
:
1613 fnumfiles
= format(int(len(tarfp
.getmembers())), 'x').lower();
1614 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[4]);
1615 if(checksumtype
=="none" or checksumtype
==""):
1616 catfileheadercshex
= format(0, 'x').lower();
1617 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1618 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1619 elif(checksumtype
=="crc16_ccitt"):
1620 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1621 elif(checksumtype
=="adler32"):
1622 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1623 elif(checksumtype
=="crc32"):
1624 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1625 elif(checksumtype
=="crc64_ecma"):
1626 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1627 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1628 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1629 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1630 checksumoutstr
= hashlib
.new(checksumtype
);
1631 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
1632 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1634 catfileheadercshex
= format(0, 'x').lower();
1635 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
1636 catfp
.write(fnumfilesa
.encode('UTF-8'));
1639 os
.fsync(catfp
.fileno());
1640 except io
.UnsupportedOperation
:
1642 except AttributeError:
1644 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
1645 catfhstart
= catfp
.tell();
1646 if(re
.findall("^[.|/]", member
.name
)):
1647 fname
= member
.name
;
1649 fname
= "./"+member
.name
;
1651 VerbosePrintOut(fname
);
1652 fpremode
= member
.mode
;
1653 ffullmode
= member
.mode
;
1657 ffullmode
= member
.mode
+ stat
.S_IFREG
;
1659 elif(member
.isdev()):
1660 ffullmode
= member
.mode
;
1662 elif(member
.islnk()):
1663 ffullmode
= member
.mode
+ stat
.S_IFREG
;
1665 elif(member
.issym()):
1666 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
1668 elif(member
.ischr()):
1669 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
1671 elif(member
.isblk()):
1672 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
1674 elif(member
.isdir()):
1675 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
1677 elif(member
.isfifo()):
1678 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
1680 elif(member
.issparse()):
1681 ffullmode
= member
.mode
;
1684 ffullmode
= member
.mode
;
1687 fcurfid
= format(int(curfid
), 'x').lower();
1688 fcurinode
= format(int(0), 'x').lower();
1689 curfid
= curfid
+ 1;
1691 flinkname
= member
.linkname
;
1692 fdev_minor
= format(int(member
.devminor
), 'x').lower();
1693 fdev_major
= format(int(member
.devmajor
), 'x').lower();
1694 frdev_minor
= format(int(member
.devminor
), 'x').lower();
1695 frdev_major
= format(int(member
.devmajor
), 'x').lower();
1696 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
1697 fsize
= format(int("0"), 'x').lower();
1698 elif(ftype
==0 or ftype
==7):
1699 fsize
= format(int(member
.size
), 'x').lower();
1701 fsize
= format(int(member
.size
), 'x').lower();
1702 fatime
= format(int(member
.mtime
), 'x').lower();
1703 fmtime
= format(int(member
.mtime
), 'x').lower();
1704 fctime
= format(int(member
.mtime
), 'x').lower();
1705 fbtime
= format(int(member
.mtime
), 'x').lower();
1706 fmode
= format(int(ffullmode
), 'x').lower();
1707 fchmode
= format(int(stat
.S_IMODE(ffullmode
)), 'x').lower();
1708 ftypemod
= format(int(stat
.S_IFMT(ffullmode
)), 'x').lower();
1709 fuid
= format(int(member
.uid
), 'x').lower();
1710 fgid
= format(int(member
.gid
), 'x').lower();
1711 funame
= member
.uname
;
1712 fgname
= member
.gname
;
1713 flinkcount
= format(int(flinkcount
), 'x').lower();
1714 fcontents
= "".encode('UTF-8');
1716 if(ftype
== 0 or ftype
== 7):
1717 with tarfp
.extractfile(member
) as fpc
:
1719 chunk
= fpc
.read(chunk_size
);
1723 ftypehex
= format(ftype
, 'x').lower();
1724 extrafields
= format(len(extradata
), 'x').lower();
1725 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
1726 if(len(extradata
)>0):
1727 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
1728 extrasizelen
= format(len(extrasizestr
), 'x').lower();
1729 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]);
1730 if(len(extradata
)>0):
1731 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[4]);
1732 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[4]);
1733 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
1734 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
1735 if(checksumtype
=="none" or checksumtype
==""):
1736 catfileheadercshex
= format(0, 'x').lower();
1737 catfilecontentcshex
= format(0, 'x').lower();
1738 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1739 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
1740 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
1741 elif(checksumtype
=="crc16_ccitt"):
1742 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
1743 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
1744 elif(checksumtype
=="adler32"):
1745 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1746 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
1747 elif(checksumtype
=="crc32"):
1748 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1749 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
1750 elif(checksumtype
=="crc64_ecma"):
1751 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1752 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
1753 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1754 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1755 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
1756 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1757 checksumoutstr
= hashlib
.new(checksumtype
);
1758 checksumoutstr
.update("".encode('UTF-8'));
1759 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1760 checksumoutstr
= hashlib
.new(checksumtype
);
1761 checksumoutstr
.update(fcontents
);
1762 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
1764 catfileheadercshex
= format(0, 'x').lower();
1765 catfilecontentcshex
= format(0, 'x').lower();
1766 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
1767 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
1768 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[4]) + catfileoutstr
;
1769 if(checksumtype
=="none" or checksumtype
==""):
1770 catfileheadercshex
= format(0, 'x').lower();
1771 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1772 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
1773 elif(checksumtype
=="crc16_ccitt"):
1774 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
1775 elif(checksumtype
=="adler32"):
1776 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
1777 elif(checksumtype
=="crc32"):
1778 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
1779 elif(checksumtype
=="crc64_ecma"):
1780 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1781 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1782 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1783 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1784 checksumoutstr
= hashlib
.new(checksumtype
);
1785 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
1786 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1788 catfileheadercshex
= format(0, 'x').lower();
1789 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
1790 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
1791 nullstrecd
= formatspecs
[4].encode('UTF-8');
1792 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
1793 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
1794 catfp
.write(catfileout
);
1797 os
.fsync(catfp
.fileno());
1798 except io
.UnsupportedOperation
:
1800 except AttributeError:
1802 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1803 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1806 os
.fsync(catfp
.fileno());
1807 except io
.UnsupportedOperation
:
1809 except AttributeError:
1813 if(hasattr(sys
.stdout
, "buffer")):
1814 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
1816 shutil
.copyfileobj(catfp
, sys
.stdout
);
1817 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
1818 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1820 upload_file_to_ftp_file(catfp
, outfile
);
1821 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
1822 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
1824 upload_file_to_sftp_file(catfp
, outfile
);
1832 create_alias_function("Pack", __file_format_name__
, "FromTarFile", PackArchiveFileFromTarFile
);
1834 def PackArchiveFileFromZipFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
1835 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1836 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1837 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1838 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1839 outfile
= RemoveWindowsPath(outfile
);
1840 checksumtype
= checksumtype
.lower();
1841 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
1842 checksumtype
="crc32";
1843 if(checksumtype
=="none"):
1845 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[1]):
1847 if(compression
not in compressionlist
and compression
is None):
1848 compression
= "auto";
1850 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
1851 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
1852 if(os
.path
.exists(outfile
)):
1857 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
1859 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
1861 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
1864 fbasename
= os
.path
.splitext(outfile
)[0];
1865 fextname
= os
.path
.splitext(outfile
)[1];
1866 catfp
= CompressOpenFile(outfile
, compressionlevel
);
1867 catver
= formatspecs
[5];
1868 fileheaderver
= str(int(catver
.replace(".", "")));
1869 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
1870 catfp
.write(fileheader
.encode('UTF-8'));
1876 inodetocatinode
= {};
1877 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
1879 if(not zipfile
.is_zipfile(infile
)):
1881 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
1882 ziptest
= zipfp
.testzip();
1884 VerbosePrintOut("Bad file found!");
1885 fnumfiles
= format(int(len(zipfp
.infolist())), 'x').lower();
1886 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[4]);
1887 if(checksumtype
=="none" or checksumtype
==""):
1888 catfileheadercshex
= format(0, 'x').lower();
1889 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
1890 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1891 elif(checksumtype
=="crc16_ccitt"):
1892 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
1893 elif(checksumtype
=="adler32"):
1894 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1895 elif(checksumtype
=="crc32"):
1896 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
1897 elif(checksumtype
=="crc64_ecma"):
1898 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1899 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
1900 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1901 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
1902 checksumoutstr
= hashlib
.new(checksumtype
);
1903 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
1904 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
1906 catfileheadercshex
= format(0, 'x').lower();
1907 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
1908 catfp
.write(fnumfilesa
.encode('UTF-8'));
1911 os
.fsync(catfp
.fileno());
1912 except io
.UnsupportedOperation
:
1914 except AttributeError:
1916 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
1917 catfhstart
= catfp
.tell();
1918 if(re
.findall("^[.|/]", member
.filename
)):
1919 fname
= member
.filename
;
1921 fname
= "./"+member
.filename
;
1922 zipinfo
= zipfp
.getinfo(member
.filename
);
1924 VerbosePrintOut(fname
);
1925 if(not member
.is_dir()):
1926 fpremode
= int(stat
.S_IFREG
+ 438);
1927 elif(member
.is_dir()):
1928 fpremode
= int(stat
.S_IFDIR
+ 511);
1931 if(not member
.is_dir()):
1933 elif(member
.is_dir()):
1936 fcurfid
= format(int(curfid
), 'x').lower();
1937 fcurinode
= format(int(0), 'x').lower();
1938 curfid
= curfid
+ 1;
1939 fdev_minor
= format(int(0), 'x').lower();
1940 fdev_major
= format(int(0), 'x').lower();
1941 frdev_minor
= format(int(0), 'x').lower();
1942 frdev_major
= format(int(0), 'x').lower();
1944 fsize
= format(int("0"), 'x').lower();
1946 fsize
= format(int(member
.file_size
), 'x').lower();
1948 fsize
= format(int(member
.file_size
), 'x').lower();
1949 fatime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
1950 fmtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
1951 fctime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
1952 fbtime
= format(int(time
.mktime(member
.date_time
+ (0, 0, -1))), 'x').lower();
1953 if(not member
.is_dir()):
1954 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
1955 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
1956 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
1957 if(member
.is_dir()):
1958 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
1959 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
1960 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
1962 fuid
= format(int(os
.getuid()), 'x').lower();
1963 except AttributeError:
1964 fuid
= format(int(0), 'x').lower();
1966 fuid
= format(int(0), 'x').lower();
1968 fgid
= format(int(os
.getgid()), 'x').lower();
1969 except AttributeError:
1970 fgid
= format(int(0), 'x').lower();
1972 fgid
= format(int(0), 'x').lower();
1976 userinfo
= pwd
.getpwuid(os
.getuid());
1977 funame
= userinfo
.pw_name
;
1980 except AttributeError:
1988 groupinfo
= grp
.getgrgid(os
.getgid());
1989 fgname
= groupinfo
.gr_name
;
1992 except AttributeError:
1996 fcontents
= "".encode('UTF-8');
1998 fcontents
= zipfp
.read(member
.filename
);
1999 ftypehex
= format(ftype
, 'x').lower();
2000 extrafields
= format(len(extradata
), 'x').lower();
2001 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
2002 if(len(extradata
)>0):
2003 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
2004 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2005 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]);
2006 if(len(extradata
)>0):
2007 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[4]);
2008 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[4]);
2009 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
2010 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
2011 if(checksumtype
=="none" or checksumtype
==""):
2012 catfileheadercshex
= format(0, 'x').lower();
2013 catfilecontentcshex
= format(0, 'x').lower();
2014 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2015 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2016 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
2017 elif(checksumtype
=="crc16_ccitt"):
2018 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2019 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
2020 elif(checksumtype
=="adler32"):
2021 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2022 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
2023 elif(checksumtype
=="crc32"):
2024 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2025 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
2026 elif(checksumtype
=="crc64_ecma"):
2027 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2028 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
2029 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2030 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2031 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
2032 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2033 checksumoutstr
= hashlib
.new(checksumtype
);
2034 checksumoutstr
.update("".encode('UTF-8'));
2035 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2036 checksumoutstr
= hashlib
.new(checksumtype
);
2037 checksumoutstr
.update(fcontents
);
2038 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
2040 catfileheadercshex
= format(0, 'x').lower();
2041 catfilecontentcshex
= format(0, 'x').lower();
2042 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
2043 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2044 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[4]) + catfileoutstr
;
2045 if(checksumtype
=="none" or checksumtype
==""):
2046 catfileheadercshex
= format(0, 'x').lower();
2047 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2048 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2049 elif(checksumtype
=="crc16_ccitt"):
2050 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
2051 elif(checksumtype
=="adler32"):
2052 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2053 elif(checksumtype
=="crc32"):
2054 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2055 elif(checksumtype
=="crc64_ecma"):
2056 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2057 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2058 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2059 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2060 checksumoutstr
= hashlib
.new(checksumtype
);
2061 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
2062 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2064 catfileheadercshex
= format(0, 'x').lower();
2065 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
2066 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
2067 nullstrecd
= formatspecs
[4].encode('UTF-8');
2068 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
2069 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
2070 catfp
.write(catfileout
);
2073 os
.fsync(catfp
.fileno());
2074 except io
.UnsupportedOperation
:
2076 except AttributeError:
2078 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2079 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2082 os
.fsync(catfp
.fileno());
2083 except io
.UnsupportedOperation
:
2085 except AttributeError:
2089 if(hasattr(sys
.stdout
, "buffer")):
2090 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2092 shutil
.copyfileobj(catfp
, sys
.stdout
);
2093 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
2094 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2096 upload_file_to_ftp_file(catfp
, outfile
);
2097 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
2098 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2100 upload_file_to_sftp_file(catfp
, outfile
);
2108 create_alias_function("Pack", __file_format_name__
, "FromZipFile", PackArchiveFileFromZipFile
);
2110 if(not rarfile_support
):
2111 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2114 if(rarfile_support
):
2115 def PackArchiveFileFromRarFile(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
2116 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2117 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2118 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
2119 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2120 outfile
= RemoveWindowsPath(outfile
);
2121 checksumtype
= checksumtype
.lower();
2122 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
2123 checksumtype
="crc32";
2124 if(checksumtype
=="none"):
2126 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[1]):
2128 if(compression
not in compressionlist
and compression
is None):
2129 compression
= "auto";
2131 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
2132 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
2133 if(os
.path
.exists(outfile
)):
2138 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2140 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
2142 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
2145 fbasename
= os
.path
.splitext(outfile
)[0];
2146 fextname
= os
.path
.splitext(outfile
)[1];
2147 catfp
= CompressOpenFile(outfile
, compressionlevel
);
2148 catver
= formatspecs
[5];
2149 fileheaderver
= str(int(catver
.replace(".", "")));
2150 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
2151 catfp
.write(fileheader
.encode('UTF-8'));
2157 inodetocatinode
= {};
2158 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
2160 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
2162 rarfp
= rarfile
.RarFile(infile
, "r");
2163 rartest
= rarfp
.testrar();
2165 VerbosePrintOut("Bad file found!");
2166 fnumfiles
= format(int(len(rarfp
.infolist())), 'x').lower();
2167 fnumfilesa
= AppendNullBytes([fnumfiles
, checksumtype
], formatspecs
[4]);
2168 if(checksumtype
=="none" or checksumtype
==""):
2169 catfileheadercshex
= format(0, 'x').lower();
2170 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2171 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2172 elif(checksumtype
=="crc16_ccitt"):
2173 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
2174 elif(checksumtype
=="adler32"):
2175 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2176 elif(checksumtype
=="crc32"):
2177 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
2178 elif(checksumtype
=="crc64_ecma"):
2179 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2180 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2181 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2182 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2183 checksumoutstr
= hashlib
.new(checksumtype
);
2184 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
2185 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2187 catfileheadercshex
= format(0, 'x').lower();
2188 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
2189 catfp
.write(fnumfilesa
.encode('UTF-8'));
2192 os
.fsync(catfp
.fileno());
2193 except io
.UnsupportedOperation
:
2195 except AttributeError:
2197 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
2200 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
2203 member
.external_attr
2205 except AttributeError:
2207 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
2210 member
.external_attr
2212 except AttributeError:
2217 catfhstart
= catfp
.tell();
2218 if(re
.findall("^[.|/]", member
.filename
)):
2219 fname
= member
.filename
;
2221 fname
= "./"+member
.filename
;
2222 rarinfo
= rarfp
.getinfo(member
.filename
);
2224 VerbosePrintOut(fname
);
2225 if(is_unix
and member
.external_attr
!=0):
2226 fpremode
= int(member
.external_attr
);
2227 elif(member
.is_file()):
2228 fpremode
= int(stat
.S_IFREG
+ 438);
2229 elif(member
.is_symlink()):
2230 fpremode
= int(stat
.S_IFLNK
+ 438);
2231 elif(member
.is_dir()):
2232 fpremode
= int(stat
.S_IFDIR
+ 511);
2233 if(is_windows
and member
.external_attr
!=0):
2234 fwinattributes
= format(int(member
.external_attr
), 'x').lower();
2236 fwinattributes
= format(int(0), 'x').lower();
2239 if(member
.is_file()):
2241 elif(member
.is_symlink()):
2243 elif(member
.is_dir()):
2247 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
2248 fcurfid
= format(int(curfid
), 'x').lower();
2249 fcurinode
= format(int(0), 'x').lower();
2250 curfid
= curfid
+ 1;
2251 fdev_minor
= format(int(0), 'x').lower();
2252 fdev_major
= format(int(0), 'x').lower();
2253 frdev_minor
= format(int(0), 'x').lower();
2254 frdev_major
= format(int(0), 'x').lower();
2256 fsize
= format(int("0"), 'x').lower();
2258 fsize
= format(int(member
.file_size
), 'x').lower();
2260 fsize
= format(int(member
.file_size
), 'x').lower();
2263 fatime
= format(int(member
.atime
.timestamp()), 'x').lower();
2265 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2266 except AttributeError:
2267 fatime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2268 fmtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2271 fctime
= format(int(member
.ctime
.timestamp()), 'x').lower();
2273 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2274 except AttributeError:
2275 fctime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2276 fbtime
= format(int(member
.mtime
.timestamp()), 'x').lower();
2277 if(is_unix
and member
.external_attr
!=0):
2278 fmode
= format(int(member
.external_attr
), 'x').lower();
2279 fchmode
= format(int(stat
.S_IMODE(member
.external_attr
)), 'x').lower();
2280 ftypemod
= format(int(stat
.S_IFMT(member
.external_attr
)), 'x').lower();
2281 elif(member
.is_file()):
2282 fmode
= format(int(stat
.S_IFREG
+ 438), 'x').lower();
2283 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
2284 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
2285 elif(member
.is_symlink()):
2286 fmode
= format(int(stat
.S_IFLNK
+ 438), 'x').lower();
2287 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438))), 'x').lower();
2288 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438))), 'x').lower();
2289 elif(member
.is_dir()):
2290 fmode
= format(int(stat
.S_IFDIR
+ 511), 'x').lower();
2291 fchmode
= format(int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511))), 'x').lower();
2292 ftypemod
= format(int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511))), 'x').lower();
2294 fuid
= format(int(os
.getuid()), 'x').lower();
2295 except AttributeError:
2296 fuid
= format(int(0), 'x').lower();
2298 fuid
= format(int(0), 'x').lower();
2300 fgid
= format(int(os
.getgid()), 'x').lower();
2301 except AttributeError:
2302 fgid
= format(int(0), 'x').lower();
2304 fgid
= format(int(0), 'x').lower();
2308 userinfo
= pwd
.getpwuid(os
.getuid());
2309 funame
= userinfo
.pw_name
;
2312 except AttributeError:
2320 groupinfo
= grp
.getgrgid(os
.getgid());
2321 fgname
= groupinfo
.gr_name
;
2324 except AttributeError:
2328 fcontents
= "".encode('UTF-8');
2330 fcontents
= rarfp
.read(member
.filename
);
2331 ftypehex
= format(ftype
, 'x').lower();
2332 extrafields
= format(len(extradata
), 'x').lower();
2333 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
2334 if(len(extradata
)>0):
2335 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
2336 extrasizelen
= format(len(extrasizestr
), 'x').lower();
2337 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]);
2338 if(len(extradata
)>0):
2339 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[4]);
2340 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[4]);
2341 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
2342 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
2343 if(checksumtype
=="none" or checksumtype
==""):
2344 catfileheadercshex
= format(0, 'x').lower();
2345 catfilecontentcshex
= format(0, 'x').lower();
2346 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2347 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2348 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
2349 elif(checksumtype
=="crc16_ccitt"):
2350 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2351 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
2352 elif(checksumtype
=="adler32"):
2353 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2354 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
2355 elif(checksumtype
=="crc32"):
2356 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2357 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
2358 elif(checksumtype
=="crc64_ecma"):
2359 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2360 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
2361 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2362 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2363 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
2364 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2365 checksumoutstr
= hashlib
.new(checksumtype
);
2366 checksumoutstr
.update("".encode('UTF-8'));
2367 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2368 checksumoutstr
= hashlib
.new(checksumtype
);
2369 checksumoutstr
.update(fcontents
);
2370 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
2372 catfileheadercshex
= format(0, 'x').lower();
2373 catfilecontentcshex
= format(0, 'x').lower();
2374 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
2375 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
2376 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[4]) + catfileoutstr
2377 if(checksumtype
=="none" or checksumtype
==""):
2378 catfileheadercshex
= format(0, 'x').lower()
2379 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
2380 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower()
2381 elif(checksumtype
=="crc16_ccitt"):
2382 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower()
2383 elif(checksumtype
=="adler32"):
2384 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower()
2385 elif(checksumtype
=="crc32"):
2386 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower()
2387 elif(checksumtype
=="crc64_ecma"):
2388 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
2389 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
2390 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
2391 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
2392 checksumoutstr
= hashlib
.new(checksumtype
)
2393 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'))
2394 catfileheadercshex
= checksumoutstr
.hexdigest().lower()
2396 catfileheadercshex
= format(0, 'x').lower()
2397 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4])
2398 catfileoutstrecd
= catfileoutstr
.encode('UTF-8')
2399 nullstrecd
= formatspecs
[4].encode('UTF-8')
2400 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
2401 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
)
2402 catfp
.write(catfileout
)
2405 os
.fsync(catfp
.fileno())
2406 except io
.UnsupportedOperation
:
2408 except AttributeError:
2410 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
2411 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
)
2414 os
.fsync(catfp
.fileno())
2415 except io
.UnsupportedOperation
:
2417 except AttributeError:
2421 if(hasattr(sys
.stdout
, "buffer")):
2422 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
2424 shutil
.copyfileobj(catfp
, sys
.stdout
);
2425 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
2426 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2428 upload_file_to_ftp_file(catfp
, outfile
);
2429 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
2430 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
2432 upload_file_to_sftp_file(catfp
, outfile
);
2440 create_alias_function("Pack", __file_format_name__
, "FromRarFile", PackArchiveFileFromRarFile
);
2442 def ArchiveFileSeekToFileNum(infile
, seekto
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
2443 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2446 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2447 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
2448 if(checkcompressfile
=="tarfile"):
2449 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2450 if(checkcompressfile
=="zipfile"):
2451 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2452 if(rarfile_support
and checkcompressfile
=="rarfile"):
2453 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2454 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[1]):
2461 if(hasattr(sys
.stdin
, "buffer")):
2462 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
2464 shutil
.copyfileobj(sys
.stdin
, catfp
);
2466 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2470 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", infile
)):
2471 catfp
= download_file_from_ftp_file(infile
);
2472 elif(re
.findall(r
"^(sftp)\:\/\/", infile
) and haveparamiko
):
2473 catfp
= download_file_from_sftp_file(infile
);
2475 infile
= RemoveWindowsPath(infile
);
2476 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
2477 if(checkcompressfile
=="tarfile"):
2478 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2479 if(checkcompressfile
=="zipfile"):
2480 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2481 if(rarfile_support
and checkcompressfile
=="rarfile"):
2482 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2483 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[1]):
2485 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
2486 if(not compresscheck
):
2487 fextname
= os
.path
.splitext(infile
)[1];
2488 if(fextname
==".gz"):
2489 compresscheck
= "gzip";
2490 if(fextname
==".bz2"):
2491 compresscheck
= "bzip2";
2492 if(fextname
==".zst"):
2493 compresscheck
= "zstd";
2494 if(fextname
==".lz4" or fextname
==".clz4"):
2495 compresscheck
= "lz4";
2496 if(fextname
==".lzo" or fextname
==".lzop"):
2497 compresscheck
= "lzo";
2498 if(fextname
==".lzma" or fextname
==".xz"):
2499 compresscheck
= "lzma";
2500 if(not compresscheck
):
2502 catfp
= UncompressFile(infile
, formatspecs
, "rb");
2507 SeekToEndOfFile(catfp);
2509 SeekToEndOfFile(catfp);
2510 CatSize = catfp.tell();
2511 CatSizeEnd = CatSize;
2519 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[4]);
2520 catstring
= catheader
[0];
2521 catversion
= re
.findall(r
"([\d]+)$", catstring
);
2522 fprenumfiles
= catheader
[1];
2523 fnumfiles
= int(fprenumfiles
, 16);
2524 fprechecksumtype
= catheader
[2];
2525 fprechecksum
= catheader
[3];
2526 fileheader
= AppendNullByte(catstring
, formatspecs
[4]);
2527 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
2528 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[4]);
2529 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
2530 catfileheadercshex
= format(0, 'x').lower();
2531 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
2532 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2533 elif(fprechecksumtype
=="crc16_ccitt"):
2534 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2535 elif(fprechecksumtype
=="adler32"):
2536 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2537 elif(fprechecksumtype
=="crc32"):
2538 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2539 elif(fprechecksumtype
=="crc64_ecma"):
2540 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2541 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
2542 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2543 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
2544 checksumoutstr
= hashlib
.new(fprechecksumtype
);
2545 checksumoutstr
.update(fileheader
.encode('UTF-8'));
2546 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2548 catfileheadercshex
= format(0, 'x').lower();
2549 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
2550 fheadtell
= len(fileheader
);
2551 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
2552 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(catfp
.tell()));
2554 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
2555 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
2556 if(seekto
>=fnumfiles
):
2557 seekto
= fnumfiles
- 1;
2563 seekstart
= catfp
.tell();
2564 preheaderdata
= ReadFileHeaderData(catfp
, 5, formatspecs
[4]);
2565 prefheadsize
= int(preheaderdata
[0], 16);
2566 prefseek
= prefheadsize
- (int(len(preheaderdata
[1]) + 1) + int(len(preheaderdata
[2]) + 1) + int(len(preheaderdata
[3]) + 1) + int(len(preheaderdata
[4]) + 1));
2567 preftype
= int(preheaderdata
[1], 16);
2568 prefsize
= int(preheaderdata
[4], 16);
2569 catfp
.seek(prefseek
, 1);
2571 catfp
.seek(prefsize
, 1);
2574 catfp
.seek(seekstart
, 0);
2576 catfheadsize
= int(preheaderdata
[0], 16);
2577 catftype
= int(preheaderdata
[1], 16);
2578 if(re
.findall("^[.|/]", preheaderdata
[2])):
2579 catfname
= preheaderdata
[2];
2581 catfname
= "./"+preheaderdata
[2];
2582 catflinkname
= preheaderdata
[3];
2583 catfsize
= int(preheaderdata
[4], 16);
2584 catfbasedir
= os
.path
.dirname(catfname
);
2585 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
2587 catlist
.update({'catfp': catfp
});
2592 create_alias_function("", __file_format_name__
, "SeekToFileNum", ArchiveFileSeekToFileNum
);
2594 def ArchiveFileSeekToFileName(infile
, seekfile
=None, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
2595 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2598 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2599 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
2600 if(checkcompressfile
=="tarfile"):
2601 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2602 if(checkcompressfile
=="zipfile"):
2603 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2604 if(rarfile_support
and checkcompressfile
=="rarfile"):
2605 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2606 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[1]):
2613 if(hasattr(sys
.stdin
, "buffer")):
2614 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
2616 shutil
.copyfileobj(sys
.stdin
, catfp
);
2618 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2622 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", infile
)):
2623 catfp
= download_file_from_ftp_file(infile
);
2624 elif(re
.findall(r
"^(sftp)\:\/\/", infile
) and haveparamiko
):
2625 catfp
= download_file_from_sftp_file(infile
);
2627 infile
= RemoveWindowsPath(infile
);
2628 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
2629 if(checkcompressfile
=="tarfile"):
2630 return TarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2631 if(checkcompressfile
=="zipfile"):
2632 return ZipFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2633 if(rarfile_support
and checkcompressfile
=="rarfile"):
2634 return RarFileToArray(infile
, 0, 0, listonly
, skipchecksum
, formatspecs
, returnfp
);
2635 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[1]):
2637 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
2638 if(not compresscheck
):
2639 fextname
= os
.path
.splitext(infile
)[1];
2640 if(fextname
==".gz"):
2641 compresscheck
= "gzip";
2642 if(fextname
==".bz2"):
2643 compresscheck
= "bzip2";
2644 if(fextname
==".zst"):
2645 compresscheck
= "zstd";
2646 if(fextname
==".lz4" or fextname
==".clz4"):
2647 compresscheck
= "lz4";
2648 if(fextname
==".lzo" or fextname
==".lzop"):
2649 compresscheck
= "lzo";
2650 if(fextname
==".lzma" or fextname
==".xz"):
2651 compresscheck
= "lzma";
2652 if(not compresscheck
):
2654 catfp
= UncompressFile(infile
, formatspecs
, "rb");
2659 SeekToEndOfFile(catfp);
2661 SeekToEndOfFile(catfp);
2662 CatSize = catfp.tell();
2663 CatSizeEnd = CatSize;
2671 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[4]);
2672 catstring
= catheader
[0];
2673 catversion
= re
.findall(r
"([\d]+)$", catstring
);
2674 fprenumfiles
= catheader
[1];
2675 fnumfiles
= int(fprenumfiles
, 16);
2676 fprechecksumtype
= catheader
[2];
2677 fprechecksum
= catheader
[3];
2678 fileheader
= AppendNullByte(catstring
, formatspecs
[4]);
2679 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
2680 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[4]);
2681 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
2682 catfileheadercshex
= format(0, 'x').lower();
2683 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
2684 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2685 elif(fprechecksumtype
=="crc16_ccitt"):
2686 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2687 elif(fprechecksumtype
=="adler32"):
2688 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2689 elif(fprechecksumtype
=="crc32"):
2690 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2691 elif(fprechecksumtype
=="crc64_ecma"):
2692 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2693 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
2694 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2695 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
2696 checksumoutstr
= hashlib
.new(fprechecksumtype
);
2697 checksumoutstr
.update(fileheader
.encode('UTF-8'));
2698 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2700 catfileheadercshex
= format(0, 'x').lower();
2701 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
2702 fheadtell
= len(fileheader
);
2703 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
2704 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(catfp
.tell()));
2706 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
2707 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
2708 seekto
= fnumfiles
- 1
2713 seekstart
= catfp
.tell();
2714 preheaderdata
= ReadFileHeaderData(catfp
, 5, formatspecs
[4]);
2715 prefheadsize
= int(preheaderdata
[0], 16);
2716 prefseek
= prefheadsize
- (int(len(preheaderdata
[1]) + 1) + int(len(preheaderdata
[2]) + 1) + int(len(preheaderdata
[3]) + 1) + int(len(preheaderdata
[4]) + 1));
2717 preftype
= int(preheaderdata
[1], 16);
2718 prefsize
= int(preheaderdata
[4], 16);
2719 catfp
.seek(prefseek
, 1);
2721 catfp
.seek(prefsize
, 1);
2725 prefname
= preheaderdata
[2];
2726 if(re
.findall("^[.|/]", preheaderdata
[2])):
2727 prefname
= preheaderdata
[2];
2729 prefname
= "./"+preheaderdata
[2];
2730 if(prefname
==seekfile
):
2733 catfp
.seek(seekstart
, 0);
2735 catfheadsize
= int(preheaderdata
[0], 16);
2736 catftype
= int(preheaderdata
[1], 16);
2737 if(re
.findall("^[.|/]", preheaderdata
[2])):
2738 catfname
= preheaderdata
[2];
2740 catfname
= "./"+preheaderdata
[2];
2741 catflinkname
= preheaderdata
[3];
2742 catfsize
= int(preheaderdata
[4], 16);
2743 catfbasedir
= os
.path
.dirname(catfname
);
2745 catlist
= {'fid': fileidnum
, 'foffset': catfp
.tell(), 'ftype': catftype
, 'fname': catfname
, 'fbasedir': catfbasedir
, 'flinkname': catflinkname
, 'fsize': catfsize
};
2749 catlist
.update({'catfp': catfp
});
2754 create_alias_function("", __file_format_name__
, "SeekToFileName", ArchiveFileSeekToFileName
);
2756 def ArchiveFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
2757 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
2760 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2761 checkcompressfile
= CheckCompressionSubType(catfp
, formatspecs
);
2762 if(checkcompressfile
=="tarfile"):
2763 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
2764 if(checkcompressfile
=="zipfile"):
2765 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
2766 if(rarfile_support
and checkcompressfile
=="rarfile"):
2767 return RarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
2768 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[1]):
2775 if(hasattr(sys
.stdin
, "buffer")):
2776 shutil
.copyfileobj(sys
.stdin
.buffer, catfp
);
2778 shutil
.copyfileobj(sys
.stdin
, catfp
);
2780 catfp
= UncompressArchiveFile(catfp
, formatspecs
);
2784 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", infile
)):
2785 catfp
= download_file_from_ftp_file(infile
);
2786 elif(re
.findall(r
"^(sftp)\:\/\/", infile
) and haveparamiko
):
2787 catfp
= download_file_from_sftp_file(infile
);
2789 infile
= RemoveWindowsPath(infile
);
2790 checkcompressfile
= CheckCompressionSubType(infile
, formatspecs
);
2791 if(checkcompressfile
=="tarfile"):
2792 return TarFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
2793 if(checkcompressfile
=="zipfile"):
2794 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
2795 if(rarfile_support
and checkcompressfile
=="rarfile"):
2796 return ZipFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
2797 if(checkcompressfile
!="catfile" and checkcompressfile
!=formatspecs
[1]):
2799 compresscheck
= CheckCompressionType(infile
, formatspecs
, True);
2800 if(not compresscheck
):
2801 fextname
= os
.path
.splitext(infile
)[1];
2802 if(fextname
==".gz"):
2803 compresscheck
= "gzip";
2804 if(fextname
==".bz2"):
2805 compresscheck
= "bzip2";
2806 if(fextname
==".zst"):
2807 compresscheck
= "zstd";
2808 if(fextname
==".lz4" or fextname
==".clz4"):
2809 compresscheck
= "lz4";
2810 if(fextname
==".lzo" or fextname
==".lzop"):
2811 compresscheck
= "lzo";
2812 if(fextname
==".lzma" or fextname
==".xz"):
2813 compresscheck
= "lzma";
2814 if(not compresscheck
):
2816 catfp
= UncompressFile(infile
, formatspecs
, "rb");
2821 SeekToEndOfFile(catfp);
2823 SeekToEndOfFile(catfp);
2824 CatSize = catfp.tell();
2825 CatSizeEnd = CatSize;
2833 catheader
= ReadFileHeaderData(catfp
, 4, formatspecs
[4]);
2834 catstring
= catheader
[0];
2835 catversion
= re
.findall(r
"([\d]+)$", catstring
);
2836 fprenumfiles
= catheader
[1];
2837 fnumfiles
= int(fprenumfiles
, 16);
2838 fprechecksumtype
= catheader
[2];
2839 fprechecksum
= catheader
[3];
2840 fileheader
= AppendNullByte(catstring
, formatspecs
[4]);
2841 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
2842 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, fprechecksumtype
], formatspecs
[4]);
2843 if(fprechecksumtype
=="none" or fprechecksumtype
==""):
2844 catfileheadercshex
= format(0, 'x').lower();
2845 elif(fprechecksumtype
=="crc16" or fprechecksumtype
=="crc16_ansi" or fprechecksumtype
=="crc16_ibm"):
2846 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2847 elif(fprechecksumtype
=="crc16_ccitt"):
2848 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
2849 elif(fprechecksumtype
=="adler32"):
2850 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2851 elif(fprechecksumtype
=="crc32"):
2852 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2853 elif(fprechecksumtype
=="crc64_ecma"):
2854 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2855 elif(fprechecksumtype
=="crc64" or fprechecksumtype
=="crc64_iso"):
2856 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2857 elif(CheckSumSupportAlt(fprechecksumtype
, hashlib_guaranteed
)):
2858 checksumoutstr
= hashlib
.new(fprechecksumtype
);
2859 checksumoutstr
.update(fileheader
.encode('UTF-8'));
2860 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
2862 catfileheadercshex
= format(0, 'x').lower();
2863 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
2864 fheadtell
= len(fileheader
);
2865 if(fprechecksum
!=catfileheadercshex
and not skipchecksum
):
2866 VerbosePrintOut("File Header Checksum Error with file " + infile
+ " at offset " + str(catfp
.tell()));
2868 catversions
= re
.search(r
'(.*?)(\d+)$', catstring
).groups();
2869 catlist
= {'fnumfiles': fnumfiles
, 'fformat': catversions
[0], 'fversion': catversions
[1], 'fformatspecs': formatspecs
, 'fchecksumtype': fprechecksumtype
, 'fheaderchecksum': fprechecksum
, 'ffilelist': {}};
2870 if(seekstart
<0 and seekstart
>fnumfiles
):
2872 if(seekend
==0 or seekend
>fnumfiles
and seekend
<seekstart
):
2873 seekend
= fnumfiles
;
2874 elif(seekend
<0 and abs(seekend
)<=fnumfiles
and abs(seekend
)>=seekstart
):
2875 seekend
= fnumfiles
- abs(seekend
);
2878 while(il
< seekstart
):
2879 preheaderdata
= ReadFileHeaderData(catfp
, 5, formatspecs
[4]);
2880 prefheadsize
= int(preheaderdata
[0], 16);
2881 prefseek
= prefheadsize
- (int(len(preheaderdata
[1]) + 1) + int(len(preheaderdata
[2]) + 1) + int(len(preheaderdata
[3]) + 1) + int(len(preheaderdata
[4]) + 1));
2882 preftype
= int(preheaderdata
[1], 16);
2883 prefsize
= int(preheaderdata
[4], 16);
2884 catfp
.seek(prefseek
, 1);
2886 catfp
.seek(prefsize
, 1);
2889 fileidnum
= seekstart
;
2891 while(fileidnum
<seekend
):
2892 catfhstart
= catfp
.tell();
2894 catheaderdata
= ReadFileHeaderDataBySize(catfp
, formatspecs
[4]);
2896 catheaderdata
= ReadFileHeaderData(catfp
, 23, formatspecs
[4]);
2897 catfheadsize
= int(catheaderdata
[0], 16);
2898 catftype
= int(catheaderdata
[1], 16);
2899 if(re
.findall("^[.|/]", catheaderdata
[2])):
2900 catfname
= catheaderdata
[2];
2902 catfname
= "./"+catheaderdata
[2];
2903 catfbasedir
= os
.path
.dirname(catfname
);
2904 catflinkname
= catheaderdata
[3];
2905 catfsize
= int(catheaderdata
[4], 16);
2906 catfatime
= int(catheaderdata
[5], 16);
2907 catfmtime
= int(catheaderdata
[6], 16);
2908 catfctime
= int(catheaderdata
[7], 16);
2909 catfbtime
= int(catheaderdata
[8], 16);
2910 catfmode
= int(catheaderdata
[9], 16);
2911 catfchmode
= stat
.S_IMODE(catfmode
);
2912 catftypemod
= stat
.S_IFMT(catfmode
);
2913 catfuid
= int(catheaderdata
[10], 16);
2914 catfuname
= catheaderdata
[11];
2915 catfgid
= int(catheaderdata
[12], 16);
2916 catfgname
= catheaderdata
[13];
2917 fid
= int(catheaderdata
[14], 16);
2918 finode
= int(catheaderdata
[15], 16);
2919 flinkcount
= int(catheaderdata
[16], 16);
2920 catfdev_minor
= int(catheaderdata
[17], 16);
2921 catfdev_major
= int(catheaderdata
[18], 16);
2922 catfrdev_minor
= int(catheaderdata
[19], 16);
2923 catfrdev_major
= int(catheaderdata
[20], 16);
2924 catfextrasize
= int(catheaderdata
[21], 16);
2925 catfextrafields
= int(catheaderdata
[22], 16);
2926 extrafieldslist
= [];
2929 extraend
= extrastart
+ catfextrafields
;
2930 extrafieldslist
= [];
2931 if(extrastart
<extraend
):
2932 extrafieldslist
.append(catheaderdata
[extrastart
]);
2933 extrastart
= extrastart
+ 1;
2934 catfchecksumtype
= catheaderdata
[extrastart
].lower();
2935 catfcs
= catheaderdata
[extrastart
+ 1].lower();
2936 catfccs
= catheaderdata
[extrastart
+ 2].lower();
2938 extrafieldslist
= [];
2939 if(catfextrafields
>0):
2940 extrafieldslist
= ReadFileHeaderData(catfp
, catfextrafields
, formatspecs
[4]);
2941 checksumsval
= ReadFileHeaderData(catfp
, 3, formatspecs
[4]);
2942 catfchecksumtype
= checksumsval
[0].lower();
2943 catfcs
= checksumsval
[1].lower();
2944 catfccs
= checksumsval
[2].lower();
2947 hcmax
= len(catheaderdata
) - 2;
2949 hcmax
= len(catheaderdata
);
2952 hout
= hout
+ AppendNullByte(catheaderdata
[hc
], formatspecs
[4]);
2954 catfnumfields
= 24 + catfextrafields
;
2955 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
2957 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
2958 catnewfcs
= format(crc16(hout
.encode('UTF-8')) & 0xffff, '04x').lower();
2959 elif(catfchecksumtype
=="adler32"):
2960 catnewfcs
= format(zlib
.adler32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2961 elif(catfchecksumtype
=="crc32"):
2962 catnewfcs
= format(crc32(hout
.encode('UTF-8')) & 0xffffffff, '08x').lower();
2963 elif(catfchecksumtype
=="crc64_ecma"):
2964 catnewfcs
= format(crc64_ecma(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2965 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
2966 catnewfcs
= format(crc64_iso(hout
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2967 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
2968 checksumoutstr
= hashlib
.new(catfchecksumtype
);
2969 checksumoutstr
.update(hout
.encode('UTF-8'));
2970 catnewfcs
= checksumoutstr
.hexdigest().lower();
2971 if(catfcs
!=catnewfcs
and not skipchecksum
):
2972 VerbosePrintOut("File Header Checksum Error with file " + catfname
+ " at offset " + str(catfhstart
));
2974 catfhend
= catfp
.tell() - 1;
2975 catfcontentstart
= catfp
.tell();
2977 pyhascontents
= False;
2978 if(catfsize
>0 and not listonly
):
2979 catfcontents
= catfp
.read(catfsize
);
2980 if(catfchecksumtype
=="none" or catfchecksumtype
==""):
2982 elif(catfchecksumtype
=="crc16" or catfchecksumtype
=="crc16_ansi" or catfchecksumtype
=="crc16_ibm"):
2983 catnewfccs
= format(crc16(catfcontents
) & 0xffff, '04x').lower();
2984 elif(catfchecksumtype
=="crc16_ccitt"):
2985 catnewfcs
= format(crc16_ccitt(catfcontents
) & 0xffff, '04x').lower();
2986 elif(catfchecksumtype
=="adler32"):
2987 catnewfccs
= format(zlib
.adler32(catfcontents
) & 0xffffffff, '08x').lower();
2988 elif(catfchecksumtype
=="crc32"):
2989 catnewfccs
= format(crc32(catfcontents
) & 0xffffffff, '08x').lower();
2990 elif(catfchecksumtype
=="crc64_ecma"):
2991 catnewfcs
= format(crc64_ecma(catfcontents
) & 0xffffffffffffffff, '016x').lower();
2992 elif(catfchecksumtype
=="crc64" or catfchecksumtype
=="crc64_iso"):
2993 catnewfcs
= format(crc64_iso(catfcontents
) & 0xffffffffffffffff, '016x').lower();
2994 elif(CheckSumSupportAlt(catfchecksumtype
, hashlib_guaranteed
)):
2995 checksumoutstr
= hashlib
.new(catfchecksumtype
);
2996 checksumoutstr
.update(catfcontents
);
2997 catnewfccs
= checksumoutstr
.hexdigest().lower();
2998 pyhascontents
= True;
2999 if(catfccs
!=catnewfccs
and skipchecksum
):
3000 VerbosePrintOut("File Content Checksum Error with file " + catfname
+ " at offset " + str(catfhstart
));
3002 if(catfsize
>0 and listonly
):
3003 catfp
.seek(catfsize
, 1);
3004 pyhascontents
= False;
3006 catfcontentend
= catfp
.tell() - 1;
3007 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
} });
3008 fileidnum
= fileidnum
+ 1;
3009 realidnum
= realidnum
+ 1;
3011 catlist
.update({'catfp': catfp
});
3016 create_alias_function("", __file_format_name__
, "ToArray", ArchiveFileToArray
);
3018 def ArchiveFileStringToArray(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3019 catfp
= BytesIO(catstr
);
3020 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3021 return listcatfiles
;
3023 create_alias_function("", __file_format_name__
, "StringToArray", ArchiveFileStringToArray
);
3025 def TarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3027 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
3028 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3029 return listcatfiles
;
3031 def ZipFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3033 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
3034 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3035 return listcatfiles
;
3037 if(not rarfile_support
):
3038 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3041 if(rarfile_support
):
3042 def RarFileToArray(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
3044 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
3045 listcatfiles
= ArchiveFileToArray(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
3046 return listcatfiles
;
3048 def ListDirToArrayAlt(infiles
, dirlistfromtxt
=False, followlink
=False, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
3049 catver
= formatspecs
[5];
3050 fileheaderver
= str(int(catver
.replace(".", "")));
3051 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
3052 advancedlist
= True;
3055 for line
in sys
.stdin
:
3056 infilelist
.append(line
.strip());
3057 infilelist
= list(filter(None, infilelist
));
3058 elif(infiles
!="-" and dirlistfromtxt
and os
.path
.exists(infiles
) and (os
.path
.isfile(infiles
) or infiles
=="/dev/null" or infiles
=="NUL")):
3059 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
3061 with
open(infiles
, "r") as finfile
:
3062 for line
in finfile
:
3063 infilelist
.append(line
.strip());
3064 infilelist
= list(filter(None, infilelist
));
3066 if(isinstance(infiles
, (list, tuple, ))):
3067 infilelist
= list(filter(None, infiles
));
3068 elif(isinstance(infiles
, (str, ))):
3069 infilelist
= list(filter(None, [infiles
]));
3071 GetDirList
= ListDirAdvanced(infilelist
, followlink
, False);
3073 GetDirList
= ListDir(infilelist
, followlink
, False);
3081 inodetocatinode
= {};
3083 fnumfiles
= int(len(GetDirList
));
3084 catver
= formatspecs
[5];
3085 fileheaderver
= str(int(catver
.replace(".", "")));
3086 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
3087 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3088 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[4]);
3089 catversion
= fileheaderver
;
3090 if(checksumtype
=="none" or checksumtype
==""):
3091 catfileheadercshex
= format(0, 'x').lower();
3092 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3093 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3094 elif(checksumtype
=="crc16_ccitt"):
3095 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3096 elif(checksumtype
=="adler32"):
3097 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3098 elif(checksumtype
=="crc32"):
3099 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3100 elif(checksumtype
=="crc64_ecma"):
3101 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3102 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3103 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3104 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3105 checksumoutstr
= hashlib
.new(checksumtype
);
3106 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3107 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3109 catfileheadercshex
= format(0, 'x').lower();
3110 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
3111 fheadtell
= len(fileheader
);
3112 catlist
= {'fnumfiles': fnumfiles
, 'fformat': formatspecs
[0], 'fversion': catversion
, 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
3113 for curfname
in GetDirList
:
3114 if(re
.findall("^[.|/]", curfname
)):
3117 fname
= "./"+curfname
;
3119 VerbosePrintOut(fname
);
3120 if(not followlink
or followlink
is None):
3121 fstatinfo
= os
.lstat(fname
);
3123 fstatinfo
= os
.stat(fname
);
3124 fpremode
= fstatinfo
.st_mode
;
3125 finode
= fstatinfo
.st_ino
;
3126 flinkcount
= fstatinfo
.st_nlink
;
3128 if(stat
.S_ISREG(fpremode
)):
3130 elif(stat
.S_ISLNK(fpremode
)):
3132 elif(stat
.S_ISCHR(fpremode
)):
3134 elif(stat
.S_ISBLK(fpremode
)):
3136 elif(stat
.S_ISDIR(fpremode
)):
3138 elif(stat
.S_ISFIFO(fpremode
)):
3140 elif(hasattr(stat
, "S_ISDOOR") and stat
.S_ISDOOR(fpremode
)):
3142 elif(hasattr(stat
, "S_ISPORT") and stat
.S_ISPORT(fpremode
)):
3144 elif(hasattr(stat
, "S_ISWHT") and stat
.S_ISWHT(fpremode
)):
3149 fbasedir
= os
.path
.dirname(fname
);
3151 if(not followlink
and finode
!=0):
3153 if(finode
in inodelist
):
3155 flinkname
= inodetofile
[finode
];
3156 fcurinode
= inodetocatinode
[finode
];
3157 if(finode
not in inodelist
):
3158 inodelist
.append(finode
);
3159 inodetofile
.update({finode
: fname
});
3160 inodetocatinode
.update({finode
: curinode
});
3161 fcurinode
= curinode
;
3162 curinode
= curinode
+ 1;
3164 fcurinode
= curinode
;
3165 curinode
= curinode
+ 1;
3166 curfid
= curfid
+ 1;
3168 flinkname
= os
.readlink(fname
);
3169 fdev
= fstatinfo
.st_dev
;
3170 getfdev
= GetDevMajorMinor(fdev
);
3171 fdev_minor
= getfdev
[0];
3172 fdev_major
= getfdev
[1];
3173 frdev
= fstatinfo
.st_dev
;
3174 if(hasattr(fstatinfo
, "st_rdev")):
3175 frdev
= fstatinfo
.st_rdev
;
3177 frdev
= fstatinfo
.st_dev
;
3178 getfrdev
= GetDevMajorMinor(frdev
);
3179 frdev_minor
= getfrdev
[0];
3180 frdev_major
= getfrdev
[1];
3181 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
3183 if(ftype
==0 or ftype
==7):
3184 fsize
= fstatinfo
.st_size
;
3185 fatime
= fstatinfo
.st_atime
;
3186 fmtime
= fstatinfo
.st_mtime
;
3187 fctime
= fstatinfo
.st_ctime
;
3188 if(hasattr(fstatinfo
, "st_birthtime")):
3189 fbtime
= fstatinfo
.st_birthtime
;
3191 fbtime
= fstatinfo
.st_ctime
;
3192 fmode
= fstatinfo
.st_mode
;
3193 fchmode
= stat
.S_IMODE(fstatinfo
.st_mode
);
3194 ftypemod
= stat
.S_IFMT(fstatinfo
.st_mode
);
3195 fuid
= fstatinfo
.st_uid
;
3196 fgid
= fstatinfo
.st_gid
;
3201 userinfo
= pwd
.getpwuid(fstatinfo
.st_uid
);
3202 funame
= userinfo
.pw_name
;
3211 groupinfo
= grp
.getgrgid(fstatinfo
.st_gid
);
3212 fgname
= groupinfo
.gr_name
;
3217 fdev_minor
= fdev_minor
;
3218 fdev_major
= fdev_major
;
3219 frdev_minor
= frdev_minor
;
3220 frdev_major
= frdev_major
;
3222 flinkcount
= flinkcount
;
3223 if(hasattr(fstatinfo
, "st_file_attributes")):
3224 fwinattributes
= fstatinfo
.st_file_attributes
;
3227 fcontents
= "".encode('UTF-8');
3229 if(ftype
== 0 or ftype
== 7):
3230 with
open(fname
, "rb") as fpc
:
3232 chunk
= fpc
.read(chunk_size
);
3236 if(followlink
and (ftype
== 1 or ftype
== 2)):
3237 flstatinfo
= os
.stat(flinkname
);
3238 with
open(flinkname
, "rb") as fpc
:
3240 chunk
= fpc
.read(chunk_size
);
3244 ftypehex
= format(ftype
, 'x').lower();
3245 extrafields
= len(extradata
);
3246 extrafieldslist
= extradata
;
3247 catfextrafields
= extrafields
;
3248 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
3249 if(len(extradata
)>0):
3250 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
3251 extrasizelen
= len(extrasizestr
);
3252 extrasizelenhex
= format(extrasizelen
, 'x').lower();
3253 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]);
3254 if(len(extradata
)>0):
3255 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[4]);
3256 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[4]);
3257 catfnumfields
= 24 + catfextrafields
;
3258 if(checksumtype
=="none" or checksumtype
==""):
3259 catfileheadercshex
= format(0, 'x').lower();
3260 catfilecontentcshex
= format(0, 'x').lower();
3261 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3262 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3263 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
3264 elif(checksumtype
=="crc16_ccitt"):
3265 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3266 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
3267 elif(checksumtype
=="adler32"):
3268 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3269 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
3270 elif(checksumtype
=="crc32"):
3271 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3272 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
3273 elif(checksumtype
=="crc64_ecma"):
3274 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3275 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
3276 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3277 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3278 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
3279 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3280 checksumoutstr
= hashlib
.new(checksumtype
);
3281 checksumoutstr
.update("".encode('UTF-8'));
3282 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3283 checksumoutstr
= hashlib
.new(checksumtype
);
3284 checksumoutstr
.update(fcontents
);
3285 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
3287 catfileheadercshex
= format(0, 'x').lower();
3288 catfilecontentcshex
= format(0, 'x').lower();
3289 catfhstart
= fheadtell
;
3290 fheadtell
+= len(catfileoutstr
);
3291 catfhend
= fheadtell
- 1;
3292 catfcontentstart
= fheadtell
;
3293 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
3294 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
3295 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[4]) + catfileoutstr
;
3296 if(checksumtype
=="none" or checksumtype
==""):
3297 catfileheadercshex
= format(0, 'x').lower();
3298 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3299 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3300 elif(checksumtype
=="crc16_ccitt"):
3301 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3302 elif(checksumtype
=="adler32"):
3303 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3304 elif(checksumtype
=="crc32"):
3305 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3306 elif(checksumtype
=="crc64_ecma"):
3307 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3308 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3309 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3310 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3311 checksumoutstr
= hashlib
.new(checksumtype
);
3312 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
3313 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3315 catfileheadercshex
= format(0, 'x').lower();
3316 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
3317 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
3318 nullstrecd
= formatspecs
[4].encode('UTF-8');
3319 fheadtell
+= len(catfileoutstr
) + 1;
3320 catfcontentend
= fheadtell
- 1;
3321 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
3322 pyhascontents
= False;
3323 if(int(fsize
)>0 and not listonly
):
3324 pyhascontents
= True;
3325 if(int(fsize
)>0 and listonly
):
3327 pyhascontents
= False;
3328 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
} });
3329 fileidnum
= fileidnum
+ 1;
3332 def TarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
3338 inodetocatinode
= {};
3340 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
3343 if(not tarfile
.is_tarfile(infiles
)):
3345 except AttributeError:
3346 if(not is_tarfile(infiles
)):
3349 tarfp
= tarfile
.open(infiles
, "r");
3350 except FileNotFoundError
:
3352 fnumfiles
= int(len(tarfp
.getmembers()));
3353 catver
= formatspecs
[5];
3354 fileheaderver
= str(int(catver
.replace(".", "")));
3355 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
3356 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3357 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[4]);
3358 catversion
= fileheaderver
;
3359 if(checksumtype
=="none" or checksumtype
==""):
3360 catfileheadercshex
= format(0, 'x').lower();
3361 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3362 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3363 elif(checksumtype
=="crc16_ccitt"):
3364 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3365 elif(checksumtype
=="adler32"):
3366 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3367 elif(checksumtype
=="crc32"):
3368 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3369 elif(checksumtype
=="crc64_ecma"):
3370 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3371 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3372 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3373 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3374 checksumoutstr
= hashlib
.new(checksumtype
);
3375 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3376 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3378 catfileheadercshex
= format(0, 'x').lower();
3379 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
3380 fheadtell
= len(fileheader
);
3381 catlist
= {'fnumfiles': fnumfiles
, 'fformat': formatspecs
[0], 'fversion': catversion
, 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
3382 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
3383 if(re
.findall("^[.|/]", member
.name
)):
3384 fname
= member
.name
;
3386 fname
= "./"+member
.name
;
3388 VerbosePrintOut(fname
);
3389 fpremode
= member
.mode
;
3390 ffullmode
= member
.mode
;
3394 ffullmode
= member
.mode
+ stat
.S_IFREG
;
3396 elif(member
.isdev()):
3397 ffullmode
= member
.mode
;
3399 elif(member
.islnk()):
3400 ffullmode
= member
.mode
+ stat
.S_IFREG
;
3402 elif(member
.issym()):
3403 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
3405 elif(member
.ischr()):
3406 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
3408 elif(member
.isblk()):
3409 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
3411 elif(member
.isdir()):
3412 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
3414 elif(member
.isfifo()):
3415 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
3417 elif(member
.issparse()):
3418 ffullmode
= member
.mode
;
3421 ffullmode
= member
.mode
;
3424 fbasedir
= os
.path
.dirname(fname
);
3428 curfid
= curfid
+ 1;
3430 flinkname
= member
.linkname
;
3431 fdev_minor
= member
.devminor
;
3432 fdev_major
= member
.devmajor
;
3433 frdev_minor
= member
.devminor
;
3434 frdev_major
= member
.devmajor
;
3435 if(ftype
==1 or ftype
==2 or ftype
==3 or ftype
==4 or ftype
==5 or ftype
==6):
3437 elif(ftype
==0 or ftype
==7):
3438 fsize
= member
.size
;
3440 fsize
= member
.size
;
3441 fatime
= member
.mtime
;
3442 fmtime
= member
.mtime
;
3443 fctime
= member
.mtime
;
3444 fbtime
= member
.mtime
;
3446 fchmode
= stat
.S_IMODE(ffullmode
);
3447 ftypemod
= stat
.S_IFMT(ffullmode
);
3450 funame
= member
.uname
;
3451 fgname
= member
.gname
;
3452 flinkcount
= flinkcount
;
3453 fcontents
= "".encode('UTF-8');
3455 if(ftype
== 0 or ftype
== 7):
3456 with tarfp
.extractfile(member
) as fpc
:
3458 chunk
= fpc
.read(chunk_size
);
3462 ftypehex
= format(ftype
, 'x').lower();
3463 extrafields
= len(extradata
);
3464 extrafieldslist
= extradata
;
3465 catfextrafields
= extrafields
;
3466 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
3467 if(len(extradata
)>0):
3468 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
3469 extrasizelen
= len(extrasizestr
);
3470 extrasizelenhex
= format(extrasizelen
, 'x').lower();
3471 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]);
3472 if(len(extradata
)>0):
3473 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[4]);
3474 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[4]);
3475 catfnumfields
= 24 + catfextrafields
;
3476 if(checksumtype
=="none" or checksumtype
==""):
3477 catfileheadercshex
= format(0, 'x').lower();
3478 catfilecontentcshex
= format(0, 'x').lower();
3479 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3480 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3481 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
3482 elif(checksumtype
=="crc16_ccitt"):
3483 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3484 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
3485 elif(checksumtype
=="adler32"):
3486 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3487 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
3488 elif(checksumtype
=="crc32"):
3489 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3490 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
3491 elif(checksumtype
=="crc64_ecma"):
3492 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3493 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
3494 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3495 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3496 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
3497 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3498 checksumoutstr
= hashlib
.new(checksumtype
);
3499 checksumoutstr
.update("".encode('UTF-8'));
3500 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3501 checksumoutstr
= hashlib
.new(checksumtype
);
3502 checksumoutstr
.update(fcontents
);
3503 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
3505 catfileheadercshex
= format(0, 'x').lower();
3506 catfilecontentcshex
= format(0, 'x').lower();
3507 catfhstart
= fheadtell
;
3508 fheadtell
+= len(catfileoutstr
);
3509 catfhend
= fheadtell
- 1;
3510 catfcontentstart
= fheadtell
;
3511 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
3512 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
3513 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[4]) + catfileoutstr
;
3514 if(checksumtype
=="none" or checksumtype
==""):
3515 catfileheadercshex
= format(0, 'x').lower();
3516 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3517 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3518 elif(checksumtype
=="crc16_ccitt"):
3519 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3520 elif(checksumtype
=="adler32"):
3521 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3522 elif(checksumtype
=="crc32"):
3523 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3524 elif(checksumtype
=="crc64_ecma"):
3525 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3526 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3527 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3528 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3529 checksumoutstr
= hashlib
.new(checksumtype
);
3530 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
3531 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3533 catfileheadercshex
= format(0, 'x').lower();
3534 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
3535 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
3536 nullstrecd
= formatspecs
[4].encode('UTF-8');
3537 fheadtell
+= len(catfileoutstr
) + 1;
3538 catfcontentend
= fheadtell
- 1;
3539 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
3540 pyhascontents
= False;
3541 if(int(fsize
)>0 and not listonly
):
3542 pyhascontents
= True;
3543 if(int(fsize
)>0 and listonly
):
3545 pyhascontents
= False;
3546 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
} });
3547 fileidnum
= fileidnum
+ 1;
3550 def ZipFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
3551 advancedlist
= True;
3557 inodetocatinode
= {};
3559 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
3561 if(not zipfile
.is_zipfile(infiles
)):
3563 zipfp
= zipfile
.ZipFile(infiles
, "r", allowZip64
=True);
3564 ziptest
= zipfp
.testzip();
3566 VerbosePrintOut("Bad file found!");
3567 fnumfiles
= int(len(zipfp
.infolist()));
3568 catver
= formatspecs
[5];
3569 fileheaderver
= str(int(catver
.replace(".", "")));
3570 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
3571 catversion
= fileheaderver
;
3572 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3573 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[4]);
3574 if(checksumtype
=="none" or checksumtype
==""):
3575 catfileheadercshex
= format(0, 'x').lower();
3576 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3577 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3578 elif(checksumtype
=="crc16_ccitt"):
3579 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3580 elif(checksumtype
=="adler32"):
3581 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3582 elif(checksumtype
=="crc32"):
3583 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3584 elif(checksumtype
=="crc64_ecma"):
3585 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3586 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3587 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3588 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3589 checksumoutstr
= hashlib
.new(checksumtype
);
3590 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3591 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3593 catfileheadercshex
= format(0, 'x').lower();
3594 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
3595 fheadtell
= len(fileheader
);
3596 catlist
= {'fnumfiles': fnumfiles
, 'fformat': formatspecs
[0], 'fversion': catversion
, 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
3597 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
3598 if(re
.findall("^[.|/]", member
.filename
)):
3599 fname
= member
.filename
;
3601 fname
= "./"+member
.filename
;
3602 zipinfo
= zipfp
.getinfo(member
.filename
);
3604 VerbosePrintOut(fname
);
3605 if(not member
.is_dir()):
3606 fpremode
= stat
.S_IFREG
+ 438;
3607 elif(member
.is_dir()):
3608 fpremode
= stat
.S_IFDIR
+ 511;
3611 if(not member
.is_dir()):
3613 elif(member
.is_dir()):
3616 fbasedir
= os
.path
.dirname(fname
);
3620 curfid
= curfid
+ 1;
3628 fsize
= member
.file_size
;
3630 fsize
= member
.file_size
;
3631 fatime
= time
.mktime(member
.date_time
+ (0, 0, -1));
3632 fmtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
3633 fctime
= time
.mktime(member
.date_time
+ (0, 0, -1));
3634 fbtime
= time
.mktime(member
.date_time
+ (0, 0, -1));
3635 if(not member
.is_dir()):
3636 fmode
= stat
.S_IFREG
+ 438;
3637 fchmode
= stat
.S_IMODE(int(stat
.S_IFREG
+ 438));
3638 ftypemod
= stat
.S_IFMT(int(stat
.S_IFREG
+ 438));
3639 if(member
.is_dir()):
3640 fmode
= stat
.S_IFDIR
+ 511;
3641 fchmode
= stat
.S_IMODE(int(stat
.S_IFDIR
+ 511));
3642 ftypemod
= stat
.S_IFMT(int(stat
.S_IFDIR
+ 511));
3645 except AttributeError:
3651 except AttributeError:
3658 userinfo
= pwd
.getpwuid(os
.getuid());
3659 funame
= userinfo
.pw_name
;
3662 except AttributeError:
3670 groupinfo
= grp
.getgrgid(os
.getgid());
3671 fgname
= groupinfo
.gr_name
;
3674 except AttributeError:
3678 fcontents
= "".encode('UTF-8');
3680 fcontents
= zipfp
.read(member
.filename
);
3681 ftypehex
= format(ftype
, 'x').lower();
3682 extrafields
= len(extradata
);
3683 extrafieldslist
= extradata
;
3684 catfextrafields
= extrafields
;
3685 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
3686 if(len(extradata
)>0):
3687 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
3688 extrasizelen
= len(extrasizestr
);
3689 extrasizelenhex
= format(extrasizelen
, 'x').lower();
3690 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]);
3691 if(len(extradata
)>0):
3692 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[4]);
3693 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[4]);
3694 catfnumfields
= 24 + catfextrafields
;
3695 if(checksumtype
=="none" or checksumtype
==""):
3696 catfileheadercshex
= format(0, 'x').lower();
3697 catfilecontentcshex
= format(0, 'x').lower();
3698 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3699 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3700 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
3701 elif(checksumtype
=="crc16_ccitt"):
3702 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3703 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
3704 elif(checksumtype
=="adler32"):
3705 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3706 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
3707 elif(checksumtype
=="crc32"):
3708 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3709 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
3710 elif(checksumtype
=="crc64_ecma"):
3711 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3712 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
3713 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3714 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3715 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
3716 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3717 checksumoutstr
= hashlib
.new(checksumtype
);
3718 checksumoutstr
.update("".encode('UTF-8'));
3719 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3720 checksumoutstr
= hashlib
.new(checksumtype
);
3721 checksumoutstr
.update(fcontents
);
3722 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
3724 catfileheadercshex
= format(0, 'x').lower();
3725 catfilecontentcshex
= format(0, 'x').lower();
3726 catfhstart
= fheadtell
;
3727 fheadtell
+= len(catfileoutstr
);
3728 catfhend
= fheadtell
- 1;
3729 catfcontentstart
= fheadtell
;
3730 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
3731 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
3732 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[4]) + catfileoutstr
;
3733 if(checksumtype
=="none" or checksumtype
==""):
3734 catfileheadercshex
= format(0, 'x').lower();
3735 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3736 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3737 elif(checksumtype
=="crc16_ccitt"):
3738 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3739 elif(checksumtype
=="adler32"):
3740 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3741 elif(checksumtype
=="crc32"):
3742 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3743 elif(checksumtype
=="crc64_ecma"):
3744 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3745 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3746 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3747 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3748 checksumoutstr
= hashlib
.new(checksumtype
);
3749 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
3750 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3752 catfileheadercshex
= format(0, 'x').lower();
3753 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
3754 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
3755 nullstrecd
= formatspecs
[4].encode('UTF-8');
3756 fheadtell
+= len(catfileoutstr
) + 1;
3757 catfcontentend
= fheadtell
- 1;
3758 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
3759 pyhascontents
= False;
3760 if(int(fsize
)>0 and not listonly
):
3761 pyhascontents
= True;
3762 if(int(fsize
)>0 and listonly
):
3764 pyhascontents
= False;
3765 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
} });
3766 fileidnum
= fileidnum
+ 1;
3769 if(not rarfile_support
):
3770 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
3773 if(rarfile_support
):
3774 def RarFileToArrayAlt(infiles
, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
3775 advancedlist
= True;
3781 inodetocatinode
= {};
3783 if(not os
.path
.exists(infiles
) or not os
.path
.isfile(infiles
)):
3785 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
3787 rarfp
= rarfile
.RarFile(infile
, "r");
3788 rartest
= rarfp
.testrar();
3790 VerbosePrintOut("Bad file found!");
3791 fnumfiles
= int(len(rarfp
.infolist()));
3792 catver
= formatspecs
[5];
3793 fileheaderver
= str(int(catver
.replace(".", "")));
3794 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
3795 catversion
= fileheaderver
;
3796 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
3797 fileheader
= fileheader
+ AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[4]);
3798 if(checksumtype
=="none" or checksumtype
==""):
3799 catfileheadercshex
= format(0, 'x').lower();
3800 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3801 catfileheadercshex
= format(crc16(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3802 elif(checksumtype
=="crc16_ccitt"):
3803 catfileheadercshex
= format(crc16_ccitt(fileheader
.encode('UTF-8')) & 0xffff, '04x').lower();
3804 elif(checksumtype
=="adler32"):
3805 catfileheadercshex
= format(zlib
.adler32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3806 elif(checksumtype
=="crc32"):
3807 catfileheadercshex
= format(crc32(fileheader
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3808 elif(checksumtype
=="crc64_ecma"):
3809 catfileheadercshex
= format(crc64_ecma(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3810 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3811 catfileheadercshex
= format(crc64_iso(fileheader
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3812 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3813 checksumoutstr
= hashlib
.new(checksumtype
);
3814 checksumoutstr
.update(fileheader
.encode('UTF-8'));
3815 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3817 catfileheadercshex
= format(0, 'x').lower();
3818 fileheader
= fileheader
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
3819 fheadtell
= len(fileheader
);
3820 catlist
= {'fnumfiles': fnumfiles
, 'fformat': formatspecs
[0], 'fversion': catversion
, 'fformatspecs': formatspecs
, 'fchecksumtype': checksumtype
, 'fheaderchecksum': catfileheadercshex
, 'ffilelist': {}};
3821 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
3824 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
3827 member
.external_attr
3829 except AttributeError:
3831 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
3834 member
.external_attr
3836 except AttributeError:
3841 if(re
.findall("^[.|/]", member
.filename
)):
3842 fname
= member
.filename
;
3844 fname
= "./"+member
.filename
;
3845 rarinfo
= rarfp
.getinfo(member
.filename
);
3847 VerbosePrintOut(fname
);
3848 if(is_unix
and member
.external_attr
!=0):
3849 fpremode
= int(member
.external_attr
);
3850 elif(member
.is_file()):
3851 fpremode
= stat
.S_IFREG
+ 438;
3852 elif(member
.is_symlink()):
3853 fpremode
= stat
.S_IFLNK
+ 438;
3854 elif(member
.is_dir()):
3855 fpremode
= stat
.S_IFDIR
+ 511;
3856 if(is_windows
and member
.external_attr
!=0):
3857 fwinattributes
= int(member
.external_attr
);
3859 fwinattributes
= int(0);
3862 if(member
.is_file()):
3864 elif(member
.is_symlink()):
3866 elif(member
.is_dir()):
3870 flinkname
= rarfp
.read(member
.filename
).decode("UTF-8");
3871 fbasedir
= os
.path
.dirname(fname
);
3875 curfid
= curfid
+ 1;
3883 fsize
= member
.file_size
;
3886 fatime
= int(member
.atime
.timestamp());
3888 fatime
= int(member
.mtime
.timestamp());
3889 except AttributeError:
3890 fatime
= int(member
.mtime
.timestamp());
3891 fmtime
= int(member
.mtime
.timestamp());
3894 fctime
= int(member
.ctime
.timestamp());
3896 fctime
= int(member
.mtime
.timestamp());
3897 except AttributeError:
3898 fctime
= int(member
.mtime
.timestamp());
3899 fbtime
= int(member
.mtime
.timestamp());
3900 if(is_unix
and member
.external_attr
!=0):
3901 fmode
= format(int(member
.external_attr
), 'x').lower();
3902 fchmode
= format(int(stat
.S_IMODE(member
.external_attr
)), 'x').lower();
3903 ftypemod
= format(int(stat
.S_IFMT(member
.external_attr
)), 'x').lower();
3904 elif(member
.is_file()):
3905 fmode
= int(stat
.S_IFREG
+ 438)
3906 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
3907 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
3908 elif(member
.is_symlink()):
3909 fmode
= int(stat
.S_IFLNK
+ 438)
3910 fchmode
= int(stat
.S_IMODE(stat
.S_IFREG
+ 438));
3911 ftypemod
= int(stat
.S_IFMT(stat
.S_IFREG
+ 438));
3912 elif(member
.is_dir()):
3913 fmode
= int(stat
.S_IFDIR
+ 511)
3914 fchmode
= int(stat
.S_IMODE(stat
.S_IFDIR
+ 511));
3915 ftypemod
= int(stat
.S_IFMT(stat
.S_IFDIR
+ 511));
3918 except AttributeError:
3924 except AttributeError:
3931 userinfo
= pwd
.getpwuid(os
.getuid());
3932 funame
= userinfo
.pw_name
;
3935 except AttributeError:
3943 groupinfo
= grp
.getgrgid(os
.getgid());
3944 fgname
= groupinfo
.gr_name
;
3947 except AttributeError:
3951 fcontents
= "".encode('UTF-8');
3953 fcontents
= rarfp
.read(member
.filename
);
3954 ftypehex
= format(ftype
, 'x').lower();
3955 extrafields
= len(extradata
);
3956 extrafieldslist
= extradata
;
3957 catfextrafields
= extrafields
;
3958 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
3959 if(len(extradata
)>0):
3960 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
3961 extrasizelen
= len(extrasizestr
);
3962 extrasizelenhex
= format(extrasizelen
, 'x').lower();
3963 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]);
3964 if(len(extradata
)>0):
3965 catfileoutstr
= catfileoutstr
+ AppendNullBytes(extradata
, formatspecs
[4]);
3966 catfileoutstr
= catfileoutstr
+ AppendNullByte(checksumtype
, formatspecs
[4]);
3967 catfnumfields
= 24 + catfextrafields
;
3968 if(checksumtype
=="none" or checksumtype
==""):
3969 catfileheadercshex
= format(0, 'x').lower();
3970 catfilecontentcshex
= format(0, 'x').lower();
3971 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
3972 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3973 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
3974 elif(checksumtype
=="crc16_ccitt"):
3975 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
3976 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
3977 elif(checksumtype
=="adler32"):
3978 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3979 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
3980 elif(checksumtype
=="crc32"):
3981 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
3982 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
3983 elif(checksumtype
=="crc64_ecma"):
3984 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3985 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
3986 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
3987 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3988 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
3989 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
3990 checksumoutstr
= hashlib
.new(checksumtype
);
3991 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
3992 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
3994 catfileheadercshex
= format(0, 'x').lower();
3995 catfilecontentcshex
= format(0, 'x').lower();
3996 catfhstart
= fheadtell
;
3997 fheadtell
+= len(catfileoutstr
);
3998 catfhend
= fheadtell
- 1;
3999 catfcontentstart
= fheadtell
;
4000 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
4001 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
4002 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[4]) + catfileoutstr
;
4003 if(checksumtype
=="none" or checksumtype
==""):
4004 catfileheadercshex
= format(0, 'x').lower();
4005 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4006 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4007 elif(checksumtype
=="crc16_ccitt"):
4008 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4009 elif(checksumtype
=="adler32"):
4010 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4011 elif(checksumtype
=="crc32"):
4012 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4013 elif(checksumtype
=="crc64_ecma"):
4014 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4015 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4016 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4017 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4018 checksumoutstr
= hashlib
.new(checksumtype
);
4019 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
4020 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4022 catfileheadercshex
= format(0, 'x').lower();
4023 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
4024 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
4025 nullstrecd
= formatspecs
[4].encode('UTF-8');
4026 fheadtell
+= len(catfileoutstr
) + 1;
4027 catfcontentend
= fheadtell
- 1;
4028 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
4029 pyhascontents
= False;
4030 if(int(fsize
)>0 and not listonly
):
4031 pyhascontents
= True;
4032 if(int(fsize
)>0 and listonly
):
4034 pyhascontents
= False;
4035 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
} });
4036 fileidnum
= fileidnum
+ 1;
4039 def ListDirToArray(infiles
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
4040 outarray
= BytesIO();
4041 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
4042 listcatfiles
= ArchiveFileToArray(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4043 return listcatfiles
;
4045 def ArchiveFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4046 if(isinstance(infile
, dict)):
4047 listcatfiles
= infile
;
4049 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
4050 infile
= RemoveWindowsPath(infile
);
4051 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4052 if(not listcatfiles
):
4054 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': {}}}};
4056 catarray
.update({'catfp': listcatfiles
['catfp']});
4057 lenlist
= len(listcatfiles
['ffilelist']);
4062 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4065 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4066 lcfx
= listcatfiles
['fnumfiles'];
4068 lcfx
= int(listcatfiles
['fnumfiles']);
4070 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
4071 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
4072 catarray
['filetoid'].update(filetoidarray
);
4073 catarray
['idtofile'].update(idtofilearray
);
4074 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4075 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
4076 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
4077 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4078 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
4079 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
4080 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4081 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4082 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4083 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4084 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4085 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4086 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4087 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
4088 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
4089 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
4090 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4091 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4092 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
4093 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
4094 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
4095 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4096 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4097 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4098 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
4099 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
4100 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
4101 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4102 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4103 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4104 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4108 create_alias_function("", __file_format_name__
, "ToArrayIndex", ArchiveFileToArrayIndex
);
4110 def ListDirToArrayIndexAlt(infiles
, dirlistfromtxt
=False, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4111 listcatfiles
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
4112 if(not listcatfiles
):
4114 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': {}}}};
4115 lenlist
= len(listcatfiles
['ffilelist']);
4120 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4123 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4124 lcfx
= listcatfiles
['fnumfiles'];
4126 lcfx
= int(listcatfiles
['fnumfiles']);
4128 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
4129 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
4130 catarray
['filetoid'].update(filetoidarray
);
4131 catarray
['idtofile'].update(idtofilearray
);
4132 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4133 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
4134 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
4135 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4136 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
4137 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
4138 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4139 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4140 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4141 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4142 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4143 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4144 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4145 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
4146 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
4147 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
4148 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4149 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4150 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
4151 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
4152 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
4153 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4154 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4155 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4156 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
4157 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
4158 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
4159 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4160 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4161 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4162 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4166 def TarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4167 listcatfiles
= TarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
4168 if(not listcatfiles
):
4170 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': {}}}};
4171 lenlist
= len(listcatfiles
['ffilelist']);
4176 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4179 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4180 lcfx
= listcatfiles
['fnumfiles'];
4182 lcfx
= int(listcatfiles
['fnumfiles']);
4184 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
4185 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
4186 catarray
['filetoid'].update(filetoidarray
);
4187 catarray
['idtofile'].update(idtofilearray
);
4188 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4189 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
4190 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
4191 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4192 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
4193 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
4194 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4195 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4196 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4197 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4198 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4199 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4200 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4201 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
4202 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
4203 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
4204 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4205 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4206 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
4207 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
4208 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
4209 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4210 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4211 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4212 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
4213 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
4214 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
4215 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4216 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4217 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4218 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4222 def ZipFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4223 listcatfiles
= ZipFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
4224 if(not listcatfiles
):
4226 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': {}}}};
4227 lenlist
= len(listcatfiles
['ffilelist']);
4232 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4235 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4236 lcfx
= listcatfiles
['fnumfiles'];
4238 lcfx
= int(listcatfiles
['fnumfiles']);
4240 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
4241 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
4242 catarray
['filetoid'].update(filetoidarray
);
4243 catarray
['idtofile'].update(idtofilearray
);
4244 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4245 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
4246 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
4247 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4248 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
4249 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
4250 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4251 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4252 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4253 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4254 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4255 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4256 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4257 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
4258 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
4259 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
4260 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4261 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4262 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
4263 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
4264 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
4265 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4266 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4267 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4268 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
4269 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
4270 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
4271 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4272 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4273 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4274 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4278 if(not rarfile_support
):
4279 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4282 if(rarfile_support
):
4283 def RarFileToArrayIndexAlt(infiles
, seekstart
=0, seekend
=0, listonly
=False, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False):
4284 listcatfiles
= RarFileToArrayAlt(infiles
, listonly
, checksumtype
, extradata
, formatspecs
, verbose
);
4285 if(not listcatfiles
):
4287 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': {}}}};
4288 lenlist
= len(listcatfiles
['ffilelist']);
4293 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4296 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4297 lcfx
= listcatfiles
['fnumfiles'];
4299 lcfx
= int(listcatfiles
['fnumfiles']);
4301 filetoidarray
= {listcatfiles
['ffilelist'][lcfi
]['fname']: listcatfiles
['ffilelist'][lcfi
]['fid']};
4302 idtofilearray
= {listcatfiles
['ffilelist'][lcfi
]['fid']: listcatfiles
['ffilelist'][lcfi
]['fname']};
4303 catarray
['filetoid'].update(filetoidarray
);
4304 catarray
['idtofile'].update(idtofilearray
);
4305 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4306 catarray
['filetypes']['files']['filetoid'].update(filetoidarray
);
4307 catarray
['filetypes']['files']['idtofile'].update(idtofilearray
);
4308 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4309 catarray
['filetypes']['hardlinks']['filetoid'].update(filetoidarray
);
4310 catarray
['filetypes']['hardlinks']['idtofile'].update(idtofilearray
);
4311 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4312 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4313 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4314 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4315 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4316 catarray
['filetypes']['links']['filetoid'].update(filetoidarray
);
4317 catarray
['filetypes']['links']['idtofile'].update(idtofilearray
);
4318 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==3):
4319 catarray
['filetypes']['character']['filetoid'].update(filetoidarray
);
4320 catarray
['filetypes']['character']['idtofile'].update(idtofilearray
);
4321 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4322 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4323 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==4):
4324 catarray
['filetypes']['block']['filetoid'].update(filetoidarray
);
4325 catarray
['filetypes']['block']['idtofile'].update(idtofilearray
);
4326 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4327 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4328 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4329 catarray
['filetypes']['directories']['filetoid'].update(filetoidarray
);
4330 catarray
['filetypes']['directories']['idtofile'].update(idtofilearray
);
4331 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6):
4332 catarray
['filetypes']['symlinks']['filetoid'].update(filetoidarray
);
4333 catarray
['filetypes']['symlinks']['idtofile'].update(idtofilearray
);
4334 catarray
['filetypes']['devices']['filetoid'].update(filetoidarray
);
4335 catarray
['filetypes']['devices']['idtofile'].update(idtofilearray
);
4339 def ArchiveFileStringToArrayIndex(catstr
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4340 catfp
= BytesIO(catstr
);
4341 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4342 return listcatfiles
;
4344 create_alias_function("", __file_format_name__
, "StringToArrayIndex", ArchiveFileStringToArrayIndex
);
4346 def TarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4348 catfp
= PackArchiveFileFromTarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4349 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4350 return listcatfiles
;
4352 def ZipFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4354 catfp
= PackArchiveFileFromZipFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4355 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4356 return listcatfiles
;
4358 if(not rarfile_support
):
4359 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4362 if(rarfile_support
):
4363 def RarFileToArrayIndex(infile
, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, returnfp
=False):
4365 catfp
= PackArchiveFileFromRarFile(infile
, catfp
, "auto", None, "crc32", [], formatspecs
, False, True);
4366 listcatfiles
= ArchiveFileToArrayIndex(catfp
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
);
4367 return listcatfiles
;
4369 def ListDirToArrayIndex(infiles
, dirlistfromtxt
=False, compression
="auto", compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, listonly
=False, skipchecksum
=False, checksumtype
="crc32", formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
4370 outarray
= BytesIO();
4371 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, verbose
, True);
4372 listcatfiles
= ArchiveFileToArrayIndex(outarray
, seekstart
, seekend
, listonly
, skipchecksum
, formatspecs
, returnfp
)
4373 return listcatfiles
;
4375 def RePackArchiveFile(infile
, outfile
, compression
="auto", compressionlevel
=None, followlink
=False, seekstart
=0, seekend
=0, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
4376 compressionlist
= ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
4377 outextlist
= ['gz', 'bz2', 'zst', 'lz4', 'lzop', 'lzo', 'lzma', 'xz'];
4378 outextlistwd
= ['.gz', '.bz2', '.zst', '.lz4', 'lzop', '.lzo', '.lzma', '.xz'];
4379 if(isinstance(infile
, dict)):
4380 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
4381 listcatfiles
= prelistcatfiles
['list'];
4383 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
4384 infile
= RemoveWindowsPath(infile
);
4386 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
4387 listcatfiles
= prelistcatfiles
['list'];
4389 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
4390 if(outfile
!="-" and not hasattr(infile
, "read") and not hasattr(outfile
, "write")):
4391 outfile
= RemoveWindowsPath(outfile
);
4392 checksumtype
= checksumtype
.lower();
4393 if(not CheckSumSupport(checksumtype
, hashlib_guaranteed
)):
4394 checksumtype
="crc32";
4395 if(checksumtype
=="none"):
4397 if(not compression
or compression
or compression
=="catfile" or compression
==formatspecs
[1]):
4399 if(compression
not in compressionlist
and compression
is None):
4400 compression
= "auto";
4402 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
4403 if(outfile
!="-" and not hasattr(outfile
, "read") and not hasattr(outfile
, "write")):
4404 if(os
.path
.exists(outfile
)):
4406 if(not listcatfiles
):
4411 elif(hasattr(outfile
, "read") or hasattr(outfile
, "write")):
4413 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
4415 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
4418 fbasename
= os
.path
.splitext(outfile
)[0];
4419 fextname
= os
.path
.splitext(outfile
)[1];
4420 catfp
= CompressOpenFile(outfile
, compressionlevel
);
4421 catver
= formatspecs
[5];
4422 fileheaderver
= str(int(catver
.replace(".", "")));
4423 fileheader
= AppendNullByte(formatspecs
[0] + fileheaderver
, formatspecs
[4]);
4424 catfp
.write(fileheader
.encode('UTF-8'));
4425 lenlist
= len(listcatfiles
['ffilelist']);
4426 fnumfiles
= int(listcatfiles
['fnumfiles']);
4427 if(lenlist
>fnumfiles
or lenlist
<fnumfiles
):
4428 fnumfiles
= lenlist
;
4429 fnumfileshex
= format(int(fnumfiles
), 'x').lower();
4430 fnumfilesa
= AppendNullBytes([fnumfileshex
, checksumtype
], formatspecs
[4]);
4431 if(checksumtype
=="none" or checksumtype
==""):
4432 catfileheadercshex
= format(0, 'x').lower();
4433 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4434 catfileheadercshex
= format(crc16(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
4435 elif(checksumtype
=="crc16_ccitt"):
4436 catfileheadercshex
= format(crc16_ccitt(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffff, '04x').lower();
4437 elif(checksumtype
=="adler32"):
4438 catfileheadercshex
= format(zlib
.adler32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
4439 elif(checksumtype
=="crc32"):
4440 catfileheadercshex
= format(crc32(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffff, '08x').lower();
4441 elif(checksumtype
=="crc64_ecma"):
4442 catfileheadercshex
= format(crc64_ecma(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4443 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4444 catfileheadercshex
= format(crc64_iso(str(fileheader
+ fnumfilesa
).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4445 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4446 checksumoutstr
= hashlib
.new(checksumtype
);
4447 checksumoutstr
.update(str(fileheader
+ fnumfilesa
).encode('UTF-8'));
4448 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4450 catfileheadercshex
= format(0, 'x').lower();
4451 fnumfilesa
= fnumfilesa
+ AppendNullByte(catfileheadercshex
, formatspecs
[4]);
4452 catfp
.write(fnumfilesa
.encode('UTF-8'));
4455 os
.fsync(catfp
.fileno());
4456 except io
.UnsupportedOperation
:
4458 except AttributeError:
4464 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4467 lcfx
= int(listcatfiles
['fnumfiles']);
4475 catfhstart
= catfp
.tell();
4476 if(re
.findall("^[.|/]", listcatfiles
['ffilelist'][reallcfi
]['fname'])):
4477 fname
= listcatfiles
['ffilelist'][reallcfi
]['fname'];
4479 fname
= "./"+listcatfiles
['ffilelist'][reallcfi
]['fname'];
4481 VerbosePrintOut(fname
);
4482 fheadersize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fheadersize']), 'x').lower();
4483 fsize
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fsize']), 'x').lower();
4484 flinkname
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
4485 fatime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fatime']), 'x').lower();
4486 fmtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmtime']), 'x').lower();
4487 fctime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fctime']), 'x').lower();
4488 fbtime
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fbtime']), 'x').lower();
4489 fmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmode']), 'x').lower();
4490 fchmode
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fchmode']), 'x').lower();
4491 fuid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fuid']), 'x').lower();
4492 funame
= listcatfiles
['ffilelist'][reallcfi
]['funame'];
4493 fgid
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fgid']), 'x').lower();
4494 fgname
= listcatfiles
['ffilelist'][reallcfi
]['fgname'];
4495 finode
= listcatfiles
['ffilelist'][reallcfi
]['finode'];
4496 flinkcount
= listcatfiles
['ffilelist'][reallcfi
]['flinkcount'];
4497 fdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fminor']), 'x').lower();
4498 fdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fmajor']), 'x').lower();
4499 frdev_minor
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frminor']), 'x').lower();
4500 frdev_major
= format(int(listcatfiles
['ffilelist'][reallcfi
]['frmajor']), 'x').lower();
4501 if(len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] and len(listcatfiles
['ffilelist'][reallcfi
]['fextralist'])>0):
4502 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
4503 if(len(extradata
) > 0):
4504 listcatfiles
['ffilelist'][reallcfi
]['fextrafields'] = len(extradata
);
4505 listcatfiles
['ffilelist'][reallcfi
]['fextralist'] = extradata
;
4506 extrafields
= format(int(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']), 'x').lower();
4507 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
4508 if(len(extradata
)>0):
4509 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
4510 extrasizelen
= format(len(extrasizestr
), 'x').lower();
4511 fcontents
= listcatfiles
['ffilelist'][reallcfi
]['fcontents'];
4513 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']==1 or listcatfiles
['ffilelist'][reallcfi
]['ftype']==2):
4514 getflinkpath
= listcatfiles
['ffilelist'][reallcfi
]['flinkname'];
4515 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
4516 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
4517 fheadersize
= format(int(flinkinfo
['fheadersize']), 'x').lower();
4518 fsize
= format(int(flinkinfo
['fsize']), 'x').lower();
4519 flinkname
= flinkinfo
['flinkname'];
4520 fatime
= format(int(flinkinfo
['fatime']), 'x').lower();
4521 fmtime
= format(int(flinkinfo
['fmtime']), 'x').lower();
4522 fctime
= format(int(flinkinfo
['fctime']), 'x').lower();
4523 fbtime
= format(int(flinkinfo
['fbtime']), 'x').lower();
4524 fmode
= format(int(flinkinfo
['fmode']), 'x').lower();
4525 fchmode
= format(int(flinkinfo
['fchmode']), 'x').lower();
4526 fuid
= format(int(flinkinfo
['fuid']), 'x').lower();
4527 funame
= flinkinfo
['funame'];
4528 fgid
= format(int(flinkinfo
['fgid']), 'x').lower();
4529 fgname
= flinkinfo
['fgname'];
4530 finode
= flinkinfo
['finode'];
4531 flinkcount
= flinkinfo
['flinkcount'];
4532 fdev_minor
= format(int(flinkinfo
['fminor']), 'x').lower();
4533 fdev_major
= format(int(flinkinfo
['fmajor']), 'x').lower();
4534 frdev_minor
= format(int(flinkinfo
['frminor']), 'x').lower();
4535 frdev_major
= format(int(flinkinfo
['frmajor']), 'x').lower();
4536 if(len(flinkinfo
['fextralist'])>flinkinfo
['fextrafields'] and len(flinkinfo
['fextralist'])>0):
4537 flinkinfo
['fextrafields'] = len(flinkinfo
['fextralist']);
4538 if(len(extradata
) > 0):
4539 flinkinfo
['fextrafields'] = len(extradata
);
4540 flinkinfo
['fextralist'] = extradata
;
4541 extrafields
= format(int(flinkinfo
['fextrafields']), 'x').lower();
4542 extrasizestr
= AppendNullByte(extrafields
, formatspecs
[4]);
4543 if(len(extradata
)>0):
4544 extrasizestr
= extrasizestr
+ AppendNullBytes(extradata
, formatspecs
[4]);
4545 extrasizelen
= format(len(extrasizestr
), 'x').lower();
4546 fcontents
= flinkinfo
['fcontents'];
4547 if(flinkinfo
['ftype']!=0 and flinkinfo
['ftype']!=7):
4549 fcontents
= fcontents
.encode('UTF-8');
4550 except AttributeError:
4552 ftypehex
= format(flinkinfo
['ftype'], 'x').lower();
4554 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=0 and listcatfiles
['ffilelist'][reallcfi
]['ftype']!=7):
4556 fcontents
= fcontents
.encode('UTF-8');
4557 except AttributeError:
4559 ftypehex
= format(listcatfiles
['ffilelist'][reallcfi
]['ftype'], 'x').lower();
4560 fcurfid
= format(curfid
, 'x').lower();
4561 if(not followlink
and finode
!=0):
4562 if(listcatfiles
['ffilelist'][reallcfi
]['ftype']!=1):
4563 fcurinode
= format(int(curinode
), 'x').lower();
4564 inodetofile
.update({curinode
: fname
});
4565 filetoinode
.update({fname
: curinode
});
4566 curinode
= curinode
+ 1;
4568 fcurinode
= format(int(filetoinode
[flinkname
]), 'x').lower();
4570 fcurinode
= format(int(curinode
), 'x').lower();
4571 curinode
= curinode
+ 1;
4572 curfid
= curfid
+ 1;
4573 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]);
4574 if(listcatfiles
['ffilelist'][reallcfi
]['fextrafields']>0):
4575 extrafieldslist
= [];
4577 exil
= listcatfiles
['ffilelist'][reallcfi
]['fextrafields'];
4579 extrafieldslist
.append(listcatfiles
['ffilelist'][reallcfi
]['fextralist']);
4581 catfileoutstr
+= AppendNullBytes([extrafieldslist
], formatspecs
[4]);
4582 catfileoutstr
+= AppendNullBytes([checksumtype
], formatspecs
[4]);
4583 catfhend
= (catfp
.tell() - 1) + len(catfileoutstr
);
4584 catfcontentstart
= catfp
.tell() + len(catfileoutstr
);
4585 if(checksumtype
=="none" or checksumtype
==""):
4586 catfileheadercshex
= format(0, 'x').lower();
4587 catfilecontentcshex
= format(0, 'x').lower();
4588 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4589 catfileheadercshex
= format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
4590 catfilecontentcshex
= format(crc16(fcontents
) & 0xffff, '04x').lower();
4591 elif(checksumtype
=="crc16_ccitt"):
4592 catfileheadercshex
= format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
4593 catfilecontentcshex
= format(crc16_ccitt(fcontents
) & 0xffff, '04x').lower();
4594 elif(checksumtype
=="adler32"):
4595 catfileheadercshex
= format(zlib
.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4596 catfilecontentcshex
= format(zlib
.adler32(fcontents
) & 0xffffffff, '08x').lower();
4597 elif(checksumtype
=="crc32"):
4598 catfileheadercshex
= format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4599 catfilecontentcshex
= format(crc32(fcontents
) & 0xffffffff, '08x').lower();
4600 elif(checksumtype
=="crc64_ecma"):
4601 catfileheadercshex
= format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4602 catfilecontentcshex
= format(crc64_ecma(fcontents
) & 0xffffffffffffffff, '016x').lower();
4603 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4604 catfileheadercshex
= format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4605 catfilecontentcshex
= format(crc64_iso(fcontents
) & 0xffffffffffffffff, '016x').lower();
4606 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4607 checksumoutstr
= hashlib
.new(checksumtype
);
4608 checksumoutstr
.update("".encode('UTF-8'));
4609 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4610 checksumoutstr
= hashlib
.new(checksumtype
);
4611 checksumoutstr
.update(fcontents
);
4612 catfilecontentcshex
= checksumoutstr
.hexdigest().lower();
4614 catfileheadercshex
= format(0, 'x').lower();
4615 catfilecontentcshex
= format(0, 'x').lower();
4616 tmpfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
4617 catheaersize
= format(int(len(tmpfileoutstr
) - 1), 'x').lower()
4618 catfileoutstr
= AppendNullByte(catheaersize
, formatspecs
[4]) + catfileoutstr
;
4619 if(checksumtype
=="none" or checksumtype
==""):
4620 catfileheadercshex
= format(0, 'x').lower();
4621 elif(checksumtype
=="crc16" or checksumtype
=="crc16_ansi" or checksumtype
=="crc16_ibm"):
4622 catfileheadercshex
= format(crc16(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4623 elif(checksumtype
=="crc16_ccitt"):
4624 catfileheadercshex
= format(crc16_ccitt(catfileoutstr
.encode('UTF-8')) & 0xffff, '04x').lower();
4625 elif(checksumtype
=="adler32"):
4626 catfileheadercshex
= format(zlib
.adler32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4627 elif(checksumtype
=="crc32"):
4628 catfileheadercshex
= format(crc32(catfileoutstr
.encode('UTF-8')) & 0xffffffff, '08x').lower();
4629 elif(checksumtype
=="crc64_ecma"):
4630 catfileheadercshex
= format(crc64_ecma(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4631 elif(checksumtype
=="crc64" or checksumtype
=="crc64_iso"):
4632 catfileheadercshex
= format(crc64_iso(catfileoutstr
.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4633 elif(CheckSumSupportAlt(checksumtype
, hashlib_guaranteed
)):
4634 checksumoutstr
= hashlib
.new(checksumtype
);
4635 checksumoutstr
.update(catfileoutstr
.encode('UTF-8'));
4636 catfileheadercshex
= checksumoutstr
.hexdigest().lower();
4638 catfileheadercshex
= format(0, 'x').lower();
4639 catfileoutstr
= catfileoutstr
+ AppendNullBytes([catfileheadercshex
, catfilecontentcshex
], formatspecs
[4]);
4640 catfileoutstrecd
= catfileoutstr
.encode('UTF-8');
4641 nullstrecd
= formatspecs
[4].encode('UTF-8');
4642 catfileout
= catfileoutstrecd
+ fcontents
+ nullstrecd
;
4643 catfcontentend
= (catfp
.tell() - 1) + len(catfileout
);
4644 catfp
.write(catfileout
);
4647 os
.fsync(catfp
.fileno());
4648 except io
.UnsupportedOperation
:
4650 except AttributeError:
4653 reallcfi
= reallcfi
+ 1;
4654 if(outfile
=="-" or hasattr(outfile
, "read") or hasattr(outfile
, "write")):
4655 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
4658 os
.fsync(catfp
.fileno());
4659 except io
.UnsupportedOperation
:
4661 except AttributeError:
4665 if(hasattr(sys
.stdout
, "buffer")):
4666 shutil
.copyfileobj(catfp
, sys
.stdout
.buffer);
4668 shutil
.copyfileobj(catfp
, sys
.stdout
);
4669 elif(re
.findall(r
"^(ftp|ftps)\:\/\/", outfile
)):
4670 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
4672 upload_file_to_ftp_file(catfp
, outfile
);
4673 elif(re
.findall(r
"^(sftp)\:\/\/", outfile
) and haveparamiko
):
4674 catfp
= CompressArchiveFile(catfp
, compression
, formatspecs
);
4676 upload_file_to_sftp_file(catfp
, outfile
);
4684 create_alias_function("RePack", __file_format_name__
, "", RePackArchiveFile
);
4686 def RePackArchiveFileFromString(catstr
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", skipchecksum
=False, extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
4687 catfp
= BytesIO(catstr
);
4688 listcatfiles
= RePackArchiveFile(catfp
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
4689 return listcatfiles
;
4691 create_alias_function("RePack", __file_format_name__
, "FromString", RePackArchiveFileFromString
);
4693 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):
4694 outarray
= BytesIO();
4695 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, extradata
, formatspecs
, verbose
, True);
4696 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
4697 return listcatfiles
;
4699 create_alias_function("Pack", __file_format_name__
, "FromListDir", PackArchiveFileFromListDir
);
4701 def ArchiveFileArrayBase64Encode(infile
, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
4703 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
4704 if(isinstance(infile
, dict)):
4705 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
4706 listcatfiles
= prelistcatfiles
['list'];
4708 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
4709 infile
= RemoveWindowsPath(infile
);
4711 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
4712 listcatfiles
= prelistcatfiles
['list'];
4714 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
4715 if(not listcatfiles
):
4717 lenlist
= len(listcatfiles
['ffilelist']);
4722 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4725 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4726 lcfx
= listcatfiles
['fnumfiles'];
4728 lcfx
= int(listcatfiles
['fnumfiles']);
4729 if(lenlist
>lcfx
or lenlist
<lcfx
):
4732 if(listcatfiles
['ffilelist'][lcfi
]['fhascontents']):
4733 listcatfiles
['ffilelist'][lcfi
]['fcontents'] = base64
.b64encode(listcatfiles
['ffilelist'][lcfi
]['fcontents']).decode("UTF-8");
4735 return listcatfiles
;
4737 create_alias_function("", __file_format_name__
, "ArrayBase64Encode", ArchiveFileArrayBase64Encode
);
4739 def ArchiveFileArrayBase64Decode(infile
, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
4741 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
4742 if(isinstance(infile
, dict)):
4743 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
4744 listcatfiles
= prelistcatfiles
['list'];
4746 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
4747 infile
= RemoveWindowsPath(infile
);
4749 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
4750 listcatfiles
= prelistcatfiles
['list'];
4752 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
4753 if(not listcatfiles
):
4755 lenlist
= len(listcatfiles
['ffilelist']);
4760 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4763 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4764 lcfx
= listcatfiles
['fnumfiles'];
4766 lcfx
= int(listcatfiles
['fnumfiles']);
4767 if(lenlist
>lcfx
or lenlist
<lcfx
):
4770 if(listcatfiles
['ffilelist'][lcfi
]['fhascontents']):
4771 listcatfiles
['ffilelist'][lcfi
]['fcontents'] = base64
.b64decode(listcatfiles
['ffilelist'][lcfi
]['fcontents'].encode("UTF-8"));
4773 return listcatfiles
;
4775 create_alias_function("", __file_format_name__
, "ArrayBase64Decode", ArchiveFileArrayBase64Decode
);
4777 def UnPackArchiveFile(infile
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
4778 if(outdir
is not None):
4779 outdir
= RemoveWindowsPath(outdir
);
4781 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
4782 if(isinstance(infile
, dict)):
4783 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
4784 listcatfiles
= prelistcatfiles
['list'];
4786 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
4787 infile
= RemoveWindowsPath(infile
);
4789 prelistcatfiles
= ArchiveFileToArrayIndex(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
4790 listcatfiles
= prelistcatfiles
['list'];
4792 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, False, skipchecksum
, formatspecs
, returnfp
);
4793 if(not listcatfiles
):
4795 lenlist
= len(listcatfiles
['ffilelist']);
4800 if(seekend
>0 and seekend
<listcatfiles
['fnumfiles']):
4803 if(lenlist
>listcatfiles
['fnumfiles'] or lenlist
<listcatfiles
['fnumfiles']):
4804 lcfx
= listcatfiles
['fnumfiles'];
4806 lcfx
= int(listcatfiles
['fnumfiles']);
4807 if(lenlist
>lcfx
or lenlist
<lcfx
):
4814 userinfo
= pwd
.getpwuid(listcatfiles
['ffilelist'][lcfi
]['fuid']);
4815 funame
= userinfo
.pw_name
;
4824 groupinfo
= grp
.getgrgid(listcatfiles
['ffilelist'][lcfi
]['fgid']);
4825 fgname
= groupinfo
.gr_name
;
4831 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
4832 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==0 or listcatfiles
['ffilelist'][lcfi
]['ftype']==7):
4833 with
open(listcatfiles
['ffilelist'][lcfi
]['fname'], "wb") as fpc
:
4834 fpc
.write(listcatfiles
['ffilelist'][lcfi
]['fcontents'])
4837 os
.fsync(fpc
.fileno())
4838 except io
.UnsupportedOperation
:
4840 except AttributeError:
4842 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname']):
4843 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
4844 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], int(listcatfiles
['ffilelist'][lcfi
]['fchmode'], 8));
4845 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
4846 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
4848 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
4849 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
4850 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
4855 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
4856 funame
= userinfo
.pw_name
;
4865 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
4866 fgname
= groupinfo
.gr_name
;
4871 if(flinkinfo
['ftype'] == 0 or flinkinfo
['ftype'] == 7):
4872 with
open(listcatfiles
['ffilelist'][lcfi
]['fname'], "wb") as fpc
:
4873 fpc
.write(flinkinfo
['fcontents'])
4876 os
.fsync(fpc
.fileno())
4877 except io
.UnsupportedOperation
:
4879 except AttributeError:
4881 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname']):
4882 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
4883 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], int(flinkinfo
['fchmode'], 8));
4884 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
4885 if(flinkinfo
['ftype']==1):
4886 os
.link(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
4887 if(flinkinfo
['ftype']==2):
4888 os
.symlink(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
4889 if(flinkinfo
['ftype']==5):
4890 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname'], int(flinkinfo
['fchmode'], 8));
4891 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname']):
4892 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
4893 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], int(flinkinfo
['fchmode'], 8));
4894 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
4895 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
4896 os
.mkfifo(listcatfiles
['ffilelist'][lcfi
]['fname'], int(flinkinfo
['fchmode'], 8));
4898 os
.link(listcatfiles
['ffilelist'][lcfi
]['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
4899 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
4901 getflinkpath
= listcatfiles
['ffilelist'][lcfi
]['flinkname'];
4902 flinkid
= prelistcatfiles
['filetoid'][getflinkpath
];
4903 flinkinfo
= listcatfiles
['ffilelist'][flinkid
];
4908 userinfo
= pwd
.getpwuid(flinkinfo
['fuid']);
4909 funame
= userinfo
.pw_name
;
4918 groupinfo
= grp
.getgrgid(flinkinfo
['fgid']);
4919 fgname
= groupinfo
.gr_name
;
4924 if(flinkinfo
['ftype']==0 or flinkinfo
['ftype']==7):
4925 with
open(listcatfiles
['ffilelist'][lcfi
]['fname'], "wb") as fpc
:
4926 fpc
.write(flinkinfo
['fcontents'])
4929 os
.fsync(fpc
.fileno())
4930 except io
.UnsupportedOperation
:
4932 except AttributeError:
4934 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname']):
4935 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
4936 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], int(flinkinfo
['fchmode'], 8));
4937 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
4938 if(flinkinfo
['ftype']==1):
4939 os
.link(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
4940 if(flinkinfo
['ftype']==2):
4941 os
.symlink(flinkinfo
['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
4942 if(flinkinfo
['ftype']==5):
4943 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname'], int(flinkinfo
['fchmode'], 8));
4944 if(hasattr(os
, "chown") and funame
==flinkinfo
['funame'] and fgname
==flinkinfo
['fgname']):
4945 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], flinkinfo
['fuid'], flinkinfo
['fgid']);
4946 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], int(flinkinfo
['fchmode'], 8));
4947 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (flinkinfo
['fatime'], flinkinfo
['fmtime']));
4948 if(flinkinfo
['ftype']==6 and hasattr(os
, "mkfifo")):
4949 os
.mkfifo(listcatfiles
['ffilelist'][lcfi
]['fname'], int(flinkinfo
['fchmode'], 8));
4951 os
.symlink(listcatfiles
['ffilelist'][lcfi
]['flinkname'], listcatfiles
['ffilelist'][lcfi
]['fname']);
4952 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==5):
4953 os
.mkdir(listcatfiles
['ffilelist'][lcfi
]['fname'], int(listcatfiles
['ffilelist'][lcfi
]['fchmode'], 8));
4954 if(hasattr(os
, "chown") and funame
==listcatfiles
['ffilelist'][lcfi
]['funame'] and fgname
==listcatfiles
['ffilelist'][lcfi
]['fgname']):
4955 os
.chown(listcatfiles
['ffilelist'][lcfi
]['fname'], listcatfiles
['ffilelist'][lcfi
]['fuid'], listcatfiles
['ffilelist'][lcfi
]['fgid']);
4956 os
.chmod(listcatfiles
['ffilelist'][lcfi
]['fname'], int(listcatfiles
['ffilelist'][lcfi
]['fchmode'], 8));
4957 os
.utime(listcatfiles
['ffilelist'][lcfi
]['fname'], (listcatfiles
['ffilelist'][lcfi
]['fatime'], listcatfiles
['ffilelist'][lcfi
]['fmtime']));
4958 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==6 and hasattr(os
, "mkfifo")):
4959 os
.mkfifo(listcatfiles
['ffilelist'][lcfi
]['fname'], int(listcatfiles
['ffilelist'][lcfi
]['fchmode'], 8));
4962 return listcatfiles
['ffilelist']['catfp'];
4966 create_alias_function("UnPack", __file_format_name__
, "", UnPackArchiveFile
);
4968 if(hasattr(shutil
, "register_unpack_format")):
4969 def UnPackArchiveFileFunc(archive_name
, extract_dir
=None, **kwargs
):
4970 return UnPackArchiveFile(archive_name
, extract_dir
, False, 0, 0, False, __file_format_delimiter__
, False, False);
4971 create_alias_function("UnPack", __file_format_name__
, "Func", UnPackArchiveFileFunc
);
4973 def UnPackArchiveFileString(catstr
, outdir
=None, followlink
=False, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
4974 catfp
= BytesIO(catstr
);
4975 listcatfiles
= UnPackArchiveFile(catfp
, outdir
, followlink
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
4976 return listcatfiles
;
4978 create_alias_function("UnPack", __file_format_name__
, "String", UnPackArchiveFileString
);
4980 def ArchiveFileListFiles(infile
, seekstart
=0, seekend
=0, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
4981 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
4982 if(isinstance(infile
, dict)):
4983 listcatfiles
= infile
;
4985 if(infile
!="-" and not hasattr(infile
, "read") and not hasattr(infile
, "write")):
4986 infile
= RemoveWindowsPath(infile
);
4987 listcatfiles
= ArchiveFileToArray(infile
, seekstart
, seekend
, True, skipchecksum
, formatspecs
, returnfp
);
4988 if(not listcatfiles
):
4990 lenlist
= len(listcatfiles
['ffilelist']);
4995 returnval
.update({lcfi
: listcatfiles
['ffilelist'][lcfi
]['fname']});
4997 VerbosePrintOut(listcatfiles
['ffilelist'][lcfi
]['fname']);
4999 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' } };
5000 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'];
5001 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==1):
5002 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " link to " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
5003 if(listcatfiles
['ffilelist'][lcfi
]['ftype']==2):
5004 printfname
= listcatfiles
['ffilelist'][lcfi
]['fname'] + " -> " + listcatfiles
['ffilelist'][lcfi
]['flinkname'];
5005 fuprint
= listcatfiles
['ffilelist'][lcfi
]['funame'];
5006 if(len(fuprint
)<=0):
5007 fuprint
= listcatfiles
['ffilelist'][lcfi
]['fuid'];
5008 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgname'];
5009 if(len(fgprint
)<=0):
5010 fgprint
= listcatfiles
['ffilelist'][lcfi
]['fgid'];
5011 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
));
5014 return listcatfiles
['catfp'];
5018 create_alias_function("", __file_format_name__
, "ListFiles", ArchiveFileListFiles
);
5020 def ArchiveFileStringListFiles(catstr
, followlink
=False, skipchecksum
=False, formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5021 catfp
= BytesIO(catstr
);
5022 listcatfiles
= UnPackArchiveFile(catfp
, None, followlink
, skipchecksum
, formatspecs
, verbose
, returnfp
);
5023 return listcatfiles
;
5025 create_alias_function("", __file_format_name__
, "StringListFiles", ArchiveFileListFiles
);
5027 def TarFileListFiles(infile
, verbose
=False, returnfp
=False):
5028 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5029 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5032 if(not tarfile
.is_tarfile(infile
)):
5034 except AttributeError:
5035 if(not is_tarfile(infile
)):
5040 tarfp
= tarfile
.open(infiles
, "r");
5041 except FileNotFoundError
:
5043 for member
in sorted(tarfp
.getmembers(), key
=lambda x
: x
.name
):
5044 returnval
.update({lcfi
: member
.name
});
5045 fpremode
= member
.mode
;
5046 ffullmode
= member
.mode
;
5050 ffullmode
= member
.mode
+ stat
.S_IFREG
;
5052 elif(member
.isdev()):
5053 ffullmode
= member
.mode
;
5055 elif(member
.islnk()):
5056 ffullmode
= member
.mode
+ stat
.S_IFREG
;
5058 elif(member
.issym()):
5059 ffullmode
= member
.mode
+ stat
.S_IFLNK
;
5061 elif(member
.ischr()):
5062 ffullmode
= member
.mode
+ stat
.S_IFCHR
;
5064 elif(member
.isblk()):
5065 ffullmode
= member
.mode
+ stat
.S_IFBLK
;
5067 elif(member
.isdir()):
5068 ffullmode
= member
.mode
+ stat
.S_IFDIR
;
5070 elif(member
.isfifo()):
5071 ffullmode
= member
.mode
+ stat
.S_IFIFO
;
5073 elif(member
.issparse()):
5074 ffullmode
= member
.mode
;
5077 VerbosePrintOut(member
.name
);
5079 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' } };
5080 printfname
= member
.name
;
5082 printfname
= member
.name
+ " link to " + member
.linkname
;
5083 elif(member
.issym()):
5084 printfname
= member
.name
+ " -> " + member
.linkname
;
5085 fuprint
= member
.uname
;
5086 if(len(fuprint
)<=0):
5087 fuprint
= member
.uid
;
5088 fgprint
= member
.gname
;
5089 if(len(fgprint
)<=0):
5090 fgprint
= member
.gid
;
5091 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
));
5094 return listcatfiles
['catfp'];
5098 def ZipFileListFiles(infile
, verbose
=False, returnfp
=False):
5099 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5100 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5102 if(not zipfile
.is_zipfile(infile
)):
5106 zipfp
= zipfile
.ZipFile(infile
, "r", allowZip64
=True);
5107 ziptest
= zipfp
.testzip();
5109 VerbosePrintOut("Bad file found!");
5110 for member
in sorted(zipfp
.infolist(), key
=lambda x
: x
.filename
):
5111 if(not member
.is_dir()):
5112 fpremode
= int(stat
.S_IFREG
+ 438);
5113 elif(member
.is_dir()):
5114 fpremode
= int(stat
.S_IFDIR
+ 511);
5115 if(not member
.is_dir()):
5116 fmode
= int(stat
.S_IFREG
+ 438);
5117 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
5118 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
5119 elif(member
.is_dir()):
5120 fmode
= int(stat
.S_IFDIR
+ 511);
5121 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
5122 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
5123 returnval
.update({lcfi
: member
.filename
});
5125 VerbosePrintOut(member
.filename
);
5127 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' } };
5129 for fmodval
in str(oct(fmode
))[-3:]:
5130 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
5131 if(not member
.is_dir()):
5133 permissionstr
= "-" + permissionstr
;
5134 elif(member
.is_dir()):
5136 permissionstr
= "d" + permissionstr
;
5137 printfname
= member
.filename
;
5139 fuid
= int(os
.getuid());
5140 except AttributeError:
5145 fgid
= int(os
.getgid());
5146 except AttributeError:
5153 userinfo
= pwd
.getpwuid(os
.getuid());
5154 funame
= userinfo
.pw_name
;
5157 except AttributeError:
5165 groupinfo
= grp
.getgrgid(os
.getgid());
5166 fgname
= groupinfo
.gr_name
;
5169 except AttributeError:
5174 if(len(fuprint
)<=0):
5175 fuprint
= str(fuid
);
5177 if(len(fgprint
)<=0):
5178 fgprint
= str(fgid
);
5179 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
));
5182 return listcatfiles
['catfp'];
5186 if(not rarfile_support
):
5187 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
5188 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5189 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5192 if(rarfile_support
):
5193 def RarFileListFiles(infile
, verbose
=False, returnfp
=False):
5194 logging
.basicConfig(format
="%(message)s", stream
=sys
.stdout
, level
=logging
.DEBUG
);
5195 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
5197 if(not rarfile
.is_rarfile(infile
) and not rarfile
.is_rarfile_sfx(infile
)):
5201 rarfp
= rarfile
.RarFile(infile
, "r");
5202 rartest
= rarfp
.testrar();
5204 VerbosePrintOut("Bad file found!");
5205 for member
in sorted(rarfp
.infolist(), key
=lambda x
: x
.filename
):
5208 if(member
.host_os
== rarfile
.RAR_OS_UNIX
):
5211 member
.external_attr
5213 except AttributeError:
5215 elif(member
.host_os
== rarfile
.RAR_OS_WIN32
):
5218 member
.external_attr
5220 except AttributeError:
5225 if(is_unix
and member
.external_attr
!=0):
5226 fpremode
= int(member
.external_attr
);
5227 elif(member
.is_file()):
5228 fpremode
= int(stat
.S_IFREG
+ 438);
5229 elif(member
.is_symlink()):
5230 fpremode
= int(stat
.S_IFLNK
+ 438);
5231 elif(member
.is_dir()):
5232 fpremode
= int(stat
.S_IFDIR
+ 511);
5233 if(is_windows
and member
.external_attr
!=0):
5234 fwinattributes
= int(member
.external_attr
);
5236 fwinattributes
= int(0);
5237 if(is_unix
and member
.external_attr
!=0):
5238 fmode
= int(member
.external_attr
);
5239 fchmode
= int(stat
.S_IMODE(member
.external_attr
));
5240 ftypemod
= int(stat
.S_IFMT(member
.external_attr
));
5241 elif(member
.is_file()):
5242 fmode
= int(stat
.S_IFREG
+ 438);
5243 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFREG
+ 438)));
5244 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFREG
+ 438)));
5245 elif(member
.is_symlink()):
5246 fmode
= int(stat
.S_IFLNK
+ 438);
5247 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFLNK
+ 438)));
5248 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFLNK
+ 438)));
5249 elif(member
.is_dir()):
5250 fmode
= int(stat
.S_IFDIR
+ 511);
5251 fchmode
= int(stat
.S_IMODE(int(stat
.S_IFDIR
+ 511)));
5252 ftypemod
= int(stat
.S_IFMT(int(stat
.S_IFDIR
+ 511)));
5253 returnval
.update({lcfi
: member
.filename
});
5255 VerbosePrintOut(member
.filename
);
5257 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' } };
5259 for fmodval
in str(oct(fmode
))[-3:]:
5260 permissionstr
= permissionstr
+ permissions
['access'].get(fmodval
, '---');
5261 if(member
.is_file()):
5263 permissionstr
= "-" + permissionstr
;
5264 printfname
= member
.filename
;
5265 elif(member
.is_symlink()):
5267 permissionstr
= "l" + permissionstr
;
5268 printfname
= member
.name
+ " -> " + member
.read().decode("UTF-8");
5269 elif(member
.is_dir()):
5271 permissionstr
= "d" + permissionstr
;
5272 printfname
= member
.filename
;
5274 fuid
= int(os
.getuid());
5275 except AttributeError:
5280 fgid
= int(os
.getgid());
5281 except AttributeError:
5288 userinfo
= pwd
.getpwuid(os
.getuid());
5289 funame
= userinfo
.pw_name
;
5292 except AttributeError:
5300 groupinfo
= grp
.getgrgid(os
.getgid());
5301 fgname
= groupinfo
.gr_name
;
5304 except AttributeError:
5309 if(len(fuprint
)<=0):
5310 fuprint
= str(fuid
);
5312 if(len(fgprint
)<=0):
5313 fgprint
= str(fgid
);
5314 VerbosePrintOut(PrintPermissionString(fmode
, ftype
) + " " + str(str(fuprint
) + "/" + str(fgprint
) + " " + str(member
.file_size
).rjust(15) + " " + member
.mtime
.strftime('%Y-%m-%d %H:%M') + " " + printfname
));
5317 return listcatfiles
['catfp'];
5321 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):
5322 outarray
= BytesIO();
5323 packcat
= PackArchiveFile(infiles
, outarray
, dirlistfromtxt
, compression
, compressionlevel
, followlink
, checksumtype
, formatspecs
, False, True);
5324 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
5325 return listcatfiles
;
5327 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):
5328 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, listonly
, checksumtype
, formatspecs
, verbose
);
5329 listcatfiles
= ArchiveFileListFiles(outarray
, seekstart
, seekend
, skipchecksum
, formatspecs
, verbose
, returnfp
);
5330 return listcatfiles
;
5332 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):
5333 outarray
= ListDirToArrayAlt(infiles
, dirlistfromtxt
, followlink
, False, checksumtype
, extradata
, formatspecs
, False);
5334 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, followlink
, checksumtype
, skipchecksum
, extradata
, formatspecs
, verbose
, returnfp
);
5335 return listcatfiles
;
5337 create_alias_function("Pack", __file_format_name__
, "FromListDirAlt", PackArchiveFileFromListDirAlt
);
5339 def PackArchiveFileFromTarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5340 outarray
= TarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
5341 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
5342 return listcatfiles
;
5344 create_alias_function("Pack", __file_format_name__
, "FromTarFileAlt", PackArchiveFileFromTarFileAlt
);
5346 def PackArchiveFileFromZipFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5347 outarray
= ZipFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
5348 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
5349 return listcatfiles
;
5351 create_alias_function("Pack", __file_format_name__
, "FromZipFileAlt", PackArchiveFileFromZipFileAlt
);
5353 if(not rarfile_support
):
5354 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5357 if(rarfile_support
):
5358 def PackArchiveFileFromRarFileAlt(infile
, outfile
, compression
="auto", compressionlevel
=None, checksumtype
="crc32", extradata
=[], formatspecs
=__file_format_list__
, verbose
=False, returnfp
=False):
5359 outarray
= RarFileToArrayAlt(infile
, False, checksumtype
, extradata
, formatspecs
, False);
5360 listcatfiles
= RePackArchiveFile(outarray
, outfile
, compression
, compressionlevel
, False, checksumtype
, False, extradata
, formatspecs
, verbose
, returnfp
);
5361 return listcatfiles
;
5363 create_alias_function("Pack", __file_format_name__
, "FromRarFileAlt", PackArchiveFileFromRarFileAlt
);
5365 def download_file_from_ftp_file(url
):
5366 urlparts
= urlparse
.urlparse(url
);
5367 file_name
= os
.path
.basename(urlparts
.path
);
5368 file_dir
= os
.path
.dirname(urlparts
.path
);
5369 if(urlparts
.username
is not None):
5370 ftp_username
= urlparts
.username
;
5372 ftp_username
= "anonymous";
5373 if(urlparts
.password
is not None):
5374 ftp_password
= urlparts
.password
;
5375 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
5376 ftp_password
= "anonymous";
5379 if(urlparts
.scheme
=="ftp"):
5381 elif(urlparts
.scheme
=="ftps"):
5385 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
5387 ftp_port
= urlparts
.port
;
5388 if(urlparts
.port
is None):
5391 ftp
.connect(urlparts
.hostname
, ftp_port
);
5392 except socket
.gaierror
:
5393 log
.info("Error With URL "+url
);
5395 except socket
.timeout
:
5396 log
.info("Error With URL "+url
);
5398 ftp
.login(urlparts
.username
, urlparts
.password
);
5399 if(urlparts
.scheme
=="ftps"):
5401 ftpfile
= BytesIO();
5402 ftp
.retrbinary("RETR "+urlparts
.path
, ftpfile
.write
);
5403 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
5408 def download_file_from_ftp_string(url
):
5409 ftpfile
= download_file_from_ftp_file(url
);
5410 return ftpfile
.read();
5412 def upload_file_to_ftp_file(ftpfile
, url
):
5413 urlparts
= urlparse
.urlparse(url
);
5414 file_name
= os
.path
.basename(urlparts
.path
);
5415 file_dir
= os
.path
.dirname(urlparts
.path
);
5416 if(urlparts
.username
is not None):
5417 ftp_username
= urlparts
.username
;
5419 ftp_username
= "anonymous";
5420 if(urlparts
.password
is not None):
5421 ftp_password
= urlparts
.password
;
5422 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
5423 ftp_password
= "anonymous";
5426 if(urlparts
.scheme
=="ftp"):
5428 elif(urlparts
.scheme
=="ftps"):
5432 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
5434 ftp_port
= urlparts
.port
;
5435 if(urlparts
.port
is None):
5438 ftp
.connect(urlparts
.hostname
, ftp_port
);
5439 except socket
.gaierror
:
5440 log
.info("Error With URL "+url
);
5442 except socket
.timeout
:
5443 log
.info("Error With URL "+url
);
5445 ftp
.login(urlparts
.username
, urlparts
.password
);
5446 if(urlparts
.scheme
=="ftps"):
5448 ftp
.storbinary("STOR "+urlparts
.path
, ftpfile
);
5453 def upload_file_to_ftp_string(ftpstring
, url
):
5454 ftpfileo
= BytesIO(ftpstring
);
5455 ftpfile
= upload_file_to_ftp_file(ftpfileo
, url
);
5460 def download_file_from_sftp_file(url
):
5461 urlparts
= urlparse
.urlparse(url
);
5462 file_name
= os
.path
.basename(urlparts
.path
);
5463 file_dir
= os
.path
.dirname(urlparts
.path
);
5464 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
5466 sftp_port
= urlparts
.port
;
5467 if(urlparts
.port
is None):
5470 sftp_port
= urlparts
.port
;
5471 if(urlparts
.username
is not None):
5472 sftp_username
= urlparts
.username
;
5474 sftp_username
= "anonymous";
5475 if(urlparts
.password
is not None):
5476 sftp_password
= urlparts
.password
;
5477 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
5478 sftp_password
= "anonymous";
5481 if(urlparts
.scheme
!="sftp"):
5483 ssh
= paramiko
.SSHClient();
5484 ssh
.load_system_host_keys();
5485 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
5487 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
5488 except paramiko
.ssh_exception
.SSHException
:
5490 except socket
.gaierror
:
5491 log
.info("Error With URL "+httpurl
);
5493 except socket
.timeout
:
5494 log
.info("Error With URL "+httpurl
);
5496 sftp
= ssh
.open_sftp();
5497 sftpfile
= BytesIO();
5498 sftp
.getfo(urlparts
.path
, sftpfile
);
5501 sftpfile
.seek(0, 0);
5504 def download_file_from_sftp_file(url
):
5508 def download_file_from_sftp_string(url
):
5509 sftpfile
= download_file_from_sftp_file(url
);
5510 return sftpfile
.read();
5512 def download_file_from_ftp_string(url
):
5516 def upload_file_to_sftp_file(sftpfile
, url
):
5517 urlparts
= urlparse
.urlparse(url
);
5518 file_name
= os
.path
.basename(urlparts
.path
);
5519 file_dir
= os
.path
.dirname(urlparts
.path
);
5520 sftp_port
= urlparts
.port
;
5521 if(urlparts
.scheme
=="http" or urlparts
.scheme
=="https"):
5523 if(urlparts
.port
is None):
5526 sftp_port
= urlparts
.port
;
5527 if(urlparts
.username
is not None):
5528 sftp_username
= urlparts
.username
;
5530 sftp_username
= "anonymous";
5531 if(urlparts
.password
is not None):
5532 sftp_password
= urlparts
.password
;
5533 elif(urlparts
.password
is None and urlparts
.username
=="anonymous"):
5534 sftp_password
= "anonymous";
5537 if(urlparts
.scheme
!="sftp"):
5539 ssh
= paramiko
.SSHClient();
5540 ssh
.load_system_host_keys();
5541 ssh
.set_missing_host_key_policy(paramiko
.AutoAddPolicy());
5543 ssh
.connect(urlparts
.hostname
, port
=sftp_port
, username
=urlparts
.username
, password
=urlparts
.password
);
5544 except paramiko
.ssh_exception
.SSHException
:
5546 except socket
.gaierror
:
5547 log
.info("Error With URL "+httpurl
);
5549 except socket
.timeout
:
5550 log
.info("Error With URL "+httpurl
);
5552 sftp
= ssh
.open_sftp();
5553 sftp
.putfo(sftpfile
, urlparts
.path
);
5556 sftpfile
.seek(0, 0);
5559 def upload_file_to_sftp_file(sftpfile
, url
):
5563 def upload_file_to_sftp_string(sftpstring
, url
):
5564 sftpfileo
= BytesIO(sftpstring
);
5565 sftpfile
= upload_file_to_sftp_files(ftpfileo
, url
);
5569 def upload_file_to_sftp_string(url
):
5573 if(hasattr(shutil
, "register_archive_format")):
5574 # Register the packing format
5575 shutil
.register_archive_format(__file_format_name__
, PackArchiveFileFunc
, description
='Pack concatenated files');
5576 except shutil
.RegistryError
:
5580 if(hasattr(shutil
, "register_unpack_format")):
5581 # Register the unpacking format
5582 shutil
.register_unpack_format(__file_format_name__
, archivefile_extensions
, UnPackArchiveFileFunc
, description
='UnPack concatenated files');
5583 except shutil
.RegistryError
: