/
Shcod
/
java-core-IO_Serialization_ZipStream
Обзор
Документация
Войти
/
Shcod
/
java-core-IO_Serialization_ZipStream
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/service/GameService.java
135 строк
5 KB
Shcod
first commit
08 авг 2026, 05:56
08 авг 2026, 05:56
79fe5e1
Код
Авторство
О чём код?
package service; import game.GameProgress; import java.io.*; import java.time.LocalDateTime; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; public class GameService { private StringBuilder log = new StringBuilder(); private String ROOT = "D:\\JavaGame"; public void createFolder(String path) { File folder = new File(ROOT, path); writeLog(log, folder, folder.mkdir(), State.SAVE); } public void createFile(String path) { File file = new File(ROOT, path); try { writeLog(log, file, file.createNewFile(), State.SAVE); } catch (IOException e) { log.append("ERROR - ".concat(LocalDateTime.now().toString()).concat("ошибка установки").concat(file.getAbsolutePath())); throw new RuntimeException(e); } } public void writeLogFile(String title) { try (FileWriter writer = new FileWriter(ROOT + "\\temp\\temp.txt", true)) { writer.append("\n--------------------------------"); writer.append(title); writer.append("--------------------------------"); writer.append("\n\n"); writer.write(String.valueOf(log)); log = new StringBuilder(); } catch (IOException e) { throw new RuntimeException(e); } } public void saveGame(String path, GameProgress progress) { path = ROOT + path; try (FileOutputStream fos = new FileOutputStream(path); ObjectOutputStream ous = new ObjectOutputStream(fos)) { ous.writeObject(progress); log.append("INFO ") .append(LocalDateTime.now()) .append(" файл ").append(path) .append(" сохранен.\n"); } catch (IOException e) { log.append("ERROR ").append(LocalDateTime.now()).append(" ").append(e.getMessage()); throw new RuntimeException(e); } } public void zipFiles(String path, List<String> files) { path = ROOT + path; try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(path))) { for(int i = 0; i < files.size(); i++) { try(FileInputStream fis = new FileInputStream(files.get(i))) { ZipEntry entry = new ZipEntry("zip" + i + ".dat"); zos.putNextEntry(entry); zos.write(fis.readAllBytes()); zos.closeEntry(); zos.flush(); log.append("INFO ").append(LocalDateTime.now()) .append(" файл ").append(files.get(i)) .append(" добавлен в архив. ") .append(path) .append("\n"); } } for(String s : files) { deleteFile(s); } } catch (IOException e) { log.append("ERROR ").append(LocalDateTime.now()).append(" ").append(e.getMessage()); throw new RuntimeException(e); } } public void openZip(String archivePath, String unzipPath) { try (ZipInputStream zis = new ZipInputStream(new FileInputStream(archivePath))) { ZipEntry entry; String name; while ((entry = zis.getNextEntry()) != null) { name = entry.getName(); try (FileOutputStream fos = new FileOutputStream(unzipPath + "\\" + name)) { fos.write(zis.readAllBytes()); fos.flush(); zis.closeEntry(); log.append("INFO ").append(LocalDateTime.now()) .append(" файл ").append(name).append(" извлечен из архива\n"); } } } catch (IOException e) { log.append("ERROR ").append(LocalDateTime.now()).append(" ").append(e.getMessage()); throw new RuntimeException(e); } } public GameProgress openProgress(String path) { try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path))) { return (GameProgress) ois.readObject(); } catch (ClassNotFoundException | IOException e) { log.append("ERROR ").append(LocalDateTime.now()).append(" ").append(e.getMessage()); throw new RuntimeException(e); } } private void deleteFile(String path) { File file = new File(path); writeLog(log, file, file.delete(), State.DELETE); } private void writeLog(StringBuilder log, File file, boolean flag, State state) { String choice; switch (state) { case State.SAVE -> choice = flag ? " создан" : " не создан"; case State.DELETE -> choice = flag ? " удалён" : " не удалён"; default -> throw new IllegalStateException("Unexpected value: " + state); } log .append(flag ? "INFO " : "WARNING ") .append(LocalDateTime.now()) .append(file.isFile() ? " файл " : " папка ") .append(file.getAbsolutePath()) .append(choice) .append("\n"); } }