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