使用 7zip 检测文件是否为存档

发布于 2024-11-07 11:10:37 字数 185 浏览 6 评论 0原文

我想使用 SevenZipSharp 来确定文件是否是存档。我知道这是可能的,因为在资源管理器中,如果我将 .zip 重命名为 .bmp,7zip 仍然会将其识别为存档。

--edit:换句话说,我希望 7zip 告诉我文件(无论扩展名如何)是否包含某种受支持的存档(zip、tar、rar、iso 等),

谢谢, 菲德尔

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

昇り龍 2024-11-14 11:10:37
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;
}
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;
}
删除→记忆 2024-11-14 11:10:37

确定文件是否为存档的方法是实际尝试将其输入 SevenZipSharp 库,然后查看它是否成功或失败。然而,这将是一个非常缓慢的过程,就像您的示例一样,您有一堆标有扩展名 .bmp 的 .zip 文件。

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.

寂寞花火° 2024-11-14 11:10:37

您不需要使用 Sevenzip 来仅了解该文件是否是存档,
检查各种文件的魔术字节就足够了。

例如:

Zip 的初始 2 字节为 50 4B (PK)

RAR 的初始为 3 字节 52 61 72 (Rar!)

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

时光暖心i 2024-11-14 11:10:37

SharpCompress 也可以轻松做到这一点。

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

SharpCompress does this easily as well.

bool x = SevenZipArchive.IsSevenZipFile(File.OpenRead(path));
独孤求败 2024-11-14 11:10:37

我没有使用过该库,而且事实上没有 没有文档 并没有帮助,但通常会尝试一下打开存档,如果出现任何错误,则可能意味着该文件不是存档(可能存在特定错误)。

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

弥繁 2024-11-14 11:10:37

我不熟悉 SevenZipSharp,但 ZIP 是一种有据可查的文件格式,例如: ZIP文件格式

请注意文件和条目开头的幻数。您不需要任何特殊的 API/库来检测 zip 文件,只需将其作为普通文件读取并检查它是否符合格式即可。如果您不想解析整个文件,您可能会偷懒,只需检查文件签名是否是您要查找的文件签名(或其中之一):文件签名列表

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

嘿哥们儿 2024-11-14 11:10:37

7z.exe 可用于确定文件是否为存档:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文