3

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.

S Andrew
  • 5,592
  • 27
  • 115
  • 237
  • 2
    Do you want this to be saved somewhere (i.e. you want basically to implement `gunzip` in Python)? – norok2 Sep 14 '18 at 13:20
  • Possible duplicate of [How to unzip gz file using Python](https://stackoverflow.com/questions/31028815/how-to-unzip-gz-file-using-python) – meyi Sep 14 '18 at 13:21
  • 1
    @norok2 yes extract the file to any destination place – S Andrew Sep 14 '18 at 13:24
  • @Meyi I have already checked that link. There is no answer where we can download the file in actual. Please let me know which answer you are referring to.? Thanks – S Andrew Sep 14 '18 at 13:30
  • @SAndrew The link that Meyi posted shows how to unzip a .gz file. Do you actually want to do something else ? You mention download - do you mean you want to download the file from some web server ? – nos Sep 14 '18 at 13:37
  • Check this link which provides an answer.
    https://stackoverflow.com/questions/31028815/how-to-unzip-gz-file-using-python
    – Anurag A S Sep 14 '18 at 13:38

2 Answers2

10

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)
norok2
  • 25,683
  • 4
  • 73
  • 99
1

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

S Andrew
  • 5,592
  • 27
  • 115
  • 237
  • 1
    Your solution is sub-optimal as you may run into memory issues for large files. Besides, this is going to be slower than it could be, as you are performing unnecessary decoding. – norok2 Sep 14 '18 at 14:07