Free Support Forum - aspose.com

How to correctly detect filetype for a password protected Excel file

Hello,
I am using Aspose cells java library. I get a file as input through email. It could be a word document “.docx” or it could be a password protected “.xlsx” file or a pdf file. I will not know previously which file is being sent. I need to correctly identify file type. When A password p protected xlsx file is sent it identfies it as word file.
thhis is code i am using:

com.aspose.words.FileFormatInfo fileInfo = null;
try {
fileInfo = FileFormatUtil.detectFileFormat(filePath);
logger.debug(" file formatInfo : "
+ FileFormatUtil.loadFormatToExtension(fileInfo.getLoadFormat()));
return FileFormatUtil.loadFormatToExtension(fileInfo.getLoadFormat());
} catch (Exception e) {
logger.error(“Error finding word filetype;” + e.getMessage());
}

@shashimn,
Could you replace the first line with below code as it is showing expected output for password protected xlsx file.

com.aspose.cells.FileFormatInfo fileInfo = null;

Let us know your feedback.

Thanks for the response. I know if I replace that line with what you told will work. For that I first need to know that I am processing an excel file, I will not have that information.
Let me explain my problem again.
In my application I may receive via email either a .docx , pdf or .xlsx (passwrod protected) file. I will not know before hand which file i will receive. I need to programmatically find the file type and then call corresponding Aspose library. Is there a common method or something like that in any aspose library that will take as input a file and tell me whether the file type is word-document / PDF / Excel file?

@shashimn,
There is no common Aspose API call to perform this task. Although it is possible to identify the file type based on the file extension but as in your environment it is not feasible so this solution may not work for you. Other option is that you may write your own logic to achieve this functionality by checking few bytes in the password protected file as shown in the following article:


I am afraid that no such document is available with Aspose which can be shared for this requirement.

@shashimn,
As mentioned earlier that no specific common method is available to detect file types, however you can determine whether input document is valid say PDF or not.

FileStream inputfile = new FileStream(@“C:\pdftest\New Microsoft PowerPoint
Presentation.pptx”, FileMode.Open);
try
{
//Aspose.PDF
Document doc = new Document(inputfile);
Console.WriteLine("Input document is proper PDf");
}
catch (Aspose.Pdf.Exceptions.InvalidPdfFileFormatException ex)
{
Console.WriteLine("Source file is not PDF. " + ex.Message);
} 

If it fails for PDF, you may load the incoming file into workbook and see if loads it or not. If it is any of the valid spread sheet type, it will be loaded otherwise it will raise exception and then you may load it to Words document. Although it is little time consuming but just shared for your reference.