mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
...
This commit is contained in:
parent
90bf890e10
commit
ea0ee8d3d7
@ -143,7 +143,7 @@ def decode_arcname(name):
|
|||||||
name = name.decode(encoding)
|
name = name.decode(encoding)
|
||||||
except:
|
except:
|
||||||
name = name.decode('utf-8', 'replace')
|
name = name.decode('utf-8', 'replace')
|
||||||
return sanitize_file_name(name.encode(filesystem_encoding, 'replace'))
|
return name.encode(filesystem_encoding, 'replace')
|
||||||
|
|
||||||
|
|
||||||
def is_zipfile(filename):
|
def is_zipfile(filename):
|
||||||
@ -690,6 +690,7 @@ class ZipFile:
|
|||||||
self.debug = 0 # Level of printing: 0 through 3
|
self.debug = 0 # Level of printing: 0 through 3
|
||||||
self.NameToInfo = {} # Find file info given name
|
self.NameToInfo = {} # Find file info given name
|
||||||
self.filelist = [] # List of ZipInfo instances for archive
|
self.filelist = [] # List of ZipInfo instances for archive
|
||||||
|
self.extract_mapping = {}
|
||||||
self.compression = compression # Method of compression
|
self.compression = compression # Method of compression
|
||||||
self.mode = key = mode.replace('b', '')[0]
|
self.mode = key = mode.replace('b', '')[0]
|
||||||
self.pwd = None
|
self.pwd = None
|
||||||
@ -1054,9 +1055,14 @@ class ZipFile:
|
|||||||
|
|
||||||
if not os.path.exists(targetpath): # Could be a previously automatically created directory
|
if not os.path.exists(targetpath): # Could be a previously automatically created directory
|
||||||
with closing(self.open(member, pwd=pwd)) as source:
|
with closing(self.open(member, pwd=pwd)) as source:
|
||||||
|
try:
|
||||||
with open(targetpath, 'wb') as target:
|
with open(targetpath, 'wb') as target:
|
||||||
shutil.copyfileobj(source, target)
|
shutil.copyfileobj(source, target)
|
||||||
|
except:
|
||||||
|
targetpath = sanitize_file_name(targetpath)
|
||||||
|
with open(targetpath, 'wb') as target:
|
||||||
|
shutil.copyfileobj(source, target)
|
||||||
|
self.extract_mapping[member.filename] = targetpath
|
||||||
return targetpath
|
return targetpath
|
||||||
|
|
||||||
def _writecheck(self, zinfo):
|
def _writecheck(self, zinfo):
|
||||||
@ -1337,14 +1343,14 @@ def safe_replace(zipstream, name, datastream):
|
|||||||
names = z.infolist()
|
names = z.infolist()
|
||||||
with TemporaryDirectory('_zipfile_replace') as tdir:
|
with TemporaryDirectory('_zipfile_replace') as tdir:
|
||||||
z.extractall(path=tdir)
|
z.extractall(path=tdir)
|
||||||
|
mapping = z.extract_mapping
|
||||||
path = os.path.join(tdir, *name.split('/'))
|
path = os.path.join(tdir, *name.split('/'))
|
||||||
shutil.copyfileobj(datastream, open(path, 'wb'))
|
shutil.copyfileobj(datastream, open(path, 'wb'))
|
||||||
zipstream.seek(0)
|
zipstream.seek(0)
|
||||||
zipstream.truncate()
|
zipstream.truncate()
|
||||||
with closing(ZipFile(zipstream, 'w')) as z:
|
with closing(ZipFile(zipstream, 'w')) as z:
|
||||||
for info in names:
|
for info in names:
|
||||||
fname = decode_arcname(info.filename)
|
current = mapping[info.filename]
|
||||||
current = os.path.join(tdir, *fname.split('/'))
|
|
||||||
if os.path.isdir(current):
|
if os.path.isdir(current):
|
||||||
z.writestr(info.filename+'/', '', 0700)
|
z.writestr(info.filename+'/', '', 0700)
|
||||||
else:
|
else:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user