通过Golang能否获取可执行文件的图标?

huangapple go评论65阅读模式
英文:

Is it possible to get executable file icon by Golang?

问题

我正在尝试编写一个“下载页面网站”,并且我想在我的网页上显示文件图标。

就像Windows系统中的“.exe”文件有内置的图标图像一样,或者Linux可执行文件也是如此。我可以读取它吗?

我知道Python可以使用一个名为“win32api”的包来实现这个功能,那么在Golang中有没有类似的包可以实现这个功能呢?

英文:

I'm trying to write a "Download Page Website", and I trying to show the file icon to my webpage.

Like Windows system, ".exe" file has icon image inside. Or linux executable file. Can I read it?

I know python can do it with a package name "win32api", is it any package for Golang to achieve this function?

答案1

得分: 2

你可以利用Linux软件包来实现你的目标。

例如,你可以使用icoextract,可以通过apt进行安装:

apt install icoextract

然后像这样运行它:

icoextract /path/to/file.exe /path/to/file.ico

使用os/exec软件包可以实现调用命令并执行它们的功能。所以你可以像这样做:

func ExtractIcon(executablePath string) []byte {
    file, err := ioutil.TempFile("dir", "prefix")
    if err != nil {
        log.Fatal(err)
    }
    defer os.Remove(file.Name())
    cmd := exec.Command("icoextract", executablePath, file.Name())
    if err = cmd.Run(); err != nil {
        log.Fatal(err)
    }

    content, _ := ioutil.ReadFile(file.Name())
    return content
}
英文:

You can use the linux package in your advantage.

For example, you can use icoextract, which can be installed via apt:

apt install icoextract

And then run it like this:

icoextract /path/to/file.exe /path/to/file.ico

Go make possible to call commands and execute them using the package os/exec. So you can do something like

func ExtractIcon(executablePath string) []byte {
    file, err := ioutil.TempFile("dir", "prefix")
    if err != nil {
        log.Fatal(err)
    }
    defer os.Remove(file.Name())
    cmd := exec.Command("icoextract", executablePath, file.Name())
    if err = cmd.Run(); err != nil {
        log.Fatal(err)
    }

    content, _ := ioutil.ReadFile(file.Name())
    return content
}

huangapple
  • 本文由 发表于 2022年5月10日 08:14:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/72179439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定