【Java开源代码栏目提醒】:网学会员,鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“UnZipServlet.java”一文,供大家参考学习!
import java.io.*;
import java.util.*;
import java.util.zip.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class UnZipServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String zipFileName = null;
String entryName = null;
ZipFile zipFile = null;
ZipEntry entry=null;
InputStream in;
BufferedInputStream bufIn;
String type;
OutputStream out;
int cur;
HttpSession session = request.getSession(true);
String values[];
try
{
values = request.getParameterValues("zipFile");
if((values!=null)&&(values.length>0))
{
zipFileName = values[0];
}
entryName = request.getPathInfo();
if(zipFileName != null)
{
zipFileName = getServletContext().getRealPath(zipFileName);
zipFile = new ZipFile(zipFileName);
session.setAttribute("zipFile",zipFile);
}
else
{
zipFile = (ZipFile) session.getAttribute("zipFile");
}
if((entryName != null)&&(entryName.length()>0))
{
//get rid of '/' for path info
entryName = entryName.substring(1);
}
if(zipFile != null)
entry = zipFile.getEntry(entryName);
if(entry != null)
{
response.setContentLength((int) entry.getSize());
type = getServletContext().getMimeType(entryName);
if(type == null) type = "application/octet-stream";
response.setContentType(type);
out = response.getOutputStream();
in = zipFile.getInputStream(entry);
bufIn = new BufferedInputStream(in);
while((cur=bufIn.read()) != -1)
{
out.write(cur);
}
out.close();
bufIn.close();
}
else
{
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
catch(Exception exp)
{
response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
}
}
public String getServletInfo()
{
return "Returns an entry in a zip file.";
}
}