Skip to content

Commit e0957ac

Browse files
author
Kazuki Suzuki Przyborowski
committed
Update pycatfile.py
1 parent 901c153 commit e0957ac

1 file changed

Lines changed: 108 additions & 50 deletions

File tree

pycatfile.py

Lines changed: 108 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,22 +2426,31 @@ def ReadFileDataBySizeWithContent(fp, listonly=False, uncompress=True, skipcheck
24262426
formatspecs = FormatSpecsListToDict(formatspecs)
24272427
delimiter = formatspecs['format_delimiter']
24282428
curloc = fp.tell()
2429+
try:
2430+
fp.seek(0, 2);
2431+
except OSError:
2432+
SeekToEndOfFile(fp);
2433+
except ValueError:
2434+
SeekToEndOfFile(fp);
2435+
CatSize = fp.tell();
2436+
CatSizeEnd = CatSize;
2437+
fp.seek(curloc, 0)
24292438
if(curloc > 0):
24302439
fp.seek(0, 0)
24312440
catheaderver = str(int(formatspecs['format_ver'].replace(".", "")))
2432-
catstring = catfp.read(formatspecs['format_len'] + len(catheaderver)).decode("UTF-8")
2441+
catstring = fp.read(formatspecs['format_len'] + len(catheaderver)).decode("UTF-8")
24332442
catdelszie = len(formatspecs['format_delimiter'])
2434-
catdel = catfp.read(catdelszie).decode("UTF-8")
2443+
catdel = fp.read(catdelszie).decode("UTF-8")
24352444
if(catstring != formatspecs['format_magic']+catheaderver):
24362445
return False
24372446
if(catdel != formatspecs['format_delimiter']):
24382447
return False
24392448
if(formatspecs['new_style']):
24402449
catheader = ReadFileHeaderDataBySize(
2441-
catfp, formatspecs['format_delimiter'])
2450+
fp, formatspecs['format_delimiter'])
24422451
else:
24432452
catheader = ReadFileHeaderDataWoSize(
2444-
catfp, formatspecs['format_delimiter'])
2453+
fp, formatspecs['format_delimiter'])
24452454
if(curloc > 0):
24462455
fp.seek(curloc, 0)
24472456
fprechecksumtype = catheader[-2]
@@ -2473,6 +2482,15 @@ def ReadFileDataBySizeWithContentToArray(fp, seekstart=0, seekend=0, listonly=Fa
24732482
formatspecs = FormatSpecsListToDict(formatspecs)
24742483
delimiter = formatspecs['format_delimiter']
24752484
curloc = fp.tell()
2485+
try:
2486+
fp.seek(0, 2);
2487+
except OSError:
2488+
SeekToEndOfFile(fp);
2489+
except ValueError:
2490+
SeekToEndOfFile(fp);
2491+
CatSize = fp.tell();
2492+
CatSizeEnd = CatSize;
2493+
fp.seek(curloc, 0)
24762494
if(curloc > 0):
24772495
fp.seek(0, 0)
24782496
catheaderver = str(int(formatspecs['format_ver'].replace(".", "")))
@@ -2601,6 +2619,15 @@ def ReadFileDataBySizeWithContentToList(fp, seekstart=0, seekend=0, listonly=Fal
26012619
formatspecs = FormatSpecsListToDict(formatspecs)
26022620
delimiter = formatspecs['format_delimiter']
26032621
curloc = fp.tell()
2622+
try:
2623+
fp.seek(0, 2);
2624+
except OSError:
2625+
SeekToEndOfFile(fp);
2626+
except ValueError:
2627+
SeekToEndOfFile(fp);
2628+
CatSize = fp.tell();
2629+
CatSizeEnd = CatSize;
2630+
fp.seek(curloc, 0)
26042631
if(curloc > 0):
26052632
fp.seek(0, 0)
26062633
catheaderver = str(int(formatspecs['format_ver'].replace(".", "")))
@@ -2727,20 +2754,27 @@ def ReadFileDataBySizeWithContentToList(fp, seekstart=0, seekend=0, listonly=Fal
27272754

27282755
def ReadInFileBySizeWithContentToArray(infile, seekstart=0, seekend=0, listonly=False, contentasfile=True, uncompress=True, skipchecksum=False, formatspecs=__file_format_dict__):
27292756
formatspecs = FormatSpecsListToDict(formatspecs)
2730-
delimiter = formatspecs['format_delimiter']
27312757
if(hasattr(infile, "read") or hasattr(infile, "write")):
27322758
fp = infile
27332759
fp.seek(0, 0)
27342760
compresscheck = CheckCompressionType(fp, formatspecs, False)
27352761
fp.seek(0, 0)
27362762
fp = UncompressArchiveFile(fp, formatspecs)
27372763
checkcompressfile = CheckCompressionSubType(fp, formatspecs, True)
2738-
if(checkcompressfile != formatspecs['format_lower']):
2764+
if(checkcompressfile == "tarfile" and TarFileCheck(infile)):
2765+
return TarFileToArray(infile, seekstart, seekend, listonly, contentasfile, skipchecksum, formatspecs, returnfp)
2766+
elif(checkcompressfile == "zipfile" and zipfile.is_zipfile(infile)):
2767+
return ZipFileToArray(infile, seekstart, seekend, listonly, contentasfile, skipchecksum, formatspecs, returnfp)
2768+
elif(rarfile_support and checkcompressfile == "rarfile" and (rarfile.is_rarfile(infile) or rarfile.is_rarfile_sfx(infile))):
2769+
return RarFileToArray(infile, seekstart, seekend, listonly, contentasfile, skipchecksum, formatspecs, returnfp)
2770+
elif(py7zr_support and checkcompressfile == "7zipfile" and py7zr.is_7zfile(infile)):
2771+
return SevenZipFileToArray(infile, seekstart, seekend, listonly, contentasfile, skipchecksum, formatspecs, returnfp)
2772+
elif(checkcompressfile != formatspecs['format_lower']):
27392773
return False
27402774
if(not fp):
27412775
return False
2742-
if(not compresscheck and hasattr(catfp, "name")):
2743-
fextname = os.path.splitext(catfp.name)[1]
2776+
if(not compresscheck and hasattr(fp, "name")):
2777+
fextname = os.path.splitext(fp.name)[1]
27442778
if(fextname == ".gz"):
27452779
compresscheck = "gzip"
27462780
elif(fextname == ".bz2"):
@@ -2773,6 +2807,16 @@ def ReadInFileBySizeWithContentToArray(infile, seekstart=0, seekend=0, listonly=
27732807
if(not fp):
27742808
return False
27752809
fp.seek(0, 0)
2810+
elif(isinstance(infile, bytes) and sys.version_info[0] >= 3):
2811+
fp = BytesIO()
2812+
fp.write(infile)
2813+
fp.seek(0, 0)
2814+
compresscheck = CheckCompressionType(fp, formatspecs, False)
2815+
fp.seek(0, 0)
2816+
fp = UncompressArchiveFile(fp, formatspecs)
2817+
if(not fp):
2818+
return False
2819+
fp.seek(0, 0)
27762820
elif(re.findall("^(http|https|ftp|ftps|sftp):\\/\\/", infile)):
27772821
fp = download_file_from_internet_file(infile)
27782822
compresscheck = CheckCompressionType(fp, formatspecs, False)
@@ -2796,14 +2840,23 @@ def ReadInFileBySizeWithContentToArray(infile, seekstart=0, seekend=0, listonly=
27962840
compresscheck = "zlib"
27972841
else:
27982842
return False
2843+
fp.seek(0, 0)
27992844
fp = UncompressArchiveFile(fp, formatspecs)
28002845
if(not fp):
28012846
return False
28022847
fp.seek(0, 0)
28032848
else:
28042849
infile = RemoveWindowsPath(infile)
28052850
checkcompressfile = CheckCompressionSubType(infile, formatspecs, True)
2806-
if(checkcompressfile != formatspecs['format_lower']):
2851+
if(checkcompressfile == "tarfile" and TarFileCheck(infile)):
2852+
return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp)
2853+
elif(checkcompressfile == "zipfile" and zipfile.is_zipfile(infile)):
2854+
return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp)
2855+
elif(rarfile_support and checkcompressfile == "rarfile" and (rarfile.is_rarfile(infile) or rarfile.is_rarfile_sfx(infile))):
2856+
return RarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp)
2857+
elif(py7zr_support and checkcompressfile == "7zipfile" and py7zr.is_7zfile(infile)):
2858+
return SevenZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp)
2859+
elif(checkcompressfile != formatspecs['format_lower']):
28072860
return False
28082861
compresscheck = CheckCompressionType(infile, formatspecs, True)
28092862
if(not compresscheck):
@@ -2834,20 +2887,27 @@ def ReadInFileBySizeWithContentToArray(infile, seekstart=0, seekend=0, listonly=
28342887

28352888
def ReadInFileBySizeWithContentToList(infile, seekstart=0, seekend=0, listonly=False, uncompress=True, skipchecksum=False, formatspecs=__file_format_dict__):
28362889
formatspecs = FormatSpecsListToDict(formatspecs)
2837-
delimiter = formatspecs['format_delimiter']
28382890
if(hasattr(infile, "read") or hasattr(infile, "write")):
28392891
fp = infile
28402892
fp.seek(0, 0)
28412893
compresscheck = CheckCompressionType(fp, formatspecs, False)
28422894
fp.seek(0, 0)
28432895
fp = UncompressArchiveFile(fp, formatspecs)
28442896
checkcompressfile = CheckCompressionSubType(fp, formatspecs, True)
2845-
if(checkcompressfile != formatspecs['format_lower']):
2897+
if(checkcompressfile == "tarfile" and TarFileCheck(infile)):
2898+
return TarFileToArray(infile, seekstart, seekend, listonly, contentasfile, skipchecksum, formatspecs, returnfp)
2899+
elif(checkcompressfile == "zipfile" and zipfile.is_zipfile(infile)):
2900+
return ZipFileToArray(infile, seekstart, seekend, listonly, contentasfile, skipchecksum, formatspecs, returnfp)
2901+
elif(rarfile_support and checkcompressfile == "rarfile" and (rarfile.is_rarfile(infile) or rarfile.is_rarfile_sfx(infile))):
2902+
return RarFileToArray(infile, seekstart, seekend, listonly, contentasfile, skipchecksum, formatspecs, returnfp)
2903+
elif(py7zr_support and checkcompressfile == "7zipfile" and py7zr.is_7zfile(infile)):
2904+
return SevenZipFileToArray(infile, seekstart, seekend, listonly, contentasfile, skipchecksum, formatspecs, returnfp)
2905+
elif(checkcompressfile != formatspecs['format_lower']):
28462906
return False
28472907
if(not fp):
28482908
return False
2849-
if(not compresscheck and hasattr(catfp, "name")):
2850-
fextname = os.path.splitext(catfp.name)[1]
2909+
if(not compresscheck and hasattr(fp, "name")):
2910+
fextname = os.path.splitext(fp.name)[1]
28512911
if(fextname == ".gz"):
28522912
compresscheck = "gzip"
28532913
elif(fextname == ".bz2"):
@@ -2880,6 +2940,16 @@ def ReadInFileBySizeWithContentToList(infile, seekstart=0, seekend=0, listonly=F
28802940
if(not fp):
28812941
return False
28822942
fp.seek(0, 0)
2943+
elif(isinstance(infile, bytes) and sys.version_info[0] >= 3):
2944+
fp = BytesIO()
2945+
fp.write(infile)
2946+
fp.seek(0, 0)
2947+
compresscheck = CheckCompressionType(fp, formatspecs, False)
2948+
fp.seek(0, 0)
2949+
fp = UncompressArchiveFile(fp, formatspecs)
2950+
if(not fp):
2951+
return False
2952+
fp.seek(0, 0)
28832953
elif(re.findall("^(http|https|ftp|ftps|sftp):\\/\\/", infile)):
28842954
fp = download_file_from_internet_file(infile)
28852955
compresscheck = CheckCompressionType(fp, formatspecs, False)
@@ -2911,7 +2981,15 @@ def ReadInFileBySizeWithContentToList(infile, seekstart=0, seekend=0, listonly=F
29112981
else:
29122982
infile = RemoveWindowsPath(infile)
29132983
checkcompressfile = CheckCompressionSubType(infile, formatspecs, True)
2914-
if(checkcompressfile != formatspecs['format_lower']):
2984+
if(checkcompressfile == "tarfile" and TarFileCheck(infile)):
2985+
return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp)
2986+
elif(checkcompressfile == "zipfile" and zipfile.is_zipfile(infile)):
2987+
return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp)
2988+
elif(rarfile_support and checkcompressfile == "rarfile" and (rarfile.is_rarfile(infile) or rarfile.is_rarfile_sfx(infile))):
2989+
return RarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp)
2990+
elif(py7zr_support and checkcompressfile == "7zipfile" and py7zr.is_7zfile(infile)):
2991+
return SevenZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, formatspecs, returnfp)
2992+
elif(checkcompressfile != formatspecs['format_lower']):
29152993
return False
29162994
compresscheck = CheckCompressionType(infile, formatspecs, True)
29172995
if(not compresscheck):
@@ -3025,6 +3103,10 @@ def MakeEmptyFilePointer(fp, checksumtype="crc32", formatspecs=__file_format_dic
30253103
return fp
30263104

30273105

3106+
def MakeEmptyArchiveFilePointer(fp, checksumtype="crc32", formatspecs=__file_format_dict__):
3107+
return MakeEmptyFilePointer(fp, checksumtype, formatspecs)
3108+
3109+
30283110
def MakeEmptyFile(outfile, compression="auto", compresswholefile=True, compressionlevel=None, checksumtype="crc32", formatspecs=__file_format_dict__, returnfp=False):
30293111
formatspecs = FormatSpecsListToDict(formatspecs)
30303112
if(outfile != "-" and outfile is not None and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
@@ -3084,6 +3166,10 @@ def MakeEmptyFile(outfile, compression="auto", compresswholefile=True, compressi
30843166
return True
30853167

30863168

3169+
def MakeEmptyArchiveFile(outfile, compression="auto", compresswholefile=True, compressionlevel=None, checksumtype="crc32", formatspecs=__file_format_dict__, returnfp=False):
3170+
return MakeEmptyFile(outfile, compression, compresswholefile, compressionlevel, checksumtype, formatspecs, returnfp)
3171+
3172+
30873173
def AppendFileHeaderWithContent(fp, filevalues=[], extradata=[], filecontent="", checksumtype=["crc32", "crc32"], formatspecs=__file_format_dict__):
30883174
if(not hasattr(fp, "write")):
30893175
return False
@@ -5959,7 +6045,7 @@ def ArchiveFileSeekToFileNum(infile, seekto=0, listonly=False, contentasfile=Tru
59596045
if(not compresscheck):
59606046
return False
59616047
catfp = UncompressFile(infile, formatspecs, "rb")
5962-
'''
6048+
curloc = catfp.tell()
59636049
try:
59646050
catfp.seek(0, 2);
59656051
except OSError:
@@ -5968,14 +6054,7 @@ def ArchiveFileSeekToFileNum(infile, seekto=0, listonly=False, contentasfile=Tru
59686054
SeekToEndOfFile(catfp);
59696055
CatSize = catfp.tell();
59706056
CatSizeEnd = CatSize;
5971-
'''
5972-
try:
5973-
catfp.seek(0, 0)
5974-
except OSError:
5975-
return False
5976-
except ValueError:
5977-
return False
5978-
curloc = catfp.tell()
6057+
catfp.seek(curloc, 0)
59796058
if(curloc > 0):
59806059
catfp.seek(0, 0)
59816060
catheaderver = str(int(formatspecs['format_ver'].replace(".", "")))
@@ -6234,7 +6313,7 @@ def ArchiveFileSeekToFileName(infile, seekfile=None, listonly=False, contentasfi
62346313
if(not compresscheck):
62356314
return False
62366315
catfp = UncompressFile(infile, formatspecs, "rb")
6237-
'''
6316+
curloc = catfp.tell()
62386317
try:
62396318
catfp.seek(0, 2);
62406319
except OSError:
@@ -6243,14 +6322,7 @@ def ArchiveFileSeekToFileName(infile, seekfile=None, listonly=False, contentasfi
62436322
SeekToEndOfFile(catfp);
62446323
CatSize = catfp.tell();
62456324
CatSizeEnd = CatSize;
6246-
'''
6247-
try:
6248-
catfp.seek(0, 0)
6249-
except OSError:
6250-
return False
6251-
except ValueError:
6252-
return False
6253-
curloc = catfp.tell()
6325+
catfp.seek(curloc, 0)
62546326
if(curloc > 0):
62556327
catfp.seek(0, 0)
62566328
catheaderver = str(int(formatspecs['format_ver'].replace(".", "")))
@@ -6542,7 +6614,7 @@ def ArchiveFileValidate(infile, formatspecs=__file_format_dict__, verbose=False,
65426614
if(not compresscheck):
65436615
return False
65446616
catfp = UncompressFile(infile, formatspecs, "rb")
6545-
'''
6617+
curloc = catfp.tell()
65466618
try:
65476619
catfp.seek(0, 2);
65486620
except OSError:
@@ -6551,14 +6623,7 @@ def ArchiveFileValidate(infile, formatspecs=__file_format_dict__, verbose=False,
65516623
SeekToEndOfFile(catfp);
65526624
CatSize = catfp.tell();
65536625
CatSizeEnd = CatSize;
6554-
'''
6555-
try:
6556-
catfp.seek(0, 0)
6557-
except OSError:
6558-
return False
6559-
except ValueError:
6560-
return False
6561-
curloc = catfp.tell()
6626+
catfp.seek(curloc, 0)
65626627
if(curloc > 0):
65636628
catfp.seek(0, 0)
65646629
catheaderver = str(int(formatspecs['format_ver'].replace(".", "")))
@@ -6884,7 +6949,7 @@ def ArchiveFileToArray(infile, seekstart=0, seekend=0, listonly=False, contentas
68846949
if(not compresscheck):
68856950
return False
68866951
catfp = UncompressFile(infile, formatspecs, "rb")
6887-
'''
6952+
curloc = catfp.tell()
68886953
try:
68896954
catfp.seek(0, 2);
68906955
except OSError:
@@ -6893,14 +6958,7 @@ def ArchiveFileToArray(infile, seekstart=0, seekend=0, listonly=False, contentas
68936958
SeekToEndOfFile(catfp);
68946959
CatSize = catfp.tell();
68956960
CatSizeEnd = CatSize;
6896-
'''
6897-
try:
6898-
catfp.seek(0, 0)
6899-
except OSError:
6900-
return False
6901-
except ValueError:
6902-
return False
6903-
curloc = catfp.tell()
6961+
catfp.seek(curloc, 0)
69046962
if(curloc > 0):
69056963
catfp.seek(0, 0)
69066964
catheaderver = str(int(formatspecs['format_ver'].replace(".", "")))

0 commit comments

Comments
 (0)