Skip to content

Commit 9ee91cf

Browse files
committed
Small update
1 parent d257243 commit 9ee91cf

File tree

1 file changed

+63
-5
lines changed

1 file changed

+63
-5
lines changed

pyneofile/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
@@ -1551,6 +1552,17 @@ def CheckCompressionSubType(infile, formatspecs=__file_format_multi_dict__, file
15511552
fp.close()
15521553
return filetype
15531554

1555+
def _advance(fp, base, n):
1556+
"""
1557+
Move file position to right after the BOM/signature.
1558+
If fp is not seekable, this silently does nothing.
1559+
"""
1560+
try:
1561+
fp.seek(base + n, 0)
1562+
except Exception:
1563+
# Not seekable or error; ignore
1564+
pass
1565+
15541566
def GetFileEncoding(infile, filestart=0, closefp=True):
15551567
"""
15561568
Detect file/text encoding from BOM (and a few special signatures).
@@ -3080,6 +3092,54 @@ def CompressOpenFile(outfile, compressionenable=True, compressionlevel=None):
30803092

30813093
return outfp
30823094

3095+
def GzipCompressData(data, compresslevel=9):
3096+
try:
3097+
# Try using modern gzip.compress if available
3098+
compressed_data = gzip.compress(data, compresslevel=compresslevel)
3099+
except AttributeError:
3100+
# Fallback to older method for Python 2.x and older 3.x versions
3101+
out = MkTempFile()
3102+
with gzip.GzipFile(filename=None, fileobj=out, mode="wb", compresslevel=compresslevel) as f:
3103+
f.write(data)
3104+
out.seek(0, 0)
3105+
compressed_data = out.read()
3106+
return compressed_data
3107+
3108+
3109+
def GzipDecompressData(compressed_data):
3110+
try:
3111+
# Try using modern gzip.decompress if available
3112+
decompressed_data = gzip.decompress(compressed_data)
3113+
except AttributeError:
3114+
# Fallback to older method for Python 2.x and older 3.x versions
3115+
inp = MkTempFile(compressed_data)
3116+
with gzip.GzipFile(filename=None, fileobj=inp, mode="rb") as f:
3117+
decompressed_data = f.read()
3118+
return decompressed_data
3119+
3120+
3121+
def BzipCompressData(data, compresslevel=9):
3122+
try:
3123+
# Try using modern bz2.compress if available
3124+
compressed_data = bz2.compress(data, compresslevel=compresslevel)
3125+
except AttributeError:
3126+
# Fallback to older method for Python 2.x and older 3.x versions
3127+
compressor = bz2.BZ2Compressor(compresslevel)
3128+
compressed_data = compressor.compress(data)
3129+
compressed_data += compressor.flush()
3130+
return compressed_data
3131+
3132+
3133+
def BzipDecompressData(compressed_data):
3134+
try:
3135+
# Try using modern bz2.decompress if available
3136+
decompressed_data = bz2.decompress(compressed_data)
3137+
except AttributeError:
3138+
# Fallback to older method for Python 2.x and older 3.x versions
3139+
decompressor = bz2.BZ2Decompressor()
3140+
decompressed_data = decompressor.decompress(compressed_data)
3141+
return decompressed_data
3142+
30833143
def GetKeyByFormatExtension(format_extension, formatspecs=__file_format_multi_dict__):
30843144
for key, value in formatspecs.items():
30853145
if value.get('format_extension') == format_extension:
@@ -7730,7 +7790,7 @@ def RePackNeoFile(infile, outfile, fmttype="auto", compression="auto", compressw
77307790
elif isinstance(infile, list):
77317791
listarrayfileslist = infile
77327792
else:
7733-
if (infile != "-" and not isinstance(infile, bytes_type) # bytes is str on Py2
7793+
if (infile != "-" and not isinstance(infile, (bytes, bytearray, memoryview)) # bytes is str on Py2
77347794
and not hasattr(infile, "read") and not hasattr(infile, "write")):
77357795
infile = RemoveWindowsPath(infile)
77367796
listarrayfileslist = NeoFileToArray(
@@ -7751,7 +7811,7 @@ def RePackNeoFile(infile, outfile, fmttype="auto", compression="auto", compressw
77517811
formatspecs = formatspecs.get(fmttype, formatspecs)
77527812

77537813
# ---------- Outfile path normalization (fixed: check outfile, not infile) ----------
7754-
if (outfile != "-" and not isinstance(outfile, bytes_type)
7814+
if (outfile != "-" and not isinstance(outfile, (bytes, bytearray, memoryview))
77557815
and not hasattr(outfile, "read") and not hasattr(outfile, "write")):
77567816
outfile = RemoveWindowsPath(outfile)
77577817

@@ -7769,8 +7829,6 @@ def RePackNeoFile(infile, outfile, fmttype="auto", compression="auto", compressw
77697829
if outfile == "-" or outfile is None:
77707830
verbose = False
77717831
fp = MkTempFile()
7772-
elif(isinstance(outfile, FileLikeAdapter)):
7773-
fp = outfile
77747832
elif hasattr(outfile, "read") or hasattr(outfile, "write"):
77757833
fp = outfile
77767834
elif re.findall(__upload_proto_support__, outfile):

0 commit comments

Comments
 (0)