2018-01-05 19:21:19 +08:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-02-07 12:59:30 +08:00
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
2016-12-06 17:56:20 +08:00
|
|
|
|
|
2017-07-26 19:22:02 +08:00
|
|
|
|
using System.Collections.Generic;
|
2016-10-05 04:29:08 +08:00
|
|
|
|
using System.IO;
|
2016-10-05 05:08:43 +08:00
|
|
|
|
using System.Linq;
|
2017-11-19 12:46:51 +08:00
|
|
|
|
using SharpCompress.Archives.Zip;
|
2016-10-05 04:29:08 +08:00
|
|
|
|
|
2018-02-15 11:56:22 +08:00
|
|
|
|
namespace osu.Game.IO.Archives
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
2018-02-15 11:56:22 +08:00
|
|
|
|
public sealed class ZipArchiveReader : ArchiveReader
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
2017-03-23 12:41:50 +08:00
|
|
|
|
private readonly Stream archiveStream;
|
2017-11-19 12:46:51 +08:00
|
|
|
|
private readonly ZipArchive archive;
|
2017-02-09 22:09:48 +08:00
|
|
|
|
|
2018-02-15 11:56:22 +08:00
|
|
|
|
public ZipArchiveReader(Stream archiveStream, string name = null)
|
2018-02-15 09:20:23 +08:00
|
|
|
|
: base(name)
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
2016-10-19 23:00:11 +08:00
|
|
|
|
this.archiveStream = archiveStream;
|
2017-11-19 12:46:51 +08:00
|
|
|
|
archive = ZipArchive.Open(archiveStream);
|
2016-10-05 04:29:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-05 19:00:14 +08:00
|
|
|
|
public override Stream GetStream(string name)
|
2016-10-05 04:29:08 +08:00
|
|
|
|
{
|
2017-11-19 12:46:51 +08:00
|
|
|
|
ZipArchiveEntry entry = archive.Entries.SingleOrDefault(e => e.Key == name);
|
2016-10-14 11:33:58 +08:00
|
|
|
|
if (entry == null)
|
|
|
|
|
throw new FileNotFoundException();
|
2017-07-26 19:22:02 +08:00
|
|
|
|
|
|
|
|
|
// allow seeking
|
|
|
|
|
MemoryStream copy = new MemoryStream();
|
|
|
|
|
|
2017-11-19 12:46:51 +08:00
|
|
|
|
using (Stream s = entry.OpenEntryStream())
|
2017-07-26 19:22:02 +08:00
|
|
|
|
s.CopyTo(copy);
|
|
|
|
|
|
|
|
|
|
copy.Position = 0;
|
|
|
|
|
|
|
|
|
|
return copy;
|
2016-10-05 04:29:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-19 01:35:01 +08:00
|
|
|
|
public override void Dispose()
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
|
|
|
|
archive.Dispose();
|
2016-10-19 23:00:11 +08:00
|
|
|
|
archiveStream.Dispose();
|
2016-10-10 21:26:34 +08:00
|
|
|
|
}
|
2017-05-23 15:26:51 +08:00
|
|
|
|
|
2017-11-19 12:46:51 +08:00
|
|
|
|
public override IEnumerable<string> Filenames => archive.Entries.Select(e => e.Key).ToArray();
|
2017-07-26 19:22:02 +08:00
|
|
|
|
|
2017-05-23 15:26:51 +08:00
|
|
|
|
public override Stream GetUnderlyingStream() => archiveStream;
|
2016-10-14 11:33:58 +08:00
|
|
|
|
}
|
2017-11-19 12:46:51 +08:00
|
|
|
|
}
|