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
|
|
|
|
|
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;
|
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;
|
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
|
|
|
|
|
2017-07-26 11:22:02 +00:00
|
|
|
|
// allow seeking
|
|
|
|
|
MemoryStream copy = new MemoryStream();
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-11-19 04:46:51 +00:00
|
|
|
|
using (Stream s = entry.OpenEntryStream())
|
2017-07-26 11:22:02 +00:00
|
|
|
|
s.CopyTo(copy);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-07-26 11:22:02 +00:00
|
|
|
|
copy.Position = 0;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-07-26 11:22:02 +00:00
|
|
|
|
return copy;
|
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();
|
2016-10-14 03:33:58 +00:00
|
|
|
|
}
|
2017-11-19 04:46:51 +00:00
|
|
|
|
}
|