Compressing Files with Python: Symlink Trouble!

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..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s