473,738 Members | 3,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 :(
Jul 18 '05 #1
8 11143
[Jay Donnell]
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 :(


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
Jul 18 '05 #2
"Jay Donnell" <ja********@yah oo.com> wrote in message
news:a6******** *************** ***@posting.goo gle.com...
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 :(


Starting from Python 2.3 there is a tarfile module in the stdlib
http://docs.python.org/lib/module-tarfile.html

Adonis
Jul 18 '05 #3
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-----

Jul 18 '05 #4
On Tue, 17 Aug 2004 21:06:45 -0500, Jeff Epler wrote:
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]


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
la**@gustaebel. de

Jul 18 '05 #5
> 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


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.
Jul 18 '05 #6
At some point, ja********@yaho o.com (Jay Donnell) wrote:
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


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


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
Jul 18 '05 #7
On Wed, 18 Aug 2004 15:13:29 -0400, David M. Cooke wrote:

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

There was a bug report relating to tarfile & WinZip:
http://sourceforge.net/tracker/?grou...ail&aid=949052

Looks like it was closed today.

regards,
Richard
Jul 18 '05 #8
> > import tarfile

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


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.
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
5335
by: FISH | last post by:
Ever have one of those days when you're not sure if it's you who's gone mad, or the rest of the world? I have an Open Source project on SourceForge for communication with YSMG - Yahoo's IM protocol. I keep the project source in three directories, based upon the code's function: one for the network API code itself, one for the support APIs (basic chat spam filtering, Swing models, rich text decoders, etc), and one for the test client...
2
2342
by: Jignesh | last post by:
Can someone please tell me how to "import" a directory structure into a project in Microsoft Visual C++ .NET 2003. I have an extremeley complex directory structure consisting of thousands of files and hundreds of subdirectories. I want to be able to view all of these inside of my project in the same directory structure format. I don't want to go through each directory and add files to the project. I have been able to do this with other...
4
9244
by: shane | last post by:
From searching around, Ive seen this question asked numerous times, but havent seen a usable answer unfortuantly. What Im trying to do is something I would have thought would be quite common, but there just seems to be no information on the topic that I can find. Basically I need to store a directory structure in memory, from a flat list of files and paths. ie given: C:\dir\dir1\abc.txt C:\dir\toop.txt
8
2590
by: James Owens | last post by:
I'm a relative newbie, interested in storing the information from several server directories and subdirectories in XML so that I can present it selectively using XSL (all files updated today or last MOnday, files from several drirectories in alphabetical order, things like that). To represent directories and subidrectories, is it advisable to nest the same element: <directory> <directory>
1
5091
by: soni29 | last post by:
when creating a project with namespace like: wrox.csharp.basics.overflowtest is it good practice to have the directory structure like that also: c:\wrox\csharp\basics\overflowtext.cs Also if it is then is there a way to make visual studio set it up for me as i'm creating the cs files? or do i have to first create the directory structures and then as i save files save them in the correct places depending on the namespace i create.
4
3710
by: Elmo Watson | last post by:
Is there a way, with the System.IO class, to do a recursive list of a directory structure? For instance, in DirectoryInfo, you have GetDirectories and GetFiles .... In Directory, you have Directory.GetFileSystemEntries(path), but I would like to know how to put this together, knowing which entry is a Subdirectory and which entry is a file, and make a recursive list of the Directory structure below a specific path - - -
2
1976
by: SharepointKida | last post by:
Hi I am building an web based .NET Application which allows users to specify a directory location from it's machine, and when it press publish it uploads all the files and subfolders folders from that directory to server. I just want to know if there is any mathod in .net which allow me to browse through client's directory structure programatically and give me subfolders and files info to upload them on server. I have build similar app using...
0
2060
by: Brent Clements | last post by:
I have been trying to determine the best way to setup a directory structure for my homegrown MVC application. What do you guys suggest? I am thinking about doing the following: | +-- app
0
2035
by: hsheboul | last post by:
Thanks to Markus for pointing me to all the Apache stuff http://bytes.com/topic/html-css/answers/876342-automating-web-site-template My Web site is hosted on a shared Linux hosting, that runs the Apache 2.2 I got multiple static "utility" files: CSS, JavaScripts, Impages, etc. In order to maintain a coherent and consistent structure, I put all these kind of utility files in one directory hierarchy. However, this directory structure might...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.