首页 > 解决方案 > 通过 Windows 命令行将特定文件编码为 Base64

问题描述

我需要在 Windows 操作系统上使用命令行在屏幕上生成特定文件的 base64 数据(不生成文件)。

我已经看到在 Unix 系统上足以使用

cat <file_name>| base64

获取编码为base64的文件内容。

在 Windows 上,我无法获得相同的结果。

我找到了这个解决方案:

certutil -encode -f <file_name> tmp.b64 && findstr /v /c:- tmp.b64 && del tmp.b64

但这需要系统生成一个临时文件,所以最后去销毁它。仅使用该certutil命令,屏幕上的结果就会被包含不相关信息的 3 行所污染。

有人可以帮我在 Windows 上提供一个只生成 base64 数据的命令吗?

更新:我通过这个新版本的命令改进了屏幕上的结果:

certutil -encode -f <file_name> tmp.b64 && cls && findstr /v /c:- tmp.b64 && del tmp.b64

结果更像我的要求,但我想避免tmp.b64每次都创建临时文件。

标签: windowscommand-linebase64batch-processingtobase64string

解决方案


我建议在你的 Windows 机器上安装OpenSSL 。设置 PATH 变量后,它很容易:

type <file_name> | openssl base64

推荐阅读