Thom Smith
Thom Smith

Reputation: 14086

Create zipfile with directory entries in Python

For a unit test, I need a zipfile that contains directory entries. In a pinch, I could create one manually and keep it in the repo in binary form, but I'd rather create it dynamically as part of the test setup.

Neither the zipfile module nor shutil.make_archive seems to be able to create directory entries. Is there a native-python solution?

Upvotes: 2

Views: 147

Answers (1)

ojdo
ojdo

Reputation: 8880

Instinctively I would go with your approach "keep it in the repor in binary form", especially since it would be quite small and guaranteed to be identical across platforms.

For dynamically creating such a ZIP file, you probably need to go the "long way" to first the target structure on disc. For that, I would be using os.makedirs within a tempfile.TemporaryDirectory, then compressing it.

Upvotes: 1

Related Questions