'A error when unzip a zipped file that contains a very long filename on Windows system
Here, I got a zipped file from Linux, now I want to unzip all files in my Windows PC. but I got a error that said the file name was too long to write to system.
Now I just have a idea that before I write to disk, firstly I can change the filename in memory, then loop the file list, change the file name to a proper one, then write it to disk. how can I implement that in Python?
Does zipfile can help me? I try to write some codes to implement my solution here:
import os
import zipfile
if __name__ == "__main__":
zf = zipfile.ZipFile('c://jekyll-export.zip', 'r')
# before I extract to local directory,
# how can I change the file name?
zf.extractall() # this can not works
Yes, I get a version that can works! but anyone can give a better solution? or suggestions?
if __name__ == "__main__":
try:
zf = zipfile.ZipFile('c://jekyll-export.zip', 'r')
except Exception as e:
print str(e)
i = 0
try:
for info in zf.infolist():
i += 1
print info.filename
original_name = urllib.unquote(info.filename)
print original_name
out_path = os.path.join(os.path.dirname(__file__), 'output') + original_name
print type(out_path)
#print os.path.dirname(os.path.dirname(out_path))
if not os.path.exists(os.path.dirname(out_path)):
os.makedirs(os.path.dirname(out_path))
buffer_size = 16 * 1024
with zf.open(info) as fin, open(unicode(out_path, 'utf-8'), 'w') as fout:
while True:
buf = fin.read(buffer_size)
if not buf:
break
fout.write(buf)
except (WindowsError, IOError) as e:
print str(e)
print i
Solution 1:[1]
Use pkzip rather than winzip. WinZip doesn't seem to support the long filenames.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Elliptical view |
