GroupDocs.Parser provides the functionality to detect a file type of container items by the detectFileType(FileTypeDetectionMode) method of the ContainerItem class.
The FileTypeDetectionMode parameter provides the ability to control file type detection:
Default. The file type is detected by the file extension; if the file extension isn’t recognized, the file type is detected by the file content.
Extension. The file type is detected only by the file extension.
Content. The file type is detected only by the file content.
Here are the steps to detect a file type of container items:
Create an instance of the Parser class for the initial document;
Call the getContainer method and obtain the collection of ContainerItem objects;
Check if the collection isn’t null (container extraction is supported for the document);
Iterate through the collection and call the detectFileType(FileTypeDetectionMode) method.
The following example shows how to detect a file type of container items:
constgroupdocs=require('@groupdocs/groupdocs.parser');// Create an instance of Parser class
constparser=newgroupdocs.Parser('sample.zip');try{// Extract attachments from the container
constattachments=parser.getContainer();// Check if container extraction is supported
if(attachments===null){console.log("Container extraction isn't supported");}else{// Iterate over attachments
constit=attachments.iterator();while(it.hasNext()){constitem=it.next();// Detect the file type
constfileType=item.detectFileType(groupdocs.FileTypeDetectionMode.Default);// Print the name and file type
console.log(`${item.getName()}: ${fileType}`);}}}finally{parser.close();}process.exit(0);
The output for sample.zip:
sample.docx: Microsoft Word Open XML Document (.docx)
sample.pdf: Portable Document Format File (.pdf)
images.pdf: Portable Document Format File (.pdf)
th.jpg: JPEG Image (.jpg)
1200px-RedPandaFullBody.JPG: JPEG Image (.jpg)
The returned FileType object also has getExtension() and getFileFormat() methods.
More resources
Free online document parser App
Along with the full-featured library we provide simple but powerful free Apps.
You are welcome to parse documents and extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our Free Online Document Parser App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
On this page
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.