本文介绍了使用 Node.js 检查文件是二进制文件还是 ASCII 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道使用 Node.js 检查文件是二进制文件还是 ASCII 文件的最佳方法是什么?

I'm wondering what would be the best way to check if a file is binary or ASCII with Node.js?

似乎有两种方式并非特定于 node.js:

There appears to be two ways not specific to node.js:

  1. 检查 MIME 类型:如何在 PHP 中检查文件是 ASCII 还是二进制 - 但是这有它的问题,例如前驱通常没有可识别的 mime 类型并恢复为 application/octet-stream 使用 mime

  1. Checking the MIME type: How to Check if File is ASCII or Binary in PHP - however this has it's problems, as for instance pre-precessors often don't have a recognised mime type and revert to application/octet-stream when checking them using mime

通过使用带有 如何将文件内容识别为 ASCII 或二进制 - 这似乎相当密集,但还提供了一个 node.js 示例.

Via checking the byte size using a stream buffer with How to identify the file content as ASCII or binary - which seems quite intensive, and does yet provide a node.js example.

那么已经有另一种方法了吗?也许是我不知道的秘密 node.js 调用或模块?或者如果我必须自己这样做,建议什么方式?

So is there another way already? Perhaps a secret node.js call or module that I don't know about? Or if I have to do this myself, what way would be suggested?

谢谢

推荐答案

感谢 David Schwartz,我创建了 isttextorbinary 来解决这个问题.

Thanks to the comments on this question by David Schwartz, I created istextorbinary to solve this problem.

这篇关于使用 Node.js 检查文件是二进制文件还是 ASCII 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 09:51