move to own files and refactor UTF8 usage

This commit is contained in:
Adam Hathcock
2026-01-07 10:39:18 +00:00
parent bd3cda0617
commit 486fdf118b
5 changed files with 64 additions and 55 deletions

View File

@@ -11,55 +11,3 @@ public class ArchiveEncoding : IArchiveEncoding
public Encoding? Forced { get; set; }
public Func<byte[], int, int, string>? CustomDecoder { get; set; }
}
public interface IArchiveEncoding
{
/// <summary>
/// Default encoding to use when archive format doesn't specify one.
/// </summary>
public Encoding Default { get; set; }
/// <summary>
/// ArchiveEncoding used by encryption schemes which don't comply with RFC 2898.
/// </summary>
public Encoding Password { get; set; }
/// <summary>
/// Default encoding to use when archive format specifies UTF-8 encoding.
/// </summary>
public Encoding UTF8 { get; set; }
/// <summary>
/// Set this encoding when you want to force it for all encoding operations.
/// </summary>
public Encoding? Forced { get; set; }
/// <summary>
/// Set this when you want to use a custom method for all decoding operations.
/// </summary>
/// <returns>string Func(bytes, index, length)</returns>
public Func<byte[], int, int, string>? CustomDecoder { get; set; }
}
public static class ArchiveEncodingExtensions
{
public static Encoding GetEncoding(this IArchiveEncoding encoding) =>
encoding.Forced ?? encoding.Default;
public static Func<byte[], int, int, string> GetDecoder(this IArchiveEncoding encoding) =>
encoding.CustomDecoder
?? ((bytes, index, count) => encoding.GetEncoding().GetString(bytes, index, count));
public static byte[] Encode(this IArchiveEncoding encoding, string str) =>
encoding.Default.GetBytes(str);
public static string Decode(this IArchiveEncoding encoding, byte[] bytes) =>
encoding.Decode(bytes, 0, bytes.Length);
public static string Decode(
this IArchiveEncoding encoding,
byte[] bytes,
int start,
int length
) => encoding.GetDecoder()(bytes, start, length);
}

View File

@@ -0,0 +1,28 @@
using System;
using System.Text;
namespace SharpCompress.Common;
public static class ArchiveEncodingExtensions
{
extension(IArchiveEncoding encoding)
{
public Encoding GetEncoding(bool useUtf8 = false) =>
encoding.Forced ?? (useUtf8 ? encoding.UTF8 : encoding.Default);
public Func<byte[], int, int, string> GetDecoder(bool useUtf8) =>
encoding.CustomDecoder
?? (
(bytes, index, count) =>
encoding.GetEncoding(useUtf8).GetString(bytes, index, count)
);
public byte[] Encode(string str) => encoding.Default.GetBytes(str);
public string Decode(byte[] bytes, bool useUtf8 = false) =>
encoding.Decode(bytes, 0, bytes.Length, useUtf8);
public string Decode(byte[] bytes, int start, int length, bool useUtf8 = false) =>
encoding.GetDecoder(useUtf8)(bytes, start, length);
}
}

View File

@@ -0,0 +1,33 @@
using System;
using System.Text;
namespace SharpCompress.Common;
public interface IArchiveEncoding
{
/// <summary>
/// Default encoding to use when archive format doesn't specify one.
/// </summary>
public Encoding Default { get; set; }
/// <summary>
/// ArchiveEncoding used by encryption schemes which don't comply with RFC 2898.
/// </summary>
public Encoding Password { get; set; }
/// <summary>
/// Default encoding to use when archive format specifies UTF-8 encoding.
/// </summary>
public Encoding UTF8 { get; set; }
/// <summary>
/// Set this encoding when you want to force it for all encoding operations.
/// </summary>
public Encoding? Forced { get; set; }
/// <summary>
/// Set this when you want to use a custom method for all decoding operations.
/// </summary>
/// <returns>string Func(bytes, index, length)</returns>
public Func<byte[], int, int, string>? CustomDecoder { get; set; }
}

View File

@@ -41,8 +41,8 @@ internal class DirectoryEntryHeader : ZipFileEntry
if (Flags.HasFlag(HeaderFlags.Efs))
{
Name = ArchiveEncoding.DecodeUTF8(name);
Comment = ArchiveEncoding.DecodeUTF8(comment);
Name = ArchiveEncoding.Decode(name, true);
Comment = ArchiveEncoding.Decode(comment, true);
}
else
{

View File

@@ -33,7 +33,7 @@ internal class LocalEntryHeader : ZipFileEntry
if (Flags.HasFlag(HeaderFlags.Efs))
{
Name = ArchiveEncoding.DecodeUTF8(name);
Name = ArchiveEncoding.Decode(name, true);
}
else
{