]> granicus.if.org Git - python/commitdiff
RFC 1952 requires the FNAME field to be Latin-1. Do not include
authorLars Gustäbel <lars@gustaebel.de>
Fri, 10 Aug 2007 12:02:32 +0000 (12:02 +0000)
committerLars Gustäbel <lars@gustaebel.de>
Fri, 10 Aug 2007 12:02:32 +0000 (12:02 +0000)
filenames that cannot be represented that way.

Lib/gzip.py

index b6cc80e37cfb46f50f4f7bba99014ff6b2541d91..e7f6737558f749fa8bc0ffede499c7b46deb6a99 100644 (file)
@@ -153,6 +153,14 @@ class GzipFile:
         if fname.endswith(".gz"):
             fname = fname[:-3]
         flags = 0
+
+        # RFC 1952 requires the FNAME field to be Latin-1. Do not
+        # include filenames that cannot be represented that way.
+        try:
+            fname = fname.encode('latin-1')
+        except UnicodeEncodeError:
+            fname = ''
+
         if fname:
             flags = FNAME
         self.fileobj.write(chr(flags).encode('latin-1'))
@@ -160,8 +168,7 @@ class GzipFile:
         self.fileobj.write(b'\002')
         self.fileobj.write(b'\377')
         if fname:
-            # XXX: Ist utf-8 the correct encoding?
-            self.fileobj.write(fname.encode('utf-8') + b'\000')
+            self.fileobj.write(fname + b'\000')
 
     def _init_read(self):
         self.crc = zlib.crc32("")