This is a follow-up of this previous post. I was trying to compress a directory that had symbolic links on it, using Python’s library zipfile. I was miguided into setting to True the os.walk argument followlinks, which in fact made me have a duplicate of my file, instead of a link. The following code is based on what A.Murat Eren wrote:
import zipfile, os Z = zipfile.ZipFile('myzip.zip', 'w') for r, d, f in os.walk('mydir'): for dd in d: if os.path.islink(os.path.join(r, dd)): a = zipfile.ZipInfo() a.filename = os.path.join(r, dd) a.create_system = 3 a.external_attr = 2716663808L Z.writestr(a, os.path.join(r, dd)) else: Z.write(os.path.join(r, ff), os.path.join(r, ff)) for ff in f: if os.path.islink(os.path.join(r, ff)): a = zipfile.ZipInfo() a.filename = ff a.create_system = 3 a.external_attr = 2716663808L Z.writestr(a, os.path.join(r, ff)) else: Z.write(os.path.join(r, ff), os.path.join(r, ff)) Z.close()
In my case I had a directory simlink, but this should be straightforward enough to implement for files. Another issue is with the line:
Z.writestr(a, os.path.join(r, dd))
That actually defines where your simbolic link will reside. Therefore, no matter which name you give to a.filename, this is what matters! I had a couple of troubles with lost symlinks because of this..