简体   繁体   中英

Unzipping a Zip File in Django

I'm trying to unzip a zip file in Django using the zipfile library.

This is my code:

if formtoaddmodel.is_valid():
        content = request.FILES['content']
        unzipped = zipfile.ZipFile(content)
        print unzipped.namelist()
        for libitem in unzipped.namelist():
            filecontent = file(libitem,'wb').write(unzipped.read(libitem))

This is the output of print unzipped.namelist()

['FileName1.jpg', 'FileName2.png', '__MACOSX/', '__MACOSX/._FileName2.png']

Im wondering what the last two items are -- it looks like the path. I don't care about there -- so how is there a way to filter them out?

https://superuser.com/questions/104500/what-is-macosx-folder

if libitem.startswith('__MACOSX/'):
  continue

Those files are tags added by the zip utility on MACS. You can assume the name starts with '__MACOSX/'

link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM