2

I have a problem such that i have zip files uploaded from forms and i want to detect the mime type of the files that contained in these zip files,i have no problem in extracting the files from the zip but the problem how to know the mime type of each file in this zip?Thanks

Computer_Engineer
  • 393
  • 1
  • 8
  • 19

2 Answers2

5

You can use the Python built-in mimetypes module for this. I believe this module relies solely on the file name and not its contents.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
4

You could use the python-magic library:

>>> mime = magic.Magic(mime=True)
>>> mime.from_file("testdata/test.pdf")
'application/pdf'
jterrace
  • 64,866
  • 22
  • 157
  • 202
  • It's not a builtin module. Did you read the installation instructions at the link I provided? You can ``pip install python-magic`` or ``easy_install python-magic`` – jterrace Mar 25 '12 at 18:39
  • @Eng_Engineer the link has installation instructions for windows – jterrace Mar 25 '12 at 18:41
  • :Sorry but in this link i don't know what can i download exactly to install magic.dll(http://gnuwin32.sourceforge.net/) – Computer_Engineer Mar 25 '12 at 18:43
  • @Eng_Engineer try [this link](http://downloads.sourceforge.net/project/getgnuwin32/getgnuwin32/0.6.30/GetGnuWin32-0.6.3.exe?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fgetgnuwin32%2Ffiles%2Fgetgnuwin32%2F0.6.30%2F&ts=1332701160&use_mirror=voxel) – jterrace Mar 25 '12 at 18:46
  • :i installed Automated gnuwin32 download tool from this link,what should i do next step? – Computer_Engineer Mar 25 '12 at 18:50
  • magic1.dll, regex2.dll and zlib1.dll all need to be in system32 directory – jterrace Mar 25 '12 at 19:21
  • these two files aren't exist in the GetGnuWin32,BTW i used mime=mimetypes.guess_type(name)[0] to get the mime type and it's worked correctly,Thanks for your help. – Computer_Engineer Mar 25 '12 at 19:24
  • those files are definitely part of gnuwin32. just note that mimetypes relies on the file extension, not the file contents itself – jterrace Mar 25 '12 at 19:46