pythonbz2

compress multiple files into a bz2 file in python


I need to compress multiple files into one bz2 file in python. I'm trying to find a way but I can't can find an answer. Is it possible?


Solution

  • This is what tarballs are for. The tar format packs the files together, then you compress the result. Python makes it easy to do both at once with the tarfile module, where passing a "mode" of 'w:bz2' opens a new tar file for write with seamless bz2 compression. Super-simple example:

    import tarfile
    
    with tarfile.open('mytar.tar.bz2', 'w:bz2') as tar:
        for file in mylistoffiles:
            tar.add(file)
    

    If you don't need much control over the operation, shutil.make_archive might be a possible alternative, which would simplify the code for compressing a whole directory tree to:

    shutil.make_archive('mytar', 'bztar', directory_to_compress)