0

I have my GWT project and I want to upload files to server. I want the following algorithm to work

1) create a folder with the name from "userNumber"

2) write uploaded files to this folder

3) add folder to zip

4) delete folder (if exists)

As I write on the server side I think it is just java problem. Here is my code. I can perform only the first and the second steps, i.e. I can create a folder and write files to this folder.

 @Override
    public String executeAction(HttpServletRequest request,
            List<FileItem> sessionFiles) throws UploadActionException {
        String response = "";
        userNumber = request.getParameter("userNumber");
        File f = new File(ConfAppServer.getRealContextPath() + "/edoc/"
                + userNumber);
        if (f.mkdir()) {
            System.out.println("Directory Created");
        } else {
            System.out.println("Directory is not created");
        }
        for (FileItem item : sessionFiles) {
            if (false == item.isFormField()) {
                try {
                    String extension = item.getName().substring(
                            item.getName().length() - 3);
                    File file = null;
                    file = new File(ConfAppServer.getRealContextPath()
                            + "/edoc/"
                            + userNumber
                            + System.getProperty("file.separator")
                            + item.getName().substring(0,
                                    item.getName().length() - 4) + "."
                            + extension);
                    item.write(file);

                    receivedFiles.put(item.getFieldName(), file);
                    receivedContentTypes.put(item.getFieldName(),
                            item.getContentType());

                    response += "<file-" + cont + "-field>"
                            + item.getFieldName() + "</file-" + cont
                            + "-field>\n";
                    response += "<file-" + cont + "-name>" + item.getName()
                            + "</file-" + cont + "-name>\n";
                    response += "<file-" + cont + "-size>" + item.getSize()
                            + "</file-" + cont + "-size>\n";
                    response += "<file-" + cont + "-type>"
                            + item.getContentType() + "</file-" + cont
                            + "type>\n";
                } catch (Exception e) {
                    throw new UploadActionException(e);
                }
            }
        }
        ZipUtils appZip = new ZipUtils(userNumber + ".zip",
                ConfAppServer.getRealContextPath() + "/edoc/" + userNumber);
        appZip.generateFileList(new File(ConfAppServer.getRealContextPath()
                + "/edoc/" + userNumber));
        appZip.zipIt(userNumber + ".zip");
        f.delete();

        removeSessionFileItems(request);

        return "<response>\n" + response + "</response>\n";
    }

Here you can find my ZipUtils class.

When I try to delete my folder, nothing happens. The delete() method doesn't work. Help me please!! My question is how to add folder to zip and then delete this folder.

Community
  • 1
  • 1
olgacosta
  • 1,076
  • 4
  • 15
  • 36

1 Answers1

0

Use java.nio.file. You won't have the hassle of manipulating the ZIP API yourself:

final Path pathToZip = Paths.get("path", "to", "your", "zip");

final URI uri = URI.create("jar:" + pathToZip.toUri());
final Map<String, ?> env = Collections.singletonMap("create", "true");

try (
    final FileSystem fs = FileSystems.getFileSystem(uri, env);
) {
    // write files into the zip here.
    // See javadoc for java.nio.file.Files and FileSystem.getPath()
}

More details on how to use the API here.

fge
  • 119,121
  • 33
  • 254
  • 329