Make zip from directory and download it In Spring without creating new files or directories

609 Views Asked by At

I have a sample directory called 'sample-directory'(in resources) with files and directories in it.

this is my endpoint:

public void downloadZipFile(@RequestBody final Parameters parameter, final HttpServletResponse response) throws FileReadingException, IOException, URISyntaxException {
    response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
    response.setHeader(CONTENT_DISPOSITION, HEADER_VALUE_STR);
    response.setStatus(HttpServletResponse.SC_OK);
    this.generateZipService.generateZipFile(parameter, response);
}

In my this.generateZipService.generateZipFile(parameter, resposne) method, I want to read all files from 'sample-directory', change some files here and download a zip file from this directory(includng all directories and files in it, including changes).

Can someone suggest me or give direction how can I do this?

I want to do it without creating new files or directories.

like this:

for all files {
   if(currentFile is "TEST1.xml") {
            change first Line here;
   }
   if(currentFile is "TEST2.xml") {
            change second Line here;
   }
}
Then zip full directory and download.

Also I don't want to use /src/main/resources or paths like that. instead I'm using getClass().getClassLoader().getResource("folderDirectlyInsideResources/otherPackages/file.txt");

I have started implementing this by:

FileOutputStream fout = new FileOutputStream("src/main/resources/generate.zip");
ZipOutputStream zout = new ZipOutputStream(fout);

File folder = new File("src/main/resources/docker-compose-zip");
File[] listOfFiles = folder.listFiles();

for (File file : listOfFiles) {
    ZipEntry ze = new ZipEntry(file.getName());
    zout.putNextEntry(ze);
    zout.closeEntry();
}
zout.close();

but it downloads generate.zip file which is not zip(it is .txt format maybe). But I'm using MediaType.APPLICATION_OCTET_STREAM_VALUE

0

There are 0 best solutions below