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