Skip to content

UPX 使用 (可执行文件压工具)

upx (the Ultimate Packer for eXecutables) 是一款专为压缩可执行文件而设计的工具。它能显著减小文件体积,但请注意,这可能会略微降低可执行文件的启动速度。


安装

upx 提供 Windows 和 Linux 版本。如果你使用的是 macOS 可以用类似工具 gzexe

  • Github 主页 ,对应的 Release 下载页面有对应的 OS 可执行文件:
License

目前 UPX 使用 GNU General Public License v2+ 协议

使用

使用 UPX 压缩可执行文件非常简单。在压缩前,强烈建议先备份原始文件,以防万一。

upx <可执行文件名>

比如:

> upx helloworld.exe
                       Ultimate Packer for eXecutables
                          Copyright (C) 1996 - 2025
UPX 5.0.2       Markus Oberhumer, Laszlo Molnar & John Reiser   Jul 20th 2025

        File size         Ratio      Format      Name
   --------------------   ------   -----------   -----------
   2326528 ->   1356800   58.32%    win64/pe     helloworld.exe

Packed 1 file.

从输出结果可以看出,helloworld.exe 文件被成功压缩,体积减小了 58.32%。

压缩率测试

按照 upx 的主页说明,压缩率一般为 50%-70% 。这里实际那 upx version 5.0.2 压缩一个 Golang helloword 程序在 Windows 上测试。

  • 压缩前 2.21MB (2,326,528 bytes)
  • 压缩后 1.29 MB (1,356,800 bytes)