From 04e3d246c2daed55bdae5f3f20b380d65b72a019 Mon Sep 17 00:00:00 2001 From: Kazuki Suzuki Przyborowski Date: Wed, 19 Oct 2022 19:11:21 -0500 Subject: [PATCH] Update pycatfile.py --- pycatfile.py | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 85 insertions(+), 4 deletions(-) diff --git a/pycatfile.py b/pycatfile.py index dde23d1..e66614d 100755 --- a/pycatfile.py +++ b/pycatfile.py @@ -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"): @@ -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]; -- 2.11.4.GIT