版博士V2.0程序
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # isBinaryFile
  2. Detects if a file is binary in Node.js using ✨promises✨. Similar to [Perl's `-B` switch](http://stackoverflow.com/questions/899206/how-does-perl-know-a-file-is-binary), in that:
  3. - it reads the first few thousand bytes of a file
  4. - checks for a `null` byte; if it's found, it's binary
  5. - flags non-ASCII characters. After a certain number of "weird" characters, the file is flagged as binary
  6. Much of the logic is pretty much ported from [ag](https://github.com/ggreer/the_silver_searcher).
  7. Note: if the file doesn't exist or is a directory, an error is thrown.
  8. ## Installation
  9. ```
  10. npm install isbinaryfile
  11. ```
  12. ## Usage
  13. Returns `Promise<boolean>` (or just `boolean` for `*Sync`). `true` if the file is binary, `false` otherwise.
  14. ### isBinaryFile(filepath)
  15. * `filepath` - a `string` indicating the path to the file.
  16. ### isBinaryFile(bytes[, size])
  17. * `bytes` - a `Buffer` of the file's contents.
  18. * `size` - an optional `number` indicating the file size.
  19. ### isBinaryFileSync(filepath)
  20. * `filepath` - a `string` indicating the path to the file.
  21. ### isBinaryFileSync(bytes[, size])
  22. * `bytes` - a `Buffer` of the file's contents.
  23. * `size` - an optional `number` indicating the file size.
  24. ### Examples
  25. Here's an arbitrary usage:
  26. ```javascript
  27. const isBinaryFile = require("isbinaryfile").isBinaryFile;
  28. const fs = require("fs");
  29. const filename = "fixtures/pdf.pdf";
  30. const data = fs.readFileSync(filename);
  31. const stat = fs.lstatSync(filename);
  32. isBinaryFile(data, stat.size).then((result) => {
  33. if (result) {
  34. console.log("It is binary!")
  35. }
  36. else {
  37. console.log("No it is not.")
  38. }
  39. });
  40. const isBinaryFileSync = require("isbinaryfile").isBinaryFileSync;
  41. const bytes = fs.readFileSync(filename);
  42. const size = fs.lstatSync(filename).size;
  43. console.log(isBinaryFileSync(bytes, size)); // true or false
  44. ```
  45. ## Testing
  46. Run `npm install`, then run `npm test`.