Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Dateityp aus Dateiheader herausfinden (https://www.delphipraxis.net/164965-dateityp-aus-dateiheader-herausfinden.html)

DrD0j0 7. Dez 2011 20:41

Delphi-Version: 2009

Dateityp aus Dateiheader herausfinden
 
Hallo,

gibt es eine Möglichkeit aus einer Datei den Dateiheader auszulesen und daraus zu erkennen um was für einen Dateityp es sich handelt. Zum Beispiel, ob es eine JPEG-Datei ist?

Wenn ja,
wie kann ich den Dateiheader auslesen?
wie bekomme ich den Dateityp heraus?


Vielen Dank schon einmal für eure Hilfe :)

bernerbaer 7. Dez 2011 20:51

AW: Dateityp aus Dateiheader herausfinden
 
Informationen zu vielen Dateiformaten findet man unter wotsit.org

himitsu 7. Dez 2011 21:18

AW: Dateityp aus Dateiheader herausfinden
 
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 = '%PDF' then ContentType := 'application/pdf'
    //else if H = 'PK¹¹' then ContentType := 'application/zip'
      else if H = 'Rar!' then ContentType := 'application/x-rar-compressed'
      else if H = 'GIF8' then ContentType := 'image/gif'
      else if K = $FFD8  then ContentType := 'image/jpeg'
      else if H = '²PNG' then 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 = '<?xm' then 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 :cry: ... im Zitat/Beitragseditor sieht es hübscher aus

DrD0j0 7. Dez 2011 21:29

AW: Dateityp aus Dateiheader herausfinden
 
Cool, Danke himitsu :)

Ist es korrekt, dass du dir die Dateien einfach mit Hex-Editor angeschaut hast und dann dementsprechend eingefügt hast?

himitsu 7. Dez 2011 23:39

AW: Dateityp aus Dateiheader herausfinden
 
Google : Dateierweiterung + fileheader
Google : Dateierweiterung + magic number
am Ende landet man jeweils bei Wiki, wotsit, und Co.
http://de.wikipedia.org/wiki/Portable_Network_Graphics > oben rechts das Infofensterchen

Wobei ich das einfache Prinzip der sogenannten "Magic Number" / "Magischen Zahl" ausgenutzt hab.


Alle Zeitangaben in WEZ +1. Es ist jetzt 21:20 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz