detecting if a file is an archive using 7zip

末鹿安然 提交于 2019-12-24 03:35:12

问题


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


回答1:


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;
}



回答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.




回答3:


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!)




回答4:


SharpCompress does this easily as well.

bool x = SevenZipArchive.IsSevenZipFile(File.OpenRead(path));



回答5:


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).




回答6:


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




回答7:


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;
}


来源:https://stackoverflow.com/questions/5978567/detecting-if-a-file-is-an-archive-using-7zip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!