Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Read a gzip file from inside a tar file

Reply
Thread Tools

Read a gzip file from inside a tar file

 
 
rohisingh@gmail.com
Guest
Posts: n/a
 
      12-13-2004
I have a tar file. The content of the file are as following.

rohits@sandman 12-08-04 $ tar tvf 20041208.tar
drwxr-xr-x root/root 0 2004-12-08 21:39:19 20041208/
-rw-r--r-- root/root 1576 2004-12-08 21:39:19 20041208/README
drwxr-xr-x root/root 0 2004-12-08 21:27:31
20041208/snapshot_01/
-rw-r--r-- was/was 103010606 2004-12-08 16:37:38
20041208/snapshot_01/tpv-2004 1208-1350.xml.gz


What is the best method to read the content of the
tpv-20041208-1350.xml.gz?

I want to do the following with minimum code
1) read above tar file
2) find the gzip file
3) read the content of this file
4) perform operations on content
5) continue

I tried various combination of following code but it does not work as
intended

fileName = sys.argv[1]
print "File Name is ", fileName
tar = tarfile.open(fileName, "r:")
for tarinfo in tar:
if tarinfo.isreg():
print tarinfo.name
if tarinfo.name.find("tpv") != -1:
#read the gzip file
print "\thttp plugin file"
fileLike = tar.extractfile(tarinfo)
fileText = fileLike.read()
stringio = StringIO.StringIO(fileText)
fileRead = gzip.GzipFile(stringio)
for aLine in fileRead:
print aLine

 
Reply With Quote
 
 
 
 
Rohit
Guest
Posts: n/a
 
      12-13-2004
if I change fileText = fileLike.read() to fileText =
fileLike.readLines().

It works for a while before it gets killed of out of memory.

These are huge files. My goal is to analyze the content of the gzip
file in the tar file without having to un gzip. If that is possible.

 
Reply With Quote
 
 
 
 
Craig Ringer
Guest
Posts: n/a
 
      12-13-2004
On Tue, 2004-12-14 at 02:39, Rohit wrote:
> if I change fileText = fileLike.read() to fileText =
> fileLike.readLines().
>
> It works for a while before it gets killed of out of memory.
>
> These are huge files. My goal is to analyze the content of the gzip
> file in the tar file without having to un gzip. If that is possible.


As far as I know, gzip is a stream compression algorithm that can't be
decompressed in small blocks. That is, I don't think you can seek 500k
into a 1MB file and decompress the next 100k.

I'd say you'll have to progressively read the file from the beginning,
processing and discarding as you go. It looks like a no-brainer to me -
see zlib.decompressobj.

Note that you _do_ have to ungzip it, you just don't have to store the
whole decompressed thing in memory / on disk at once. If you need to do
anything to it that does require the entire thing to be loaded (or
anything that means you have to seek around the file), I'd say you're
SOL.

--
Craig Ringer

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      12-13-2004
Craig Ringer wrote:

>> These are huge files. My goal is to analyze the content of the gzip
>> file in the tar file without having to un gzip. If that is possible.

>
> As far as I know, gzip is a stream compression algorithm that can't be
> decompressed in small blocks. That is, I don't think you can seek 500k
> into a 1MB file and decompress the next 100k.


correct.

> I'd say you'll have to progressively read the file from the beginning,
> processing and discarding as you go. It looks like a no-brainer to me -
> see zlib.decompressobj.


it can be a bit tricky to set things up properly, though. here's a piece
of code that uses Python's good old consumer interface to decode things
incrementally:

http://effbot.org/zone/consumer-gzip.htm

you can either use this as is; just create a "target consumer", wrap it in the
gzip consumer, and feed data to the gzip consumer in suitable pieces.

alternatively, hack it until it does what you want.

</F>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question on using tarfile to read a *.tar.gzip file m_ahlenius Python 2 02-08-2010 12:25 AM
os.system('tar -c * | tar -C dst') ##Any other suggestions... list.repository@gmail.com Python 2 04-24-2007 10:29 PM
does java has any api for tar and gzip? Nagesh Java 4 02-27-2006 11:17 AM
Version of TAR in tarfile module? TAR 1.14 or 1.15 port to Windows? Claudio Grondi Python 4 08-20-2005 08:01 PM
trouble with Archive::Tar : Can not read compressed format in tar-mode benoit Guyon Perl Misc 2 07-26-2005 01:53 PM



Advertisments