Bits dropped from st0w's various technical experimentations
RSS icon Email icon Home icon
  • Python: Extracting a file from a zip file with a different name

    Posted on July 23rd, 2010 st0w No comments

    When dealing with zip files, sometimes you may want to alter the filename before extraction. Whether it is to run the name through some kind of sanitization routine or adjust it in some other way, the docs don’t seem to offer a clear way to do this. Instead, I’ve found the following works quite nicely:

    import zipfile
    zipdata = zipfile.ZipFile('')
    zipinfos = zipdata.infolist()
    for zipinfo in zipinfos:
        zipinfo.filename = do_something_to(zipinfo.filename)

    The Python zipfile module provides the ability to reference the contents of a zip file via ZipInfo objects. These are tied to individual archive entries, but not by filename. During extraction, you can pass either an archive filename, or a ZipInfo object. If you alter the ZipInfo object’s filename attribute before extraction, extract(zipinfo) will then use the new name for extraction, but extract the original data.