In Java you can pass a url like “zip:zip_file!xml_file” and it will use the specified XML file inside the zip file.XmlUrlResolver does not have this functionality. Is there a way to do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is an open source solution to this. (Code for XmlZipResolver at xml reports.) It inherits from XmlUrlResolver to add this functionality.
It’s actually pretty simple to do this. The class XmlZipResolver inherits from XmlUrlResolver. So where you before created an XmlUrlResolver object to access an XML file, you instead create an XmlZipResolver object and you can then treat it as you would an XmlUrlResolver object. And this works for any url that XmlUrlResolver will handle using the additional code only if the url starts with zip: or jar: (a Java jar file is a zip file).
The key part is on the call to GetEntity where it will open the zip file and then get a stream to the requested xml file in the zip file. This code uses SharpZipLib for all zip file access.
After this everything is pretty straightforward where all the calls to member functions return from the stream of the embedded xml file. Because GetEntity() returns an object, if it returns a base XmlUrlResolver object then the methods in this class are not called. Therefore all remaining member functions are written specifically for the case of a file in a zip.
The one other item of not is the Stream returned is an object that holds three objects, the ZipFile, the Stream that is the zip file, and the stream that is the zip entry. This returned object inherits from Stream. For every call except Close() it just passes that same call to the zip entry stream object. But on a Close (and therefore indirectly on a Dispose), it closes all three objects.
Again, code for XmlZipResolver at xml reports.