From 57bfa18359fe9d0d4fd1221e888405065342635d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Thu, 10 Oct 2019 23:42:02 +0200 Subject: [PATCH] Filter out OS-generated files from archives Add a filename ignore list to ZipArchiveReader to filter out superfluous OS-generated files from archives during the import process. In addition to decreasing the size of files imported this allows imports of some incorrectly-constructed archives. An example is the case of having a __MACOSX directory next to a single directory with the actual files - filtering out the former at ZipArchiveReader allows the fallback added in #6170 to work. --- osu.Game/IO/Archives/ZipArchiveReader.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/osu.Game/IO/Archives/ZipArchiveReader.cs b/osu.Game/IO/Archives/ZipArchiveReader.cs index d934ac54c4..2233520916 100644 --- a/osu.Game/IO/Archives/ZipArchiveReader.cs +++ b/osu.Game/IO/Archives/ZipArchiveReader.cs @@ -1,15 +1,30 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System; using System.Collections.Generic; using System.IO; using System.Linq; using SharpCompress.Archives.Zip; +using SharpCompress.Common; namespace osu.Game.IO.Archives { public sealed class ZipArchiveReader : ArchiveReader { + /// + /// 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). + /// + private static readonly string[] filename_ignore_list = + { + // Mac-specific + "__MACOSX", + ".DS_Store", + // Windows-specific + "Thumbs.db" + }; + private readonly Stream archiveStream; private readonly ZipArchive archive; @@ -43,7 +58,9 @@ namespace osu.Game.IO.Archives archiveStream.Dispose(); } - public override IEnumerable Filenames => archive.Entries.Select(e => e.Key).ToArray(); + private static bool canBeIgnored(IEntry entry) => filename_ignore_list.Any(ignoredName => entry.Key.IndexOf(ignoredName, StringComparison.InvariantCultureIgnoreCase) >= 0); + + public override IEnumerable Filenames => archive.Entries.Where(e => !canBeIgnored(e)).Select(e => e.Key).ToArray(); public override Stream GetUnderlyingStream() => archiveStream; }