1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 04:49:40 +08:00
osu-lazer/osu.Game/IO/Archives/ZipArchiveReader.cs

51 lines
1.5 KiB
C#
Raw Normal View History

2018-01-05 19:21:19 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2016-12-06 17:56:20 +08:00
using System.Collections.Generic;
using System.IO;
2016-10-05 05:08:43 +08:00
using System.Linq;
using SharpCompress.Archives.Zip;
namespace osu.Game.IO.Archives
{
public sealed class ZipArchiveReader : ArchiveReader
{
private readonly Stream archiveStream;
private readonly ZipArchive archive;
public ZipArchiveReader(Stream archiveStream, string name = null)
2018-02-15 09:20:23 +08:00
: base(name)
{
this.archiveStream = archiveStream;
archive = ZipArchive.Open(archiveStream);
}
2016-11-05 19:00:14 +08:00
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;
}
2016-10-19 01:35:01 +08:00
public override void Dispose()
{
archive.Dispose();
archiveStream.Dispose();
}
2017-05-23 15:26:51 +08:00
public override IEnumerable<string> Filenames => archive.Entries.Select(e => e.Key).ToArray();
2017-05-23 15:26:51 +08:00
public override Stream GetUnderlyingStream() => archiveStream;
}
}