1

I have a folder full of jar, html, css, exe type file. How can I check the file?

I already run "file" command on *NIX and using python-magic. but the result is all like this.

test : Zip archive data, at least v1.0 to extract

How can I get information specifically like test : jar only using using magic number.

How do I do like this?

bunseokbot
  • 11
  • 2
  • All jar files are zip files - you need to extract the contents and determine manually whether they can be understood by Java, or just trust that the file name extension of `.jar` marking them as `jar` files is true. – metatoaster Jul 04 '16 at 00:32

1 Answers1

0

While not required, most JAR files have a META-INF/MANIFEST.MF file contained within them. You could check for the existence of this file, after checking if it's a zip file:

import zipfile

def zipFileContains(zipFileName, pathName):
    f = zipfile.ZipFile(zipFileName, "r")
    result = any(x.startswith(pathName.rstrip("/")) for x in f.namelist())
    f.close()

    return result

print zipFileContains("test.jar", "META-INF/MANIFEST.MF")

However, it might be better to just check if it's a zip file that ends in .jar.

Magic alone won't do it for you, since a JAR is literally just a zip file. Read more about the format here.

Will
  • 24,082
  • 14
  • 97
  • 108