Skip to content

Commit 46900f9

Browse files
committed
Switch to async archive extraction methods in ArchiveExtraction
- Replaced synchronous `ReaderFactory.Open` with `ReaderFactory.OpenAsyncReader`. - Updated entry processing to use `MoveToNextEntryAsync` and `WriteEntryToDirectoryAsync`. - Improved non-blocking behavior for better performance.
1 parent e002a85 commit 46900f9

1 file changed

Lines changed: 20 additions & 28 deletions

File tree

src/PicView.Core/ArchiveHandling/ArchiveExtraction.cs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using PicView.Core.DebugTools;
22
using PicView.Core.FileHandling;
3-
using SharpCompress.Common;
43
using SharpCompress.Readers;
54

65
namespace PicView.Core.ArchiveHandling;
@@ -62,43 +61,36 @@ public static async Task<bool> ExtractArchiveAsync(string archivePath,
6261
}
6362

6463
await using var stream = File.OpenRead(archivePath);
65-
using var reader = ReaderFactory.Open(stream);
64+
await using var reader = await ReaderFactory.OpenAsyncReader(stream);
6665

6766
var count = 0;
68-
69-
await Task.Run(() =>
67+
68+
// Process each entry asynchronously to avoid blocking the thread
69+
while (await reader.MoveToNextEntryAsync())
7070
{
71-
// Process each entry asynchronously to avoid blocking the thread
72-
while (reader.MoveToNextEntry())
71+
if (reader.Entry.IsDirectory)
72+
{
73+
continue;
74+
}
75+
76+
// Extract only if the file is supported
77+
var entryFileName = reader.Entry.Key;
78+
if (entryFileName.IsSupported())
7379
{
74-
if (reader.Entry.IsDirectory)
75-
{
76-
continue;
77-
}
78-
79-
// Extract only if the file is supported
80-
var entryFileName = reader.Entry.Key;
81-
if (entryFileName.IsSupported())
82-
{
83-
reader.WriteEntryToDirectory(tempDirectory, new ExtractionOptions
84-
{
85-
ExtractFullPath = true,
86-
Overwrite = true
87-
});
80+
await reader.WriteEntryToDirectoryAsync(tempDirectory);
8881
#if DEBUG
89-
Console.WriteLine($"Extracted: {entryFileName}");
82+
Console.WriteLine($"Extracted: {entryFileName}");
9083
#endif
9184

92-
count++;
93-
}
94-
else
95-
{
85+
count++;
86+
}
87+
else
88+
{
9689
#if DEBUG
97-
Console.WriteLine($"Skipped unsupported file: {entryFileName}");
90+
Console.WriteLine($"Skipped unsupported file: {entryFileName}");
9891
#endif
99-
}
10092
}
101-
});
93+
}
10294

10395
if (count <= 0)
10496
{

0 commit comments

Comments
 (0)