3

I am trying to extract a bz2 compressed folder in a specific location. I can see the data inside by :

handler = bz2.BZ2File(path, 'r')
print handler.read()

But I wish to extract all the files in this compressed folder into a location (specified by the user) maintaining the internal directory structure of the folder.

I am fairly new to this language .. Please help...

3 Answers 3

5

Like gzip, BZ2 is only a compressor for single files, it can not archive a directory structure. What I suspect you have is an archive that is first created by a software like tar, that is then compressed with BZ2. In order to recover the "full directory structure", first extract your Bz2 file, then un-tar (or equivalent) the file.

Fortunately, the Python tarfile module supports bz2 option, so you can do this process in one shot.

Sign up to request clarification or add additional context in comments.

2 Comments

And how to I do that ? the extracting BZ2 file part ?? Is there a function through which I directly get the uncompressed file or do I need to read the bz2 file first and then write that data to some other file ?
Open a tar.bz file as if it was an ordinary tar file. The Python tarfile module will take care of the rest.
2

bzip2 is a data compression system which compresses one entire file. It does not bundle files and compress them like PKZip does. Therefore handler in your example has one and only one file in it and there is no "internal directory structure".

If, on the other hand, your file is actually a compressed tar-file, you should look at the tarfile module of Python which will handle decompression for you.

Comments

1

You need to use the tarfile module to uncompress a .tar.bz2 file ... from the docs here is how you can do it:

import tarfile
tar = tarfile.open(path, "r:bz2")
for tarinfo in tar:
    print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
    if tarinfo.isreg():
        print "a regular file."
        # read the file
        f = tar.extractfile(tarinfo)
        print f.read()
    elif tarinfo.isdir():
        print "a directory."
    else:
        print "something else."
tar.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.