解决方案 »

  1.   

    unit mgr;interfaceuses
      Windows, Messages, SysUtils, Classes, Forms,
      StdCtrls,shlobj, Controls, Dialogs,shellapi;type
      TForm1 = class(TForm)
        GroupBox1: TGroupBox;
        Button1: TButton;
        OpenDialog1: TOpenDialog;
        SaveDialog1: TSaveDialog;
        Button3: TButton;
        GroupBox2: TGroupBox;
        ListBox1: TListBox;
        procedure Button1Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure SaveDialog1CanClose(Sender: TObject; var CanClose: Boolean);
        procedure ListBox1DblClick(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
      private
        { Private declarations }
        fstream1:tfilestream;
        fstream2:tfilestream;
        list:tstrings;
        len:tstrings;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}
    const
      flen=136192;  //请注意修改这儿的长度type
      FILE_INFO=record
        filename:array[0..MAX_PATH] of char;
        len:integer;
      end;function SelectDirectory(handle:hwnd;const Caption: string; const Root: WideString;out Directory: string): Boolean;
    var
      lpbi:_browseinfo;
      buf:array [0..MAX_PATH] of char;
      id:ishellfolder;
      eaten,att:cardinal;
      rt:pitemidlist;
      initdir:pwidechar;
    begin
      result:=false;
      lpbi.hwndOwner:=handle;
      lpbi.lpfn:=nil;
      lpbi.lpszTitle:=pchar(caption);
      lpbi.ulFlags:=BIF_RETURNONLYFSDIRS+BIF_EDITBOX;
      SHGetDesktopFolder(id);
      initdir:=pwchar(root);
      id.ParseDisplayName(0,nil,initdir,eaten,rt,att);
      lpbi.pidlRoot:=rt;
      getmem(lpbi.pszDisplayName,MAX_PATH);
      try
       result:=shgetpathfromidlist(shbrowseforfolder(lpbi),buf);
      except
       freemem(lpbi.pszDisplayName);
      end;
      if result then
      begin
       directory:=buf;
       if length(directory)<>3 then directory:=directory+'\';
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      info:FILE_INFO;
      i:integer;
      buf:array[0..4096] of byte;
      s:integer;
    begin
      if savedialog1.Execute then
      if opendialog1.Execute then
      begin
        try
          copyfile(pchar(paramstr(0)),pchar(savedialog1.FileName),false);
          fstream1:=tfilestream.Create(pchar(savedialog1.FileName),fmopenreadwrite);
          fstream1.Seek(flen,soFromBeginning);
          for i:=0 to opendialog1.Files.Count-1 do
          begin
            strpcopy(info.filename,extractfilename(opendialog1.files.strings[i]));
            fstream2:=tfilestream.Create(opendialog1.Files.Strings[i],fmopenread);
            info.len:=fstream2.Size;
            fstream1.Write(info,sizeof(info));
            while fstream2.Position<>fstream2.Size do
            begin
            s:=fstream2.Read(buf,sizeof(buf));
            fstream1.Write(buf,s);
            end;
            fstream2.Free;
          end;
        finally
          fstream1.Free;
        end;
      end;
    end;procedure TForm1.Button3Click(Sender: TObject);
    var
      f:textfile;
      info:FILE_INFO;
      i:integer;
      buf:array[0..4096] of byte;
      s:integer;
      count,b:integer;
      dir:string;
    begin
    if selectdirectory(handle,'选择输出文件夹','',dir) then
       try
          fstream1:=tfilestream.Create(paramstr(0),fmShareDenyWrite);
          fstream1.Seek(flen,soFromBeginning);
          while fstream1.Position<>fstream1.Size do
          begin
            fstream1.Read(info,sizeof(info));
            count:=0;
            assignfile(f,dir+info.filename);
            rewrite(f);
            closefile(f);
            fstream2:=tfilestream.Create(dir+info.filename,fmopenwrite);
            fstream2.Size:=0;
            i:=info.len div sizeof(buf);
            for b:=1 to i do
            begin
            s:=fstream1.Read(buf,sizeof(buf));
            fstream2.Write(buf,s);
            inc(count,s);
            end;
            s:=fstream1.Read(buf,info.len-count);
            fstream2.Write(buf,s);
            fstream2.Free;
          end;
        finally
          fstream1.Free;
      end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
      info:FILE_INFO;
    begin
       list:=tstringlist.Create;
       len:=tstringlist.Create;
       try
          fstream1:=tfilestream.Create(paramstr(0),fmShareDenyWrite);
          fstream1.Seek(flen,soFromBeginning);
          while fstream1.Position<>fstream1.Size do
          begin
            fstream1.Read(info,sizeof(info));
            list.Add(inttostr(fstream1.position));
            len.Add(inttostr(info.len));
            listbox1.Items.Add(info.filename);
            fstream1.Seek(info.len,soFromCurrent);
          end;
        finally
          fstream1.Free;
      end;
      if listbox1.Items.Count>0 then button3.Enabled:=true else button3.Enabled:=false;
    end;procedure TForm1.SaveDialog1CanClose(Sender: TObject;
      var CanClose: Boolean);
    var
      f:integer;
    begin
      f:=filecreate(savedialog1.FileName);
      if f<=0 then
      begin
        MessageBox(handle,'不能选择输出到该文件!',pchar(application.Title),MB_OK+MB_ICONerror);
        canclose:=false;
      end;
      fileclose(f);
    end;procedure TForm1.ListBox1DblClick(Sender: TObject);
    var
      path:array[0..max_path] of char;
      filename:string;
      f,b,s,count:integer;
      buf:array[0..4096] of char;
    begin
      if button3.Enabled=false then exit;
      gettemppath(Max_path,path);
      filename:=path+listbox1.Items.Strings[listbox1.itemindex];
      fstream1:=tfilestream.Create(paramstr(0),fmShareDenyWrite);
      f:=filecreate(filename);
      fileclose(f);
      count:=0;
      fstream2:=tfilestream.Create(filename,fmopenwrite);
      fstream1.Seek(strtoint(list.Strings[listbox1.ItemIndex]),sofrombeginning);
      f:=strtoint(len.Strings[listbox1.itemindex]) div sizeof(buf);
      for b:=1 to f do
      begin
        s:=fstream1.Read(buf,sizeof(buf));
        fstream2.Write(buf,s);
        inc(count,s);
      end;
      s:=fstream1.Read(buf,strtoint(len.Strings[listbox1.itemindex])-count);
      fstream2.Write(buf,s);
      fstream2.Free;
      shellexecute(handle,'open',pchar(filename),'','',sw_show);
      fstream1.Free;
    end;procedure TForm1.FormDestroy(Sender: TObject);
    var
     i:integer;
     path:array[0..max_path] of char;
     filename:string;
    begin
      list.Free;
      len.Free;
      gettemppath(Max_path,path);
      for i:=0 to listbox1.Items.Count-1 do
      begin
        filename:=path+listbox1.Items.Strings[i];
        deletefile(filename);
      end;
    end;end.
      

  2.   

    /绑定多个文件为一个合成文件
    bool CBindFileDlg::Bind_Files()
    {
         FILE* myself;   //自身文件
         FILE* out;      //最终合成文件
         FILE* in;       //待绑定文件
         int bytesin;
         int totalbytes = 0;
         struct _stat ST;
         unsigned int finder = 0x12345678;
         unsigned int i, k;
     int l=1;                //状态显示
     char buff[20];         //状态显示     his_name = strFirstFilePath; //第一个绑定的文件名     _stat(my_name, &ST);
         modify_data.my_length = ST.st_size;
         if (modify_data.my_length == 0)
         {
              MessageBox("绑定文件中,自身文件长度为零时出错!","错误");
      return false;
         }     buf = (BYTE *)malloc(modify_data.my_length);
         if (buf == NULL)
         {
              MessageBox("绑定文件中,分配自身文件长度时出错!","错误");
      return false;
         }     myself = fopen(my_name, "rb");  //打开自身文件
         if (myself == NULL)
         {
              free(buf);
              MessageBox("绑定文件中,打开自身文件时出错!","错误");
      return false;
         }     bytesin = fread(buf, 1, modify_data.my_length, myself);
         fclose(myself);     if (bytesin != modify_data.my_length)
         {
              free(buf);
              MessageBox("绑定文件中,不能完全读取自身文件内容时出错!","错误");
      return false;
         }     for (i = 0; i < modify_data.my_length - sizeof(finder); i += sizeof(finder))
         {
              for (k = 0; k < sizeof(finder); k++)
              {
                   if (buf[i+k] != ((BYTE*)&finder)[k])
                        break;
              }
              if (k == sizeof(finder))   //定位并保存自身数据文件大小
              {
                   memcpy(buf+ i, &modify_data, sizeof(modify_data));
                   break;
              }
         }     if (i >= modify_data.my_length - sizeof(finder))
         {
              free(buf);
              MessageBox("绑定文件中,不能定位自身文件时出错!","错误");
      return false;
         }     if (_stat(strFirstFilePath, &ST) != 0 || ST.st_size == 0)
         {
              free(buf);
              MessageBox("绑定文件中,读取第一个要绑定文件时出错!","错误");
      return false;
         }     list_my_icons();     out = fopen(strFinalFilePath, "wb"); //创建最终合成文件
         if (out == NULL)
         {
              free(buf);
              MessageBox("绑定文件中,创建绑定后生成的合成文件时出错!","错误");
      return false;
         }     totalbytes += fwrite(buf, 1, bytesin, out);     in = fopen(strFirstFilePath, "rb");  //打开第一个要绑定的文件
         if (in == NULL)
         {
              free(buf);
              MessageBox("绑定文件中,打开第一个要绑定文件时出错!","错误");
      return false;
         }  //写入第一个要绑定文件的长度到合成文件中
         totalbytes += fwrite(&ST.st_size, 1, sizeof(ST.st_size), out);  //写入最终分解后文件执行方式的标志位(同步或异步执行)
     UpdateData(TRUE);  //传控件值到变量m_Sync中
     totalbytes += fwrite(&m_Sync, 1, sizeof(int), out);     while (bytesin = fread(buf, 1, modify_data.my_length, in))
         {
              totalbytes += fwrite(buf, 1, bytesin, out);
         }
         fclose(in); //关闭第一个绑定文件句柄  //设置进度条显示
         m_Progress.SetRange(0,500);
         for (int m = 0; m < 500; m++)
    m_Progress.SetPos(m);
     m_Parts = _ltoa(l, buff, 10);
     m_Parts += _T("个文件已绑定");
     UpdateData(FALSE);
     l++;     in = fopen(strSecondFilePath, "rb");   //打开第二个要绑定的文件
         if (in == NULL)
         {
              free(buf);
              MessageBox("绑定文件中,打开第二个要绑定文件时出错!","错误");
      return false;
         }
         while (bytesin = fread(buf, 1, modify_data.my_length, in))
         {
              totalbytes += fwrite(buf, 1, bytesin, out);
         }  //设置进度条显示
         m_Progress.SetRange(0,500);
     for (int n = 0; n < 500; n++)
    m_Progress.SetPos(n);
     m_Parts = _ltoa(l, buff, 10);
     m_Parts += _T("个文件已绑定");
     UpdateData(FALSE);
     l++;     fclose(in);  //关闭第二个绑定文件句柄
         fclose(out); //关闭最终合成文件句柄
         free(buf);   //释放缓冲区  return true;
    }//打开“关于”对话框
    void CBindFileDlg::OnButtonAbout() 
    {
    CAboutDlg dlgAbout; dlgAbout.DoModal();

    }//选取第一个要绑定的文件
    void CBindFileDlg::OnFirstBrowse() 
    {
        CFileDialog fileDialog(TRUE,NULL,NULL,NULL,"可执行文件(*.exe)|*.exe|所有文件(*.*)|*.*||"); if (fileDialog.DoModal() == IDOK) 
    {
    strFirstFilePath = fileDialog.GetPathName(); //保存第一个绑定文件名
    m_strFirstPath = strFirstFilePath; UpdateData(FALSE);
    }
    }//选取第二个要绑定的文件
    void CBindFileDlg::OnSecondBrowse() 
    {
    CFileDialog fileDialog(TRUE,NULL,NULL,NULL,"可执行文件(*.exe)|*.exe|所有文件(*.*)|*.*||"); if (fileDialog.DoModal() == IDOK) 
    {
    strSecondFilePath = fileDialog.GetPathName(); //保存第二个绑定文件名
    m_strSecondPath = strSecondFilePath; UpdateData(FALSE);
    }

    }//选择绑定后生成的合成文件名
    void CBindFileDlg::OnFinalBrowse() 
    {
        CFileDialog fileDialog( FALSE,"*.exe",NULL,NULL,"可执行文件(*.exe)|*.exe||");
    if (fileDialog.DoModal() == IDOK) 
    {
    strFinalFilePath = fileDialog.GetPathName();   //保存第终合成的文件名
            m_strFinalPath = strFinalFilePath; UpdateData(FALSE);
    }
    }BOOL CAboutDlg::OnInitDialog() 
    {
    CDialog::OnInitDialog();

    //设置“关于”对话框的位图按钮显示
    m_AboutOK.LoadBitmaps(IDB_BITMAP1,5, 5, 5, 5, 4 );

    return TRUE;  // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
    }//进行绑定文件操作
    void CBindFileDlg::OnButtonBindFile() 
    {
         if(strFirstFilePath=="" || strSecondFilePath=="" ||strFinalFilePath=="")
     {
     MessageBox("请先选择要进行绑定的文件和最终合成的目标文件名!","提示",MB_ICONINFORMATION);
     return;
     }      if(Bind_Files())  //绑定文件
     MessageBox("文件捆绑成功!","提示",MB_ICONINFORMATION);
    }
      

  3.   

    unit unitFrmMain;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, ComCtrls, FileCtrl;type
      TForm1 = class(TForm)
        OpenDialog1: TOpenDialog;
        SaveDialog1: TSaveDialog;
        UpDown1: TUpDown;
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        Button4: TButton;
        Button5: TButton;
        ListBox1: TListBox;
        Edit2: TEdit;
        Edit3: TEdit;
        Label1: TLabel;
        Label2: TLabel;
        Edit1: TEdit;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);
        procedure Button5Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}
    //选择待分割的文件procedure TForm1.Button1Click(Sender: TObject);
    begin
      if OpenDialog1.Execute then
        begin
          Edit2.Text := OpenDialog1.FileName;
        end;
    end;//设置分割后的分卷文件的存储位置procedure TForm1.Button2Click(Sender: TObject);
    var
      Dir: string;
    begin
      if SelectDirectory('选择存储目录:', '', Dir) then
        begin
          Edit3.Text := Dir + '\';
        end;end;//分割文件为分卷文件,自动命名procedure TForm1.Button3Click(Sender: TObject);
    var
      I, K, Rest, Len: Integer;
      AF, BF: TFileStream; //文件流对象
      Fn: string; //文件名
      Buf: Integer; //单碟容量
    begin  if (Edit2.Text = '') or (Edit3.Text = '') then
        begin
          Beep;
          Exit;
        end;
      //单碟容量计算为字节数  buf := StrToInt(Edit1.Text) * 1024;
      I := 1;
      try
        Af := TFileStream.Create(Edit2.Text, fmOpenRead);
        K := Af.Size div Buf;
        Rest := Af.Size - K * Buf;
        for I := 1 to K do
          begin
            Fn := Edit3.Text + IntToStr(I) + '.spt'; //文件名
            Bf := TFileStream.Create(Fn, fmCreate);
            Bf.CopyFrom(Af, Buf);
            ListBox1.Items.Add(Fn);
            Bf.Free;
          end;
        if Rest > 0 then
          begin
            Fn := Edit3.Text + IntToStr(I) + '.spt';
            Bf := TFileStream.Create(Fn, fmCreate);
            Bf.CopyFrom(Af, Rest);
            Bf.Free;
            ListBox1.Items.Add(Fn);
          end;
      finally
        AF.Free;
      end;end;//将分割后的分卷文件在列表框中显示procedure TForm1.Button4Click(Sender: TObject);
    var
      I: Integer;
    begin
      if OpenDialog1.Execute then
        for I := 0 to OpenDialog1.Files.Count - 1 do
          ListBox1.Items.Add(OpenDialog1.files[I]);
    end;//合并分卷文件并保存合并结果procedure TForm1.Button5Click(Sender: TObject);
    var
      Af, Bf: TFileStream; //文件流对象
      Fn, Ft: string; //文件名
      I: Integer;
    begin
      if ListBox1.Items.Count = 0 then
        begin
          Beep;
          Exit;
        end;
      if SaveDialog1.Execute then
        begin
          Fn := SaveDialog1.FileName;
          try
            Af := TFileStream.Create(Fn, fmCreate);
            for I := 0 to ListBox1.Items.Count - 1 do
              begin
                Ft := ListBox1.Items[I];
                Bf := TFileStream.Create(Ft, fmOpenRead);
                Af.CopyFrom(Bf, Bf.Size);
                Bf.Free;
              end;
          finally
            Af.Free;
          end;
        end;
    end;
    end.