简体   繁体   中英

How to extract a gz file in python

I have a .gz file, inside which there is another file. I need to extract the file inside the zipped file.

f = gzip.open(dest, 'rb')

This only opens the file but I need to download that particular file which is inside gz instead of just opening the gz file.

This question has been marked as duplicate which I accept, but I haven't found a solution where we can actually download the file and not just read its content. Same is the case with the link mentioned.

You could just open two files, read from the gzipped file and write to the other file (in blocks to avoid clogging the memory).

import gzip

def gunzip(source_filepath, dest_filepath, block_size=65536):
    with gzip.open(source_filepath, 'rb') as s_file, \
            open(dest_filepath, 'wb') as d_file:
        while True:
            block = s_file.read(block_size)
            if not block:
                break
            else:
                d_file.write(block)

Otherwise, you could use shutil , as suggested in How to unzip gz file using Python :

import gzip
import shutil

def gunzip_shutil(source_filepath, dest_filepath, block_size=65536):
    with gzip.open(source_filepath, 'rb') as s_file, \
            open(dest_filepath, 'wb') as d_file:
        shutil.copyfileobj(s_file, d_file, block_size)

Both solutions would work in Python 2 and 3.

Performance-wise, they are substantially equivalent, at least on my system:

%timeit gunzip(source_filepath, dest_filepath)
# 129 ms ± 1.89 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%timeit gunzip_shutil(source_filepath, dest_filepath)
# 132 ms ± 2.99 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

I have resolved the issue like this:

f = gzip.open(dest, 'r')
file_content = f.read()
file_content = file_content.decode('utf-8')
f_out = open('file', 'w+')
f_out.write(file_content)
f.close()
f_out.close()

dest is the file with gz

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM