PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
oleobject loo_Fac
oleobject loo_JpgData
oleobject loo_PngData
oleobject loo_PdfData
oleobject loo_ZipData
// 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.
loo_Fac = create oleobject
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")
if li_rc < 0 then
destroy loo_Fac
MessageBox("Error","Connecting to COM object failed")
return
end if
// A JPG file.
loo_JpgData = create oleobject
li_rc = loo_JpgData.ConnectToNewObject("Chilkat.BinData")
loo_Fac.OpenForRead("qa_data/jpg/starfish.jpg")
// The the first 8 bytes of the JPG file.
loo_Fac.FileReadBd(8,loo_JpgData)
loo_Fac.FileClose()
// JPG hex: FFD8FFE000104A46
// JPG qp: =FF=D8=FF=E0=00=10JF
Write-Debug "JPG hex: " + loo_JpgData.GetEncoded("hex")
Write-Debug "JPG qp: " + loo_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.
loo_PngData = create oleobject
li_rc = loo_PngData.ConnectToNewObject("Chilkat.BinData")
loo_Fac.OpenForRead("qa_data/png/anemone.png")
// The the first 8 bytes of the PNG file.
loo_Fac.FileReadBd(8,loo_PngData)
loo_Fac.FileClose()
// PNG hex: 89504E470D0A1A0A
// PNG qp: =89PNG=1A=0A
Write-Debug "PNG hex: " + loo_PngData.GetEncoded("hex")
Write-Debug "PNG qp: " + loo_PngData.GetEncoded("qp")
// A PNG file begins with the byte 0x89, followed by the us-ascii bytes "PNG".
// ----------------------------------------
// A PDF file.
loo_PdfData = create oleobject
li_rc = loo_PdfData.ConnectToNewObject("Chilkat.BinData")
loo_Fac.OpenForRead("qa_data/pdf/fishing.pdf")
// The the first 8 bytes of the PDF file.
loo_Fac.FileReadBd(8,loo_PdfData)
loo_Fac.FileClose()
// PDF hex: 255044462D312E33
// PDF qp: %PDF-1.3
Write-Debug "PDF hex: " + loo_PdfData.GetEncoded("hex")
Write-Debug "PDF qp: " + loo_PdfData.GetEncoded("qp")
// A PDF file begins with the us-ascii chars "%PDF"
// ----------------------------------------
// A Zip file.
loo_ZipData = create oleobject
li_rc = loo_ZipData.ConnectToNewObject("Chilkat.BinData")
loo_Fac.OpenForRead("qa_data/zips/test.zip")
// The the first 8 bytes of the Zip file.
loo_Fac.FileReadBd(8,loo_ZipData)
loo_Fac.FileClose()
// PDF hex: 504B030414000000
// PDF qp: PK=03=04=14=00=00=00
Write-Debug "PDF hex: " + loo_ZipData.GetEncoded("hex")
Write-Debug "PDF qp: " + loo_ZipData.GetEncoded("qp")
// A Zip archive begins with the us-ascii chars "PK" followed by the bytes 0x03, 0x04.
destroy loo_Fac
destroy loo_JpgData
destroy loo_PngData
destroy loo_PdfData
destroy loo_ZipData