1
0
mirror of https://github.com/ciromattia/kcc synced 2025-12-13 01:36:27 +00:00

Merge pull request #1087 from jtronicus/fix-zip-folders

Fix makeZIP function to allow for special characters in folder names
This commit is contained in:
Alex Xu
2025-09-16 12:48:27 -07:00
committed by GitHub
2 changed files with 14 additions and 13 deletions

View File

@@ -1242,7 +1242,7 @@ def makeZIP(zipfilename, basedir, isepub=False):
mimetypeFile = open(os.path.join(basedir, 'mimetype'), 'w') mimetypeFile = open(os.path.join(basedir, 'mimetype'), 'w')
mimetypeFile.write('application/epub+zip') mimetypeFile.write('application/epub+zip')
mimetypeFile.close() mimetypeFile.close()
subprocess_run([SEVENZIP, 'a', '-tzip', zipfilename, os.path.join(basedir, "*")], capture_output=True, check=True) subprocess_run([SEVENZIP, 'a', '-tzip', zipfilename, "*"], capture_output=True, check=True, cwd=basedir)
else: else:
zipOutput = ZipFile(zipfilename, 'w', ZIP_DEFLATED) zipOutput = ZipFile(zipfilename, 'w', ZIP_DEFLATED)
if isepub: if isepub:

View File

@@ -36,21 +36,22 @@ class ComicArchive:
self.filepath = filepath self.filepath = filepath
if not os.path.isfile(self.filepath): if not os.path.isfile(self.filepath):
raise OSError('File not found.') raise OSError('File not found.')
self.dirname, self.basename = os.path.split(filepath)
@cached_property @cached_property
def type(self): def type(self):
extraction_commands = [ extraction_commands = [
[SEVENZIP, 'l', '-y', '-p1', self.filepath], [SEVENZIP, 'l', '-y', '-p1', self.basename],
] ]
if distro.id() == 'fedora' or distro.like() == 'fedora': if distro.id() == 'fedora' or distro.like() == 'fedora':
extraction_commands.append( extraction_commands.append(
['unrar', 'l', '-y', '-p1', self.filepath], ['unrar', 'l', '-y', '-p1', self.basename],
) )
for cmd in extraction_commands: for cmd in extraction_commands:
try: try:
process = subprocess_run(cmd, capture_output=True, check=True) process = subprocess_run(cmd, capture_output=True, check=True, cwd=self.dirname)
for line in process.stdout.splitlines(): for line in process.stdout.splitlines():
if b'Type =' in line: if b'Type =' in line:
return line.rstrip().decode().split(' = ')[1].upper() return line.rstrip().decode().split(' = ')[1].upper()
@@ -68,25 +69,25 @@ class ComicArchive:
missing = [] missing = []
extraction_commands = [ extraction_commands = [
['tar', '--exclude', '__MACOSX', '--exclude', '.DS_Store', '--exclude', 'thumbs.db', '--exclude', 'Thumbs.db', '-xf', self.filepath, '-C', targetdir], ['tar', '--exclude', '__MACOSX', '--exclude', '.DS_Store', '--exclude', 'thumbs.db', '--exclude', 'Thumbs.db', '-xf', self.basename, '-C', targetdir],
[SEVENZIP, 'x', '-y', '-xr!__MACOSX', '-xr!.DS_Store', '-xr!thumbs.db', '-xr!Thumbs.db', '-o' + targetdir, self.filepath], [SEVENZIP, 'x', '-y', '-xr!__MACOSX', '-xr!.DS_Store', '-xr!thumbs.db', '-xr!Thumbs.db', '-o' + targetdir, self.basename],
] ]
if platform.system() == 'Darwin': if platform.system() == 'Darwin':
extraction_commands.append( extraction_commands.append(
['unar', self.filepath, '-D', '-f', '-o', targetdir] ['unar', self.basename, '-D', '-f', '-o', targetdir]
) )
extraction_commands.reverse() extraction_commands.reverse()
if distro.id() == 'fedora' or distro.like() == 'fedora': if distro.id() == 'fedora' or distro.like() == 'fedora':
extraction_commands.append( extraction_commands.append(
['unrar', 'x', '-y', '-x__MACOSX', '-x.DS_Store', '-xthumbs.db', '-xThumbs.db', self.filepath, targetdir] ['unrar', 'x', '-y', '-x__MACOSX', '-x.DS_Store', '-xthumbs.db', '-xThumbs.db', self.basename, targetdir]
) )
for cmd in extraction_commands: for cmd in extraction_commands:
try: try:
subprocess_run(cmd, capture_output=True, check=True) subprocess_run(cmd, capture_output=True, check=True, cwd=self.dirname)
return targetdir return targetdir
except FileNotFoundError: except FileNotFoundError:
missing.append(cmd[0]) missing.append(cmd[0])
@@ -101,14 +102,14 @@ class ComicArchive:
def addFile(self, sourcefile): def addFile(self, sourcefile):
if self.type in ['RAR', 'RAR5']: if self.type in ['RAR', 'RAR5']:
raise NotImplementedError raise NotImplementedError
process = subprocess_run([SEVENZIP, 'a', '-y', self.filepath, sourcefile], process = subprocess_run([SEVENZIP, 'a', '-y', self.basename, sourcefile],
stdout=PIPE, stderr=STDOUT) stdout=PIPE, stderr=STDOUT, cwd=self.dirname)
if process.returncode != 0: if process.returncode != 0:
raise OSError('Failed to add the file.') raise OSError('Failed to add the file.')
def extractMetadata(self): def extractMetadata(self):
process = subprocess_run([SEVENZIP, 'x', '-y', '-so', self.filepath, 'ComicInfo.xml'], process = subprocess_run([SEVENZIP, 'x', '-y', '-so', self.basename, 'ComicInfo.xml'],
stdout=PIPE, stderr=STDOUT) stdout=PIPE, stderr=STDOUT, cwd=self.dirname)
if process.returncode != 0: if process.returncode != 0:
raise OSError(EXTRACTION_ERROR) raise OSError(EXTRACTION_ERROR)
try: try: