Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: OLE and WORD docs

by afoken (Parson)
on Jul 11, 2009 at 19:03 UTC ( #779241=note: print w/ replies, xml ) Need Help??


in reply to Re: OLE and WORD docs
in thread OLE and WORD docs

Magic numbers do not solve the problem completely. Old versions of MS Word use a file format that is also used by other products. For example, old Crystal Reports files are often misidentified as MS Word files.

But the OP gave a nice clue: *.docx -- that means a newer MS Word, stuffing XML into a ZIP file. So, if we talk ONLY about newer MS Word files in zipped XML format, the file can be testet easily: Try to open the file using Archive::ZIP (it is a ZIP file, after all). Look inside the archive, try to find an XML file with the name MS Word uses for the content, unpack that file from the archive. Use an XML parser to see if the file has one of the well-known type declarations for MS Word (or similar products, if you want to allow OpenOffice and friends). Should any of those steps fail, the input file is not a valid *.docx MS Word file.

If also old MS Word files (*.doc) have to be processed, you need a second test routine that can properly detect the old MS Word binary dump formats. There are several, one for each version. Testing "magic numbers" works well most of the times, but you may have false positives (see above).

Word can also read and write Rich Text Format files (*.rtf). If this format has to be processed, you need a third test. Again, "magic numbers" may work here. CPAN has some RTF readers, using them to test for a valid file should give less false positives.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)


Comment on Re^2: OLE and WORD docs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://779241]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2014-10-01 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (386 votes), past polls