2019-01-24 08:43:03 +00:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2022-05-30 08:42:27 +00:00
|
|
|
|
using System.Buffers;
|
2017-07-26 11:22:02 +00:00
|
|
|
|
using System.Collections.Generic;
|
2016-10-04 20:29:08 +00:00
|
|
|
|
using System.IO;
|
2016-10-04 21:08:43 +00:00
|
|
|
|
using System.Linq;
|
2022-05-30 08:42:27 +00:00
|
|
|
|
using Microsoft.Toolkit.HighPerformance;
|
|
|
|
|
using osu.Framework.Extensions;
|
2019-10-30 10:33:54 +00:00
|
|
|
|
using osu.Framework.IO.Stores;
|
2017-11-19 04:46:51 +00:00
|
|
|
|
using SharpCompress.Archives.Zip;
|
2022-05-30 08:42:27 +00:00
|
|
|
|
using SixLabors.ImageSharp.Memory;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-02-15 03:56:22 +00:00
|
|
|
|
namespace osu.Game.IO.Archives
|
2016-10-14 03:33:58 +00:00
|
|
|
|
{
|
2018-02-15 03:56:22 +00:00
|
|
|
|
public sealed class ZipArchiveReader : ArchiveReader
|
2016-10-14 03:33:58 +00:00
|
|
|
|
{
|
2017-03-23 04:41:50 +00:00
|
|
|
|
private readonly Stream archiveStream;
|
2017-11-19 04:46:51 +00:00
|
|
|
|
private readonly ZipArchive archive;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-02-15 03:56:22 +00:00
|
|
|
|
public ZipArchiveReader(Stream archiveStream, string name = null)
|
2018-02-15 01:20:23 +00:00
|
|
|
|
: base(name)
|
2016-10-14 03:33:58 +00:00
|
|
|
|
{
|
2016-10-19 15:00:11 +00:00
|
|
|
|
this.archiveStream = archiveStream;
|
2017-11-19 04:46:51 +00:00
|
|
|
|
archive = ZipArchive.Open(archiveStream);
|
2016-10-04 20:29:08 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2016-11-05 11:00:14 +00:00
|
|
|
|
public override Stream GetStream(string name)
|
2016-10-04 20:29:08 +00:00
|
|
|
|
{
|
2017-11-19 04:46:51 +00:00
|
|
|
|
ZipArchiveEntry entry = archive.Entries.SingleOrDefault(e => e.Key == name);
|
2016-10-14 03:33:58 +00:00
|
|
|
|
if (entry == null)
|
|
|
|
|
throw new FileNotFoundException();
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-05-30 08:42:27 +00:00
|
|
|
|
var owner = MemoryAllocator.Default.Allocate<byte>((int)entry.Size);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-11-19 04:46:51 +00:00
|
|
|
|
using (Stream s = entry.OpenEntryStream())
|
2022-05-30 08:42:27 +00:00
|
|
|
|
s.ReadToFill(owner.Memory.Span);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-05-30 08:42:27 +00:00
|
|
|
|
return new MemoryOwnerMemoryStream(owner);
|
2016-10-04 20:29:08 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2016-10-18 17:35:01 +00:00
|
|
|
|
public override void Dispose()
|
2016-10-14 03:33:58 +00:00
|
|
|
|
{
|
|
|
|
|
archive.Dispose();
|
2016-10-19 15:00:11 +00:00
|
|
|
|
archiveStream.Dispose();
|
2016-10-10 13:26:34 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2019-10-30 10:33:54 +00:00
|
|
|
|
public override IEnumerable<string> Filenames => archive.Entries.Select(e => e.Key).ExcludeSystemFileNames();
|
2022-05-30 08:42:27 +00:00
|
|
|
|
|
|
|
|
|
private class MemoryOwnerMemoryStream : Stream
|
|
|
|
|
{
|
|
|
|
|
private readonly IMemoryOwner<byte> owner;
|
|
|
|
|
private readonly Stream stream;
|
|
|
|
|
|
|
|
|
|
public MemoryOwnerMemoryStream(IMemoryOwner<byte> owner)
|
|
|
|
|
{
|
|
|
|
|
this.owner = owner;
|
|
|
|
|
|
|
|
|
|
stream = owner.Memory.AsStream();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
owner?.Dispose();
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Flush() => stream.Flush();
|
|
|
|
|
|
|
|
|
|
public override int Read(byte[] buffer, int offset, int count) => stream.Read(buffer, offset, count);
|
|
|
|
|
|
|
|
|
|
public override long Seek(long offset, SeekOrigin origin) => stream.Seek(offset, origin);
|
|
|
|
|
|
|
|
|
|
public override void SetLength(long value) => stream.SetLength(value);
|
|
|
|
|
|
|
|
|
|
public override void Write(byte[] buffer, int offset, int count) => stream.Write(buffer, offset, count);
|
|
|
|
|
|
|
|
|
|
public override bool CanRead => stream.CanRead;
|
|
|
|
|
|
|
|
|
|
public override bool CanSeek => stream.CanSeek;
|
|
|
|
|
|
|
|
|
|
public override bool CanWrite => stream.CanWrite;
|
|
|
|
|
|
|
|
|
|
public override long Length => stream.Length;
|
|
|
|
|
|
|
|
|
|
public override long Position
|
|
|
|
|
{
|
|
|
|
|
get => stream.Position;
|
|
|
|
|
set => stream.Position = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-14 03:33:58 +00:00
|
|
|
|
}
|
2017-11-19 04:46:51 +00:00
|
|
|
|
}
|