Thursday, November 23rd, 2017

Display the contents of a Zip file using java

3

package com.org.servlet;

import java.io.*;

import java.util.zip.*;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class ZipView extends HttpServlet

{

/**

* Display the contents of a zip file

*/

private static final long serialVersionUID = 1L;

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

{

try

{

String zipFile = request.getParameter(“fileNames”);//specify the zip file

response.setContentType(“text/html”);

PrintWriter out = response.getWriter();

out.println(“<table><tr><th>File Name</th><th>Size</th><th>Date </th><th>Compressed Size</th></tr>”);

if(zipFile != null && zipFile.trim().length()>0){

//BufferedOutputStream out = null;

ZipInputStream  in = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));

ZipEntry entry;

while((entry = in.getNextEntry()) != null)

{                    out.println(“<tr><td>”+ entry.getName()+”</td><td>”+ entry.getSize()+”</td><td>”+ entry.getTime() +” </td><td>”+ entry.getCompressedSize() +” </td></tr>”);

int count;

byte data[] = new byte[1000];

out = new BufferedOutputStream(new FileOutputStream(“C:/pdf/out.txt”),1000);

while ((count = in.read(data,0,1000)) != -1)

{

out.write(data,0,count);

}

out.flush();

out.close();

*/

}

}

out.println(“</table>”);

out.flush();

out.close();

}

catch(Exception e)

{

e.printStackTrace();

}

}

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

doPost(request, response);

}

}

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • RSS
  • Technorati
  • Twitter

Comments

3 Responses to “Display the contents of a Zip file using java”
  1. Luckyboy SSVR says:

    How to include “com.org.servlet” package,

    Because, the error, cannot resolve symbol occurs in these lines,
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    • rajesh says:

      com.org.servlet is the package where the class is placed.

      for eg:- a.class is placed under classes/com/org/servlet/a.class then we need to give package com.org.servlet.

  2. Some time back I was coding a utility which would compare two directories, and sync them up with deleting the old entries.

    In that the requirement for zip files was a tricky one. You will have to check each entries in the zip file and compare the two version in two directories. You can just compare the dates of the zip files, but you will also have to check out the dates of the contents of the zip files and sync them also.

    It was a tough task. This article would really have benifitted me that time.

    Good Article !!

    thanks…

    /neer

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

Secure Linux Web Hosting