Checking File Type using a Java Stream?

Hello,

In Aspose for Java, is there a way to detect the file type using only a stream (ex. ByteArrayInputStream, DataInputStream, etc.)? I am trying to convert to PDF with several different file types and I need to do it in memory with streams, not files.

@jmmorgan,

Regarding Aspose.Cells for Java to detect MS Excel spreadsheets or other file formats, you may try to use FileFormatUtil.detectFileFormat(InputStream stream) overload for your requirements.
e.g.
Sample code:

File file = new File("f:\\files\\Book1.xlsx");
byte[] array = Files.readAllBytes(file.toPath());
ByteArrayInputStream stream = new ByteArrayInputStream(array);
// Detect file format from streams
FileFormatInfo info = FileFormatUtil.detectFileFormat(stream);

if(info.getFileFormatType() == FileFormatType.XLSX)
{
            System.out.print("this is XLSX");
}
else if(info.getFileFormatType() == FileFormatType.ODS)
{
           System.out.print("this is ODS");
}
else if(info.getFileFormatType() == FileFormatType.XLSM)
{
          System.out.print("this is XLSM");
}
else if(info.getFileFormatType() == FileFormatType.DOCX)
{
            System.out.print("this is DOCX");
}
//.......

Also, see the documents for your reference.
https://docs.aspose.com/cells/java/detect-file-format-of-encrypted-office-open-xml-ooxml-files/
https://docs.aspose.com/cells/java/how-to-detect-a-file-format-and-check-if-the-file-is-encrypted/

Hope, this helps a bit.

Moreover, regarding other Aspose for Java APIs (e.g., Aspose.Words, Aspose.PDF, Aspose.Slides), our colleagues from other teams will give details and assist you for your requirements soon.

@jmmorgan,
You can use Aspose.Slides for Java to detect the file type of a presentation using the PresentationFactory class like this:

var info = PresentationFactory.getInstance().getPresentationInfo(inputStream);

// Returns a value from the LoadFormat enumeration.
var fileFormat = info.getLoadFormat(); 

System.out.println(fileFormat);

@jmmorgan You can use FileFormatUtil.detectFileFormat method to detect the document format using Aspose.Words. Please see our documentation for more information:
https://docs.aspose.com/words/java/detect-file-format-and-check-format-compatibility/