Skip to content

Usenet streaming with a WebDAV server and a SABnzbd-compatible API

C#

Added SevenZipUtil class to read archive entries.

nzbdav-dev2f460715e304
diff --git a/backend/Extensions/ObjectExtensions.cs b/backend/Extensions/ObjectExtensions.cs
index 4d98bb01..a3c3e12f 100644
--- a/backend/Extensions/ObjectExtensions.cs
+++ b/backend/Extensions/ObjectExtensions.cs
@@ -6,11 +6,19 @@ namespace NzbWebDAV.Extensions;
public static class ObjectExtensions
{
private static readonly JsonSerializerOptions Indented = new() { WriteIndented = true };
+ private const BindingFlags BindingAttr = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
public static object? GetReflectionProperty(this object obj, string propertyName)
{
var type = obj.GetType();
- var prop = type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
+ var prop = type.GetProperty(propertyName, BindingAttr);
+ return prop?.GetValue(obj);
+ }
+
+ public static object? GetReflectionField(this object obj, string fieldName)
+ {
+ var type = obj.GetType();
+ var prop = type.GetField(fieldName, BindingAttr);
return prop?.GetValue(obj);
}
diff --git a/backend/Extensions/SevenZipArchiveEntryExtensions.cs b/backend/Extensions/SevenZipArchiveEntryExtensions.cs
new file mode 100644
index 00000000..ca343758
--- /dev/null
+++ b/backend/Extensions/SevenZipArchiveEntryExtensions.cs
@@ -0,0 +1,34 @@
+using System.Collections;
+using SharpCompress.Archives.SevenZip;
+using SharpCompress.Common;
+
+namespace NzbWebDAV.Extensions;
+
+public static class SevenZipArchiveEntryExtensions
+{
+ public static CompressionType GetCompressionType(this SevenZipArchiveEntry entry)
+ {
+ try
+ {
+ return entry.CompressionType;
+ }
+ catch (NotImplementedException)
+ {
+ var coders = (IEnumerable?)entry
+ ?.GetReflectionProperty("FilePart")
+ ?.GetReflectionProperty("Folder")
+ ?.GetReflectionField("_coders");
+ var compressionMethodId = (ulong?)FirstOrDefault(coders)
+ ?.GetReflectionField("_methodId")
+ ?.GetReflectionField("_id");
+ return compressionMethodId == 0
+ ? CompressionType.None
+ : CompressionType.Unknown;
+ }
+ }
+
+ private static object? FirstOrDefault(IEnumerable? enumerable)
+ {
+ return enumerable?.Cast<object?>().FirstOrDefault();
+ }
+}
\ No newline at end of file
diff --git a/backend/Extensions/SevenZipArchiveExtensions.cs b/backend/Extensions/SevenZipArchiveExtensions.cs
new file mode 100644
index 00000000..44cd7bca
--- /dev/null
+++ b/backend/Extensions/SevenZipArchiveExtensions.cs
@@ -0,0 +1,14 @@
+using SharpCompress.Archives.SevenZip;
+
+namespace NzbWebDAV.Extensions;
+
+public static class SevenZipArchiveExtensions
+{
+ public static long GetEntryStartByteOffset(this SevenZipArchive archive, int index)
+ {
+ var database = archive?.GetReflectionField("_database");
+ var dataStartPosition = (long?)database?.GetReflectionField("_dataStartPosition");
+ var packStreamStartPositions = (List<long>?)database?.GetReflectionField("_packStreamStartPositions");
+ return dataStartPosition!.Value + packStreamStartPositions![index];
+ }
+}
\ No newline at end of file
diff --git a/backend/Utils/SevenZipUtil.cs b/backend/Utils/SevenZipUtil.cs
new file mode 100644
index 00000000..3e06adae
--- /dev/null
+++ b/backend/Utils/SevenZipUtil.cs
@@ -0,0 +1,34 @@
+using NzbWebDAV.Extensions;
+using NzbWebDAV.Models;
+using NzbWebDAV.Streams;
+using SharpCompress.Archives.SevenZip;
+using SharpCompress.Common;
+
+namespace NzbWebDAV.Utils;
+
+public static class SevenZipUtil
+{
+ public static async Task<List<SevenZipEntry>> GetSevenZipEntriesAsync(Stream stream, CancellationToken ct)
+ {
+ await using var cancellableStream = new CancellableStream(stream, ct);
+ return await Task.Run(() => GetSevenZipEntries(cancellableStream), ct);
+ }
+
+ public static List<SevenZipEntry> GetSevenZipEntries(Stream stream)
+ {
+ using var archive = SevenZipArchive.Open(stream);
+ return archive.Entries
+ .Where(x => !x.IsDirectory)
+ .Select((entry, index) => new SevenZipEntry(entry, archive, index))
+ .ToList();
+ }
+
+ public class SevenZipEntry(SevenZipArchiveEntry entry, SevenZipArchive archive, int index)
+ {
+ public string PathWithinArchive { get; } = entry.Key!;
+ public CompressionType CompressionType { get; } = entry.GetCompressionType();
+
+ public LongRange ByteRangeWithinArchive { get; } =
+ LongRange.FromStartAndSize(archive.GetEntryStartByteOffset(index), entry.Size);
+ }
+}
\ No newline at end of file