Skip to content

Commit

Permalink
Merged duplicate code used to clear temp zip directory into FileUtils…
Browse files Browse the repository at this point in the history
….ClearTempDir.
  • Loading branch information
crowbarmaster committed Oct 6, 2021
1 parent 120e5c7 commit 87776a4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
16 changes: 3 additions & 13 deletions BedrockService/Service/Server/PackParser/MinecraftPackParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using BedrockService.Service.Utilities;

namespace BedrockService.Service.Server.PackParser
{
Expand Down Expand Up @@ -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))
Expand All @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions BedrockService/Service/Utilities/FileUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit 87776a4

Please sign in to comment.