From 87776a4a4cbf7f3c44cf03258b7e24ec2d1ec72f Mon Sep 17 00:00:00 2001 From: crowbarmaster Date: Wed, 6 Oct 2021 14:47:08 -0400 Subject: [PATCH] Merged duplicate code used to clear temp zip directory into FileUtils.ClearTempDir. --- .../Server/PackParser/MinecraftPackParser.cs | 16 +++------------- BedrockService/Service/Utilities/FileUtils.cs | 11 +++++++++++ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/BedrockService/Service/Server/PackParser/MinecraftPackParser.cs b/BedrockService/Service/Server/PackParser/MinecraftPackParser.cs index a01237ab..560254fc 100644 --- a/BedrockService/Service/Server/PackParser/MinecraftPackParser.cs +++ b/BedrockService/Service/Server/PackParser/MinecraftPackParser.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.IO.Compression; +using BedrockService.Service.Utilities; namespace BedrockService.Service.Server.PackParser { @@ -33,13 +34,7 @@ public MinecraftPackParser(string serverPackPath) public MinecraftPackParser(byte[] fileContents) { - if (!PackExtractDirectory.Exists) - PackExtractDirectory.Create(); - else - foreach (FileInfo file in PackExtractDirectory.GetFiles("*", SearchOption.AllDirectories)) - file.Delete(); - foreach (DirectoryInfo directory in PackExtractDirectory.GetDirectories()) - directory.Delete(true); + FileUtils.ClearTempDir(); using (MemoryStream fileStream = new MemoryStream(fileContents, 5, fileContents.Length - 5)) { using (ZipArchive zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Read)) @@ -52,14 +47,9 @@ public MinecraftPackParser(byte[] fileContents) public MinecraftPackParser(string[] files) { - if (!PackExtractDirectory.Exists) - PackExtractDirectory.Create(); + FileUtils.ClearTempDir(); if (Directory.Exists($@"{PackExtractDirectory.FullName}\ZipTemp")) Directory.CreateDirectory($@"{PackExtractDirectory.FullName}\ZipTemp"); - foreach (FileInfo file in PackExtractDirectory.GetFiles("*", SearchOption.AllDirectories)) - file.Delete(); - foreach (DirectoryInfo directory in PackExtractDirectory.GetDirectories()) - directory.Delete(true); foreach (string file in files) { FileInfo fInfo = new FileInfo(file); diff --git a/BedrockService/Service/Utilities/FileUtils.cs b/BedrockService/Service/Utilities/FileUtils.cs index 5033a29b..1f4715d9 100644 --- a/BedrockService/Service/Utilities/FileUtils.cs +++ b/BedrockService/Service/Utilities/FileUtils.cs @@ -28,5 +28,16 @@ public static void DeleteFilesRecursively(DirectoryInfo source, bool removeSourc if (removeSourceFolder) source.Delete(true); } + + public static void ClearTempDir() + { + DirectoryInfo tempDirectory = new DirectoryInfo($@"{Program.ServiceDirectory}\Temp"); + if (!tempDirectory.Exists) + tempDirectory.Create(); + foreach (FileInfo file in tempDirectory.GetFiles("*", SearchOption.AllDirectories)) + file.Delete(); + foreach (DirectoryInfo directory in tempDirectory.GetDirectories()) + directory.Delete(true); + } } }