0

Im trying to zip my created folder. Right now im testing localy and it create folder but after that i want to zip it.

This is my code for zip:

public static void pack(final String sourceDirPath, final String zipFilePath) throws IOException {
    Path p = Files.createFile(Paths.get(zipFilePath));
    try (ZipOutputStream zs = new ZipOutputStream(Files.newOutputStream(p))) {
        Path pp = Paths.get(sourceDirPath);
        Files.walk(pp).filter(path -> !Files.isDirectory(path)).forEach(path -> {
            ZipEntry zipEntry = new ZipEntry(pp.relativize(path).toString());
            try {
                zs.putNextEntry(zipEntry);
                Files.copy(path, zs);
                zs.closeEntry();
            } catch (IOException e) {
                System.err.println(e);
            }
        });
    } }

But im getting an error AccessDeniedException. Is there any option to zip created folder, i dont want to zip file because in that folder i will have subfolders, so i want to zip main folder. Any suggestion how can i achive that?

None
  • 8,817
  • 26
  • 96
  • 171

1 Answers1

1

According to: Getting "java.nio.file.AccessDeniedException" when trying to write to a folder

I think you should add the filename and the extension to your 'zipFilePath', for example: "C:\Users\XXXXX\Desktop\zippedFile.zip"

Mio
  • 175
  • 2
  • 11