4

I'm trying to merge two zip files using python.

I had it working, until I realised that it wasn't behaving when it came to the symbolic links that the source zip file contained. Since I was using zipfile.read() on each file when I was adding it to the new zip file, it was reading the symlink and creating a text file, not a symlink.

Does anyone know of a way to get python to preserve the symlink's from the source zip file when I'm writing them to the target zip file?

Thanks.

2
  • 1
    Related: mail-archive.com/[email protected]/msg34223.html Commented Oct 22, 2012 at 17:48
  • 2
    When you say merge the two zip files, do you mean zip file A's contents and zip files B's distinct contents into a single zip file? What happens when the same file exists in both? What is the broader goal that requires merging two zip files? Commented Oct 22, 2012 at 19:05

1 Answer 1

2

This is how it should be done :

      if os.path.islink(filePath):
            attr = zipfile.ZipInfo(filePath)
            attr.create_system = 3 # 3 for unix, 0 for windoze
            attr.external_attr = 2716663808L # to include file as a symlink
            newZip.writestr(attr, os.readlink(filePath))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.