• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to check if a docx, xlsx, or pptx file is password protected using apache POI?

 
Thileepa Asokan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My requirement is to just check if the MS Office 2007 document(docx,xlsx or pptx) is password protected or not. I plan to use apache POI for that.

For doc, xls and ppt documents, I am just trying to create an instance of HWPFDocument, HSSFWorkbook or HSLFSlideShow and checking for the EncryptedDocumentException thrown.

Sample code:


When I try a similar thing for docx, xlsx or pptx document, org.apache.poi.POIXMLException( org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13]") is thrown. I believe that this exception is thrown not only when the file is password protected but also when the file is invalid or corrupt. My guess is that the exception is thrown when trying to access the [Content_Types].xml after unzipping the file(package).

Now. my question again: Is there a more elegant way (other than checking for the org.apache.poi.POIXMLException) to validate if a docx, xlsx or pptx document is password protected?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and welcome to the JavaRanch.

Please tell us when you're post the same message to other websites. We allow it be you have to be forthright about it.
 
Thileepa Asokan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Wouter,

I posted it in another forum but the status has been showing as pending for more than a day( and is still in the same status) and so, I have registered in Java Ranch and posted my question here.

Hope I will get my question answered.

Thanks.

 
Thileepa Asokan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I will certainly declare it in the forum if I got my question answered.

Thanks again.
 
Tom Simmons
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you ever get an answer on this, I need to do exactly the same thing - namely check files that people are uploading to make sure they aren't passworded.


Tom
 
Thileepa Asokan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. No answer as of now..
 
Tom Simmons
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this thread I started on the poi mailing list, seems to be going somewhere and I'm getting good results.

http://thread.gmane.org/gmane.comp.jakarta.poi.user/17344

Tom
 
Tom Simmons
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Simmons wrote:Have a look at this thread I started on the poi mailing list, seems to be going somewhere and I'm getting good results.

http://thread.gmane.org/gmane.comp.jakarta.poi.user/17344

Tom


Thileepa,

I don't know if you have been keeping an eye on the above thread, however I have now successully been able to check XLS and XLSX files for passwords, including being able to handle large files within 1-2 seconds with tiny memory footprints.

Next stop is DOC and DOCX files.


Tom
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic