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