2025-03-09 18:22:52 +01:00
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2025-10-14 08:52:10 +01:00
|
|
|
namespace SharpCompress.Common.Arc;
|
|
|
|
|
|
|
|
|
|
public class ArcEntryHeader
|
2025-03-09 18:22:52 +01:00
|
|
|
{
|
2025-10-14 08:52:10 +01:00
|
|
|
public ArchiveEncoding ArchiveEncoding { get; }
|
|
|
|
|
public CompressionType CompressionMethod { get; private set; }
|
|
|
|
|
public string? Name { get; private set; }
|
|
|
|
|
public long CompressedSize { get; private set; }
|
|
|
|
|
public DateTime DateTime { get; private set; }
|
|
|
|
|
public int Crc16 { get; private set; }
|
|
|
|
|
public long OriginalSize { get; private set; }
|
|
|
|
|
public long DataStartPosition { get; private set; }
|
2025-03-09 18:22:52 +01:00
|
|
|
|
2025-10-14 08:52:10 +01:00
|
|
|
public ArcEntryHeader(ArchiveEncoding archiveEncoding)
|
|
|
|
|
{
|
2025-10-14 09:13:00 +01:00
|
|
|
ArchiveEncoding = archiveEncoding;
|
2025-10-14 08:52:10 +01:00
|
|
|
}
|
2025-03-09 18:22:52 +01:00
|
|
|
|
2025-10-14 08:52:10 +01:00
|
|
|
public ArcEntryHeader? ReadHeader(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
byte[] headerBytes = new byte[29];
|
|
|
|
|
if (stream.Read(headerBytes, 0, headerBytes.Length) != headerBytes.Length)
|
2025-03-09 18:22:52 +01:00
|
|
|
{
|
2025-10-14 08:52:10 +01:00
|
|
|
return null;
|
2025-03-09 18:22:52 +01:00
|
|
|
}
|
2025-10-14 08:52:10 +01:00
|
|
|
DataStartPosition = stream.Position;
|
|
|
|
|
return LoadFrom(headerBytes);
|
|
|
|
|
}
|
2025-03-09 18:22:52 +01:00
|
|
|
|
2025-10-14 08:52:10 +01:00
|
|
|
public ArcEntryHeader LoadFrom(byte[] headerBytes)
|
|
|
|
|
{
|
|
|
|
|
CompressionMethod = GetCompressionType(headerBytes[1]);
|
2025-03-09 18:22:52 +01:00
|
|
|
|
2025-10-14 08:52:10 +01:00
|
|
|
// Read name
|
|
|
|
|
int nameEnd = Array.IndexOf(headerBytes, (byte)0, 1); // Find null terminator
|
|
|
|
|
Name = Encoding.UTF8.GetString(headerBytes, 2, nameEnd > 0 ? nameEnd - 2 : 12);
|
2025-03-09 18:22:52 +01:00
|
|
|
|
2025-10-14 08:52:10 +01:00
|
|
|
int offset = 15;
|
|
|
|
|
CompressedSize = BitConverter.ToUInt32(headerBytes, offset);
|
|
|
|
|
offset += 4;
|
|
|
|
|
uint rawDateTime = BitConverter.ToUInt32(headerBytes, offset);
|
|
|
|
|
DateTime = ConvertToDateTime(rawDateTime);
|
|
|
|
|
offset += 4;
|
|
|
|
|
Crc16 = BitConverter.ToUInt16(headerBytes, offset);
|
|
|
|
|
offset += 2;
|
|
|
|
|
OriginalSize = BitConverter.ToUInt32(headerBytes, offset);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2025-03-09 18:22:52 +01:00
|
|
|
|
2025-10-14 10:20:04 +01:00
|
|
|
private CompressionType GetCompressionType(byte value) =>
|
|
|
|
|
value switch
|
|
|
|
|
{
|
|
|
|
|
1 or 2 => CompressionType.None,
|
|
|
|
|
3 => CompressionType.RLE90,
|
|
|
|
|
4 => CompressionType.Squeezed,
|
|
|
|
|
5 or 6 or 7 or 8 => CompressionType.Crunched,
|
|
|
|
|
9 => CompressionType.Squashed,
|
|
|
|
|
10 => CompressionType.Crushed,
|
|
|
|
|
11 => CompressionType.Distilled,
|
|
|
|
|
_ => CompressionType.Unknown,
|
|
|
|
|
};
|
2025-03-09 18:22:52 +01:00
|
|
|
|
2025-10-14 10:20:04 +01:00
|
|
|
public static DateTime ConvertToDateTime(long rawDateTime) =>
|
|
|
|
|
|
|
|
|
|
// Convert Unix timestamp to DateTime (UTC)
|
|
|
|
|
DateTimeOffset.FromUnixTimeSeconds(rawDateTime).UtcDateTime;
|
2025-10-14 08:52:38 +01:00
|
|
|
}
|