Hi again, On Thursday 23 June 2005 23:43, A. Murat Eren wrote: > I have a problem about zipfile.
I solved the problem and want to share my solution for archiving purposes (Hi Googlers ;)).. > -------8<-------------------8<--------------------8<------------------8<--- > def add_file(self, fileName): > """add file or directory to a zip file""" > if os.path.isdir(fileName): > self.zip.writestr(fileName + '/', '') > for f in os.listdir(fileName): > self.add_file(fileName + '/' + f) > else: > if os.path.islink(fileName): > dest = os.readlink(fileName) > self.zip.writestr(fileName, dest) > #do something here to set 'fileName's attributes > #to write it as a symlink into the zip > else: > self.zip.write(fileName, fileName, zipfile.ZIP_DEFLATED) > > ------->8------------------->8-------------------->8------------------>8--- >-- > > But here is a problem raising for symlinks (at the commented section of > the code passage).. If i don't perform any special process for symlinks in > the function, function produces zip files without symlinks, naturally; > symlinks become regular files with the 'dest' content.. I have to set the > attribute of the symlinks in the zip to make them a real symlink for the > 'dest', how can i do that? Here is the solution for adding properly both dir and file symlinks into a zipfile: -------8<-------------------8<--------------------8<------------------8<--- def add_file(self, fileName): """Add file or directory to a zip file""" if os.path.isdir(fileName) and not os.path.islink(fileName): self.zip.writestr(fileName + '/', '') for f in os.listdir(fileName): self.add_file(fileName + '/' + f) else: if os.path.islink(fileName): dest = os.readlink(fileName) attr = zipfile.ZipInfo() attr.filename = fileName attr.create_system = 3 attr.external_attr = 2716663808L # long type of hex val # of '0xA1ED0000L' # say, symlink attr magic.. self.zip.writestr(attr, dest) else: self.zip.write(fileName, fileName, zipfile.ZIP_DEFLATED) ------->8------------------->8-------------------->8------------------>8--- When you trying to unpack this zipfile (or any zip file which contains symlinks), you have to check this value instead of directly reading from zip and writing to the file system: -------8<-------------------8<--------------------8<------------------8<--- info = zip.getinfo(fileName) if hex(info.external_attr) == 2716663808L: target = zip.read(fileName) os.symlink(target, ofile) ------->8------------------->8-------------------->8------------------>8--- Ciao, -- - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - A. Murat Eren meren at uludag.org.tr http://cekirdek.uludag.org.tr/~meren/ 0x527D7293, 7BCD A5A1 8101 0F6D 84A4 BD11 FE46 2B92 527D 7293 - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - -- Alan Cox sounds as if he hasn't followed the development of programming languages and compilers for the last 10 years (exa). -
pgpzCyu9ndaDX.pgp
Description: PGP signature
-- http://mail.python.org/mailman/listinfo/python-list