1 year ago

#343531

test-img

TranceAddict

AccessDeniedException when trying to zip folder

I want to zip a directory and it is content and download with REST request, but i get java.nio.file.AccessDeniedException error when i copy the directory.

public void downloadZipFile(String folderName, HttpServletResponse response) {
    response.setContentType("application/zip");
    response.setHeader("Content-Disposition", "attachment; filename=download.zip");

        try (ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream())) {
            FileSystemResource fileSystemResource = new FileSystemResource(folderName);
            ZipEntry zipEntry = new ZipEntry(Objects.requireNonNull(fileSystemResource.getFilename()));
            zipEntry.setSize(fileSystemResource.contentLength());
            zipEntry.setTime(System.currentTimeMillis());

            zipOutputStream.putNextEntry(zipEntry);
            
            //The error is thrown here
            StreamUtils.copy(fileSystemResource.getInputStream(), zipOutputStream);

            zipOutputStream.closeEntry();

            zipOutputStream.finish();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

folderName:

src\main\resources\images\marker-1

java

spring-boot

java.nio.file

0 Answers

Your Answer

Accepted video resources