Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 846
Thom Smith Avatar asked Apr 13 '26 10:04

Thom Smith


1 Answers

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.

like image 72
ojdo Avatar answered Apr 14 '26 23:04

ojdo