Code Search for Developers
 
 
  

ZipContentServlet.java from Kneobase at Krugle


Show ZipContentServlet.java syntax highlighted

package com.kneobase.web.util;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet that retrieves a <code>ZipEntry</code> from a zip file, 
 * via two parameters passed in http request
 * 
 * e.g.: http://myzipservlet?zipFile=my_file.zip&content=my_doc.pdf
 *     
 * @author patricio.keilty@colaborativa.net
 *
 */
public class ZipContentServlet extends HttpServlet {

	private static final String ZIP_FILE_PARAM = "zipFile";
	private static final String ZIP_FILE_CONTENT_PARAM = "content";
	
	private ServletContext _servletContext;

	public void doGet( HttpServletRequest request, HttpServletResponse response )
		throws ServletException, IOException {

		//get the filename from the "file" parameter
		String fileName = (String) request.getParameter( ZIP_FILE_PARAM );
		String contentName = (String) request.getParameter( ZIP_FILE_CONTENT_PARAM );

		if( fileName == null || fileName.equals( "" ))
			throw new ServletException( "Invalid or non-existent zipFile parameter in ZipContentServlet servlet." );

		if( contentName == null || contentName.equals( "" ))
			throw new ServletException( "Invalid or non-existent content parameter in ZipContentServlet servlet." );

		ZipFile zipFile = null;

		try {

			zipFile = new ZipFile( fileName );
			ZipEntry entry = zipFile.getEntry( contentName );

			if( entry != null ){

				ServletOutputStream outputStream = null;
				BufferedInputStream inputStream = null;

				try {
					outputStream = response.getOutputStream();

					//set response headers
					response.setContentType( _servletContext.getMimeType( contentName ));

					response.addHeader(
						"Content-Disposition",
						"attachment; filename=" + contentName );

					response.setContentLength((int) entry.getSize());

					inputStream =
						new BufferedInputStream( zipFile.getInputStream( entry ));

					byte[] buffer = new byte[1024];
					int bytesRead;

					while( ( bytesRead = inputStream.read( buffer )) != -1 ){
						outputStream.write( buffer, 0, bytesRead );
					}
				} finally {
					//close the input/output streams
					if( outputStream != null )
						outputStream.close();
					if( inputStream != null )
						inputStream.close();
				}
			} else {
				// error, no entry exists!
				throw new ServletException( "Non-existent " + contentName + " file inside " + fileName + "." ); 
			}
		} catch( IOException ioe ){
			throw new ServletException( ioe.getMessage() );
		} finally {
			if( zipFile != null )
				zipFile.close();
		}
	}

	public void doPost( HttpServletRequest request, HttpServletResponse response )
		throws ServletException, IOException {
		doGet(request, response);
	}

	/**
	 * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
	 */
	public void init( ServletConfig arg0 ) throws ServletException {
		super.init( arg0 );
		_servletContext = arg0.getServletContext();
	}

}



See more files for this project here

Kneobase

Kneobase is an enterprise search engine, based upon the Lucene search engine and the Spring framework. It allows to perform full-text search across many different content sources. It is highly adaptable out-of-the-box and has a pluggable architecture.

Project homepage: http://sourceforge.net/projects/kneobase
Programming language(s): Java,XML
License: other

  RelativePathConfigurator.java
  RobotLoggerPathConfigurator.java
  ZipContentServlet.java