using System;
using System.Text;
using SabreTools.Data.Models.ZArchive;
namespace SabreTools.Data.Extensions
{
public static class ZArchiveExtensions
{
///
/// Retrieves the name of the specified node from the NameTable
///
/// Node in the file tree
/// ZArchive NameTable
/// UTF-8 string representing node's name
public static string? GetName(this FileDirectoryEntry node, NameTable nameTable)
{
// Check for a valid offset into the NameTable
uint nameOffset = node.NameOffsetAndTypeFlag & Constants.RootNode;
if (nameOffset == Constants.RootNode)
return null;
// Get the index into the name table
var index = Array.IndexOf(nameTable.NameTableOffsets, nameOffset);
if (index < 0)
return null;
// Get the name entry for the requested index
var nameEntry = nameTable.NameEntries[index];
if (nameEntry is null)
return null;
// Decode name to UTF-8
return Encoding.UTF8.GetString(nameEntry.NodeName);
}
}
}