2010-11-24 95 views
0

我在一個名爲öl_och_ål_är_gott.zip的zip壓縮文件中有一個文件öl_och_ål_är_gott.txt。檔案不是使用zipfile創建的。它可能來自任何能夠創建zip檔案的軟件。如何在壓縮文件中使用非ASCII文件名並在Python 2.4中使用ZipFile將其解壓縮?

src = open(file_path, "rb") 
zip_file = ZipFile(src) 
for info in zip_file.infolist(): 
    print info.filename 
    ... 

打印出:

」l_och_†l_„r_gott.txt 

如何強制壓縮文件來代表,我希望它是代表的名字嗎?

回答

1

作爲文檔狀態,沒有ZIP文件的官方文件名編碼。如果你有Unicode文件名(如你的情況),你必須在通過它們之前將它們轉換爲你想要的編碼的字節串。

DOCS

雖然我不知道爲什麼它不爲你工作。

>>> src = open('/Desktop/test.zip', 'rb') 
>>> zip_file = zipfile.ZipFile(src) 
>>> for info in zip_file.infolist(): 
...  print info.filename 
... 
öl_och_ål_är_gott 

在我的Ubuntu盒子上。

+0

對不起,我應該提到該檔案不是使用zipfile創建的。檔案來自任意來源。 – MdaG 2010-11-24 16:39:46