// Copyright (c) 2007-2018 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System.Collections.Generic; using System.IO; using System.Linq; using SharpCompress.Archives.Zip; namespace osu.Game.Beatmaps.IO { public sealed class OszArchiveReader : ArchiveReader { private readonly Stream archiveStream; private readonly ZipArchive archive; public OszArchiveReader(Stream archiveStream) { this.archiveStream = archiveStream; archive = ZipArchive.Open(archiveStream); } public override Stream GetStream(string name) { ZipArchiveEntry entry = archive.Entries.SingleOrDefault(e => e.Key == name); if (entry == null) throw new FileNotFoundException(); // allow seeking MemoryStream copy = new MemoryStream(); using (Stream s = entry.OpenEntryStream()) s.CopyTo(copy); copy.Position = 0; return copy; } public override void Dispose() { archive.Dispose(); archiveStream.Dispose(); } public override IEnumerable Filenames => archive.Entries.Select(e => e.Key).ToArray(); public override Stream GetUnderlyingStream() => archiveStream; } }