I am trying to write a spring boot java code to compress a Directory using REST API call. I find many codes using plain Java, but I am unable to find a one in Spring boot Java. I tried the below code and its zipping the file but when i try to zip the directory it throws FilenotFound Exception. Could anyone please help me with a working code. I dont know where I am going wrong. I am a beginner to Spring boot.
@RequestMapping(value="/zip", produces="application/zip")
public void zipFiles(HttpServletResponse response) throws IOException {
response.setStatus(HttpServletResponse.SC_OK);
response.addHeader("Content-Disposition", "attachment; filename=\"compressedfile.zip\"");
ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
ArrayList<File> files = new ArrayList<>();
files.add(new File("C:\\Pavan\\zipfolder")); //(zip is working if(C:\\Pavan\\zipfolder\\dockerfile.txt)
for (File file : files) {
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
FileInputStream fileInputStream = new FileInputStream(file);
IOUtils.copy(fileInputStream, zipOutputStream);
fileInputStream.close();
zipOutputStream.closeEntry();
}
zipOutputStream.close();
}
Error: