Repository: pallets/werkzeug

Issue:
downloading from send_file unpacks gzipped files
The gzipped files returned with
````
return send_file( fpath, as_attachment=True ) 
````
are being downloaded unpacked. The file are saved as \*.gz but its content is plaintext.

This does not happen with 
Flask==1.1.2
werkzeug==1.0.1

Environment:
- Python version: 3.7.1
- Flask version: 2.0.1



Discussion:
How are you downloading the file? See pallets/flask#3755 for the change.
So maybe `send_file` should only set `Content-Encoding` if `as_attachment` is `False`?
I am not sure what you mean... I click on a link http://127.0.0.1:5000/download/test.txt.gz

I have created a simple test view:
````
@app.route('/download/<fname>')
def download(fname):
    return send_from_directory(app.config['EXP_DIR'], fname, as_attachment=True)  
````

The file in the EXP_DIR is just a gzipped plain text file. I would expect that this file is just downloaded "AS IS".
I have the same issue.
I found that doing:
```bash
curl -X GET http://myurl.com/files/1 --output /tmp/test.tar.gz
```
works fine, and the filesize is ~10kb
But when I do:
```python
import requests
resp = requests.request('GET', url='http://myurl.com/files/1')
with open('/tmp/test.tar.gz', 'wb') as f:
    f.write(resp.content)
```
The file size is ~100kb

I am using the following very simple call in my flask API:
```python
import io
from flask import send_file

send_file(
    io.BytesIO(open('/src/test.tar.gz', 'rb').read()),
    download_name='test.tar.gz',
    as_attachment=True,
)
```

by checking the before/after response headers in Postman, I found the only extra header related to this is `Content-Encoding: gzip` in flask 2.x which led me to this issue.
Yes, @AbdealiJK - downloading with curl is OK - ie. 

        curl -O "http://127.0.0.1:5000/download/whatever.gz"
Hey all, not sure if you saw my comment above, but anyone is welcome to submit a PR to fix this.

> So maybe send_file should only set Content-Encoding if as_attachment is False?

I can confirm that the suggested change works as expected:

          #if encoding is not None:
          if encoding is not None and not as_attachment:
              headers.set("Content-Encoding", encoding) 

Resolving patch:
diff --git a/src/werkzeug/utils.py b/src/werkzeug/utils.py
--- a/src/werkzeug/utils.py
+++ b/src/werkzeug/utils.py
@@ -650,6 +650,10 @@ def send_file(
     :param _root_path: Do not use. For internal use only. Use
         :func:`send_from_directory` to safely send files under a path.
 
+    .. versionchanged:: 2.0.2
+        ``send_file`` only sets a detected ``Content-Encoding`` if
+        ``as_attachment`` is disabled.
+
     .. versionadded:: 2.0
         Adapted from Flask's implementation.
 
@@ -716,7 +720,9 @@ def send_file(
         if mimetype is None:
             mimetype = "application/octet-stream"
 
-        if encoding is not None:
+        # Don't send encoding for attachments, it causes browsers to
+        # save decompress tar.gz files.
+        if encoding is not None and not as_attachment:
             headers.set("Content-Encoding", encoding)
 
     if download_name is not None:


Test patch:
diff --git a/tests/test_send_file.py b/tests/test_send_file.py
--- a/tests/test_send_file.py
+++ b/tests/test_send_file.py
@@ -160,11 +160,14 @@ def test_etag():
     assert rv.headers["ETag"] == '"unique"'
 
 
-def test_content_encoding():
-    rv = send_file(txt_path, environ, download_name="logo.svgz")
+@pytest.mark.parametrize("as_attachment", (True, False))
+def test_content_encoding(as_attachment):
+    rv = send_file(
+        txt_path, environ, download_name="logo.svgz", as_attachment=as_attachment
+    )
     rv.close()
     assert rv.mimetype == "image/svg+xml"
-    assert rv.content_encoding == "gzip"
+    assert rv.content_encoding == ("gzip" if not as_attachment else None)
 
 
 @pytest.mark.parametrize(