'How to create symlink in zipfile in python

I have a zipfile which is stored in memory as io.BytesIO buffer. Within, the zipfile, I need to create a symlink to one of the directories.

Below is what I have tried so far (from this similar question on SO) but it is not creating the link as I want at top level.

Link to be created to : root/scale/lib/hypervisor/kvm/pika_3_5

Link name : 'pika'

Link location : at top level

Existing Zipfile : data['egg_buffer']

with zipfile.ZipFile(data['egg_buffer'], 'a') as zip_buffer:
    dest = 'root/scale/lib/hypervisor/kvm/pika_3_5'
    info = zipfile.ZipInfo('pika')
    info.external_attr |= 0120000 << 16L # symlink file type
    info.compress_type = zipfile.ZIP_STORED
    zip_buffer.writestr(info, dest)
data['egg_buffer'].seek(os.SEEK_SET)

Actually this is creating a file at root level with name 'pika' but it is not a symlink and has content as text root/scale/lib/hypervisor/esx65/pika_3_5 instead.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source