Skip to content

Commit

Permalink
Update pycatfile.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KazukiPrzyborowski authored Oct 20, 2022
1 parent 55e48b8 commit 04e3d24
Showing 1 changed file with 85 additions and 4 deletions.
89 changes: 85 additions & 4 deletions pycatfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,24 +281,98 @@ def CheckCompressionType(infile, closefp=True):
if(prefp==binascii.unhexlify("504B0304")):
filetype = "zipfile";
catfp.seek(0, 0);
prefp = catfp.read(5);
if(prefp==binascii.unhexlify("7573746172")):
filetype = "tarfile";
catfp.seek(0, 0);
prefp = catfp.read(7);
if(prefp==binascii.unhexlify("fd377a585a0000")):
filetype = "lzma";
if(prefp==binascii.unhexlify("43617446696c65")):
filetype = "catfile";
catfp.seek(0, 0);
prefp = catfp.read(5);
if(prefp==binascii.unhexlify("7573746172")):
filetype = "tarfile";
catfp.seek(0, 0);
prefp = catfp.read(9);
if(prefp==binascii.unhexlify("894c5a4f000d0a1a0a")):
filetype = "lzo";
catfp.seek(0, 0);
prefp = catfp.read(10);
if(prefp==binascii.unhexlify("7061785f676c6f62616c")):
filetype = "tarfile";
catfp.seek(0, 0);
if(closefp):
catfp.close();
return filetype;

def CheckCompressionSubType(infile):
compresscheck = CheckCompressionType(infile, False);
if(not compresscheck):
fextname = os.path.splitext(infile)[1];
if(fextname==".gz"):
compresscheck = "gzip";
if(fextname==".bz2"):
compresscheck = "bzip2";
if(fextname==".zst"):
compresscheck = "zstd";
if(fextname==".lz4"):
compresscheck = "lz4";
if(fextname==".lzo" or fextname==".lzop"):
compresscheck = "lzo";
if(fextname==".lzma" or fextname==".xz"):
compresscheck = "lzma";
if(not compresscheck):
return False;
if(compresscheck=="catfile"):
return "catfile";
if(compresscheck=="tarfile"):
return "tarfile";
if(compresscheck=="zipfile"):
return "zipfile";
if(compresscheck=="gzip"):
try:
import gzip;
except ImportError:
return False;
catfp = gzip.open(infile, "rb");
if(compresscheck=="bzip2"):
try:
import bz2;
except ImportError:
return False;
catfp = bz2.BZ2File(infile, "rb");
if(compresscheck=="lz4"):
try:
import lz4.frame;
except ImportError:
return False;
catfp = lz4.frame.open(infile, "rb");
if(compresscheck=="zstd"):
try:
import zstandard;
except ImportError:
return False;
catfp = zstandard.open(infile, "rb");
if(compresscheck=="lzma"):
try:
import lzma;
except ImportError:
return False;
catfp = lzma.open(infile, "rb");
filetype = False;
prefp = catfp.read(5);
if(prefp==binascii.unhexlify("7573746172")):
filetype = "tarfile";
catfp.seek(0, 0);
prefp = catfp.read(7);
if(prefp==binascii.unhexlify("43617446696c65")):
filetype = "catfile";
catfp.seek(0, 0);
prefp = catfp.read(10);
if(prefp==binascii.unhexlify("7061785f676c6f62616c")):
filetype = "tarfile";
catfp.seek(0, 0);
catfp.close();
return filetype;

def GetCompressionMimeType(infile):
compresscheck = CheckCompressionType(fp, False);
if(compresscheck=="gzip" or compresscheck=="gz"):
Expand Down Expand Up @@ -1227,6 +1301,13 @@ def CatFileToArray(infile, seekstart=0, seekend=0, listonly=False, skipchecksum=
catfp.seek(0, 0);
else:
infile = RemoveWindowsPath(infile);
checkcompressfile = CheckCompressionSubType(infile);
if(checkcompressfile=="tarfile"):
return TarFileToArray(infile, seekstart, seekend, listonly, skipchecksum, returnfp);
if(checkcompressfile=="zipfile"):
return ZipFileToArray(infile, seekstart, seekend, listonly, skipchecksum, returnfp);
if(checkcompressfile!="catfile"):
return False;
compresscheck = CheckCompressionType(infile, True);
if(not compresscheck):
fextname = os.path.splitext(infile)[1];
Expand Down

0 comments on commit 04e3d24

Please sign in to comment.