/
SofiMut
/
installGame
Обзор
Документация
Войти
/
SofiMut
/
installGame
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Main.java
179 строк
7 KB
Софья Степанова
upload files
21 сен 2025, 12:00
21 сен 2025, 12:00
10f1bd8
Код
Авторство
О чём код?
import java.io.*; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; public class Main { public static void main(String[] args) throws IOException { StringBuilder sb = new StringBuilder(); // СОЗДАНИЕ ПАПОК И ФАЙЛОВ File dir = new File("C://Games"); if (dir.canWrite()) System.out.println("Доступно для записи"); File dir1 = new File("C://Games", "srs"); if (dir1.mkdirs()) sb.append("Создана папка: " + dir1 + "\n"); File s = new File("C://Games//srs", "main"); if (s.mkdirs()) sb.append("Создана папка: " + s + "\n"); File s1 = new File("C://Games//srs", "test"); if (s1.mkdirs()) sb.append("Создана папка: " + s1 + "\n"); File dir2 = new File("C://Games", "res"); if (dir2.mkdir()) sb.append("Создана папка: " + dir2 + "\n"); File r = new File("C://Games//res", "drawables"); if (r.mkdir()) sb.append("Создана папка: " + r + "\n"); File r1 = new File("C://Games//res", "vectors"); if (r1.mkdir()) sb.append("Создана папка: " + r1 + "\n"); File r2 = new File("C://Games//res", "icons"); if (r2.mkdir()) sb.append("Создана папка: " + r2 + "\n"); File dir3 = new File("C://Games", "saveGames"); if (dir3.mkdir()) sb.append("Создана папка: " + dir3 + "\n"); File dir4 = new File("C://Games", "temp"); if (dir4.mkdir()) sb.append("Создана папка: " + dir4 + "\n"); File myFile = new File("C://Games//srs//main//Main.java"); try { if (myFile.createNewFile()) sb.append("Создан файл: " + myFile + "\n"); } catch (IOException ex) { System.out.println(ex.getMessage()); } File myFile1 = new File("C://Games//srs//main//Utils.java"); try { if (myFile1.createNewFile()) sb.append("Создан файл: " + myFile1 + "\n"); } catch (IOException ex) { System.out.println(ex.getMessage()); } File myFile2 = new File("C://Games//temp//temp.txt"); try { if (myFile2.createNewFile()) sb.append("Создан файл: " + myFile2 + "\n"); } catch (IOException ex) { System.out.println(ex.getMessage()); } GameProgress game1 = new GameProgress(100, 100, 1, 0); GameProgress game2 = new GameProgress(90, 50, 2, 30); GameProgress game3 = new GameProgress(50, 35, 3, 35.5); // СОХРАНЕНИЕ ПРОГРЕССА ИГРЫ saveGame("C://Games//saveGames//game1.dat", game1); saveGame("C://Games//saveGames//game2.dat", game2); saveGame("C://Games//saveGames//game3.dat", game3); // СОЗДАНИЕ СПИСКА ССЫЛОК НА ПРОГРЕСС ИГРЫ String filePath1 = "C://Games//saveGames//game1.dat"; String filePath2 = "C://Games//saveGames//game2.dat"; String filePath3 = "C://Games//saveGames//game3.dat"; List<String> arrayList = Arrays.asList(filePath1, filePath2, filePath3); // АРХИВАЦИЯ И УДАЛЕНИЕ ВРЕМЕННЫХ ФАЙЛОВ zipFiles("C://Games//saveGames//zip.zip", arrayList); sb.append("Создан архив: C://Games//zip.zip\n"); File game1Dat = new File("C://Games//saveGames//game1.dat"); File game2Dat = new File("C://Games//saveGames//game2.dat"); File game3Dat = new File("C://Games//saveGames//game3.dat"); if (game1Dat.delete()) sb.append("Удален временный файл: " + game1Dat + "\n"); if (game2Dat.delete()) sb.append("Удален временный файл: " + game2Dat + "\n"); if (game3Dat.delete()) sb.append("Удален временный файл: " + game3Dat + "\n"); // РАСПАКОВКА И ВЫВОД ПРОГРЕССА В КОНСОЛЬ openZip("C://Games//saveGames//zip.zip", "C://Games//saveGames"); sb.append("Архив распакован в папку: C://Games//saveGames\n"); System.out.println(openProgress("C://Games//saveGames//packed_save3.dat")); // ЗАПИСЬ ЛОГА FileWriter writer = new FileWriter("C://Games//temp//temp.txt"); writer.write(sb.toString()); writer.close(); System.out.println("Запись завершена"); } private static void saveGame(String path, GameProgress game) { try (FileOutputStream fos = new FileOutputStream(path); ObjectOutputStream oos = new ObjectOutputStream(fos)) { oos.writeObject(game); } catch (Exception ex) { System.out.println(ex.getMessage()); } } private static void zipFiles(String pathZip, List<String> arrayList) { try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(pathZip), StandardCharsets.UTF_8)) { int count = 1; for (String arr : arrayList) { try (FileInputStream fis = new FileInputStream(arr)) { ZipEntry entry = new ZipEntry("packed_save" + count++ + ".dat"); zout.putNextEntry(entry); byte[] buffer = new byte[fis.available()]; fis.read(buffer); zout.write(buffer); zout.closeEntry(); } } } catch (Exception ex) { System.out.println(ex.getMessage()); } } private static void openZip(String pathZip, String path) { try (ZipInputStream zin = new ZipInputStream(new FileInputStream(pathZip))) { ZipEntry entry; String name; while ((entry = zin.getNextEntry()) != null) { name = entry.getName(); FileOutputStream fout = new FileOutputStream(path + "\\" + name); for (int c = zin.read(); c != -1; c = zin.read()) { fout.write(c); } fout.flush(); zin.closeEntry(); fout.close(); } } catch (Exception ex) { System.out.println(ex.getMessage()); } } private static GameProgress openProgress(String path) { GameProgress gameProgress = null; try (FileInputStream fis = new FileInputStream(path); ObjectInputStream ois = new ObjectInputStream(fis)) { gameProgress = (GameProgress) ois.readObject(); } catch (Exception ex) { System.out.println(ex.getMessage()); } return gameProgress; } }