views:

44

answers:

2

I'm having problems setting the path of the zip file, X, in ZipFile zipfile = new ZipFile("X");.

I don't want to hardcode the path such that it becomes ZipFile zipfile = new ZipFile("C:/docs/data.zip");.
I want to do something like :

ZipFile zipfile = new ZipFile(getServletContext().getResourceAsStream("/WEB-INF/" + request.getAttribute("myFile").toString());

Where the path of the zip file is determined by the selection of the user. But, this gives an error, because this only works for InputStream.

Previously, I've already retrieved the multipart/form data and gotten the real path of the zip file:

String path = getServletContext().getRealPath("/WEB-INF");
UploadBean bean = new UploadBean();
bean.setFolderstore(path);
MultipartFormDataRequest multiPartRequest = new MultipartFormDataRequest(request);
bean.store(multiPartRequest); //store in WEB-INF

// get real path / name of zip file which is store in the WEB-INF
Hashtable files = multiPartRequest.getFiles();
UploadFile upFile = (UploadFile) files.get("file");
if (upFile != null) request.setAttribute("myFile", upFile.getFileName());

Any solutions to this?

+1  A: 

I don't understand why you don't use the real path that you already have.

Anyway, you can work with a ZipInputStream.

This way you can handle your file as a simple Stream. The only big differences are the getName() method and size() that you can't directly access. With a ZIS you will be able to read every entry.


Resources :

Colin Hebert
Thanks for your help!
+2  A: 

You can convert webcontent-relative paths to absolute disk file system paths in two ways:

  1. Just use ServletContext#getRealPath() as you previously already did.

    ZipFile zipfile = new ZipFile(getServletContext().getRealPath("/WEB-INF/" + request.getAttribute("myFile").toString()));
    
  2. Use ServletContext#getResource() instead. It returns an URL. Call getPath() on it.

    ZipFile zipfile = new ZipFile(getServletContext().getResource("/WEB-INF/" + request.getAttribute("myFile").toString()).getPath());
    

Way #1 is preferred.

BalusC
+1 for this solution. But like I said, I really don't understand why the question is asked whereas the solution is already in the snippet the OP gave.
Colin Hebert
@Colin: Some people only copypaste without actually understanding what each line of code does.
BalusC
Thanks for your help.