can tarfile maintain directory structure?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jay Donnell

    can tarfile maintain directory structure?

    Is there a way to use the tarfile module to recursively compress the
    contents of a directory and maintain the directory structure in the
    tar archive?

    Simply doing os.system('tar -czvf ' + fileName +'.tar.gz ' +
    directory)
    works great on linux, but I need this script to work on windows as
    well :(
  • François Pinard

    #2
    Re: can tarfile maintain directory structure?

    [Jay Donnell]
    [color=blue]
    > Simply doing os.system('tar -czvf ' + fileName +'.tar.gz ' +
    > directory) works great on linux, but I need this script to work on
    > windows as well :([/color]

    GNU tar, and surely others, have been ported to Windows. Check within
    the DJGPP and Cygwin projects.

    --
    François Pinard http://www.iro.umontreal.ca/~pinard

    Comment

    • Adonis

      #3
      Re: can tarfile maintain directory structure?

      "Jay Donnell" <jaydonnell@yah oo.com> wrote in message
      news:a6fdfd6b.0 408171508.65d67 0d0@posting.goo gle.com...[color=blue]
      > Is there a way to use the tarfile module to recursively compress the
      > contents of a directory and maintain the directory structure in the
      > tar archive?
      >
      > Simply doing os.system('tar -czvf ' + fileName +'.tar.gz ' +
      > directory)
      > works great on linux, but I need this script to work on windows as
      > well :([/color]

      Starting from Python 2.3 there is a tarfile module in the stdlib
      Source code: Lib/tarfile.py The tarfile module makes it possible to read and write tar archives, including those using gzip, bz2 and lzma compression. Use the zipfile module to read or write.zip fi...


      Adonis


      Comment

      • Jeff Epler

        #4
        Re: can tarfile maintain directory structure?

        You can use os.walk (or os.path.walk for older versions of Python) to
        recurse a directory tree. Here's a simple script to use tarfile and
        os.walk:

        import tarfile, sys, os

        t = tarfile.TarFile (sys.argv[1], "w")
        for f in sys.argv[2:]:
        for dirpath, dirnames, filenames in os.walk(f):
        for f in filenames:
        f = os.path.join(di rpath, f)
        print "Adding", f
        t.add(f)

        t.close()

        Here's a sample session with it:
        * Creating a simple directory structure
        $ mkdir a
        $ touch a/file.txt
        $ mkdir a/subdir
        $ touch a/subdir/subfile.txt

        * Invoking the script
        $ python ~/mktar.py test.tar a
        Adding a/file.txt
        Adding a/subdir/subfile .txt

        * Checking on the results
        $ tar tvf test.tar
        -rw-rw-r-- jepler/jepler 0 2004-08-17 21:00:57 a/file.txt
        -rw-rw-r-- jepler/jepler 0 2004-08-17 21:01:03 a/subdir/subfile.txt

        I suspect that to get compressed output would involve use of gzip.open
        and the 3-argument TarFile constructor, something like
        import gzip
        g = gzip.open(sys.a rgv[1], "w")
        t = tarfile.TarFile (sys.argv[1], "w", g)
        ...
        indeed, this seems to work for me.
        $ python ~/mktargz.py test.tar.gz a
        Adding a/file.txt
        Adding a/subdir/subfile.txt
        $ file test.tar.gz
        test.tar.gz: gzip compressed data, was "test.tar", max compression

        Jeff

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

        iD8DBQFBIrm1Jd0 1MZaTXX0RAmjCAK CpkTSIzUwPrrEbN OAZGeQAJ1TZlACg ob8E
        QGka1Q/r08XFFKnUcY+lJM o=
        =A2fD
        -----END PGP SIGNATURE-----

        Comment

        • Lars Gustaebel

          #5
          Re: can tarfile maintain directory structure?

          On Tue, 17 Aug 2004 21:06:45 -0500, Jeff Epler wrote:
          [color=blue]
          > You can use os.walk (or os.path.walk for older versions of Python) to
          > recurse a directory tree. Here's a simple script to use tarfile and
          > os.walk:
          > [snip][/color]

          Far too complicated... tarfile.py is rather high-level:

          import tarfile

          tar = tarfile.open(fi lename, "w:gz")
          tar.add(directo ry)
          tar.close()

          The add() method is recursive by default. More information and examples
          here: http://docs.python.org/lib/module-tarfile.html

          --
          Lars Gustäbel
          lars@gustaebel. de

          Comment

          • Jay Donnell

            #6
            Re: can tarfile maintain directory structure?

            > import tarfile[color=blue]
            >
            > tar = tarfile.open(fi lename, "w:gz")
            > tar.add(directo ry)
            > tar.close()
            >
            > The add() method is recursive by default. More information and examples
            > here: http://docs.python.org/lib/module-tarfile.html[/color]

            That doesn't maintain the directory structure. When you untar it all
            the files are in the base directory (when I untar it on windows with
            winzip).

            I haven't tried jeff's suggestion yet, but I'll let ya'll know how
            that goes.

            Comment

            • David M. Cooke

              #7
              Re: can tarfile maintain directory structure?

              At some point, jaydonnell@yaho o.com (Jay Donnell) wrote:
              [color=blue][color=green]
              >> import tarfile
              >>
              >> tar = tarfile.open(fi lename, "w:gz")
              >> tar.add(directo ry)
              >> tar.close()
              >>
              >> The add() method is recursive by default. More information and examples
              >> here: http://docs.python.org/lib/module-tarfile.html[/color]
              >
              > That doesn't maintain the directory structure. When you untar it all
              > the files are in the base directory (when I untar it on windows with
              > winzip).[/color]

              Winzip is probably broken? It works for me using GNU tar on Linux.

              --
              |>|\/|<
              /--------------------------------------------------------------------------\
              |David M. Cooke
              |cookedm(at)phy sics(dot)mcmast er(dot)ca

              Comment

              • Richard Townsend

                #8
                Re: can tarfile maintain directory structure?

                On Wed, 18 Aug 2004 15:13:29 -0400, David M. Cooke wrote:[color=blue]
                >
                > Winzip is probably broken? It works for me using GNU tar on Linux.[/color]


                There was a bug report relating to tarfile & WinZip:


                Looks like it was closed today.

                regards,
                Richard

                Comment

                • Jay Donnell

                  #9
                  Re: can tarfile maintain directory structure?

                  > > import tarfile[color=blue][color=green]
                  > >
                  > > tar = tarfile.open(fi lename, "w:gz")
                  > > tar.add(directo ry)
                  > > tar.close()[/color][/color]

                  This works perfectly on linux, but it wasn't working for me on windows
                  yesterday. I'll try it again the next time I'm on windows.

                  Comment

                  Working...