Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unzipping a Zip File in Django

Tags:

python

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?

like image 766
user1328021 Avatar asked Jan 21 '13 15:01

user1328021


2 Answers

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

if libitem.startswith('__MACOSX/'):
  continue
like image 88
Ignacio Vazquez-Abrams Avatar answered Oct 18 '22 18:10

Ignacio Vazquez-Abrams


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

link

like image 22
Emanuele Paolini Avatar answered Oct 18 '22 18:10

Emanuele Paolini