1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 14:17:26 +08:00

use ZipWriter

Export directly to stream instead of creating a archive
so we can cancel this anytime
This commit is contained in:
cdwcgt 2022-12-15 23:12:25 +09:00
parent ec251664a7
commit f5226bd50b
No known key found for this signature in database
GPG Key ID: 144396D01095C3A2

View File

@ -12,7 +12,9 @@ using osu.Game.Extensions;
using osu.Game.Overlays.Notifications; using osu.Game.Overlays.Notifications;
using osu.Game.Utils; using osu.Game.Utils;
using Realms; using Realms;
using SharpCompress.Archives.Zip; using SharpCompress.Common;
using SharpCompress.Writers;
using SharpCompress.Writers.Zip;
namespace osu.Game.Database namespace osu.Game.Database
{ {
@ -123,8 +125,6 @@ namespace osu.Game.Database
public abstract class LegacyArchiveExporter<TModel> : LegacyModelExporter<TModel> public abstract class LegacyArchiveExporter<TModel> : LegacyModelExporter<TModel>
where TModel : RealmObject, IHasNamedFiles, IHasGuidPrimaryKey where TModel : RealmObject, IHasNamedFiles, IHasGuidPrimaryKey
{ {
private bool canCancel = true;
protected LegacyArchiveExporter(Storage storage, RealmAccess realm) protected LegacyArchiveExporter(Storage storage, RealmAccess realm)
: base(storage, realm) : base(storage, realm)
{ {
@ -142,9 +142,7 @@ namespace osu.Game.Database
{ {
try try
{ {
notification.CancelRequested += () => canCancel; using (var writer = new ZipWriter(outputStream, new ZipWriterOptions(CompressionType.Deflate)))
using (var archive = ZipArchive.Create())
{ {
float i = 0; float i = 0;
@ -152,15 +150,11 @@ namespace osu.Game.Database
{ {
notification.CancellationToken.ThrowIfCancellationRequested(); notification.CancellationToken.ThrowIfCancellationRequested();
archive.AddEntry(file.Filename, UserFileStorage.GetStream(file.File.GetStoragePath())); writer.Write(file.Filename, UserFileStorage.GetStream(file.File.GetStoragePath()));
i++; i++;
notification.Progress = i / model.Files.Count(); notification.Progress = i / model.Files.Count();
notification.Text = $"Exporting... ({i}/{model.Files.Count()})"; notification.Text = $"Exporting... ({i}/{model.Files.Count()})";
} }
notification.Text = "Saving Zip Archive...";
canCancel = false;
archive.SaveTo(outputStream);
} }
} }
catch (OperationCanceledException) catch (OperationCanceledException)