Javarush

Javarush

DZHIN
public void removeFiles(List<Path> pathList) throws Exception {
    // Проверяем существует ли zip файл
    if (!Files.isRegularFile(zipFile)) {
        throw new WrongZipFileException();
    }

    // Создаем временный файл
    Path tempZipFile = Files.createTempFile(null, null);

    try (ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(tempZipFile))) {
        try (ZipInputStream zipInputStream = new ZipInputStream(Files.newInputStream(zipFile))) {

            ZipEntry zipEntry = zipInputStream.getNextEntry();
            while (zipEntry != null) {

                Path archivedFile = Paths.get(zipEntry.getName());

                if (!pathList.contains(archivedFile)) {
                    String fileName = zipEntry.getName();
                    zipOutputStream.putNextEntry(new ZipEntry(fileName));

                    copyData(zipInputStream, zipOutputStream);

                    zipOutputStream.closeEntry();
                    zipInputStream.closeEntry();
                }
                else {
                    ConsoleHelper.writeMessage(String.format("Файл '%s' удален из архива.", archivedFile.toString()));
                }
                zipEntry = zipInputStream.getNextEntry();
            }
        }
    }

    // Перемещаем временный файл на место оригинального
    Files.move(tempZipFile, zipFile, StandardCopyOption.REPLACE_EXISTING);
}


Report Page