88import hmac
99import json
1010import stat
11- import datetime
1211import shutil
1312import logging
13+ import zipfile
1414import platform
15+ import datetime
1516import binascii
1617import hashlib
1718import inspect
@@ -1557,6 +1558,17 @@ def CheckCompressionSubType(infile, formatspecs=__file_format_multi_dict__, file
15571558 fp .close ()
15581559 return filetype
15591560
1561+ def _advance (fp , base , n ):
1562+ """
1563+ Move file position to right after the BOM/signature.
1564+ If fp is not seekable, this silently does nothing.
1565+ """
1566+ try :
1567+ fp .seek (base + n , 0 )
1568+ except Exception :
1569+ # Not seekable or error; ignore
1570+ pass
1571+
15601572def GetFileEncoding (infile , filestart = 0 , closefp = True ):
15611573 """
15621574 Detect file/text encoding from BOM (and a few special signatures).
@@ -3086,6 +3098,54 @@ def CompressOpenFile(outfile, compressionenable=True, compressionlevel=None):
30863098
30873099 return outfp
30883100
3101+ def GzipCompressData (data , compresslevel = 9 ):
3102+ try :
3103+ # Try using modern gzip.compress if available
3104+ compressed_data = gzip .compress (data , compresslevel = compresslevel )
3105+ except AttributeError :
3106+ # Fallback to older method for Python 2.x and older 3.x versions
3107+ out = MkTempFile ()
3108+ with gzip .GzipFile (filename = None , fileobj = out , mode = "wb" , compresslevel = compresslevel ) as f :
3109+ f .write (data )
3110+ out .seek (0 , 0 )
3111+ compressed_data = out .read ()
3112+ return compressed_data
3113+
3114+
3115+ def GzipDecompressData (compressed_data ):
3116+ try :
3117+ # Try using modern gzip.decompress if available
3118+ decompressed_data = gzip .decompress (compressed_data )
3119+ except AttributeError :
3120+ # Fallback to older method for Python 2.x and older 3.x versions
3121+ inp = MkTempFile (compressed_data )
3122+ with gzip .GzipFile (filename = None , fileobj = inp , mode = "rb" ) as f :
3123+ decompressed_data = f .read ()
3124+ return decompressed_data
3125+
3126+
3127+ def BzipCompressData (data , compresslevel = 9 ):
3128+ try :
3129+ # Try using modern bz2.compress if available
3130+ compressed_data = bz2 .compress (data , compresslevel = compresslevel )
3131+ except AttributeError :
3132+ # Fallback to older method for Python 2.x and older 3.x versions
3133+ compressor = bz2 .BZ2Compressor (compresslevel )
3134+ compressed_data = compressor .compress (data )
3135+ compressed_data += compressor .flush ()
3136+ return compressed_data
3137+
3138+
3139+ def BzipDecompressData (compressed_data ):
3140+ try :
3141+ # Try using modern bz2.decompress if available
3142+ decompressed_data = bz2 .decompress (compressed_data )
3143+ except AttributeError :
3144+ # Fallback to older method for Python 2.x and older 3.x versions
3145+ decompressor = bz2 .BZ2Decompressor ()
3146+ decompressed_data = decompressor .decompress (compressed_data )
3147+ return decompressed_data
3148+
30893149def GetKeyByFormatExtension (format_extension , formatspecs = __file_format_multi_dict__ ):
30903150 for key , value in formatspecs .items ():
30913151 if value .get ('format_extension' ) == format_extension :
@@ -7736,7 +7796,7 @@ def RePackCatFile(infile, outfile, fmttype="auto", compression="auto", compressw
77367796 elif isinstance (infile , list ):
77377797 listarrayfileslist = infile
77387798 else :
7739- if (infile != "-" and not isinstance (infile , bytes_type ) # bytes is str on Py2
7799+ if (infile != "-" and not isinstance (infile , ( bytes , bytearray , memoryview ) ) # bytes is str on Py2
77407800 and not hasattr (infile , "read" ) and not hasattr (infile , "write" )):
77417801 infile = RemoveWindowsPath (infile )
77427802 listarrayfileslist = CatFileToArray (
@@ -7757,7 +7817,7 @@ def RePackCatFile(infile, outfile, fmttype="auto", compression="auto", compressw
77577817 formatspecs = formatspecs .get (fmttype , formatspecs )
77587818
77597819 # ---------- Outfile path normalization (fixed: check outfile, not infile) ----------
7760- if (outfile != "-" and not isinstance (outfile , bytes_type )
7820+ if (outfile != "-" and not isinstance (outfile , ( bytes , bytearray , memoryview ) )
77617821 and not hasattr (outfile , "read" ) and not hasattr (outfile , "write" )):
77627822 outfile = RemoveWindowsPath (outfile )
77637823
@@ -7775,8 +7835,6 @@ def RePackCatFile(infile, outfile, fmttype="auto", compression="auto", compressw
77757835 if outfile == "-" or outfile is None :
77767836 verbose = False
77777837 fp = MkTempFile ()
7778- elif (isinstance (outfile , FileLikeAdapter )):
7779- fp = outfile
77807838 elif hasattr (outfile , "read" ) or hasattr (outfile , "write" ):
77817839 fp = outfile
77827840 elif re .findall (__upload_proto_support__ , outfile ):
0 commit comments