'Download multiple files as zip using SpringBoot

i've been trying to download multiple files as a zip from the database using Spring Boot. I am using Mysql and i did store the file succesfully on the database, but i can t find any help on how to retrive a list of files put it in a zip and download it. I did manage to get files paths from database and download a zip file , but i would prefer getting some help in how to get the files directly from the database. This is the code i am using to get a zip file out of local files.

WebService:

@GetMapping("/downloadZipFile/demande/{demandeRef}")
    public void downloadZipFile(HttpServletResponse response,@PathVariable String demandeRef) throws IOException {
        List<String> listOfFileNames = new ArrayList<>();
        Demande d = demandeService.findByReference(demandeRef);
        for (DemandePieceJointe dmd : d.getDemandePieceJointes() ){
            listOfFileNames.add(dmd.getPath());
        }
        demandePieceJointeService.downloadZipFile(response, listOfFileNames);
    }

Service:

@Override
    public void downloadZipFile(HttpServletResponse response, List<String> name) throws IOException {
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=download.zip");
        ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
        for (String fileName : name) {
            File resource = new File(fileName);
            FileInputStream fileInputStream = new FileInputStream(resource);
            ZipEntry zipEntry = new ZipEntry(resource.getName());
            zipEntry.setSize(resource.getTotalSpace());
            zipOut.putNextEntry(zipEntry);
            StreamUtils.copy(fileInputStream, zipOut);
            zipOut.closeEntry();
        }
        zipOut.finish();
        zipOut.close();
        response.setStatus(HttpServletResponse.SC_OK);
        response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"\"");


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source