/
siman_coder
/
TaskMake
Обзор
Документация
Войти
/
siman_coder
/
TaskMake
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Main.java
115 строк
5 KB
siman_coder
Create: GameProgress.java, Main.java
26 июн 2026, 11:22
Верифицирован
26 июн 2026, 11:22
ccfa001
Код
Авторство
О чём код?
import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { public static void main(String[] args) { String savegamesPath = "D:/Games/savegames"; GameProgress progress1 = new GameProgress(100, 5, 10, 150.5); GameProgress progress2 = new GameProgress(85, 3, 7, 98.2); GameProgress progress3 = new GameProgress(120, 8, 15, 250.0); System.out.println("--- СОХРАНЕНИЕ ИГРЫ ---"); String save1 = savegamesPath + "/save1.bat"; String save2 = savegamesPath + "/save2.bat"; String save3 = savegamesPath + "/save3.dat"; saveGame(save1, progress1); saveGame(save2, progress2); saveGame(save3, progress3); List<String> filesToZip = new ArrayList<>(); filesToZip.add(save1); filesToZip.add(save2); filesToZip.add(save3); System.out.println("\n--- АРХИВАЦИЯ ---"); String zipPath = savegamesPath + "/saves.zip"; zipFiles(zipPath, filesToZip); // 5. Удаление файлов сохранений (кроме архива) System.out.println("\n--- УДАЛЕНИЕ ФАЙЛОВ ---"); deleteFiles(filesToZip); System.out.println("\n✅ Все операции выполнены успешно!"); } public static void saveGame(String filePath, GameProgress gameProgress) { // Используем try-with-resources для автоматического закрытия потоков try (FileOutputStream fos = new FileOutputStream(filePath); ObjectOutputStream oos = new ObjectOutputStream(fos)) { oos.writeObject(gameProgress); System.out.println("✓ Сохранение создано: " + filePath); } catch (IOException e) { System.err.println("✗ ОШИБКА при сохранении игры: " + e.getMessage()); } } public static void zipFiles(String zipPath, List<String> files) { // Используем буфер для оптимизации чтения/записи byte[] buffer = new byte[1024]; try (FileOutputStream fos = new FileOutputStream(zipPath); ZipOutputStream zos = new ZipOutputStream(fos)) { System.out.println("Создание архива: " + zipPath); for (String filePath : files) { File fileToZip = new File(filePath); // Проверяем, существует ли файл if (!fileToZip.exists()) { System.err.println("✗ Файл не найден: " + filePath); continue; } // Создаем ZipEntry с именем файла ZipEntry zipEntry = new ZipEntry(fileToZip.getName()); zos.putNextEntry(zipEntry); // Читаем файл и записываем в архив try (FileInputStream fis = new FileInputStream(fileToZip)) { int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } System.out.println("✓ Добавлен в архив: " + fileToZip.getName()); } catch (IOException e) { System.err.println("✗ ОШИБКА при чтении файла " + filePath + ": " + e.getMessage()); } zos.closeEntry(); } System.out.println("✓ Архив создан: " + zipPath); } catch (IOException e) { System.err.println("✗ ОШИБКА при создании архива: " + e.getMessage()); } } public static void deleteFiles(List<String> files) { int deletedCount = 0; for (String filePath : files) { File file = new File(filePath); if (file.exists()) { if (file.delete()) { System.out.println("✓ Удален файл: " + file.getName()); deletedCount++; } else { System.err.println("✗ Не удалось удалить файл: " + file.getName()); } } else { System.out.println("• Файл уже удален или не существует: " + file.getName()); } } System.out.println("Удалено файлов: " + deletedCount + " из " + files.size()); } }