tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Libbrecht <>
Subject About serving zips, here it is !
Date Sun, 04 Jun 2000 00:42:28 GMT

Hi there,

A month or so ago, I asked questions about serving zipped directories as
files... could find no good answer. In the meantime, I found and made it, 
here's the very tiny little servlet (note: it's complete) :

import javax.servlet.*; 
import javax.servlet.http.*; 
public class ServeFromResource extends HttpServlet { 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws IOException, ServletException 

	String resourceName = "public" + request.getPathInfo();
	String type = "?/?";
	if ( resourceName.endsWith(".html") || resourceName.endsWith(".htm") )
		type = "text/html";
	if ( resourceName.endsWith(".xml") )
		type = "text/xml";
	if ( resourceName.endsWith(".xsl") )
		type = "text/xsl";
	if ( resourceName.endsWith(".dtd") )
		type = "text/dtd";
	else if (resourceName.endsWith(".txt") || resourceName.endsWith(".text") )
		type = "text/plain";
	else if (resourceName.endsWith(".gif")  )
		type = "image/gif";
	else if (resourceName.endsWith(".jpeg") || resourceName.endsWith(".jpg") )
		type = "image/jpeg";
	System.out.println("Serving for resource : -->" + resourceName + "<--");
	InputStream in = getClass().getResourceAsStream( resourceName  );
	if ( in == null ) {
		System.out.println("-------- The resource did not exit. -------------");
	else {
	        OutputStream out = response.getOutputStream(); 
		int r=0;
		while( ( r = ) != -1 ) {
			out.write((byte) r);
		System.out.println("-------- Served " + resourceName);


Some remarks here:
- how to make it work ?? First make sure the zip-file you want to publish is in 
the classpath and that everything inside that zip file is contained in a 
top-level folder named "public". Then simply invoke that servlet with 
supplementary path the path to your desired file, inside the zip, not including 
the public path.
Something of the form:

- this can be viewed as a huge security hole as it serves any files accessible 
to the classpath. Well, not any, just the one starting at a directory called 
"public". I'd like some comments on the possibility of backtracking here along 
the folders hierarchy... I do not think that's true, but just in case...

- one drawback: no folder listing

- this has an advantage however: the zip utilities used here are the native ones 
of the VM which, I guess, are faster than the package. Just a guess.


Wishing you good disks saving
for your documentation...

View raw message