Search code examples
c#filearchive7zipsevenzipsharp

detecting if a file is an archive using 7zip


I would like to use SevenZipSharp in order to determine if a file is an archive. I know that it's possible because in explorer if I rename a .zip to .bmp, 7zip still recognises it as an archive.

--edit: In other words, I want 7zip to tell me if a file (no matter the extension) contains some kind of supported archive (zip, tar, rar, iso etc.)

Thanks, Fidel


Solution

  • static bool IsArchive(string filename)
    {
        bool result = false;
        try
        {
            new ArchiveFile(File.OpenRead(filename));
            result = true;
        }
        catch
        {
            //log if you're going to do something about it
        }
        return result;
    }