简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM