Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu
Online

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.205 Beiträge
 
Delphi 12 Athens
 
#3

AW: Dateityp aus Dateiheader herausfinden

  Alt 7. Dez 2011, 21:18
Es gibt auch schon hier und da fertige Funktionen.

Dieses Ungetüm hatte ich mir gestern zusammengeklickt und nun rate mal wo ich die ganzen Header-Infos her hatte?
Delphi-Quellcode:
procedure TSMStreamAttachmentLoader.LoadContentType;
var
  i: Int64;
  L: Integer;
  H: array[0..3] of AnsiChar;
  M: array[0..1] of AnsiChar absolute H;
  K: Word;
begin
  i := FData.Position;
  try
    ContentType := '';
    FData.Position := 0;
    if FData.Size >= SizeOf(H) then begin
      FData.ReadBuffer(H, SizeOf(H));
      for L := 0 to High(H) do
        if H[L] = #0 then H[L] := '°'
        else if H[L] <= #8 then H[L] := '¹'
        else if H[L] = #$89 then H[L] := '²';
      K := (Ord(H[0]) shl 8) or Ord(H[1]);
      if M = 'PK'   then ContentType := 'application/octet-stream'
      else if H = '%PDFthen ContentType := 'application/pdf'
    //else if H = 'PK¹¹' then ContentType := 'application/zip'
      else if H = 'Rar!then ContentType := 'application/x-rar-compressed'
      else if H = 'GIF8then ContentType := 'image/gif'
      else if K = $FFD8 then ContentType := 'image/jpeg'
      else if H = '²PNGthen ContentType := 'image/png'
      else if M = 'BM'   then ContentType := 'image/bmp'
      else if H = 'MM°*then ContentType := 'image/tiff'
      else if H = 'II*°then ContentType := 'image/tiff'
      else if H = '<?xmthen ContentType := 'text/xml'
      else if H = '<°?°then ContentType := 'text/xml';
    end;
  finally
    FData.Position := i;
  end;
end;
Nja, das ist keine 100%ige Prüfung ... wenn man sicher gehn will und auch noch die Unterformate genauer rausbekommen möchte, dann muß man schon mehr, als nur die 2-4 Byte prüfen.


[edit]
schottige Codeausrichtung ... im Zitat/Beitragseditor sieht es hübscher aus
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests

Geändert von himitsu ( 7. Dez 2011 um 23:42 Uhr)
  Mit Zitat antworten Zitat