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
(DataFlex) 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.
Use ChilkatAx-win32.pkg Procedure Test Handle hoFac Variant vJpgData Handle hoJpgData Boolean iSuccess Variant vPngData Handle hoPngData Variant vPdfData Handle hoPdfData Variant vZipData Handle hoZipData String sTemp1 // 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. Get Create (RefClass(cComCkFileAccess)) To hoFac If (Not(IsComObjectCreated(hoFac))) Begin Send CreateComObject of hoFac End // A JPG file. Get Create (RefClass(cComChilkatBinData)) To hoJpgData If (Not(IsComObjectCreated(hoJpgData))) Begin Send CreateComObject of hoJpgData End Get ComOpenForRead Of hoFac "qa_data/jpg/starfish.jpg" To iSuccess // The the first 8 bytes of the JPG file. Get pvComObject of hoJpgData to vJpgData Get ComFileReadBd Of hoFac 8 vJpgData To iSuccess Send ComFileClose To hoFac // JPG hex: FFD8FFE000104A46 // JPG qp: =FF=D8=FF=E0=00=10JF Get ComGetEncoded Of hoJpgData "hex" To sTemp1 Showln "JPG hex: " sTemp1 Get ComGetEncoded Of hoJpgData "qp" To sTemp1 Showln "JPG qp: " sTemp1 // 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. Get Create (RefClass(cComChilkatBinData)) To hoPngData If (Not(IsComObjectCreated(hoPngData))) Begin Send CreateComObject of hoPngData End Get ComOpenForRead Of hoFac "qa_data/png/anemone.png" To iSuccess // The the first 8 bytes of the PNG file. Get pvComObject of hoPngData to vPngData Get ComFileReadBd Of hoFac 8 vPngData To iSuccess Send ComFileClose To hoFac // PNG hex: 89504E470D0A1A0A // PNG qp: =89PNG=1A=0A Get ComGetEncoded Of hoPngData "hex" To sTemp1 Showln "PNG hex: " sTemp1 Get ComGetEncoded Of hoPngData "qp" To sTemp1 Showln "PNG qp: " sTemp1 // A PNG file begins with the byte 0x89, followed by the us-ascii bytes "PNG". // ---------------------------------------- // A PDF file. Get Create (RefClass(cComChilkatBinData)) To hoPdfData If (Not(IsComObjectCreated(hoPdfData))) Begin Send CreateComObject of hoPdfData End Get ComOpenForRead Of hoFac "qa_data/pdf/fishing.pdf" To iSuccess // The the first 8 bytes of the PDF file. Get pvComObject of hoPdfData to vPdfData Get ComFileReadBd Of hoFac 8 vPdfData To iSuccess Send ComFileClose To hoFac // PDF hex: 255044462D312E33 // PDF qp: %PDF-1.3 Get ComGetEncoded Of hoPdfData "hex" To sTemp1 Showln "PDF hex: " sTemp1 Get ComGetEncoded Of hoPdfData "qp" To sTemp1 Showln "PDF qp: " sTemp1 // A PDF file begins with the us-ascii chars "%PDF" // ---------------------------------------- // A Zip file. Get Create (RefClass(cComChilkatBinData)) To hoZipData If (Not(IsComObjectCreated(hoZipData))) Begin Send CreateComObject of hoZipData End Get ComOpenForRead Of hoFac "qa_data/zips/test.zip" To iSuccess // The the first 8 bytes of the Zip file. Get pvComObject of hoZipData to vZipData Get ComFileReadBd Of hoFac 8 vZipData To iSuccess Send ComFileClose To hoFac // PDF hex: 504B030414000000 // PDF qp: PK=03=04=14=00=00=00 Get ComGetEncoded Of hoZipData "hex" To sTemp1 Showln "PDF hex: " sTemp1 Get ComGetEncoded Of hoZipData "qp" To sTemp1 Showln "PDF qp: " sTemp1 // A Zip archive begins with the us-ascii chars "PK" followed by the bytes 0x03, 0x04. End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.