I'm working migrating content from one DMS(Filenet) to another(Webcenter Content), in this process I encountered an excel file with .xls extension(with content type application/vnd.ms-excel) in Filenet.
I got the InputStream of the file and wrote(File IO Operations) it to a temp location beforing pushing it to Webcenter Content.
The problem is when I download and open the Filenet Version of the excel file F1.xls, It prompts me with unmatching file format and extension warming but still opens the file and display content.
But the version which I pushed into Webcenter Content (WCC.xls)does not behave the same way.
It prompts the same message but after ignoring the prompt it shows junk characters, and if I change the extension of WCC.xls to WCC.xlsx it display appears fine.
what can i do to identify such things at runtime, any help would be highly appreciated.
Here is the code snipped from my local
InputStream initialStream;
try {
initialStream = new FileInputStream(
new File("C:\\Users\\xxxxxx\\Desktop\\Utils\\YYYYY\\FN1.xls"));
FileOutputStream oStream = null;
oStream = new FileOutputStream("C:\\Users\\xxxxxx\\Desktop\\Utils\\YYYYY\\WCC1.xls");
FilenetConnectionUtil fCU = new FilenetConnectionUtil();
fCU. writeFileToTempLocation("xx","xx",initialStream,oStream);
oStream.flush();
oStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* Write Method*/
public void writeFileToTempLocation(String filename, String filepath, InputStream inStream,FileOutputStream oStream) throws IOException {
// FileOutputStream oStream = new FileOutputStream(filepath);
byte[] buffer = new byte[1024];
int n;
if(inStream != null) {
while ((n = inStream.read(buffer)) != -1) {
oStream.write(buffer);
}
}
oStream.flush();
}
Thanks, Rahul Dumpala
As .xslx is a zip format, the first two bytes (the magic cookie) should be "PK".
Path path = Paths.get(
"C:\\Users\\xxxxxx\\Desktop\\Utils\\YYYYY\\FN1.xls"));
// Or: Path path = file.toPath();
boolean isXlsx(Path path) {
try (InputStream in = Files.newInputStream(path)) {
byte[] magicCookie = new byte[2];
return in.read(magicCookie) == 2
&& magicCookie[0] == 'P'
&& magicCookie[1] == 'K';
} catch (IOException) {
return false;
}
}