osu/osu.Game/IO/Archives/ZipArchiveReader.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

50 lines
1.4 KiB
C#
Raw Normal View History

// 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
using System.Collections.Generic;
using System.IO;
2016-10-04 21:08:43 +00:00
using System.Linq;
using osu.Framework.IO.Stores;
using SharpCompress.Archives.Zip;
2018-04-13 09:19:50 +00:00
namespace osu.Game.IO.Archives
{
public sealed class ZipArchiveReader : ArchiveReader
{
private readonly Stream archiveStream;
private readonly ZipArchive archive;
2018-04-13 09:19:50 +00:00
public ZipArchiveReader(Stream archiveStream, string name = null)
2018-02-15 01:20:23 +00:00
: base(name)
{
this.archiveStream = archiveStream;
archive = ZipArchive.Open(archiveStream);
}
2018-04-13 09:19:50 +00:00
2016-11-05 11:00:14 +00:00
public override Stream GetStream(string name)
{
ZipArchiveEntry entry = archive.Entries.SingleOrDefault(e => e.Key == name);
if (entry == null)
throw new FileNotFoundException();
2018-04-13 09:19:50 +00:00
// allow seeking
MemoryStream copy = new MemoryStream();
2018-04-13 09:19:50 +00:00
using (Stream s = entry.OpenEntryStream())
s.CopyTo(copy);
2018-04-13 09:19:50 +00:00
copy.Position = 0;
2018-04-13 09:19:50 +00:00
return copy;
}
2018-04-13 09:19:50 +00:00
2016-10-18 17:35:01 +00:00
public override void Dispose()
{
archive.Dispose();
archiveStream.Dispose();
}
2018-04-13 09:19:50 +00:00
public override IEnumerable<string> Filenames => archive.Entries.Select(e => e.Key).ExcludeSystemFileNames();
}
}