2019-01-24 16:43:03 +08: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 17:19:50 +08:00
|
|
|
|
|
2019-10-11 05:42:02 +08:00
|
|
|
|
using System;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using SharpCompress.Archives.Zip;
|
2019-10-11 05:42:02 +08:00
|
|
|
|
using SharpCompress.Common;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.IO.Archives
|
|
|
|
|
{
|
|
|
|
|
public sealed class ZipArchiveReader : ArchiveReader
|
|
|
|
|
{
|
2019-10-11 05:42:02 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// List of substrings that indicate a file should be ignored during the import process
|
|
|
|
|
/// (usually due to representing no useful data and being autogenerated by the OS).
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static readonly string[] filename_ignore_list =
|
|
|
|
|
{
|
|
|
|
|
// Mac-specific
|
|
|
|
|
"__MACOSX",
|
|
|
|
|
".DS_Store",
|
|
|
|
|
// Windows-specific
|
|
|
|
|
"Thumbs.db"
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
private readonly Stream archiveStream;
|
|
|
|
|
private readonly ZipArchive archive;
|
|
|
|
|
|
|
|
|
|
public ZipArchiveReader(Stream archiveStream, string name = null)
|
|
|
|
|
: base(name)
|
|
|
|
|
{
|
|
|
|
|
this.archiveStream = archiveStream;
|
|
|
|
|
archive = ZipArchive.Open(archiveStream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Dispose()
|
|
|
|
|
{
|
|
|
|
|
archive.Dispose();
|
|
|
|
|
archiveStream.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-11 05:42:02 +08:00
|
|
|
|
private static bool canBeIgnored(IEntry entry) => filename_ignore_list.Any(ignoredName => entry.Key.IndexOf(ignoredName, StringComparison.InvariantCultureIgnoreCase) >= 0);
|
|
|
|
|
|
|
|
|
|
public override IEnumerable<string> Filenames => archive.Entries.Where(e => !canBeIgnored(e)).Select(e => e.Key).ToArray();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
public override Stream GetUnderlyingStream() => archiveStream;
|
|
|
|
|
}
|
|
|
|
|
}
|