2

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

Fidel
  • 7,027
  • 11
  • 57
  • 81

7 Answers7

5
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;
}
Fidel
  • 7,027
  • 11
  • 57
  • 81
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

The way you would determine if the file is an archive, is to actually try to feed it in to the SevenZipSharp library, and see if it succeeds or fails. However this is going to be a really slow process like your example you have a bunch of .zip files marked with the extension .bmp.

Nick Berardi
  • 54,393
  • 15
  • 113
  • 135
  • Thanks Nick - yep I tried: SevenZip.SevenZipExtractor e = new SevenZip.SevenZipExtractor(fullFilename);, however it throws an exception saying: "Extension .BMP is not a supported archive file name extension.". It has a list of valid extensions oddly enough. – Fidel May 12 '11 at 13:28
  • Was digging around the source code and it seems the library initially doesn't look at the extension. It runs a "CheckSignature" that checks the initial bytes of the file to try and match it with the supported formats. If that fails, it displays an error message like the one you get. If all you did was rename a .zip file to .bmp and it started failing, it might be a bug in the library. Did it work when the file had a .zip extension? – Farinha May 12 '11 at 13:47
  • @Fidel - I don't know because I haven't tried, but I suspect that if you pass in the STREAM to the SevenZipExtractor (like SLaks suggests in the answer that was accepted) instead of the FILENAME, then the SevenZipExtractor simply won't know the extension of the file that the stream is coming from. You've probably figured this out by now, but thought I would clarify. – Phil Whittington May 27 '14 at 12:03
2

You don't need to use sevenzip to only know whether the file is an archive or not, It is suffice to check for the magic byte for various files.

For example:

Zip has initial 2 bytes 50 4B (PK)

RAR has initial 3 bytes 52 61 72 (Rar!)

crypted
  • 10,118
  • 3
  • 39
  • 52
  • thanks lnt, but I just want to go the easy way and support whatever archives 7zip supports. – Fidel May 12 '11 at 13:31
  • ok, for that try to open file using sevenzipsharp if it opens successfully it is probably an archive if not it is not an archive. – crypted May 12 '11 at 13:34
  • indeed that was my first attempt, please see comment on Nick's post – Fidel May 12 '11 at 13:37
2

SharpCompress does this easily as well.

bool x = SevenZipArchive.IsSevenZipFile(File.OpenRead(path));
adamhathcock
  • 313
  • 1
  • 7
0

7z.exe can be used to determine if a file is an archive:

static bool IsArchive(string filename)
{
    string _7z = @"C:\Program Files\7-Zip\7z.exe";

    bool result = false;
    using (Process p = new Process())
    {
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.FileName = _7z;
        p.StartInfo.Arguments = $"l \"{filename}\"";
        p.Start();
        string stdout = p.StandardOutput.ReadToEnd();
        string stderr = p.StandardError.ReadToEnd();

        if (stdout.Contains("Type = "))
        {
            result = true;
        }

        p.WaitForExit();
    }

    return result;
}
Fidel
  • 7,027
  • 11
  • 57
  • 81
0

I haven't used that library and the fact that there's no documentation doesn't help, but typically one tries to open the archive and if any error comes out, it might mean the file is not an archive (there's probably a specific error for that).

Farinha
  • 17,636
  • 21
  • 64
  • 80
0

I'm not familiar with SevenZipSharp, but ZIP is a well documented file format, for example: ZIP File Format

Note the magic numbers at the start of the file and entries. You don't need any special API/library to detect a zip file, just read it as a normal file and check if it conforms to the format. If you don't feel like parsing the whole file, you could be lazy and just check the file signature is the one (or one of the ones) you're looking for: List of file signatures

Matt
  • 425
  • 5
  • 11
  • Thanks Matt, it's not just zips I'm looking for - it's any archive supported by 7zip. – Fidel May 12 '11 at 13:35
  • Which leads us to the question of whether all supported formats use a file signature. If so, no problem, if not it's a shame SevenZipSharp doesn't seem to be a documented API. – Matt May 12 '11 at 13:39