OK, I have got the event approach working, and it seems be working well.

There is room for improvement, namely writing my own record parser so that it's 
not outputting what it's doing.

This is certainly low memory footprint and fast, I can feed it a 40-50 MB file 
and get a result in a second to two.

The code:

        System.out.println("XLS");
        
        Boolean password = false;

        System.out.println(new Date());
        try
        {
                FileInputStream fs = new FileInputStream("d:\\big w p.xls");
                POIFSFileSystem poifs = new POIFSFileSystem(fs);
                InputStream din = poifs.createDocumentInputStream("Workbook");
                HSSFRequest req = new HSSFRequest();
                req.addListener(new EventExample(), BOFRecord.sid);
                HSSFEventFactory factory = new HSSFEventFactory();
                factory.processEvents(req, din);
                fs.close();
                din.close();
        }
        catch (EncryptedDocumentException ede)
        {
                password = true;
        }
        finally
        {
                System.out.println(new Date());
                System.out.println("Password? " + password);
        }

This for now covers what I need to do with XLS/XLSX files, I've already 
completed password checking of PDFs.  

Now I need to look at Word (DOC/DOCX) files, sadly it never occurred to me that 
POI would be less advanced in this field.  Checking for a password in Excel 
files has not been easy, in fact I owe everything to Nick because there is no 
concise, complete reference that I have come across.  It seems that there is 
even less for checking Word files, if anyone has any info on this I would be 
very grateful.


Tom

-----Original Message-----
From: Simmons, Tom (GE Oil & Gas, VG) 
Sent: 12 September 2011 14:36
To: POI Users List
Subject: RE: How to check whether an XLSX has a read password

I'll give the event a try then.

I've been working with two files, and it appeared to be working, but to be 
honest I find (and I think many other do judging by the quantity of questions 
on the same topic) that things a pretty vague and poorly explained in the 
documentation.

I was happy that the isWriteProtected was correct, then you see 
question/answers talking about with or without encryption and it gets you to 
wondering if you've really got the correct solution.


Tom

-----Original Message-----
From: Nick Burch [mailto:[email protected]]
Sent: 12 September 2011 14:17
To: POI Users List
Subject: RE: How to check whether an XLSX has a read password

On Mon, 12 Sep 2011, Simmons, Tom (GE Oil & Gas, VG) wrote:
> I've now got...
>
>       System.out.println(new Date());
>       NPOIFSFileSystem npoiFS = new NPOIFSFileSystem( new File("d:\\big n 
> p.xls"));
>       System.out.println(new Date());
>       HSSFWorkbook wb = new HSSFWorkbook(npoiFS.getRoot(), true);
>       System.out.println(new Date());
>       Boolean check = wb. isWriteProtected();
>       System.out.println(check);

You could try with the event usermodel, and bail out once you hit a point where 
it has worked.

> I'm also no longer sure I'm using the correct test (isWiteProtected). 
> All I am looking for is to check that the workbook hasn't had a 
> password added that prevents it being loaded/opened.

I'd suggest you create two files, one which isn't protected and one that is (in 
the way you want to check for). Try with them and use that to verify!

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected] For additional 
commands, e-mail: [email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected] For additional 
commands, e-mail: [email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to