Skip to content

Commit 5d36ca0

Browse files
committed
Fixes the issue with folder inside extracted zip
The current library fails if there are no folders inside the extracted zip file. This fixes the issue
1 parent f5473df commit 5d36ca0

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

src/example.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
ASSETS_DIR = os.path.join(PARENT_DIR, 'tests/assets')
66
VALID_ZIP_FILE = os.path.join(ASSETS_DIR, 'valid.zip')
77
INVALID_ZIP_FILE = os.path.join(ASSETS_DIR, 'invalid.zip')
8+
TEST_ZIP_FILE = os.path.join(ASSETS_DIR,'Archive.zip')
89
SCHEMA_DIR = os.path.join(PARENT_DIR, 'src/python_osw_validation/schema')
910
SCHEMA_FILE_PATH = os.path.join(SCHEMA_DIR, 'opensidewalks.schema.json')
1011

@@ -35,9 +36,16 @@ def invalid_test_without_provided_schema():
3536
result = validator.validate()
3637
print(f'Invalid Test Without Schema: {"Failed" if result.is_valid else "Passed"}')
3738

39+
def test_with_archive_file():
40+
validator = OSWValidation(zipfile_path=TEST_ZIP_FILE)
41+
result = validator.validate()
42+
print(result.errors)
43+
print(result.is_valid)
44+
3845

3946
if __name__ == '__main__':
40-
valid_test_with_provided_schema()
41-
valid_test_without_provided_schema()
42-
invalid_test_with_provided_schema()
43-
invalid_test_without_provided_schema()
47+
# valid_test_with_provided_schema()
48+
# valid_test_without_provided_schema()
49+
# invalid_test_with_provided_schema()
50+
# invalid_test_without_provided_schema()
51+
test_with_archive_file()

src/python_osw_validation/zipfile_handler.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,20 @@ def extract_zip(self) -> Optional[str]:
5050
if len(zip_ref.namelist()) == 0:
5151
raise Exception('ZIP file is empty')
5252

53-
first_item = zip_ref.namelist()[0]
54-
55-
return f'{self.extracted_dir}/{first_item}'
53+
internal_folder_name = self.find_internal_folder(zip_ref)
54+
return os.path.join(self.extracted_dir,internal_folder_name)
5655
except Exception as e:
5756
self.error = f'Error extracting ZIP file: {e}'
57+
58+
# finds the first folder available in the extracted folder.
59+
# returns empty if there are no folders inside
60+
def find_internal_folder(self, zip_ref: zipfile.ZipFile)-> str:
61+
for filename in zip_ref.namelist():
62+
path = os.path.join(self.extracted_dir,filename)
63+
if(os.path.isdir(path)):
64+
return filename
65+
return ''
66+
5867

5968
def remove_extracted_files(self) -> None:
6069
if self.extracted_dir and os.path.exists(self.extracted_dir):

tests/assets/Archive.zip

268 KB
Binary file not shown.

tests/assets/Archive_2.zip

271 KB
Binary file not shown.

0 commit comments

Comments
 (0)