Skip to content

Commit

Permalink
Fix issue with subdirectories and zip compression, closes #609
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmezzetti committed Dec 4, 2023
1 parent c11fc7b commit 57812b7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/python/txtai/archive/zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ def pack(self, path, output):
with ZipFile(output, "w", ZIP_DEFLATED) as zfile:
for root, _, files in sorted(os.walk(path)):
for f in files:
zfile.write(os.path.join(root, f), arcname=f)
# Generate archive name with relative path, if necessary
name = os.path.join(os.path.relpath(root, path), f)

# Write file to zip
zfile.write(os.path.join(root, f), arcname=name)

def unpack(self, path, output):
with ZipFile(path, "r") as zfile:
# Validate path if directory specified
for fullpath in zfile.namelist():
fullpath = os.path.join(path, fullpath)
if os.path.dirname(fullpath) and not self.validate(path, fullpath):
raise IOError(f"Invalid zip entry: {fullpath}")

Expand Down
29 changes: 29 additions & 0 deletions test/python/testarchive.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,35 @@ class TestArchive(unittest.TestCase):
Archive tests.
"""

def testDirectory(self):
"""
Test directory included in compressed files
"""

for extension in ["tar", "zip"]:
# Create archive instance
archive = ArchiveFactory.create()

# Create subdirectory in archive working path
path = os.path.join(archive.path(), "dir")
os.makedirs(path, exist_ok=True)

# Create file in archive working path
with open(os.path.join(path, "test"), "w", encoding="utf-8") as f:
f.write("test")

# Save archive
path = os.path.join(tempfile.gettempdir(), f"subdir.{extension}")
archive.save(path)

# Extract files from archive
archive = ArchiveFactory.create()
archive.load(path)

# Check if file properly extracted
path = os.path.join(archive.path(), "dir", "test")
self.assertTrue(os.path.exists(path))

def testInvalidTar(self):
"""
Test invalid tar file
Expand Down

0 comments on commit 57812b7

Please sign in to comment.