mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
[SimpleSort] Improve archive write handling
This commit is contained in:
@@ -68,6 +68,8 @@
|
|||||||
<HintPath>..\packages\Mono.Data.Sqlite.Portable.1.0.3.5\lib\net4\System.Data.Portable.dll</HintPath>
|
<HintPath>..\packages\Mono.Data.Sqlite.Portable.1.0.3.5\lib\net4\System.Data.Portable.dll</HintPath>
|
||||||
<Private>True</Private>
|
<Private>True</Private>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="System.IO.Compression" />
|
||||||
|
<Reference Include="System.IO.Compression.FileSystem" />
|
||||||
<Reference Include="System.Transactions" />
|
<Reference Include="System.Transactions" />
|
||||||
<Reference Include="System.Transactions.Portable, Version=4.0.0.0, Culture=neutral, PublicKeyToken=59e704a76bc4613a, processorArchitecture=MSIL">
|
<Reference Include="System.Transactions.Portable, Version=4.0.0.0, Culture=neutral, PublicKeyToken=59e704a76bc4613a, processorArchitecture=MSIL">
|
||||||
<HintPath>..\packages\Mono.Data.Sqlite.Portable.1.0.3.5\lib\net4\System.Transactions.Portable.dll</HintPath>
|
<HintPath>..\packages\Mono.Data.Sqlite.Portable.1.0.3.5\lib\net4\System.Transactions.Portable.dll</HintPath>
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
using SharpCompress.Archive;
|
using SharpCompress.Archive;
|
||||||
using SharpCompress.Common;
|
using SharpCompress.Common;
|
||||||
using SharpCompress.Reader;
|
using SharpCompress.Reader;
|
||||||
using SharpCompress.Writer;
|
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.IO.Compression;
|
using System.IO.Compression;
|
||||||
@@ -17,67 +16,44 @@ namespace SabreTools.Helper
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="input">Input filename to be moved</param>
|
/// <param name="input">Input filename to be moved</param>
|
||||||
/// <param name="output">Output directory to build to</param>
|
/// <param name="output">Output directory to build to</param>
|
||||||
/// <param name="archiveType">Type of archive to attempt to write to</param>
|
|
||||||
/// <param name="rom">RomData representing the new information</param>
|
/// <param name="rom">RomData representing the new information</param>
|
||||||
public static void WriteFileToArchive(string input, string output, ArchiveType archiveType, RomData rom)
|
public static void WriteToArchive(string input, string output, RomData rom)
|
||||||
{
|
{
|
||||||
string archiveFileName = output + Path.DirectorySeparatorChar + rom.Game + ".zip";
|
string archiveFileName = output + Path.DirectorySeparatorChar + rom.Game + ".zip";
|
||||||
string singleFileName = output + Path.DirectorySeparatorChar + rom.Game + Path.DirectorySeparatorChar + rom.Name;
|
|
||||||
|
|
||||||
IWritableArchive outarchive = null;
|
ZipArchive outarchive = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!File.Exists(archiveFileName))
|
if (!File.Exists(archiveFileName))
|
||||||
{
|
{
|
||||||
outarchive = ArchiveFactory.Create(archiveType) as IWritableArchive;
|
outarchive = ZipFile.Open(archiveFileName, ZipArchiveMode.Create);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
outarchive = ArchiveFactory.Open(archiveFileName, Options.LookForHeader) as IWritableArchive;
|
outarchive = ZipFile.Open(archiveFileName, ZipArchiveMode.Update);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (File.Exists(input))
|
||||||
|
{
|
||||||
|
if (outarchive.Mode == ZipArchiveMode.Create || outarchive.GetEntry(rom.Name) == null)
|
||||||
|
{
|
||||||
|
outarchive.CreateEntryFromFile(input, rom.Name, CompressionLevel.Optimal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (Directory.Exists(input))
|
||||||
|
{
|
||||||
|
foreach (string file in Directory.EnumerateFiles(input, "*", SearchOption.AllDirectories))
|
||||||
|
{
|
||||||
|
if (outarchive.Mode == ZipArchiveMode.Create || outarchive.GetEntry(file) == null)
|
||||||
|
{
|
||||||
|
outarchive.CreateEntryFromFile(file, file, CompressionLevel.Optimal);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
outarchive.AddEntry(rom.Name, input);
|
|
||||||
outarchive.SaveTo(archiveFileName, new CompressionInfo { Type = CompressionType.Deflate });
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex);
|
Console.WriteLine(ex);
|
||||||
outarchive?.SaveTo(archiveFileName, new CompressionInfo { Type = CompressionType.Deflate });
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
outarchive?.Dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Copy a file either to an output archive or to an output folder
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="input">Input filename to be moved</param>
|
|
||||||
/// <param name="output">Output directory to build to</param>
|
|
||||||
/// <param name="archiveType">Type of archive to attempt to write to</param>
|
|
||||||
/// <param name="rom">RomData representing the new information</param>
|
|
||||||
public static void WriteFolderToArchive(string input, string output, ArchiveType archiveType)
|
|
||||||
{
|
|
||||||
string archiveFileName = output + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(input) + ".zip";
|
|
||||||
|
|
||||||
IWritableArchive outarchive = null;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (!File.Exists(archiveFileName))
|
|
||||||
{
|
|
||||||
outarchive = ArchiveFactory.Create(archiveType) as IWritableArchive;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
outarchive = ArchiveFactory.Open(archiveFileName, Options.LookForHeader) as IWritableArchive;
|
|
||||||
}
|
|
||||||
outarchive.AddAllFromDirectory(input, "*", SearchOption.AllDirectories);
|
|
||||||
outarchive.SaveTo(archiveFileName, new CompressionInfo { Type = CompressionType.Deflate });
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine(ex);
|
|
||||||
outarchive?.SaveTo(archiveFileName, new CompressionInfo { Type = CompressionType.Deflate });
|
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -144,10 +120,12 @@ namespace SabreTools.Helper
|
|||||||
DirectoryInfo di = Directory.CreateDirectory(tempdir);
|
DirectoryInfo di = Directory.CreateDirectory(tempdir);
|
||||||
|
|
||||||
// Extract all files to the temp directory
|
// Extract all files to the temp directory
|
||||||
IReader reader = archive.ExtractAllEntries();
|
using (IReader reader = archive.ExtractAllEntries())
|
||||||
|
{
|
||||||
reader.WriteAllToDirectory(tempdir, ExtractOptions.ExtractFullPath);
|
reader.WriteAllToDirectory(tempdir, ExtractOptions.ExtractFullPath);
|
||||||
encounteredErrors = false;
|
encounteredErrors = false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else if (at == ArchiveType.GZip && gz != ArchiveScanLevel.External)
|
else if (at == ArchiveType.GZip && gz != ArchiveScanLevel.External)
|
||||||
{
|
{
|
||||||
// Close the original archive handle
|
// Close the original archive handle
|
||||||
@@ -168,24 +146,19 @@ namespace SabreTools.Helper
|
|||||||
}
|
}
|
||||||
encounteredErrors = false;
|
encounteredErrors = false;
|
||||||
}
|
}
|
||||||
archive.Dispose();
|
|
||||||
}
|
}
|
||||||
catch (InvalidOperationException)
|
catch (InvalidOperationException)
|
||||||
{
|
{
|
||||||
encounteredErrors = true;
|
encounteredErrors = true;
|
||||||
if (archive != null)
|
|
||||||
{
|
|
||||||
archive.Dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
logger.Error(ex.ToString());
|
logger.Error(ex.ToString());
|
||||||
encounteredErrors = true;
|
encounteredErrors = true;
|
||||||
if (archive != null)
|
|
||||||
{
|
|
||||||
archive.Dispose();
|
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
archive?.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
return !encounteredErrors;
|
return !encounteredErrors;
|
||||||
|
|||||||
@@ -286,22 +286,18 @@ namespace SabreTools
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now process the output directory and write all to zipfiles
|
// Now one final delete of the temp directory
|
||||||
foreach (string dir in Directory.EnumerateDirectories(_outdir, "*", SearchOption.TopDirectoryOnly))
|
while (Directory.Exists(_tempdir))
|
||||||
{
|
{
|
||||||
ArchiveTools.WriteFolderToArchive(dir, _outdir, ArchiveType.Zip);
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Directory.Delete(dir, true);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.Error(ex.ToString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now one final delete of the temp directory
|
|
||||||
Directory.Delete(_tempdir, true);
|
Directory.Delete(_tempdir, true);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
@@ -363,8 +359,7 @@ namespace SabreTools
|
|||||||
foreach (RomData found in foundroms)
|
foreach (RomData found in foundroms)
|
||||||
{
|
{
|
||||||
_logger.Log("Matched name: " + found.Name);
|
_logger.Log("Matched name: " + found.Name);
|
||||||
string singleFileName = _outdir + Path.DirectorySeparatorChar + found.Game + Path.DirectorySeparatorChar + found.Name;
|
ArchiveTools.WriteToArchive(input, _outdir, found);
|
||||||
Output.CopyFileToNewLocation(input, singleFileName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now get the headerless file if it exists
|
// Now get the headerless file if it exists
|
||||||
@@ -388,20 +383,16 @@ namespace SabreTools
|
|||||||
_logger.User("File '" + newinput + "' had " + founddroms.Count + " matches in the DAT!");
|
_logger.User("File '" + newinput + "' had " + founddroms.Count + " matches in the DAT!");
|
||||||
foreach (RomData found in founddroms)
|
foreach (RomData found in founddroms)
|
||||||
{
|
{
|
||||||
_logger.Log("Matched name: " + found.Name);
|
|
||||||
|
|
||||||
// First output the headerless rom
|
// First output the headerless rom
|
||||||
string singleFileName = _outdir + Path.DirectorySeparatorChar + found.Game + Path.DirectorySeparatorChar + found.Name;
|
_logger.Log("Matched name: " + found.Name);
|
||||||
Output.CopyFileToNewLocation(newinput, singleFileName);
|
ArchiveTools.WriteToArchive(newinput, _outdir, found);
|
||||||
|
|
||||||
// Then output the headered rom (renamed)
|
// Then output the headered rom (renamed)
|
||||||
RomData newfound = found;
|
RomData newfound = found;
|
||||||
newfound.Name = Path.GetFileNameWithoutExtension(newfound.Name) + " (" + rom.CRC + ")" + Path.GetExtension(newfound.Name);
|
newfound.Name = Path.GetFileNameWithoutExtension(newfound.Name) + " (" + rom.CRC + ")" + Path.GetExtension(newfound.Name);
|
||||||
|
|
||||||
_logger.Log("Matched name: " + newfound.Name);
|
_logger.Log("Matched name: " + newfound.Name);
|
||||||
|
ArchiveTools.WriteToArchive(input, _outdir, newfound);
|
||||||
singleFileName = _outdir + Path.DirectorySeparatorChar + newfound.Game + Path.DirectorySeparatorChar + newfound.Name;
|
|
||||||
Output.CopyFileToNewLocation(input, singleFileName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now remove this temporary file
|
// Now remove this temporary file
|
||||||
@@ -414,9 +405,16 @@ namespace SabreTools
|
|||||||
|
|
||||||
// Remove the current file if we are in recursion so it's not picked up in the next step
|
// Remove the current file if we are in recursion so it's not picked up in the next step
|
||||||
if (recurse)
|
if (recurse)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
File.Delete(input);
|
File.Delete(input);
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.Error(ex.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// If no errors were encountered, we loop through the temp directory
|
// If no errors were encountered, we loop through the temp directory
|
||||||
if (!encounteredErrors)
|
if (!encounteredErrors)
|
||||||
|
|||||||
Reference in New Issue
Block a user