Add files via upload
[PyCatFile.git] / pycatfile.py
blob54119195a9074aa2a33b3db9f8e1b9e352e61fd5
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 '''
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the Revised BSD License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 Revised BSD License for more details.
13 Copyright 2018-2024 Cool Dude 2k - http://idb.berlios.de/
14 Copyright 2018-2024 Game Maker 2k - http://intdb.sourceforge.net/
15 Copyright 2018-2024 Kazuki Przyborowski - https://github.com/KazukiPrzyborowski
17 $FileInfo: pycatfile.py - Last Update: 3/15/2024 Ver. 0.5.0 RC 1 - Author: cooldude2k $
18 '''
20 from __future__ import absolute_import, division, print_function, unicode_literals;
21 import io, os, re, sys, time, stat, zlib, base64, shutil, socket, hashlib, datetime, logging, binascii, tempfile, zipfile, ftplib, platform;
22 from ftplib import FTP, FTP_TLS;
23 if(sys.version[0]=="2"):
24 from urlparse import urlparse, urlunparse, urlsplit, urlunsplit, urljoin;
25 elif(sys.version[0]>="3"):
26 from urllib.parse import urlunparse, urlsplit, urlunsplit, urljoin, urlencode;
27 from urllib.parse import urlparse;
29 if os.name == 'nt': # Only modify if on Windows
30 if sys.version[0] == "2":
31 import codecs;
32 sys.stdout = codecs.getwriter('utf-8')(sys.stdout);
33 sys.stderr = codecs.getwriter('utf-8')(sys.stderr);
34 else:
35 import io;
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";
42 try:
43 reload(sys);
44 try:
45 sys.setdefaultencoding('UTF-8');
46 except NameError:
47 pass;
48 except AttributeError:
49 pass;
50 except NameError:
51 pass;
52 except AttributeError:
53 pass;
55 try:
56 import simplejson as json;
57 except ImportError:
58 import json;
60 try:
61 from zlib import crc32;
62 except ImportError:
63 from binascii import crc32;
65 rarfile_support = False;
66 try:
67 import rarfile;
68 rarfile_support = True;
69 except ImportError:
70 rarfile_support = False;
72 try:
73 from safetar import is_tarfile;
74 except ImportError:
75 try:
76 from xtarfile import is_tarfile;
77 except ImportError:
78 from tarfile import is_tarfile;
80 try:
81 import safetar as tarfile;
82 except ImportError:
83 try:
84 import xtarfile as tarfile;
85 except ImportError:
86 import tarfile;
88 haveparamiko = False;
89 try:
90 import paramiko;
91 haveparamiko = True;
92 except ImportError:
93 haveparamiko = False;
95 havepysftp = False;
96 try:
97 import pysftp;
98 havepysftp = True;
99 except ImportError:
100 havepysftp = False;
102 haverequests = False;
103 try:
104 import requests;
105 haverequests = True;
106 except ImportError:
107 haverequests = False;
109 if(haverequests):
110 import urllib3;
111 logging.getLogger("urllib3").setLevel(logging.WARNING);
113 try:
114 # Python 3 imports
115 from urllib.request import Request, build_opener, HTTPBasicAuthHandler;
116 from urllib.parse import urlparse;
117 except ImportError:
118 # Python 2 imports
119 from urllib2 import Request, build_opener, HTTPBasicAuthHandler;
120 from urlparse import urlparse;
122 if(sys.version[0]=="2"):
123 try:
124 from io import StringIO, BytesIO;
125 except ImportError:
126 try:
127 from cStringIO import StringIO;
128 from cStringIO import StringIO as BytesIO;
129 except ImportError:
130 from StringIO import StringIO;
131 from StringIO import StringIO as BytesIO;
132 elif(sys.version[0]>="3"):
133 from io import StringIO, BytesIO;
134 else:
135 teststringio = 0;
136 if(teststringio<=0):
137 try:
138 from cStringIO import StringIO as BytesIO;
139 teststringio = 1;
140 except ImportError:
141 teststringio = 0;
142 if(teststringio<=0):
143 try:
144 from StringIO import StringIO as BytesIO;
145 teststringio = 2;
146 except ImportError:
147 teststringio = 0;
148 if(teststringio<=0):
149 try:
150 from io import BytesIO;
151 teststringio = 3;
152 except ImportError:
153 teststringio = 0;
155 __use_pysftp__ = False;
156 if(not havepysftp):
157 __use_pysftp__ = False;
158 __file_format_name__ = "CatFile";
159 __program_name__ = "Py"+__file_format_name__;
160 __file_format_lower__ = __file_format_name__.lower();
161 __file_format_magic__ = __file_format_name__;
162 __file_format_len__ = len(__file_format_magic__);
163 __file_format_hex__ = binascii.hexlify(__file_format_magic__.encode("UTF-8")).decode("UTF-8");
164 __file_format_delimiter__ = "\x00";
165 __file_format_ver__ = "001";
166 __use_new_style__ = True;
167 __file_format_list__ = [__file_format_name__, __file_format_magic__, __file_format_lower__, __file_format_len__, __file_format_hex__, __file_format_delimiter__, __file_format_ver__, __use_new_style__];
168 __project__ = __program_name__;
169 __project_url__ = "https://github.com/GameMaker2k/PyCatFile";
170 __version_info__ = (0, 5, 0, "RC 1", 1);
171 __version_date_info__ = (2024, 3, 15, "RC 1", 1);
172 __version_date__ = str(__version_date_info__[0]) + "." + str(__version_date_info__[1]).zfill(2) + "." + str(__version_date_info__[2]).zfill(2);
173 __revision__ = __version_info__[3];
174 __revision_id__ = "$Id$";
175 if(__version_info__[4] is not None):
176 __version_date_plusrc__ = __version_date__ + "-" + str(__version_date_info__[4]);
177 if(__version_info__[4] is None):
178 __version_date_plusrc__ = __version_date__;
179 if(__version_info__[3] is not None):
180 __version__ = str(__version_info__[0]) + "." + str(__version_info__[1]) + "." + str(__version_info__[2]) + " " + str(__version_info__[3]);
181 if(__version_info__[3] is None):
182 __version__ = str(__version_info__[0]) + "." + str(__version_info__[1]) + "." + str(__version_info__[2]);
184 PyBitness = platform.architecture();
185 if(PyBitness=="32bit" or PyBitness=="32"):
186 PyBitness = "32";
187 elif(PyBitness=="64bit" or PyBitness=="64"):
188 PyBitness = "64";
189 else:
190 PyBitness = "32";
192 geturls_ua_pycatfile_python = "Mozilla/5.0 (compatible; {proname}/{prover}; +{prourl})".format(proname=__project__, prover=__version__, prourl=__project_url__);
193 if(platform.python_implementation()!=""):
194 py_implementation = platform.python_implementation();
195 if(platform.python_implementation()==""):
196 py_implementation = "Python";
197 geturls_ua_pycatfile_python_alt = "Mozilla/5.0 ({osver}; {archtype}; +{prourl}) {pyimp}/{pyver} (KHTML, like Gecko) {proname}/{prover}".format(osver=platform.system()+" "+platform.release(), archtype=platform.machine(), prourl=__project_url__, pyimp=py_implementation, pyver=platform.python_version(), proname=__project__, prover=__version__);
198 geturls_ua_googlebot_google = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
199 geturls_ua_googlebot_google_old = "Googlebot/2.1 (+http://www.google.com/bot.html)";
200 geturls_headers_pycatfile_python = {'Referer': "http://google.com/", 'User-Agent': geturls_ua_pycatfile_python, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close", 'SEC-CH-UA': "\""+__project__+"\";v=\""+str(__version__)+"\", \"Not;A=Brand\";v=\"8\", \""+py_implementation+"\";v=\""+str(platform.release())+"\"", 'SEC-CH-UA-FULL-VERSION': str(__version__), 'SEC-CH-UA-PLATFORM': ""+py_implementation+"", 'SEC-CH-UA-ARCH': ""+platform.machine()+"", 'SEC-CH-UA-PLATFORM': str(__version__), 'SEC-CH-UA-BITNESS': str(PyBitness)};
201 geturls_headers_pycatfile_python_alt = {'Referer': "http://google.com/", 'User-Agent': geturls_ua_pycatfile_python_alt, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close", 'SEC-CH-UA': "\""+__project__+"\";v=\""+str(__version__)+"\", \"Not;A=Brand\";v=\"8\", \""+py_implementation+"\";v=\""+str(platform.release())+"\"", 'SEC-CH-UA-FULL-VERSION': str(__version__), 'SEC-CH-UA-PLATFORM': ""+py_implementation+"", 'SEC-CH-UA-ARCH': ""+platform.machine()+"", 'SEC-CH-UA-PLATFORM': str(__version__), 'SEC-CH-UA-BITNESS': str(PyBitness)};
202 geturls_headers_googlebot_google = {'Referer': "http://google.com/", 'User-Agent': geturls_ua_googlebot_google, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close"};
203 geturls_headers_googlebot_google_old = {'Referer': "http://google.com/", 'User-Agent': geturls_ua_googlebot_google_old, 'Accept-Encoding': "none", 'Accept-Language': "en-US,en;q=0.8,en-CA,en-GB;q=0.6", 'Accept-Charset': "ISO-8859-1,ISO-8859-15,utf-8;q=0.7,*;q=0.7", 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 'Connection': "close"};
205 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'xz', 'lz4', 'lzo', 'lzma'];
206 outextlist = ['gz', 'bz2', 'zst', 'xz', 'lz4', 'lzo', 'lzma'];
207 outextlistwd = ['.gz', '.bz2', '.zst', '.xz', '.lz4', '.lzo', '.lzma'];
209 tarfile_mimetype = "application/tar";
210 tarfile_tar_mimetype = tarfile_mimetype;
211 zipfile_mimetype = "application/zip";
212 zipfile_zip_mimetype = zipfile_mimetype;
213 rarfile_mimetype = "application/rar";
214 rarfile_rar_mimetype = rarfile_mimetype;
215 archivefile_mimetype = "application/x-"+__file_format_list__[1]+"";
216 archivefile_cat_mimetype = archivefile_mimetype;
217 archivefile_gzip_mimetype = "application/x-"+__file_format_list__[1]+"+gzip";
218 archivefile_gz_mimetype = archivefile_gzip_mimetype;
219 archivefile_bzip2_mimetype = "application/x-"+__file_format_list__[1]+"+bzip2";
220 archivefile_bz2_mimetype = archivefile_bzip2_mimetype;
221 archivefile_lz4_mimetype = "application/x-"+__file_format_list__[1]+"+lz4";
222 archivefile_lzop_mimetype = "application/x-"+__file_format_list__[1]+"+lzop";
223 archivefile_lzo_mimetype = archivefile_lzop_mimetype;
224 archivefile_zstandard_mimetype = "application/x-"+__file_format_list__[1]+"+zstandard";
225 archivefile_zstd_mimetype = archivefile_zstandard_mimetype;
226 archivefile_lzma_mimetype = "application/x-"+__file_format_list__[1]+"+lzma";
227 archivefile_xz_mimetype = "application/x-"+__file_format_list__[1]+"+xz";
228 archivefile_extensions = ['.cat', '.cat.gz', '.cat.bz2', '.cat.zst', '.cat.lz4', '.cat.lzo', '.cat.lzop', '.cat.lzma', '.cat.xz'];
230 if __name__ == "__main__":
231 import subprocess;
232 curscrpath = os.path.dirname(sys.argv[0]);
233 if(curscrpath==""):
234 curscrpath = ".";
235 if(os.sep=="\\"):
236 curscrpath = curscrpath.replace(os.sep, "/");
237 curscrpath = curscrpath + "/";
238 scrfile = curscrpath + "catfile.py";
239 if(os.path.exists(scrfile) and os.path.isfile(scrfile)):
240 scrcmd = subprocess.Popen([sys.executable, scrfile] + sys.argv[1:]);
241 scrcmd.wait();
243 def VerbosePrintOut(dbgtxt, outtype="log", dbgenable=True, dgblevel=20):
244 if(not dbgenable):
245 return True;
246 log_functions = {
247 "print": print,
248 "log": logging.info,
249 "warning": logging.warning,
250 "error": logging.error,
251 "critical": logging.critical,
252 "exception": logging.exception,
253 "logalt": lambda x: logging.log(dgblevel, x),
254 "debug": logging.debug
256 log_function = log_functions.get(outtype);
257 if(log_function):
258 log_function(dbgtxt);
259 return True;
260 return False;
262 def VerbosePrintOutReturn(dbgtxt, outtype="log", dbgenable=True, dgblevel=20):
263 VerbosePrintOut(dbgtxt, outtype, dbgenable, dgblevel);
264 return dbgtxt;
266 def RemoveWindowsPath(dpath):
267 if(dpath is None):
268 dpath = "";
269 if(os.sep!="/"):
270 dpath = dpath.replace(os.path.sep, "/");
271 dpath = dpath.rstrip("/");
272 if(dpath=="." or dpath==".."):
273 dpath = dpath + "/";
274 return dpath;
276 def NormalizeRelativePath(inpath):
277 inpath = RemoveWindowsPath(inpath);
278 if(os.path.isabs(inpath)):
279 outpath = inpath;
280 else:
281 if(inpath.startswith("./") or inpath.startswith("../")):
282 outpath = inpath;
283 else:
284 outpath = "./" + inpath;
285 return outpath;
287 def ListDir(dirpath, followlink=False, duplicates=False):
288 if(isinstance(dirpath, (list, tuple, ))):
289 dirpath = list(filter(None, dirpath));
290 elif(isinstance(dirpath, (str, ))):
291 dirpath = list(filter(None, [dirpath]));
292 retlist = [];
293 for mydirfile in dirpath:
294 if(not os.path.exists(mydirfile)):
295 return False;
296 mydirfile = NormalizeRelativePath(mydirfile);
297 if(os.path.exists(mydirfile) and os.path.islink(mydirfile)):
298 mydirfile = RemoveWindowsPath(os.path.realpath(mydirfile));
299 if(os.path.exists(mydirfile) and os.path.isdir(mydirfile)):
300 for root, dirs, filenames in os.walk(mydirfile):
301 dpath = root;
302 dpath = RemoveWindowsPath(dpath);
303 if(dpath not in retlist and not duplicates):
304 retlist.append(dpath);
305 if(duplicates):
306 retlist.append(dpath);
307 for file in filenames:
308 fpath = os.path.join(root, file);
309 fpath = RemoveWindowsPath(fpath);
310 if(fpath not in retlist and not duplicates):
311 retlist.append(fpath);
312 if(duplicates):
313 retlist.append(fpath);
314 else:
315 retlist.append(RemoveWindowsPath(mydirfile));
316 return retlist;
318 def ListDirAdvanced(dirpath, followlink=False, duplicates=False):
319 if isinstance(dirpath, (list, tuple)):
320 dirpath = list(filter(None, dirpath));
321 elif isinstance(dirpath, str):
322 dirpath = list(filter(None, [dirpath]));
323 retlist = []
324 for mydirfile in dirpath:
325 if not os.path.exists(mydirfile):
326 return False;
327 mydirfile = NormalizeRelativePath(mydirfile);
328 if os.path.exists(mydirfile) and os.path.islink(mydirfile) and followlink:
329 mydirfile = RemoveWindowsPath(os.path.realpath(mydirfile))
330 if os.path.exists(mydirfile) and os.path.isdir(mydirfile):
331 for root, dirs, filenames in os.walk(mydirfile):
332 # Sort dirs and filenames alphabetically in place
333 dirs.sort(key=lambda x: x.lower());
334 filenames.sort(key=lambda x: x.lower());
335 dpath = RemoveWindowsPath(root);
336 if not duplicates and dpath not in retlist:
337 retlist.append(dpath);
338 elif duplicates:
339 retlist.append(dpath);
340 for file in filenames:
341 fpath = os.path.join(root, file);
342 fpath = RemoveWindowsPath(fpath);
343 if not duplicates and fpath not in retlist:
344 retlist.append(fpath);
345 elif duplicates:
346 retlist.append(fpath);
347 else:
348 retlist.append(RemoveWindowsPath(mydirfile));
349 return retlist;
351 def create_alias_function(prefix, base_name, suffix, target_function):
352 # Define a new function that wraps the target function
353 def alias_function(*args, **kwargs):
354 return target_function(*args, **kwargs);
356 # Create the function name by combining the prefix, base name, and the suffix
357 function_name = "{}{}{}".format(prefix, base_name, suffix);
359 # Add the new function to the global namespace
360 globals()[function_name] = alias_function;
362 # initial_value can be 0xFFFF or 0x0000
363 def crc16_ansi(msg, initial_value=0xFFFF):
364 # CRC-16-IBM / CRC-16-ANSI polynomial and initial value
365 poly = 0x8005; # Polynomial for CRC-16-IBM / CRC-16-ANSI
366 crc = initial_value; # Initial value
367 for b in msg:
368 crc ^= b << 8; # XOR byte into CRC top byte
369 for _ in range(8): # Process each bit
370 if crc & 0x8000: # If the top bit is set
371 crc = (crc << 1) ^ poly; # Shift left and XOR with the polynomial
372 else:
373 crc = crc << 1; # Just shift left
374 crc &= 0xFFFF; # Ensure CRC remains 16-bit
375 return crc;
377 # initial_value can be 0xFFFF or 0x0000
378 def crc16_ibm(msg, initial_value=0xFFFF):
379 return crc16_ansi(msg, initial_value);
381 # initial_value is 0xFFFF
382 def crc16(msg):
383 return crc16_ansi(msg, 0xFFFF);
385 # initial_value can be 0xFFFF, 0x1D0F or 0x0000
386 def crc16_ccitt(msg, initial_value=0xFFFF):
387 # CRC-16-CCITT polynomial
388 poly = 0x1021; # Polynomial for CRC-16-CCITT
389 # Use the specified initial value
390 crc = initial_value;
391 for b in msg:
392 crc ^= b << 8; # XOR byte into CRC top byte
393 for _ in range(8): # Process each bit
394 if crc & 0x8000: # If the top bit is set
395 crc = (crc << 1) ^ poly; # Shift left and XOR with the polynomial
396 else:
397 crc = crc << 1; # Just shift left
398 crc &= 0xFFFF; # Ensure CRC remains 16-bit
399 return crc;
401 # initial_value can be 0x42F0E1EBA9EA3693 or 0x0000000000000000
402 def crc64_ecma(msg, initial_value=0x0000000000000000):
403 # CRC-64-ECMA polynomial and initial value
404 poly = 0x42F0E1EBA9EA3693;
405 crc = initial_value; # Initial value for CRC-64-ECMA
406 for b in msg:
407 crc ^= b << 56; # XOR byte into the most significant byte of the CRC
408 for _ in range(8): # Process each bit
409 if crc & (1 << 63): # Check if the leftmost (most significant) bit is set
410 crc = (crc << 1) ^ poly; # Shift left and XOR with poly if the MSB is 1
411 else:
412 crc <<= 1; # Just shift left if the MSB is 0
413 crc &= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
414 return crc;
416 # initial_value can be 0x000000000000001B or 0xFFFFFFFFFFFFFFFF
417 def crc64_iso(msg, initial_value=0xFFFFFFFFFFFFFFFF):
418 # CRC-64-ISO polynomial and initial value
419 poly = 0x000000000000001B;
420 crc = initial_value; # Common initial value for CRC-64-ISO
421 for b in msg:
422 crc ^= b << 56; # XOR byte into the most significant byte of the CRC
423 for _ in range(8): # Process each bit
424 if crc & (1 << 63): # Check if the leftmost (most significant) bit is set
425 crc = (crc << 1) ^ poly; # Shift left and XOR with poly if the MSB is 1
426 else:
427 crc <<= 1; # Just shift left if the MSB is 0
428 crc &= 0xFFFFFFFFFFFFFFFF; # Ensure CRC remains 64-bit
429 return crc;
431 def GetDataFromArray(data, path, default=None):
432 element = data;
433 try:
434 for key in path:
435 element = element[key];
436 return element;
437 except (KeyError, TypeError, IndexError):
438 return default;
440 def GetDataFromArrayAlt(structure, path, default=None):
441 element = structure;
442 for key in path:
443 if isinstance(element, dict) and key in element:
444 element = element[key];
445 elif isinstance(element, list) and isinstance(key, int) and -len(element) <= key < len(element):
446 element = element[key];
447 else:
448 return default;
449 return element;
451 def ReadTillNullByte(fp, delimiter=__file_format_delimiter__):
452 curbyte = b"";
453 curfullbyte = b"";
454 nullbyte = delimiter.encode("UTF-8");
455 while(True):
456 curbyte = fp.read(1);
457 if(curbyte==nullbyte or not curbyte):
458 break;
459 curfullbyte = curfullbyte + curbyte;
460 return curfullbyte.decode('UTF-8');
462 def ReadUntilNullByte(fp, delimiter=__file_format_delimiter__):
463 return ReadTillNullByte(fp, delimiter);
465 def SeekToEndOfFile(fp):
466 lasttell = 0;
467 while(True):
468 fp.seek(1, 1);
469 if(lasttell==fp.tell()):
470 break;
471 lasttell = fp.tell();
472 return True;
474 def ReadFileHeaderData(fp, rounds=0, delimiter=__file_format_delimiter__):
475 rocount = 0;
476 roend = int(rounds);
477 HeaderOut = [];
478 while(rocount<roend):
479 HeaderOut.append(ReadTillNullByte(fp, delimiter));
480 rocount = rocount + 1;
481 return HeaderOut;
483 def ReadFileHeaderDataBySize(fp, delimiter=__file_format_delimiter__):
484 headerpresize = ReadTillNullByte(fp, delimiter);
485 headersize = int(headerpresize, 16);
486 headercontent = str(fp.read(headersize).decode('UTF-8')).split(delimiter);
487 fp.seek(1, 1);
488 rocount = 0;
489 roend = int(len(headercontent));
490 HeaderOut = [headerpresize];
491 while(rocount<roend):
492 HeaderOut.append(headercontent[rocount]);
493 rocount = rocount + 1;
494 return HeaderOut;
496 def ReadFileHeaderDataByList(fp, listval=[], delimiter=__file_format_delimiter__):
497 rocount = 0;
498 roend = int(len(listval));
499 HeaderOut = {};
500 while(rocount<roend):
501 RoundArray = {listval[rocount]: ReadTillNullByte(fp, delimiter)};
502 HeaderOut.update(RoundArray);
503 rocount = rocount + 1;
504 return HeaderOut;
506 def ReadFileHeaderDataByListSize(fp, listval=[], delimiter=__file_format_delimiter__):
507 headerpresize = ReadTillNullByte(fp, delimiter);
508 headersize = int(headerpresize, 16);
509 headercontent = str(fp.read(headersize).decode('UTF-8')).split(delimiter);
510 fp.seek(1, 1);
511 rocount = 0;
512 listcount = 1;
513 roend = int(len(headercontent));
514 HeaderOut = {listval[0]: headerpresize};
515 while(rocount<roend):
516 RoundArray = {listval[rocount]: headercontent[rocount]};
517 HeaderOut.update(RoundArray);
518 rocount = rocount + 1;
519 listcount = listcount + 1;
520 return HeaderOut;
522 def AppendNullByte(indata, delimiter=__file_format_delimiter__):
523 outdata = str(indata) + delimiter;
524 return outdata;
526 def AppendNullBytes(indata=[], delimiter=__file_format_delimiter__):
527 outdata = "";
528 inum = 0;
529 il = len(indata);
530 while(inum < il):
531 outdata = outdata + AppendNullByte(indata[inum], delimiter);
532 inum = inum + 1;
533 return outdata;
535 def ReadTillNullByteAlt(fp, delimiter=__file_format_delimiter__):
536 """Read bytes from file pointer until a null byte is encountered."""
537 bytes_list = [] # Use list for efficient append operation.
538 while True:
539 cur_byte = fp.read(1);
540 if cur_byte == delimiter.encode() or not cur_byte:
541 break;
542 bytes_list.append(cur_byte);
543 return b''.join(bytes_list).decode('UTF-8');
545 def ReadUntilNullByteAlt(fp, delimiter=__file_format_delimiter__):
546 return ReadTillNullByteAlt(fp, delimiter);
548 def ReadFileHeaderDataAlt(fp, rounds=0, delimiter=__file_format_delimiter__):
549 """Read multiple null-byte terminated strings from a file."""
550 header_out = [];
551 for round_count in range(rounds):
552 header_out[round_count] = ReadTillNullByteAlt(fp, delimiter);
553 return header_out;
555 def ReadFileHeaderDataBySizeAlt(fp, delimiter=__file_format_delimiter__):
556 # Read and convert header size from hexadecimal to integer
557 header_pre_size = ReadTillNullByte(fp, delimiter);
558 header_size = int(header_pre_size, 16);
559 # Read and split the header content
560 header_content = str(fp.read(header_size).decode('UTF-8')).split(delimiter);
561 fp.seek(1, 1);
562 # Prepend the pre-size and return the combined list
563 return [header_pre_size] + header_content;
565 def ReadFileHeaderDataByListSizeAlt(fp, listval=[], delimiter=__file_format_delimiter__):
566 # Read the size and content from the header
567 header_pre_size = ReadTillNullByte(fp, delimiter);
568 header_size = int(header_pre_size, 16);
569 header_content = str(fp.read(header_size).decode('UTF-8')).split(delimiter);
570 fp.seek(1, 1);
571 # Initialize HeaderOut with the header pre-size if listval is not empty
572 HeaderOut = {listval[0]: header_pre_size} if listval else {};
573 # Map the remaining listval items to their corresponding header content, starting from the second item
574 for i in range(1, min(len(header_content) + 1, len(listval))):
575 HeaderOut[listval[i]] = header_content[i - 1]; # -1 because header_content is 0-indexed
576 return HeaderOut;
578 def ReadFileHeaderDataByListAlt(fp, listval=[], delimiter=__file_format_delimiter__):
579 """Read multiple null-byte terminated strings from a file."""
580 header_out = {};
581 for round_count in listval:
582 header_out.append(ReadTillNullByteAlt(fp, delimiter));
583 return header_out;
585 def AppendNullByteAlt(indata, delimiter=__file_format_delimiter__):
586 """Append a null byte to the given data."""
587 return str(indata) + delimiter;
589 def AppendNullBytesAlt(indata=[], delimiter=__file_format_delimiter__):
590 """Append a null byte to each element in the list and concatenate."""
591 return delimiter.join(map(str, indata)) + delimiter; # Efficient concatenation with null byte.
593 def PrintPermissionString(fchmode, ftype):
594 permissions = { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
595 permissionstr = "";
596 for fmodval in str(oct(fchmode))[-3:]:
597 permissionstr = permissionstr + permissions['access'].get(fmodval, '---');
598 if(ftype==0 or ftype==7):
599 permissionstr = "-" + permissionstr;
600 if(ftype==1):
601 permissionstr = "h" + permissionstr;
602 if(ftype==2):
603 permissionstr = "l" + permissionstr;
604 if(ftype==3):
605 permissionstr = "c" + permissionstr;
606 if(ftype==4):
607 permissionstr = "b" + permissionstr;
608 if(ftype==5):
609 permissionstr = "d" + permissionstr;
610 if(ftype==6):
611 permissionstr = "f" + permissionstr;
612 if(ftype==8):
613 permissionstr = "D" + permissionstr;
614 if(ftype==9):
615 permissionstr = "p" + permissionstr;
616 if(ftype==10):
617 permissionstr = "w" + permissionstr;
618 try:
619 permissionoutstr = stat.filemode(fchmode);
620 except AttributeError:
621 permissionoutstr = permissionstr;
622 except KeyError:
623 permissionoutstr = permissionstr;
624 return permissionoutstr;
626 def PrintPermissionStringAlt(fchmode, ftype):
627 permissions = {
628 '0': '---', '1': '--x', '2': '-w-', '3': '-wx',
629 '4': 'r--', '5': 'r-x', '6': 'rw-', '7': 'rwx'
631 # Translate file mode into permission string
632 permissionstr = ''.join([permissions[i] for i in str(oct(fchmode))[-3:]]);
633 # Append file type indicator
634 type_indicators = {
635 0: '-', 1: 'h', 2: 'l', 3: 'c', 4: 'b',
636 5: 'd', 6: 'f', 8: 'D', 9: 'p', 10: 'w'
638 file_type = type_indicators.get(ftype, '-');
639 permissionstr = file_type + permissionstr;
640 try:
641 permissionoutstr = stat.filemode(fchmode);
642 except AttributeError:
643 permissionoutstr = permissionstr;
644 return permissionoutstr;
646 def CompressionSupport():
647 compression_list = [];
648 try:
649 import gzip;
650 compression_list.append("gz");
651 compression_list.append("gzip");
652 except ImportError:
653 '''return False;'''
654 try:
655 import bz2;
656 compression_list.append("bz2");
657 compression_list.append("bzip2");
658 except ImportError:
659 '''return False;'''
660 try:
661 import lz4;
662 compression_list.append("lz4");
663 except ImportError:
664 '''return False;'''
665 try:
666 import lzo;
667 compression_list.append("lzo");
668 compression_list.append("lzop");
669 except ImportError:
670 '''return False;'''
671 try:
672 import zstandard;
673 compression_list.append("zstd");
674 compression_list.append("zstandard");
675 except ImportError:
676 '''return False;'''
677 try:
678 import lzma;
679 compression_list.append("lzma");
680 compression_list.append("xz");
681 except ImportError:
682 '''return False;'''
683 return compression_list;
685 def CheckCompressionType(infile, formatspecs=__file_format_list__, closefp=True):
686 if(hasattr(infile, "read") or hasattr(infile, "write")):
687 catfp = infile;
688 else:
689 try:
690 catfp = open(infile, "rb");
691 except FileNotFoundError:
692 return False;
693 catfp.seek(0, 0);
694 prefp = catfp.read(2);
695 filetype = False;
696 if(prefp==binascii.unhexlify("1f8b")):
697 filetype = "gzip";
698 catfp.seek(0, 0);
699 prefp = catfp.read(3);
700 if(prefp==binascii.unhexlify("425a68")):
701 filetype = "bzip2";
702 if(prefp==binascii.unhexlify("5d0000")):
703 filetype = "lzma";
704 catfp.seek(0, 0);
705 prefp = catfp.read(4);
706 if(prefp==binascii.unhexlify("28b52ffd")):
707 filetype = "zstd";
708 if(prefp==binascii.unhexlify("04224d18")):
709 filetype = "lz4";
710 if(prefp==binascii.unhexlify("504B0304")):
711 filetype = "zipfile";
712 catfp.seek(0, 0);
713 prefp = catfp.read(5);
714 if(prefp==binascii.unhexlify("7573746172")):
715 filetype = "tarfile";
716 catfp.seek(0, 0);
717 prefp = catfp.read(6);
718 if(prefp==binascii.unhexlify("fd377a585a00")):
719 filetype = "lzma";
720 catfp.seek(0, 0);
721 prefp = catfp.read(7);
722 if(prefp==binascii.unhexlify("526172211a0700")):
723 filetype = "rarfile";
724 if(prefp==binascii.unhexlify("43617446696c65")):
725 filetype = "catfile";
726 catfp.seek(0, 0);
727 prefp = catfp.read(8);
728 if(prefp==binascii.unhexlify("526172211a070100")):
729 filetype = "rarfile";
730 catfp.seek(0, 0);
731 prefp = catfp.read(formatspecs[3]);
732 if(prefp==binascii.unhexlify(formatspecs[4])):
733 filetype = formatspecs[2];
734 catfp.seek(0, 0);
735 prefp = catfp.read(9);
736 if(prefp==binascii.unhexlify("894c5a4f000d0a1a0a")):
737 filetype = "lzo";
738 catfp.seek(0, 0);
739 prefp = catfp.read(10);
740 if(prefp==binascii.unhexlify("7061785f676c6f62616c")):
741 filetype = "tarfile";
742 catfp.seek(0, 0);
743 if(closefp):
744 catfp.close();
745 return filetype;
747 def CheckCompressionTypeFromString(instring, formatspecs=__file_format_list__, closefp=True):
748 try:
749 instringsfile = BytesIO(instring);
750 except TypeError:
751 instringsfile = BytesIO(instring.encode("UTF-8"));
752 return CheckCompressionType(instringsfile, formatspecs, closefp);
754 def GetCompressionMimeType(infile, formatspecs=__file_format_list__):
755 compresscheck = CheckCompressionType(fp, formatspecs, False);
756 if(compresscheck=="gzip" or compresscheck=="gz"):
757 return archivefile_gzip_mimetype;
758 if(compresscheck=="bzip2" or compresscheck=="bz2"):
759 return archivefile_bzip2_mimetype;
760 if(compresscheck=="zstd" or compresscheck=="zstandard"):
761 return archivefile_zstandard_mimetype;
762 if(compresscheck=="lz4"):
763 return archivefile_lz4_mimetype;
764 if(compresscheck=="lzo" or compresscheck=="lzop"):
765 return archivefile_lzop_mimetype;
766 if(compresscheck=="lzma"):
767 return archivefile_lzma_mimetype;
768 if(compresscheck=="xz"):
769 return archivefile_xz_mimetype;
770 if(compresscheck=="catfile" or compresscheck=="cat" or compresscheck==formatspecs[2]):
771 return archivefile_cat_mimetype;
772 if(not compresscheck):
773 return False;
774 return False;
776 def UncompressArchiveFile(fp, formatspecs=__file_format_list__):
777 if(not hasattr(fp, "read") and not hasattr(fp, "write")):
778 return False;
779 compresscheck = CheckCompressionType(fp, formatspecs, False);
780 if(compresscheck=="gzip"):
781 try:
782 import gzip;
783 except ImportError:
784 return False;
785 catfp = gzip.GzipFile(fileobj=fp, mode="rb");
786 if(compresscheck=="bzip2"):
787 try:
788 import bz2;
789 except ImportError:
790 return False;
791 catfp = BytesIO();
792 catfp.write(bz2.decompress(fp.read()));
793 if(compresscheck=="zstd"):
794 try:
795 import zstandard;
796 except ImportError:
797 return False;
798 catfp = BytesIO();
799 catfp.write(zstandard.decompress(fp.read()));
800 if(compresscheck=="lz4"):
801 try:
802 import lz4.frame;
803 except ImportError:
804 return False;
805 catfp = BytesIO();
806 catfp.write(lz4.frame.decompress(fp.read()));
807 if(compresscheck=="lzo" or compresscheck=="lzop"):
808 try:
809 import lzo;
810 except ImportError:
811 return False;
812 catfp = BytesIO();
813 catfp.write(lzo.decompress(fp.read()));
814 if(compresscheck=="lzma" or compresscheck=="xz"):
815 try:
816 import lzma;
817 except ImportError:
818 return False;
819 catfp = BytesIO();
820 catfp.write(lzma.decompress(fp.read()));
821 if(compresscheck=="catfile" or compresscheck==formatspecs[2]):
822 catfp = fp;
823 if(not compresscheck):
824 try:
825 import lzma;
826 except ImportError:
827 return False;
828 catfp = BytesIO();
829 with fp as fpcontent:
830 try:
831 catfp.write(lzma.decompress(fp.read()));
832 except lzma.LZMAError:
833 return False;
834 return catfp;
836 create_alias_function("Uncompress", __file_format_name__, "", UncompressArchiveFile);
838 def UncompressFile(infile, formatspecs=__file_format_list__, mode="rb"):
839 compresscheck = CheckCompressionType(infile, formatspecs, False);
840 if(sys.version_info[0]==2 and compresscheck):
841 if(mode=="rt"):
842 mode = "r";
843 if(mode=="wt"):
844 mode = "w";
845 try:
846 if(compresscheck=="gzip"):
847 try:
848 import gzip;
849 except ImportError:
850 return False;
851 try:
852 filefp = gzip.open(infile, mode, encoding="UTF-8");
853 except (ValueError, TypeError) as e:
854 filefp = gzip.open(infile, mode);
855 if(compresscheck=="bzip2"):
856 try:
857 import bz2;
858 except ImportError:
859 return False;
860 try:
861 filefp = bz2.open(infile, mode, encoding="UTF-8");
862 except (ValueError, TypeError) as e:
863 filefp = bz2.open(infile, mode);
864 if(compresscheck=="zstd"):
865 try:
866 import zstandard;
867 except ImportError:
868 return False;
869 try:
870 filefp = zstandard.open(infile, mode, encoding="UTF-8");
871 except (ValueError, TypeError) as e:
872 filefp = zstandard.open(infile, mode);
873 if(compresscheck=="lz4"):
874 try:
875 import lz4.frame;
876 except ImportError:
877 return False;
878 try:
879 filefp = lz4.frame.open(infile, mode, encoding="UTF-8");
880 except (ValueError, TypeError) as e:
881 filefp = lz4.frame.open(infile, mode);
882 if(compresscheck=="lzo"):
883 try:
884 import lzo;
885 except ImportError:
886 return False;
887 try:
888 filefp = lzo.open(infile, mode, encoding="UTF-8");
889 except (ValueError, TypeError) as e:
890 filefp = lzo.open(infile, mode);
891 if(compresscheck=="lzma"):
892 try:
893 import lzma;
894 except ImportError:
895 return False;
896 try:
897 filefp = lzma.open(infile, mode, encoding="UTF-8");
898 except (ValueError, TypeError) as e:
899 filefp = lzma.open(infile, mode);
900 if(compresscheck=="catfile" or compresscheck==formatspecs[2]):
901 try:
902 filefp = open(infile, mode, encoding="UTF-8");
903 except (ValueError, TypeError) as e:
904 filefp = open(infile, mode);
905 if(not compresscheck):
906 try:
907 filefp = open(infile, mode, encoding="UTF-8");
908 except (ValueError, TypeError) as e:
909 filefp = open(infile, mode);
910 except FileNotFoundError:
911 return False;
912 return filefp;
914 def UncompressString(infile):
915 compresscheck = CheckCompressionTypeFromString(infile, formatspecs, False);
916 if(compresscheck=="gzip"):
917 try:
918 import gzip;
919 except ImportError:
920 return False;
921 fileuz = gzip.decompress(infile);
922 if(compresscheck=="bzip2"):
923 try:
924 import bz2;
925 except ImportError:
926 return False;
927 fileuz = bz2.decompress(infile);
928 if(compresscheck=="zstd"):
929 try:
930 import zstandard;
931 except ImportError:
932 return False;
933 fileuz = zstandard.decompress(infile);
934 if(compresscheck=="lz4"):
935 try:
936 import lz4.frame;
937 except ImportError:
938 return False;
939 fileuz = lz4.frame.decompress(infile);
940 if(compresscheck=="lzo"):
941 try:
942 import lzo;
943 except ImportError:
944 return False;
945 fileuz = lzo.decompress(infile);
946 if(compresscheck=="lzma"):
947 try:
948 import lzma;
949 except ImportError:
950 return False;
951 fileuz = lzma.decompress(infile);
952 if(not compresscheck):
953 fileuz = infile;
954 if(hasattr(fileuz, 'decode')):
955 fileuz = fileuz.decode("UTF-8");
956 return fileuz;
958 def UncompressStringAlt(infile):
959 filefp = StringIO();
960 outstring = UncompressString(infile);
961 filefp.write(outstring);
962 filefp.seek(0);
963 return filefp;
965 def CheckCompressionSubType(infile, formatspecs=__file_format_list__):
966 compresscheck = CheckCompressionType(infile, formatspecs, False);
967 if(not compresscheck):
968 fextname = os.path.splitext(infile)[1];
969 if(fextname==".gz"):
970 compresscheck = "gzip";
971 if(fextname==".bz2"):
972 compresscheck = "bzip2";
973 if(fextname==".zst"):
974 compresscheck = "zstd";
975 if(fextname==".lz4"):
976 compresscheck = "lz4";
977 if(fextname==".lzo" or fextname==".lzop"):
978 compresscheck = "lzo";
979 if(fextname==".lzma" or fextname==".xz"):
980 compresscheck = "lzma";
981 if(not compresscheck):
982 return False;
983 if(compresscheck=="catfile"):
984 return "catfile";
985 if(compresscheck==formatspecs[2]):
986 return formatspecs[2];
987 if(compresscheck=="tarfile"):
988 return "tarfile";
989 if(compresscheck=="zipfile"):
990 return "zipfile";
991 if(hasattr(infile, "read") or hasattr(infile, "write")):
992 catfp = UncompressArchiveFile(infile, formatspecs[2]);
993 else:
994 try:
995 if(compresscheck=="gzip"):
996 try:
997 import gzip;
998 except ImportError:
999 return False;
1000 catfp = gzip.GzipFile(infile, "rb");
1001 if(compresscheck=="bzip2"):
1002 try:
1003 import bz2;
1004 except ImportError:
1005 return False;
1006 catfp = bz2.BZ2File(infile, "rb");
1007 if(compresscheck=="lz4"):
1008 try:
1009 import lz4.frame;
1010 except ImportError:
1011 return False;
1012 catfp = lz4.frame.open(infile, "rb");
1013 if(compresscheck=="zstd"):
1014 try:
1015 import zstandard;
1016 except ImportError:
1017 return False;
1018 catfp = zstandard.open(infile, "rb");
1019 if(compresscheck=="lzma" or compresscheck=="xz"):
1020 try:
1021 import lzma;
1022 except ImportError:
1023 return False;
1024 catfp = lzma.open(infile, "rb");
1025 except FileNotFoundError:
1026 return False;
1027 filetype = False;
1028 prefp = catfp.read(5);
1029 if(prefp==binascii.unhexlify("7573746172")):
1030 filetype = "tarfile";
1031 catfp.seek(0, 0);
1032 prefp = catfp.read(7);
1033 if(prefp==binascii.unhexlify("43617446696c65")):
1034 filetype = "catfile";
1035 catfp.seek(0, 0);
1036 prefp = catfp.read(formatspecs[3]);
1037 if(prefp==binascii.unhexlify(formatspecs[4])):
1038 filetype = formatspecs[2];
1039 catfp.seek(0, 0);
1040 prefp = catfp.read(10);
1041 if(prefp==binascii.unhexlify("7061785f676c6f62616c")):
1042 filetype = "tarfile";
1043 catfp.seek(0, 0);
1044 catfp.close();
1045 return filetype;
1047 def GZipCompress(data, compresslevel=9):
1048 try:
1049 import gzip;
1050 except ImportError:
1051 return False;
1052 tmpfp = tempfile.NamedTemporaryFile("wb", delete=False);
1053 tmpfp.close();
1054 tmpfp = gzip.GzipFile(tmpfp.name, mode="wb", compresslevel=compresslevel);
1055 tmpfp.write(data);
1056 tmpfp.close();
1057 try:
1058 catfp = open(tmpfp.name, "rb");
1059 except FileNotFoundError:
1060 return False;
1061 catdata = catfp.read();
1062 catfp.close();
1063 return catdata;
1065 def CompressArchiveFile(fp, compression="auto", compressionlevel=None, formatspecs=__file_format_list__):
1066 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1067 if(not hasattr(fp, "read") and not hasattr(fp, "write")):
1068 return False;
1069 fp.seek(0, 0);
1070 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
1071 compression = None;
1072 if(compression not in compressionlist and compression is None):
1073 compression = "auto";
1074 if(compression=="gzip"):
1075 try:
1076 import gzip;
1077 except ImportError:
1078 return False;
1079 catfp = BytesIO();
1080 if(compressionlevel is None):
1081 compressionlevel = 9;
1082 else:
1083 compressionlevel = int(compressionlevel);
1084 catfp.write(gzip.compress(fp.read(), compresslevel=compressionlevel));
1085 if(compression=="bzip2"):
1086 try:
1087 import bz2;
1088 except ImportError:
1089 return False;
1090 catfp = BytesIO();
1091 if(compressionlevel is None):
1092 compressionlevel = 9;
1093 else:
1094 compressionlevel = int(compressionlevel);
1095 catfp.write(bz2.compress(fp.read(), compresslevel=compressionlevel));
1096 if(compression=="lz4"):
1097 try:
1098 import lz4.frame;
1099 except ImportError:
1100 return False;
1101 catfp = BytesIO();
1102 if(compressionlevel is None):
1103 compressionlevel = 9;
1104 else:
1105 compressionlevel = int(compressionlevel);
1106 catfp.write(lz4.frame.compress(fp.read(), compression_level=compressionlevel));
1107 if(compression=="lzo" or compression=="lzop"):
1108 try:
1109 import lzo;
1110 except ImportError:
1111 return False;
1112 catfp = BytesIO();
1113 if(compressionlevel is None):
1114 compressionlevel = 9;
1115 else:
1116 compressionlevel = int(compressionlevel);
1117 catfp.write(lzo.compress(fp.read(), compresslevel=compressionlevel));
1118 if(compression=="zstd"):
1119 try:
1120 import zstandard;
1121 except ImportError:
1122 return False;
1123 catfp = BytesIO();
1124 if(compressionlevel is None):
1125 compressionlevel = 10;
1126 else:
1127 compressionlevel = int(compressionlevel);
1128 catfp.write(zstandard.compress(fp.read(), level=compressionlevel));
1129 if(compression=="lzma"):
1130 try:
1131 import lzma;
1132 except ImportError:
1133 return False;
1134 catfp = BytesIO();
1135 if(compressionlevel is None):
1136 compressionlevel = 9;
1137 else:
1138 compressionlevel = int(compressionlevel);
1139 catfp.write(lzma.compress(fp.read(), format=lzma.FORMAT_ALONE, filters=[{"id": lzma.FILTER_LZMA1, "preset": compressionlevel}]));
1140 if(compression=="xz"):
1141 try:
1142 import lzma;
1143 except ImportError:
1144 return False;
1145 catfp = BytesIO();
1146 if(compressionlevel is None):
1147 compressionlevel = 9;
1148 else:
1149 compressionlevel = int(compressionlevel);
1150 catfp.write(lzma.compress(fp.read(), format=lzma.FORMAT_XZ, filters=[{"id": lzma.FILTER_LZMA2, "preset": compressionlevel}]));
1151 if(compression=="auto" or compression is None):
1152 catfp = fp;
1153 catfp.seek(0, 0);
1154 return catfp;
1156 create_alias_function("Compress", __file_format_name__, "", CompressArchiveFile);
1158 def CompressOpenFile(outfile, compressionlevel=None):
1159 if(outfile is None):
1160 return False;
1161 fbasename = os.path.splitext(outfile)[0];
1162 fextname = os.path.splitext(outfile)[1];
1163 if(compressionlevel is None and fextname!=".zst"):
1164 compressionlevel = 9;
1165 elif(compressionlevel is None and fextname==".zst"):
1166 compressionlevel = 10;
1167 else:
1168 compressionlevel = int(compressionlevel);
1169 if(sys.version_info[0]==2):
1170 mode = "w";
1171 else:
1172 mode = "wb";
1173 try:
1174 if(fextname not in outextlistwd):
1175 try:
1176 outfp = open(outfile, "wb", encoding="UTF-8");
1177 except (ValueError, TypeError) as e:
1178 outfp = open(outfile, "wb");
1179 elif(fextname==".gz"):
1180 try:
1181 import gzip;
1182 except ImportError:
1183 return False;
1184 try:
1185 outfp = gzip.open(outfile, mode, compressionlevel, encoding="UTF-8");
1186 except (ValueError, TypeError) as e:
1187 outfp = gzip.open(outfile, mode, compressionlevel);
1188 elif(fextname==".bz2"):
1189 try:
1190 import bz2;
1191 except ImportError:
1192 return False;
1193 try:
1194 outfp = bz2.open(outfile, mode, compressionlevel, encoding="UTF-8");
1195 except (ValueError, TypeError) as e:
1196 outfp = bz2.open(outfile, mode, compressionlevel);
1197 elif(fextname==".zst"):
1198 try:
1199 import zstandard;
1200 except ImportError:
1201 return False;
1202 try:
1203 outfp = zstandard.open(outfile, mode, zstandard.ZstdCompressor(level=compressionlevel), encoding="UTF-8");
1204 except (ValueError, TypeError) as e:
1205 outfp = zstandard.open(outfile, mode, zstandard.ZstdCompressor(level=compressionlevel));
1206 elif(fextname==".xz"):
1207 try:
1208 import lzma;
1209 except ImportError:
1210 return False;
1211 try:
1212 outfp = lzma.open(outfile, mode, format=lzma.FORMAT_XZ, filters=[{"id": lzma.FILTER_LZMA2, "preset": compressionlevel}], encoding="UTF-8");
1213 except (ValueError, TypeError) as e:
1214 outfp = lzma.open(outfile, mode, format=lzma.FORMAT_XZ, filters=[{"id": lzma.FILTER_LZMA2, "preset": compressionlevel}]);
1215 elif(fextname==".lz4"):
1216 try:
1217 import lz4.frame;
1218 except ImportError:
1219 return False;
1220 try:
1221 outfp = lz4.frame.open(outfile, mode, compression_level=compressionlevel, encoding="UTF-8");
1222 except (ValueError, TypeError) as e:
1223 outfp = lz4.frame.open(outfile, mode, compression_level=compressionlevel);
1224 elif(fextname==".lzo"):
1225 try:
1226 import lzo;
1227 except ImportError:
1228 return False;
1229 try:
1230 outfp = lzo.open(outfile, mode, compresslevel=compressionlevel, encoding="UTF-8");
1231 except (ValueError, TypeError) as e:
1232 outfp = lzo.open(outfile, mode, compresslevel=compressionlevel);
1233 elif(fextname==".lzma"):
1234 try:
1235 import lzma;
1236 except ImportError:
1237 return False;
1238 try:
1239 outfp = lzma.open(outfile, mode, format=lzma.FORMAT_ALONE, filters=[{"id": lzma.FILTER_LZMA1, "preset": compressionlevel}], encoding="UTF-8");
1240 except (ValueError, TypeError) as e:
1241 outfp = lzma.open(outfile, mode, format=lzma.FORMAT_ALONE, filters=[{"id": lzma.FILTER_LZMA1, "preset": compressionlevel}]);
1242 except FileNotFoundError:
1243 return False;
1244 return outfp;
1246 def GetDevMajorMinor(fdev):
1247 retdev = [];
1248 if(hasattr(os, "minor")):
1249 retdev.append(os.minor(fdev));
1250 else:
1251 retdev.append(0);
1252 if(hasattr(os, "major")):
1253 retdev.append(os.major(fdev));
1254 else:
1255 retdev.append(0);
1256 return retdev;
1258 def CheckSumSupport(checkfor, guaranteed=True):
1259 if(guaranteed):
1260 hash_list = sorted(list(hashlib.algorithms_guaranteed));
1261 else:
1262 hash_list = sorted(list(hashlib.algorithms_available));
1263 checklistout = sorted(hash_list + ['adler32', 'crc16', 'crc16_ansi', 'crc16_ibm', 'crc16_ccitt', 'crc32', 'crc64', 'crc64_ecma', 'crc64_iso', 'none']);
1264 if(checkfor in checklistout):
1265 return True;
1266 else:
1267 return False;
1269 def CheckSumSupportAlt(checkfor, guaranteed=True):
1270 if(guaranteed):
1271 hash_list = sorted(list(hashlib.algorithms_guaranteed));
1272 else:
1273 hash_list = sorted(list(hashlib.algorithms_available));
1274 checklistout = hash_list;
1275 if(checkfor in checklistout):
1276 return True;
1277 else:
1278 return False;
1280 def PackArchiveFile(infiles, outfile, dirlistfromtxt=False, compression="auto", compressionlevel=None, followlink=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
1281 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1282 outextlist = ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1283 outextlistwd = ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1284 advancedlist = True;
1285 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
1286 outfile = RemoveWindowsPath(outfile);
1287 checksumtype = checksumtype.lower();
1288 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
1289 checksumtype="crc32";
1290 if(checksumtype=="none"):
1291 checksumtype = "";
1292 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
1293 compression = None;
1294 if(compression not in compressionlist and compression is None):
1295 compression = "auto";
1296 if(verbose):
1297 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
1298 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
1299 if(os.path.exists(outfile)):
1300 os.unlink(outfile);
1301 if(outfile=="-"):
1302 verbose = False;
1303 catfp = BytesIO();
1304 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
1305 catfp = outfile;
1306 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
1307 catfp = BytesIO();
1308 else:
1309 fbasename = os.path.splitext(outfile)[0];
1310 fextname = os.path.splitext(outfile)[1];
1311 catfp = CompressOpenFile(outfile, compressionlevel);
1312 catver = formatspecs[6];
1313 fileheaderver = str(int(catver.replace(".", "")));
1314 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
1315 catfp.write(fileheader.encode('UTF-8'));
1316 infilelist = [];
1317 if(infiles=="-"):
1318 for line in sys.stdin:
1319 infilelist.append(line.strip());
1320 infilelist = list(filter(None, infilelist));
1321 elif(infiles!="-" and dirlistfromtxt and os.path.exists(infiles) and (os.path.isfile(infiles) or infiles=="/dev/null" or infiles=="NUL")):
1322 if(not os.path.exists(infiles) or not os.path.isfile(infiles)):
1323 return False;
1324 with open(infiles, "r") as finfile:
1325 for line in finfile:
1326 infilelist.append(line.strip());
1327 infilelist = list(filter(None, infilelist));
1328 else:
1329 if(isinstance(infiles, (list, tuple, ))):
1330 infilelist = list(filter(None, infiles));
1331 elif(isinstance(infiles, (str, ))):
1332 infilelist = list(filter(None, [infiles]));
1333 if(advancedlist):
1334 GetDirList = ListDirAdvanced(infilelist, followlink, False);
1335 else:
1336 GetDirList = ListDir(infilelist, followlink, False);
1337 if(not GetDirList):
1338 return False;
1339 curinode = 0;
1340 curfid = 0;
1341 inodelist = [];
1342 inodetofile = {};
1343 filetoinode = {};
1344 inodetocatinode = {};
1345 fnumfiles = format(int(len(GetDirList)), 'x').lower();
1346 fnumfilesa = AppendNullBytes([fnumfiles, checksumtype], formatspecs[5]);
1347 if(checksumtype=="none" or checksumtype==""):
1348 catfileheadercshex = format(0, 'x').lower();
1349 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
1350 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
1351 elif(checksumtype=="crc16_ccitt"):
1352 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
1353 elif(checksumtype=="adler32"):
1354 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
1355 elif(checksumtype=="crc32"):
1356 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
1357 elif(checksumtype=="crc64_ecma"):
1358 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1359 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
1360 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1361 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
1362 checksumoutstr = hashlib.new(checksumtype);
1363 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
1364 catfileheadercshex = checksumoutstr.hexdigest().lower();
1365 else:
1366 catfileheadercshex = format(0, 'x').lower
1367 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[5]);
1368 catfp.write(fnumfilesa.encode('UTF-8'));
1369 try:
1370 catfp.flush();
1371 os.fsync(catfp.fileno());
1372 except io.UnsupportedOperation:
1373 pass;
1374 except AttributeError:
1375 pass;
1376 for curfname in GetDirList:
1377 catfhstart = catfp.tell();
1378 if(re.findall("^[.|/]", curfname)):
1379 fname = curfname;
1380 else:
1381 fname = "./"+curfname;
1382 if(verbose):
1383 VerbosePrintOut(fname);
1384 if(not followlink or followlink is None):
1385 fstatinfo = os.lstat(fname);
1386 else:
1387 fstatinfo = os.stat(fname);
1388 fpremode = fstatinfo.st_mode;
1389 finode = fstatinfo.st_ino;
1390 flinkcount = fstatinfo.st_nlink;
1391 ftype = 0;
1392 if(stat.S_ISREG(fpremode)):
1393 ftype = 0;
1394 elif(stat.S_ISLNK(fpremode)):
1395 ftype = 2;
1396 elif(stat.S_ISCHR(fpremode)):
1397 ftype = 3;
1398 elif(stat.S_ISBLK(fpremode)):
1399 ftype = 4;
1400 elif(stat.S_ISDIR(fpremode)):
1401 ftype = 5;
1402 elif(stat.S_ISFIFO(fpremode)):
1403 ftype = 6;
1404 elif(hasattr(stat, "S_ISDOOR") and stat.S_ISDOOR(fpremode)):
1405 ftype = 8;
1406 elif(hasattr(stat, "S_ISPORT") and stat.S_ISPORT(fpremode)):
1407 ftype = 9;
1408 elif(hasattr(stat, "S_ISWHT") and stat.S_ISWHT(fpremode)):
1409 ftype = 10;
1410 else:
1411 ftype = 0;
1412 flinkname = "";
1413 fcurfid = format(int(curfid), 'x').lower();
1414 if(not followlink and finode!=0):
1415 if(ftype!=1):
1416 if(finode in inodelist):
1417 ftype = 1;
1418 flinkname = inodetofile[finode];
1419 fcurinode = format(int(inodetocatinode[finode]), 'x').lower();
1420 if(finode not in inodelist):
1421 inodelist.append(finode);
1422 inodetofile.update({finode: fname});
1423 inodetocatinode.update({finode: curinode});
1424 fcurinode = format(int(curinode), 'x').lower();
1425 curinode = curinode + 1;
1426 else:
1427 fcurinode = format(int(curinode), 'x').lower();
1428 curinode = curinode + 1;
1429 curfid = curfid + 1;
1430 if(ftype==2):
1431 flinkname = os.readlink(fname);
1432 fdev = fstatinfo.st_dev;
1433 getfdev = GetDevMajorMinor(fdev);
1434 fdev_minor = getfdev[0];
1435 fdev_major = getfdev[1];
1436 frdev = fstatinfo.st_dev;
1437 if(hasattr(fstatinfo, "st_rdev")):
1438 frdev = fstatinfo.st_rdev;
1439 else:
1440 frdev = fstatinfo.st_dev;
1441 getfrdev = GetDevMajorMinor(frdev);
1442 frdev_minor = getfrdev[0];
1443 frdev_major = getfrdev[1];
1444 if(ftype==1 or ftype==2 or ftype==3 or ftype==4 or ftype==5 or ftype==6):
1445 fsize = format(int("0"), 'x').lower();
1446 elif(ftype==0 or ftype==7):
1447 fsize = format(int(fstatinfo.st_size), 'x').lower();
1448 else:
1449 fsize = format(int(fstatinfo.st_size)).lower();
1450 fatime = format(int(fstatinfo.st_atime), 'x').lower();
1451 fmtime = format(int(fstatinfo.st_mtime), 'x').lower();
1452 fctime = format(int(fstatinfo.st_ctime), 'x').lower();
1453 if(hasattr(fstatinfo, "st_birthtime")):
1454 fbtime = format(int(fstatinfo.st_birthtime), 'x').lower();
1455 else:
1456 fbtime = format(int(fstatinfo.st_ctime), 'x').lower();
1457 fmode = format(int(fstatinfo.st_mode), 'x').lower();
1458 fchmode = format(int(stat.S_IMODE(fstatinfo.st_mode)), 'x').lower();
1459 ftypemod = format(int(stat.S_IFMT(fstatinfo.st_mode)), 'x').lower();
1460 fuid = format(int(fstatinfo.st_uid), 'x').lower();
1461 fgid = format(int(fstatinfo.st_gid), 'x').lower();
1462 funame = "";
1463 try:
1464 import pwd;
1465 try:
1466 userinfo = pwd.getpwuid(fstatinfo.st_uid);
1467 funame = userinfo.pw_name;
1468 except KeyError:
1469 funame = "";
1470 except ImportError:
1471 funame = "";
1472 fgname = "";
1473 try:
1474 import grp;
1475 try:
1476 groupinfo = grp.getgrgid(fstatinfo.st_gid);
1477 fgname = groupinfo.gr_name;
1478 except KeyError:
1479 fgname = "";
1480 except ImportError:
1481 fgname = "";
1482 fdev_minor = format(int(fdev_minor), 'x').lower();
1483 fdev_major = format(int(fdev_major), 'x').lower();
1484 frdev_minor = format(int(frdev_minor), 'x').lower();
1485 frdev_major = format(int(frdev_major), 'x').lower();
1486 finode = format(int(finode), 'x').lower();
1487 flinkcount = format(int(flinkcount), 'x').lower();
1488 if(hasattr(fstatinfo, "st_file_attributes")):
1489 fwinattributes = format(int(fstatinfo.st_file_attributes), 'x').lower();
1490 else:
1491 fwinattributes = format(int(0), 'x').lower();
1492 fcontents = "".encode('UTF-8');
1493 chunk_size = 1024;
1494 if(ftype == 0 or ftype == 7):
1495 with open(fname, "rb") as fpc:
1496 while(True):
1497 chunk = fpc.read(chunk_size);
1498 if(not chunk):
1499 break
1500 fcontents += chunk;
1501 if(followlink and (ftype == 1 or ftype == 2)):
1502 flstatinfo = os.stat(flinkname);
1503 with open(flinkname, "rb") as fpc:
1504 while(True):
1505 chunk = fpc.read(chunk_size);
1506 if(not chunk):
1507 break;
1508 fcontents += chunk;
1509 ftypehex = format(ftype, 'x').lower();
1510 extrafields = format(len(extradata), 'x').lower();
1511 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
1512 if(len(extradata)>0):
1513 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
1514 extrasizelen = format(len(extrasizestr), 'x').lower();
1515 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields], formatspecs[5]);
1516 if(len(extradata)>0):
1517 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
1518 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
1519 if(checksumtype=="none" or checksumtype==""):
1520 catfileheadercshex = format(0, 'x').lower();
1521 catfilecontentcshex = format(0, 'x').lower();
1522 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
1523 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
1524 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
1525 elif(checksumtype=="crc16_ccitt"):
1526 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
1527 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
1528 elif(checksumtype=="adler32"):
1529 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1530 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
1531 elif(checksumtype=="crc32"):
1532 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1533 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
1534 elif(checksumtype=="crc64_ecma"):
1535 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1536 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
1537 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
1538 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1539 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
1540 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
1541 checksumoutstr = hashlib.new(checksumtype);
1542 checksumoutstr.update("".encode('UTF-8'));
1543 catfileheadercshex = checksumoutstr.hexdigest().lower();
1544 checksumoutstr = hashlib.new(checksumtype);
1545 checksumoutstr.update(fcontents);
1546 catfilecontentcshex = checksumoutstr.hexdigest().lower();
1547 else:
1548 catfileheadercshex = format(0, 'x').lower();
1549 catfilecontentcshex = format(0, 'x').lower();
1550 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
1551 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
1552 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
1553 if(checksumtype=="none" or checksumtype==""):
1554 catfileheadercshex = format(0, 'x').lower();
1555 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
1556 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
1557 elif(checksumtype=="crc16_ccitt"):
1558 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
1559 elif(checksumtype=="adler32"):
1560 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
1561 elif(checksumtype=="crc32"):
1562 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
1563 elif(checksumtype=="crc64_ecma"):
1564 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1565 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
1566 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1567 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
1568 checksumoutstr = hashlib.new(checksumtype);
1569 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
1570 catfileheadercshex = checksumoutstr.hexdigest().lower();
1571 else:
1572 catfileheadercshex = format(0, 'x').lower();
1573 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
1574 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
1575 catfcontentstart = catfp.tell() + len(catfileoutstr);
1576 catfileoutstrecd = catfileoutstr.encode('UTF-8');
1577 nullstrecd = formatspecs[5].encode('UTF-8');
1578 catfileout = catfileoutstrecd + fcontents + nullstrecd;
1579 catfcontentend = (catfp.tell() - 1) + len(catfileout);
1580 catfp.write(catfileout);
1581 try:
1582 catfp.flush();
1583 os.fsync(catfp.fileno());
1584 except io.UnsupportedOperation:
1585 pass;
1586 except AttributeError:
1587 pass;
1588 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
1589 catfp = CompressArchiveFile(catfp, compression, formatspecs);
1590 try:
1591 catfp.flush();
1592 os.fsync(catfp.fileno());
1593 except io.UnsupportedOperation:
1594 pass;
1595 except AttributeError:
1596 pass;
1597 if(outfile=="-"):
1598 catfp.seek(0, 0);
1599 if(hasattr(sys.stdout, "buffer")):
1600 shutil.copyfileobj(catfp, sys.stdout.buffer);
1601 else:
1602 shutil.copyfileobj(catfp, sys.stdout);
1603 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
1604 catfp = CompressArchiveFile(catfp, compression, formatspecs);
1605 catfp.seek(0, 0);
1606 upload_file_to_internet_file(catfp, outfile);
1607 if(returnfp):
1608 catfp.seek(0, 0);
1609 return catfp;
1610 else:
1611 catfp.close();
1612 return True;
1614 create_alias_function("Pack", __file_format_name__, "", PackArchiveFile);
1616 if(hasattr(shutil, "register_archive_format")):
1617 def PackArchiveFileFunc(archive_name, source_dir, **kwargs):
1618 return PackArchiveFile(source_dir, archive_name, False, "auto", None, False, "crc32", [], __file_format_delimiter__, False, False);
1619 create_alias_function("Pack", __file_format_name__, "Func", PackArchiveFileFunc);
1621 def PackArchiveFileFromDirList(infiles, outfile, dirlistfromtxt=False, compression="auto", compressionlevel=None, followlink=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
1622 return PackArchiveFile(infiles, outfile, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, extradata, formatspecs, verbose, returnfp);
1624 def PackArchiveFileFromTarFile(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
1625 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1626 outextlist = ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1627 outextlistwd = ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1628 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
1629 outfile = RemoveWindowsPath(outfile);
1630 checksumtype = checksumtype.lower();
1631 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
1632 checksumtype="crc32";
1633 if(checksumtype=="none"):
1634 checksumtype = "";
1635 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
1636 compression = None;
1637 if(compression not in compressionlist and compression is None):
1638 compression = "auto";
1639 if(verbose):
1640 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
1641 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
1642 if(os.path.exists(outfile)):
1643 os.unlink(outfile);
1644 if(outfile=="-"):
1645 verbose = False;
1646 catfp = BytesIO();
1647 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
1648 catfp = outfile;
1649 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
1650 catfp = BytesIO();
1651 else:
1652 fbasename = os.path.splitext(outfile)[0];
1653 fextname = os.path.splitext(outfile)[1];
1654 catfp = CompressOpenFile(outfile, compressionlevel);
1655 catver = formatspecs[6];
1656 fileheaderver = str(int(catver.replace(".", "")));
1657 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
1658 catfp.write(fileheader.encode('UTF-8'));
1659 curinode = 0;
1660 curfid = 0;
1661 inodelist = [];
1662 inodetofile = {};
1663 filetoinode = {};
1664 inodetocatinode = {};
1665 if(infile=="-"):
1666 infile = BytesIO();
1667 if(hasattr(sys.stdin, "buffer")):
1668 shutil.copyfileobj(sys.stdin.buffer, infile);
1669 else:
1670 shutil.copyfileobj(sys.stdin, infile);
1671 infile.seek(0, 0);
1672 if(not infile):
1673 return False;
1674 infile.seek(0, 0);
1675 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
1676 infile = download_file_from_internet_file(infile);
1677 infile.seek(0, 0);
1678 if(not infile):
1679 return False;
1680 infile.seek(0, 0);
1681 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
1682 return False;
1683 elif(os.path.exists(infile) and os.path.isfile(infile)):
1684 try:
1685 if(not tarfile.is_tarfile(infile)):
1686 return False;
1687 except AttributeError:
1688 if(not is_tarfile(infile)):
1689 return False;
1690 else:
1691 return False;
1692 try:
1693 if(hasattr(infile, "read") or hasattr(infile, "write")):
1694 tarfp = tarfile.open(fileobj=infile, mode="r");
1695 else:
1696 tarfp = tarfile.open(infile, "r");
1697 except FileNotFoundError:
1698 return False;
1699 fnumfiles = format(int(len(tarfp.getmembers())), 'x').lower();
1700 fnumfilesa = AppendNullBytes([fnumfiles, checksumtype], formatspecs[5]);
1701 if(checksumtype=="none" or checksumtype==""):
1702 catfileheadercshex = format(0, 'x').lower();
1703 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
1704 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
1705 elif(checksumtype=="crc16_ccitt"):
1706 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
1707 elif(checksumtype=="adler32"):
1708 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
1709 elif(checksumtype=="crc32"):
1710 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
1711 elif(checksumtype=="crc64_ecma"):
1712 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1713 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
1714 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1715 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
1716 checksumoutstr = hashlib.new(checksumtype);
1717 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
1718 catfileheadercshex = checksumoutstr.hexdigest().lower();
1719 else:
1720 catfileheadercshex = format(0, 'x').lower();
1721 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[5]);
1722 catfp.write(fnumfilesa.encode('UTF-8'));
1723 try:
1724 catfp.flush();
1725 os.fsync(catfp.fileno());
1726 except io.UnsupportedOperation:
1727 pass;
1728 except AttributeError:
1729 pass;
1730 for member in sorted(tarfp.getmembers(), key=lambda x: x.name):
1731 catfhstart = catfp.tell();
1732 if(re.findall("^[.|/]", member.name)):
1733 fname = member.name;
1734 else:
1735 fname = "./"+member.name;
1736 if(verbose):
1737 VerbosePrintOut(fname);
1738 fpremode = member.mode;
1739 ffullmode = member.mode;
1740 flinkcount = 0;
1741 ftype = 0;
1742 if(member.isreg()):
1743 ffullmode = member.mode + stat.S_IFREG;
1744 ftype = 0;
1745 elif(member.isdev()):
1746 ffullmode = member.mode;
1747 ftype = 7;
1748 elif(member.islnk()):
1749 ffullmode = member.mode + stat.S_IFREG;
1750 ftype = 1;
1751 elif(member.issym()):
1752 ffullmode = member.mode + stat.S_IFLNK;
1753 ftype = 2;
1754 elif(member.ischr()):
1755 ffullmode = member.mode + stat.S_IFCHR;
1756 ftype = 3;
1757 elif(member.isblk()):
1758 ffullmode = member.mode + stat.S_IFBLK;
1759 ftype = 4;
1760 elif(member.isdir()):
1761 ffullmode = member.mode + stat.S_IFDIR;
1762 ftype = 5;
1763 elif(member.isfifo()):
1764 ffullmode = member.mode + stat.S_IFIFO;
1765 ftype = 6;
1766 elif(member.issparse()):
1767 ffullmode = member.mode;
1768 ftype = 11;
1769 else:
1770 ffullmode = member.mode;
1771 ftype = 0;
1772 flinkname = "";
1773 fcurfid = format(int(curfid), 'x').lower();
1774 fcurinode = format(int(0), 'x').lower();
1775 curfid = curfid + 1;
1776 if(ftype==2):
1777 flinkname = member.linkname;
1778 fdev_minor = format(int(member.devminor), 'x').lower();
1779 fdev_major = format(int(member.devmajor), 'x').lower();
1780 frdev_minor = format(int(member.devminor), 'x').lower();
1781 frdev_major = format(int(member.devmajor), 'x').lower();
1782 if(ftype==1 or ftype==2 or ftype==3 or ftype==4 or ftype==5 or ftype==6):
1783 fsize = format(int("0"), 'x').lower();
1784 elif(ftype==0 or ftype==7):
1785 fsize = format(int(member.size), 'x').lower();
1786 else:
1787 fsize = format(int(member.size), 'x').lower();
1788 fatime = format(int(member.mtime), 'x').lower();
1789 fmtime = format(int(member.mtime), 'x').lower();
1790 fctime = format(int(member.mtime), 'x').lower();
1791 fbtime = format(int(member.mtime), 'x').lower();
1792 fmode = format(int(ffullmode), 'x').lower();
1793 fchmode = format(int(stat.S_IMODE(ffullmode)), 'x').lower();
1794 ftypemod = format(int(stat.S_IFMT(ffullmode)), 'x').lower();
1795 fuid = format(int(member.uid), 'x').lower();
1796 fgid = format(int(member.gid), 'x').lower();
1797 funame = member.uname;
1798 fgname = member.gname;
1799 flinkcount = format(int(flinkcount), 'x').lower();
1800 fcontents = "".encode('UTF-8');
1801 chunk_size = 1024;
1802 if(ftype == 0 or ftype == 7):
1803 with tarfp.extractfile(member) as fpc:
1804 while(True):
1805 chunk = fpc.read(chunk_size);
1806 if(not chunk):
1807 break
1808 fcontents += chunk;
1809 ftypehex = format(ftype, 'x').lower();
1810 extrafields = format(len(extradata), 'x').lower();
1811 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
1812 if(len(extradata)>0):
1813 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
1814 extrasizelen = format(len(extrasizestr), 'x').lower();
1815 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields], formatspecs[5]);
1816 if(len(extradata)>0):
1817 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
1818 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
1819 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
1820 catfcontentstart = catfp.tell() + len(catfileoutstr);
1821 if(checksumtype=="none" or checksumtype==""):
1822 catfileheadercshex = format(0, 'x').lower();
1823 catfilecontentcshex = format(0, 'x').lower();
1824 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
1825 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
1826 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
1827 elif(checksumtype=="crc16_ccitt"):
1828 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
1829 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
1830 elif(checksumtype=="adler32"):
1831 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1832 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
1833 elif(checksumtype=="crc32"):
1834 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
1835 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
1836 elif(checksumtype=="crc64_ecma"):
1837 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1838 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
1839 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
1840 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1841 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
1842 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
1843 checksumoutstr = hashlib.new(checksumtype);
1844 checksumoutstr.update("".encode('UTF-8'));
1845 catfileheadercshex = checksumoutstr.hexdigest().lower();
1846 checksumoutstr = hashlib.new(checksumtype);
1847 checksumoutstr.update(fcontents);
1848 catfilecontentcshex = checksumoutstr.hexdigest().lower();
1849 else:
1850 catfileheadercshex = format(0, 'x').lower();
1851 catfilecontentcshex = format(0, 'x').lower();
1852 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
1853 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
1854 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
1855 if(checksumtype=="none" or checksumtype==""):
1856 catfileheadercshex = format(0, 'x').lower();
1857 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
1858 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
1859 elif(checksumtype=="crc16_ccitt"):
1860 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
1861 elif(checksumtype=="adler32"):
1862 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
1863 elif(checksumtype=="crc32"):
1864 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
1865 elif(checksumtype=="crc64_ecma"):
1866 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1867 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
1868 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
1869 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
1870 checksumoutstr = hashlib.new(checksumtype);
1871 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
1872 catfileheadercshex = checksumoutstr.hexdigest().lower();
1873 else:
1874 catfileheadercshex = format(0, 'x').lower();
1875 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
1876 catfileoutstrecd = catfileoutstr.encode('UTF-8');
1877 nullstrecd = formatspecs[5].encode('UTF-8');
1878 catfileout = catfileoutstrecd + fcontents + nullstrecd;
1879 catfcontentend = (catfp.tell() - 1) + len(catfileout);
1880 catfp.write(catfileout);
1881 try:
1882 catfp.flush();
1883 os.fsync(catfp.fileno());
1884 except io.UnsupportedOperation:
1885 pass;
1886 except AttributeError:
1887 pass;
1888 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
1889 catfp = CompressArchiveFile(catfp, compression, formatspecs);
1890 try:
1891 catfp.flush();
1892 os.fsync(catfp.fileno());
1893 except io.UnsupportedOperation:
1894 pass;
1895 except AttributeError:
1896 pass;
1897 if(outfile=="-"):
1898 catfp.seek(0, 0);
1899 if(hasattr(sys.stdout, "buffer")):
1900 shutil.copyfileobj(catfp, sys.stdout.buffer);
1901 else:
1902 shutil.copyfileobj(catfp, sys.stdout);
1903 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
1904 catfp = CompressArchiveFile(catfp, compression, formatspecs);
1905 catfp.seek(0, 0);
1906 upload_file_from_internet_file(catfp, outfile);
1907 if(returnfp):
1908 catfp.seek(0, 0);
1909 return catfp;
1910 else:
1911 catfp.close();
1912 return True;
1914 create_alias_function("Pack", __file_format_name__, "FromTarFile", PackArchiveFileFromTarFile);
1916 def PackArchiveFileFromZipFile(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
1917 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1918 outextlist = ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
1919 outextlistwd = ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
1920 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
1921 outfile = RemoveWindowsPath(outfile);
1922 checksumtype = checksumtype.lower();
1923 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
1924 checksumtype="crc32";
1925 if(checksumtype=="none"):
1926 checksumtype = "";
1927 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
1928 compression = None;
1929 if(compression not in compressionlist and compression is None):
1930 compression = "auto";
1931 if(verbose):
1932 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
1933 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
1934 if(os.path.exists(outfile)):
1935 os.unlink(outfile);
1936 if(outfile=="-"):
1937 verbose = False;
1938 catfp = BytesIO();
1939 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
1940 catfp = outfile;
1941 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
1942 catfp = BytesIO();
1943 else:
1944 fbasename = os.path.splitext(outfile)[0];
1945 fextname = os.path.splitext(outfile)[1];
1946 catfp = CompressOpenFile(outfile, compressionlevel);
1947 catver = formatspecs[6];
1948 fileheaderver = str(int(catver.replace(".", "")));
1949 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
1950 catfp.write(fileheader.encode('UTF-8'));
1951 curinode = 0;
1952 curfid = 0;
1953 inodelist = [];
1954 inodetofile = {};
1955 filetoinode = {};
1956 inodetocatinode = {};
1957 if(infile=="-"):
1958 infile = BytesIO();
1959 if(hasattr(sys.stdin, "buffer")):
1960 shutil.copyfileobj(sys.stdin.buffer, infile);
1961 else:
1962 shutil.copyfileobj(sys.stdin, infile);
1963 infile.seek(0, 0);
1964 if(not infile):
1965 return False;
1966 infile.seek(0, 0);
1967 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
1968 infile = download_file_from_internet_file(infile);
1969 infile.seek(0, 0);
1970 if(not infile):
1971 return False;
1972 infile.seek(0, 0);
1973 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
1974 return False;
1975 else:
1976 return False;
1977 if(not zipfile.is_zipfile(infile)):
1978 return False;
1979 try:
1980 zipfp = zipfile.ZipFile(infile, "r", allowZip64=True);
1981 except FileNotFoundError:
1982 return False;
1983 ziptest = zipfp.testzip();
1984 if(ziptest):
1985 VerbosePrintOut("Bad file found!");
1986 fnumfiles = format(int(len(zipfp.infolist())), 'x').lower();
1987 fnumfilesa = AppendNullBytes([fnumfiles, checksumtype], formatspecs[5]);
1988 if(checksumtype=="none" or checksumtype==""):
1989 catfileheadercshex = format(0, 'x').lower();
1990 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
1991 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
1992 elif(checksumtype=="crc16_ccitt"):
1993 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
1994 elif(checksumtype=="adler32"):
1995 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
1996 elif(checksumtype=="crc32"):
1997 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
1998 elif(checksumtype=="crc64_ecma"):
1999 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2000 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2001 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2002 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2003 checksumoutstr = hashlib.new(checksumtype);
2004 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
2005 catfileheadercshex = checksumoutstr.hexdigest().lower();
2006 else:
2007 catfileheadercshex = format(0, 'x').lower();
2008 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[5]);
2009 catfp.write(fnumfilesa.encode('UTF-8'));
2010 try:
2011 catfp.flush();
2012 os.fsync(catfp.fileno());
2013 except io.UnsupportedOperation:
2014 pass;
2015 except AttributeError:
2016 pass;
2017 for member in sorted(zipfp.infolist(), key=lambda x: x.filename):
2018 catfhstart = catfp.tell();
2019 if(re.findall("^[.|/]", member.filename)):
2020 fname = member.filename;
2021 else:
2022 fname = "./"+member.filename;
2023 zipinfo = zipfp.getinfo(member.filename);
2024 if(verbose):
2025 VerbosePrintOut(fname);
2026 if(not member.is_dir()):
2027 fpremode = int(stat.S_IFREG + 438);
2028 elif(member.is_dir()):
2029 fpremode = int(stat.S_IFDIR + 511);
2030 flinkcount = 0;
2031 ftype = 0;
2032 if(not member.is_dir()):
2033 ftype = 0;
2034 elif(member.is_dir()):
2035 ftype = 5;
2036 flinkname = "";
2037 fcurfid = format(int(curfid), 'x').lower();
2038 fcurinode = format(int(0), 'x').lower();
2039 curfid = curfid + 1;
2040 fdev_minor = format(int(0), 'x').lower();
2041 fdev_major = format(int(0), 'x').lower();
2042 frdev_minor = format(int(0), 'x').lower();
2043 frdev_major = format(int(0), 'x').lower();
2044 if(ftype==5):
2045 fsize = format(int("0"), 'x').lower();
2046 elif(ftype==0):
2047 fsize = format(int(member.file_size), 'x').lower();
2048 else:
2049 fsize = format(int(member.file_size), 'x').lower();
2050 fatime = format(int(time.mktime(member.date_time + (0, 0, -1))), 'x').lower();
2051 fmtime = format(int(time.mktime(member.date_time + (0, 0, -1))), 'x').lower();
2052 fctime = format(int(time.mktime(member.date_time + (0, 0, -1))), 'x').lower();
2053 fbtime = format(int(time.mktime(member.date_time + (0, 0, -1))), 'x').lower();
2054 if(not member.is_dir()):
2055 fmode = format(int(stat.S_IFREG + 438), 'x').lower();
2056 fchmode = format(int(stat.S_IMODE(int(stat.S_IFREG + 438))), 'x').lower();
2057 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFREG + 438))), 'x').lower();
2058 if(member.is_dir()):
2059 fmode = format(int(stat.S_IFDIR + 511), 'x').lower();
2060 fchmode = format(int(stat.S_IMODE(int(stat.S_IFDIR + 511))), 'x').lower();
2061 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFDIR + 511))), 'x').lower();
2062 try:
2063 fuid = format(int(os.getuid()), 'x').lower();
2064 except AttributeError:
2065 fuid = format(int(0), 'x').lower();
2066 except KeyError:
2067 fuid = format(int(0), 'x').lower();
2068 try:
2069 fgid = format(int(os.getgid()), 'x').lower();
2070 except AttributeError:
2071 fgid = format(int(0), 'x').lower();
2072 except KeyError:
2073 fgid = format(int(0), 'x').lower();
2074 try:
2075 import pwd;
2076 try:
2077 userinfo = pwd.getpwuid(os.getuid());
2078 funame = userinfo.pw_name;
2079 except KeyError:
2080 funame = "";
2081 except AttributeError:
2082 funame = "";
2083 except ImportError:
2084 funame = "";
2085 fgname = "";
2086 try:
2087 import grp;
2088 try:
2089 groupinfo = grp.getgrgid(os.getgid());
2090 fgname = groupinfo.gr_name;
2091 except KeyError:
2092 fgname = "";
2093 except AttributeError:
2094 fgname = "";
2095 except ImportError:
2096 fgname = "";
2097 fcontents = "".encode('UTF-8');
2098 if(ftype==0):
2099 fcontents = zipfp.read(member.filename);
2100 ftypehex = format(ftype, 'x').lower();
2101 extrafields = format(len(extradata), 'x').lower();
2102 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
2103 if(len(extradata)>0):
2104 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
2105 extrasizelen = format(len(extrasizestr), 'x').lower();
2106 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields], formatspecs[5]);
2107 if(len(extradata)>0):
2108 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
2109 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
2110 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
2111 catfcontentstart = catfp.tell() + len(catfileoutstr);
2112 if(checksumtype=="none" or checksumtype==""):
2113 catfileheadercshex = format(0, 'x').lower();
2114 catfilecontentcshex = format(0, 'x').lower();
2115 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2116 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2117 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
2118 elif(checksumtype=="crc16_ccitt"):
2119 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2120 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
2121 elif(checksumtype=="adler32"):
2122 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2123 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
2124 elif(checksumtype=="crc32"):
2125 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2126 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
2127 elif(checksumtype=="crc64_ecma"):
2128 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2129 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
2130 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2131 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2132 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
2133 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2134 checksumoutstr = hashlib.new(checksumtype);
2135 checksumoutstr.update("".encode('UTF-8'));
2136 catfileheadercshex = checksumoutstr.hexdigest().lower();
2137 checksumoutstr = hashlib.new(checksumtype);
2138 checksumoutstr.update(fcontents);
2139 catfilecontentcshex = checksumoutstr.hexdigest().lower();
2140 else:
2141 catfileheadercshex = format(0, 'x').lower();
2142 catfilecontentcshex = format(0, 'x').lower();
2143 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
2144 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
2145 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
2146 if(checksumtype=="none" or checksumtype==""):
2147 catfileheadercshex = format(0, 'x').lower();
2148 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2149 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
2150 elif(checksumtype=="crc16_ccitt"):
2151 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
2152 elif(checksumtype=="adler32"):
2153 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
2154 elif(checksumtype=="crc32"):
2155 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
2156 elif(checksumtype=="crc64_ecma"):
2157 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2158 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2159 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2160 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2161 checksumoutstr = hashlib.new(checksumtype);
2162 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
2163 catfileheadercshex = checksumoutstr.hexdigest().lower();
2164 else:
2165 catfileheadercshex = format(0, 'x').lower();
2166 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
2167 catfileoutstrecd = catfileoutstr.encode('UTF-8');
2168 nullstrecd = formatspecs[5].encode('UTF-8');
2169 catfileout = catfileoutstrecd + fcontents + nullstrecd;
2170 catfcontentend = (catfp.tell() - 1) + len(catfileout);
2171 catfp.write(catfileout);
2172 try:
2173 catfp.flush();
2174 os.fsync(catfp.fileno());
2175 except io.UnsupportedOperation:
2176 pass;
2177 except AttributeError:
2178 pass;
2179 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
2180 catfp = CompressArchiveFile(catfp, compression, formatspecs);
2181 try:
2182 catfp.flush();
2183 os.fsync(catfp.fileno());
2184 except io.UnsupportedOperation:
2185 pass;
2186 except AttributeError:
2187 pass;
2188 if(outfile=="-"):
2189 catfp.seek(0, 0);
2190 if(hasattr(sys.stdout, "buffer")):
2191 shutil.copyfileobj(catfp, sys.stdout.buffer);
2192 else:
2193 shutil.copyfileobj(catfp, sys.stdout);
2194 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
2195 catfp = CompressArchiveFile(catfp, compression, formatspecs);
2196 catfp.seek(0, 0);
2197 upload_file_to_internet_file(catfp, outfile);
2198 if(returnfp):
2199 catfp.seek(0, 0);
2200 return catfp;
2201 else:
2202 catfp.close();
2203 return True;
2205 create_alias_function("Pack", __file_format_name__, "FromZipFile", PackArchiveFileFromZipFile);
2207 if(not rarfile_support):
2208 def PackArchiveFileFromRarFile(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
2209 return False
2211 if(rarfile_support):
2212 def PackArchiveFileFromRarFile(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
2213 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2214 outextlist = ['gz', 'bz2', 'zst', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
2215 outextlistwd = ['.gz', '.bz2', '.zst', '.lz4', '.lzo', '.lzop', '.lzma', '.xz'];
2216 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
2217 outfile = RemoveWindowsPath(outfile);
2218 checksumtype = checksumtype.lower();
2219 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
2220 checksumtype="crc32";
2221 if(checksumtype=="none"):
2222 checksumtype = "";
2223 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
2224 compression = None;
2225 if(compression not in compressionlist and compression is None):
2226 compression = "auto";
2227 if(verbose):
2228 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
2229 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
2230 if(os.path.exists(outfile)):
2231 os.unlink(outfile);
2232 if(outfile=="-"):
2233 verbose = False;
2234 catfp = BytesIO();
2235 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
2236 catfp = outfile;
2237 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
2238 catfp = BytesIO();
2239 else:
2240 fbasename = os.path.splitext(outfile)[0];
2241 fextname = os.path.splitext(outfile)[1];
2242 catfp = CompressOpenFile(outfile, compressionlevel);
2243 catver = formatspecs[6];
2244 fileheaderver = str(int(catver.replace(".", "")));
2245 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
2246 catfp.write(fileheader.encode('UTF-8'));
2247 curinode = 0;
2248 curfid = 0;
2249 inodelist = [];
2250 inodetofile = {};
2251 filetoinode = {};
2252 inodetocatinode = {};
2253 if(not os.path.exists(infile) or not os.path.isfile(infile)):
2254 return False;
2255 if(not rarfile.is_rarfile(infile) and not rarfile.is_rarfile_sfx(infile)):
2256 return False;
2257 rarfp = rarfile.RarFile(infile, "r");
2258 rartest = rarfp.testrar();
2259 if(rartest):
2260 VerbosePrintOut("Bad file found!");
2261 fnumfiles = format(int(len(rarfp.infolist())), 'x').lower();
2262 fnumfilesa = AppendNullBytes([fnumfiles, checksumtype], formatspecs[5]);
2263 if(checksumtype=="none" or checksumtype==""):
2264 catfileheadercshex = format(0, 'x').lower();
2265 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2266 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
2267 elif(checksumtype=="crc16_ccitt"):
2268 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
2269 elif(checksumtype=="adler32"):
2270 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
2271 elif(checksumtype=="crc32"):
2272 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
2273 elif(checksumtype=="crc64_ecma"):
2274 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2275 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2276 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2277 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2278 checksumoutstr = hashlib.new(checksumtype);
2279 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
2280 catfileheadercshex = checksumoutstr.hexdigest().lower();
2281 else:
2282 catfileheadercshex = format(0, 'x').lower();
2283 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[5]);
2284 catfp.write(fnumfilesa.encode('UTF-8'));
2285 try:
2286 catfp.flush();
2287 os.fsync(catfp.fileno());
2288 except io.UnsupportedOperation:
2289 pass;
2290 except AttributeError:
2291 pass;
2292 for member in sorted(rarfp.infolist(), key=lambda x: x.filename):
2293 is_unix = False;
2294 is_windows = False;
2295 if(member.host_os == rarfile.RAR_OS_UNIX):
2296 is_windows = False;
2297 try:
2298 member.external_attr
2299 is_unix = True;
2300 except AttributeError:
2301 is_unix = False;
2302 elif(member.host_os == rarfile.RAR_OS_WIN32):
2303 is_unix = False;
2304 try:
2305 member.external_attr
2306 is_windows = True;
2307 except AttributeError:
2308 is_windows = False;
2309 else:
2310 is_unix = False;
2311 is_windows = False;
2312 catfhstart = catfp.tell();
2313 if(re.findall("^[.|/]", member.filename)):
2314 fname = member.filename;
2315 else:
2316 fname = "./"+member.filename;
2317 rarinfo = rarfp.getinfo(member.filename);
2318 if(verbose):
2319 VerbosePrintOut(fname);
2320 if(is_unix and member.external_attr !=0):
2321 fpremode = int(member.external_attr);
2322 elif(member.is_file()):
2323 fpremode = int(stat.S_IFREG + 438);
2324 elif(member.is_symlink()):
2325 fpremode = int(stat.S_IFLNK + 438);
2326 elif(member.is_dir()):
2327 fpremode = int(stat.S_IFDIR + 511);
2328 if(is_windows and member.external_attr !=0):
2329 fwinattributes = format(int(member.external_attr), 'x').lower();
2330 else:
2331 fwinattributes = format(int(0), 'x').lower();
2332 flinkcount = 0;
2333 ftype = 0;
2334 if(member.is_file()):
2335 ftype = 0;
2336 elif(member.is_symlink()):
2337 ftype = 2;
2338 elif(member.is_dir()):
2339 ftype = 5;
2340 flinkname = "";
2341 if(ftype==2):
2342 flinkname = rarfp.read(member.filename).decode("UTF-8");
2343 fcurfid = format(int(curfid), 'x').lower();
2344 fcurinode = format(int(0), 'x').lower();
2345 curfid = curfid + 1;
2346 fdev_minor = format(int(0), 'x').lower();
2347 fdev_major = format(int(0), 'x').lower();
2348 frdev_minor = format(int(0), 'x').lower();
2349 frdev_major = format(int(0), 'x').lower();
2350 if(ftype==5):
2351 fsize = format(int("0"), 'x').lower();
2352 elif(ftype==0):
2353 fsize = format(int(member.file_size), 'x').lower();
2354 else:
2355 fsize = format(int(member.file_size), 'x').lower();
2356 try:
2357 if(member.atime):
2358 fatime = format(int(member.atime.timestamp()), 'x').lower();
2359 else:
2360 fatime = format(int(member.mtime.timestamp()), 'x').lower();
2361 except AttributeError:
2362 fatime = format(int(member.mtime.timestamp()), 'x').lower();
2363 fmtime = format(int(member.mtime.timestamp()), 'x').lower();
2364 try:
2365 if(member.ctime):
2366 fctime = format(int(member.ctime.timestamp()), 'x').lower();
2367 else:
2368 fctime = format(int(member.mtime.timestamp()), 'x').lower();
2369 except AttributeError:
2370 fctime = format(int(member.mtime.timestamp()), 'x').lower();
2371 fbtime = format(int(member.mtime.timestamp()), 'x').lower();
2372 if(is_unix and member.external_attr !=0):
2373 fmode = format(int(member.external_attr), 'x').lower();
2374 fchmode = format(int(stat.S_IMODE(member.external_attr)), 'x').lower();
2375 ftypemod = format(int(stat.S_IFMT(member.external_attr)), 'x').lower();
2376 elif(member.is_file()):
2377 fmode = format(int(stat.S_IFREG + 438), 'x').lower();
2378 fchmode = format(int(stat.S_IMODE(int(stat.S_IFREG + 438))), 'x').lower();
2379 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFREG + 438))), 'x').lower();
2380 elif(member.is_symlink()):
2381 fmode = format(int(stat.S_IFLNK + 438), 'x').lower();
2382 fchmode = format(int(stat.S_IMODE(int(stat.S_IFREG + 438))), 'x').lower();
2383 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFREG + 438))), 'x').lower();
2384 elif(member.is_dir()):
2385 fmode = format(int(stat.S_IFDIR + 511), 'x').lower();
2386 fchmode = format(int(stat.S_IMODE(int(stat.S_IFDIR + 511))), 'x').lower();
2387 ftypemod = format(int(stat.S_IFMT(int(stat.S_IFDIR + 511))), 'x').lower();
2388 try:
2389 fuid = format(int(os.getuid()), 'x').lower();
2390 except AttributeError:
2391 fuid = format(int(0), 'x').lower();
2392 except KeyError:
2393 fuid = format(int(0), 'x').lower();
2394 try:
2395 fgid = format(int(os.getgid()), 'x').lower();
2396 except AttributeError:
2397 fgid = format(int(0), 'x').lower();
2398 except KeyError:
2399 fgid = format(int(0), 'x').lower();
2400 try:
2401 import pwd;
2402 try:
2403 userinfo = pwd.getpwuid(os.getuid());
2404 funame = userinfo.pw_name;
2405 except KeyError:
2406 funame = "";
2407 except AttributeError:
2408 funame = "";
2409 except ImportError:
2410 funame = "";
2411 fgname = "";
2412 try:
2413 import grp;
2414 try:
2415 groupinfo = grp.getgrgid(os.getgid());
2416 fgname = groupinfo.gr_name;
2417 except KeyError:
2418 fgname = "";
2419 except AttributeError:
2420 fgname = "";
2421 except ImportError:
2422 fgname = "";
2423 fcontents = "".encode('UTF-8');
2424 if(ftype==0):
2425 fcontents = rarfp.read(member.filename);
2426 ftypehex = format(ftype, 'x').lower();
2427 extrafields = format(len(extradata), 'x').lower();
2428 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
2429 if(len(extradata)>0):
2430 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
2431 extrasizelen = format(len(extrasizestr), 'x').lower();
2432 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields], formatspecs[5]);
2433 if(len(extradata)>0):
2434 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
2435 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
2436 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
2437 catfcontentstart = catfp.tell() + len(catfileoutstr);
2438 if(checksumtype=="none" or checksumtype==""):
2439 catfileheadercshex = format(0, 'x').lower();
2440 catfilecontentcshex = format(0, 'x').lower();
2441 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2442 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
2443 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
2444 elif(checksumtype=="crc16_ccitt"):
2445 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
2446 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
2447 elif(checksumtype=="adler32"):
2448 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2449 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
2450 elif(checksumtype=="crc32"):
2451 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
2452 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
2453 elif(checksumtype=="crc64_ecma"):
2454 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2455 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
2456 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2457 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2458 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
2459 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2460 checksumoutstr = hashlib.new(checksumtype);
2461 checksumoutstr.update("".encode('UTF-8'));
2462 catfileheadercshex = checksumoutstr.hexdigest().lower();
2463 checksumoutstr = hashlib.new(checksumtype);
2464 checksumoutstr.update(fcontents);
2465 catfilecontentcshex = checksumoutstr.hexdigest().lower();
2466 else:
2467 catfileheadercshex = format(0, 'x').lower();
2468 catfilecontentcshex = format(0, 'x').lower();
2469 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
2470 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
2471 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr
2472 if(checksumtype=="none" or checksumtype==""):
2473 catfileheadercshex = format(0, 'x').lower()
2474 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
2475 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower()
2476 elif(checksumtype=="crc16_ccitt"):
2477 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower()
2478 elif(checksumtype=="adler32"):
2479 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower()
2480 elif(checksumtype=="crc32"):
2481 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower()
2482 elif(checksumtype=="crc64_ecma"):
2483 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
2484 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
2485 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower()
2486 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
2487 checksumoutstr = hashlib.new(checksumtype)
2488 checksumoutstr.update(catfileoutstr.encode('UTF-8'))
2489 catfileheadercshex = checksumoutstr.hexdigest().lower()
2490 else:
2491 catfileheadercshex = format(0, 'x').lower()
2492 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5])
2493 catfileoutstrecd = catfileoutstr.encode('UTF-8')
2494 nullstrecd = formatspecs[5].encode('UTF-8')
2495 catfileout = catfileoutstrecd + fcontents + nullstrecd
2496 catfcontentend = (catfp.tell() - 1) + len(catfileout)
2497 catfp.write(catfileout)
2498 try:
2499 catfp.flush()
2500 os.fsync(catfp.fileno())
2501 except io.UnsupportedOperation:
2502 pass
2503 except AttributeError:
2504 pass
2505 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
2506 catfp = CompressArchiveFile(catfp, compression, formatspecs)
2507 try:
2508 catfp.flush()
2509 os.fsync(catfp.fileno())
2510 except io.UnsupportedOperation:
2511 pass
2512 except AttributeError:
2513 pass
2514 if(outfile=="-"):
2515 catfp.seek(0, 0)
2516 if(hasattr(sys.stdout, "buffer")):
2517 shutil.copyfileobj(catfp, sys.stdout.buffer);
2518 else:
2519 shutil.copyfileobj(catfp, sys.stdout);
2520 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
2521 catfp = CompressArchiveFile(catfp, compression, formatspecs);
2522 catfp.seek(0, 0);
2523 upload_file_to_internet_file(catfp, outfile);
2524 if(returnfp):
2525 catfp.seek(0, 0)
2526 return catfp
2527 else:
2528 catfp.close()
2529 return True;
2531 create_alias_function("Pack", __file_format_name__, "FromRarFile", PackArchiveFileFromRarFile);
2533 def ArchiveFileSeekToFileNum(infile, seekto=0, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
2534 if(hasattr(infile, "read") or hasattr(infile, "write")):
2535 catfp = infile;
2536 catfp.seek(0, 0);
2537 catfp = UncompressArchiveFile(catfp, formatspecs);
2538 checkcompressfile = CheckCompressionSubType(catfp, formatspecs);
2539 if(checkcompressfile=="tarfile"):
2540 return TarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2541 if(checkcompressfile=="zipfile"):
2542 return ZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2543 if(rarfile_support and checkcompressfile=="rarfile"):
2544 return RarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2545 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
2546 return False;
2547 if(not catfp):
2548 return False;
2549 catfp.seek(0, 0);
2550 elif(infile=="-"):
2551 catfp = BytesIO();
2552 if(hasattr(sys.stdin, "buffer")):
2553 shutil.copyfileobj(sys.stdin.buffer, catfp);
2554 else:
2555 shutil.copyfileobj(sys.stdin, catfp);
2556 catfp.seek(0, 0);
2557 catfp = UncompressArchiveFile(catfp, formatspecs);
2558 if(not catfp):
2559 return False;
2560 catfp.seek(0, 0);
2561 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
2562 catfp = download_file_from_internet_file(infile);
2563 catfp.seek(0, 0);
2564 catfp = UncompressArchiveFile(catfp, formatspecs);
2565 if(not catfp):
2566 return False;
2567 catfp.seek(0, 0);
2568 else:
2569 infile = RemoveWindowsPath(infile);
2570 checkcompressfile = CheckCompressionSubType(infile, formatspecs);
2571 if(checkcompressfile=="tarfile"):
2572 return TarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2573 if(checkcompressfile=="zipfile"):
2574 return ZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2575 if(rarfile_support and checkcompressfile=="rarfile"):
2576 return RarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2577 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
2578 return False;
2579 compresscheck = CheckCompressionType(infile, formatspecs, True);
2580 if(not compresscheck):
2581 fextname = os.path.splitext(infile)[1];
2582 if(fextname==".gz"):
2583 compresscheck = "gzip";
2584 if(fextname==".bz2"):
2585 compresscheck = "bzip2";
2586 if(fextname==".zst"):
2587 compresscheck = "zstd";
2588 if(fextname==".lz4" or fextname==".clz4"):
2589 compresscheck = "lz4";
2590 if(fextname==".lzo" or fextname==".lzop"):
2591 compresscheck = "lzo";
2592 if(fextname==".lzma" or fextname==".xz"):
2593 compresscheck = "lzma";
2594 if(not compresscheck):
2595 return False;
2596 catfp = UncompressFile(infile, formatspecs, "rb");
2598 try:
2599 catfp.seek(0, 2);
2600 except OSError:
2601 SeekToEndOfFile(catfp);
2602 except ValueError:
2603 SeekToEndOfFile(catfp);
2604 CatSize = catfp.tell();
2605 CatSizeEnd = CatSize;
2607 try:
2608 catfp.seek(0, 0);
2609 except OSError:
2610 return False;
2611 except ValueError:
2612 return False;
2613 catheader = ReadFileHeaderData(catfp, 4, formatspecs[5]);
2614 catstring = catheader[0];
2615 catversion = re.findall(r"([\d]+)$", catstring);
2616 fprenumfiles = catheader[1];
2617 fnumfiles = int(fprenumfiles, 16);
2618 fprechecksumtype = catheader[2];
2619 fprechecksum = catheader[3];
2620 fileheader = AppendNullByte(catstring, formatspecs[5]);
2621 fnumfileshex = format(int(fnumfiles), 'x').lower();
2622 fileheader = fileheader + AppendNullBytes([fnumfileshex, fprechecksumtype], formatspecs[5]);
2623 if(fprechecksumtype=="none" or fprechecksumtype==""):
2624 catfileheadercshex = format(0, 'x').lower();
2625 elif(fprechecksumtype=="crc16" or fprechecksumtype=="crc16_ansi" or fprechecksumtype=="crc16_ibm"):
2626 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
2627 elif(fprechecksumtype=="crc16_ccitt"):
2628 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
2629 elif(fprechecksumtype=="adler32"):
2630 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
2631 elif(fprechecksumtype=="crc32"):
2632 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
2633 elif(fprechecksumtype=="crc64_ecma"):
2634 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2635 elif(fprechecksumtype=="crc64" or fprechecksumtype=="crc64_iso"):
2636 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2637 elif(CheckSumSupportAlt(fprechecksumtype, hashlib_guaranteed)):
2638 checksumoutstr = hashlib.new(fprechecksumtype);
2639 checksumoutstr.update(fileheader.encode('UTF-8'));
2640 catfileheadercshex = checksumoutstr.hexdigest().lower();
2641 else:
2642 catfileheadercshex = format(0, 'x').lower();
2643 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
2644 fheadtell = len(fileheader);
2645 if(fprechecksum!=catfileheadercshex and not skipchecksum):
2646 VerbosePrintOut("File Header Checksum Error with file " + infile + " at offset " + str(catfp.tell()));
2647 return False;
2648 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
2649 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': fprechecksumtype, 'fheaderchecksum': fprechecksum, 'ffilelist': {}};
2650 if(seekto>=fnumfiles):
2651 seekto = fnumfiles - 1;
2652 if(seekto<0):
2653 seekto = 0;
2654 if(seekto>=0):
2655 il = -1;
2656 while(il < seekto):
2657 prefhstart = catfp.tell();
2658 if(formatspecs[7]):
2659 preheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
2660 else:
2661 preheaderdata = ReadFileHeaderData(catfp, 23, formatspecs[5]);
2662 prefheadsize = int(preheaderdata[0], 16);
2663 preftype = int(preheaderdata[1], 16);
2664 if(re.findall("^[.|/]", preheaderdata[2])):
2665 prefname = preheaderdata[2];
2666 else:
2667 prefname = "./"+preheaderdata[2];
2668 prefbasedir = os.path.dirname(prefname);
2669 preflinkname = preheaderdata[3];
2670 prefsize = int(preheaderdata[4], 16);
2671 prefatime = int(preheaderdata[5], 16);
2672 prefmtime = int(preheaderdata[6], 16);
2673 prefctime = int(preheaderdata[7], 16);
2674 prefbtime = int(preheaderdata[8], 16);
2675 prefmode = int(preheaderdata[9], 16);
2676 prefchmode = stat.S_IMODE(prefmode);
2677 preftypemod = stat.S_IFMT(prefmode);
2678 prefuid = int(preheaderdata[10], 16);
2679 prefuname = preheaderdata[11];
2680 prefgid = int(preheaderdata[12], 16);
2681 prefgname = preheaderdata[13];
2682 fid = int(preheaderdata[14], 16);
2683 finode = int(preheaderdata[15], 16);
2684 flinkcount = int(preheaderdata[16], 16);
2685 prefdev_minor = int(preheaderdata[17], 16);
2686 prefdev_major = int(preheaderdata[18], 16);
2687 prefrdev_minor = int(preheaderdata[19], 16);
2688 prefrdev_major = int(preheaderdata[20], 16);
2689 prefextrasize = int(preheaderdata[21], 16);
2690 prefextrafields = int(preheaderdata[22], 16);
2691 extrafieldslist = [];
2692 if(formatspecs[7]):
2693 extrastart = 23;
2694 extraend = extrastart + prefextrafields;
2695 extrafieldslist = [];
2696 if(extrastart<extraend):
2697 extrafieldslist.append(preheaderdata[extrastart]);
2698 extrastart = extrastart + 1;
2699 prefchecksumtype = preheaderdata[extrastart].lower();
2700 prefcs = preheaderdata[extrastart + 1].lower();
2701 prefccs = preheaderdata[extrastart + 2].lower();
2702 else:
2703 extrafieldslist = [];
2704 if(formatspecs[7]):
2705 extrafieldslist = ReadFileHeaderData(catfp, prefextrafields, formatspecs[5]);
2706 checksumsval = ReadFileHeaderData(catfp, 3, formatspecs[5]);
2707 prefchecksumtype = checksumsval[0].lower();
2708 prefcs = checksumsval[1].lower();
2709 prefccs = checksumsval[2].lower();
2710 hc = 0;
2711 if(formatspecs[7]):
2712 hcmax = len(preheaderdata) - 2;
2713 else:
2714 hcmax = len(preheaderdata);
2715 hout = "";
2716 while(hc<hcmax):
2717 hout = hout + AppendNullByte(preheaderdata[hc], formatspecs[5]);
2718 hc = hc + 1;
2719 prefnumfields = 24 + prefextrafields;
2720 if(prefchecksumtype=="none" or prefchecksumtype==""):
2721 prenewfcs = 0;
2722 elif(prefchecksumtype=="crc16" or prefchecksumtype=="crc16_ansi" or prefchecksumtype=="crc16_ibm"):
2723 prenewfcs = format(crc16(hout.encode('UTF-8')) & 0xffff, '04x').lower();
2724 elif(prefchecksumtype=="adler32"):
2725 prenewfcs = format(zlib.adler32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
2726 elif(prefchecksumtype=="crc32"):
2727 prenewfcs = format(crc32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
2728 elif(prefchecksumtype=="crc64_ecma"):
2729 prenewfcs = format(crc64_ecma(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2730 elif(prefchecksumtype=="crc64" or prefchecksumtype=="crc64_iso"):
2731 prenewfcs = format(crc64_iso(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2732 elif(CheckSumSupportAlt(prefchecksumtype, hashlib_guaranteed)):
2733 checksumoutstr = hashlib.new(prefchecksumtype);
2734 checksumoutstr.update(hout.encode('UTF-8'));
2735 prenewfcs = checksumoutstr.hexdigest().lower();
2736 if(prefcs!=prenewfcs and not skipchecksum):
2737 VerbosePrintOut("File Header Checksum Error with file " + prefname + " at offset " + str(prefhstart));
2738 return False;
2739 valid_archive = False;
2740 invalid_archive = True;
2741 prefhend = catfp.tell() - 1;
2742 prefcontentstart = catfp.tell();
2743 prefcontents = "";
2744 pyhascontents = False;
2745 if(prefsize>0):
2746 prefcontents = catfp.read(prefsize);
2747 if(prefchecksumtype=="none" or prefchecksumtype==""):
2748 prenewfccs = 0;
2749 elif(prefchecksumtype=="crc16" or prefchecksumtype=="crc16_ansi" or prefchecksumtype=="crc16_ibm"):
2750 prenewfccs = format(crc16(prefcontents) & 0xffff, '04x').lower();
2751 elif(prefchecksumtype=="crc16_ccitt"):
2752 prenewfcs = format(crc16_ccitt(prefcontents) & 0xffff, '04x').lower();
2753 elif(prefchecksumtype=="adler32"):
2754 prenewfccs = format(zlib.adler32(prefcontents) & 0xffffffff, '08x').lower();
2755 elif(prefchecksumtype=="crc32"):
2756 prenewfccs = format(crc32(prefcontents) & 0xffffffff, '08x').lower();
2757 elif(prefchecksumtype=="crc64_ecma"):
2758 prenewfcs = format(crc64_ecma(prefcontents) & 0xffffffffffffffff, '016x').lower();
2759 elif(prefchecksumtype=="crc64" or prefchecksumtype=="crc64_iso"):
2760 prenewfcs = format(crc64_iso(prefcontents) & 0xffffffffffffffff, '016x').lower();
2761 elif(CheckSumSupportAlt(prefchecksumtype, hashlib_guaranteed)):
2762 checksumoutstr = hashlib.new(prefchecksumtype);
2763 checksumoutstr.update(prefcontents);
2764 prenewfccs = checksumoutstr.hexdigest().lower();
2765 pyhascontents = True;
2766 if(prefccs!=prenewfccs and not skipchecksum):
2767 VerbosePrintOut("File Content Checksum Error with file " + prefname + " at offset " + str(prefcontentstart));
2768 return False;
2769 catfp.seek(1, 1);
2770 il = il + 1;
2771 catfp.seek(seekstart, 0);
2772 fileidnum = il;
2773 catfheadsize = int(preheaderdata[0], 16);
2774 catftype = int(preheaderdata[1], 16);
2775 if(re.findall("^[.|/]", preheaderdata[2])):
2776 catfname = preheaderdata[2];
2777 else:
2778 catfname = "./"+preheaderdata[2];
2779 catflinkname = preheaderdata[3];
2780 catfsize = int(preheaderdata[4], 16);
2781 catfbasedir = os.path.dirname(catfname);
2782 catlist = {'fid': fileidnum, 'foffset': catfp.tell(), 'ftype': catftype, 'fname': catfname, 'fbasedir': catfbasedir, 'flinkname': catflinkname, 'fsize': catfsize};
2783 if(returnfp):
2784 catlist.update({'catfp': catfp});
2785 else:
2786 catfp.close();
2787 return catlist;
2789 create_alias_function("", __file_format_name__, "SeekToFileNum", ArchiveFileSeekToFileNum);
2791 def ArchiveFileSeekToFileName(infile, seekfile=None, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
2792 if(hasattr(infile, "read") or hasattr(infile, "write")):
2793 catfp = infile;
2794 catfp.seek(0, 0);
2795 catfp = UncompressArchiveFile(catfp, formatspecs);
2796 checkcompressfile = CheckCompressionSubType(catfp, formatspecs);
2797 if(checkcompressfile=="tarfile"):
2798 return TarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2799 if(checkcompressfile=="zipfile"):
2800 return ZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2801 if(rarfile_support and checkcompressfile=="rarfile"):
2802 return RarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2803 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
2804 return False;
2805 if(not catfp):
2806 return False;
2807 catfp.seek(0, 0);
2808 elif(infile=="-"):
2809 catfp = BytesIO();
2810 if(hasattr(sys.stdin, "buffer")):
2811 shutil.copyfileobj(sys.stdin.buffer, catfp);
2812 else:
2813 shutil.copyfileobj(sys.stdin, catfp);
2814 catfp.seek(0, 0);
2815 catfp = UncompressArchiveFile(catfp, formatspecs);
2816 if(not catfp):
2817 return False;
2818 catfp.seek(0, 0);
2819 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
2820 catfp = download_file_from_internet_file(infile);
2821 catfp = UncompressArchiveFile(catfp, formatspecs);
2822 catfp.seek(0, 0);
2823 if(not catfp):
2824 return False;
2825 catfp.seek(0, 0);
2826 else:
2827 infile = RemoveWindowsPath(infile);
2828 checkcompressfile = CheckCompressionSubType(infile, formatspecs);
2829 if(checkcompressfile=="tarfile"):
2830 return TarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2831 if(checkcompressfile=="zipfile"):
2832 return ZipFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2833 if(rarfile_support and checkcompressfile=="rarfile"):
2834 return RarFileToArray(infile, 0, 0, listonly, skipchecksum, formatspecs, returnfp);
2835 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
2836 return False;
2837 compresscheck = CheckCompressionType(infile, formatspecs, True);
2838 if(not compresscheck):
2839 fextname = os.path.splitext(infile)[1];
2840 if(fextname==".gz"):
2841 compresscheck = "gzip";
2842 if(fextname==".bz2"):
2843 compresscheck = "bzip2";
2844 if(fextname==".zst"):
2845 compresscheck = "zstd";
2846 if(fextname==".lz4" or fextname==".clz4"):
2847 compresscheck = "lz4";
2848 if(fextname==".lzo" or fextname==".lzop"):
2849 compresscheck = "lzo";
2850 if(fextname==".lzma" or fextname==".xz"):
2851 compresscheck = "lzma";
2852 if(not compresscheck):
2853 return False;
2854 catfp = UncompressFile(infile, formatspecs, "rb");
2856 try:
2857 catfp.seek(0, 2);
2858 except OSError:
2859 SeekToEndOfFile(catfp);
2860 except ValueError:
2861 SeekToEndOfFile(catfp);
2862 CatSize = catfp.tell();
2863 CatSizeEnd = CatSize;
2865 try:
2866 catfp.seek(0, 0);
2867 except OSError:
2868 return False;
2869 except ValueError:
2870 return False;
2871 catheader = ReadFileHeaderData(catfp, 4, formatspecs[5]);
2872 catstring = catheader[0];
2873 catversion = re.findall(r"([\d]+)$", catstring);
2874 fprenumfiles = catheader[1];
2875 fnumfiles = int(fprenumfiles, 16);
2876 fprechecksumtype = catheader[2];
2877 fprechecksum = catheader[3];
2878 fileheader = AppendNullByte(catstring, formatspecs[5]);
2879 fnumfileshex = format(int(fnumfiles), 'x').lower();
2880 fileheader = fileheader + AppendNullBytes([fnumfileshex, fprechecksumtype], formatspecs[5]);
2881 if(fprechecksumtype=="none" or fprechecksumtype==""):
2882 catfileheadercshex = format(0, 'x').lower();
2883 elif(fprechecksumtype=="crc16" or fprechecksumtype=="crc16_ansi" or fprechecksumtype=="crc16_ibm"):
2884 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
2885 elif(fprechecksumtype=="crc16_ccitt"):
2886 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
2887 elif(fprechecksumtype=="adler32"):
2888 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
2889 elif(fprechecksumtype=="crc32"):
2890 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
2891 elif(fprechecksumtype=="crc64_ecma"):
2892 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2893 elif(fprechecksumtype=="crc64" or fprechecksumtype=="crc64_iso"):
2894 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2895 elif(CheckSumSupportAlt(fprechecksumtype, hashlib_guaranteed)):
2896 checksumoutstr = hashlib.new(fprechecksumtype);
2897 checksumoutstr.update(fileheader.encode('UTF-8'));
2898 catfileheadercshex = checksumoutstr.hexdigest().lower();
2899 else:
2900 catfileheadercshex = format(0, 'x').lower();
2901 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
2902 fheadtell = len(fileheader);
2903 if(fprechecksum!=catfileheadercshex and not skipchecksum):
2904 VerbosePrintOut("File Header Checksum Error with file " + infile + " at offset " + str(catfp.tell()));
2905 return False;
2906 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
2907 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': fprechecksumtype, 'fheaderchecksum': fprechecksum, 'ffilelist': {}};
2908 seekto = fnumfiles - 1
2909 filefound = False;
2910 if(seekto>=0):
2911 il = -1;
2912 while(il < seekto):
2913 prefhstart = catfp.tell();
2914 if(formatspecs[7]):
2915 preheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
2916 else:
2917 preheaderdata = ReadFileHeaderData(catfp, 23, formatspecs[5]);
2918 prefheadsize = int(preheaderdata[0], 16);
2919 preftype = int(preheaderdata[1], 16);
2920 if(re.findall("^[.|/]", preheaderdata[2])):
2921 prefname = preheaderdata[2];
2922 else:
2923 prefname = "./"+preheaderdata[2];
2924 prefbasedir = os.path.dirname(prefname);
2925 preflinkname = preheaderdata[3];
2926 prefsize = int(preheaderdata[4], 16);
2927 prefatime = int(preheaderdata[5], 16);
2928 prefmtime = int(preheaderdata[6], 16);
2929 prefctime = int(preheaderdata[7], 16);
2930 prefbtime = int(preheaderdata[8], 16);
2931 prefmode = int(preheaderdata[9], 16);
2932 prefchmode = stat.S_IMODE(prefmode);
2933 preftypemod = stat.S_IFMT(prefmode);
2934 prefuid = int(preheaderdata[10], 16);
2935 prefuname = preheaderdata[11];
2936 prefgid = int(preheaderdata[12], 16);
2937 prefgname = preheaderdata[13];
2938 fid = int(preheaderdata[14], 16);
2939 finode = int(preheaderdata[15], 16);
2940 flinkcount = int(preheaderdata[16], 16);
2941 prefdev_minor = int(preheaderdata[17], 16);
2942 prefdev_major = int(preheaderdata[18], 16);
2943 prefrdev_minor = int(preheaderdata[19], 16);
2944 prefrdev_major = int(preheaderdata[20], 16);
2945 prefextrasize = int(preheaderdata[21], 16);
2946 prefextrafields = int(preheaderdata[22], 16);
2947 extrafieldslist = [];
2948 if(formatspecs[7]):
2949 extrastart = 23;
2950 extraend = extrastart + prefextrafields;
2951 extrafieldslist = [];
2952 if(extrastart<extraend):
2953 extrafieldslist.append(preheaderdata[extrastart]);
2954 extrastart = extrastart + 1;
2955 prefchecksumtype = preheaderdata[extrastart].lower();
2956 prefcs = preheaderdata[extrastart + 1].lower();
2957 prefccs = preheaderdata[extrastart + 2].lower();
2958 else:
2959 extrafieldslist = [];
2960 if(formatspecs[7]):
2961 extrafieldslist = ReadFileHeaderData(catfp, prefextrafields, formatspecs[5]);
2962 checksumsval = ReadFileHeaderData(catfp, 3, formatspecs[5]);
2963 prefchecksumtype = checksumsval[0].lower();
2964 prefcs = checksumsval[1].lower();
2965 prefccs = checksumsval[2].lower();
2966 hc = 0;
2967 if(formatspecs[7]):
2968 hcmax = len(preheaderdata) - 2;
2969 else:
2970 hcmax = len(preheaderdata);
2971 hout = "";
2972 while(hc<hcmax):
2973 hout = hout + AppendNullByte(preheaderdata[hc], formatspecs[5]);
2974 hc = hc + 1;
2975 prefnumfields = 24 + prefextrafields;
2976 if(prefchecksumtype=="none" or prefchecksumtype==""):
2977 prenewfcs = 0;
2978 elif(prefchecksumtype=="crc16" or prefchecksumtype=="crc16_ansi" or prefchecksumtype=="crc16_ibm"):
2979 prenewfcs = format(crc16(hout.encode('UTF-8')) & 0xffff, '04x').lower();
2980 elif(prefchecksumtype=="adler32"):
2981 prenewfcs = format(zlib.adler32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
2982 elif(prefchecksumtype=="crc32"):
2983 prenewfcs = format(crc32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
2984 elif(prefchecksumtype=="crc64_ecma"):
2985 prenewfcs = format(crc64_ecma(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2986 elif(prefchecksumtype=="crc64" or prefchecksumtype=="crc64_iso"):
2987 prenewfcs = format(crc64_iso(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
2988 elif(CheckSumSupportAlt(prefchecksumtype, hashlib_guaranteed)):
2989 checksumoutstr = hashlib.new(prefchecksumtype);
2990 checksumoutstr.update(hout.encode('UTF-8'));
2991 prenewfcs = checksumoutstr.hexdigest().lower();
2992 if(prefcs!=prenewfcs and not skipchecksum):
2993 VerbosePrintOut("File Header Checksum Error with file " + prefname + " at offset " + str(prefhstart));
2994 return False;
2995 valid_archive = False;
2996 invalid_archive = True;
2997 prefhend = catfp.tell() - 1;
2998 prefcontentstart = catfp.tell();
2999 prefcontents = "";
3000 pyhascontents = False;
3001 if(prefsize>0):
3002 prefcontents = catfp.read(prefsize);
3003 if(prefchecksumtype=="none" or prefchecksumtype==""):
3004 prenewfccs = 0;
3005 elif(prefchecksumtype=="crc16" or prefchecksumtype=="crc16_ansi" or prefchecksumtype=="crc16_ibm"):
3006 prenewfccs = format(crc16(prefcontents) & 0xffff, '04x').lower();
3007 elif(prefchecksumtype=="crc16_ccitt"):
3008 prenewfcs = format(crc16_ccitt(prefcontents) & 0xffff, '04x').lower();
3009 elif(prefchecksumtype=="adler32"):
3010 prenewfccs = format(zlib.adler32(prefcontents) & 0xffffffff, '08x').lower();
3011 elif(prefchecksumtype=="crc32"):
3012 prenewfccs = format(crc32(prefcontents) & 0xffffffff, '08x').lower();
3013 elif(prefchecksumtype=="crc64_ecma"):
3014 prenewfcs = format(crc64_ecma(prefcontents) & 0xffffffffffffffff, '016x').lower();
3015 elif(prefchecksumtype=="crc64" or prefchecksumtype=="crc64_iso"):
3016 prenewfcs = format(crc64_iso(prefcontents) & 0xffffffffffffffff, '016x').lower();
3017 elif(CheckSumSupportAlt(prefchecksumtype, hashlib_guaranteed)):
3018 checksumoutstr = hashlib.new(prefchecksumtype);
3019 checksumoutstr.update(prefcontents);
3020 prenewfccs = checksumoutstr.hexdigest().lower();
3021 pyhascontents = True;
3022 if(prefccs!=prenewfccs and not skipchecksum):
3023 VerbosePrintOut("File Content Checksum Error with file " + prefname + " at offset " + str(prefcontentstart));
3024 return False;
3025 catfp.seek(1, 1);
3026 il = il + 1;
3027 filefound = False;
3028 prefname = preheaderdata[2];
3029 if(re.findall("^[.|/]", preheaderdata[2])):
3030 prefname = preheaderdata[2];
3031 else:
3032 prefname = "./"+preheaderdata[2];
3033 if(prefname==seekfile):
3034 filefound = True;
3035 break;
3036 catfp.seek(seekstart, 0);
3037 fileidnum = il;
3038 catfheadsize = int(preheaderdata[0], 16);
3039 catftype = int(preheaderdata[1], 16);
3040 if(re.findall("^[.|/]", preheaderdata[2])):
3041 catfname = preheaderdata[2];
3042 else:
3043 catfname = "./"+preheaderdata[2];
3044 catflinkname = preheaderdata[3];
3045 catfsize = int(preheaderdata[4], 16);
3046 catfbasedir = os.path.dirname(catfname);
3047 if(filefound):
3048 catlist = {'fid': fileidnum, 'foffset': catfp.tell(), 'ftype': catftype, 'fname': catfname, 'fbasedir': catfbasedir, 'flinkname': catflinkname, 'fsize': catfsize};
3049 else:
3050 return False;
3051 if(returnfp):
3052 catlist.update({'catfp': catfp});
3053 else:
3054 catfp.close();
3055 return catlist;
3057 create_alias_function("", __file_format_name__, "SeekToFileName", ArchiveFileSeekToFileName);
3059 def ArchiveFileValidate(infile, formatspecs=__file_format_list__, verbose=False, returnfp=False):
3060 if(verbose):
3061 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
3062 if(hasattr(infile, "read") or hasattr(infile, "write")):
3063 catfp = infile;
3064 catfp.seek(0, 0);
3065 catfp = UncompressArchiveFile(catfp, formatspecs);
3066 checkcompressfile = CheckCompressionSubType(catfp, formatspecs);
3067 if(checkcompressfile=="tarfile"):
3068 return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3069 if(checkcompressfile=="zipfile"):
3070 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3071 if(rarfile_support and checkcompressfile=="rarfile"):
3072 return RarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3073 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
3074 return False;
3075 if(not catfp):
3076 return False;
3077 catfp.seek(0, 0);
3078 elif(infile=="-"):
3079 catfp = BytesIO();
3080 if(hasattr(sys.stdin, "buffer")):
3081 shutil.copyfileobj(sys.stdin.buffer, catfp);
3082 else:
3083 shutil.copyfileobj(sys.stdin, catfp);
3084 catfp.seek(0, 0);
3085 catfp = UncompressArchiveFile(catfp, formatspecs);
3086 if(not catfp):
3087 return False;
3088 catfp.seek(0, 0);
3089 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
3090 catfp = download_file_from_internet_file(infile);
3091 catfp = UncompressArchiveFile(catfp, formatspecs);
3092 catfp.seek(0, 0);
3093 if(not catfp):
3094 return False;
3095 catfp.seek(0, 0);
3096 else:
3097 infile = RemoveWindowsPath(infile);
3098 checkcompressfile = CheckCompressionSubType(infile, formatspecs);
3099 if(checkcompressfile=="tarfile"):
3100 return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3101 if(checkcompressfile=="zipfile"):
3102 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3103 if(rarfile_support and checkcompressfile=="rarfile"):
3104 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3105 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
3106 return False;
3107 compresscheck = CheckCompressionType(infile, formatspecs, True);
3108 if(not compresscheck):
3109 fextname = os.path.splitext(infile)[1];
3110 if(fextname==".gz"):
3111 compresscheck = "gzip";
3112 if(fextname==".bz2"):
3113 compresscheck = "bzip2";
3114 if(fextname==".zst"):
3115 compresscheck = "zstd";
3116 if(fextname==".lz4" or fextname==".clz4"):
3117 compresscheck = "lz4";
3118 if(fextname==".lzo" or fextname==".lzop"):
3119 compresscheck = "lzo";
3120 if(fextname==".lzma" or fextname==".xz"):
3121 compresscheck = "lzma";
3122 if(not compresscheck):
3123 return False;
3124 catfp = UncompressFile(infile, formatspecs, "rb");
3126 try:
3127 catfp.seek(0, 2);
3128 except OSError:
3129 SeekToEndOfFile(catfp);
3130 except ValueError:
3131 SeekToEndOfFile(catfp);
3132 CatSize = catfp.tell();
3133 CatSizeEnd = CatSize;
3135 try:
3136 catfp.seek(0, 0);
3137 except OSError:
3138 return False;
3139 except ValueError:
3140 return False;
3141 catheader = ReadFileHeaderData(catfp, 4, formatspecs[5]);
3142 catstring = catheader[0];
3143 catversion = re.findall(r"([\d]+)$", catstring);
3144 fprenumfiles = catheader[1];
3145 fnumfiles = int(fprenumfiles, 16);
3146 fprechecksumtype = catheader[2];
3147 fprechecksum = catheader[3];
3148 il = 0;
3149 fileheader = AppendNullByte(catstring, formatspecs[5]);
3150 fnumfileshex = format(int(fnumfiles), 'x').lower();
3151 fileheader = fileheader + AppendNullBytes([fnumfileshex, fprechecksumtype], formatspecs[5]);
3152 if(fprechecksumtype=="none" or fprechecksumtype==""):
3153 catfileheadercshex = format(0, 'x').lower();
3154 elif(fprechecksumtype=="crc16" or fprechecksumtype=="crc16_ansi" or fprechecksumtype=="crc16_ibm"):
3155 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3156 elif(fprechecksumtype=="crc16_ccitt"):
3157 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3158 elif(fprechecksumtype=="adler32"):
3159 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3160 elif(fprechecksumtype=="crc32"):
3161 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3162 elif(fprechecksumtype=="crc64_ecma"):
3163 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3164 elif(fprechecksumtype=="crc64" or fprechecksumtype=="crc64_iso"):
3165 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3166 elif(CheckSumSupportAlt(fprechecksumtype, hashlib_guaranteed)):
3167 checksumoutstr = hashlib.new(fprechecksumtype);
3168 checksumoutstr.update(fileheader.encode('UTF-8'));
3169 catfileheadercshex = checksumoutstr.hexdigest().lower();
3170 else:
3171 catfileheadercshex = format(0, 'x').lower();
3172 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
3173 valid_archive = True;
3174 invalid_archive = False;
3175 if(verbose):
3176 VerbosePrintOut("Checking File Header Checksum of file at offset " + str(0));
3177 VerbosePrintOut(infile);
3178 if(fprechecksum==catfileheadercshex):
3179 if(verbose):
3180 VerbosePrintOut("File Header Checksum Passed");
3181 else:
3182 if(verbose):
3183 VerbosePrintOut("File Header Checksum Error with file " + infile + " at offset " + str(0));
3184 valid_archive = False;
3185 invalid_archive = True;
3186 while(il<fnumfiles):
3187 catfhstart = catfp.tell();
3188 if(formatspecs[7]):
3189 catheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
3190 else:
3191 catheaderdata = ReadFileHeaderData(catfp, 23, formatspecs[5]);
3192 catfheadsize = int(catheaderdata[0], 16);
3193 catftype = int(catheaderdata[1], 16);
3194 if(re.findall("^[.|/]", catheaderdata[2])):
3195 catfname = catheaderdata[2];
3196 else:
3197 catfname = "./"+catheaderdata[2];
3198 catfbasedir = os.path.dirname(catfname);
3199 catflinkname = catheaderdata[3];
3200 catfsize = int(catheaderdata[4], 16);
3201 catfatime = int(catheaderdata[5], 16);
3202 catfmtime = int(catheaderdata[6], 16);
3203 catfctime = int(catheaderdata[7], 16);
3204 catfbtime = int(catheaderdata[8], 16);
3205 catfmode = int(catheaderdata[9], 16);
3206 catfchmode = stat.S_IMODE(catfmode);
3207 catftypemod = stat.S_IFMT(catfmode);
3208 catfuid = int(catheaderdata[10], 16);
3209 catfuname = catheaderdata[11];
3210 catfgid = int(catheaderdata[12], 16);
3211 catfgname = catheaderdata[13];
3212 fid = int(catheaderdata[14], 16);
3213 finode = int(catheaderdata[15], 16);
3214 flinkcount = int(catheaderdata[16], 16);
3215 catfdev_minor = int(catheaderdata[17], 16);
3216 catfdev_major = int(catheaderdata[18], 16);
3217 catfrdev_minor = int(catheaderdata[19], 16);
3218 catfrdev_major = int(catheaderdata[20], 16);
3219 catfextrasize = int(catheaderdata[21], 16);
3220 catfextrafields = int(catheaderdata[22], 16);
3221 extrafieldslist = [];
3222 if(formatspecs[7]):
3223 extrastart = 23;
3224 extraend = extrastart + catfextrafields;
3225 extrafieldslist = [];
3226 if(extrastart<extraend):
3227 extrafieldslist.append(catheaderdata[extrastart]);
3228 extrastart = extrastart + 1;
3229 catfchecksumtype = catheaderdata[extrastart].lower();
3230 catfcs = catheaderdata[extrastart + 1].lower();
3231 catfccs = catheaderdata[extrastart + 2].lower();
3232 else:
3233 extrafieldslist = [];
3234 if(formatspecs[7]):
3235 extrafieldslist = ReadFileHeaderData(catfp, catfextrafields, formatspecs[5]);
3236 checksumsval = ReadFileHeaderData(catfp, 3, formatspecs[5]);
3237 catfchecksumtype = checksumsval[0].lower();
3238 catfcs = checksumsval[1].lower();
3239 catfccs = checksumsval[2].lower();
3240 hc = 0;
3241 if(formatspecs[7]):
3242 hcmax = len(catheaderdata) - 2;
3243 else:
3244 hcmax = len(catheaderdata);
3245 hout = "";
3246 while(hc<hcmax):
3247 hout = hout + AppendNullByte(catheaderdata[hc], formatspecs[5]);
3248 hc = hc + 1;
3249 catfnumfields = 24 + catfextrafields;
3250 if(catfchecksumtype=="none" or catfchecksumtype==""):
3251 catnewfcs = 0;
3252 elif(catfchecksumtype=="crc16" or catfchecksumtype=="crc16_ansi" or catfchecksumtype=="crc16_ibm"):
3253 catnewfcs = format(crc16(hout.encode('UTF-8')) & 0xffff, '04x').lower();
3254 elif(catfchecksumtype=="adler32"):
3255 catnewfcs = format(zlib.adler32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
3256 elif(catfchecksumtype=="crc32"):
3257 catnewfcs = format(crc32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
3258 elif(catfchecksumtype=="crc64_ecma"):
3259 catnewfcs = format(crc64_ecma(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3260 elif(catfchecksumtype=="crc64" or catfchecksumtype=="crc64_iso"):
3261 catnewfcs = format(crc64_iso(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3262 elif(CheckSumSupportAlt(catfchecksumtype, hashlib_guaranteed)):
3263 checksumoutstr = hashlib.new(catfchecksumtype);
3264 checksumoutstr.update(hout.encode('UTF-8'));
3265 catnewfcs = checksumoutstr.hexdigest().lower();
3266 if(verbose):
3267 VerbosePrintOut("Checking File Header Checksum of file at offset " + str(catfhstart));
3268 VerbosePrintOut(catfname);
3269 if(catfcs==catnewfcs):
3270 if(verbose):
3271 VerbosePrintOut("File Header Checksum Passed");
3272 else:
3273 if(verbose):
3274 VerbosePrintOut("File Header Checksum Error with file " + catfname + " at offset " + str(catfhstart));
3275 valid_archive = False;
3276 invalid_archive = True;
3277 catfhend = catfp.tell() - 1;
3278 catfcontentstart = catfp.tell();
3279 catfcontents = "";
3280 pyhascontents = False;
3281 if(catfsize>0):
3282 catfcontents = catfp.read(catfsize);
3283 if(catfchecksumtype=="none" or catfchecksumtype==""):
3284 catnewfccs = 0;
3285 elif(catfchecksumtype=="crc16" or catfchecksumtype=="crc16_ansi" or catfchecksumtype=="crc16_ibm"):
3286 catnewfccs = format(crc16(catfcontents) & 0xffff, '04x').lower();
3287 elif(catfchecksumtype=="crc16_ccitt"):
3288 catnewfcs = format(crc16_ccitt(catfcontents) & 0xffff, '04x').lower();
3289 elif(catfchecksumtype=="adler32"):
3290 catnewfccs = format(zlib.adler32(catfcontents) & 0xffffffff, '08x').lower();
3291 elif(catfchecksumtype=="crc32"):
3292 catnewfccs = format(crc32(catfcontents) & 0xffffffff, '08x').lower();
3293 elif(catfchecksumtype=="crc64_ecma"):
3294 catnewfcs = format(crc64_ecma(catfcontents) & 0xffffffffffffffff, '016x').lower();
3295 elif(catfchecksumtype=="crc64" or catfchecksumtype=="crc64_iso"):
3296 catnewfcs = format(crc64_iso(catfcontents) & 0xffffffffffffffff, '016x').lower();
3297 elif(CheckSumSupportAlt(catfchecksumtype, hashlib_guaranteed)):
3298 checksumoutstr = hashlib.new(catfchecksumtype);
3299 checksumoutstr.update(catfcontents);
3300 catnewfccs = checksumoutstr.hexdigest().lower();
3301 pyhascontents = True;
3302 if(verbose):
3303 VerbosePrintOut("Checking File Content Checksum of file at offset " + str(catfcontentstart));
3304 VerbosePrintOut(catfname);
3305 if(catfccs==catnewfccs):
3306 if(verbose):
3307 VerbosePrintOut("File Content Checksum Passed");
3308 else:
3309 if(verbose):
3310 VerbosePrintOut("File Header Checksum Error with file " + catfname + " at offset " + str(catfcontentstart));
3311 valid_archive = False;
3312 invalid_archive = True;
3313 if(verbose):
3314 VerbosePrintOut("");
3315 catfp.seek(1, 1);
3316 il = il + 1;
3317 if(valid_archive):
3318 if(returnfp):
3319 return catfp;
3320 else:
3321 catfp.close();
3322 return True;
3323 else:
3324 catfp.close();
3325 return False;
3327 create_alias_function("", __file_format_name__, "Validate", ArchiveFileValidate);
3329 def ArchiveFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
3330 if(hasattr(infile, "read") or hasattr(infile, "write")):
3331 catfp = infile;
3332 catfp.seek(0, 0);
3333 catfp = UncompressArchiveFile(catfp, formatspecs);
3334 checkcompressfile = CheckCompressionSubType(catfp, formatspecs);
3335 if(checkcompressfile=="tarfile"):
3336 return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3337 if(checkcompressfile=="zipfile"):
3338 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3339 if(rarfile_support and checkcompressfile=="rarfile"):
3340 return RarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3341 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
3342 return False;
3343 if(not catfp):
3344 return False;
3345 catfp.seek(0, 0);
3346 elif(infile=="-"):
3347 catfp = BytesIO();
3348 if(hasattr(sys.stdin, "buffer")):
3349 shutil.copyfileobj(sys.stdin.buffer, catfp);
3350 else:
3351 shutil.copyfileobj(sys.stdin, catfp);
3352 catfp.seek(0, 0);
3353 catfp = UncompressArchiveFile(catfp, formatspecs);
3354 if(not catfp):
3355 return False;
3356 catfp.seek(0, 0);
3357 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
3358 catfp = download_file_from_internet_file(infile);
3359 catfp = UncompressArchiveFile(catfp, formatspecs);
3360 catfp.seek(0, 0);
3361 if(not catfp):
3362 return False;
3363 catfp.seek(0, 0);
3364 else:
3365 infile = RemoveWindowsPath(infile);
3366 checkcompressfile = CheckCompressionSubType(infile, formatspecs);
3367 if(checkcompressfile=="tarfile"):
3368 return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3369 if(checkcompressfile=="zipfile"):
3370 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3371 if(rarfile_support and checkcompressfile=="rarfile"):
3372 return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3373 if(checkcompressfile!="catfile" and checkcompressfile!=formatspecs[2]):
3374 return False;
3375 compresscheck = CheckCompressionType(infile, formatspecs, True);
3376 if(not compresscheck):
3377 fextname = os.path.splitext(infile)[1];
3378 if(fextname==".gz"):
3379 compresscheck = "gzip";
3380 if(fextname==".bz2"):
3381 compresscheck = "bzip2";
3382 if(fextname==".zst"):
3383 compresscheck = "zstd";
3384 if(fextname==".lz4" or fextname==".clz4"):
3385 compresscheck = "lz4";
3386 if(fextname==".lzo" or fextname==".lzop"):
3387 compresscheck = "lzo";
3388 if(fextname==".lzma" or fextname==".xz"):
3389 compresscheck = "lzma";
3390 if(not compresscheck):
3391 return False;
3392 catfp = UncompressFile(infile, formatspecs, "rb");
3394 try:
3395 catfp.seek(0, 2);
3396 except OSError:
3397 SeekToEndOfFile(catfp);
3398 except ValueError:
3399 SeekToEndOfFile(catfp);
3400 CatSize = catfp.tell();
3401 CatSizeEnd = CatSize;
3403 try:
3404 catfp.seek(0, 0);
3405 except OSError:
3406 return False;
3407 except ValueError:
3408 return False;
3409 catheader = ReadFileHeaderData(catfp, 4, formatspecs[5]);
3410 catstring = catheader[0];
3411 catversion = re.findall(r"([\d]+)$", catstring);
3412 fprenumfiles = catheader[1];
3413 fnumfiles = int(fprenumfiles, 16);
3414 fprechecksumtype = catheader[2];
3415 fprechecksum = catheader[3];
3416 fileheader = AppendNullByte(catstring, formatspecs[5]);
3417 fnumfileshex = format(int(fnumfiles), 'x').lower();
3418 fileheader = fileheader + AppendNullBytes([fnumfileshex, fprechecksumtype], formatspecs[5]);
3419 if(fprechecksumtype=="none" or fprechecksumtype==""):
3420 catfileheadercshex = format(0, 'x').lower();
3421 elif(fprechecksumtype=="crc16" or fprechecksumtype=="crc16_ansi" or fprechecksumtype=="crc16_ibm"):
3422 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3423 elif(fprechecksumtype=="crc16_ccitt"):
3424 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3425 elif(fprechecksumtype=="adler32"):
3426 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3427 elif(fprechecksumtype=="crc32"):
3428 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3429 elif(fprechecksumtype=="crc64_ecma"):
3430 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3431 elif(fprechecksumtype=="crc64" or fprechecksumtype=="crc64_iso"):
3432 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3433 elif(CheckSumSupportAlt(fprechecksumtype, hashlib_guaranteed)):
3434 checksumoutstr = hashlib.new(fprechecksumtype);
3435 checksumoutstr.update(fileheader.encode('UTF-8'));
3436 catfileheadercshex = checksumoutstr.hexdigest().lower();
3437 else:
3438 catfileheadercshex = format(0, 'x').lower();
3439 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
3440 fheadtell = len(fileheader);
3441 if(fprechecksum!=catfileheadercshex and not skipchecksum):
3442 VerbosePrintOut("File Header Checksum Error with file " + infile + " at offset " + str(0));
3443 return False;
3444 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
3445 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': fprechecksumtype, 'fheaderchecksum': fprechecksum, 'ffilelist': {}};
3446 if(seekstart<0 and seekstart>fnumfiles):
3447 seekstart = 0;
3448 if(seekend==0 or seekend>fnumfiles and seekend<seekstart):
3449 seekend = fnumfiles;
3450 elif(seekend<0 and abs(seekend)<=fnumfiles and abs(seekend)>=seekstart):
3451 seekend = fnumfiles - abs(seekend);
3452 if(seekstart>0):
3453 il = 0;
3454 while(il < seekstart):
3455 prefhstart = catfp.tell();
3456 if(formatspecs[7]):
3457 preheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
3458 else:
3459 preheaderdata = ReadFileHeaderData(catfp, 23, formatspecs[5]);
3460 prefheadsize = int(preheaderdata[0], 16);
3461 preftype = int(preheaderdata[1], 16);
3462 if(re.findall("^[.|/]", preheaderdata[2])):
3463 prefname = preheaderdata[2];
3464 else:
3465 prefname = "./"+preheaderdata[2];
3466 prefbasedir = os.path.dirname(prefname);
3467 preflinkname = preheaderdata[3];
3468 prefsize = int(preheaderdata[4], 16);
3469 prefatime = int(preheaderdata[5], 16);
3470 prefmtime = int(preheaderdata[6], 16);
3471 prefctime = int(preheaderdata[7], 16);
3472 prefbtime = int(preheaderdata[8], 16);
3473 prefmode = int(preheaderdata[9], 16);
3474 prefchmode = stat.S_IMODE(prefmode);
3475 preftypemod = stat.S_IFMT(prefmode);
3476 prefuid = int(preheaderdata[10], 16);
3477 prefuname = preheaderdata[11];
3478 prefgid = int(preheaderdata[12], 16);
3479 prefgname = preheaderdata[13];
3480 fid = int(preheaderdata[14], 16);
3481 finode = int(preheaderdata[15], 16);
3482 flinkcount = int(preheaderdata[16], 16);
3483 prefdev_minor = int(preheaderdata[17], 16);
3484 prefdev_major = int(preheaderdata[18], 16);
3485 prefrdev_minor = int(preheaderdata[19], 16);
3486 prefrdev_major = int(preheaderdata[20], 16);
3487 prefextrasize = int(preheaderdata[21], 16);
3488 prefextrafields = int(preheaderdata[22], 16);
3489 extrafieldslist = [];
3490 if(formatspecs[7]):
3491 extrastart = 23;
3492 extraend = extrastart + prefextrafields;
3493 extrafieldslist = [];
3494 if(extrastart<extraend):
3495 extrafieldslist.append(preheaderdata[extrastart]);
3496 extrastart = extrastart + 1;
3497 prefchecksumtype = preheaderdata[extrastart].lower();
3498 prefcs = preheaderdata[extrastart + 1].lower();
3499 prefccs = preheaderdata[extrastart + 2].lower();
3500 else:
3501 extrafieldslist = [];
3502 if(formatspecs[7]):
3503 extrafieldslist = ReadFileHeaderData(catfp, prefextrafields, formatspecs[5]);
3504 checksumsval = ReadFileHeaderData(catfp, 3, formatspecs[5]);
3505 prefchecksumtype = checksumsval[0].lower();
3506 prefcs = checksumsval[1].lower();
3507 prefccs = checksumsval[2].lower();
3508 hc = 0;
3509 if(formatspecs[7]):
3510 hcmax = len(preheaderdata) - 2;
3511 else:
3512 hcmax = len(preheaderdata);
3513 hout = "";
3514 while(hc<hcmax):
3515 hout = hout + AppendNullByte(preheaderdata[hc], formatspecs[5]);
3516 hc = hc + 1;
3517 prefnumfields = 24 + prefextrafields;
3518 if(prefchecksumtype=="none" or prefchecksumtype==""):
3519 prenewfcs = 0;
3520 elif(prefchecksumtype=="crc16" or prefchecksumtype=="crc16_ansi" or prefchecksumtype=="crc16_ibm"):
3521 prenewfcs = format(crc16(hout.encode('UTF-8')) & 0xffff, '04x').lower();
3522 elif(prefchecksumtype=="adler32"):
3523 prenewfcs = format(zlib.adler32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
3524 elif(prefchecksumtype=="crc32"):
3525 prenewfcs = format(crc32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
3526 elif(prefchecksumtype=="crc64_ecma"):
3527 prenewfcs = format(crc64_ecma(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3528 elif(prefchecksumtype=="crc64" or prefchecksumtype=="crc64_iso"):
3529 prenewfcs = format(crc64_iso(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3530 elif(CheckSumSupportAlt(prefchecksumtype, hashlib_guaranteed)):
3531 checksumoutstr = hashlib.new(prefchecksumtype);
3532 checksumoutstr.update(hout.encode('UTF-8'));
3533 prenewfcs = checksumoutstr.hexdigest().lower();
3534 if(prefcs!=prenewfcs and not skipchecksum):
3535 VerbosePrintOut("File Header Checksum Error with file " + prefname + " at offset " + str(prefhstart));
3536 return False;
3537 valid_archive = False;
3538 invalid_archive = True;
3539 prefhend = catfp.tell() - 1;
3540 prefcontentstart = catfp.tell();
3541 prefcontents = "";
3542 pyhascontents = False;
3543 if(prefsize>0):
3544 prefcontents = catfp.read(prefsize);
3545 if(prefchecksumtype=="none" or prefchecksumtype==""):
3546 prenewfccs = 0;
3547 elif(prefchecksumtype=="crc16" or prefchecksumtype=="crc16_ansi" or prefchecksumtype=="crc16_ibm"):
3548 prenewfccs = format(crc16(prefcontents) & 0xffff, '04x').lower();
3549 elif(prefchecksumtype=="crc16_ccitt"):
3550 prenewfcs = format(crc16_ccitt(prefcontents) & 0xffff, '04x').lower();
3551 elif(prefchecksumtype=="adler32"):
3552 prenewfccs = format(zlib.adler32(prefcontents) & 0xffffffff, '08x').lower();
3553 elif(prefchecksumtype=="crc32"):
3554 prenewfccs = format(crc32(prefcontents) & 0xffffffff, '08x').lower();
3555 elif(prefchecksumtype=="crc64_ecma"):
3556 prenewfcs = format(crc64_ecma(prefcontents) & 0xffffffffffffffff, '016x').lower();
3557 elif(prefchecksumtype=="crc64" or prefchecksumtype=="crc64_iso"):
3558 prenewfcs = format(crc64_iso(prefcontents) & 0xffffffffffffffff, '016x').lower();
3559 elif(CheckSumSupportAlt(prefchecksumtype, hashlib_guaranteed)):
3560 checksumoutstr = hashlib.new(prefchecksumtype);
3561 checksumoutstr.update(prefcontents);
3562 prenewfccs = checksumoutstr.hexdigest().lower();
3563 pyhascontents = True;
3564 if(prefccs!=prenewfccs and not skipchecksum):
3565 VerbosePrintOut("File Content Checksum Error with file " + prefname + " at offset " + str(prefcontentstart));
3566 return False;
3567 catfp.seek(1, 1);
3568 il = il + 1;
3569 fileidnum = seekstart;
3570 realidnum = 0;
3571 while(fileidnum<seekend):
3572 catfhstart = catfp.tell();
3573 if(formatspecs[7]):
3574 catheaderdata = ReadFileHeaderDataBySize(catfp, formatspecs[5]);
3575 else:
3576 catheaderdata = ReadFileHeaderData(catfp, 23, formatspecs[5]);
3577 catfheadsize = int(catheaderdata[0], 16);
3578 catftype = int(catheaderdata[1], 16);
3579 if(re.findall("^[.|/]", catheaderdata[2])):
3580 catfname = catheaderdata[2];
3581 else:
3582 catfname = "./"+catheaderdata[2];
3583 catfbasedir = os.path.dirname(catfname);
3584 catflinkname = catheaderdata[3];
3585 catfsize = int(catheaderdata[4], 16);
3586 catfatime = int(catheaderdata[5], 16);
3587 catfmtime = int(catheaderdata[6], 16);
3588 catfctime = int(catheaderdata[7], 16);
3589 catfbtime = int(catheaderdata[8], 16);
3590 catfmode = int(catheaderdata[9], 16);
3591 catfchmode = stat.S_IMODE(catfmode);
3592 catftypemod = stat.S_IFMT(catfmode);
3593 catfuid = int(catheaderdata[10], 16);
3594 catfuname = catheaderdata[11];
3595 catfgid = int(catheaderdata[12], 16);
3596 catfgname = catheaderdata[13];
3597 fid = int(catheaderdata[14], 16);
3598 finode = int(catheaderdata[15], 16);
3599 flinkcount = int(catheaderdata[16], 16);
3600 catfdev_minor = int(catheaderdata[17], 16);
3601 catfdev_major = int(catheaderdata[18], 16);
3602 catfrdev_minor = int(catheaderdata[19], 16);
3603 catfrdev_major = int(catheaderdata[20], 16);
3604 catfextrasize = int(catheaderdata[21], 16);
3605 catfextrafields = int(catheaderdata[22], 16);
3606 extrafieldslist = [];
3607 if(formatspecs[7]):
3608 extrastart = 23;
3609 extraend = extrastart + catfextrafields;
3610 extrafieldslist = [];
3611 if(extrastart<extraend):
3612 extrafieldslist.append(catheaderdata[extrastart]);
3613 extrastart = extrastart + 1;
3614 catfchecksumtype = catheaderdata[extrastart].lower();
3615 catfcs = catheaderdata[extrastart + 1].lower();
3616 catfccs = catheaderdata[extrastart + 2].lower();
3617 else:
3618 extrafieldslist = [];
3619 if(formatspecs[7]):
3620 extrafieldslist = ReadFileHeaderData(catfp, catfextrafields, formatspecs[5]);
3621 checksumsval = ReadFileHeaderData(catfp, 3, formatspecs[5]);
3622 catfchecksumtype = checksumsval[0].lower();
3623 catfcs = checksumsval[1].lower();
3624 catfccs = checksumsval[2].lower();
3625 hc = 0;
3626 if(formatspecs[7]):
3627 hcmax = len(catheaderdata) - 2;
3628 else:
3629 hcmax = len(catheaderdata);
3630 hout = "";
3631 while(hc<hcmax):
3632 hout = hout + AppendNullByte(catheaderdata[hc], formatspecs[5]);
3633 hc = hc + 1;
3634 catfnumfields = 24 + catfextrafields;
3635 if(catfchecksumtype=="none" or catfchecksumtype==""):
3636 catnewfcs = 0;
3637 elif(catfchecksumtype=="crc16" or catfchecksumtype=="crc16_ansi" or catfchecksumtype=="crc16_ibm"):
3638 catnewfcs = format(crc16(hout.encode('UTF-8')) & 0xffff, '04x').lower();
3639 elif(catfchecksumtype=="adler32"):
3640 catnewfcs = format(zlib.adler32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
3641 elif(catfchecksumtype=="crc32"):
3642 catnewfcs = format(crc32(hout.encode('UTF-8')) & 0xffffffff, '08x').lower();
3643 elif(catfchecksumtype=="crc64_ecma"):
3644 catnewfcs = format(crc64_ecma(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3645 elif(catfchecksumtype=="crc64" or catfchecksumtype=="crc64_iso"):
3646 catnewfcs = format(crc64_iso(hout.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3647 elif(CheckSumSupportAlt(catfchecksumtype, hashlib_guaranteed)):
3648 checksumoutstr = hashlib.new(catfchecksumtype);
3649 checksumoutstr.update(hout.encode('UTF-8'));
3650 catnewfcs = checksumoutstr.hexdigest().lower();
3651 if(catfcs!=catnewfcs and not skipchecksum):
3652 VerbosePrintOut("File Header Checksum Error with file " + catfname + " at offset " + str(catfhstart));
3653 return False;
3654 catfhend = catfp.tell() - 1;
3655 catfcontentstart = catfp.tell();
3656 catfcontents = "";
3657 pyhascontents = False;
3658 if(catfsize>0 and not listonly):
3659 catfcontents = catfp.read(catfsize);
3660 if(catfchecksumtype=="none" or catfchecksumtype==""):
3661 catnewfccs = 0;
3662 elif(catfchecksumtype=="crc16" or catfchecksumtype=="crc16_ansi" or catfchecksumtype=="crc16_ibm"):
3663 catnewfccs = format(crc16(catfcontents) & 0xffff, '04x').lower();
3664 elif(catfchecksumtype=="crc16_ccitt"):
3665 catnewfcs = format(crc16_ccitt(catfcontents) & 0xffff, '04x').lower();
3666 elif(catfchecksumtype=="adler32"):
3667 catnewfccs = format(zlib.adler32(catfcontents) & 0xffffffff, '08x').lower();
3668 elif(catfchecksumtype=="crc32"):
3669 catnewfccs = format(crc32(catfcontents) & 0xffffffff, '08x').lower();
3670 elif(catfchecksumtype=="crc64_ecma"):
3671 catnewfcs = format(crc64_ecma(catfcontents) & 0xffffffffffffffff, '016x').lower();
3672 elif(catfchecksumtype=="crc64" or catfchecksumtype=="crc64_iso"):
3673 catnewfcs = format(crc64_iso(catfcontents) & 0xffffffffffffffff, '016x').lower();
3674 elif(CheckSumSupportAlt(catfchecksumtype, hashlib_guaranteed)):
3675 checksumoutstr = hashlib.new(catfchecksumtype);
3676 checksumoutstr.update(catfcontents);
3677 catnewfccs = checksumoutstr.hexdigest().lower();
3678 pyhascontents = True;
3679 if(catfccs!=catnewfccs and skipchecksum):
3680 VerbosePrintOut("File Content Checksum Error with file " + catfname + " at offset " + str(catfcontentstart));
3681 return False;
3682 if(catfsize>0 and listonly):
3683 catfp.seek(catfsize, 1);
3684 pyhascontents = False;
3685 catfp.seek(1, 1);
3686 catfcontentend = catfp.tell() - 1;
3687 catlist['ffilelist'].update({realidnum: {'fid': realidnum, 'fidalt': fileidnum, 'fheadersize': catfheadsize, 'fhstart': catfhstart, 'fhend': catfhend, 'ftype': catftype, 'fname': catfname, 'fbasedir': catfbasedir, 'flinkname': catflinkname, 'fsize': catfsize, 'fatime': catfatime, 'fmtime': catfmtime, 'fctime': catfctime, 'fbtime': catfbtime, 'fmode': catfmode, 'fchmode': catfchmode, 'ftypemod': catftypemod, 'fuid': catfuid, 'funame': catfuname, 'fgid': catfgid, 'fgname': catfgname, 'finode': finode, 'flinkcount': flinkcount, 'fminor': catfdev_minor, 'fmajor': catfdev_major, 'frminor': catfrdev_minor, 'frmajor': catfrdev_major, 'fchecksumtype': catfchecksumtype, 'fnumfields': catfnumfields, 'fextrafields': catfextrafields, 'fextrafieldsize': catfextrasize, 'fextralist': extrafieldslist, 'fheaderchecksum': catfcs, 'fcontentchecksum': catfccs, 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': catfcontents} });
3688 fileidnum = fileidnum + 1;
3689 realidnum = realidnum + 1;
3690 if(returnfp):
3691 catlist.update({'catfp': catfp});
3692 else:
3693 catfp.close();
3694 return catlist;
3696 create_alias_function("", __file_format_name__, "ToArray", ArchiveFileToArray);
3698 def ArchiveFileStringToArray(catstr, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
3699 catfp = BytesIO(catstr);
3700 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3701 return listcatfiles;
3703 create_alias_function("", __file_format_name__, "StringToArray", ArchiveFileStringToArray);
3705 def TarFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
3706 catfp = BytesIO();
3707 catfp = PackArchiveFileFromTarFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
3708 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3709 return listcatfiles;
3711 def ZipFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
3712 catfp = BytesIO();
3713 catfp = PackArchiveFileFromZipFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
3714 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3715 return listcatfiles;
3717 if(not rarfile_support):
3718 def RarFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
3719 return False;
3721 if(rarfile_support):
3722 def RarFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
3723 catfp = BytesIO();
3724 catfp = PackArchiveFileFromRarFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
3725 listcatfiles = ArchiveFileToArray(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
3726 return listcatfiles;
3728 def ListDirToArrayAlt(infiles, dirlistfromtxt=False, followlink=False, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
3729 catver = formatspecs[6];
3730 fileheaderver = str(int(catver.replace(".", "")));
3731 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
3732 advancedlist = True;
3733 infilelist = [];
3734 if(infiles=="-"):
3735 for line in sys.stdin:
3736 infilelist.append(line.strip());
3737 infilelist = list(filter(None, infilelist));
3738 elif(infiles!="-" and dirlistfromtxt and os.path.exists(infiles) and (os.path.isfile(infiles) or infiles=="/dev/null" or infiles=="NUL")):
3739 if(not os.path.exists(infiles) or not os.path.isfile(infiles)):
3740 return False;
3741 with open(infiles, "r") as finfile:
3742 for line in finfile:
3743 infilelist.append(line.strip());
3744 infilelist = list(filter(None, infilelist));
3745 else:
3746 if(isinstance(infiles, (list, tuple, ))):
3747 infilelist = list(filter(None, infiles));
3748 elif(isinstance(infiles, (str, ))):
3749 infilelist = list(filter(None, [infiles]));
3750 if(advancedlist):
3751 GetDirList = ListDirAdvanced(infilelist, followlink, False);
3752 else:
3753 GetDirList = ListDir(infilelist, followlink, False);
3754 if(not GetDirList):
3755 return False;
3756 curinode = 0;
3757 curfid = 0;
3758 inodelist = [];
3759 inodetofile = {};
3760 filetoinode = {};
3761 inodetocatinode = {};
3762 fileidnum = 0;
3763 fnumfiles = int(len(GetDirList));
3764 catver = formatspecs[6];
3765 fileheaderver = str(int(catver.replace(".", "")));
3766 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
3767 fnumfileshex = format(int(fnumfiles), 'x').lower();
3768 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
3769 catversion = re.findall(r"([\d]+)$", fileheader);
3770 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
3771 if(checksumtype=="none" or checksumtype==""):
3772 catfileheadercshex = format(0, 'x').lower();
3773 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3774 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3775 elif(checksumtype=="crc16_ccitt"):
3776 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
3777 elif(checksumtype=="adler32"):
3778 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3779 elif(checksumtype=="crc32"):
3780 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
3781 elif(checksumtype=="crc64_ecma"):
3782 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3783 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3784 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3785 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3786 checksumoutstr = hashlib.new(checksumtype);
3787 checksumoutstr.update(fileheader.encode('UTF-8'));
3788 catfileheadercshex = checksumoutstr.hexdigest().lower();
3789 else:
3790 catfileheadercshex = format(0, 'x').lower();
3791 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
3792 fheadtell = len(fileheader);
3793 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
3794 for curfname in GetDirList:
3795 if(re.findall("^[.|/]", curfname)):
3796 fname = curfname;
3797 else:
3798 fname = "./"+curfname;
3799 if(verbose):
3800 VerbosePrintOut(fname);
3801 if(not followlink or followlink is None):
3802 fstatinfo = os.lstat(fname);
3803 else:
3804 fstatinfo = os.stat(fname);
3805 fpremode = fstatinfo.st_mode;
3806 finode = fstatinfo.st_ino;
3807 flinkcount = fstatinfo.st_nlink;
3808 ftype = 0;
3809 if(stat.S_ISREG(fpremode)):
3810 ftype = 0;
3811 elif(stat.S_ISLNK(fpremode)):
3812 ftype = 2;
3813 elif(stat.S_ISCHR(fpremode)):
3814 ftype = 3;
3815 elif(stat.S_ISBLK(fpremode)):
3816 ftype = 4;
3817 elif(stat.S_ISDIR(fpremode)):
3818 ftype = 5;
3819 elif(stat.S_ISFIFO(fpremode)):
3820 ftype = 6;
3821 elif(hasattr(stat, "S_ISDOOR") and stat.S_ISDOOR(fpremode)):
3822 ftype = 8;
3823 elif(hasattr(stat, "S_ISPORT") and stat.S_ISPORT(fpremode)):
3824 ftype = 9;
3825 elif(hasattr(stat, "S_ISWHT") and stat.S_ISWHT(fpremode)):
3826 ftype = 10;
3827 else:
3828 ftype = 0;
3829 flinkname = "";
3830 fbasedir = os.path.dirname(fname);
3831 fcurfid = curfid;
3832 if(not followlink and finode!=0):
3833 if(ftype!=1):
3834 if(finode in inodelist):
3835 ftype = 1;
3836 flinkname = inodetofile[finode];
3837 fcurinode = inodetocatinode[finode];
3838 if(finode not in inodelist):
3839 inodelist.append(finode);
3840 inodetofile.update({finode: fname});
3841 inodetocatinode.update({finode: curinode});
3842 fcurinode = curinode;
3843 curinode = curinode + 1;
3844 else:
3845 fcurinode = curinode;
3846 curinode = curinode + 1;
3847 curfid = curfid + 1;
3848 if(ftype==2):
3849 flinkname = os.readlink(fname);
3850 fdev = fstatinfo.st_dev;
3851 getfdev = GetDevMajorMinor(fdev);
3852 fdev_minor = getfdev[0];
3853 fdev_major = getfdev[1];
3854 frdev = fstatinfo.st_dev;
3855 if(hasattr(fstatinfo, "st_rdev")):
3856 frdev = fstatinfo.st_rdev;
3857 else:
3858 frdev = fstatinfo.st_dev;
3859 getfrdev = GetDevMajorMinor(frdev);
3860 frdev_minor = getfrdev[0];
3861 frdev_major = getfrdev[1];
3862 if(ftype==1 or ftype==2 or ftype==3 or ftype==4 or ftype==5 or ftype==6):
3863 fsize = "0";
3864 if(ftype==0 or ftype==7):
3865 fsize = fstatinfo.st_size;
3866 fatime = fstatinfo.st_atime;
3867 fmtime = fstatinfo.st_mtime;
3868 fctime = fstatinfo.st_ctime;
3869 if(hasattr(fstatinfo, "st_birthtime")):
3870 fbtime = fstatinfo.st_birthtime;
3871 else:
3872 fbtime = fstatinfo.st_ctime;
3873 fmode = fstatinfo.st_mode;
3874 fchmode = stat.S_IMODE(fstatinfo.st_mode);
3875 ftypemod = stat.S_IFMT(fstatinfo.st_mode);
3876 fuid = fstatinfo.st_uid;
3877 fgid = fstatinfo.st_gid;
3878 funame = "";
3879 try:
3880 import pwd;
3881 try:
3882 userinfo = pwd.getpwuid(fstatinfo.st_uid);
3883 funame = userinfo.pw_name;
3884 except KeyError:
3885 funame = "";
3886 except ImportError:
3887 funame = "";
3888 fgname = "";
3889 try:
3890 import grp;
3891 try:
3892 groupinfo = grp.getgrgid(fstatinfo.st_gid);
3893 fgname = groupinfo.gr_name;
3894 except KeyError:
3895 fgname = "";
3896 except ImportError:
3897 fgname = "";
3898 fdev_minor = fdev_minor;
3899 fdev_major = fdev_major;
3900 frdev_minor = frdev_minor;
3901 frdev_major = frdev_major;
3902 finode = finode;
3903 flinkcount = flinkcount;
3904 if(hasattr(fstatinfo, "st_file_attributes")):
3905 fwinattributes = fstatinfo.st_file_attributes;
3906 else:
3907 fwinattributes = 0;
3908 fcontents = "".encode('UTF-8');
3909 chunk_size = 1024;
3910 if(ftype == 0 or ftype == 7):
3911 with open(fname, "rb") as fpc:
3912 while(True):
3913 chunk = fpc.read(chunk_size);
3914 if(not chunk):
3915 break
3916 fcontents += chunk;
3917 if(followlink and (ftype == 1 or ftype == 2)):
3918 flstatinfo = os.stat(flinkname);
3919 with open(flinkname, "rb") as fpc:
3920 while(True):
3921 chunk = fpc.read(chunk_size);
3922 if(not chunk):
3923 break;
3924 fcontents += chunk;
3925 ftypehex = format(ftype, 'x').lower();
3926 extrafields = len(extradata);
3927 extrafieldslist = extradata;
3928 catfextrafields = extrafields;
3929 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
3930 if(len(extradata)>0):
3931 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
3932 extrasizelen = len(extrasizestr);
3933 extrasizelenhex = format(extrasizelen, 'x').lower();
3934 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, format(int(fsize), 'x').lower(), format(int(fatime), 'x').lower(), format(int(fmtime), 'x').lower(), format(int(fctime), 'x').lower(), format(int(fbtime), 'x').lower(), format(int(fmode), 'x').lower(), format(int(fuid), 'x').lower(), funame, format(int(fgid), 'x').lower(), fgname, format(int(fcurfid), 'x').lower(), format(int(fcurinode), 'x').lower(), format(int(flinkcount), 'x').lower(), format(int(fdev_minor), 'x').lower(), format(int(fdev_major), 'x').lower(), format(int(frdev_minor), 'x').lower(), format(int(frdev_major), 'x').lower(), extrasizelenhex, format(catfextrafields, 'x').lower()], formatspecs[5]);
3935 if(len(extradata)>0):
3936 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
3937 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
3938 catfnumfields = 24 + catfextrafields;
3939 if(checksumtype=="none" or checksumtype==""):
3940 catfileheadercshex = format(0, 'x').lower();
3941 catfilecontentcshex = format(0, 'x').lower();
3942 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3943 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
3944 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
3945 elif(checksumtype=="crc16_ccitt"):
3946 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
3947 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
3948 elif(checksumtype=="adler32"):
3949 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3950 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
3951 elif(checksumtype=="crc32"):
3952 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
3953 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
3954 elif(checksumtype=="crc64_ecma"):
3955 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3956 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
3957 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3958 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3959 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
3960 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3961 checksumoutstr = hashlib.new(checksumtype);
3962 checksumoutstr.update("".encode('UTF-8'));
3963 catfileheadercshex = checksumoutstr.hexdigest().lower();
3964 checksumoutstr = hashlib.new(checksumtype);
3965 checksumoutstr.update(fcontents);
3966 catfilecontentcshex = checksumoutstr.hexdigest().lower();
3967 else:
3968 catfileheadercshex = format(0, 'x').lower();
3969 catfilecontentcshex = format(0, 'x').lower();
3970 catfhstart = fheadtell;
3971 fheadtell += len(catfileoutstr);
3972 catfhend = fheadtell - 1;
3973 catfcontentstart = fheadtell;
3974 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
3975 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
3976 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
3977 if(checksumtype=="none" or checksumtype==""):
3978 catfileheadercshex = format(0, 'x').lower();
3979 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
3980 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
3981 elif(checksumtype=="crc16_ccitt"):
3982 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
3983 elif(checksumtype=="adler32"):
3984 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
3985 elif(checksumtype=="crc32"):
3986 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
3987 elif(checksumtype=="crc64_ecma"):
3988 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3989 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
3990 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
3991 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
3992 checksumoutstr = hashlib.new(checksumtype);
3993 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
3994 catfileheadercshex = checksumoutstr.hexdigest().lower();
3995 else:
3996 catfileheadercshex = format(0, 'x').lower();
3997 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
3998 catfileoutstrecd = catfileoutstr.encode('UTF-8');
3999 nullstrecd = formatspecs[5].encode('UTF-8');
4000 fheadtell += len(catfileoutstr) + 1;
4001 catfcontentend = fheadtell - 1;
4002 catfileout = catfileoutstrecd + fcontents + nullstrecd;
4003 pyhascontents = False;
4004 if(int(fsize)>0 and not listonly):
4005 pyhascontents = True;
4006 if(int(fsize)>0 and listonly):
4007 fcontents = "";
4008 pyhascontents = False;
4009 catlist['ffilelist'].update({fileidnum: {'fid': fileidnum, 'fidalt': fileidnum, 'fheadersize': int(catheaersize, 16), 'fhstart': catfhstart, 'fhend': catfhend, 'ftype': ftype, 'fname': fname, 'fbasedir': fbasedir, 'flinkname': flinkname, 'fsize': fsize, 'fatime': fatime, 'fmtime': fmtime, 'fctime': fctime, 'fbtime': fbtime, 'fmode': fmode, 'fchmode': fchmode, 'ftypemod': ftypemod, 'fuid': fuid, 'funame': funame, 'fgid': fgid, 'fgname': fgname, 'finode': finode, 'flinkcount': flinkcount, 'fminor': fdev_minor, 'fmajor': fdev_major, 'frminor': frdev_minor, 'frmajor': frdev_major, 'fchecksumtype': checksumtype, 'fnumfields': catfnumfields, 'fextrafields': catfextrafields, 'fextrafieldsize': extrasizelen, 'fextralist': extrafieldslist, 'fheaderchecksum': int(catfileheadercshex, 16), 'fcontentchecksum': int(catfilecontentcshex, 16), 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': fcontents} });
4010 fileidnum = fileidnum + 1;
4011 return catlist;
4013 def TarFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
4014 curinode = 0;
4015 curfid = 0;
4016 inodelist = [];
4017 inodetofile = {};
4018 filetoinode = {};
4019 inodetocatinode = {};
4020 fileidnum = 0;
4021 if(infile=="-"):
4022 infile = BytesIO();
4023 if(hasattr(sys.stdin, "buffer")):
4024 shutil.copyfileobj(sys.stdin.buffer, infile);
4025 else:
4026 shutil.copyfileobj(sys.stdin, infile);
4027 infile.seek(0, 0);
4028 if(not infile):
4029 return False;
4030 infile.seek(0, 0);
4031 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
4032 infile = download_file_from_internet_file(infile);
4033 infile.seek(0, 0);
4034 if(not infile):
4035 return False;
4036 infile.seek(0, 0);
4037 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
4038 return False;
4039 elif(os.path.exists(infile) and os.path.isfile(infile)):
4040 try:
4041 if(not tarfile.is_tarfile(infile)):
4042 return False;
4043 except AttributeError:
4044 if(not is_tarfile(infile)):
4045 return False;
4046 else:
4047 return False;
4048 try:
4049 if(hasattr(infile, "read") or hasattr(infile, "write")):
4050 tarfp = tarfile.open(fileobj=infile, mode="r");
4051 else:
4052 tarfp = tarfile.open(infile, "r");
4053 except FileNotFoundError:
4054 return False;
4055 fnumfiles = int(len(tarfp.getmembers()));
4056 catver = formatspecs[6];
4057 fileheaderver = str(int(catver.replace(".", "")));
4058 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
4059 fnumfileshex = format(int(fnumfiles), 'x').lower();
4060 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
4061 catversion = re.findall(r"([\d]+)$", fileheader);
4062 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
4063 if(checksumtype=="none" or checksumtype==""):
4064 catfileheadercshex = format(0, 'x').lower();
4065 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4066 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4067 elif(checksumtype=="crc16_ccitt"):
4068 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4069 elif(checksumtype=="adler32"):
4070 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4071 elif(checksumtype=="crc32"):
4072 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4073 elif(checksumtype=="crc64_ecma"):
4074 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4075 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4076 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4077 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4078 checksumoutstr = hashlib.new(checksumtype);
4079 checksumoutstr.update(fileheader.encode('UTF-8'));
4080 catfileheadercshex = checksumoutstr.hexdigest().lower();
4081 else:
4082 catfileheadercshex = format(0, 'x').lower();
4083 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
4084 fheadtell = len(fileheader);
4085 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
4086 for member in sorted(tarfp.getmembers(), key=lambda x: x.name):
4087 if(re.findall("^[.|/]", member.name)):
4088 fname = member.name;
4089 else:
4090 fname = "./"+member.name;
4091 if(verbose):
4092 VerbosePrintOut(fname);
4093 fpremode = member.mode;
4094 ffullmode = member.mode;
4095 flinkcount = 0;
4096 ftype = 0;
4097 if(member.isreg()):
4098 ffullmode = member.mode + stat.S_IFREG;
4099 ftype = 0;
4100 elif(member.isdev()):
4101 ffullmode = member.mode;
4102 ftype = 7;
4103 elif(member.islnk()):
4104 ffullmode = member.mode + stat.S_IFREG;
4105 ftype = 1;
4106 elif(member.issym()):
4107 ffullmode = member.mode + stat.S_IFLNK;
4108 ftype = 2;
4109 elif(member.ischr()):
4110 ffullmode = member.mode + stat.S_IFCHR;
4111 ftype = 3;
4112 elif(member.isblk()):
4113 ffullmode = member.mode + stat.S_IFBLK;
4114 ftype = 4;
4115 elif(member.isdir()):
4116 ffullmode = member.mode + stat.S_IFDIR;
4117 ftype = 5;
4118 elif(member.isfifo()):
4119 ffullmode = member.mode + stat.S_IFIFO;
4120 ftype = 6;
4121 elif(member.issparse()):
4122 ffullmode = member.mode;
4123 ftype = 11;
4124 else:
4125 ffullmode = member.mode;
4126 ftype = 0;
4127 flinkname = "";
4128 fbasedir = os.path.dirname(fname);
4129 fcurfid = curfid;
4130 fcurinode = 0;
4131 finode = fcurinode;
4132 curfid = curfid + 1;
4133 if(ftype==2):
4134 flinkname = member.linkname;
4135 fdev_minor = member.devminor;
4136 fdev_major = member.devmajor;
4137 frdev_minor = member.devminor;
4138 frdev_major = member.devmajor;
4139 if(ftype==1 or ftype==2 or ftype==3 or ftype==4 or ftype==5 or ftype==6):
4140 fsize = "0";
4141 elif(ftype==0 or ftype==7):
4142 fsize = member.size;
4143 else:
4144 fsize = member.size;
4145 fatime = member.mtime;
4146 fmtime = member.mtime;
4147 fctime = member.mtime;
4148 fbtime = member.mtime;
4149 fmode = ffullmode;
4150 fchmode = stat.S_IMODE(ffullmode);
4151 ftypemod = stat.S_IFMT(ffullmode);
4152 fuid = member.uid;
4153 fgid = member.gid;
4154 funame = member.uname;
4155 fgname = member.gname;
4156 flinkcount = flinkcount;
4157 fcontents = "".encode('UTF-8');
4158 chunk_size = 1024;
4159 if(ftype == 0 or ftype == 7):
4160 with tarfp.extractfile(member) as fpc:
4161 while(True):
4162 chunk = fpc.read(chunk_size);
4163 if(not chunk):
4164 break
4165 fcontents += chunk;
4166 ftypehex = format(ftype, 'x').lower();
4167 extrafields = len(extradata);
4168 extrafieldslist = extradata;
4169 catfextrafields = extrafields;
4170 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
4171 if(len(extradata)>0):
4172 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
4173 extrasizelen = len(extrasizestr);
4174 extrasizelenhex = format(extrasizelen, 'x').lower();
4175 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, format(int(fsize), 'x').lower(), format(int(fatime), 'x').lower(), format(int(fmtime), 'x').lower(), format(int(fctime), 'x').lower(), format(int(fbtime), 'x').lower(), format(int(fmode), 'x').lower(), format(int(fuid), 'x').lower(), funame, format(int(fgid), 'x').lower(), fgname, format(int(fcurfid), 'x').lower(), format(int(fcurinode), 'x').lower(), format(int(flinkcount), 'x').lower(), format(int(fdev_minor), 'x').lower(), format(int(fdev_major), 'x').lower(), format(int(frdev_minor), 'x').lower(), format(int(frdev_major), 'x').lower(), extrasizelenhex, format(catfextrafields, 'x').lower()], formatspecs[5]);
4176 if(len(extradata)>0):
4177 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
4178 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
4179 catfnumfields = 24 + catfextrafields;
4180 if(checksumtype=="none" or checksumtype==""):
4181 catfileheadercshex = format(0, 'x').lower();
4182 catfilecontentcshex = format(0, 'x').lower();
4183 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4184 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
4185 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
4186 elif(checksumtype=="crc16_ccitt"):
4187 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
4188 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
4189 elif(checksumtype=="adler32"):
4190 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4191 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
4192 elif(checksumtype=="crc32"):
4193 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4194 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
4195 elif(checksumtype=="crc64_ecma"):
4196 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4197 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
4198 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4199 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4200 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
4201 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4202 checksumoutstr = hashlib.new(checksumtype);
4203 checksumoutstr.update("".encode('UTF-8'));
4204 catfileheadercshex = checksumoutstr.hexdigest().lower();
4205 checksumoutstr = hashlib.new(checksumtype);
4206 checksumoutstr.update(fcontents);
4207 catfilecontentcshex = checksumoutstr.hexdigest().lower();
4208 else:
4209 catfileheadercshex = format(0, 'x').lower();
4210 catfilecontentcshex = format(0, 'x').lower();
4211 catfhstart = fheadtell;
4212 fheadtell += len(catfileoutstr);
4213 catfhend = fheadtell - 1;
4214 catfcontentstart = fheadtell;
4215 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
4216 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
4217 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
4218 if(checksumtype=="none" or checksumtype==""):
4219 catfileheadercshex = format(0, 'x').lower();
4220 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4221 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
4222 elif(checksumtype=="crc16_ccitt"):
4223 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
4224 elif(checksumtype=="adler32"):
4225 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
4226 elif(checksumtype=="crc32"):
4227 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
4228 elif(checksumtype=="crc64_ecma"):
4229 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4230 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4231 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4232 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4233 checksumoutstr = hashlib.new(checksumtype);
4234 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
4235 catfileheadercshex = checksumoutstr.hexdigest().lower();
4236 else:
4237 catfileheadercshex = format(0, 'x').lower();
4238 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
4239 catfileoutstrecd = catfileoutstr.encode('UTF-8');
4240 nullstrecd = formatspecs[5].encode('UTF-8');
4241 fheadtell += len(catfileoutstr) + 1;
4242 catfcontentend = fheadtell - 1;
4243 catfileout = catfileoutstrecd + fcontents + nullstrecd;
4244 pyhascontents = False;
4245 if(int(fsize)>0 and not listonly):
4246 pyhascontents = True;
4247 if(int(fsize)>0 and listonly):
4248 fcontents = "";
4249 pyhascontents = False;
4250 catlist['ffilelist'].update({fileidnum: {'fid': fileidnum, 'fidalt': fileidnum, 'fheadersize': int(catheaersize, 16), 'fhstart': catfhstart, 'fhend': catfhend, 'ftype': ftype, 'fname': fname, 'fbasedir': fbasedir, 'flinkname': flinkname, 'fsize': fsize, 'fatime': fatime, 'fmtime': fmtime, 'fctime': fctime, 'fbtime': fbtime, 'fmode': fmode, 'fchmode': fchmode, 'ftypemod': ftypemod, 'fuid': fuid, 'funame': funame, 'fgid': fgid, 'fgname': fgname, 'finode': finode, 'flinkcount': flinkcount, 'fminor': fdev_minor, 'fmajor': fdev_major, 'frminor': frdev_minor, 'frmajor': frdev_major, 'fchecksumtype': checksumtype, 'fnumfields': catfnumfields, 'fextrafields': catfextrafields, 'fextrafieldsize': extrasizelen, 'fextralist': extrafieldslist, 'fheaderchecksum': int(catfileheadercshex, 16), 'fcontentchecksum': int(catfilecontentcshex, 16), 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': fcontents} });
4251 fileidnum = fileidnum + 1;
4252 return catlist;
4254 def ZipFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
4255 advancedlist = True;
4256 curinode = 0;
4257 curfid = 0;
4258 inodelist = [];
4259 inodetofile = {};
4260 filetoinode = {};
4261 inodetocatinode = {};
4262 fileidnum = 0;
4263 if(infile=="-"):
4264 infile = BytesIO();
4265 if(hasattr(sys.stdin, "buffer")):
4266 shutil.copyfileobj(sys.stdin.buffer, infile);
4267 else:
4268 shutil.copyfileobj(sys.stdin, infile);
4269 infile.seek(0, 0);
4270 if(not infile):
4271 return False;
4272 infile.seek(0, 0);
4273 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
4274 infile = download_file_from_internet_file(infile);
4275 infile.seek(0, 0);
4276 if(not infile):
4277 return False;
4278 infile.seek(0, 0);
4279 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
4280 return False;
4281 else:
4282 return False;
4283 if(not zipfile.is_zipfile(infile)):
4284 return False;
4285 try:
4286 zipfp = zipfile.ZipFile(infile, "r", allowZip64=True);
4287 except FileNotFoundError:
4288 return False;
4289 ziptest = zipfp.testzip();
4290 if(ziptest):
4291 VerbosePrintOut("Bad file found!");
4292 fnumfiles = int(len(zipfp.infolist()));
4293 catver = formatspecs[6];
4294 fileheaderver = str(int(catver.replace(".", "")));
4295 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
4296 catversion = re.findall(r"([\d]+)$", fileheader);
4297 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
4298 fnumfileshex = format(int(fnumfiles), 'x').lower();
4299 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
4300 if(checksumtype=="none" or checksumtype==""):
4301 catfileheadercshex = format(0, 'x').lower();
4302 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4303 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4304 elif(checksumtype=="crc16_ccitt"):
4305 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4306 elif(checksumtype=="adler32"):
4307 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4308 elif(checksumtype=="crc32"):
4309 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4310 elif(checksumtype=="crc64_ecma"):
4311 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4312 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4313 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4314 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4315 checksumoutstr = hashlib.new(checksumtype);
4316 checksumoutstr.update(fileheader.encode('UTF-8'));
4317 catfileheadercshex = checksumoutstr.hexdigest().lower();
4318 else:
4319 catfileheadercshex = format(0, 'x').lower();
4320 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
4321 fheadtell = len(fileheader);
4322 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
4323 for member in sorted(zipfp.infolist(), key=lambda x: x.filename):
4324 if(re.findall("^[.|/]", member.filename)):
4325 fname = member.filename;
4326 else:
4327 fname = "./"+member.filename;
4328 zipinfo = zipfp.getinfo(member.filename);
4329 if(verbose):
4330 VerbosePrintOut(fname);
4331 if(not member.is_dir()):
4332 fpremode = stat.S_IFREG + 438;
4333 elif(member.is_dir()):
4334 fpremode = stat.S_IFDIR + 511;
4335 flinkcount = 0;
4336 ftype = 0;
4337 if(not member.is_dir()):
4338 ftype = 0;
4339 elif(member.is_dir()):
4340 ftype = 5;
4341 flinkname = "";
4342 fbasedir = os.path.dirname(fname);
4343 fcurfid = curfid;
4344 fcurinode = 0;
4345 finode = fcurinode;
4346 curfid = curfid + 1;
4347 fdev_minor = 0;
4348 fdev_major = 0;
4349 frdev_minor = 0;
4350 frdev_major = 0;
4351 if(ftype==5):
4352 fsize = "0";
4353 elif(ftype==0):
4354 fsize = member.file_size;
4355 else:
4356 fsize = member.file_size;
4357 fatime = time.mktime(member.date_time + (0, 0, -1));
4358 fmtime = time.mktime(member.date_time + (0, 0, -1));
4359 fctime = time.mktime(member.date_time + (0, 0, -1));
4360 fbtime = time.mktime(member.date_time + (0, 0, -1));
4361 if(not member.is_dir()):
4362 fmode = stat.S_IFREG + 438;
4363 fchmode = stat.S_IMODE(int(stat.S_IFREG + 438));
4364 ftypemod = stat.S_IFMT(int(stat.S_IFREG + 438));
4365 if(member.is_dir()):
4366 fmode = stat.S_IFDIR + 511;
4367 fchmode = stat.S_IMODE(int(stat.S_IFDIR + 511));
4368 ftypemod = stat.S_IFMT(int(stat.S_IFDIR + 511));
4369 try:
4370 fuid = os.getuid();
4371 except AttributeError:
4372 fuid = 0;
4373 except KeyError:
4374 fuid = 0;
4375 try:
4376 fgid = os.getgid();
4377 except AttributeError:
4378 fgid = 0;
4379 except KeyError:
4380 fgid = 0;
4381 try:
4382 import pwd;
4383 try:
4384 userinfo = pwd.getpwuid(os.getuid());
4385 funame = userinfo.pw_name;
4386 except KeyError:
4387 funame = "";
4388 except AttributeError:
4389 funame = "";
4390 except ImportError:
4391 funame = "";
4392 fgname = "";
4393 try:
4394 import grp;
4395 try:
4396 groupinfo = grp.getgrgid(os.getgid());
4397 fgname = groupinfo.gr_name;
4398 except KeyError:
4399 fgname = "";
4400 except AttributeError:
4401 fgname = "";
4402 except ImportError:
4403 fgname = "";
4404 fcontents = "".encode('UTF-8');
4405 if(ftype==0):
4406 fcontents = zipfp.read(member.filename);
4407 ftypehex = format(ftype, 'x').lower();
4408 extrafields = len(extradata);
4409 extrafieldslist = extradata;
4410 catfextrafields = extrafields;
4411 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
4412 if(len(extradata)>0):
4413 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
4414 extrasizelen = len(extrasizestr);
4415 extrasizelenhex = format(extrasizelen, 'x').lower();
4416 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, format(int(fsize), 'x').lower(), format(int(fatime), 'x').lower(), format(int(fmtime), 'x').lower(), format(int(fctime), 'x').lower(), format(int(fbtime), 'x').lower(), format(int(fmode), 'x').lower(), format(int(fuid), 'x').lower(), funame, format(int(fgid), 'x').lower(), fgname, format(int(fcurfid), 'x').lower(), format(int(fcurinode), 'x').lower(), format(int(flinkcount), 'x').lower(), format(int(fdev_minor), 'x').lower(), format(int(fdev_major), 'x').lower(), format(int(frdev_minor), 'x').lower(), format(int(frdev_major), 'x').lower(), extrasizelenhex, format(catfextrafields, 'x').lower()], formatspecs[5]);
4417 if(len(extradata)>0):
4418 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
4419 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
4420 catfnumfields = 24 + catfextrafields;
4421 if(checksumtype=="none" or checksumtype==""):
4422 catfileheadercshex = format(0, 'x').lower();
4423 catfilecontentcshex = format(0, 'x').lower();
4424 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4425 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
4426 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
4427 elif(checksumtype=="crc16_ccitt"):
4428 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
4429 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
4430 elif(checksumtype=="adler32"):
4431 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4432 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
4433 elif(checksumtype=="crc32"):
4434 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
4435 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
4436 elif(checksumtype=="crc64_ecma"):
4437 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4438 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
4439 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4440 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4441 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
4442 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4443 checksumoutstr = hashlib.new(checksumtype);
4444 checksumoutstr.update("".encode('UTF-8'));
4445 catfileheadercshex = checksumoutstr.hexdigest().lower();
4446 checksumoutstr = hashlib.new(checksumtype);
4447 checksumoutstr.update(fcontents);
4448 catfilecontentcshex = checksumoutstr.hexdigest().lower();
4449 else:
4450 catfileheadercshex = format(0, 'x').lower();
4451 catfilecontentcshex = format(0, 'x').lower();
4452 catfhstart = fheadtell;
4453 fheadtell += len(catfileoutstr);
4454 catfhend = fheadtell - 1;
4455 catfcontentstart = fheadtell;
4456 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
4457 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
4458 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
4459 if(checksumtype=="none" or checksumtype==""):
4460 catfileheadercshex = format(0, 'x').lower();
4461 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4462 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
4463 elif(checksumtype=="crc16_ccitt"):
4464 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
4465 elif(checksumtype=="adler32"):
4466 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
4467 elif(checksumtype=="crc32"):
4468 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
4469 elif(checksumtype=="crc64_ecma"):
4470 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4471 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4472 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4473 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4474 checksumoutstr = hashlib.new(checksumtype);
4475 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
4476 catfileheadercshex = checksumoutstr.hexdigest().lower();
4477 else:
4478 catfileheadercshex = format(0, 'x').lower();
4479 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
4480 catfileoutstrecd = catfileoutstr.encode('UTF-8');
4481 nullstrecd = formatspecs[5].encode('UTF-8');
4482 fheadtell += len(catfileoutstr) + 1;
4483 catfcontentend = fheadtell - 1;
4484 catfileout = catfileoutstrecd + fcontents + nullstrecd;
4485 pyhascontents = False;
4486 if(int(fsize)>0 and not listonly):
4487 pyhascontents = True;
4488 if(int(fsize)>0 and listonly):
4489 fcontents = "";
4490 pyhascontents = False;
4491 catlist['ffilelist'].update({fileidnum: {'fid': fileidnum, 'fidalt': fileidnum, 'fheadersize': int(catheaersize, 16), 'fhstart': catfhstart, 'fhend': catfhend, 'ftype': ftype, 'fname': fname, 'fbasedir': fbasedir, 'flinkname': flinkname, 'fsize': fsize, 'fatime': fatime, 'fmtime': fmtime, 'fctime': fctime, 'fbtime': fbtime, 'fmode': fmode, 'fchmode': fchmode, 'ftypemod': ftypemod, 'fuid': fuid, 'funame': funame, 'fgid': fgid, 'fgname': fgname, 'finode': finode, 'flinkcount': flinkcount, 'fminor': fdev_minor, 'fmajor': fdev_major, 'frminor': frdev_minor, 'frmajor': frdev_major, 'fchecksumtype': checksumtype, 'fnumfields': catfnumfields, 'fextrafields': catfextrafields, 'fextrafieldsize': extrasizelen, 'fextralist': extrafieldslist, 'fheaderchecksum': int(catfileheadercshex, 16), 'fcontentchecksum': int(catfilecontentcshex, 16), 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': fcontents} });
4492 fileidnum = fileidnum + 1;
4493 return catlist;
4495 if(not rarfile_support):
4496 def RarFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
4497 return False;
4499 if(rarfile_support):
4500 def RarFileToArrayAlt(infiles, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
4501 advancedlist = True;
4502 curinode = 0;
4503 curfid = 0;
4504 inodelist = [];
4505 inodetofile = {};
4506 filetoinode = {};
4507 inodetocatinode = {};
4508 fileidnum = 0;
4509 if(not os.path.exists(infiles) or not os.path.isfile(infiles)):
4510 return False;
4511 if(not rarfile.is_rarfile(infile) and not rarfile.is_rarfile_sfx(infile)):
4512 return False;
4513 rarfp = rarfile.RarFile(infile, "r");
4514 rartest = rarfp.testrar();
4515 if(rartest):
4516 VerbosePrintOut("Bad file found!");
4517 fnumfiles = int(len(rarfp.infolist()));
4518 catver = formatspecs[6];
4519 fileheaderver = str(int(catver.replace(".", "")));
4520 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
4521 catversion = re.findall(r"([\d]+)$", fileheader);
4522 catversions = re.search(r'(.*?)(\d+)$', catstring).groups();
4523 fnumfileshex = format(int(fnumfiles), 'x').lower();
4524 fileheader = fileheader + AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
4525 if(checksumtype=="none" or checksumtype==""):
4526 catfileheadercshex = format(0, 'x').lower();
4527 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4528 catfileheadercshex = format(crc16(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4529 elif(checksumtype=="crc16_ccitt"):
4530 catfileheadercshex = format(crc16_ccitt(fileheader.encode('UTF-8')) & 0xffff, '04x').lower();
4531 elif(checksumtype=="adler32"):
4532 catfileheadercshex = format(zlib.adler32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4533 elif(checksumtype=="crc32"):
4534 catfileheadercshex = format(crc32(fileheader.encode('UTF-8')) & 0xffffffff, '08x').lower();
4535 elif(checksumtype=="crc64_ecma"):
4536 catfileheadercshex = format(crc64_ecma(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4537 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4538 catfileheadercshex = format(crc64_iso(fileheader.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4539 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4540 checksumoutstr = hashlib.new(checksumtype);
4541 checksumoutstr.update(fileheader.encode('UTF-8'));
4542 catfileheadercshex = checksumoutstr.hexdigest().lower();
4543 else:
4544 catfileheadercshex = format(0, 'x').lower();
4545 fileheader = fileheader + AppendNullByte(catfileheadercshex, formatspecs[5]);
4546 fheadtell = len(fileheader);
4547 catlist = {'fnumfiles': fnumfiles, 'fformat': catversions[0], 'fversion': catversions[1], 'fformatspecs': formatspecs, 'fchecksumtype': checksumtype, 'fheaderchecksum': catfileheadercshex, 'ffilelist': {}};
4548 for member in sorted(rarfp.infolist(), key=lambda x: x.filename):
4549 is_unix = False;
4550 is_windows = False;
4551 if(member.host_os == rarfile.RAR_OS_UNIX):
4552 is_windows = False;
4553 try:
4554 member.external_attr
4555 is_unix = True;
4556 except AttributeError:
4557 is_unix = False;
4558 elif(member.host_os == rarfile.RAR_OS_WIN32):
4559 is_unix = False;
4560 try:
4561 member.external_attr
4562 is_windows = True;
4563 except AttributeError:
4564 is_windows = False;
4565 else:
4566 is_unix = False;
4567 is_windows = False;
4568 if(re.findall("^[.|/]", member.filename)):
4569 fname = member.filename;
4570 else:
4571 fname = "./"+member.filename;
4572 rarinfo = rarfp.getinfo(member.filename);
4573 if(verbose):
4574 VerbosePrintOut(fname);
4575 if(is_unix and member.external_attr !=0):
4576 fpremode = int(member.external_attr);
4577 elif(member.is_file()):
4578 fpremode = stat.S_IFREG + 438;
4579 elif(member.is_symlink()):
4580 fpremode = stat.S_IFLNK + 438;
4581 elif(member.is_dir()):
4582 fpremode = stat.S_IFDIR + 511;
4583 if(is_windows and member.external_attr !=0):
4584 fwinattributes = int(member.external_attr);
4585 else:
4586 fwinattributes = int(0);
4587 flinkcount = 0;
4588 ftype = 0;
4589 if(member.is_file()):
4590 ftype = 0;
4591 elif(member.is_symlink()):
4592 ftype = 2;
4593 elif(member.is_dir()):
4594 ftype = 5;
4595 flinkname = "";
4596 if(ftype==2):
4597 flinkname = rarfp.read(member.filename).decode("UTF-8");
4598 fbasedir = os.path.dirname(fname);
4599 fcurfid = curfid;
4600 fcurinode = 0;
4601 finode = fcurinode;
4602 curfid = curfid + 1;
4603 fdev_minor = 0;
4604 fdev_major = 0;
4605 frdev_minor = 0;
4606 frdev_major = 0;
4607 if(ftype==5):
4608 fsize = "0";
4609 if(ftype==0):
4610 fsize = member.file_size;
4611 try:
4612 if(member.atime):
4613 fatime = int(member.atime.timestamp());
4614 else:
4615 fatime = int(member.mtime.timestamp());
4616 except AttributeError:
4617 fatime = int(member.mtime.timestamp());
4618 fmtime = int(member.mtime.timestamp());
4619 try:
4620 if(member.ctime):
4621 fctime = int(member.ctime.timestamp());
4622 else:
4623 fctime = int(member.mtime.timestamp());
4624 except AttributeError:
4625 fctime = int(member.mtime.timestamp());
4626 fbtime = int(member.mtime.timestamp());
4627 if(is_unix and member.external_attr !=0):
4628 fmode = format(int(member.external_attr), 'x').lower();
4629 fchmode = format(int(stat.S_IMODE(member.external_attr)), 'x').lower();
4630 ftypemod = format(int(stat.S_IFMT(member.external_attr)), 'x').lower();
4631 elif(member.is_file()):
4632 fmode = int(stat.S_IFREG + 438)
4633 fchmode = int(stat.S_IMODE(stat.S_IFREG + 438));
4634 ftypemod = int(stat.S_IFMT(stat.S_IFREG + 438));
4635 elif(member.is_symlink()):
4636 fmode = int(stat.S_IFLNK + 438)
4637 fchmode = int(stat.S_IMODE(stat.S_IFREG + 438));
4638 ftypemod = int(stat.S_IFMT(stat.S_IFREG + 438));
4639 elif(member.is_dir()):
4640 fmode = int(stat.S_IFDIR + 511)
4641 fchmode = int(stat.S_IMODE(stat.S_IFDIR + 511));
4642 ftypemod = int(stat.S_IFMT(stat.S_IFDIR + 511));
4643 try:
4644 fuid = os.getuid();
4645 except AttributeError:
4646 fuid = 0;
4647 except KeyError:
4648 fuid = 0;
4649 try:
4650 fgid = os.getgid();
4651 except AttributeError:
4652 fgid = 0;
4653 except KeyError:
4654 fgid = 0;
4655 try:
4656 import pwd;
4657 try:
4658 userinfo = pwd.getpwuid(os.getuid());
4659 funame = userinfo.pw_name;
4660 except KeyError:
4661 funame = "";
4662 except AttributeError:
4663 funame = "";
4664 except ImportError:
4665 funame = "";
4666 fgname = "";
4667 try:
4668 import grp;
4669 try:
4670 groupinfo = grp.getgrgid(os.getgid());
4671 fgname = groupinfo.gr_name;
4672 except KeyError:
4673 fgname = "";
4674 except AttributeError:
4675 fgname = "";
4676 except ImportError:
4677 fgname = "";
4678 fcontents = "".encode('UTF-8');
4679 if(ftype==0):
4680 fcontents = rarfp.read(member.filename);
4681 ftypehex = format(ftype, 'x').lower();
4682 extrafields = len(extradata);
4683 extrafieldslist = extradata;
4684 catfextrafields = extrafields;
4685 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
4686 if(len(extradata)>0):
4687 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
4688 extrasizelen = len(extrasizestr);
4689 extrasizelenhex = format(extrasizelen, 'x').lower();
4690 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, format(int(fsize), 'x').lower(), format(int(fatime), 'x').lower(), format(int(fmtime), 'x').lower(), format(int(fctime), 'x').lower(), format(int(fbtime), 'x').lower(), format(int(fmode), 'x').lower(), format(int(fuid), 'x').lower(), funame, format(int(fgid), 'x').lower(), fgname, format(int(fcurfid), 'x').lower(), format(int(fcurinode), 'x').lower(), format(int(flinkcount), 'x').lower(), format(int(fdev_minor), 'x').lower(), format(int(fdev_major), 'x').lower(), format(int(frdev_minor), 'x').lower(), format(int(frdev_major), 'x').lower(), extrasizelenhex, format(catfextrafields, 'x').lower()], formatspecs[5]);
4691 if(len(extradata)>0):
4692 catfileoutstr = catfileoutstr + AppendNullBytes(extradata, formatspecs[5]);
4693 catfileoutstr = catfileoutstr + AppendNullByte(checksumtype, formatspecs[5]);
4694 catfnumfields = 24 + catfextrafields;
4695 if(checksumtype=="none" or checksumtype==""):
4696 catfileheadercshex = format(0, 'x').lower();
4697 catfilecontentcshex = format(0, 'x').lower();
4698 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4699 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
4700 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
4701 elif(checksumtype=="crc16_ccitt"):
4702 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
4703 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
4704 elif(checksumtype=="adler32"):
4705 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
4706 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
4707 elif(checksumtype=="crc32"):
4708 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
4709 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
4710 elif(checksumtype=="crc64_ecma"):
4711 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4712 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
4713 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4714 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4715 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
4716 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4717 checksumoutstr = hashlib.new(checksumtype);
4718 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
4719 catfileheadercshex = checksumoutstr.hexdigest().lower();
4720 else:
4721 catfileheadercshex = format(0, 'x').lower();
4722 catfilecontentcshex = format(0, 'x').lower();
4723 catfhstart = fheadtell;
4724 fheadtell += len(catfileoutstr);
4725 catfhend = fheadtell - 1;
4726 catfcontentstart = fheadtell;
4727 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
4728 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
4729 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
4730 if(checksumtype=="none" or checksumtype==""):
4731 catfileheadercshex = format(0, 'x').lower();
4732 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
4733 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
4734 elif(checksumtype=="crc16_ccitt"):
4735 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
4736 elif(checksumtype=="adler32"):
4737 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
4738 elif(checksumtype=="crc32"):
4739 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
4740 elif(checksumtype=="crc64_ecma"):
4741 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4742 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
4743 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
4744 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
4745 checksumoutstr = hashlib.new(checksumtype);
4746 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
4747 catfileheadercshex = checksumoutstr.hexdigest().lower();
4748 else:
4749 catfileheadercshex = format(0, 'x').lower();
4750 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
4751 catfileoutstrecd = catfileoutstr.encode('UTF-8');
4752 nullstrecd = formatspecs[5].encode('UTF-8');
4753 fheadtell += len(catfileoutstr) + 1;
4754 catfcontentend = fheadtell - 1;
4755 catfileout = catfileoutstrecd + fcontents + nullstrecd;
4756 pyhascontents = False;
4757 if(int(fsize)>0 and not listonly):
4758 pyhascontents = True;
4759 if(int(fsize)>0 and listonly):
4760 fcontents = "";
4761 pyhascontents = False;
4762 catlist['ffilelist'].update({fileidnum: {'fid': fileidnum, 'fidalt': fileidnum, 'fheadersize': int(catheaersize, 16), 'fhstart': catfhstart, 'fhend': catfhend, 'ftype': ftype, 'fname': fname, 'fbasedir': fbasedir, 'flinkname': flinkname, 'fsize': fsize, 'fatime': fatime, 'fmtime': fmtime, 'fctime': fctime, 'fbtime': fbtime, 'fmode': fmode, 'fchmode': fchmode, 'ftypemod': ftypemod, 'fuid': fuid, 'funame': funame, 'fgid': fgid, 'fgname': fgname, 'finode': finode, 'flinkcount': flinkcount, 'fminor': fdev_minor, 'fmajor': fdev_major, 'frminor': frdev_minor, 'frmajor': frdev_major, 'fchecksumtype': checksumtype, 'fnumfields': catfnumfields, 'fextrafields': catfextrafields, 'fextrafieldsize': extrasizelen, 'fextralist': extrafieldslist, 'fheaderchecksum': int(catfileheadercshex, 16), 'fcontentchecksum': int(catfilecontentcshex, 16), 'fhascontents': pyhascontents, 'fcontentstart': catfcontentstart, 'fcontentend': catfcontentend, 'fcontents': fcontents} });
4763 fileidnum = fileidnum + 1;
4764 return catlist;
4766 def ListDirToArray(infiles, dirlistfromtxt=False, compression="auto", compressionlevel=None, followlink=False, seekstart=0, seekend=0, listonly=False, skipchecksum=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
4767 outarray = BytesIO();
4768 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, extradata, formatspecs, verbose, True);
4769 listcatfiles = ArchiveFileToArray(outarray, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4770 return listcatfiles;
4772 def ArchiveFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
4773 if(isinstance(infile, dict)):
4774 listcatfiles = infile;
4775 else:
4776 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
4777 infile = RemoveWindowsPath(infile);
4778 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
4779 if(not listcatfiles):
4780 return False;
4781 catarray = {'list': listcatfiles, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
4782 if(returnfp):
4783 catarray.update({'catfp': listcatfiles['catfp']});
4784 lenlist = len(listcatfiles['ffilelist']);
4785 if(seekstart>0):
4786 lcfi = seekstart;
4787 else:
4788 lcfi = 0;
4789 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
4790 lcfx = seekend;
4791 else:
4792 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
4793 lcfx = listcatfiles['fnumfiles'];
4794 else:
4795 lcfx = int(listcatfiles['fnumfiles']);
4796 while(lcfi < lcfx):
4797 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
4798 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
4799 catarray['filetoid'].update(filetoidarray);
4800 catarray['idtofile'].update(idtofilearray);
4801 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
4802 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
4803 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
4804 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
4805 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
4806 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
4807 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
4808 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
4809 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
4810 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
4811 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
4812 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
4813 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
4814 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
4815 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
4816 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
4817 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4818 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4819 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
4820 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
4821 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
4822 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4823 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4824 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
4825 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
4826 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
4827 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
4828 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
4829 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
4830 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4831 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4832 lcfi = lcfi + 1;
4833 return catarray;
4835 create_alias_function("", __file_format_name__, "ToArrayIndex", ArchiveFileToArrayIndex);
4837 def ListDirToArrayIndexAlt(infiles, dirlistfromtxt=False, followlink=False, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
4838 listcatfiles = ListDirToArrayAlt(infiles, dirlistfromtxt, followlink, listonly, checksumtype, extradata, formatspecs, verbose);
4839 if(not listcatfiles):
4840 return False;
4841 catarray = {'list': listcatfiles, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
4842 lenlist = len(listcatfiles['ffilelist']);
4843 if(seekstart>0):
4844 lcfi = seekstart;
4845 else:
4846 lcfi = 0;
4847 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
4848 lcfx = seekend;
4849 else:
4850 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
4851 lcfx = listcatfiles['fnumfiles'];
4852 else:
4853 lcfx = int(listcatfiles['fnumfiles']);
4854 while(lcfi < lcfx):
4855 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
4856 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
4857 catarray['filetoid'].update(filetoidarray);
4858 catarray['idtofile'].update(idtofilearray);
4859 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
4860 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
4861 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
4862 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
4863 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
4864 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
4865 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
4866 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
4867 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
4868 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
4869 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
4870 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
4871 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
4872 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
4873 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
4874 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
4875 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4876 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4877 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
4878 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
4879 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
4880 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4881 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4882 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
4883 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
4884 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
4885 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
4886 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
4887 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
4888 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4889 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4890 lcfi = lcfi + 1;
4891 return catarray;
4893 def TarFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
4894 listcatfiles = TarFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
4895 if(not listcatfiles):
4896 return False;
4897 catarray = {'list': listcatfiles, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
4898 lenlist = len(listcatfiles['ffilelist']);
4899 if(seekstart>0):
4900 lcfi = seekstart;
4901 else:
4902 lcfi = 0;
4903 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
4904 lcfx = seekend;
4905 else:
4906 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
4907 lcfx = listcatfiles['fnumfiles'];
4908 else:
4909 lcfx = int(listcatfiles['fnumfiles']);
4910 while(lcfi < lcfx):
4911 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
4912 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
4913 catarray['filetoid'].update(filetoidarray);
4914 catarray['idtofile'].update(idtofilearray);
4915 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
4916 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
4917 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
4918 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
4919 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
4920 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
4921 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
4922 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
4923 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
4924 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
4925 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
4926 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
4927 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
4928 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
4929 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
4930 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
4931 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4932 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4933 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
4934 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
4935 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
4936 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4937 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4938 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
4939 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
4940 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
4941 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
4942 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
4943 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
4944 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4945 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4946 lcfi = lcfi + 1;
4947 return catarray;
4949 def ZipFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
4950 listcatfiles = ZipFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
4951 if(not listcatfiles):
4952 return False;
4953 catarray = {'list': listcatfiles, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
4954 lenlist = len(listcatfiles['ffilelist']);
4955 if(seekstart>0):
4956 lcfi = seekstart;
4957 else:
4958 lcfi = 0;
4959 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
4960 lcfx = seekend;
4961 else:
4962 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
4963 lcfx = listcatfiles['fnumfiles'];
4964 else:
4965 lcfx = int(listcatfiles['fnumfiles']);
4966 while(lcfi < lcfx):
4967 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
4968 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
4969 catarray['filetoid'].update(filetoidarray);
4970 catarray['idtofile'].update(idtofilearray);
4971 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
4972 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
4973 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
4974 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
4975 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
4976 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
4977 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
4978 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
4979 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
4980 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
4981 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
4982 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
4983 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
4984 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
4985 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
4986 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
4987 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4988 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4989 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
4990 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
4991 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
4992 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
4993 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
4994 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
4995 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
4996 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
4997 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
4998 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
4999 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
5000 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
5001 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
5002 lcfi = lcfi + 1;
5003 return catarray;
5005 if(not rarfile_support):
5006 def RarFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
5007 return False;
5009 if(rarfile_support):
5010 def RarFileToArrayIndexAlt(infiles, seekstart=0, seekend=0, listonly=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False):
5011 listcatfiles = RarFileToArrayAlt(infiles, listonly, checksumtype, extradata, formatspecs, verbose);
5012 if(not listcatfiles):
5013 return False;
5014 catarray = {'list': listcatfiles, 'filetoid': {}, 'idtofile': {}, 'filetypes': {'directories': {'filetoid': {}, 'idtofile': {}}, 'files': {'filetoid': {}, 'idtofile': {}}, 'links': {'filetoid': {}, 'idtofile': {}}, 'symlinks': {'filetoid': {}, 'idtofile': {}}, 'hardlinks': {'filetoid': {}, 'idtofile': {}}, 'character': {'filetoid': {}, 'idtofile': {}}, 'block': {'filetoid': {}, 'idtofile': {}}, 'fifo': {'filetoid': {}, 'idtofile': {}}, 'devices': {'filetoid': {}, 'idtofile': {}}}};
5015 lenlist = len(listcatfiles['ffilelist']);
5016 if(seekstart>0):
5017 lcfi = seekstart;
5018 else:
5019 lcfi = 0;
5020 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
5021 lcfx = seekend;
5022 else:
5023 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
5024 lcfx = listcatfiles['fnumfiles'];
5025 else:
5026 lcfx = int(listcatfiles['fnumfiles']);
5027 while(lcfi < lcfx):
5028 filetoidarray = {listcatfiles['ffilelist'][lcfi]['fname']: listcatfiles['ffilelist'][lcfi]['fid']};
5029 idtofilearray = {listcatfiles['ffilelist'][lcfi]['fid']: listcatfiles['ffilelist'][lcfi]['fname']};
5030 catarray['filetoid'].update(filetoidarray);
5031 catarray['idtofile'].update(idtofilearray);
5032 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
5033 catarray['filetypes']['files']['filetoid'].update(filetoidarray);
5034 catarray['filetypes']['files']['idtofile'].update(idtofilearray);
5035 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
5036 catarray['filetypes']['hardlinks']['filetoid'].update(filetoidarray);
5037 catarray['filetypes']['hardlinks']['idtofile'].update(idtofilearray);
5038 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
5039 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
5040 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
5041 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
5042 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
5043 catarray['filetypes']['links']['filetoid'].update(filetoidarray);
5044 catarray['filetypes']['links']['idtofile'].update(idtofilearray);
5045 if(listcatfiles['ffilelist'][lcfi]['ftype']==3):
5046 catarray['filetypes']['character']['filetoid'].update(filetoidarray);
5047 catarray['filetypes']['character']['idtofile'].update(idtofilearray);
5048 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
5049 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
5050 if(listcatfiles['ffilelist'][lcfi]['ftype']==4):
5051 catarray['filetypes']['block']['filetoid'].update(filetoidarray);
5052 catarray['filetypes']['block']['idtofile'].update(idtofilearray);
5053 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
5054 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
5055 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
5056 catarray['filetypes']['directories']['filetoid'].update(filetoidarray);
5057 catarray['filetypes']['directories']['idtofile'].update(idtofilearray);
5058 if(listcatfiles['ffilelist'][lcfi]['ftype']==6):
5059 catarray['filetypes']['symlinks']['filetoid'].update(filetoidarray);
5060 catarray['filetypes']['symlinks']['idtofile'].update(idtofilearray);
5061 catarray['filetypes']['devices']['filetoid'].update(filetoidarray);
5062 catarray['filetypes']['devices']['idtofile'].update(idtofilearray);
5063 lcfi = lcfi + 1;
5064 return catarray;
5066 def ArchiveFileStringToArrayIndex(catstr, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
5067 catfp = BytesIO(catstr);
5068 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
5069 return listcatfiles;
5071 create_alias_function("", __file_format_name__, "StringToArrayIndex", ArchiveFileStringToArrayIndex);
5073 def TarFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
5074 catfp = BytesIO();
5075 catfp = PackArchiveFileFromTarFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
5076 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
5077 return listcatfiles;
5079 def ZipFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
5080 catfp = BytesIO();
5081 catfp = PackArchiveFileFromZipFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
5082 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
5083 return listcatfiles;
5085 if(not rarfile_support):
5086 def RarFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
5087 return False;
5089 if(rarfile_support):
5090 def RarFileToArrayIndex(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=False, formatspecs=__file_format_list__, returnfp=False):
5091 catfp = BytesIO();
5092 catfp = PackArchiveFileFromRarFile(infile, catfp, "auto", None, "crc32", [], formatspecs, False, True);
5093 listcatfiles = ArchiveFileToArrayIndex(catfp, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp);
5094 return listcatfiles;
5096 def ListDirToArrayIndex(infiles, dirlistfromtxt=False, compression="auto", compressionlevel=None, followlink=False, seekstart=0, seekend=0, listonly=False, skipchecksum=False, checksumtype="crc32", formatspecs=__file_format_list__, verbose=False, returnfp=False):
5097 outarray = BytesIO();
5098 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, formatspecs, verbose, True);
5099 listcatfiles = ArchiveFileToArrayIndex(outarray, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp)
5100 return listcatfiles;
5102 def RePackArchiveFile(infile, outfile, compression="auto", compressionlevel=None, followlink=False, seekstart=0, seekend=0, checksumtype="crc32", skipchecksum=False, extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
5103 compressionlist = ['auto', 'gzip', 'bzip2', 'zstd', 'lz4', 'lzo', 'lzop', 'lzma', 'xz'];
5104 outextlist = ['gz', 'bz2', 'zst', 'lz4', 'lzop', 'lzo', 'lzma', 'xz'];
5105 outextlistwd = ['.gz', '.bz2', '.zst', '.lz4', 'lzop', '.lzo', '.lzma', '.xz'];
5106 if(isinstance(infile, dict)):
5107 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5108 listcatfiles = prelistcatfiles['list'];
5109 else:
5110 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
5111 infile = RemoveWindowsPath(infile);
5112 if(followlink):
5113 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5114 listcatfiles = prelistcatfiles['list'];
5115 else:
5116 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5117 if(outfile!="-" and not hasattr(infile, "read") and not hasattr(outfile, "write")):
5118 outfile = RemoveWindowsPath(outfile);
5119 checksumtype = checksumtype.lower();
5120 if(not CheckSumSupport(checksumtype, hashlib_guaranteed)):
5121 checksumtype="crc32";
5122 if(checksumtype=="none"):
5123 checksumtype = "";
5124 if(not compression or compression or compression=="catfile" or compression==formatspecs[2]):
5125 compression = None;
5126 if(compression not in compressionlist and compression is None):
5127 compression = "auto";
5128 if(verbose):
5129 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
5130 if(outfile!="-" and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
5131 if(os.path.exists(outfile)):
5132 os.unlink(outfile);
5133 if(not listcatfiles):
5134 return False;
5135 if(outfile=="-"):
5136 verbose = False;
5137 catfp = BytesIO();
5138 elif(hasattr(outfile, "read") or hasattr(outfile, "write")):
5139 catfp = outfile;
5140 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
5141 catfp = BytesIO();
5142 else:
5143 fbasename = os.path.splitext(outfile)[0];
5144 fextname = os.path.splitext(outfile)[1];
5145 catfp = CompressOpenFile(outfile, compressionlevel);
5146 catver = formatspecs[6];
5147 fileheaderver = str(int(catver.replace(".", "")));
5148 fileheader = AppendNullByte(formatspecs[1] + fileheaderver, formatspecs[5]);
5149 catfp.write(fileheader.encode('UTF-8'));
5150 lenlist = len(listcatfiles['ffilelist']);
5151 fnumfiles = int(listcatfiles['fnumfiles']);
5152 if(lenlist>fnumfiles or lenlist<fnumfiles):
5153 fnumfiles = lenlist;
5154 fnumfileshex = format(int(fnumfiles), 'x').lower();
5155 fnumfilesa = AppendNullBytes([fnumfileshex, checksumtype], formatspecs[5]);
5156 if(checksumtype=="none" or checksumtype==""):
5157 catfileheadercshex = format(0, 'x').lower();
5158 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5159 catfileheadercshex = format(crc16(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
5160 elif(checksumtype=="crc16_ccitt"):
5161 catfileheadercshex = format(crc16_ccitt(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffff, '04x').lower();
5162 elif(checksumtype=="adler32"):
5163 catfileheadercshex = format(zlib.adler32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
5164 elif(checksumtype=="crc32"):
5165 catfileheadercshex = format(crc32(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffff, '08x').lower();
5166 elif(checksumtype=="crc64_ecma"):
5167 catfileheadercshex = format(crc64_ecma(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5168 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5169 catfileheadercshex = format(crc64_iso(str(fileheader + fnumfilesa).encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5170 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5171 checksumoutstr = hashlib.new(checksumtype);
5172 checksumoutstr.update(str(fileheader + fnumfilesa).encode('UTF-8'));
5173 catfileheadercshex = checksumoutstr.hexdigest().lower();
5174 else:
5175 catfileheadercshex = format(0, 'x').lower();
5176 fnumfilesa = fnumfilesa + AppendNullByte(catfileheadercshex, formatspecs[5]);
5177 catfp.write(fnumfilesa.encode('UTF-8'));
5178 try:
5179 catfp.flush();
5180 os.fsync(catfp.fileno());
5181 except io.UnsupportedOperation:
5182 pass;
5183 except AttributeError:
5184 pass;
5185 if(seekstart>0):
5186 lcfi = seekstart;
5187 else:
5188 lcfi = 0;
5189 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
5190 lcfx = seekend;
5191 else:
5192 lcfx = int(listcatfiles['fnumfiles']);
5193 curinode = 0;
5194 curfid = 0;
5195 inodelist = [];
5196 inodetofile = {};
5197 filetoinode = {};
5198 reallcfi = 0;
5199 while(lcfi < lcfx):
5200 catfhstart = catfp.tell();
5201 if(re.findall("^[.|/]", listcatfiles['ffilelist'][reallcfi]['fname'])):
5202 fname = listcatfiles['ffilelist'][reallcfi]['fname'];
5203 else:
5204 fname = "./"+listcatfiles['ffilelist'][reallcfi]['fname'];
5205 if(verbose):
5206 VerbosePrintOut(fname);
5207 fheadersize = format(int(listcatfiles['ffilelist'][reallcfi]['fheadersize']), 'x').lower();
5208 fsize = format(int(listcatfiles['ffilelist'][reallcfi]['fsize']), 'x').lower();
5209 flinkname = listcatfiles['ffilelist'][reallcfi]['flinkname'];
5210 fatime = format(int(listcatfiles['ffilelist'][reallcfi]['fatime']), 'x').lower();
5211 fmtime = format(int(listcatfiles['ffilelist'][reallcfi]['fmtime']), 'x').lower();
5212 fctime = format(int(listcatfiles['ffilelist'][reallcfi]['fctime']), 'x').lower();
5213 fbtime = format(int(listcatfiles['ffilelist'][reallcfi]['fbtime']), 'x').lower();
5214 fmode = format(int(listcatfiles['ffilelist'][reallcfi]['fmode']), 'x').lower();
5215 fchmode = format(int(listcatfiles['ffilelist'][reallcfi]['fchmode']), 'x').lower();
5216 fuid = format(int(listcatfiles['ffilelist'][reallcfi]['fuid']), 'x').lower();
5217 funame = listcatfiles['ffilelist'][reallcfi]['funame'];
5218 fgid = format(int(listcatfiles['ffilelist'][reallcfi]['fgid']), 'x').lower();
5219 fgname = listcatfiles['ffilelist'][reallcfi]['fgname'];
5220 finode = listcatfiles['ffilelist'][reallcfi]['finode'];
5221 flinkcount = listcatfiles['ffilelist'][reallcfi]['flinkcount'];
5222 fdev_minor = format(int(listcatfiles['ffilelist'][reallcfi]['fminor']), 'x').lower();
5223 fdev_major = format(int(listcatfiles['ffilelist'][reallcfi]['fmajor']), 'x').lower();
5224 frdev_minor = format(int(listcatfiles['ffilelist'][reallcfi]['frminor']), 'x').lower();
5225 frdev_major = format(int(listcatfiles['ffilelist'][reallcfi]['frmajor']), 'x').lower();
5226 if(len(listcatfiles['ffilelist'][reallcfi]['fextralist'])>listcatfiles['ffilelist'][reallcfi]['fextrafields'] and len(listcatfiles['ffilelist'][reallcfi]['fextralist'])>0):
5227 listcatfiles['ffilelist'][reallcfi]['fextrafields'] = len(listcatfiles['ffilelist'][reallcfi]['fextralist']);
5228 if(len(extradata) > 0):
5229 listcatfiles['ffilelist'][reallcfi]['fextrafields'] = len(extradata);
5230 listcatfiles['ffilelist'][reallcfi]['fextralist'] = extradata;
5231 extrafields = format(int(listcatfiles['ffilelist'][reallcfi]['fextrafields']), 'x').lower();
5232 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
5233 if(len(extradata)>0):
5234 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
5235 extrasizelen = format(len(extrasizestr), 'x').lower();
5236 fcontents = listcatfiles['ffilelist'][reallcfi]['fcontents'];
5237 if(followlink):
5238 if(listcatfiles['ffilelist'][reallcfi]['ftype']==1 or listcatfiles['ffilelist'][reallcfi]['ftype']==2):
5239 getflinkpath = listcatfiles['ffilelist'][reallcfi]['flinkname'];
5240 flinkid = prelistcatfiles['filetoid'][getflinkpath];
5241 flinkinfo = listcatfiles['ffilelist'][flinkid];
5242 fheadersize = format(int(flinkinfo['fheadersize']), 'x').lower();
5243 fsize = format(int(flinkinfo['fsize']), 'x').lower();
5244 flinkname = flinkinfo['flinkname'];
5245 fatime = format(int(flinkinfo['fatime']), 'x').lower();
5246 fmtime = format(int(flinkinfo['fmtime']), 'x').lower();
5247 fctime = format(int(flinkinfo['fctime']), 'x').lower();
5248 fbtime = format(int(flinkinfo['fbtime']), 'x').lower();
5249 fmode = format(int(flinkinfo['fmode']), 'x').lower();
5250 fchmode = format(int(flinkinfo['fchmode']), 'x').lower();
5251 fuid = format(int(flinkinfo['fuid']), 'x').lower();
5252 funame = flinkinfo['funame'];
5253 fgid = format(int(flinkinfo['fgid']), 'x').lower();
5254 fgname = flinkinfo['fgname'];
5255 finode = flinkinfo['finode'];
5256 flinkcount = flinkinfo['flinkcount'];
5257 fdev_minor = format(int(flinkinfo['fminor']), 'x').lower();
5258 fdev_major = format(int(flinkinfo['fmajor']), 'x').lower();
5259 frdev_minor = format(int(flinkinfo['frminor']), 'x').lower();
5260 frdev_major = format(int(flinkinfo['frmajor']), 'x').lower();
5261 if(len(flinkinfo['fextralist'])>flinkinfo['fextrafields'] and len(flinkinfo['fextralist'])>0):
5262 flinkinfo['fextrafields'] = len(flinkinfo['fextralist']);
5263 if(len(extradata) > 0):
5264 flinkinfo['fextrafields'] = len(extradata);
5265 flinkinfo['fextralist'] = extradata;
5266 extrafields = format(int(flinkinfo['fextrafields']), 'x').lower();
5267 extrasizestr = AppendNullByte(extrafields, formatspecs[5]);
5268 if(len(extradata)>0):
5269 extrasizestr = extrasizestr + AppendNullBytes(extradata, formatspecs[5]);
5270 extrasizelen = format(len(extrasizestr), 'x').lower();
5271 fcontents = flinkinfo['fcontents'];
5272 if(flinkinfo['ftype']!=0 and flinkinfo['ftype']!=7):
5273 try:
5274 fcontents = fcontents.encode('UTF-8');
5275 except AttributeError:
5276 pass;
5277 ftypehex = format(flinkinfo['ftype'], 'x').lower();
5278 else:
5279 if(listcatfiles['ffilelist'][reallcfi]['ftype']!=0 and listcatfiles['ffilelist'][reallcfi]['ftype']!=7):
5280 try:
5281 fcontents = fcontents.encode('UTF-8');
5282 except AttributeError:
5283 pass;
5284 ftypehex = format(listcatfiles['ffilelist'][reallcfi]['ftype'], 'x').lower();
5285 fcurfid = format(curfid, 'x').lower();
5286 if(not followlink and finode!=0):
5287 if(listcatfiles['ffilelist'][reallcfi]['ftype']!=1):
5288 fcurinode = format(int(curinode), 'x').lower();
5289 inodetofile.update({curinode: fname});
5290 filetoinode.update({fname: curinode});
5291 curinode = curinode + 1;
5292 else:
5293 fcurinode = format(int(filetoinode[flinkname]), 'x').lower();
5294 else:
5295 fcurinode = format(int(curinode), 'x').lower();
5296 curinode = curinode + 1;
5297 curfid = curfid + 1;
5298 catfileoutstr = AppendNullBytes([ftypehex, fname, flinkname, fsize, fatime, fmtime, fctime, fbtime, fmode, fuid, funame, fgid, fgname, fcurfid, fcurinode, flinkcount, fdev_minor, fdev_major, frdev_minor, frdev_major, extrasizelen, extrafields], formatspecs[5]);
5299 if(listcatfiles['ffilelist'][reallcfi]['fextrafields']>0):
5300 extrafieldslist = [];
5301 exi = 0;
5302 exil = listcatfiles['ffilelist'][reallcfi]['fextrafields'];
5303 while(exi < exil):
5304 extrafieldslist.append(listcatfiles['ffilelist'][reallcfi]['fextralist']);
5305 exi = exi + 1;
5306 catfileoutstr += AppendNullBytes([extrafieldslist], formatspecs[5]);
5307 catfileoutstr += AppendNullBytes([checksumtype], formatspecs[5]);
5308 catfhend = (catfp.tell() - 1) + len(catfileoutstr);
5309 catfcontentstart = catfp.tell() + len(catfileoutstr);
5310 if(checksumtype=="none" or checksumtype==""):
5311 catfileheadercshex = format(0, 'x').lower();
5312 catfilecontentcshex = format(0, 'x').lower();
5313 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5314 catfileheadercshex = format(crc16("".encode('UTF-8')) & 0xffff, '04x').lower();
5315 catfilecontentcshex = format(crc16(fcontents) & 0xffff, '04x').lower();
5316 elif(checksumtype=="crc16_ccitt"):
5317 catfileheadercshex = format(crc16_ccitt("".encode('UTF-8')) & 0xffff, '04x').lower();
5318 catfilecontentcshex = format(crc16_ccitt(fcontents) & 0xffff, '04x').lower();
5319 elif(checksumtype=="adler32"):
5320 catfileheadercshex = format(zlib.adler32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5321 catfilecontentcshex = format(zlib.adler32(fcontents) & 0xffffffff, '08x').lower();
5322 elif(checksumtype=="crc32"):
5323 catfileheadercshex = format(crc32("".encode('UTF-8')) & 0xffffffff, '08x').lower();
5324 catfilecontentcshex = format(crc32(fcontents) & 0xffffffff, '08x').lower();
5325 elif(checksumtype=="crc64_ecma"):
5326 catfileheadercshex = format(crc64_ecma("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5327 catfilecontentcshex = format(crc64_ecma(fcontents) & 0xffffffffffffffff, '016x').lower();
5328 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5329 catfileheadercshex = format(crc64_iso("".encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5330 catfilecontentcshex = format(crc64_iso(fcontents) & 0xffffffffffffffff, '016x').lower();
5331 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5332 checksumoutstr = hashlib.new(checksumtype);
5333 checksumoutstr.update("".encode('UTF-8'));
5334 catfileheadercshex = checksumoutstr.hexdigest().lower();
5335 checksumoutstr = hashlib.new(checksumtype);
5336 checksumoutstr.update(fcontents);
5337 catfilecontentcshex = checksumoutstr.hexdigest().lower();
5338 else:
5339 catfileheadercshex = format(0, 'x').lower();
5340 catfilecontentcshex = format(0, 'x').lower();
5341 tmpfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5342 catheaersize = format(int(len(tmpfileoutstr) - 1), 'x').lower()
5343 catfileoutstr = AppendNullByte(catheaersize, formatspecs[5]) + catfileoutstr;
5344 if(checksumtype=="none" or checksumtype==""):
5345 catfileheadercshex = format(0, 'x').lower();
5346 elif(checksumtype=="crc16" or checksumtype=="crc16_ansi" or checksumtype=="crc16_ibm"):
5347 catfileheadercshex = format(crc16(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5348 elif(checksumtype=="crc16_ccitt"):
5349 catfileheadercshex = format(crc16_ccitt(catfileoutstr.encode('UTF-8')) & 0xffff, '04x').lower();
5350 elif(checksumtype=="adler32"):
5351 catfileheadercshex = format(zlib.adler32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5352 elif(checksumtype=="crc32"):
5353 catfileheadercshex = format(crc32(catfileoutstr.encode('UTF-8')) & 0xffffffff, '08x').lower();
5354 elif(checksumtype=="crc64_ecma"):
5355 catfileheadercshex = format(crc64_ecma(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5356 elif(checksumtype=="crc64" or checksumtype=="crc64_iso"):
5357 catfileheadercshex = format(crc64_iso(catfileoutstr.encode('UTF-8')) & 0xffffffffffffffff, '016x').lower();
5358 elif(CheckSumSupportAlt(checksumtype, hashlib_guaranteed)):
5359 checksumoutstr = hashlib.new(checksumtype);
5360 checksumoutstr.update(catfileoutstr.encode('UTF-8'));
5361 catfileheadercshex = checksumoutstr.hexdigest().lower();
5362 else:
5363 catfileheadercshex = format(0, 'x').lower();
5364 catfileoutstr = catfileoutstr + AppendNullBytes([catfileheadercshex, catfilecontentcshex], formatspecs[5]);
5365 catfileoutstrecd = catfileoutstr.encode('UTF-8');
5366 nullstrecd = formatspecs[5].encode('UTF-8');
5367 catfileout = catfileoutstrecd + fcontents + nullstrecd;
5368 catfcontentend = (catfp.tell() - 1) + len(catfileout);
5369 catfp.write(catfileout);
5370 try:
5371 catfp.flush();
5372 os.fsync(catfp.fileno());
5373 except io.UnsupportedOperation:
5374 pass;
5375 except AttributeError:
5376 pass;
5377 lcfi = lcfi + 1;
5378 reallcfi = reallcfi + 1;
5379 if(outfile=="-" or hasattr(outfile, "read") or hasattr(outfile, "write")):
5380 catfp = CompressArchiveFile(catfp, compression, formatspecs);
5381 try:
5382 catfp.flush();
5383 os.fsync(catfp.fileno());
5384 except io.UnsupportedOperation:
5385 pass;
5386 except AttributeError:
5387 pass;
5388 if(outfile=="-"):
5389 catfp.seek(0, 0);
5390 if(hasattr(sys.stdout, "buffer")):
5391 shutil.copyfileobj(catfp, sys.stdout.buffer);
5392 else:
5393 shutil.copyfileobj(catfp, sys.stdout);
5394 elif(re.findall(r"^(ftp|ftps|sftp)\:\/\/", outfile)):
5395 catfp = CompressArchiveFile(catfp, compression, formatspecs);
5396 catfp.seek(0, 0);
5397 upload_file_to_internet_file(catfp, outfile);
5398 if(returnfp):
5399 catfp.seek(0, 0);
5400 return catfp;
5401 else:
5402 catfp.close();
5403 return True;
5405 create_alias_function("RePack", __file_format_name__, "", RePackArchiveFile);
5407 def RePackArchiveFileFromString(catstr, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", skipchecksum=False, extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
5408 catfp = BytesIO(catstr);
5409 listcatfiles = RePackArchiveFile(catfp, compression, compressionlevel, checksumtype, skipchecksum, extradata, formatspecs, verbose, returnfp);
5410 return listcatfiles;
5412 create_alias_function("RePack", __file_format_name__, "FromString", RePackArchiveFileFromString);
5414 def PackArchiveFileFromListDir(infiles, outfile, dirlistfromtxt=False, compression="auto", compressionlevel=None, followlink=False, skipchecksum=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
5415 outarray = BytesIO();
5416 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, extradata, formatspecs, verbose, True);
5417 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, checksumtype, skipchecksum, extradata, formatspecs, verbose, returnfp);
5418 return listcatfiles;
5420 create_alias_function("Pack", __file_format_name__, "FromListDir", PackArchiveFileFromListDir);
5422 def ArchiveFileArrayBase64Encode(infile, followlink=False, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
5423 if(verbose):
5424 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
5425 if(isinstance(infile, dict)):
5426 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5427 listcatfiles = prelistcatfiles['list'];
5428 else:
5429 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
5430 infile = RemoveWindowsPath(infile);
5431 if(followlink):
5432 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5433 listcatfiles = prelistcatfiles['list'];
5434 else:
5435 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5436 if(not listcatfiles):
5437 return False;
5438 lenlist = len(listcatfiles['ffilelist']);
5439 if(seekstart>0):
5440 lcfi = seekstart;
5441 else:
5442 lcfi = 0;
5443 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
5444 lcfx = seekend;
5445 else:
5446 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
5447 lcfx = listcatfiles['fnumfiles'];
5448 else:
5449 lcfx = int(listcatfiles['fnumfiles']);
5450 if(lenlist>lcfx or lenlist<lcfx):
5451 lcfx = lenlist;
5452 while(lcfi < lcfx):
5453 if(listcatfiles['ffilelist'][lcfi]['fhascontents']):
5454 listcatfiles['ffilelist'][lcfi]['fcontents'] = base64.b64encode(listcatfiles['ffilelist'][lcfi]['fcontents']).decode("UTF-8");
5455 lcfi = lcfi + 1;
5456 return listcatfiles;
5458 create_alias_function("", __file_format_name__, "ArrayBase64Encode", ArchiveFileArrayBase64Encode);
5460 def ArchiveFileArrayBase64Decode(infile, followlink=False, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
5461 if(verbose):
5462 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
5463 if(isinstance(infile, dict)):
5464 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5465 listcatfiles = prelistcatfiles['list'];
5466 else:
5467 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
5468 infile = RemoveWindowsPath(infile);
5469 if(followlink):
5470 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5471 listcatfiles = prelistcatfiles['list'];
5472 else:
5473 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5474 if(not listcatfiles):
5475 return False;
5476 lenlist = len(listcatfiles['ffilelist']);
5477 if(seekstart>0):
5478 lcfi = seekstart;
5479 else:
5480 lcfi = 0;
5481 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
5482 lcfx = seekend;
5483 else:
5484 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
5485 lcfx = listcatfiles['fnumfiles'];
5486 else:
5487 lcfx = int(listcatfiles['fnumfiles']);
5488 if(lenlist>lcfx or lenlist<lcfx):
5489 lcfx = lenlist;
5490 while(lcfi < lcfx):
5491 if(listcatfiles['ffilelist'][lcfi]['fhascontents']):
5492 listcatfiles['ffilelist'][lcfi]['fcontents'] = base64.b64decode(listcatfiles['ffilelist'][lcfi]['fcontents'].encode("UTF-8"));
5493 lcfi = lcfi + 1;
5494 return listcatfiles;
5496 create_alias_function("", __file_format_name__, "ArrayBase64Decode", ArchiveFileArrayBase64Decode);
5498 def UnPackArchiveFile(infile, outdir=None, followlink=False, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, preservepermissions=True, preservetime=True, verbose=False, returnfp=False):
5499 if(outdir is not None):
5500 outdir = RemoveWindowsPath(outdir);
5501 if(verbose):
5502 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
5503 if(isinstance(infile, dict)):
5504 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5505 listcatfiles = prelistcatfiles['list'];
5506 else:
5507 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
5508 infile = RemoveWindowsPath(infile);
5509 if(followlink):
5510 prelistcatfiles = ArchiveFileToArrayIndex(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5511 listcatfiles = prelistcatfiles['list'];
5512 else:
5513 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, False, skipchecksum, formatspecs, returnfp);
5514 if(not listcatfiles):
5515 return False;
5516 lenlist = len(listcatfiles['ffilelist']);
5517 if(seekstart>0):
5518 lcfi = seekstart;
5519 else:
5520 lcfi = 0;
5521 if(seekend>0 and seekend<listcatfiles['fnumfiles']):
5522 lcfx = seekend;
5523 else:
5524 if(lenlist>listcatfiles['fnumfiles'] or lenlist<listcatfiles['fnumfiles']):
5525 lcfx = listcatfiles['fnumfiles'];
5526 else:
5527 lcfx = int(listcatfiles['fnumfiles']);
5528 if(lenlist>lcfx or lenlist<lcfx):
5529 lcfx = lenlist;
5530 while(lcfi < lcfx):
5531 funame = "";
5532 try:
5533 import pwd;
5534 try:
5535 userinfo = pwd.getpwuid(listcatfiles['ffilelist'][lcfi]['fuid']);
5536 funame = userinfo.pw_name;
5537 except KeyError:
5538 funame = "";
5539 except ImportError:
5540 funame = "";
5541 fgname = "";
5542 try:
5543 import grp;
5544 try:
5545 groupinfo = grp.getgrgid(listcatfiles['ffilelist'][lcfi]['fgid']);
5546 fgname = groupinfo.gr_name;
5547 except KeyError:
5548 fgname = "";
5549 except ImportError:
5550 fgname = "";
5551 if(verbose):
5552 VerbosePrintOut(listcatfiles['ffilelist'][lcfi]['fname']);
5553 if(listcatfiles['ffilelist'][lcfi]['ftype']==0 or listcatfiles['ffilelist'][lcfi]['ftype']==7):
5554 with open(listcatfiles['ffilelist'][lcfi]['fname'], "wb") as fpc:
5555 fpc.write(listcatfiles['ffilelist'][lcfi]['fcontents'])
5556 try:
5557 fpc.flush()
5558 os.fsync(fpc.fileno())
5559 except io.UnsupportedOperation:
5560 pass
5561 except AttributeError:
5562 pass
5563 if(hasattr(os, "chown") and funame==listcatfiles['ffilelist'][lcfi]['funame'] and fgname==listcatfiles['ffilelist'][lcfi]['fgname'] and preservepermissions):
5564 os.chown(listcatfiles['ffilelist'][lcfi]['fname'], listcatfiles['ffilelist'][lcfi]['fuid'], listcatfiles['ffilelist'][lcfi]['fgid']);
5565 if(preservepermissions):
5566 os.chmod(listcatfiles['ffilelist'][lcfi]['fname'], listcatfiles['ffilelist'][lcfi]['fchmode']);
5567 if(preservetime):
5568 os.utime(listcatfiles['ffilelist'][lcfi]['fname'], (listcatfiles['ffilelist'][lcfi]['fatime'], listcatfiles['ffilelist'][lcfi]['fmtime']));
5569 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
5570 if(followlink):
5571 getflinkpath = listcatfiles['ffilelist'][lcfi]['flinkname'];
5572 flinkid = prelistcatfiles['filetoid'][getflinkpath];
5573 flinkinfo = listcatfiles['ffilelist'][flinkid];
5574 funame = "";
5575 try:
5576 import pwd;
5577 try:
5578 userinfo = pwd.getpwuid(flinkinfo['fuid']);
5579 funame = userinfo.pw_name;
5580 except KeyError:
5581 funame = "";
5582 except ImportError:
5583 funame = "";
5584 fgname = "";
5585 try:
5586 import grp;
5587 try:
5588 groupinfo = grp.getgrgid(flinkinfo['fgid']);
5589 fgname = groupinfo.gr_name;
5590 except KeyError:
5591 fgname = "";
5592 except ImportError:
5593 fgname = "";
5594 if(flinkinfo['ftype'] == 0 or flinkinfo['ftype'] == 7):
5595 with open(listcatfiles['ffilelist'][lcfi]['fname'], "wb") as fpc:
5596 fpc.write(flinkinfo['fcontents'])
5597 try:
5598 fpc.flush()
5599 os.fsync(fpc.fileno())
5600 except io.UnsupportedOperation:
5601 pass
5602 except AttributeError:
5603 pass
5604 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname'] and preservepermissions):
5605 os.chown(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fuid'], flinkinfo['fgid']);
5606 if(preservepermissions):
5607 os.chmod(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fchmode']);
5608 if(preservetime):
5609 os.utime(listcatfiles['ffilelist'][lcfi]['fname'], (flinkinfo['fatime'], flinkinfo['fmtime']));
5610 if(flinkinfo['ftype']==1):
5611 os.link(flinkinfo['flinkname'], listcatfiles['ffilelist'][lcfi]['fname']);
5612 if(flinkinfo['ftype']==2):
5613 os.symlink(flinkinfo['flinkname'], listcatfiles['ffilelist'][lcfi]['fname']);
5614 if(flinkinfo['ftype']==5):
5615 if(preservepermissions):
5616 os.mkdir(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fchmode']);
5617 else:
5618 os.mkdir(listcatfiles['ffilelist'][lcfi]['fname']);
5619 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname'] and preservepermissions):
5620 os.chown(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fuid'], flinkinfo['fgid']);
5621 if(preservepermissions):
5622 os.chmod(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fchmode']);
5623 if(preservetime):
5624 os.utime(listcatfiles['ffilelist'][lcfi]['fname'], (flinkinfo['fatime'], flinkinfo['fmtime']));
5625 if(flinkinfo['ftype']==6 and hasattr(os, "mkfifo")):
5626 os.mkfifo(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fchmode']);
5627 else:
5628 os.link(listcatfiles['ffilelist'][lcfi]['flinkname'], listcatfiles['ffilelist'][lcfi]['fname']);
5629 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
5630 if(followlink):
5631 getflinkpath = listcatfiles['ffilelist'][lcfi]['flinkname'];
5632 flinkid = prelistcatfiles['filetoid'][getflinkpath];
5633 flinkinfo = listcatfiles['ffilelist'][flinkid];
5634 funame = "";
5635 try:
5636 import pwd;
5637 try:
5638 userinfo = pwd.getpwuid(flinkinfo['fuid']);
5639 funame = userinfo.pw_name;
5640 except KeyError:
5641 funame = "";
5642 except ImportError:
5643 funame = "";
5644 fgname = "";
5645 try:
5646 import grp;
5647 try:
5648 groupinfo = grp.getgrgid(flinkinfo['fgid']);
5649 fgname = groupinfo.gr_name;
5650 except KeyError:
5651 fgname = "";
5652 except ImportError:
5653 fgname = "";
5654 if(flinkinfo['ftype']==0 or flinkinfo['ftype']==7):
5655 with open(listcatfiles['ffilelist'][lcfi]['fname'], "wb") as fpc:
5656 fpc.write(flinkinfo['fcontents'])
5657 try:
5658 fpc.flush()
5659 os.fsync(fpc.fileno())
5660 except io.UnsupportedOperation:
5661 pass
5662 except AttributeError:
5663 pass
5664 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname'] and preservepermissions):
5665 os.chown(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fuid'], flinkinfo['fgid']);
5666 if(preservepermissions):
5667 os.chmod(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fchmode']);
5668 if(preservetime):
5669 os.utime(listcatfiles['ffilelist'][lcfi]['fname'], (flinkinfo['fatime'], flinkinfo['fmtime']));
5670 if(flinkinfo['ftype']==1):
5671 os.link(flinkinfo['flinkname'], listcatfiles['ffilelist'][lcfi]['fname']);
5672 if(flinkinfo['ftype']==2):
5673 os.symlink(flinkinfo['flinkname'], listcatfiles['ffilelist'][lcfi]['fname']);
5674 if(flinkinfo['ftype']==5):
5675 if(preservepermissions):
5676 os.mkdir(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fchmode']);
5677 else:
5678 os.mkdir(listcatfiles['ffilelist'][lcfi]['fname']);
5679 if(hasattr(os, "chown") and funame==flinkinfo['funame'] and fgname==flinkinfo['fgname'] and preservepermissions):
5680 os.chown(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fuid'], flinkinfo['fgid']);
5681 if(preservepermissions):
5682 os.chmod(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fchmode']);
5683 if(preservetime):
5684 os.utime(listcatfiles['ffilelist'][lcfi]['fname'], (flinkinfo['fatime'], flinkinfo['fmtime']));
5685 if(flinkinfo['ftype']==6 and hasattr(os, "mkfifo")):
5686 os.mkfifo(listcatfiles['ffilelist'][lcfi]['fname'], flinkinfo['fchmode']);
5687 else:
5688 os.symlink(listcatfiles['ffilelist'][lcfi]['flinkname'], listcatfiles['ffilelist'][lcfi]['fname']);
5689 if(listcatfiles['ffilelist'][lcfi]['ftype']==5):
5690 if(preservepermissions):
5691 os.mkdir(listcatfiles['ffilelist'][lcfi]['fname'], listcatfiles['ffilelist'][lcfi]['fchmode']);
5692 else:
5693 os.mkdir(listcatfiles['ffilelist'][lcfi]['fname']);
5694 if(hasattr(os, "chown") and funame==listcatfiles['ffilelist'][lcfi]['funame'] and fgname==listcatfiles['ffilelist'][lcfi]['fgname'] and preservepermissions):
5695 os.chown(listcatfiles['ffilelist'][lcfi]['fname'], listcatfiles['ffilelist'][lcfi]['fuid'], listcatfiles['ffilelist'][lcfi]['fgid']);
5696 if(preservepermissions):
5697 os.chmod(listcatfiles['ffilelist'][lcfi]['fname'], listcatfiles['ffilelist'][lcfi]['fchmode']);
5698 if(preservetime):
5699 os.utime(listcatfiles['ffilelist'][lcfi]['fname'], (listcatfiles['ffilelist'][lcfi]['fatime'], listcatfiles['ffilelist'][lcfi]['fmtime']));
5700 if(listcatfiles['ffilelist'][lcfi]['ftype']==6 and hasattr(os, "mkfifo")):
5701 os.mkfifo(listcatfiles['ffilelist'][lcfi]['fname'], listcatfiles['ffilelist'][lcfi]['fchmode']);
5702 lcfi = lcfi + 1;
5703 if(returnfp):
5704 return listcatfiles['ffilelist']['catfp'];
5705 else:
5706 return True;
5708 create_alias_function("UnPack", __file_format_name__, "", UnPackArchiveFile);
5710 if(hasattr(shutil, "register_unpack_format")):
5711 def UnPackArchiveFileFunc(archive_name, extract_dir=None, **kwargs):
5712 return UnPackArchiveFile(archive_name, extract_dir, False, 0, 0, False, __file_format_delimiter__, False, False);
5713 create_alias_function("UnPack", __file_format_name__, "Func", UnPackArchiveFileFunc);
5715 def UnPackArchiveFileString(catstr, outdir=None, followlink=False, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
5716 catfp = BytesIO(catstr);
5717 listcatfiles = UnPackArchiveFile(catfp, outdir, followlink, seekstart, seekend, skipchecksum, formatspecs, verbose, returnfp);
5718 return listcatfiles;
5720 create_alias_function("UnPack", __file_format_name__, "String", UnPackArchiveFileString);
5722 def ArchiveFileListFiles(infile, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
5723 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
5724 if(isinstance(infile, dict)):
5725 listcatfiles = infile;
5726 else:
5727 if(infile!="-" and not hasattr(infile, "read") and not hasattr(infile, "write")):
5728 infile = RemoveWindowsPath(infile);
5729 listcatfiles = ArchiveFileToArray(infile, seekstart, seekend, True, skipchecksum, formatspecs, returnfp);
5730 if(not listcatfiles):
5731 return False;
5732 lenlist = len(listcatfiles['ffilelist']);
5733 lcfi = 0;
5734 lcfx = lenlist;
5735 returnval = {};
5736 while(lcfi < lcfx):
5737 returnval.update({lcfi: listcatfiles['ffilelist'][lcfi]['fname']});
5738 if(not verbose):
5739 VerbosePrintOut(listcatfiles['ffilelist'][lcfi]['fname']);
5740 if(verbose):
5741 permissions = { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
5742 printfname = listcatfiles['ffilelist'][lcfi]['fname'];
5743 if(listcatfiles['ffilelist'][lcfi]['ftype']==1):
5744 printfname = listcatfiles['ffilelist'][lcfi]['fname'] + " link to " + listcatfiles['ffilelist'][lcfi]['flinkname'];
5745 if(listcatfiles['ffilelist'][lcfi]['ftype']==2):
5746 printfname = listcatfiles['ffilelist'][lcfi]['fname'] + " -> " + listcatfiles['ffilelist'][lcfi]['flinkname'];
5747 fuprint = listcatfiles['ffilelist'][lcfi]['funame'];
5748 if(len(fuprint)<=0):
5749 fuprint = listcatfiles['ffilelist'][lcfi]['fuid'];
5750 fgprint = listcatfiles['ffilelist'][lcfi]['fgname'];
5751 if(len(fgprint)<=0):
5752 fgprint = listcatfiles['ffilelist'][lcfi]['fgid'];
5753 VerbosePrintOut(PrintPermissionString(listcatfiles['ffilelist'][lcfi]['fmode'], listcatfiles['ffilelist'][lcfi]['ftype']) + " " + str(str(fuprint) + "/" + str(fgprint) + " " + str(listcatfiles['ffilelist'][lcfi]['fsize']).rjust(15) + " " + datetime.datetime.utcfromtimestamp(listcatfiles['ffilelist'][lcfi]['fmtime']).strftime('%Y-%m-%d %H:%M') + " " + printfname));
5754 lcfi = lcfi + 1;
5755 if(returnfp):
5756 return listcatfiles['catfp'];
5757 else:
5758 return True;
5760 create_alias_function("", __file_format_name__, "ListFiles", ArchiveFileListFiles);
5762 def ArchiveFileStringListFiles(catstr, followlink=False, skipchecksum=False, formatspecs=__file_format_list__, verbose=False, returnfp=False):
5763 catfp = BytesIO(catstr);
5764 listcatfiles = UnPackArchiveFile(catfp, None, followlink, skipchecksum, formatspecs, verbose, returnfp);
5765 return listcatfiles;
5767 create_alias_function("", __file_format_name__, "StringListFiles", ArchiveFileListFiles);
5769 def TarFileListFiles(infile, verbose=False, returnfp=False):
5770 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
5771 if(infile=="-"):
5772 infile = BytesIO();
5773 if(hasattr(sys.stdin, "buffer")):
5774 shutil.copyfileobj(sys.stdin.buffer, infile);
5775 else:
5776 shutil.copyfileobj(sys.stdin, infile);
5777 infile.seek(0, 0);
5778 if(not infile):
5779 return False;
5780 infile.seek(0, 0);
5781 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
5782 infile = download_file_from_internet_file(infile);
5783 infile.seek(0, 0);
5784 if(not infile):
5785 return False;
5786 infile.seek(0, 0);
5787 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
5788 return False;
5789 elif(os.path.exists(infile) and os.path.isfile(infile)):
5790 try:
5791 if(not tarfile.is_tarfile(infile)):
5792 return False;
5793 except AttributeError:
5794 if(not is_tarfile(infile)):
5795 return False;
5796 else:
5797 return False;
5798 try:
5799 if(hasattr(infile, "read") or hasattr(infile, "write")):
5800 tarfp = tarfile.open(fileobj=infile, mode="r");
5801 else:
5802 tarfp = tarfile.open(infile, "r");
5803 except FileNotFoundError:
5804 return False;
5805 for member in sorted(tarfp.getmembers(), key=lambda x: x.name):
5806 returnval.update({lcfi: member.name});
5807 fpremode = member.mode;
5808 ffullmode = member.mode;
5809 flinkcount = 0;
5810 ftype = 0;
5811 if(member.isreg()):
5812 ffullmode = member.mode + stat.S_IFREG;
5813 ftype = 0;
5814 elif(member.isdev()):
5815 ffullmode = member.mode;
5816 ftype = 7;
5817 elif(member.islnk()):
5818 ffullmode = member.mode + stat.S_IFREG;
5819 ftype = 1;
5820 elif(member.issym()):
5821 ffullmode = member.mode + stat.S_IFLNK;
5822 ftype = 2;
5823 elif(member.ischr()):
5824 ffullmode = member.mode + stat.S_IFCHR;
5825 ftype = 3;
5826 elif(member.isblk()):
5827 ffullmode = member.mode + stat.S_IFBLK;
5828 ftype = 4;
5829 elif(member.isdir()):
5830 ffullmode = member.mode + stat.S_IFDIR;
5831 ftype = 5;
5832 elif(member.isfifo()):
5833 ffullmode = member.mode + stat.S_IFIFO;
5834 ftype = 6;
5835 elif(member.issparse()):
5836 ffullmode = member.mode;
5837 ftype = 11;
5838 if(not verbose):
5839 VerbosePrintOut(member.name);
5840 elif(verbose):
5841 permissions = { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
5842 printfname = member.name;
5843 if(member.islnk()):
5844 printfname = member.name + " link to " + member.linkname;
5845 elif(member.issym()):
5846 printfname = member.name + " -> " + member.linkname;
5847 fuprint = member.uname;
5848 if(len(fuprint)<=0):
5849 fuprint = member.uid;
5850 fgprint = member.gname;
5851 if(len(fgprint)<=0):
5852 fgprint = member.gid;
5853 VerbosePrintOut(PrintPermissionString(ffullmode, ftype) + " " + str(str(fuprint) + "/" + str(fgprint) + " " + str(member.size).rjust(15) + " " + datetime.datetime.utcfromtimestamp(member.mtime).strftime('%Y-%m-%d %H:%M') + " " + printfname));
5854 lcfi = lcfi + 1;
5855 if(returnfp):
5856 return listcatfiles['catfp'];
5857 else:
5858 return True;
5860 def ZipFileListFiles(infile, verbose=False, returnfp=False):
5861 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
5862 if(infile=="-"):
5863 infile = BytesIO();
5864 if(hasattr(sys.stdin, "buffer")):
5865 shutil.copyfileobj(sys.stdin.buffer, infile);
5866 else:
5867 shutil.copyfileobj(sys.stdin, infile);
5868 infile.seek(0, 0);
5869 if(not infile):
5870 return False;
5871 infile.seek(0, 0);
5872 elif(re.findall(r"^(http|https|ftp|ftps|sftp)\:\/\/", infile)):
5873 infile = download_file_from_internet_file(infile);
5874 infile.seek(0, 0);
5875 if(not infile):
5876 return False;
5877 infile.seek(0, 0);
5878 elif(not os.path.exists(infile) or not os.path.isfile(infile)):
5879 return False;
5880 else:
5881 return False;
5882 if(not zipfile.is_zipfile(infile)):
5883 return False;
5884 try:
5885 zipfp = zipfile.ZipFile(infile, "r", allowZip64=True);
5886 except FileNotFoundError:
5887 return False;
5888 lcfi = 0;
5889 returnval = {};
5890 ziptest = zipfp.testzip();
5891 if(ziptest):
5892 VerbosePrintOut("Bad file found!");
5893 for member in sorted(zipfp.infolist(), key=lambda x: x.filename):
5894 if(not member.is_dir()):
5895 fpremode = int(stat.S_IFREG + 438);
5896 elif(member.is_dir()):
5897 fpremode = int(stat.S_IFDIR + 511);
5898 if(not member.is_dir()):
5899 fmode = int(stat.S_IFREG + 438);
5900 fchmode = int(stat.S_IMODE(int(stat.S_IFREG + 438)));
5901 ftypemod = int(stat.S_IFMT(int(stat.S_IFREG + 438)));
5902 elif(member.is_dir()):
5903 fmode = int(stat.S_IFDIR + 511);
5904 fchmode = int(stat.S_IMODE(int(stat.S_IFDIR + 511)));
5905 ftypemod = int(stat.S_IFMT(int(stat.S_IFDIR + 511)));
5906 returnval.update({lcfi: member.filename});
5907 if(not verbose):
5908 VerbosePrintOut(member.filename);
5909 if(verbose):
5910 permissions = { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
5911 permissionstr = "";
5912 for fmodval in str(oct(fmode))[-3:]:
5913 permissionstr = permissionstr + permissions['access'].get(fmodval, '---');
5914 if(not member.is_dir()):
5915 ftype = 0;
5916 permissionstr = "-" + permissionstr;
5917 elif(member.is_dir()):
5918 ftype = 5;
5919 permissionstr = "d" + permissionstr;
5920 printfname = member.filename;
5921 try:
5922 fuid = int(os.getuid());
5923 except AttributeError:
5924 fuid = int(0);
5925 except KeyError:
5926 fuid = int(0);
5927 try:
5928 fgid = int(os.getgid());
5929 except AttributeError:
5930 fgid = int(0);
5931 except KeyError:
5932 fgid = int(0);
5933 try:
5934 import pwd;
5935 try:
5936 userinfo = pwd.getpwuid(os.getuid());
5937 funame = userinfo.pw_name;
5938 except KeyError:
5939 funame = "";
5940 except AttributeError:
5941 funame = "";
5942 except ImportError:
5943 funame = "";
5944 fgname = "";
5945 try:
5946 import grp;
5947 try:
5948 groupinfo = grp.getgrgid(os.getgid());
5949 fgname = groupinfo.gr_name;
5950 except KeyError:
5951 fgname = "";
5952 except AttributeError:
5953 fgname = "";
5954 except ImportError:
5955 fgname = "";
5956 fuprint = funame;
5957 if(len(fuprint)<=0):
5958 fuprint = str(fuid);
5959 fgprint = fgname;
5960 if(len(fgprint)<=0):
5961 fgprint = str(fgid);
5962 VerbosePrintOut(PrintPermissionString(fmode, ftype) + " " + str(str(fuprint) + "/" + str(fgprint) + " " + str(member.file_size).rjust(15) + " " + datetime.datetime.utcfromtimestamp(int(time.mktime(member.date_time + (0, 0, -1)))).strftime('%Y-%m-%d %H:%M') + " " + printfname));
5963 lcfi = lcfi + 1;
5964 if(returnfp):
5965 return listcatfiles['catfp'];
5966 else:
5967 return True;
5969 if(not rarfile_support):
5970 def RarFileListFiles(infile, verbose=False, returnfp=False):
5971 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
5972 if(not os.path.exists(infile) or not os.path.isfile(infile)):
5973 return False;
5975 if(rarfile_support):
5976 def RarFileListFiles(infile, verbose=False, returnfp=False):
5977 logging.basicConfig(format="%(message)s", stream=sys.stdout, level=logging.DEBUG);
5978 if(not os.path.exists(infile) or not os.path.isfile(infile)):
5979 return False;
5980 if(not rarfile.is_rarfile(infile) and not rarfile.is_rarfile_sfx(infile)):
5981 return False;
5982 lcfi = 0;
5983 returnval = {};
5984 rarfp = rarfile.RarFile(infile, "r");
5985 rartest = rarfp.testrar();
5986 if(rartest):
5987 VerbosePrintOut("Bad file found!");
5988 for member in sorted(rarfp.infolist(), key=lambda x: x.filename):
5989 is_unix = False;
5990 is_windows = False;
5991 if(member.host_os == rarfile.RAR_OS_UNIX):
5992 is_windows = False;
5993 try:
5994 member.external_attr
5995 is_unix = True;
5996 except AttributeError:
5997 is_unix = False;
5998 elif(member.host_os == rarfile.RAR_OS_WIN32):
5999 is_unix = False;
6000 try:
6001 member.external_attr
6002 is_windows = True;
6003 except AttributeError:
6004 is_windows = False;
6005 else:
6006 is_unix = False;
6007 is_windows = False;
6008 if(is_unix and member.external_attr !=0):
6009 fpremode = int(member.external_attr);
6010 elif(member.is_file()):
6011 fpremode = int(stat.S_IFREG + 438);
6012 elif(member.is_symlink()):
6013 fpremode = int(stat.S_IFLNK + 438);
6014 elif(member.is_dir()):
6015 fpremode = int(stat.S_IFDIR + 511);
6016 if(is_windows and member.external_attr !=0):
6017 fwinattributes = int(member.external_attr);
6018 else:
6019 fwinattributes = int(0);
6020 if(is_unix and member.external_attr !=0):
6021 fmode = int(member.external_attr);
6022 fchmode = int(stat.S_IMODE(member.external_attr));
6023 ftypemod = int(stat.S_IFMT(member.external_attr));
6024 elif(member.is_file()):
6025 fmode = int(stat.S_IFREG + 438);
6026 fchmode = int(stat.S_IMODE(int(stat.S_IFREG + 438)));
6027 ftypemod = int(stat.S_IFMT(int(stat.S_IFREG + 438)));
6028 elif(member.is_symlink()):
6029 fmode = int(stat.S_IFLNK + 438);
6030 fchmode = int(stat.S_IMODE(int(stat.S_IFLNK + 438)));
6031 ftypemod = int(stat.S_IFMT(int(stat.S_IFLNK + 438)));
6032 elif(member.is_dir()):
6033 fmode = int(stat.S_IFDIR + 511);
6034 fchmode = int(stat.S_IMODE(int(stat.S_IFDIR + 511)));
6035 ftypemod = int(stat.S_IFMT(int(stat.S_IFDIR + 511)));
6036 returnval.update({lcfi: member.filename});
6037 if(not verbose):
6038 VerbosePrintOut(member.filename);
6039 if(verbose):
6040 permissions = { 'access': { '0': ('---'), '1': ('--x'), '2': ('-w-'), '3': ('-wx'), '4': ('r--'), '5': ('r-x'), '6': ('rw-'), '7': ('rwx') }, 'roles': { 0: 'owner', 1: 'group', 2: 'other' } };
6041 permissionstr = "";
6042 for fmodval in str(oct(fmode))[-3:]:
6043 permissionstr = permissionstr + permissions['access'].get(fmodval, '---');
6044 if(member.is_file()):
6045 ftype = 0;
6046 permissionstr = "-" + permissionstr;
6047 printfname = member.filename;
6048 elif(member.is_symlink()):
6049 ftype = 2;
6050 permissionstr = "l" + permissionstr;
6051 printfname = member.name + " -> " + member.read().decode("UTF-8");
6052 elif(member.is_dir()):
6053 ftype = 5;
6054 permissionstr = "d" + permissionstr;
6055 printfname = member.filename;
6056 try:
6057 fuid = int(os.getuid());
6058 except AttributeError:
6059 fuid = int(0);
6060 except KeyError:
6061 fuid = int(0);
6062 try:
6063 fgid = int(os.getgid());
6064 except AttributeError:
6065 fgid = int(0);
6066 except KeyError:
6067 fgid = int(0);
6068 try:
6069 import pwd;
6070 try:
6071 userinfo = pwd.getpwuid(os.getuid());
6072 funame = userinfo.pw_name;
6073 except KeyError:
6074 funame = "";
6075 except AttributeError:
6076 funame = "";
6077 except ImportError:
6078 funame = "";
6079 fgname = "";
6080 try:
6081 import grp;
6082 try:
6083 groupinfo = grp.getgrgid(os.getgid());
6084 fgname = groupinfo.gr_name;
6085 except KeyError:
6086 fgname = "";
6087 except AttributeError:
6088 fgname = "";
6089 except ImportError:
6090 fgname = "";
6091 fuprint = funame;
6092 if(len(fuprint)<=0):
6093 fuprint = str(fuid);
6094 fgprint = fgname;
6095 if(len(fgprint)<=0):
6096 fgprint = str(fgid);
6097 VerbosePrintOut(PrintPermissionString(fmode, ftype) + " " + str(str(fuprint) + "/" + str(fgprint) + " " + str(member.file_size).rjust(15) + " " + member.mtime.strftime('%Y-%m-%d %H:%M') + " " + printfname));
6098 lcfi = lcfi + 1;
6099 if(returnfp):
6100 return listcatfiles['catfp'];
6101 else:
6102 return True;
6104 def ListDirListFiles(infiles, dirlistfromtxt=False, compression="auto", compressionlevel=None, followlink=False, seekstart=0, seekend=0, skipchecksum=False, checksumtype="crc32", formatspecs=__file_format_list__, verbose=False, returnfp=False):
6105 outarray = BytesIO();
6106 packcat = PackArchiveFile(infiles, outarray, dirlistfromtxt, compression, compressionlevel, followlink, checksumtype, formatspecs, False, True);
6107 listcatfiles = ArchiveFileListFiles(outarray, seekstart, seekend, skipchecksum, formatspecs, verbose, returnfp);
6108 return listcatfiles;
6110 def ListDirListFilesAlt(infiles, dirlistfromtxt=False, followlink=False, listonly=True, seekstart=0, seekend=0, skipchecksum=False, checksumtype="crc32", formatspecs=__file_format_list__, verbose=False, returnfp=False):
6111 outarray = ListDirToArrayAlt(infiles, dirlistfromtxt, followlink, listonly, checksumtype, formatspecs, verbose);
6112 listcatfiles = ArchiveFileListFiles(outarray, seekstart, seekend, skipchecksum, formatspecs, verbose, returnfp);
6113 return listcatfiles;
6115 def PackArchiveFileFromListDirAlt(infiles, outfile, dirlistfromtxt=False, compression="auto", compressionlevel=None, followlink=False, skipchecksum=False, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
6116 outarray = ListDirToArrayAlt(infiles, dirlistfromtxt, followlink, False, checksumtype, extradata, formatspecs, False);
6117 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, followlink, checksumtype, skipchecksum, extradata, formatspecs, verbose, returnfp);
6118 return listcatfiles;
6120 create_alias_function("Pack", __file_format_name__, "FromListDirAlt", PackArchiveFileFromListDirAlt);
6122 def PackArchiveFileFromTarFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
6123 outarray = TarFileToArrayAlt(infile, False, checksumtype, extradata, formatspecs, False);
6124 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, False, checksumtype, False, extradata, formatspecs, verbose, returnfp);
6125 return listcatfiles;
6127 create_alias_function("Pack", __file_format_name__, "FromTarFileAlt", PackArchiveFileFromTarFileAlt);
6129 def PackArchiveFileFromZipFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
6130 outarray = ZipFileToArrayAlt(infile, False, checksumtype, extradata, formatspecs, False);
6131 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, False, checksumtype, False, extradata, formatspecs, verbose, returnfp);
6132 return listcatfiles;
6134 create_alias_function("Pack", __file_format_name__, "FromZipFileAlt", PackArchiveFileFromZipFileAlt);
6136 if(not rarfile_support):
6137 def PackArchiveFileFromRarFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
6138 return False;
6140 if(rarfile_support):
6141 def PackArchiveFileFromRarFileAlt(infile, outfile, compression="auto", compressionlevel=None, checksumtype="crc32", extradata=[], formatspecs=__file_format_list__, verbose=False, returnfp=False):
6142 outarray = RarFileToArrayAlt(infile, False, checksumtype, extradata, formatspecs, False);
6143 listcatfiles = RePackArchiveFile(outarray, outfile, compression, compressionlevel, False, checksumtype, False, extradata, formatspecs, verbose, returnfp);
6144 return listcatfiles;
6146 create_alias_function("Pack", __file_format_name__, "FromRarFileAlt", PackArchiveFileFromRarFileAlt);
6148 def download_file_from_ftp_file(url):
6149 urlparts = urlparse(url);
6150 file_name = os.path.basename(urlparts.path);
6151 file_dir = os.path.dirname(urlparts.path);
6152 if(urlparts.username is not None):
6153 ftp_username = urlparts.username;
6154 else:
6155 ftp_username = "anonymous";
6156 if(urlparts.password is not None):
6157 ftp_password = urlparts.password;
6158 elif(urlparts.password is None and urlparts.username=="anonymous"):
6159 ftp_password = "anonymous";
6160 else:
6161 ftp_password = "";
6162 if(urlparts.scheme=="ftp"):
6163 ftp = FTP();
6164 elif(urlparts.scheme=="ftps"):
6165 ftp = FTP_TLS();
6166 else:
6167 return False;
6168 if(urlparts.scheme=="sftp"):
6169 if(__use_pysftp__):
6170 return download_file_from_pysftp_file(url);
6171 else:
6172 return download_file_from_sftp_file(url);
6173 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
6174 return download_file_from_http_file(url);
6175 ftp_port = urlparts.port;
6176 if(urlparts.port is None):
6177 ftp_port = 21;
6178 try:
6179 ftp.connect(urlparts.hostname, ftp_port);
6180 except socket.gaierror:
6181 log.info("Error With URL "+url);
6182 return False;
6183 except socket.timeout:
6184 log.info("Error With URL "+url);
6185 return False;
6186 ftp.login(urlparts.username, urlparts.password);
6187 if(urlparts.scheme=="ftps"):
6188 ftp.prot_p();
6189 ftpfile = BytesIO();
6190 ftp.retrbinary("RETR "+urlparts.path, ftpfile.write);
6191 #ftp.storbinary("STOR "+urlparts.path, ftpfile.write);
6192 ftp.close();
6193 ftpfile.seek(0, 0);
6194 return ftpfile;
6196 def download_file_from_ftp_string(url):
6197 ftpfile = download_file_from_ftp_file(url);
6198 return ftpfile.read();
6200 def upload_file_to_ftp_file(ftpfile, url):
6201 urlparts = urlparse(url);
6202 file_name = os.path.basename(urlparts.path);
6203 file_dir = os.path.dirname(urlparts.path);
6204 if(urlparts.username is not None):
6205 ftp_username = urlparts.username;
6206 else:
6207 ftp_username = "anonymous";
6208 if(urlparts.password is not None):
6209 ftp_password = urlparts.password;
6210 elif(urlparts.password is None and urlparts.username=="anonymous"):
6211 ftp_password = "anonymous";
6212 else:
6213 ftp_password = "";
6214 if(urlparts.scheme=="ftp"):
6215 ftp = FTP();
6216 elif(urlparts.scheme=="ftps"):
6217 ftp = FTP_TLS();
6218 else:
6219 return False;
6220 if(urlparts.scheme=="sftp"):
6221 if(__use_pysftp__):
6222 return upload_file_from_pysftp_file(url);
6223 else:
6224 return upload_file_from_sftp_file(url);
6225 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
6226 return False;
6227 ftp_port = urlparts.port;
6228 if(urlparts.port is None):
6229 ftp_port = 21;
6230 try:
6231 ftp.connect(urlparts.hostname, ftp_port);
6232 except socket.gaierror:
6233 log.info("Error With URL "+url);
6234 return False;
6235 except socket.timeout:
6236 log.info("Error With URL "+url);
6237 return False;
6238 ftp.login(urlparts.username, urlparts.password);
6239 if(urlparts.scheme=="ftps"):
6240 ftp.prot_p();
6241 ftp.storbinary("STOR "+urlparts.path, ftpfile);
6242 ftp.close();
6243 ftpfile.seek(0, 0);
6244 return ftpfile;
6246 def upload_file_to_ftp_string(ftpstring, url):
6247 ftpfileo = BytesIO(ftpstring);
6248 ftpfile = upload_file_to_ftp_file(ftpfileo, url);
6249 ftpfileo.close();
6250 return ftpfile;
6252 def download_file_from_http_file(url, headers=geturls_headers_pycatfile_python_alt):
6253 # Parse the URL to extract username and password if present
6254 urlparts = urlparse(url);
6255 username = urlparts.username;
6256 password = urlparts.password;
6257 # Rebuild the URL without the username and password
6258 netloc = urlparts.hostname;
6259 if(urlparts.scheme=="sftp"):
6260 if(__use_pysftp__):
6261 return download_file_from_pysftp_file(url);
6262 else:
6263 return download_file_from_sftp_file(url);
6264 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
6265 return download_file_from_ftp_file(url);
6266 if urlparts.port:
6267 netloc += ':' + str(urlparts.port);
6268 rebuilt_url = urlunparse((urlparts.scheme, netloc, urlparts.path, urlparts.params, urlparts.query, urlparts.fragment));
6269 # Create a temporary file object
6270 temp_file = BytesIO();
6271 if haverequests:
6272 # Use the requests library if available
6273 if username and password:
6274 response = requests.get(rebuilt_url, headers=headers, auth=(username, password), stream=True);
6275 else:
6276 response = requests.get(rebuilt_url, headers=headers, stream=True);
6277 response.raw.decode_content = True
6278 shutil.copyfileobj(response.raw, temp_file);
6279 else:
6280 # Build a Request object for urllib
6281 request = Request(rebuilt_url, headers=headers);
6282 # Create an opener object for handling URLs
6283 if username and password:
6284 # Create a password manager
6285 password_mgr = HTTPPasswordMgrWithDefaultRealm();
6286 # Add the username and password
6287 password_mgr.add_password(None, rebuilt_url, username, password);
6288 # Create an authentication handler using the password manager
6289 auth_handler = HTTPBasicAuthHandler(password_mgr);
6290 # Build the opener with the authentication handler
6291 opener = build_opener(auth_handler);
6292 else:
6293 opener = build_opener();
6294 with opener.open(request) as response:
6295 shutil.copyfileobj(response, temp_file);
6296 # Reset file pointer to the start
6297 temp_file.seek(0);
6298 # Return the temporary file object
6299 return temp_file;
6301 def download_file_from_http_string(url, headers=geturls_headers_pycatfile_python_alt):
6302 httpfile = download_file_from_http_file(url, headers);
6303 return ftpfile.read();
6305 if(haveparamiko):
6306 def download_file_from_sftp_file(url):
6307 urlparts = urlparse(url);
6308 file_name = os.path.basename(urlparts.path);
6309 file_dir = os.path.dirname(urlparts.path);
6310 sftp_port = urlparts.port;
6311 if(urlparts.port is None):
6312 sftp_port = 22;
6313 else:
6314 sftp_port = urlparts.port;
6315 if(urlparts.username is not None):
6316 sftp_username = urlparts.username;
6317 else:
6318 sftp_username = "anonymous";
6319 if(urlparts.password is not None):
6320 sftp_password = urlparts.password;
6321 elif(urlparts.password is None and urlparts.username=="anonymous"):
6322 sftp_password = "anonymous";
6323 else:
6324 sftp_password = "";
6325 if(urlparts.scheme=="ftp"):
6326 return download_file_from_ftp_file(url);
6327 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
6328 return download_file_from_http_file(url);
6329 if(urlparts.scheme!="sftp"):
6330 return False;
6331 ssh = paramiko.SSHClient();
6332 ssh.load_system_host_keys();
6333 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy());
6334 try:
6335 ssh.connect(urlparts.hostname, port=sftp_port, username=urlparts.username, password=urlparts.password);
6336 except paramiko.ssh_exception.SSHException:
6337 return False;
6338 except socket.gaierror:
6339 log.info("Error With URL "+url);
6340 return False;
6341 except socket.timeout:
6342 log.info("Error With URL "+url);
6343 return False;
6344 sftp = ssh.open_sftp();
6345 sftpfile = BytesIO();
6346 sftp.getfo(urlparts.path, sftpfile);
6347 sftp.close();
6348 ssh.close();
6349 sftpfile.seek(0, 0);
6350 return sftpfile;
6351 else:
6352 def download_file_from_sftp_file(url):
6353 return False;
6355 if(haveparamiko):
6356 def download_file_from_sftp_string(url):
6357 sftpfile = download_file_from_sftp_file(url);
6358 return sftpfile.read();
6359 else:
6360 def download_file_from_ftp_string(url):
6361 return False;
6363 if(haveparamiko):
6364 def upload_file_to_sftp_file(sftpfile, url):
6365 urlparts = urlparse(url);
6366 file_name = os.path.basename(urlparts.path);
6367 file_dir = os.path.dirname(urlparts.path);
6368 sftp_port = urlparts.port;
6369 if(urlparts.port is None):
6370 sftp_port = 22;
6371 else:
6372 sftp_port = urlparts.port;
6373 if(urlparts.username is not None):
6374 sftp_username = urlparts.username;
6375 else:
6376 sftp_username = "anonymous";
6377 if(urlparts.password is not None):
6378 sftp_password = urlparts.password;
6379 elif(urlparts.password is None and urlparts.username=="anonymous"):
6380 sftp_password = "anonymous";
6381 else:
6382 sftp_password = "";
6383 if(urlparts.scheme=="ftp"):
6384 return upload_file_from_ftp_file(url);
6385 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
6386 return False;
6387 if(urlparts.scheme!="sftp"):
6388 return False;
6389 ssh = paramiko.SSHClient();
6390 ssh.load_system_host_keys();
6391 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy());
6392 try:
6393 ssh.connect(urlparts.hostname, port=sftp_port, username=urlparts.username, password=urlparts.password);
6394 except paramiko.ssh_exception.SSHException:
6395 return False;
6396 except socket.gaierror:
6397 log.info("Error With URL "+url);
6398 return False;
6399 except socket.timeout:
6400 log.info("Error With URL "+url);
6401 return False;
6402 sftp = ssh.open_sftp();
6403 sftp.putfo(sftpfile, urlparts.path);
6404 sftp.close();
6405 ssh.close();
6406 sftpfile.seek(0, 0);
6407 return sftpfile;
6408 else:
6409 def upload_file_to_sftp_file(sftpfile, url):
6410 return False;
6412 if(haveparamiko):
6413 def upload_file_to_sftp_string(sftpstring, url):
6414 sftpfileo = BytesIO(sftpstring);
6415 sftpfile = upload_file_to_sftp_files(ftpfileo, url);
6416 sftpfileo.close();
6417 return sftpfile;
6418 else:
6419 def upload_file_to_sftp_string(url):
6420 return False;
6422 if(havepysftp):
6423 def download_file_from_pysftp_file(url):
6424 urlparts = urlparse(url);
6425 file_name = os.path.basename(urlparts.path);
6426 file_dir = os.path.dirname(urlparts.path);
6427 sftp_port = urlparts.port;
6428 if(urlparts.port is None):
6429 sftp_port = 22;
6430 else:
6431 sftp_port = urlparts.port;
6432 if(urlparts.username is not None):
6433 sftp_username = urlparts.username;
6434 else:
6435 sftp_username = "anonymous";
6436 if(urlparts.password is not None):
6437 sftp_password = urlparts.password;
6438 elif(urlparts.password is None and urlparts.username=="anonymous"):
6439 sftp_password = "anonymous";
6440 else:
6441 sftp_password = "";
6442 if(urlparts.scheme=="ftp"):
6443 return download_file_from_ftp_file(url);
6444 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
6445 return download_file_from_http_file(url);
6446 if(urlparts.scheme!="sftp"):
6447 return False;
6448 try:
6449 pysftp.Connection(urlparts.hostname, port=sftp_port, username=urlparts.username, password=urlparts.password);
6450 except paramiko.ssh_exception.SSHException:
6451 return False;
6452 except socket.gaierror:
6453 log.info("Error With URL "+url);
6454 return False;
6455 except socket.timeout:
6456 log.info("Error With URL "+url);
6457 return False;
6458 sftp = ssh.open_sftp();
6459 sftpfile = BytesIO();
6460 sftp.getfo(urlparts.path, sftpfile);
6461 sftp.close();
6462 ssh.close();
6463 sftpfile.seek(0, 0);
6464 return sftpfile;
6465 else:
6466 def download_file_from_pysftp_file(url):
6467 return False;
6469 if(havepysftp):
6470 def download_file_from_pysftp_string(url):
6471 sftpfile = download_file_from_pysftp_file(url);
6472 return sftpfile.read();
6473 else:
6474 def download_file_from_ftp_string(url):
6475 return False;
6477 if(havepysftp):
6478 def upload_file_to_pysftp_file(sftpfile, url):
6479 urlparts = urlparse(url);
6480 file_name = os.path.basename(urlparts.path);
6481 file_dir = os.path.dirname(urlparts.path);
6482 sftp_port = urlparts.port;
6483 if(urlparts.port is None):
6484 sftp_port = 22;
6485 else:
6486 sftp_port = urlparts.port;
6487 if(urlparts.username is not None):
6488 sftp_username = urlparts.username;
6489 else:
6490 sftp_username = "anonymous";
6491 if(urlparts.password is not None):
6492 sftp_password = urlparts.password;
6493 elif(urlparts.password is None and urlparts.username=="anonymous"):
6494 sftp_password = "anonymous";
6495 else:
6496 sftp_password = "";
6497 if(urlparts.scheme=="ftp"):
6498 return upload_file_from_ftp_file(url);
6499 elif(urlparts.scheme=="http" or urlparts.scheme=="https"):
6500 return False;
6501 if(urlparts.scheme!="sftp"):
6502 return False;
6503 try:
6504 pysftp.Connection(urlparts.hostname, port=sftp_port, username=urlparts.username, password=urlparts.password);
6505 except paramiko.ssh_exception.SSHException:
6506 return False;
6507 except socket.gaierror:
6508 log.info("Error With URL "+url);
6509 return False;
6510 except socket.timeout:
6511 log.info("Error With URL "+url);
6512 return False;
6513 sftp = ssh.open_sftp();
6514 sftp.putfo(sftpfile, urlparts.path);
6515 sftp.close();
6516 ssh.close();
6517 sftpfile.seek(0, 0);
6518 return sftpfile;
6519 else:
6520 def upload_file_to_pysftp_file(sftpfile, url):
6521 return False;
6523 if(havepysftp):
6524 def upload_file_to_pysftp_string(sftpstring, url):
6525 sftpfileo = BytesIO(sftpstring);
6526 sftpfile = upload_file_to_pysftp_files(ftpfileo, url);
6527 sftpfileo.close();
6528 return sftpfile;
6529 else:
6530 def upload_file_to_pysftp_string(url):
6531 return False;
6533 def download_file_from_internet_file(url, headers=geturls_headers_pycatfile_python_alt):
6534 urlparts = urlparse(url);
6535 if(urlparts.scheme=="http" or urlparts.scheme=="https"):
6536 return download_file_from_http_file(url, headers);
6537 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
6538 return download_file_from_ftp_file(url);
6539 elif(urlparts.scheme=="sftp"):
6540 if(__use_pysftp__ and havepysftp):
6541 return download_file_from_pysftp_file(url);
6542 else:
6543 return download_file_from_sftp_file(url);
6544 else:
6545 return False;
6546 return False;
6548 def download_file_from_internet_string(url, headers=geturls_headers_pycatfile_python_alt):
6549 urlparts = urlparse(url);
6550 if(urlparts.scheme=="http" or urlparts.scheme=="https"):
6551 return download_file_from_http_string(url, headers);
6552 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
6553 return download_file_from_ftp_string(url);
6554 elif(urlparts.scheme=="sftp"):
6555 if(__use_pysftp__ and havepysftp):
6556 return download_file_from_pysftp_string(url);
6557 else:
6558 return download_file_from_sftp_string(url);
6559 else:
6560 return False;
6561 return False;
6563 def upload_file_to_internet_file(ifp, url):
6564 urlparts = urlparse(url);
6565 if(urlparts.scheme=="http" or urlparts.scheme=="https"):
6566 return False;
6567 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
6568 return upload_file_from_ftp_file(ifp, url);
6569 elif(urlparts.scheme=="sftp"):
6570 if(__use_pysftp__ and havepysftp):
6571 return upload_file_from_pysftp_file(ifp, url);
6572 else:
6573 return download_file_from_sftp_file(ifp, url);
6574 else:
6575 return False;
6576 return False;
6578 def upload_file_to_internet_string(ifp, url):
6579 urlparts = urlparse(url);
6580 if(urlparts.scheme=="http" or urlparts.scheme=="https"):
6581 return False;
6582 elif(urlparts.scheme=="ftp" or urlparts.scheme=="ftps"):
6583 return upload_file_from_ftp_string(ifp, url);
6584 elif(urlparts.scheme=="sftp"):
6585 if(__use_pysftp__ and havepysftp):
6586 return upload_file_from_pysftp_string(ifp, url);
6587 else:
6588 return download_file_from_sftp_string(ifp, url);
6589 else:
6590 return False;
6591 return False;
6593 try:
6594 if(hasattr(shutil, "register_archive_format")):
6595 # Register the packing format
6596 shutil.register_archive_format(__file_format_name__, PackArchiveFileFunc, description='Pack concatenated files');
6597 except shutil.RegistryError:
6598 pass;
6600 try:
6601 if(hasattr(shutil, "register_unpack_format")):
6602 # Register the unpacking format
6603 shutil.register_unpack_format(__file_format_name__, archivefile_extensions, UnPackArchiveFileFunc, description='UnPack concatenated files');
6604 except shutil.RegistryError:
6605 pass;