user1328021
user1328021

Reputation: 9850

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?

Upvotes: 6

Views: 3168

Answers (2)

Emanuele Paolini
Emanuele Paolini

Reputation: 10172

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

link

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799490

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

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

Upvotes: 5

Related Questions