Re: zipfile + symlink [Solved]..
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: zipfile + symlink [Solved]..

From: A. Murat Eren <meren@uludag.org.tr>
Date: Fri Jun 24 2005 - 11:29:31 CEST

 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=20
Googlers ;))..

> -------8<-------------------8<--------------------8<------------------8<-=
=2D-
> 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 =3D 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)
>=20
> ------->8------------------->8-------------------->8------------------>8-=
=2D-
>--
>
> 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=
=20
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 =3D os.readlink(fileName)
                attr =3D zipfile.ZipInfo()
                attr.filename =3D fileName
                attr.create_system =3D 3
                attr.external_attr =3D 2716663808L # long type of=C2=A0hex =
val=20
                                                 # 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=20
symlinks), you have to check this value instead of directly reading from zi=
p=20
and writing to the file system:

 -------8<-------------------8<--------------------8<------------------8<---

      info =3D zip.getinfo(fileName)
      if hex(info.external_attr) =3D=3D 2716663808L:
          target =3D zip.read(fileName)
          os.symlink(target, ofile)

 ------->8------------------->8-------------------->8------------------>8---

 Ciao,
=2D-=20

=2D -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
 A. Murat Eren
 meren at uludag.org.tr
 http://cekirdek.uludag.org.tr/~meren/
 0x527D7293,=20
 7BCD A5A1 8101 0F6D 84A4 BD11 FE46 2B92 527D 7293
=2D -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -

=2D-
 Alan Cox sounds as if he hasn't followed the
 development of programming languages
 and compilers for the last 10 years (exa).
=2D

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBCu9J//kYrklJ9cpMRAh9nAJ9EUuNeR2YntX1jHzqyy6gPJ+w8tgCfepVL
uJMBRCKNdV43hCfZ4xbrESo=
=yhFf
-----END PGP SIGNATURE-----
Received on Thu Sep 29 16:34:29 2005