Add log file compression

This commit is contained in:
Matt Nadareski
2021-04-02 21:21:50 -07:00
parent da0bf64e94
commit 01bf3c9efb
3 changed files with 57 additions and 0 deletions

View File

@@ -160,6 +160,13 @@ namespace MPF.Data
/// <returns></returns>
public bool IsValid() => GenerateParameters() != null;
/// <summary>
/// Generate a list of all log files generated
/// </summary>
/// <param name="basePath">Base filename and path to use for checking</param>
/// <returns>List of all log file paths, empty otherwise</returns>
public List<string> GetLogFilePaths(string basePath) => new List<string>();
/// <summary>
/// Reset all special variables to have default values
/// </summary>

View File

@@ -478,6 +478,10 @@ namespace MPF.Data
resultProgress?.Report(Result.Failure("Writing could not complete!"));
}
// Conpress the logs, if required
if (Options.CompressLogFiles)
CompressLogFiles();
resultProgress?.Report(Result.Success("Submission information process complete!"));
return Result.Success();
}
@@ -1292,6 +1296,51 @@ namespace MPF.Data
return true;
}
/// <summary>
/// Compress log files to save space
/// </summary>
/// <returns>True if the process succeeded, false otherwise</returns>
private bool CompressLogFiles()
{
// Prepare the necessary paths
string outputFilename = Path.GetFileNameWithoutExtension(OutputFilename);
string combinedBase = Path.Combine(OutputDirectory, outputFilename);
string archiveName = Path.Combine(OutputDirectory, "!dumpinfo.zip");
// Get the list of log files from the parameters object
var files = Parameters.GetLogFilePaths(combinedBase);
if (!files.Any())
return true;
// Add the log files to the archive and delete the uncompressed file after
ZipArchive zf = null;
try
{
zf = ZipFile.Open(archiveName, ZipArchiveMode.Create);
foreach (string file in files)
{
string entryName = file.Substring(OutputDirectory.Length).TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
zf.CreateEntryFromFile(file, entryName);
try
{
File.Delete(file);
}
catch { }
}
return true;
}
catch
{
return false;
}
finally
{
zf?.Dispose();
}
}
#endregion
#region Information Extraction

View File

@@ -71,6 +71,7 @@
<IncludeAssets>runtime; compile; build; native; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.IO.Compression" Version="4.3.0" />
<PackageReference Include="System.Management" Version="5.0.0" />
</ItemGroup>