Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Node.js) Determine File Type from Binary Content of FileNote: This example requires Chilkat v9.5.0.64 or later. Many file types have "signatures" (leading bytes) that signify the type of file. It allows for programs to identify the likely type of file given the first few bytes contained within the file. This example shows how to identify a few common types files. For other file types, you can do a short bit of investigative work by examining the first few bytes of an sample file, and searching the Internet for information about the file type. Use this same technique for handling other file types that have leading "signature" bytes.
var os = require('os'); if (os.platform() == 'win32') { if (os.arch() == 'ia32') { var chilkat = require('@chilkat/ck-node21-win-ia32'); } else { var chilkat = require('@chilkat/ck-node21-win64'); } } else if (os.platform() == 'linux') { if (os.arch() == 'arm') { var chilkat = require('@chilkat/ck-node21-arm'); } else if (os.arch() == 'x86') { var chilkat = require('@chilkat/ck-node21-linux32'); } else { var chilkat = require('@chilkat/ck-node21-linux64'); } } else if (os.platform() == 'darwin') { if (os.arch() == 'arm64') { var chilkat = require('@chilkat/ck-node21-mac-m1'); } else { var chilkat = require('@chilkat/ck-node21-macosx'); } } function chilkatExample() { // Note: This example requires Chilkat v9.5.0.64 or later. // To identify a file by the first few bytes, we'll load a few bytes from the start // of the file, and then examine the bytes as both hex and quoted-printable. var fac = new chilkat.FileAccess(); // A JPG file. var jpgData = new chilkat.BinData(); fac.OpenForRead("qa_data/jpg/starfish.jpg"); // The the first 8 bytes of the JPG file. fac.FileReadBd(8,jpgData); fac.FileClose(); // JPG hex: FFD8FFE000104A46 // JPG qp: =FF=D8=FF=E0=00=10JF console.log("JPG hex: " + jpgData.GetEncoded("hex")); console.log("JPG qp: " + jpgData.GetEncoded("qp")); // A JPG begins with the following two bytes: 0xFF, 0xD8 // Your program can check to see if the hex string begins with "FFD8", or if the qp string begins with "=FF=D8". // ---------------------------------------- // A PNG file. var pngData = new chilkat.BinData(); fac.OpenForRead("qa_data/png/anemone.png"); // The the first 8 bytes of the PNG file. fac.FileReadBd(8,pngData); fac.FileClose(); // PNG hex: 89504E470D0A1A0A // PNG qp: =89PNG=1A=0A console.log("PNG hex: " + pngData.GetEncoded("hex")); console.log("PNG qp: " + pngData.GetEncoded("qp")); // A PNG file begins with the byte 0x89, followed by the us-ascii bytes "PNG". // ---------------------------------------- // A PDF file. var pdfData = new chilkat.BinData(); fac.OpenForRead("qa_data/pdf/fishing.pdf"); // The the first 8 bytes of the PDF file. fac.FileReadBd(8,pdfData); fac.FileClose(); // PDF hex: 255044462D312E33 // PDF qp: %PDF-1.3 console.log("PDF hex: " + pdfData.GetEncoded("hex")); console.log("PDF qp: " + pdfData.GetEncoded("qp")); // A PDF file begins with the us-ascii chars "%PDF" // ---------------------------------------- // A Zip file. var zipData = new chilkat.BinData(); fac.OpenForRead("qa_data/zips/test.zip"); // The the first 8 bytes of the Zip file. fac.FileReadBd(8,zipData); fac.FileClose(); // PDF hex: 504B030414000000 // PDF qp: PK=03=04=14=00=00=00 console.log("PDF hex: " + zipData.GetEncoded("hex")); console.log("PDF qp: " + zipData.GetEncoded("qp")); // A Zip archive begins with the us-ascii chars "PK" followed by the bytes 0x03, 0x04. } chilkatExample(); |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.