模极社社区 - 制造业全产业链交流平台

    • 登录
    • 版块
    • 最新
    • 话题
    • 热门
    • 用户
    • 群组

    isBinaryFile——模具行业从业者必备知识手册

    社区公告
    1
    1
    0
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • S
      system 2 最后由 编辑

      <h2>isBinaryFile</h2>

      <p>Detects if a file is binary in Node.js using ✨promises✨. Similar to <a href="http://stackoverflow.com/questions/899206/how-does-perl-know-a-file-is-binary">Perl's <code>-B</code> switch</a>, in that:</p>
      <ul>
      <li>it reads the first few thousand bytes of a file</li>
      <li>checks for a <code>null</code> byte; if it's found, it's binary</li>
      <li>flags non-ASCII characters. After a certain number of "weird" characters, the file is flagged as binary</li>
      </ul>

      <p>Much of the logic is pretty much ported from <a href="https://github.com/ggreer/the_silver_searcher">ag</a>.</p>

      <p>Note: if the file doesn't exist or is a directory, an error is thrown.</p>

      <h3>Installation</h3>

      <pre><code>
      npm install isbinaryfile
      </code></pre>

      <h3>Usage</h3>

      <p>Returns <code>Promise<boolean></code> (or just <code>boolean</code> for <code>*Sync</code>). <code>true</code> if the file is binary, <code>false</code> otherwise.</p>

      <h4>isBinaryFile(filepath)</h4>

      <ul>
      <li><code>filepath</code> - a <code>string</code> indicating the path to the file.</li>
      </ul>

      <h4>isBinaryFile(bytes[, size])</h4>

      <ul>
      <li><code>bytes</code> - a <code>Buffer</code> of the file's contents.</li>
      <li><code>size</code> - an optional <code>number</code> indicating the file size.</li>
      </ul>

      <h4>isBinaryFileSync(filepath)</h4>

      <ul>
      <li><code>filepath</code> - a <code>string</code> indicating the path to the file.</li>
      </ul>

      <h4>isBinaryFileSync(bytes[, size])</h4>

      <ul>
      <li><code>bytes</code> - a <code>Buffer</code> of the file's contents.</li>
      <li><code>size</code> - an optional <code>number</code> indicating the file size.</li>
      </ul>

      <h4>Examples</h4>

      <p>Here's an arbitrary usage:</p>

      <pre><code>
      const isBinaryFile = require("isbinaryfile").isBinaryFile;
      const fs = require("fs");

      const filename = "fixtures/pdf.pdf";
      const data = fs.readFileSync(filename);
      const stat = fs.lstatSync(filename);

      isBinaryFile(data, stat.size).then((result) => {
      if (result) {
      console.log("It is binary!")
      }
      else {
      console.log("No it is not.")
      }
      });

      const isBinaryFileSync = require("isbinaryfile").isBinaryFileSync;
      const bytes = fs.readFileSync(filename);
      const size = fs.lstatSync(filename).size;
      console.log(isBinaryFileSync(bytes, size)); // true or false
      </code></pre>

      <h3>Testing</h3>

      <p>Run <code>npm install</code>, then run <code>npm test</code>.</p>

      <hr>

      <p>本文由模极社发布</p>

      <hr>
      <h4>🔗 你可能还感兴趣</h4>
      <ul>
      <li><a href="/t/3149/3149">PEEK聚醚醚酮</a></li>
      <li><a href="/t/3681/3681">广州国际模具展览会(Asiamold Select 2026)</a></li>
      <li><a href="/t/4812/4812">东莞横沥镇模具产业 — 模具制造专业镇</a></li>
      </ul>

      <hr>
      <p><strong>相关推荐:</strong></p>
      <ul>
      <li><a href="https://www.mj7.cn/t/4836-4836">北京模具产业 — 科技研发驱动型模具基地</a></li>
      <li><a href="https://www.mj7.cn/t/4835-4835">上海模具产业 — 高端精密模具研发中心</a></li>
      <li><a href="https://www.mj7.cn/t/4834-4834">产业集群与模具产业发展的关系</a></li>
      </ul>

      <hr>
      <p>哪个方案更实用?欢迎投票讨论。</p>

      1 条回复 最后回复 回复 引用 0
      • First post
        Last post
      Powered by NodeBB | Contributors