⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.~pas

📁 delphi写的扫描硬盘中jpg文件的代码
💻 ~PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, Buttons, StdCtrls, ExtCtrls,QDialogs,FileCtrl, Grids,
  DBGrids, ComCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    startsec: TEdit;
    endsec: TEdit;
    savedir: TEdit;
    size: TEdit;
    Button1: TButton;
    Button2: TButton;
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    Label5: TLabel;
    DataSource1: TDataSource;
    ADOQuery2: TADOQuery;
    StatusBar1: TStatusBar;
    ADOConnection2: TADOConnection;
    SpeedButton2: TSpeedButton;
    ListBox1: TListBox;
    Label6: TLabel;
    PB: TProgressBar;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  function TDCopyFiles(startSector,fileSize,filename,exname,DI:string):integer;
  function ExtFileSeek(Handle: Integer; const Offset: Int64; Origin: Integer): Int64;
  function Findjpgfilehead(startsec,endsec:string;ADOC:TADOConnection;ADOQ:TADOQuery;f:Tlabel;list:Tlistbox;pb:Tprogressbar):string;
  function FindjpgFileend(startsec,endsec,sizemax:string;lab:Tlabel):string;
  function killspace(s:string):string;
  function compareflag(text:string;offi:integer;step:integer):boolean;
var
  Form1: TForm1;
  drivers:pchar;
implementation

{$R *.dfm}
function killspace(s:string):string;
var
  i:integer;
  t:string;
begin
  for i:=0 to length(s)-1 do
  begin
    t:=t+copy(s,i*3+1,2);
  end;
  result:=t;
  //showmessage(vartostr(pos('ff',t)));
end;
function FindjpgFileHead(startsec,endsec:string;ADOC:TADOConnection;ADOQ:TADOQuery;f:Tlabel;list:Tlistbox;pb:Tprogressbar):string;
var
  Buf: array [0..8191] of Byte;     //数据缓冲区
  Buf1: array [0..511] of Byte;
  ShowText, TmpStr: string;
  StarSector,EndSector:integer;
  FileHandle: THandle;
  Count,i,j: Cardinal;
  Curr,pos: Int64;
begin
  adoc.Close;
  adoc.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb;Persist Security Info=True' ;
  adoq.Connection:=adoc;
  if adoq.Active then
  begin
    adoq.Close;
  end;
  adoq.SQL.Clear;
  adoq.SQL.Add('delete * from TD where 1=1');
  adoq.ExecSQL;
  adoq.Close;
  adoq.SQL.Clear;
  adoq.SQL.Add('select * from TD where 1=1');
  adoq.Open;
  begin
    StarSector:=strtoint(trim(startsec));
    EndSector:=strtoint(trim(endsec));
    Pos:=StarSector;
    pb.Position:=0;
    pb.min:=0;
    pb.Max:=(endSector-starsector);
    FileHandle := CreateFile(drivers, GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE,
      nil, OPEN_EXISTING, 0, 0);
    if FileHandle = INVALID_HANDLE_VALUE then
    begin
      MessageBox(GetDesktopWindow, '打开磁盘失败', 'ERROR', MB_OK);
      ExitProcess(0);
    end;
    while pos<(EndSector+16) do
    begin
      Curr:=Int64(pos*512);
      Curr:=ExtFileSeek(FileHandle,Curr,0);
      if FileRead(FileHandle, Buf, 8192)<>8192 then
      begin
        showmessage('磁盘读错误');
        ExitProcess(0);
      end else
      begin
      for count:=0 to 16 do
      begin
      i:=0;
      for j:=count*512 to (count+1)*512-1 do
      begin
        buf1[i]:=buf[j];
        i:=i+1;
      end;
        for i := Low(Buf1) to High(Buf1) do
        begin
          TmpStr :=TmpStr+ Format('%2.2x ', [Buf1[i]]);
        end;
        showText:=copy(TmpStr,1,8);
        TmpStr:='';
        f.Caption:=vartostr(pos);
        if showText='FF D8 FF'  then
        begin
          adoq.Insert;
          adoq.FieldByName('filestart').AsString:=vartostr(pos+(count));
          adoq.Post;
          list.Items.Add('找到一个目标,            位于  '+vartostr(pos+(count))+'  扇区')
        end;
      if pb.Position<pb.Max then
        pb.StepIt;
      end;

      end;
      pos:=pos+16;
      Application.ProcessMessages;
    end;
  end;
  result:='1';
  pb.Position:=0;
end;
function FindjpgFileend(startsec,endsec,sizemax:string;lab:Tlabel):string;
var
  Buf: array [0..511] of Byte;     //数据缓冲区
  ShowText, TmpStr: string;
  StarSector,EndSector:integer;
  FileHandle: THandle;
  ReadCount,i: Cardinal;
  Curr,pos1,offi,chushi,t: Int64;
  flag:boolean;
begin
  result:='0';
  begin
    StarSector:=strtoint(trim(startsec));
    EndSector:=strtoint(trim(endsec));
    Pos1:=StarSector;
    chushi:=pos1;
    FileHandle := CreateFile(drivers, GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE,
      nil, OPEN_EXISTING, 0, 0);
    if FileHandle = INVALID_HANDLE_VALUE then
    begin
      MessageBox(GetDesktopWindow, '打开磁盘失败', 'ERROR', MB_OK);
      ExitProcess(0);
    end;
    T:=(chushi+strtoint(sizemax) div 512);
    while (pos1<EndSector) and (pos1<T)  and (not flag) do
    begin
        Curr:=Int64(pos1*512);
        Curr:=ExtFileSeek(FileHandle,Curr,0);
        if FileRead(FileHandle, Buf, 512)<>512 then
        begin
          showmessage('磁盘读错误');
          ExitProcess(0);
        end else
        begin
          for i := Low(Buf) to High(Buf) do
          begin
            TmpStr :=TmpStr+ Format('%2.2x ', [Buf[i]]);
          end;
          showtext:=killspace(TmpStr);
          offi:=pos('FFD9',showtext);

          if (offi>0) and  compareflag(showtext,offi,4)then
          begin
            result:=vartostr(((pos1)*512+((offi+1) div 2))-chushi*512);
            flag:=true;
          end;
          TmpStr:='';
        end;
        lab.Caption:=vartostr(pos1);
        pos1:=pos1+1;
        Application.ProcessMessages;
      end;
  end;
end;

function ExtFileSeek(Handle: Integer; const Offset: Int64; Origin: Integer):Int64;
begin
  Result := Offset;
  Int64Rec(Result).Lo := SetFilePointer(THandle(Handle), Int64Rec(Result).Lo,
    @Int64Rec(Result).Hi, Origin);
end;
function TDCopyFiles(startSector,fileSize,filename,exname,DI:string):integer;
var
  Buf: array[0..511]  of Byte;     //数据缓冲区
  ShowText, TmpStr: string;
  StarSector:integer;
  FileHandle: THandle;
  i: Cardinal;
  Fsize:integer;
  Curr,pos,Compare: Int64;
  FileStream:Tfilestream;
  dir:string;
begin
  result:=strtoint(trim(filesize));
  begin
    StarSector:=strtoint(startSector);
    Fsize:=strtoint(trim(fileSize));
    dir:=di;
    FileHandle := CreateFile(drivers, GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE,
      nil, OPEN_EXISTING, 0, 0);
    if FileHandle = INVALID_HANDLE_VALUE then
    begin
      MessageBox(GetDesktopWindow, '打开磁盘失败', 'ERROR', MB_OK);
      ExitProcess(0);
    end;
      pos:=int64(StarSector);
      Curr:=Int64(pos*512);
      FileStream:=TFileStream.Create(dir+'\'+filename+'.'+exname,fmCreate);
      Compare:=(Curr+FSize);
      while Curr<=Compare do
      begin
        Curr:=ExtFileSeek(FileHandle,Curr,0);
        FileRead(FileHandle,buf,sizeof(buf));
        FileStream.Write(Buf,Sizeof(Buf));
        Curr:=Curr+512;
        Application.ProcessMessages;
      end;
        FileStream.Free;

  end;
end;
function compareflag(text:string;offi:integer;step:integer):boolean;
var
  temp,temp1,middle:string;
  i,j:integer;
begin

  temp:=text;
  j:=offi;
  middle:=copy(temp,j+step,(1024-(j+step))+1);
  for i:=0 to (1024-(j+step)) do
  begin
    temp1:=temp1+'0';
  end;
  if middle=temp1 then
  result:=true
  else
  result:=false;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
  button2.Enabled:=false;
  drivers:=pchar('\\.\Physicaldrive0');
  if adoconnection1.Connected then
  adoconnection1.Close;
  adoconnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb;Persist Security Info=True' ;
  adoconnection1.open;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  temp,temp1:array of string;
  i,j:integer;
  hello:string;
begin
  if button2.Enabled then
    button2.Enabled:=false;
  StatusBar1.Panels.Items[0].Text:='';
  listbox1.Clear;
  FindjpgFileHead(trim(startsec.text),trim(endsec.Text),adoconnection1,adoquery1,label5,listbox1,pb);
  if adoconnection1.Connected then
  adoconnection1.Close;
  adoconnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb;Persist Security Info=True' ;
  adoconnection1.open;
  adoquery1.Close;
  adoquery1.SQL.Clear;
  adoquery1.SQL.Add('select * from TD where 1=1 order by ID');
  adoquery1.Open;
  StatusBar1.Panels.Items[0].Text:='文件扫描完成,共找到'+vartostr(adoquery1.recordcount)+'个文件头,正在完成收尾工作...';
  adoquery1.First;
   if adoquery1.RecordCount>0 then
  begin
  setlength(temp,adoquery1.recordcount+1);
  setlength(temp1,adoquery1.recordcount);
  for i:=0 to adoquery1.RecordCount-1 do
  begin
    temp[i]:=adoquery1.FieldValues['filestart'];
    adoquery1.Next;
  end;

  temp[adoquery1.RecordCount]:=vartostr(strtoint(temp[adoquery1.RecordCount-1])+strtoint(trim(size.text)) div 512 );
  for i:=low(temp)to  high(temp)-1 do
  begin
    temp1[i]:=FindjpgFileend(temp[i],temp[i+1],trim(size.text),label5);
  end;
  adoquery1.First;
  for i:=0 to adoquery1.RecordCount-1 do
  begin
    adoquery1.Edit;
    adoquery1.FieldByName('filesize').AsString:=vartostr(strtoint(temp1[i]));
    adoquery1.Post;
    adoquery1.Next;
  end;
   StatusBar1.Panels.Items[0].Text:='文件扫描完成';
   button2.Enabled:=true;
  end else
   StatusBar1.Panels.Items[0].Text:='没有找到任何目标对象';

end;

procedure TForm1.Button2Click(Sender: TObject);
var
  i:integer;
begin
  if savedir.Text='' then
  begin
    showmessage('没有选择文件的保存目录');
    exit;
  end else
  begin
    if adoquery1.Active then
    adoquery1.Close;
    adoquery1.SQL.Clear;
    adoquery1.SQL.Add('select * from TD where filesize<>''0''');
    adoquery1.Open;
    if adoquery1.RecordCount>0 then
    begin
      for i:=0 to adoquery1.RecordCount-1 do
      begin
        TDCopyFiles(adoquery1.FieldValues['filestart'],adoquery1.FieldValues['filesize'],'TD_XK_'+inttostr(i),'jpg',trim(savedir.Text));
        StatusBar1.Panels.Items[0].Text:='已恢复'+inttostr(i+1)+'个文件!';
        adoquery1.Next;
      end;
    end else
      showmessage('没有可以恢复的jpg文件!');
      exit;
  end;
end;

procedure TForm1.SpeedButton2Click(Sender: TObject);
var
  dir:string;
begin
  if SelectDirectory(dir, [sdAllowCreate, sdPerformCreate, sdPrompt],0) then
  begin
    savedir.text:=dir;
  end else
    Exit;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -