Skip to content

Commit 4f7646b

Browse files
committed
Small update
1 parent 62bd963 commit 4f7646b

1 file changed

Lines changed: 63 additions & 5 deletions

File tree

pyfoxfile/pyfile.py

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
import hmac
99
import json
1010
import stat
11-
import datetime
1211
import shutil
1312
import logging
13+
import zipfile
1414
import platform
15+
import datetime
1516
import binascii
1617
import hashlib
1718
import inspect
@@ -1558,6 +1559,17 @@ def CheckCompressionSubType(infile, formatspecs=__file_format_multi_dict__, file
15581559
fp.close()
15591560
return filetype
15601561

1562+
def _advance(fp, base, n):
1563+
"""
1564+
Move file position to right after the BOM/signature.
1565+
If fp is not seekable, this silently does nothing.
1566+
"""
1567+
try:
1568+
fp.seek(base + n, 0)
1569+
except Exception:
1570+
# Not seekable or error; ignore
1571+
pass
1572+
15611573
def GetFileEncoding(infile, filestart=0, closefp=True):
15621574
"""
15631575
Detect file/text encoding from BOM (and a few special signatures).
@@ -3087,6 +3099,54 @@ def CompressOpenFile(outfile, compressionenable=True, compressionlevel=None):
30873099

30883100
return outfp
30893101

3102+
def GzipCompressData(data, compresslevel=9):
3103+
try:
3104+
# Try using modern gzip.compress if available
3105+
compressed_data = gzip.compress(data, compresslevel=compresslevel)
3106+
except AttributeError:
3107+
# Fallback to older method for Python 2.x and older 3.x versions
3108+
out = MkTempFile()
3109+
with gzip.GzipFile(filename=None, fileobj=out, mode="wb", compresslevel=compresslevel) as f:
3110+
f.write(data)
3111+
out.seek(0, 0)
3112+
compressed_data = out.read()
3113+
return compressed_data
3114+
3115+
3116+
def GzipDecompressData(compressed_data):
3117+
try:
3118+
# Try using modern gzip.decompress if available
3119+
decompressed_data = gzip.decompress(compressed_data)
3120+
except AttributeError:
3121+
# Fallback to older method for Python 2.x and older 3.x versions
3122+
inp = MkTempFile(compressed_data)
3123+
with gzip.GzipFile(filename=None, fileobj=inp, mode="rb") as f:
3124+
decompressed_data = f.read()
3125+
return decompressed_data
3126+
3127+
3128+
def BzipCompressData(data, compresslevel=9):
3129+
try:
3130+
# Try using modern bz2.compress if available
3131+
compressed_data = bz2.compress(data, compresslevel=compresslevel)
3132+
except AttributeError:
3133+
# Fallback to older method for Python 2.x and older 3.x versions
3134+
compressor = bz2.BZ2Compressor(compresslevel)
3135+
compressed_data = compressor.compress(data)
3136+
compressed_data += compressor.flush()
3137+
return compressed_data
3138+
3139+
3140+
def BzipDecompressData(compressed_data):
3141+
try:
3142+
# Try using modern bz2.decompress if available
3143+
decompressed_data = bz2.decompress(compressed_data)
3144+
except AttributeError:
3145+
# Fallback to older method for Python 2.x and older 3.x versions
3146+
decompressor = bz2.BZ2Decompressor()
3147+
decompressed_data = decompressor.decompress(compressed_data)
3148+
return decompressed_data
3149+
30903150
def GetKeyByFormatExtension(format_extension, formatspecs=__file_format_multi_dict__):
30913151
for key, value in formatspecs.items():
30923152
if value.get('format_extension') == format_extension:
@@ -7737,7 +7797,7 @@ def RePackFoxFile(infile, outfile, fmttype="auto", compression="auto", compressw
77377797
elif isinstance(infile, list):
77387798
listarrayfileslist = infile
77397799
else:
7740-
if (infile != "-" and not isinstance(infile, bytes_type) # bytes is str on Py2
7800+
if (infile != "-" and not isinstance(infile, (bytes, bytearray, memoryview)) # bytes is str on Py2
77417801
and not hasattr(infile, "read") and not hasattr(infile, "write")):
77427802
infile = RemoveWindowsPath(infile)
77437803
listarrayfileslist = FoxFileToArray(
@@ -7758,7 +7818,7 @@ def RePackFoxFile(infile, outfile, fmttype="auto", compression="auto", compressw
77587818
formatspecs = formatspecs.get(fmttype, formatspecs)
77597819

77607820
# ---------- Outfile path normalization (fixed: check outfile, not infile) ----------
7761-
if (outfile != "-" and not isinstance(outfile, bytes_type)
7821+
if (outfile != "-" and not isinstance(outfile, (bytes, bytearray, memoryview))
77627822
and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
77637823
outfile = RemoveWindowsPath(outfile)
77647824

@@ -7776,8 +7836,6 @@ def RePackFoxFile(infile, outfile, fmttype="auto", compression="auto", compressw
77767836
if outfile == "-" or outfile is None:
77777837
verbose = False
77787838
fp = MkTempFile()
7779-
elif(isinstance(outfile, FileLikeAdapter)):
7780-
fp = outfile
77817839
elif hasattr(outfile, "read") or hasattr(outfile, "write"):
77827840
fp = outfile
77837841
elif re.findall(__upload_proto_support__, outfile):

0 commit comments

Comments
 (0)