1

Possible Duplicate:
How Can I Check That A File Is A Valid XPS File With C#?

How do I identify an XPS file if I'm passed a stream or a file with no extension? I know XPS files are zip files so I can look for the ZIP magic number at the start but how can I identify an XPS file?

From the research I've done I think I need to extract and look in [Content_Types].xml but it's not (too) apparent what I need to look for once I'm in there.

Community
  • 1
  • 1
making
  • 408
  • 6
  • 21
  • usually, one way to check this is to analyze the header and the first HEX values. – user1717079 Oct 14 '12 at 00:33
  • An xps file is a zip file with certain content. So I can check for a zip signature but my problem is what do I look for in the content. – making Oct 14 '12 at 08:43

1 Answers1

2

I think in most cases you'd be fine just checking for [Content_Types].xml. It's possible that someone would pass you a zip file including a file named like that but not containing the information you'd expect it to have as part of an XPS document, but how likely is that? The file name is pretty specific and, to me, unlikely to produce accidental collisions.

But if you want to be extra sure, you could check if the XML file conforms to the expected schema. You should generally see something like this:

<?xml version="1.0" encoding="utf-8"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
    <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
    <Default Extension="fdseq" ContentType="application/vnd.ms-package.xps-fixeddocumentsequence+xml"/>
    <Default Extension="fdoc" ContentType="application/vnd.ms-package.xps-fixeddocument+xml"/>
    <Default Extension="xml" ContentType="application/vnd.ms-printing.printticket+xml"/>
    <Default Extension="JPG" ContentType="image/jpeg"/>
    <Default Extension="fpage" ContentType="application/vnd.ms-package.xps-fixedpage+xml"/>
    <Default Extension="dict" ContentType="application/vnd.ms-package.xps-resourcedictionary+xml"/>
</Types>

You could also check for the required FixedDocumentSequence.fdseq file.

Hopefully helpful/relevant links:

Adam Lear
  • 38,111
  • 12
  • 81
  • 101
  • Will try the fdseq but when I get near my pc later. I can't just check for content types as xps shares its file format with the ms office XML format so they all have a content type file. I'll also carry on researching the contents if content type. – making Oct 14 '12 at 08:47