Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3....
File: zipfile.py
"""
[0] Fix | Delete
Read and write ZIP files.
[1] Fix | Delete
[2] Fix | Delete
XXX references to utf-8 need further investigation.
[3] Fix | Delete
"""
[4] Fix | Delete
import io
[5] Fix | Delete
import os
[6] Fix | Delete
import re
[7] Fix | Delete
import importlib.util
[8] Fix | Delete
import sys
[9] Fix | Delete
import time
[10] Fix | Delete
import stat
[11] Fix | Delete
import shutil
[12] Fix | Delete
import struct
[13] Fix | Delete
import binascii
[14] Fix | Delete
[15] Fix | Delete
try:
[16] Fix | Delete
import threading
[17] Fix | Delete
except ImportError:
[18] Fix | Delete
import dummy_threading as threading
[19] Fix | Delete
[20] Fix | Delete
try:
[21] Fix | Delete
import zlib # We may need its compression method
[22] Fix | Delete
crc32 = zlib.crc32
[23] Fix | Delete
except ImportError:
[24] Fix | Delete
zlib = None
[25] Fix | Delete
crc32 = binascii.crc32
[26] Fix | Delete
[27] Fix | Delete
try:
[28] Fix | Delete
import bz2 # We may need its compression method
[29] Fix | Delete
except ImportError:
[30] Fix | Delete
bz2 = None
[31] Fix | Delete
[32] Fix | Delete
try:
[33] Fix | Delete
import lzma # We may need its compression method
[34] Fix | Delete
except ImportError:
[35] Fix | Delete
lzma = None
[36] Fix | Delete
[37] Fix | Delete
__all__ = ["BadZipFile", "BadZipfile", "error",
[38] Fix | Delete
"ZIP_STORED", "ZIP_DEFLATED", "ZIP_BZIP2", "ZIP_LZMA",
[39] Fix | Delete
"is_zipfile", "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile"]
[40] Fix | Delete
[41] Fix | Delete
class BadZipFile(Exception):
[42] Fix | Delete
pass
[43] Fix | Delete
[44] Fix | Delete
[45] Fix | Delete
class LargeZipFile(Exception):
[46] Fix | Delete
"""
[47] Fix | Delete
Raised when writing a zipfile, the zipfile requires ZIP64 extensions
[48] Fix | Delete
and those extensions are disabled.
[49] Fix | Delete
"""
[50] Fix | Delete
[51] Fix | Delete
error = BadZipfile = BadZipFile # Pre-3.2 compatibility names
[52] Fix | Delete
[53] Fix | Delete
[54] Fix | Delete
ZIP64_LIMIT = (1 << 31) - 1
[55] Fix | Delete
ZIP_FILECOUNT_LIMIT = (1 << 16) - 1
[56] Fix | Delete
ZIP_MAX_COMMENT = (1 << 16) - 1
[57] Fix | Delete
[58] Fix | Delete
# constants for Zip file compression methods
[59] Fix | Delete
ZIP_STORED = 0
[60] Fix | Delete
ZIP_DEFLATED = 8
[61] Fix | Delete
ZIP_BZIP2 = 12
[62] Fix | Delete
ZIP_LZMA = 14
[63] Fix | Delete
# Other ZIP compression methods not supported
[64] Fix | Delete
[65] Fix | Delete
DEFAULT_VERSION = 20
[66] Fix | Delete
ZIP64_VERSION = 45
[67] Fix | Delete
BZIP2_VERSION = 46
[68] Fix | Delete
LZMA_VERSION = 63
[69] Fix | Delete
# we recognize (but not necessarily support) all features up to that version
[70] Fix | Delete
MAX_EXTRACT_VERSION = 63
[71] Fix | Delete
[72] Fix | Delete
# Below are some formats and associated data for reading/writing headers using
[73] Fix | Delete
# the struct module. The names and structures of headers/records are those used
[74] Fix | Delete
# in the PKWARE description of the ZIP file format:
[75] Fix | Delete
# http://www.pkware.com/documents/casestudies/APPNOTE.TXT
[76] Fix | Delete
# (URL valid as of January 2008)
[77] Fix | Delete
[78] Fix | Delete
# The "end of central directory" structure, magic number, size, and indices
[79] Fix | Delete
# (section V.I in the format document)
[80] Fix | Delete
structEndArchive = b"<4s4H2LH"
[81] Fix | Delete
stringEndArchive = b"PK\005\006"
[82] Fix | Delete
sizeEndCentDir = struct.calcsize(structEndArchive)
[83] Fix | Delete
[84] Fix | Delete
_ECD_SIGNATURE = 0
[85] Fix | Delete
_ECD_DISK_NUMBER = 1
[86] Fix | Delete
_ECD_DISK_START = 2
[87] Fix | Delete
_ECD_ENTRIES_THIS_DISK = 3
[88] Fix | Delete
_ECD_ENTRIES_TOTAL = 4
[89] Fix | Delete
_ECD_SIZE = 5
[90] Fix | Delete
_ECD_OFFSET = 6
[91] Fix | Delete
_ECD_COMMENT_SIZE = 7
[92] Fix | Delete
# These last two indices are not part of the structure as defined in the
[93] Fix | Delete
# spec, but they are used internally by this module as a convenience
[94] Fix | Delete
_ECD_COMMENT = 8
[95] Fix | Delete
_ECD_LOCATION = 9
[96] Fix | Delete
[97] Fix | Delete
# The "central directory" structure, magic number, size, and indices
[98] Fix | Delete
# of entries in the structure (section V.F in the format document)
[99] Fix | Delete
structCentralDir = "<4s4B4HL2L5H2L"
[100] Fix | Delete
stringCentralDir = b"PK\001\002"
[101] Fix | Delete
sizeCentralDir = struct.calcsize(structCentralDir)
[102] Fix | Delete
[103] Fix | Delete
# indexes of entries in the central directory structure
[104] Fix | Delete
_CD_SIGNATURE = 0
[105] Fix | Delete
_CD_CREATE_VERSION = 1
[106] Fix | Delete
_CD_CREATE_SYSTEM = 2
[107] Fix | Delete
_CD_EXTRACT_VERSION = 3
[108] Fix | Delete
_CD_EXTRACT_SYSTEM = 4
[109] Fix | Delete
_CD_FLAG_BITS = 5
[110] Fix | Delete
_CD_COMPRESS_TYPE = 6
[111] Fix | Delete
_CD_TIME = 7
[112] Fix | Delete
_CD_DATE = 8
[113] Fix | Delete
_CD_CRC = 9
[114] Fix | Delete
_CD_COMPRESSED_SIZE = 10
[115] Fix | Delete
_CD_UNCOMPRESSED_SIZE = 11
[116] Fix | Delete
_CD_FILENAME_LENGTH = 12
[117] Fix | Delete
_CD_EXTRA_FIELD_LENGTH = 13
[118] Fix | Delete
_CD_COMMENT_LENGTH = 14
[119] Fix | Delete
_CD_DISK_NUMBER_START = 15
[120] Fix | Delete
_CD_INTERNAL_FILE_ATTRIBUTES = 16
[121] Fix | Delete
_CD_EXTERNAL_FILE_ATTRIBUTES = 17
[122] Fix | Delete
_CD_LOCAL_HEADER_OFFSET = 18
[123] Fix | Delete
[124] Fix | Delete
# The "local file header" structure, magic number, size, and indices
[125] Fix | Delete
# (section V.A in the format document)
[126] Fix | Delete
structFileHeader = "<4s2B4HL2L2H"
[127] Fix | Delete
stringFileHeader = b"PK\003\004"
[128] Fix | Delete
sizeFileHeader = struct.calcsize(structFileHeader)
[129] Fix | Delete
[130] Fix | Delete
_FH_SIGNATURE = 0
[131] Fix | Delete
_FH_EXTRACT_VERSION = 1
[132] Fix | Delete
_FH_EXTRACT_SYSTEM = 2
[133] Fix | Delete
_FH_GENERAL_PURPOSE_FLAG_BITS = 3
[134] Fix | Delete
_FH_COMPRESSION_METHOD = 4
[135] Fix | Delete
_FH_LAST_MOD_TIME = 5
[136] Fix | Delete
_FH_LAST_MOD_DATE = 6
[137] Fix | Delete
_FH_CRC = 7
[138] Fix | Delete
_FH_COMPRESSED_SIZE = 8
[139] Fix | Delete
_FH_UNCOMPRESSED_SIZE = 9
[140] Fix | Delete
_FH_FILENAME_LENGTH = 10
[141] Fix | Delete
_FH_EXTRA_FIELD_LENGTH = 11
[142] Fix | Delete
[143] Fix | Delete
# The "Zip64 end of central directory locator" structure, magic number, and size
[144] Fix | Delete
structEndArchive64Locator = "<4sLQL"
[145] Fix | Delete
stringEndArchive64Locator = b"PK\x06\x07"
[146] Fix | Delete
sizeEndCentDir64Locator = struct.calcsize(structEndArchive64Locator)
[147] Fix | Delete
[148] Fix | Delete
# The "Zip64 end of central directory" record, magic number, size, and indices
[149] Fix | Delete
# (section V.G in the format document)
[150] Fix | Delete
structEndArchive64 = "<4sQ2H2L4Q"
[151] Fix | Delete
stringEndArchive64 = b"PK\x06\x06"
[152] Fix | Delete
sizeEndCentDir64 = struct.calcsize(structEndArchive64)
[153] Fix | Delete
[154] Fix | Delete
_CD64_SIGNATURE = 0
[155] Fix | Delete
_CD64_DIRECTORY_RECSIZE = 1
[156] Fix | Delete
_CD64_CREATE_VERSION = 2
[157] Fix | Delete
_CD64_EXTRACT_VERSION = 3
[158] Fix | Delete
_CD64_DISK_NUMBER = 4
[159] Fix | Delete
_CD64_DISK_NUMBER_START = 5
[160] Fix | Delete
_CD64_NUMBER_ENTRIES_THIS_DISK = 6
[161] Fix | Delete
_CD64_NUMBER_ENTRIES_TOTAL = 7
[162] Fix | Delete
_CD64_DIRECTORY_SIZE = 8
[163] Fix | Delete
_CD64_OFFSET_START_CENTDIR = 9
[164] Fix | Delete
[165] Fix | Delete
_DD_SIGNATURE = 0x08074b50
[166] Fix | Delete
[167] Fix | Delete
_EXTRA_FIELD_STRUCT = struct.Struct('<HH')
[168] Fix | Delete
[169] Fix | Delete
def _strip_extra(extra, xids):
[170] Fix | Delete
# Remove Extra Fields with specified IDs.
[171] Fix | Delete
unpack = _EXTRA_FIELD_STRUCT.unpack
[172] Fix | Delete
modified = False
[173] Fix | Delete
buffer = []
[174] Fix | Delete
start = i = 0
[175] Fix | Delete
while i + 4 <= len(extra):
[176] Fix | Delete
xid, xlen = unpack(extra[i : i + 4])
[177] Fix | Delete
j = i + 4 + xlen
[178] Fix | Delete
if xid in xids:
[179] Fix | Delete
if i != start:
[180] Fix | Delete
buffer.append(extra[start : i])
[181] Fix | Delete
start = j
[182] Fix | Delete
modified = True
[183] Fix | Delete
i = j
[184] Fix | Delete
if not modified:
[185] Fix | Delete
return extra
[186] Fix | Delete
return b''.join(buffer)
[187] Fix | Delete
[188] Fix | Delete
def _check_zipfile(fp):
[189] Fix | Delete
try:
[190] Fix | Delete
if _EndRecData(fp):
[191] Fix | Delete
return True # file has correct magic number
[192] Fix | Delete
except OSError:
[193] Fix | Delete
pass
[194] Fix | Delete
return False
[195] Fix | Delete
[196] Fix | Delete
def is_zipfile(filename):
[197] Fix | Delete
"""Quickly see if a file is a ZIP file by checking the magic number.
[198] Fix | Delete
[199] Fix | Delete
The filename argument may be a file or file-like object too.
[200] Fix | Delete
"""
[201] Fix | Delete
result = False
[202] Fix | Delete
try:
[203] Fix | Delete
if hasattr(filename, "read"):
[204] Fix | Delete
result = _check_zipfile(fp=filename)
[205] Fix | Delete
else:
[206] Fix | Delete
with open(filename, "rb") as fp:
[207] Fix | Delete
result = _check_zipfile(fp)
[208] Fix | Delete
except OSError:
[209] Fix | Delete
pass
[210] Fix | Delete
return result
[211] Fix | Delete
[212] Fix | Delete
def _EndRecData64(fpin, offset, endrec):
[213] Fix | Delete
"""
[214] Fix | Delete
Read the ZIP64 end-of-archive records and use that to update endrec
[215] Fix | Delete
"""
[216] Fix | Delete
try:
[217] Fix | Delete
fpin.seek(offset - sizeEndCentDir64Locator, 2)
[218] Fix | Delete
except OSError:
[219] Fix | Delete
# If the seek fails, the file is not large enough to contain a ZIP64
[220] Fix | Delete
# end-of-archive record, so just return the end record we were given.
[221] Fix | Delete
return endrec
[222] Fix | Delete
[223] Fix | Delete
data = fpin.read(sizeEndCentDir64Locator)
[224] Fix | Delete
if len(data) != sizeEndCentDir64Locator:
[225] Fix | Delete
return endrec
[226] Fix | Delete
sig, diskno, reloff, disks = struct.unpack(structEndArchive64Locator, data)
[227] Fix | Delete
if sig != stringEndArchive64Locator:
[228] Fix | Delete
return endrec
[229] Fix | Delete
[230] Fix | Delete
if diskno != 0 or disks != 1:
[231] Fix | Delete
raise BadZipFile("zipfiles that span multiple disks are not supported")
[232] Fix | Delete
[233] Fix | Delete
# Assume no 'zip64 extensible data'
[234] Fix | Delete
fpin.seek(offset - sizeEndCentDir64Locator - sizeEndCentDir64, 2)
[235] Fix | Delete
data = fpin.read(sizeEndCentDir64)
[236] Fix | Delete
if len(data) != sizeEndCentDir64:
[237] Fix | Delete
return endrec
[238] Fix | Delete
sig, sz, create_version, read_version, disk_num, disk_dir, \
[239] Fix | Delete
dircount, dircount2, dirsize, diroffset = \
[240] Fix | Delete
struct.unpack(structEndArchive64, data)
[241] Fix | Delete
if sig != stringEndArchive64:
[242] Fix | Delete
return endrec
[243] Fix | Delete
[244] Fix | Delete
# Update the original endrec using data from the ZIP64 record
[245] Fix | Delete
endrec[_ECD_SIGNATURE] = sig
[246] Fix | Delete
endrec[_ECD_DISK_NUMBER] = disk_num
[247] Fix | Delete
endrec[_ECD_DISK_START] = disk_dir
[248] Fix | Delete
endrec[_ECD_ENTRIES_THIS_DISK] = dircount
[249] Fix | Delete
endrec[_ECD_ENTRIES_TOTAL] = dircount2
[250] Fix | Delete
endrec[_ECD_SIZE] = dirsize
[251] Fix | Delete
endrec[_ECD_OFFSET] = diroffset
[252] Fix | Delete
return endrec
[253] Fix | Delete
[254] Fix | Delete
[255] Fix | Delete
def _EndRecData(fpin):
[256] Fix | Delete
"""Return data from the "End of Central Directory" record, or None.
[257] Fix | Delete
[258] Fix | Delete
The data is a list of the nine items in the ZIP "End of central dir"
[259] Fix | Delete
record followed by a tenth item, the file seek offset of this record."""
[260] Fix | Delete
[261] Fix | Delete
# Determine file size
[262] Fix | Delete
fpin.seek(0, 2)
[263] Fix | Delete
filesize = fpin.tell()
[264] Fix | Delete
[265] Fix | Delete
# Check to see if this is ZIP file with no archive comment (the
[266] Fix | Delete
# "end of central directory" structure should be the last item in the
[267] Fix | Delete
# file if this is the case).
[268] Fix | Delete
try:
[269] Fix | Delete
fpin.seek(-sizeEndCentDir, 2)
[270] Fix | Delete
except OSError:
[271] Fix | Delete
return None
[272] Fix | Delete
data = fpin.read()
[273] Fix | Delete
if (len(data) == sizeEndCentDir and
[274] Fix | Delete
data[0:4] == stringEndArchive and
[275] Fix | Delete
data[-2:] == b"\000\000"):
[276] Fix | Delete
# the signature is correct and there's no comment, unpack structure
[277] Fix | Delete
endrec = struct.unpack(structEndArchive, data)
[278] Fix | Delete
endrec=list(endrec)
[279] Fix | Delete
[280] Fix | Delete
# Append a blank comment and record start offset
[281] Fix | Delete
endrec.append(b"")
[282] Fix | Delete
endrec.append(filesize - sizeEndCentDir)
[283] Fix | Delete
[284] Fix | Delete
# Try to read the "Zip64 end of central directory" structure
[285] Fix | Delete
return _EndRecData64(fpin, -sizeEndCentDir, endrec)
[286] Fix | Delete
[287] Fix | Delete
# Either this is not a ZIP file, or it is a ZIP file with an archive
[288] Fix | Delete
# comment. Search the end of the file for the "end of central directory"
[289] Fix | Delete
# record signature. The comment is the last item in the ZIP file and may be
[290] Fix | Delete
# up to 64K long. It is assumed that the "end of central directory" magic
[291] Fix | Delete
# number does not appear in the comment.
[292] Fix | Delete
maxCommentStart = max(filesize - (1 << 16) - sizeEndCentDir, 0)
[293] Fix | Delete
fpin.seek(maxCommentStart, 0)
[294] Fix | Delete
data = fpin.read()
[295] Fix | Delete
start = data.rfind(stringEndArchive)
[296] Fix | Delete
if start >= 0:
[297] Fix | Delete
# found the magic number; attempt to unpack and interpret
[298] Fix | Delete
recData = data[start:start+sizeEndCentDir]
[299] Fix | Delete
if len(recData) != sizeEndCentDir:
[300] Fix | Delete
# Zip file is corrupted.
[301] Fix | Delete
return None
[302] Fix | Delete
endrec = list(struct.unpack(structEndArchive, recData))
[303] Fix | Delete
commentSize = endrec[_ECD_COMMENT_SIZE] #as claimed by the zip file
[304] Fix | Delete
comment = data[start+sizeEndCentDir:start+sizeEndCentDir+commentSize]
[305] Fix | Delete
endrec.append(comment)
[306] Fix | Delete
endrec.append(maxCommentStart + start)
[307] Fix | Delete
[308] Fix | Delete
# Try to read the "Zip64 end of central directory" structure
[309] Fix | Delete
return _EndRecData64(fpin, maxCommentStart + start - filesize,
[310] Fix | Delete
endrec)
[311] Fix | Delete
[312] Fix | Delete
# Unable to find a valid end of central directory structure
[313] Fix | Delete
return None
[314] Fix | Delete
[315] Fix | Delete
[316] Fix | Delete
class ZipInfo (object):
[317] Fix | Delete
"""Class with attributes describing each file in the ZIP archive."""
[318] Fix | Delete
[319] Fix | Delete
__slots__ = (
[320] Fix | Delete
'orig_filename',
[321] Fix | Delete
'filename',
[322] Fix | Delete
'date_time',
[323] Fix | Delete
'compress_type',
[324] Fix | Delete
'comment',
[325] Fix | Delete
'extra',
[326] Fix | Delete
'create_system',
[327] Fix | Delete
'create_version',
[328] Fix | Delete
'extract_version',
[329] Fix | Delete
'reserved',
[330] Fix | Delete
'flag_bits',
[331] Fix | Delete
'volume',
[332] Fix | Delete
'internal_attr',
[333] Fix | Delete
'external_attr',
[334] Fix | Delete
'header_offset',
[335] Fix | Delete
'CRC',
[336] Fix | Delete
'compress_size',
[337] Fix | Delete
'file_size',
[338] Fix | Delete
'_raw_time',
[339] Fix | Delete
'_end_offset',
[340] Fix | Delete
)
[341] Fix | Delete
[342] Fix | Delete
def __init__(self, filename="NoName", date_time=(1980,1,1,0,0,0)):
[343] Fix | Delete
self.orig_filename = filename # Original file name in archive
[344] Fix | Delete
[345] Fix | Delete
# Terminate the file name at the first null byte. Null bytes in file
[346] Fix | Delete
# names are used as tricks by viruses in archives.
[347] Fix | Delete
null_byte = filename.find(chr(0))
[348] Fix | Delete
if null_byte >= 0:
[349] Fix | Delete
filename = filename[0:null_byte]
[350] Fix | Delete
# This is used to ensure paths in generated ZIP files always use
[351] Fix | Delete
# forward slashes as the directory separator, as required by the
[352] Fix | Delete
# ZIP format specification.
[353] Fix | Delete
if os.sep != "/" and os.sep in filename:
[354] Fix | Delete
filename = filename.replace(os.sep, "/")
[355] Fix | Delete
[356] Fix | Delete
self.filename = filename # Normalized file name
[357] Fix | Delete
self.date_time = date_time # year, month, day, hour, min, sec
[358] Fix | Delete
[359] Fix | Delete
if date_time[0] < 1980:
[360] Fix | Delete
raise ValueError('ZIP does not support timestamps before 1980')
[361] Fix | Delete
[362] Fix | Delete
# Standard values:
[363] Fix | Delete
self.compress_type = ZIP_STORED # Type of compression for the file
[364] Fix | Delete
self.comment = b"" # Comment for each file
[365] Fix | Delete
self.extra = b"" # ZIP extra data
[366] Fix | Delete
if sys.platform == 'win32':
[367] Fix | Delete
self.create_system = 0 # System which created ZIP archive
[368] Fix | Delete
else:
[369] Fix | Delete
# Assume everything else is unix-y
[370] Fix | Delete
self.create_system = 3 # System which created ZIP archive
[371] Fix | Delete
self.create_version = DEFAULT_VERSION # Version which created ZIP archive
[372] Fix | Delete
self.extract_version = DEFAULT_VERSION # Version needed to extract archive
[373] Fix | Delete
self.reserved = 0 # Must be zero
[374] Fix | Delete
self.flag_bits = 0 # ZIP flag bits
[375] Fix | Delete
self.volume = 0 # Volume number of file header
[376] Fix | Delete
self.internal_attr = 0 # Internal attributes
[377] Fix | Delete
self.external_attr = 0 # External file attributes
[378] Fix | Delete
self._end_offset = None # Start of the next local header or central directory
[379] Fix | Delete
# Other attributes are set by class ZipFile:
[380] Fix | Delete
# header_offset Byte offset to the file header
[381] Fix | Delete
# CRC CRC-32 of the uncompressed file
[382] Fix | Delete
# compress_size Size of the compressed file
[383] Fix | Delete
# file_size Size of the uncompressed file
[384] Fix | Delete
[385] Fix | Delete
def __repr__(self):
[386] Fix | Delete
result = ['<%s filename=%r' % (self.__class__.__name__, self.filename)]
[387] Fix | Delete
if self.compress_type != ZIP_STORED:
[388] Fix | Delete
result.append(' compress_type=%s' %
[389] Fix | Delete
compressor_names.get(self.compress_type,
[390] Fix | Delete
self.compress_type))
[391] Fix | Delete
hi = self.external_attr >> 16
[392] Fix | Delete
lo = self.external_attr & 0xFFFF
[393] Fix | Delete
if hi:
[394] Fix | Delete
result.append(' filemode=%r' % stat.filemode(hi))
[395] Fix | Delete
if lo:
[396] Fix | Delete
result.append(' external_attr=%#x' % lo)
[397] Fix | Delete
isdir = self.is_dir()
[398] Fix | Delete
if not isdir or self.file_size:
[399] Fix | Delete
result.append(' file_size=%r' % self.file_size)
[400] Fix | Delete
if ((not isdir or self.compress_size) and
[401] Fix | Delete
(self.compress_type != ZIP_STORED or
[402] Fix | Delete
self.file_size != self.compress_size)):
[403] Fix | Delete
result.append(' compress_size=%r' % self.compress_size)
[404] Fix | Delete
result.append('>')
[405] Fix | Delete
return ''.join(result)
[406] Fix | Delete
[407] Fix | Delete
def FileHeader(self, zip64=None):
[408] Fix | Delete
"""Return the per-file header as a bytes object."""
[409] Fix | Delete
dt = self.date_time
[410] Fix | Delete
dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]
[411] Fix | Delete
dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)
[412] Fix | Delete
if self.flag_bits & 0x08:
[413] Fix | Delete
# Set these to zero because we write them after the file data
[414] Fix | Delete
CRC = compress_size = file_size = 0
[415] Fix | Delete
else:
[416] Fix | Delete
CRC = self.CRC
[417] Fix | Delete
compress_size = self.compress_size
[418] Fix | Delete
file_size = self.file_size
[419] Fix | Delete
[420] Fix | Delete
extra = self.extra
[421] Fix | Delete
[422] Fix | Delete
min_version = 0
[423] Fix | Delete
if zip64 is None:
[424] Fix | Delete
zip64 = file_size > ZIP64_LIMIT or compress_size > ZIP64_LIMIT
[425] Fix | Delete
if zip64:
[426] Fix | Delete
fmt = '<HHQQ'
[427] Fix | Delete
extra = extra + struct.pack(fmt,
[428] Fix | Delete
1, struct.calcsize(fmt)-4, file_size, compress_size)
[429] Fix | Delete
if file_size > ZIP64_LIMIT or compress_size > ZIP64_LIMIT:
[430] Fix | Delete
if not zip64:
[431] Fix | Delete
raise LargeZipFile("Filesize would require ZIP64 extensions")
[432] Fix | Delete
# File is larger than what fits into a 4 byte integer,
[433] Fix | Delete
# fall back to the ZIP64 extension
[434] Fix | Delete
file_size = 0xffffffff
[435] Fix | Delete
compress_size = 0xffffffff
[436] Fix | Delete
min_version = ZIP64_VERSION
[437] Fix | Delete
[438] Fix | Delete
if self.compress_type == ZIP_BZIP2:
[439] Fix | Delete
min_version = max(BZIP2_VERSION, min_version)
[440] Fix | Delete
elif self.compress_type == ZIP_LZMA:
[441] Fix | Delete
min_version = max(LZMA_VERSION, min_version)
[442] Fix | Delete
[443] Fix | Delete
self.extract_version = max(min_version, self.extract_version)
[444] Fix | Delete
self.create_version = max(min_version, self.create_version)
[445] Fix | Delete
filename, flag_bits = self._encodeFilenameFlags()
[446] Fix | Delete
header = struct.pack(structFileHeader, stringFileHeader,
[447] Fix | Delete
self.extract_version, self.reserved, flag_bits,
[448] Fix | Delete
self.compress_type, dostime, dosdate, CRC,
[449] Fix | Delete
compress_size, file_size,
[450] Fix | Delete
len(filename), len(extra))
[451] Fix | Delete
return header + filename + extra
[452] Fix | Delete
[453] Fix | Delete
def _encodeFilenameFlags(self):
[454] Fix | Delete
try:
[455] Fix | Delete
return self.filename.encode('ascii'), self.flag_bits
[456] Fix | Delete
except UnicodeEncodeError:
[457] Fix | Delete
return self.filename.encode('utf-8'), self.flag_bits | 0x800
[458] Fix | Delete
[459] Fix | Delete
def _decodeExtra(self):
[460] Fix | Delete
# Try to decode the extra field.
[461] Fix | Delete
extra = self.extra
[462] Fix | Delete
unpack = struct.unpack
[463] Fix | Delete
while len(extra) >= 4:
[464] Fix | Delete
tp, ln = unpack('<HH', extra[:4])
[465] Fix | Delete
if tp == 1:
[466] Fix | Delete
if ln >= 24:
[467] Fix | Delete
counts = unpack('<QQQ', extra[4:28])
[468] Fix | Delete
elif ln == 16:
[469] Fix | Delete
counts = unpack('<QQ', extra[4:20])
[470] Fix | Delete
elif ln == 8:
[471] Fix | Delete
counts = unpack('<Q', extra[4:12])
[472] Fix | Delete
elif ln == 0:
[473] Fix | Delete
counts = ()
[474] Fix | Delete
else:
[475] Fix | Delete
raise BadZipFile("Corrupt extra field %04x (size=%d)" % (tp, ln))
[476] Fix | Delete
[477] Fix | Delete
idx = 0
[478] Fix | Delete
[479] Fix | Delete
# ZIP64 extension (large files and/or large archives)
[480] Fix | Delete
if self.file_size in (0xffffffffffffffff, 0xffffffff):
[481] Fix | Delete
self.file_size = counts[idx]
[482] Fix | Delete
idx += 1
[483] Fix | Delete
[484] Fix | Delete
if self.compress_size == 0xFFFFFFFF:
[485] Fix | Delete
self.compress_size = counts[idx]
[486] Fix | Delete
idx += 1
[487] Fix | Delete
[488] Fix | Delete
if self.header_offset == 0xffffffff:
[489] Fix | Delete
old = self.header_offset
[490] Fix | Delete
self.header_offset = counts[idx]
[491] Fix | Delete
idx+=1
[492] Fix | Delete
[493] Fix | Delete
extra = extra[ln+4:]
[494] Fix | Delete
[495] Fix | Delete
@classmethod
[496] Fix | Delete
def from_file(cls, filename, arcname=None):
[497] Fix | Delete
"""Construct an appropriate ZipInfo for a file on the filesystem.
[498] Fix | Delete
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function