1
0

Fixed naming conventions

This commit is contained in:
2024-01-10 09:33:51 +01:00
parent 68dd0839eb
commit 9cd1344266
11 changed files with 218 additions and 241 deletions

View File

@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using AMWD.Common.Packing.Tar.Interfaces;
using AMWD.Common.Packing.Tar.Utils;
@@ -16,10 +14,10 @@ namespace AMWD.Common.Packing.Tar
/// </remarks>
public class TarReader
{
private readonly byte[] dataBuffer = new byte[512];
private readonly UsTarHeader header;
private readonly Stream inStream;
private long remainingBytesInFile;
private readonly byte[] _dataBuffer = new byte[512];
private readonly UsTarHeader _header;
private readonly Stream _inStream;
private long _remainingBytesInFile;
/// <summary>
/// Constructs TarReader object to read data from `tarredData` stream
@@ -27,14 +25,11 @@ namespace AMWD.Common.Packing.Tar
/// <param name="tarredData">A stream to read tar archive from</param>
public TarReader(Stream tarredData)
{
inStream = tarredData;
header = new UsTarHeader();
_inStream = tarredData;
_header = new UsTarHeader();
}
public ITarHeader FileInfo
{
get { return header; }
}
public ITarHeader FileInfo => _header;
/// <summary>
/// Read all files from an archive to a directory. It creates some child directories to
@@ -49,7 +44,7 @@ namespace AMWD.Common.Packing.Tar
/// to your business logic.
public void ReadToEnd(string destDirectory)
{
while (MoveNext(false))
while (MoveNext(skipData: false))
{
string fileNameFromArchive = FileInfo.FileName;
string totalPath = destDirectory + Path.DirectorySeparatorChar + fileNameFromArchive;
@@ -59,14 +54,14 @@ namespace AMWD.Common.Packing.Tar
Directory.CreateDirectory(totalPath);
continue;
}
// If record is a file
string fileName = Path.GetFileName(totalPath);
string directory = totalPath.Remove(totalPath.Length - fileName.Length);
Directory.CreateDirectory(directory);
using (FileStream file = File.Create(totalPath))
{
Read(file);
}
using FileStream file = File.Create(totalPath);
Read(file);
}
}
@@ -74,55 +69,57 @@ namespace AMWD.Common.Packing.Tar
/// Read data from a current file to a Stream.
/// </summary>
/// <param name="dataDestanation">A stream to read data to</param>
///
/// <seealso cref="MoveNext"/>
public void Read(Stream dataDestanation)
{
Debug.WriteLine("tar stream position Read in: " + inStream.Position);
Debug.WriteLine("tar stream position Read in: " + _inStream.Position);
int readBytes;
byte[] read;
while ((readBytes = Read(out read)) != -1)
while ((readBytes = Read(out byte[] read)) != -1)
{
Debug.WriteLine("tar stream position Read while(...) : " + inStream.Position);
Debug.WriteLine("tar stream position Read while(...) : " + _inStream.Position);
dataDestanation.Write(read, 0, readBytes);
}
Debug.WriteLine("tar stream position Read out: " + inStream.Position);
Debug.WriteLine("tar stream position Read out: " + _inStream.Position);
}
protected int Read(out byte[] buffer)
{
if (remainingBytesInFile == 0)
if (_remainingBytesInFile == 0)
{
buffer = null;
return -1;
}
int align512 = -1;
long toRead = remainingBytesInFile - 512;
long toRead = _remainingBytesInFile - 512;
if (toRead > 0)
{
toRead = 512;
}
else
{
align512 = 512 - (int)remainingBytesInFile;
toRead = remainingBytesInFile;
align512 = 512 - (int)_remainingBytesInFile;
toRead = _remainingBytesInFile;
}
int bytesRead = _inStream.Read(_dataBuffer, 0, (int)toRead);
_remainingBytesInFile -= bytesRead;
int bytesRead = inStream.Read(dataBuffer, 0, (int)toRead);
remainingBytesInFile -= bytesRead;
if (inStream.CanSeek && align512 > 0)
if (_inStream.CanSeek && align512 > 0)
{
inStream.Seek(align512, SeekOrigin.Current);
_inStream.Seek(align512, SeekOrigin.Current);
}
else
while (align512 > 0)
{
inStream.ReadByte();
_inStream.ReadByte();
--align512;
}
buffer = dataBuffer;
buffer = _dataBuffer;
return bytesRead;
}
@@ -135,7 +132,8 @@ namespace AMWD.Common.Packing.Tar
{
foreach (byte b in buffer)
{
if (b != 0) return false;
if (b != 0)
return false;
}
return true;
}
@@ -154,56 +152,52 @@ namespace AMWD.Common.Packing.Tar
/// <seealso cref="Read(Stream)"/>
public bool MoveNext(bool skipData)
{
Debug.WriteLine("tar stream position MoveNext in: " + inStream.Position);
if (remainingBytesInFile > 0)
Debug.WriteLine("tar stream position MoveNext in: " + _inStream.Position);
if (_remainingBytesInFile > 0)
{
if (!skipData)
{
throw new TarException(
"You are trying to change file while not all the data from the previous one was read. If you do want to skip files use skipData parameter set to true.");
}
// Skip to the end of file.
if (inStream.CanSeek)
if (_inStream.CanSeek)
{
long remainer = (remainingBytesInFile % 512);
inStream.Seek(remainingBytesInFile + (512 - (remainer == 0 ? 512 : remainer)), SeekOrigin.Current);
long remainer = _remainingBytesInFile % 512;
_inStream.Seek(_remainingBytesInFile + (512 - (remainer == 0 ? 512 : remainer)), SeekOrigin.Current);
}
else
{
byte[] buffer;
while (Read(out buffer) != -1)
{
}
while (Read(out _) != -1) ;
}
}
byte[] bytes = header.GetBytes();
byte[] bytes = _header.GetBytes();
int headerRead = inStream.Read(bytes, 0, header.HeaderSize);
int headerRead = _inStream.Read(bytes, 0, _header.HeaderSize);
if (headerRead < 512)
{
throw new TarException("Can not read header");
}
if (IsEmpty(bytes))
{
headerRead = inStream.Read(bytes, 0, header.HeaderSize);
headerRead = _inStream.Read(bytes, 0, _header.HeaderSize);
if (headerRead == 512 && IsEmpty(bytes))
{
Debug.WriteLine("tar stream position MoveNext out(false): " + inStream.Position);
Debug.WriteLine("tar stream position MoveNext out(false): " + _inStream.Position);
return false;
}
throw new TarException("Broken archive");
}
if (header.UpdateHeaderFromBytes())
if (_header.UpdateHeaderFromBytes())
{
throw new TarException("Checksum check failed");
}
remainingBytesInFile = header.SizeInBytes;
_remainingBytesInFile = _header.SizeInBytes;
Debug.WriteLine("tar stream position MoveNext out(true): " + inStream.Position);
Debug.WriteLine("tar stream position MoveNext out(true): " + _inStream.Position);
return true;
}
}