Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix filename decoding issue for zip files archived by macOS #75

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 27 additions & 2 deletions jupyter_archive/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,42 @@ async def get(self, archive_path, include_body=False):
self.finish()

def extract_archive(self, archive_path):
def try_macos_decode(fn):
try:
return fn.encode('cp437').decode('utf-8')
except UnicodeError:
return None

def try_windows_chinese_decode(fn):
try:
return fn.encode('cp437').decode('gbk')
except UnicodeError:
return None

def unzip(reader, destination):
# most of Windows implementations use DOS (OEM) encoding
# Mac OS zip utility uses utf-8, but it doesn't set utf-8 bit flags
# *nix zip utilities silently uses system encoding(utf-8 generally)
with reader as f:
for fn in f.namelist():
extreact_path = pathlib.Path(f.extract(fn, path=destination))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix wording

Suggested change
extreact_path = pathlib.Path(f.extract(fn, path=destination))
extracted_path = pathlib.Path(f.extract(fn, path=destination))

correct_filename = try_macos_decode(fn) or try_windows_chinese_decode(fn) or fn
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change this a bit to use a single function that has the following structure:

def decode_filename(fn):
    try:
        encoded_filename = fn.encode('cp437')
        try:
            # MacOS encoding
            return encoded_filename.decode('utf-8')
        except UnicodeError:
            # Windows encoding
            return encoded_filename.decode('gbk')
    except UnicodeError:
        return fn

extreact_path.rename(os.path.join(destination, correct_filename))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

destination is a Path, so you can write:

Suggested change
extreact_path.rename(os.path.join(destination, correct_filename))
extracted_path.rename(destination / correct_filename)


archive_destination = archive_path.parent
self.log.info("Begin extraction of {} to {}.".format(archive_path, archive_destination))

archive_reader = make_reader(archive_path)
with archive_reader as archive:
archive.extractall(archive_destination)
if isinstance(archive_reader, zipfile.ZipFile):
unzip(archive_reader, archive_destination)
else:
with archive_reader as archive:
archive.extractall(archive_destination)

self.log.info("Finished extracting {} to {}.".format(archive_path, archive_destination))



Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

def setup_handlers(web_app):
host_pattern = ".*$"
base_url = web_app.settings["base_url"]
Expand Down