views:

339

answers:

2

I want to upload a zip file in a jsp page and the user is sent to another jsp page after he selects and uploads the file.

In this second page I want to

1.)extract the uploaded zip file.

2.)read some content from the extracted file(s) and allow the user to change/edit them

3.)save the user changes in the files.

4.)zip the files again and save it in another destination.

Please suggest me a rough outline of a solution as I am stuck in the phase in which I get the uploaded file and re-direct the user to the second jsp.

A: 

You might be getting the uploaded zip file in a servlet.

For your step 1, extract the zip file contents using http://java.sun.com/j2se/1.4.2/docs/api/java/util/zip/ZipFile.html

Procedure is here http://java.sun.com/developer/technicalArticles/Programming/compression/

Example is here http://www.devx.com/getHelpOn/10MinuteSolution/20447

Step 2 and 3 are simple

Step 4 of creating zip again is available in http://java.sun.com/developer/technicalArticles/Programming/compression/ (See Zip.java in that page)

RaviG
A: 

I want to upload a zip file in a jsp page...

Use the following type of form:

<form action="upload" method="post" enctype="multipart/form-data">
    <input type="file">
    <input type="submit">
</form>

...and the user is sent to another jsp page after he selects and uploads the file.

Hold on. You shouldn't be using JSP for business logic. Raw Java code doesn't belong in a JSP file, but in a real Java class. Let the form submit to a Servlet, which in turn processes the file upload and finally forwards the request to a JSP page to display some result.

1) extract the uploaded zip file.

Use Apache Commons FileUpload to obtain the uploaded file. You can find code examples and tips&tricks(!) in respectively the User Guide and Frequently Asked Questions sections at their homepage.

You should end up with an InputStream containing the uploaded file.

Once having that, then just use java.util.zip.ZipInputStream API to read the uploaded file.

2) read some content from the extracted file(s) and allow the user to change/edit them

Get hold of the data in model objects (javabeans), store them in the request or session scope and use a JSP/EL to display model values in HTML input fields. E.g.

<input type="text" name="foo" value="${fn:escapeXml(bean.foo)}">

The JSTL fn:escapeXml is just there to prevent XSS.

3) save the user changes in the files.

Let the form submit to a servlet, gather the new input, write it to some file.

4) zip the files again and save it in another destination.

Use ZipOutputStream.


See also

BalusC
Thank you for the answer. I got it working!
sunnyj
You're welcome.
BalusC