Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

SQL Server Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(SQL Server) Determine File Type from Binary Content of File

Note: 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.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- 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.

    DECLARE @fac int
    -- Use "Chilkat_9_5_0.FileAccess" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- A JPG file.
    DECLARE @jpgData int
    -- Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @jpgData OUT

    DECLARE @success int
    EXEC sp_OAMethod @fac, 'OpenForRead', @success OUT, 'qa_data/jpg/starfish.jpg'
    -- The the first 8 bytes of the JPG file.
    EXEC sp_OAMethod @fac, 'FileReadBd', @success OUT, 8, @jpgData
    EXEC sp_OAMethod @fac, 'FileClose', NULL

    -- JPG hex: FFD8FFE000104A46
    -- JPG qp: =FF=D8=FF=E0=00=10JF

    EXEC sp_OAMethod @jpgData, 'GetEncoded', @sTmp0 OUT, 'hex'
    PRINT 'JPG hex: ' + @sTmp0

    EXEC sp_OAMethod @jpgData, 'GetEncoded', @sTmp0 OUT, 'qp'
    PRINT 'JPG qp: ' + @sTmp0

    -- 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.
    DECLARE @pngData int
    -- Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @pngData OUT

    EXEC sp_OAMethod @fac, 'OpenForRead', @success OUT, 'qa_data/png/anemone.png'
    -- The the first 8 bytes of the PNG file.
    EXEC sp_OAMethod @fac, 'FileReadBd', @success OUT, 8, @pngData
    EXEC sp_OAMethod @fac, 'FileClose', NULL

    -- PNG hex: 89504E470D0A1A0A
    -- PNG qp: =89PNG=1A=0A

    EXEC sp_OAMethod @pngData, 'GetEncoded', @sTmp0 OUT, 'hex'
    PRINT 'PNG hex: ' + @sTmp0

    EXEC sp_OAMethod @pngData, 'GetEncoded', @sTmp0 OUT, 'qp'
    PRINT 'PNG qp: ' + @sTmp0
    -- A PNG file begins with the byte 0x89, followed by the us-ascii bytes "PNG".

    -- ----------------------------------------
    -- A PDF file.
    DECLARE @pdfData int
    -- Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @pdfData OUT

    EXEC sp_OAMethod @fac, 'OpenForRead', @success OUT, 'qa_data/pdf/fishing.pdf'
    -- The the first 8 bytes of the PDF file.
    EXEC sp_OAMethod @fac, 'FileReadBd', @success OUT, 8, @pdfData
    EXEC sp_OAMethod @fac, 'FileClose', NULL

    -- PDF hex: 255044462D312E33
    -- PDF qp: %PDF-1.3

    EXEC sp_OAMethod @pdfData, 'GetEncoded', @sTmp0 OUT, 'hex'
    PRINT 'PDF hex: ' + @sTmp0

    EXEC sp_OAMethod @pdfData, 'GetEncoded', @sTmp0 OUT, 'qp'
    PRINT 'PDF qp: ' + @sTmp0
    -- A PDF file begins with the us-ascii chars "%PDF"

    -- ----------------------------------------
    -- A Zip file.
    DECLARE @zipData int
    -- Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @zipData OUT

    EXEC sp_OAMethod @fac, 'OpenForRead', @success OUT, 'qa_data/zips/test.zip'
    -- The the first 8 bytes of the Zip file.
    EXEC sp_OAMethod @fac, 'FileReadBd', @success OUT, 8, @zipData
    EXEC sp_OAMethod @fac, 'FileClose', NULL

    -- PDF hex: 504B030414000000
    -- PDF qp: PK=03=04=14=00=00=00

    EXEC sp_OAMethod @zipData, 'GetEncoded', @sTmp0 OUT, 'hex'
    PRINT 'PDF hex: ' + @sTmp0

    EXEC sp_OAMethod @zipData, 'GetEncoded', @sTmp0 OUT, 'qp'
    PRINT 'PDF qp: ' + @sTmp0
    -- A Zip archive begins with the us-ascii chars "PK" followed by the bytes 0x03, 0x04.

    EXEC @hr = sp_OADestroy @fac
    EXEC @hr = sp_OADestroy @jpgData
    EXEC @hr = sp_OADestroy @pngData
    EXEC @hr = sp_OADestroy @pdfData
    EXEC @hr = sp_OADestroy @zipData


END
GO

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.