文件类型 - 获取原始扩展名

发布于 2024-10-10 14:40:29 字数 121 浏览 1 评论 0原文

如果文件已重命名,如何查找文件扩展名? 有没有可用的工具?

示例:我有一个文件“1.doc”;我希望大家知道这是一个我刚刚重命名为“1.txt”的Word文档。但该文件原本是Word文档;如何获得原始文件扩展名?

How to find file extension if the file has been renamed?
Is there any tool are available for this?

Example: I have a file "1.doc"; I hope everybody know this is a Word document I just renamed as "1.txt". But the file is a Word document originally; how can I get the original file extension?

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

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

发布评论

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

评论(3

贪了杯 2024-10-17 14:40:29

当然可以:)

这是适合您的 C# 代码。我想你可以构建自己的工具;)

using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Win32;

    [DllImport(@"urlmon.dll", CharSet = CharSet.Auto)]
    private extern static System.UInt32 FindMimeFromData(
        System.UInt32 pBC,
        [MarshalAs(UnmanagedType.LPStr)] System.String pwzUrl,
        [MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer,
        System.UInt32 cbSize,
        [MarshalAs(UnmanagedType.LPStr)] System.String pwzMimeProposed,
        System.UInt32 dwMimeFlags,
        out System.UInt32 ppwzMimeOut,
        System.UInt32 dwReserverd
    );


    public static string getMimeFromFile(string filename)
    {
        if (!File.Exists(filename))
            throw new FileNotFoundException(filename + " not found");

        byte[] buffer = new byte[256];
        using (FileStream fs = new FileStream(filename, FileMode.Open))
        {
            if (fs.Length >= 256)
                fs.Read(buffer, 0, 256);
            else
                fs.Read(buffer, 0, (int)fs.Length);
        }
        try
        {
            System.UInt32 mimetype;
            FindMimeFromData(0, null, buffer, 256, null, 0, out mimetype, 0);
            System.IntPtr mimeTypePtr = new IntPtr(mimetype);
            string mime = Marshal.PtrToStringUni(mimeTypePtr);
            Marshal.FreeCoTaskMem(mimeTypePtr);
            return mime;
        }
        catch (Exception e)
        {
            return "unknown/unknown";
        }
    }

你可以使用此代码获得 mimetype。要找到 mime-type 的扩展名,只需进行一些谷歌搜索即可。

Of Course You can :)

This is the C# code for you. I guess you can bulid your own tool ;)

using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Win32;

    [DllImport(@"urlmon.dll", CharSet = CharSet.Auto)]
    private extern static System.UInt32 FindMimeFromData(
        System.UInt32 pBC,
        [MarshalAs(UnmanagedType.LPStr)] System.String pwzUrl,
        [MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer,
        System.UInt32 cbSize,
        [MarshalAs(UnmanagedType.LPStr)] System.String pwzMimeProposed,
        System.UInt32 dwMimeFlags,
        out System.UInt32 ppwzMimeOut,
        System.UInt32 dwReserverd
    );


    public static string getMimeFromFile(string filename)
    {
        if (!File.Exists(filename))
            throw new FileNotFoundException(filename + " not found");

        byte[] buffer = new byte[256];
        using (FileStream fs = new FileStream(filename, FileMode.Open))
        {
            if (fs.Length >= 256)
                fs.Read(buffer, 0, 256);
            else
                fs.Read(buffer, 0, (int)fs.Length);
        }
        try
        {
            System.UInt32 mimetype;
            FindMimeFromData(0, null, buffer, 256, null, 0, out mimetype, 0);
            System.IntPtr mimeTypePtr = new IntPtr(mimetype);
            string mime = Marshal.PtrToStringUni(mimeTypePtr);
            Marshal.FreeCoTaskMem(mimeTypePtr);
            return mime;
        }
        catch (Exception e)
        {
            return "unknown/unknown";
        }
    }

You get the mimetype using this code. To find the extension from mime-type just do a little google search.

幼儿园老大 2024-10-17 14:40:29

不可能的。如果您使用的是 *nix 类型系统,请使用 file 命令来确定文件格式。

如果您真的对发生这样的事情感到偏执(并扰乱您的工作流程),您可以做两件事:

  1. 对文件进行哈希处理,例如 MD5 哈希,以便您知道您的文件尚未被修改
  2. 记下文件的时间戳,以便您可以看到上次更改的时间
  3. 记下您文件的时间戳该时间戳的扩展名

这将通过以下几种方式保护您:

哈希将确保您的文件没有被更改。

时间戳会告诉您上次修改的时间。

该扩展名会告诉您其原始扩展名。

由于仅重命名文件的扩展名不会修改其时间戳,因此您需要步骤 3。

使用此类技术将在 99.99999999999% 的情况下告诉您您的文件已被某物或某人修改。

Impossible. If you're on a *nix type system, use the file command to determine file format.

If you're really paranoid about stuff like this happening (and messing up your workflow), you can do 2 things:

  1. make a hash of your file, for example, an MD5 hash so you know your file hasn't been tinkered with
  2. take note of your file's timestamp so you can see when was the last time it changed
  3. take note of your file's extension at that timestamp

This will guard you in few ways:

The hash will make sure your file hasn't been changed.

The timestamp will tell you the last time it was modified.

The extension will tell you its original extension.

Since just renaming file's extension won't modify its timestamp, you need step 3.

Using techniques like this will tell you in 99.99999999999% cases that your file has been modified by something or somebody.

梦萦几度 2024-10-17 14:40:29

你不能。您必须使用像 file 这样的工具来尝试检测文件的格式。

You can't. You'd have to use a tool like file to try and detect the file's format.

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