1、如何将多个文件及文件夹打包成一个文件?及如何将还原???
2、有:1.exe 2.exe 3.txt 用Tstreamfile类合并成一个文件后[123.exe],如何将其分离,并将其分别执行:执行:1.exe
                             2.exe
                             3.txt

解决方案 »

  1.   

    一个变通的方法:
    用Winrar的将1.exe+2exe+3.txt压缩成123.zip,,
    打开它,,--》生成释放文件--》高级自释放选项
    释放后运行1.exe  (2.exe  3.txt 好像没有办法执行)不知这个意见是否有用,,呵呵,懒得写代码,,
      

  2.   

    qqqqs(qqqqs)您好!用文件流确实可以将信息加入其中, 这我也试过了, 但:打成一个变成一个文件后,还原时《记录写入流的尾部》是从第几个字节开始了??????
    我看只有天知道,而且《记录写入流的尾部》文件的大小是随目录本身信息的多少而确定大小的
    这样行不通////////////////////////不过谢谢你的回答!
      

  3.   

    xiaoyuer0851(小鱼儿0851:1、用纯DELPHI代码,打包一个即有子目录又有文件的目录????且如何还原????但不能压缩(或用第三方控件)这是一个技术问题!
    2、1.exe 、2.exe、 3.txt三个文件打包成一个文件(4.exe), 当运行4.exe 时,等于运行了:1.exe 、2.exe、 3.txt 三个文件???? 用DELPHI代码
      

  4.   

    unit Unit1;
    interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Buttons, StdCtrls, Menus, ShellAPI;type
      TForm1 = class(TForm)
        ListBox1: TListBox;
        SpeedButton1: TSpeedButton;
        SpeedButton2: TSpeedButton;
        SpeedButton3: TSpeedButton;
        SpeedButton4: TSpeedButton;
        SpeedButton5: TSpeedButton;
        SpeedButton6: TSpeedButton;
        OpenDialog1: TOpenDialog;
        SaveDialog1: TSaveDialog;
        SpeedButton7: TSpeedButton;
        SpeedButton8: TSpeedButton;
        Label1: TLabel;
        procedure SpeedButton1Click(Sender: TObject);
        procedure SpeedButton2Click(Sender: TObject);
        procedure SpeedButton5Click(Sender: TObject);
        procedure SpeedButton6Click(Sender: TObject);
        procedure SpeedButton3Click(Sender: TObject);
        procedure SpeedButton7Click(Sender: TObject);
        procedure SpeedButton8Click(Sender: TObject);
        procedure SpeedButton4Click(Sender: TObject);
      private
        { Private declarations }
      protected
        function PackMultiFile(AFiles: TStrings; ADestFileName: string): Boolean; //打包
        function UnPackMultiFile(AFileName, AFilePath: string): Boolean; //解包    function GetMyTempFile(AFileName: string): string; //取临时文件名
        function GetTheFileSize(AFileName: string): DWord; //文件大小
        function GetLeftStr(var ASource: string; ASeperate: string = '|'): string;    function OpenAFile(ASourceFileName: string; Index: Integer): Boolean; //打开一个文件
        function UnPackAFile(ASourceFileName: string; Index: Integer; AFilePath: string): Boolean; //解开一个文件
        function GetAllFileName(ASourceFileName: string; AFiles: TStrings): Boolean; //所有文件名称
        function GetAllFileCount(ASourceFileName: string): Integer; //包中的文件数目
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation
    {$R *.dfm}{ TForm1 }function TForm1.GetLeftStr(var ASource: string; ASeperate: string): string;
    var
      I: Integer;
    begin
      Result := '';
      I := Pos(ASeperate, ASource);
      if I < 1 then Exit;
      Result := Copy(ASource, 1, I - 1);
      Delete(Asource, 1, I + Length(ASeperate) - 1);
    end;function TForm1.GetMyTempFile(AFileName: string): string;
    var
      C: PChar;
    begin
      Result := '';
      GetMem(C, 255);
      GetTempPath(255, C);
      Result := StrPas(C) + '\' + AFileName;
      FreeMem(C, 255);
    end;function TForm1.GetTheFileSize(AFileName: string): DWord;
    var
      hfile: Cardinal;
      dSize: DWord;
    begin
      Result := 0;
      hFile := CreateFile(PChar(AFileName), 0, FILE_SHARE_READ, nil, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
      if hFile = INVALID_HANDLE_value then Exit;
      dSize := GetFileSize(hFile, nil);
      CloseHandle(hFile);
      Result := dSize;
    end;function TForm1.OpenAFile(ASourceFileName: string; Index: Integer): Boolean;
    var
      mFileStream: TFileStream;  //需要解析的包文件
      mMemoryStream: TMemoryStream; //解析后的单个文件
      mList: TStringList; //包头(每行:文件名、开始位置、大小)
      I,
      iHeadLength, //包头长度
      iDataBegin,  //包内容的开始位置
      iFileBegin,  //文件的开始位置
      iFileSize: Integer; //文件大小
      S,
      sHeadContent, //包头内容
      sFileName: string;
      FBuffer: array of Byte;
    begin
      Result := True;
      if not FileExists(ASourceFileName) then Exit;  mList := TStringList.Create;
      mMemoryStream := TMemoryStream.Create;
      mFileStream := TFileStream.Create(ASourceFileName, fmOpenRead);
      try
        try
          //读包头的长度
          SetLength(FBuffer, 4);
          mFileStream.Read(FBuffer[0], 4);
          Move(FBuffer[0], iHeadLength, 4);      //读包头的内容
          FBuffer := nil; //清空FBuffer
          SetLength(FBuffer, iHeadLength);
          SetLength(sHeadContent, iHeadLength);
          mFileStream.Read(FBuffer[0], iHeadLength);  //读包头
          Move(FBuffer[0], sHeadContent[1], iHeadLength);  //包头内容-->HeadStr
          iDataBegin := mFileStream.Position;      mList.Text := sHeadContent;
          if (Index < 1) or (Index > mList.Count) then Exit;
          for I := 0 to mList.Count - 1 do
            if I = Index then
            begin
              S := mList[I];
              sFileName := GetLeftStr(S);
              iFileBegin := StrToInt(GetLeftStr(S));
              iFileSize := StrToInt(S);
              sFileName := GetMyTempFile(sFileName);          mMemoryStream.Clear;
              mFileStream.Position := iDataBegin + iFileBegin;
              mMemoryStream.CopyFrom(mFileStream, iFileSize);
              mMemoryStream.SaveToFile(sFileName);
              ShellExecute(0, 'open', PChar(sFileName), nil, nil, SW_Show);          
              Break;
            end;
          Result := True;
        except
          ;
        end;
      finally
        mList.Free;
        mMemoryStream.Free;
        mFileStream.Free;
        FBuffer := nil;
      end;
    end;function TForm1.PackMultiFile(AFiles: TStrings; ADestFileName: string): Boolean;
    var
      I, iPos, iSize,  //文件在包中的开始开始位置、文件大小
      iHeadLength: Integer;  //文件描述信息长度
      sHeadContent,  //文件描述信息
      sFileName: string; 
      mMemoryStream: TMemoryStream; //打包后的流
      mFileStream: TFileStream;
      FBuffer: array of Byte;
    begin
      Result := False;
      if not DirectoryExists(ExtractFileDir(ADestFileName)) then Exit;
      if not Assigned(AFiles) or (AFiles.Count = 0) then Exit;
      mMemoryStream := TMemoryStream.Create;  try
        try
          //写包头
          iPos := 0; iSize := 0;
          for I := 0 to AFiles.Count - 1 do
          begin
            if I = 0 then iPos := 0
            else iPos := iPos + iSize;
            iSize := GetTheFileSize(AFiles[I]);
            sFileName := ExtractFileName(AFiles[I]);
            sHeadContent := sHeadContent + sFileName + '|' + IntToStr(iPos) + '|' + IntToStr(iSize) + #13#10;
          end;
          iHeadLength := Length(sHeadContent);
          SetLength(FBuffer, iHeadLength + 4);
          Move(iHeadLength, FBuffer[0], 4);  //复制文件信息长度 1
          Move(sHeadContent[1], FBuffer[4], iHeadLength); //复制文件信息内容 2
          mMemoryStream.Write(FBuffer[0], iHeadLength + 4); //写包头信息(1 + 2)      //写包内容
          for I := 0 to AFiles.Count - 1 do
          begin
            if not FileExists(AFiles[I]) then Exit;
            mFileStream := TFileStream.Create(AFiles[I], fmOpenRead);
            mMemoryStream.CopyFrom(mFileStream, mFileStream.Size);
            mFileStream.Free;
          end;
          mMemoryStream.SaveToFile(ADestFileName);
          Result := True;
        except
          ;
        end;
      finally
        mMemoryStream.Free;
        FBuffer := nil;
      end;
    end;procedure TForm1.SpeedButton1Click(Sender: TObject);
    begin
      if OpenDialog1.Execute then
        ListBox1.Items.Add(OpenDialog1.FileName);
    end;procedure TForm1.SpeedButton2Click(Sender: TObject);
    begin
      ListBox1.DeleteSelected;
    end;
      

  5.   

    function TForm1.UnPackAFile(ASourceFileName: string; Index: Integer; AFilePath: string): Boolean;
    var
      mFileStream: TFileStream;  //需要解析的包文件
      mMemoryStream: TMemoryStream; //解析后的单个文件
      mList: TStringList; //包头(每行:文件名、开始位置、大小)
      I,
      iHeadLength, //包头长度
      iDataBegin,  //包内容的开始位置
      iFileBegin,  //文件的开始位置
      iFileSize: Integer; //文件大小
      S,
      sHeadContent, //包头内容
      sFileName: string;
      FBuffer: array of Byte;
    begin
      Result := False;
      if not FileExists(ASourceFileName) then Exit;
      if not DirectoryExists(AFilePath) then Exit;  mList := TStringList.Create;
      mMemoryStream := TMemoryStream.Create;
      mFileStream := TFileStream.Create(ASourceFileName, fmOpenRead);
      try
        try
          //读包头的长度
          SetLength(FBuffer, 4);
          mFileStream.Read(FBuffer[0], 4);
          Move(FBuffer[0], iHeadLength, 4);      //读包头的内容
          FBuffer := nil; //清空FBuffer
          SetLength(FBuffer, iHeadLength);
          SetLength(sHeadContent, iHeadLength);
          mFileStream.Read(FBuffer[0], iHeadLength);  //读包头
          Move(FBuffer[0], sHeadContent[1], iHeadLength);  //包头内容-->HeadStr
          iDataBegin := mFileStream.Position;      mList.Text := sHeadContent;
          if (Index < 1) or (Index > mList.Count) then Exit;
          for I := 0 to mList.Count - 1 do
          begin
            if I = Index then
            begin
              S := mList[I];
              sFileName := GetLeftStr(S);
              iFileBegin := StrToInt(GetLeftStr(S));
              iFileSize := StrToInt(S);
              if AFilePath[Length(AFilePath)] = '\' then sFileName := AFilePath + sFileName
              else sFileName := AFilePath + '\' + sFileName;          mMemoryStream.Clear;
              mFileStream.Position := iDataBegin + iFileBegin;
              mMemoryStream.CopyFrom(mFileStream, iFileSize);
              mMemoryStream.SaveToFile(sFileName);
              Break;
            end;
          end;
          Result := True;
        except
          ;
        end;
      finally
        mList.Free;
        mMemoryStream.Free;
        mFileStream.Free;
        FBuffer := nil;
      end;
    end;function TForm1.UnPackMultiFile(AFileName, AFilePath: string): Boolean;
    var
      mFileStream: TFileStream;  //需要解析的包文件
      mMemoryStream: TMemoryStream; //解析后的单个文件
      mList: TStringList; //包头(每行:文件名、开始位置、大小)
      I,
      iHeadLength, //包头长度
      iDataBegin,  //包内容的开始位置
      iFileBegin,  //文件的开始位置
      iFileSize: Integer; //文件大小
      S,
      sHeadContent, //包头内容
      sFileName: string;
      FBuffer: array of Byte;
    begin
      Result := False;
      if not DirectoryExists(AFilePath) then Exit;
      if not FileExists(AFileName) then Exit;  mList := TStringList.Create;
      mMemoryStream := TMemoryStream.Create;
      mFileStream := TFileStream.Create(AFileName, fmOpenRead);
      try
        try
          //读包头的长度
          SetLength(FBuffer, 4);
          mFileStream.Read(FBuffer[0], 4);
          Move(FBuffer[0], iHeadLength, 4);      //读包头的内容
          FBuffer := nil; //清空FBuffer
          SetLength(FBuffer, iHeadLength);
          SetLength(sHeadContent, iHeadLength);
          mFileStream.Read(FBuffer[0], iHeadLength);  //读包头
          Move(FBuffer[0], sHeadContent[1], iHeadLength);  //包头内容-->HeadStr
          iDataBegin := mFileStream.Position;      mList.Text := sHeadContent;
          for I := 0 to mList.Count - 1 do
          begin
            S := mList[I];
            sFileName := GetLeftStr(S);
            iFileBegin := StrToInt(GetLeftStr(S));
            iFileSize := StrToInt(S);
            if AFilePath[Length(AFilePath)] = '\' then sFileName := AFilePath + sFileName
            else sFileName := AFilePath + '\' + sFileName;        mMemoryStream.Clear;
            mFileStream.Position := iDataBegin + iFileBegin;
            mMemoryStream.CopyFrom(mFileStream, iFileSize);
            mMemoryStream.SaveToFile(sFileName);
          end;
          Result := True;
        except
          ;
        end;
      finally
        mList.Free;
        mMemoryStream.Free;
        mFileStream.Free;
        FBuffer := nil;
      end;
    end;procedure TForm1.SpeedButton5Click(Sender: TObject);
    begin
      if SaveDialog1.Execute then
        if PackMultiFile(ListBox1.Items, SaveDialog1.FileName) then
          ShowMessage('OK!')
        else
          ShowMessage('Error!');
    end;procedure TForm1.SpeedButton6Click(Sender: TObject);
    begin
      if OpenDialog1.Execute then
        if UnPackMultiFile(OpenDialog1.FileName, 'c:\') then
          ShowMessage('OK!!')
        else
          ShowMessage('Error!!');
    end;procedure TForm1.SpeedButton3Click(Sender: TObject);
    begin
      if OpenDialog1.Execute then
        if not OpenAFile(OpenDialog1.FileName, 1) then
          ShowMessage('Error!!');
    end;procedure TForm1.SpeedButton7Click(Sender: TObject);
    begin
      if OpenDialog1.Execute then
        if UnPackAFile(OpenDialog1.FileName, 1, 'c:\') then
          ShowMessage('OK!!');
    end;function TForm1.GetAllFileName(ASourceFileName: string;  AFiles: TStrings): Boolean;
    var
      mFileStream: TFileStream;  //需要解析的包文件
      mMemoryStream: TMemoryStream; //解析后的单个文件
      mList: TStringList; //包头(每行:文件名、开始位置、大小)
      I,
      iHeadLength: Integer; //包头长度
      S,
      sHeadContent, //包头内容
      sFileName: string;
      FBuffer: array of Byte;
    begin
      Result := False;
      if not FileExists(ASourceFileName) then Exit;
      if not Assigned(AFiles) then Exit;  mList := TStringList.Create;
      mMemoryStream := TMemoryStream.Create;
      mFileStream := TFileStream.Create(ASourceFileName, fmOpenRead);
      try
        try
          //读包头的长度
          SetLength(FBuffer, 4);
          mFileStream.Read(FBuffer[0], 4);
          Move(FBuffer[0], iHeadLength, 4);      //读包头的内容
          FBuffer := nil; //清空FBuffer
          SetLength(FBuffer, iHeadLength);
          SetLength(sHeadContent, iHeadLength);
          mFileStream.Read(FBuffer[0], iHeadLength);  //读包头
          Move(FBuffer[0], sHeadContent[1], iHeadLength);  //包头内容-->HeadStr      mList.Text := sHeadContent;
          for I := 0 to mList.Count - 1 do
          begin
            S := mList[I];
            sFileName := GetLeftStr(S);
            AFiles.Add(sFileName);
          end;
          Result := True;
        except
          ;
        end;
      finally
        mList.Free;
        mMemoryStream.Free;
        mFileStream.Free;
        FBuffer := nil;
      end;
    end;procedure TForm1.SpeedButton8Click(Sender: TObject);
    begin
      if OpenDialog1.Execute then
        GetAllFileName(OpenDialog1.FileName, ListBox1.Items);
    end;function TForm1.GetAllFileCount(ASourceFileName: string): Integer;
    var
      mFileStream: TFileStream;  //需要解析的包文件
      mMemoryStream: TMemoryStream; //解析后的单个文件
      mList: TStringList; //包头(每行:文件名、开始位置、大小)
      iHeadLength: Integer; //包头长度
      sHeadContent: string; //包头内容
      FBuffer: array of Byte;  
    begin
      Result := -1;
      if not FileExists(ASourceFileName) then Exit;  mList := TStringList.Create;
      mMemoryStream := TMemoryStream.Create;
      mFileStream := TFileStream.Create(ASourceFileName, fmOpenRead);
      try
        try
          //读包头的长度
          SetLength(FBuffer, 4);
          mFileStream.Read(FBuffer[0], 4);
          Move(FBuffer[0], iHeadLength, 4);      //读包头的内容
          FBuffer := nil; //清空FBuffer
          SetLength(FBuffer, iHeadLength);
          SetLength(sHeadContent, iHeadLength);
          mFileStream.Read(FBuffer[0], iHeadLength);  //读包头
          Move(FBuffer[0], sHeadContent[1], iHeadLength);  //包头内容-->HeadStr      mList.Text := sHeadContent;
          Result := mList.Count;
        except
          ;
        end;
      finally
        mList.Free;
        mMemoryStream.Free;
        mFileStream.Free;
        FBuffer := nil;
      end;
    end;procedure TForm1.SpeedButton4Click(Sender: TObject);
    begin
      if OpenDialog1.Execute then
        ShowMessage(IntToStr(GetAllFileCount(OpenDialog1.FileName)));
    end;end.
      

  6.   

    这是我借用的代码object Form1: TForm1
      Left = 188
      Top = 252
      BorderStyle = bsDialog
      Caption = 'Demo:多个文件打包/解包'
      ClientHeight = 247
      ClientWidth = 515
      Color = clBtnFace
      Font.Charset = GB2312_CHARSET
      Font.Color = clWindowText
      Font.Height = -12
      Font.Name = '宋体'
      Font.Style = []
      OldCreateOrder = False
      PixelsPerInch = 96
      TextHeight = 12
      object SpeedButton1: TSpeedButton
        Left = 431
        Top = 25
        Width = 72
        Height = 22
        Caption = '添加'
        Font.Charset = DEFAULT_CHARSET
        Font.Color = clBlue
        Font.Height = -12
        Font.Name = 'MS Sans Serif'
        Font.Style = [fsBold]
        ParentFont = False
        OnClick = SpeedButton1Click
      end
      object SpeedButton2: TSpeedButton
        Left = 431
        Top = 49
        Width = 72
        Height = 22
        Caption = '删除'
        Font.Charset = DEFAULT_CHARSET
        Font.Color = clBlue
        Font.Height = -12
        Font.Name = 'MS Sans Serif'
        Font.Style = [fsBold]
        ParentFont = False
        OnClick = SpeedButton2Click
      end
      object SpeedButton3: TSpeedButton
        Left = 431
        Top = 145
        Width = 72
        Height = 22
        Caption = 'OpenOne'
        OnClick = SpeedButton3Click
      end
      object SpeedButton4: TSpeedButton
        Left = 431
        Top = 217
        Width = 72
        Height = 22
        Caption = 'FileCount'
        OnClick = SpeedButton4Click
      end
      object SpeedButton5: TSpeedButton
        Left = 431
        Top = 77
        Width = 72
        Height = 22
        Caption = '打包'
        Font.Charset = GB2312_CHARSET
        Font.Color = clTeal
        Font.Height = -12
        Font.Name = '宋体'
        Font.Style = [fsBold]
        ParentFont = False
        OnClick = SpeedButton5Click
      end
      object SpeedButton6: TSpeedButton
        Left = 431
        Top = 101
        Width = 72
        Height = 22
        Caption = '解包'
        Font.Charset = GB2312_CHARSET
        Font.Color = clTeal
        Font.Height = -12
        Font.Name = '宋体'
        Font.Style = [fsBold]
        ParentFont = False
        OnClick = SpeedButton6Click
      end
      object SpeedButton7: TSpeedButton
        Left = 431
        Top = 169
        Width = 72
        Height = 22
        Caption = 'UnPackOne'
        OnClick = SpeedButton7Click
      end
      object SpeedButton8: TSpeedButton
        Left = 431
        Top = 193
        Width = 72
        Height = 22
        Caption = 'FileNames'
        OnClick = SpeedButton8Click
      end
      object Label1: TLabel
        Left = 11
        Top = 7
        Width = 136
        Height = 16
        Caption = '需要打包的文件:'
        Font.Charset = GB2312_CHARSET
        Font.Color = clWindowText
        Font.Height = -16
        Font.Name = '宋体'
        Font.Style = [fsBold]
        ParentFont = False
      end
      object ListBox1: TListBox
        Left = 8
        Top = 29
        Width = 409
        Height = 210
        ImeName = '中文 (简体) - 微软拼音'
        ItemHeight = 12
        TabOrder = 0
      end
      object OpenDialog1: TOpenDialog
        InitialDir = 'C:\xbl\Source'
        Left = 32
        Top = 64
      end
      object SaveDialog1: TSaveDialog
        InitialDir = 'c:\xbl'
        Left = 112
        Top = 64
      end
    end
      

  7.   

    To 小小  你这个....
    大家有兴趣去www.cndelphi.com里面看....