mirror of
https://github.com/ppy/osu.git
synced 2026-05-18 14:50:54 +08:00
a996261304
It's been a while. Notes: - `SharpCompress` usages changed a bit. Manually adjusted these, mostly just renames or adjusted parameters. - nUnit 3 -> 4 migrated using https://gist.github.com/peppy/07994386d793a117350cb5f24b156585. there's a mode in this script to update to the newer `Assert.That` syntax but it requires fixes and couldn't really be bothered. - DeepEqual nuked as the only usage was on a disabled test. The reason it's disabled has been merged upstream, but it's failing for other (realm) reasons which I don't think is worthwhile to investigate for now. - This bumps Moq. I think the author is back in a sensible headspace and the new version has the stupid shit removed, so probably okay? Nice to be on a level playing field with packages for once in a long time. - Automapper is silly, but we've discussed this elsewhere. - `TestRealmKeyBindingStore` failures are a wildcard, but fixed by using a more standardised testing method. Dunno why, don't care. --------- Co-authored-by: Bartłomiej Dach <dach.bartlomiej@gmail.com>
79 lines
2.9 KiB
C#
79 lines
2.9 KiB
C#
// 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.
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using SharpCompress.Archives.Zip;
|
|
|
|
namespace osu.Game.Utils
|
|
{
|
|
public static class ZipUtils
|
|
{
|
|
public static bool IsZipArchive(MemoryStream stream)
|
|
{
|
|
try
|
|
{
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
using (var arc = ZipArchive.OpenArchive(stream))
|
|
{
|
|
foreach (var entry in arc.Entries)
|
|
{
|
|
using (entry.OpenEntryStream())
|
|
{
|
|
}
|
|
}
|
|
|
|
// aside from opening every zip entry not failing, we also require there to *be* at least one entry.
|
|
// if there are no entries, the best case is that it's an actual empty zip
|
|
// and as such probably useless to whatever wants to use it later.
|
|
// the worst case is that it's actually *not* a zip and instead a stream of binary
|
|
// which *accidentally* happened to contain the magic sequence of bytes for the zip header (50 4b 05 06),
|
|
// and if that's the case, then we are *misclassifying* it as a zip by returning `true` unconditionally.
|
|
return arc.Entries.Any();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return false;
|
|
}
|
|
finally
|
|
{
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
}
|
|
}
|
|
|
|
public static bool IsZipArchive(string path)
|
|
{
|
|
if (!File.Exists(path))
|
|
return false;
|
|
|
|
try
|
|
{
|
|
using (var arc = ZipArchive.OpenArchive(path))
|
|
{
|
|
foreach (var entry in arc.Entries)
|
|
{
|
|
using (entry.OpenEntryStream())
|
|
{
|
|
}
|
|
}
|
|
|
|
// aside from opening every zip entry not failing, we also require there to *be* at least one entry.
|
|
// if there are no entries, the best case is that it's an actual empty zip
|
|
// and as such probably useless to whatever wants to use it later.
|
|
// the worst case is that it's actually *not* a zip and instead a stream of binary
|
|
// which *accidentally* happened to contain the magic sequence of bytes for the zip header (50 4b 05 06),
|
|
// and if that's the case, then we are *misclassifying* it as a zip by returning `true` unconditionally.
|
|
return arc.Entries.Any();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|