0

If I have a filepath or stream, is there a static method I can use to determine whether the file/stream is a package, other than by trying to use Package.Open on the file/stream and catching the System.IO.FileFormatException?

1
  • 1
    Not that I can see; and stepping through the reference source down about 10 levels, suggests that there would be no work saved in writing one. I could not even find the throw of FileFormatException in about 20 minutes searching. Sep 13, 2017 at 0:54

1 Answer 1

3

You can either attempt to open the file as a .zip file and then look for metadata (thus implementing part of the package specification where you verify the package format) or you can just use Open() and catch any potential exception. (The .docx / .xlsx / etc. formats are just ZIP files that follow a certain structure.)

You can try reading the ZIP header but that will only verify that the file is a ZIP file - I'm not sure this buys you much (if anything).

My guess is that trying to open the file and catching the exception is the easiest route to go - if the specification changes your code will keep working. If you roll your own code to verify the file format, you'll have to keep maintaining it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.