0

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

  • ZipOutputStream is a `Stream` object. Assuming you have sufficient memory in your computer, can you simply hold the files you want to unzip as streams? – Robert Harvey May 02 '22 at 14:00
  • Yes I'm trying to implement it with ZipOutPutStream but I couldn't manage despite many tries. Can someone give me a pseudocode or something like that? – Nikusha Ozashvili May 02 '22 at 14:04
  • Can you give us any context? "It doesn't work" is not a meaningful problem statement, and "I don't want to use files" seems like an arbitrary requirement. – Robert Harvey May 02 '22 at 14:08
  • If I create new files, I cannot deploy app, because in java we cannot change resources in jar. I edited the post. – Nikusha Ozashvili May 02 '22 at 14:09
  • Are you saying that whatever store you're deploying to does not allow the creation of new files? That seems like a pretty severe restriction. – Robert Harvey May 02 '22 at 14:11
  • Which store are you deploying to? Google Play? – Robert Harvey May 02 '22 at 14:12
  • Where do you plan on storing the unzipped files? – Robert Harvey May 02 '22 at 14:15
  • I don't know yet. I should thrieve to create solution that is fast and efficient. I don't want to create new files. Instead of this, I want to read sample directory, change some values while reading and add it to outputstream which will be downloaded. – Nikusha Ozashvili May 02 '22 at 14:17
  • What is the exact wording of the error message you are getting? – Robert Harvey May 02 '22 at 14:19
  • sample directory will be in jar resources. No errors, it downloads generate.zip file but it's not real zip, its txt and files aren't added. – Nikusha Ozashvili May 02 '22 at 14:19
  • Have you written the result you've created to a local file to make sure you're actually getting a zip file? According to your problem description, the problem is not zipping files; it is downloading the unzipped files without corrupting. So you problem may not actually be creating the zip files, and you can stop focusing on that if you can prove they really are zip files before you download them. – Robert Harvey May 02 '22 at 14:22

0 Answers0