[Delphi] 파일 포맷 알아내기
- 09-08
- 51,117 회
- 0 건
[code]
function GetFormat(Filename: String): String;
function IsPatten(Buffer: TBytes; Index: Integer; Values: array of Byte): Boolean;
var
I: Integer;
begin
Result := True;
for I := Low(Values) to High(Values) do
if Buffer[I+Index] <> Values[I] then
begin
Result := False;
Break;
end;
end;
const
CheckSize = 12;
var
Stream: TFileStream;
Buffer: TBytes;
FileExt: String;
begin
Result := '';
try
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
if Stream.Size > CheckSize then
begin
SetLength(Buffer, CheckSize);
Stream.ReadBuffer(Buffer, CheckSize);
if IsPatten(Buffer, 0, [$FF, $D8, $FF]) then Result := 'JPG'
else if IsPatten(Buffer, 0, [$42, $4D]) then Result := 'BMP'
else if IsPatten(Buffer, 0, [$47, $49, $46, $38]) then Result := 'GIF'
else if IsPatten(Buffer, 8, [$57, $45, $42, $50]) then Result := 'WEBP'
else if IsPatten(Buffer, 0, [$49, $20, $49]) then Result := 'TIFF'
else if IsPatten(Buffer, 0, [$49, $49, $2A, $00]) then Result := 'TIFF'
else if IsPatten(Buffer, 0, [$4D, $4D, $00, $2A]) then Result := 'TIFF'
else if IsPatten(Buffer, 0, [$4D, $4D, $00, $2B]) then Result := 'TIFF'
else if IsPatten(Buffer, 0, [$38, $42, $50, $53]) then Result := 'PSD'
else if IsPatten(Buffer, 0, [$49, $49, $55, $00]) then Result := 'RAW'
else if IsPatten(Buffer, 4, [$66, $74, $79, $70, $68, $65, $69, $63]) then Result := 'RAW'
else if IsPatten(Buffer, 0, [$25, $50, $44, $46]) then Result := 'PDF'
else if IsPatten(Buffer, 0, [$89, $50, $4E, $47, $0D, $0A, $1A, $0A]) then Result := 'PNG';
end;
Stream.Free;
except
on e: Exception do
begin
end;
end;
end;
[/code]