From 1d8889721e9d659831c6bc6e9af9a3497b76e691 Mon Sep 17 00:00:00 2001 From: jteulade Date: Thu, 7 Mar 2024 16:55:00 +0100 Subject: [PATCH 1/2] Fix: zip can contain directories and files at the root --- sertit/files.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sertit/files.py b/sertit/files.py index 5539393..1169b0f 100644 --- a/sertit/files.py +++ b/sertit/files.py @@ -200,7 +200,8 @@ def extract_file( if file_path.suffix == ".zip": # Manage the case with several directories inside one zipfile arch = zipfile.ZipFile(file_path, "r") - extr_names = list({p.split("/")[0] for p in arch.namelist()}) + # extr_names contains only directory names + extr_names = list({p.split("/")[0] for p in arch.namelist() if "/" in p}) elif file_path.suffix == ".tar" or file_path.suffixes == [".tar", ".gz"]: # Tar files have no subdirectories, so create one extr_names = [path.get_filename(file_path)] From 2c483cff466b0547e1b12b61e008c86e65be4ba4 Mon Sep 17 00:00:00 2001 From: jteulade Date: Mon, 11 Mar 2024 10:42:45 +0100 Subject: [PATCH 2/2] =?UTF-8?q?Add=20detail=20in=20comment=20+=20bugfix=20?= =?UTF-8?q?in=20CHANGES.md=C3=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGES.md | 1 + sertit/files.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 607f4f8..1650585 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,7 @@ ## 1.36.1 (2024-xx-xx) +- FIX: Fix `files.extract_file` when there is a file in the root of the zip archive ([#11](https://github.com/sertit/sertit-utils/pull/11)) - FIX: Fix `geometry.nearest_neighbors` when k is bigger than the number of candidates - DOC: Update some examples in documentation diff --git a/sertit/files.py b/sertit/files.py index 1169b0f..14def2d 100644 --- a/sertit/files.py +++ b/sertit/files.py @@ -200,7 +200,9 @@ def extract_file( if file_path.suffix == ".zip": # Manage the case with several directories inside one zipfile arch = zipfile.ZipFile(file_path, "r") - # extr_names contains only directory names + + # zipfile.namelist returns the relative path of the file names in the archive + # if a file is in the root of the archive, there is no "/", so it should not be included extr_names = list({p.split("/")[0] for p in arch.namelist() if "/" in p}) elif file_path.suffix == ".tar" or file_path.suffixes == [".tar", ".gz"]: # Tar files have no subdirectories, so create one