diff --git a/game-app/game-core/src/main/java/games/strategy/engine/framework/map/file/system/loader/ZippedMapsExtractor.java b/game-app/game-core/src/main/java/games/strategy/engine/framework/map/file/system/loader/ZippedMapsExtractor.java index 1b73bb28af7..e53e8ecaf9d 100644 --- a/game-app/game-core/src/main/java/games/strategy/engine/framework/map/file/system/loader/ZippedMapsExtractor.java +++ b/game-app/game-core/src/main/java/games/strategy/engine/framework/map/file/system/loader/ZippedMapsExtractor.java @@ -19,7 +19,6 @@ import org.triplea.io.ZipExtractor; import org.triplea.io.ZipExtractor.FileSystemException; import org.triplea.io.ZipExtractor.ZipReadException; -import org.triplea.java.collections.CollectionUtils; import org.triplea.map.description.file.MapDescriptionYaml; /** @@ -125,68 +124,34 @@ public static Optional unzipMap(final Path mapZip) { mapZip.getFileName().toString().endsWith(".zip"), mapZip.toAbsolutePath()); try { - return unzipMapThrowing(mapZip); - } catch (final IOException e) { - log.warn("Error extracting file: {}, {}", mapZip.toAbsolutePath() + ", " + e.getMessage(), e); - return Optional.empty(); - } - } - - // unzip map - // if previous folder exists then back it up - private static Optional unzipMapThrowing(final Path mapZip) throws IOException { - // extraction target is important, it is the folder path we seek to create with - // extracted map contents. - final Path mapsFolder = ClientFileSystemHelper.getUserMapsFolder(); - final Path extractionTarget = - mapsFolder.resolve(computeExtractionFolderName(mapZip.getFileName().toString())); - - if (!GameRunner.headless() && Files.exists(extractionTarget)) { + // extraction target is important, it is the folder path we seek to create with + // extracted map contents. + final Path mapsFolder = ClientFileSystemHelper.getUserMapsFolder(); + final Path extractionTarget = + mapsFolder.resolve(computeExtractionFolderName(mapZip.getFileName().toString())); log.info( - "Skipping extraction of: {}, extraction target already exists: {}", + "Extracting map zip: {} -> {}", mapZip.toAbsolutePath(), extractionTarget.toAbsolutePath()); - return Optional.empty(); - } - - // if this is a bot, then we are updating the map - delete the old folder and extract a new. - if (GameRunner.headless() && Files.exists(extractionTarget)) { - log.info("Deleting old map folder: " + extractionTarget.toAbsolutePath()); FileUtils.deleteDirectory(extractionTarget); - } + ZipExtractor.unzipFile(mapZip, extractionTarget); - log.info( - "Extracting map zip: {} -> {}", mapZip.toAbsolutePath(), extractionTarget.toAbsolutePath()); - - // extract to a temp folder. - // If the temp folder then contains a single folder: - // -> move that single folder to extraction target and remove the temp folder - // If the temp folder contains many files: - // -> rename the temp folder to extraction target - final Path tempFolder = Files.createTempDirectory(mapsFolder, "map-unzip"); - ZipExtractor.unzipFile(mapZip, tempFolder); - final Collection files = FileUtils.listFiles(tempFolder); - if (files.size() == 1 && Files.isDirectory(CollectionUtils.getAny(files))) { - // temp folder contains a folder that contains all the map files - Files.move(CollectionUtils.getAny(files), extractionTarget); - Files.delete(tempFolder); - } else { - // temp folder contains all the map files. Rename the temp folder - Files.move(tempFolder, extractionTarget); - } - - // delete old properties file if they exists - final Path propertiesFile = - mapZip.resolveSibling(mapZip.getFileName().toString() + ".properties"); - if (Files.exists(propertiesFile)) { - FileUtils.delete(propertiesFile); - } + // delete old properties file if they exists + final Path propertiesFile = + mapZip.resolveSibling(mapZip.getFileName().toString() + ".properties"); + if (Files.exists(propertiesFile)) { + FileUtils.delete(propertiesFile); + } - final boolean successfullyExtracted = Files.exists(extractionTarget); - if (successfullyExtracted) { - Files.delete(mapZip); - return Optional.of(extractionTarget); - } else { + final boolean successfullyExtracted = Files.exists(extractionTarget); + if (successfullyExtracted) { + Files.delete(mapZip); + return Optional.of(extractionTarget); + } else { + return Optional.empty(); + } + } catch (final IOException e) { + log.warn("Error extracting file: {}, {}", mapZip.toAbsolutePath(), e.getMessage(), e); return Optional.empty(); } }