Corrupted GZIP file downloaded from Google Cloud Storage
<p>When I download a GZIP file stored in a bucket in Google Cloud Storage from the Storage platform web UI, everything goes well and I can unzip the file without any problem.</p>

<p>However, when I use googleapiclient with Python in order to download the file, I cannot unzip it. 7-Zip says that the file is broken.
My Code:</p>

<pre><code>import io
from apiclient.http import MediaIoBaseDownload
from googleapiclient import http
bucket='bqtoredshiftdaily'

out_file=os.path.join(current_dir,process_name,"Upload",gcsfile.replace("/", "_"))

with open(out_file, 'w') as f:

    req = gcs_service.objects().get_media(bucket=bucket, object=gcsfile)

    downloader = http.MediaIoBaseDownload(f, req)

    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print("Download {}%.".format(int(status.progress() * 100)))
</code></pre>

<p>The download succeeds but as I said, I cannot unzip the downloaded GZIP file.
Any idea why?</p>

<p>I changed the output file to binary and it solved it:</p>

<pre><code>with open(out_file, 'wb') as f:
</code></pre>

<p>instead of:</p>

<pre><code>with open(out_file, 'w') as f:
</code></pre>

python gzip google-cloud-storage