0
votes

I have compressed a file into several chunks using 7zip:

HAVE:

foo.txt.gz.001
foo.txt.gz.002
foo.txt.gz.003
foo.txt.gz.004
foo.txt.gz.005

WANT:

foo.txt

How do I unzip and combine these chunks to get a single file using python?

2
It is not very clear from the title and the content whether you intend to package all the compressed files into one .gz file or unzip, read and concatenate contents.newkid
I edited my question.r0f1
Did you try anything, so far?Isma
You should concatenate all into one file and decompress itMauro Baraldi

2 Answers

3
votes

First, get the list of all files.

files = ['/path/to/foo.txt.gz.001', '/path/to/foo.txt.gz.002', '/path/to/foo.txt.gz.003']

Then iterate over each file and append to a result file.

with open('./result.gz', 'ab') as result:  # append in binary mode
    for f in files:
        with open(f, 'rb') as tmpf:        # open in binary mode also
            result.write(tmpf.read())

Then extract is using zipfile lib. You could use tempfile to avoid handle with temporary zip file.

0
votes

First you must extract all the zip files sequentially:

import zipfile

paths = ["path_to_1", "path_to_2" ]
extract_paths = ["path_to_extract1", "path_to_extrac2"]

for i in range(0, paths):
    zip_ref = zipfile.ZipFile(paths[i], 'r')
    zip_ref.extractall(extract_paths[i])
    zip_ref.close()

Next you can go to the extracted location and read() individual files with open into a string. Concatenate those strings and save to foo.txt.