Fixed naming conventions
This commit is contained in:
@@ -13,11 +13,9 @@ namespace AMWD.Common.Packing.Ar
|
||||
{
|
||||
// Source: http://en.wikipedia.org/wiki/Ar_%28Unix%29
|
||||
|
||||
private readonly Stream inStream;
|
||||
private readonly List<ArFileInfoExtended> files = new();
|
||||
private readonly long streamStartPosition;
|
||||
|
||||
private static readonly DateTime unixEpoch = new(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
|
||||
private readonly Stream _inStream;
|
||||
private readonly List<ArFileInfoExtended> _files = new();
|
||||
private readonly long _streamStartPosition;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ArReader"/> class.
|
||||
@@ -28,8 +26,8 @@ namespace AMWD.Common.Packing.Ar
|
||||
if (!inStream.CanRead || !inStream.CanSeek)
|
||||
throw new ArgumentException("Stream not readable or seekable", nameof(inStream));
|
||||
|
||||
streamStartPosition = inStream.Position;
|
||||
this.inStream = inStream;
|
||||
_streamStartPosition = inStream.Position;
|
||||
_inStream = inStream;
|
||||
|
||||
Initialize();
|
||||
}
|
||||
@@ -38,9 +36,7 @@ namespace AMWD.Common.Packing.Ar
|
||||
/// Returns a list with all filenames of the archive.
|
||||
/// </summary>
|
||||
public IEnumerable<string> GetFileList()
|
||||
{
|
||||
return files.Select(fi => fi.FileName).ToList();
|
||||
}
|
||||
=> _files.Select(fi => fi.FileName).ToList();
|
||||
|
||||
/// <summary>
|
||||
/// Returns the file info of a specific file in the archive.
|
||||
@@ -48,7 +44,7 @@ namespace AMWD.Common.Packing.Ar
|
||||
/// <param name="fileName">The name of the specific file.</param>
|
||||
public ArFileInfo GetFileInfo(string fileName)
|
||||
{
|
||||
return files
|
||||
return _files
|
||||
.Where(fi => fi.FileName == fileName)
|
||||
.Select(fi => new ArFileInfo
|
||||
{
|
||||
@@ -72,23 +68,23 @@ namespace AMWD.Common.Packing.Ar
|
||||
if (!outStream.CanWrite)
|
||||
throw new ArgumentException("Stream not writable", nameof(outStream));
|
||||
|
||||
var info = files.Where(fi => fi.FileName == fileName).FirstOrDefault();
|
||||
var info = _files.Where(fi => fi.FileName == fileName).FirstOrDefault();
|
||||
if (info == null)
|
||||
return;
|
||||
|
||||
long bytesToRead = info.FileSize;
|
||||
byte[] buffer = new byte[1024 * 1024];
|
||||
|
||||
inStream.Seek(info.DataPosition, SeekOrigin.Begin);
|
||||
_inStream.Seek(info.DataPosition, SeekOrigin.Begin);
|
||||
while (bytesToRead > 0)
|
||||
{
|
||||
int readCount = (int)Math.Min(bytesToRead, buffer.Length);
|
||||
inStream.Read(buffer, 0, readCount);
|
||||
_inStream.Read(buffer, 0, readCount);
|
||||
outStream.Write(buffer, 0, readCount);
|
||||
|
||||
bytesToRead -= readCount;
|
||||
}
|
||||
inStream.Seek(streamStartPosition, SeekOrigin.Begin);
|
||||
_inStream.Seek(_streamStartPosition, SeekOrigin.Begin);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -98,7 +94,7 @@ namespace AMWD.Common.Packing.Ar
|
||||
/// <param name="destinationPath">The destination path on disk.</param>
|
||||
public void ReadFile(string fileName, string destinationPath)
|
||||
{
|
||||
var info = files.Where(fi => fi.FileName == fileName).FirstOrDefault();
|
||||
var info = _files.Where(fi => fi.FileName == fileName).FirstOrDefault();
|
||||
if (info == null)
|
||||
return;
|
||||
|
||||
@@ -112,7 +108,7 @@ namespace AMWD.Common.Packing.Ar
|
||||
private void Initialize()
|
||||
{
|
||||
// Reset stream
|
||||
inStream.Seek(streamStartPosition, SeekOrigin.Begin);
|
||||
_inStream.Seek(_streamStartPosition, SeekOrigin.Begin);
|
||||
|
||||
// Read header
|
||||
string header = ReadAsciiString(8);
|
||||
@@ -120,33 +116,33 @@ namespace AMWD.Common.Packing.Ar
|
||||
throw new FormatException("The file stream is no archive");
|
||||
|
||||
// Create file list
|
||||
while (inStream.Position < inStream.Length)
|
||||
while (_inStream.Position < _inStream.Length)
|
||||
{
|
||||
var info = ReadFileHeader();
|
||||
files.Add(info);
|
||||
_files.Add(info);
|
||||
|
||||
// Move stream behind file content
|
||||
inStream.Seek(info.FileSize, SeekOrigin.Current);
|
||||
_inStream.Seek(info.FileSize, SeekOrigin.Current);
|
||||
|
||||
// Align to even offsets (padded with LF bytes)
|
||||
if (inStream.Position % 2 != 0)
|
||||
inStream.Seek(1, SeekOrigin.Current);
|
||||
if (_inStream.Position % 2 != 0)
|
||||
_inStream.Seek(1, SeekOrigin.Current);
|
||||
}
|
||||
|
||||
// Reset stream
|
||||
inStream.Seek(streamStartPosition, SeekOrigin.Begin);
|
||||
_inStream.Seek(_streamStartPosition, SeekOrigin.Begin);
|
||||
}
|
||||
|
||||
private string ReadAsciiString(int byteCount)
|
||||
{
|
||||
byte[] buffer = new byte[byteCount];
|
||||
inStream.Read(buffer, 0, byteCount);
|
||||
_inStream.Read(buffer, 0, byteCount);
|
||||
return Encoding.ASCII.GetString(buffer);
|
||||
}
|
||||
|
||||
private ArFileInfoExtended ReadFileHeader()
|
||||
{
|
||||
long startPosition = inStream.Position;
|
||||
long startPosition = _inStream.Position;
|
||||
|
||||
string fileName = ReadAsciiString(16).Trim();
|
||||
|
||||
@@ -159,7 +155,7 @@ namespace AMWD.Common.Packing.Ar
|
||||
|
||||
// file magic
|
||||
byte[] magic = new byte[2];
|
||||
inStream.Read(magic, 0, magic.Length);
|
||||
_inStream.Read(magic, 0, magic.Length);
|
||||
|
||||
if (magic[0] != 0x60 || magic[1] != 0x0A) // `\n
|
||||
throw new FormatException("Invalid file magic");
|
||||
@@ -167,9 +163,9 @@ namespace AMWD.Common.Packing.Ar
|
||||
return new ArFileInfoExtended
|
||||
{
|
||||
HeaderPosition = startPosition,
|
||||
DataPosition = inStream.Position,
|
||||
DataPosition = _inStream.Position,
|
||||
FileName = fileName,
|
||||
ModifyTime = unixEpoch.AddSeconds(unixTimestamp),
|
||||
ModifyTime = DateTimeOffset.FromUnixTimeSeconds(unixTimestamp).DateTime,
|
||||
UserId = userId,
|
||||
GroupId = groupId,
|
||||
Mode = mode,
|
||||
|
||||
Reference in New Issue
Block a user