Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5590d8c

Browse files
committed
RFC 1952 requires the FNAME field to be Latin-1. Do not include
filenames that cannot be represented that way.
1 parent 36f938f commit 5590d8c

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

Lib/gzip.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,22 @@ def _write_gzip_header(self):
153153
if fname.endswith(".gz"):
154154
fname = fname[:-3]
155155
flags = 0
156+
157+
# RFC 1952 requires the FNAME field to be Latin-1. Do not
158+
# include filenames that cannot be represented that way.
159+
try:
160+
fname = fname.encode('latin-1')
161+
except UnicodeEncodeError:
162+
fname = ''
163+
156164
if fname:
157165
flags = FNAME
158166
self.fileobj.write(chr(flags).encode('latin-1'))
159167
write32u(self.fileobj, int(time.time()))
160168
self.fileobj.write(b'\002')
161169
self.fileobj.write(b'\377')
162170
if fname:
163-
# XXX: Ist utf-8 the correct encoding?
164-
self.fileobj.write(fname.encode('utf-8') + b'\000')
171+
self.fileobj.write(fname + b'\000')
165172

166173
def _init_read(self):
167174
self.crc = zlib.crc32("")

0 commit comments

Comments
 (0)