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

📄 filesplitfrm.pas

📁 MiniHex 1.1 源程序说明 “MiniHex11SrcSource”目录中的所有文件是MiniHex 1.1的主程序; “MiniHex11SrcControls”目录中的是该软件
💻 PAS
字号:
unit FileSplitFrm;

interface

uses
  Windows, Messages, SysUtils, Classes,
  Graphics, Controls, Forms, Dialogs,
  ComCtrls, StdCtrls, Grids, Buttons,
  ExtCtrls, FileCtrl;

type
  TFileSplitForm = class(TForm)
    Label1: TLabel;
    FileNameEdit: TEdit;
    Label3: TLabel;
    DirEdit: TEdit;
    BrowseFileNameButton: TButton;
    OpenDialog: TOpenDialog;
    BrowseDirButton: TButton;
    GroupBox1: TGroupBox;
    Label4: TLabel;
    GroupBox2: TGroupBox;
    SameSplitRadioButton: TRadioButton;
    DiffSplitRadioButton: TRadioButton;
    Label5: TLabel;
    SameBytesComboBox: TComboBox;
    Label6: TLabel;
    OkButton: TButton;
    CanclButton: TButton;
    BytesListBox: TListBox;
    Inp144Button: TSpeedButton;
    Inp360Button: TSpeedButton;
    AddByteItemButton: TButton;
    DelByteItemButton: TButton;
    ClearByteItemButton: TButton;
    Inp288Button: TSpeedButton;
    Inp720Button: TSpeedButton;
    Bevel1: TBevel;
    AddByteEdit: TEdit;
    procedure OkButtonClick(Sender: TObject);
    procedure CanclButtonClick(Sender: TObject);
    procedure BrowseFileNameButtonClick(Sender: TObject);
    procedure BrowseDirButtonClick(Sender: TObject);
    procedure SameSplitRadioButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure AddByteItemButtonClick(Sender: TObject);
    procedure DelByteItemButtonClick(Sender: TObject);
    procedure ClearByteItemButtonClick(Sender: TObject);
    procedure Inp144ButtonClick(Sender: TObject);
    procedure AddByteEditKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
    function CheckValid: Boolean;
    function Split: Boolean;
  public
    { Public declarations }
  end;

var
  FileSplitForm: TFileSplitForm;

procedure ShowFileSplitForm;

implementation

{$R *.DFM}

uses
  Misc;

procedure ShowFileSplitForm;
var
  Frm: TFileSplitForm;
begin
  Frm := TFileSplitForm.Create(Application);
  Frm.ShowModal;
  Frm.Free;
end;

function TFileSplitForm.CheckValid: Boolean;
begin
  Result := True;
  if FileNameEdit.Text = '' then
  begin
    Result := False;
    FileNameEdit.SetFocus;
    Exit;
  end;
  if DirEdit.Text = '' then
  begin
    Result := False;
    DirEdit.SetFocus;
    Exit;
  end;
  if SameSplitRadioButton.Checked then
  begin
    if not IsInt(SameBytesComboBox.Text) then
    begin
      Result := False;
      SameBytesComboBox.SetFocus;
      Exit;
    end
    else if StrToInt(SameBytesComboBox.Text) <= 0 then
    begin
      Result := False;
      SameBytesComboBox.SetFocus;
      Exit;
    end;
  end else
  begin
    if BytesListBox.Items.Count = 0 then
    begin
      Result := False;
      MessageBox(Handle, '列表中的项目数必须大于0。', '提示', 48);
      Exit;
    end;
  end;
end;

function TFileSplitForm.Split: Boolean;
var
  FromFileName: string;
  ToDir: string;
  SameBlock: Boolean;
  BlockSize: Integer;
  BlockSizes: array of Integer;
  i, j, Idx: Integer;
  FromFile, ToFile: TFileStream;
  ToFileName: string;
begin
  FromFileName := FileNameEdit.Text;
  ToDir := AddDirSuffix(DirEdit.Text);
  SameBlock := SameSplitRadioButton.Checked;
  BlockSize := 1;
  if SameBlock then
  begin
    BlockSize := StrToInt(SameBytesComboBox.Text);
  end else
  begin
    SetLength(BlockSizes, BytesListBox.Items.Count);
    for i := 0 to High(BlockSizes) do
      BlockSizes[i] := StrToInt(BytesListBox.Items[i]);
  end;
  ForceDirectories(ToDir);
  try
    FromFile := TFileStream.Create(FromFileName, fmOpenRead);
  except
    MessageBox(Handle, '文件无法打开。', '错误', 16);
    Result := False;
    Exit;
  end;
  try
    FromFile.Position := 0;
    if SameBlock then
    begin
      Idx := 0;
      while True do
      begin
        if FromFile.Position = FromFile.Size then Break;
        ToFileName := ToDir + ExtractFileName(FromFileName) + '.' + IntToStr(Idx);
        ToFile := TFileStream.Create(ToFileName, fmCreate);
        if FromFile.Position + BlockSize > FromFile.Size then
          j := FromFile.Size - FromFile.Position
        else
          j := BlockSize;
        ToFile.CopyFrom(FromFile, j);
        ToFile.Free;
        Inc(Idx);
      end;
    end else
    begin
      Idx := 0;
      while True do
      begin
        if FromFile.Position = FromFile.Size then Break;
        ToFileName := ToDir + ExtractFileName(FromFileName) + '.' + IntToStr(Idx);
        ToFile := TFileStream.Create(ToFileName, fmCreate);
        if Idx <= High(BlockSizes) then
          j := BlockSizes[Idx]
        else
          j := FromFile.Size - FromFile.Position;
        if FromFile.Position + j > FromFile.Size then
          j := FromFile.Size - FromFile.Position;
        ToFile.CopyFrom(FromFile, j);
        ToFile.Free;
        Inc(Idx);
      end;
    end;
  finally
    FromFile.Free;
    SetLength(BlockSizes, 0);
  end;
  Result := True;
end;

procedure TFileSplitForm.FormCreate(Sender: TObject);
begin
  SameSplitRadioButtonClick(nil);
end;

procedure TFileSplitForm.OkButtonClick(Sender: TObject);
begin
  if CheckValid then
  begin
    Split;
    ModalResult := mrOk;
  end;
end;

procedure TFileSplitForm.CanclButtonClick(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

procedure TFileSplitForm.BrowseFileNameButtonClick(Sender: TObject);
begin
  if OpenDialog.Execute then
    FileNameEdit.Text := OpenDialog.FileName;
end;

procedure TFileSplitForm.BrowseDirButtonClick(Sender: TObject);
var
  Path: string;
  Ok: Boolean;
begin
  Path := '';
  Ok := SelectDir(Handle, '请选择一个文件夹:', '', Path);
  if Ok then DirEdit.Text := AddDirSuffix(Path);
end;

procedure TFileSplitForm.SameSplitRadioButtonClick(Sender: TObject);
var
  B: Boolean;
begin
  B := SameSplitRadioButton.Checked;
  SameBytesComboBox.Enabled := B;
  AddByteEdit.Enabled := not B;
  BytesListBox.Enabled := not B;
  AddByteItemButton.Enabled := not B;
  DelByteItemButton.Enabled := not B;
  ClearByteItemButton.Enabled := not B;
  if B then
  begin
    SameBytesComboBox.Color := clWindow;
    AddByteEdit.Color := clBtnFace;
    BytesListBox.Color := clBtnFace;
  end else
  begin
    SameBytesComboBox.Color := clBtnFace;
    AddByteEdit.Color := clWindow;
    BytesListBox.Color := clWindow;
  end;
end;

procedure TFileSplitForm.AddByteItemButtonClick(Sender: TObject);
begin
  if IsInt(AddByteEdit.Text) then
  begin
    if StrToInt(AddByteEdit.Text) <= 0 then Exit;
    BytesListBox.Items.Add(AddByteEdit.Text);
    AddByteEdit.Text := '';
    AddByteEdit.SetFocus;
  end;
end;

procedure TFileSplitForm.DelByteItemButtonClick(Sender: TObject);
begin
  if BytesListBox.ItemIndex = -1 then Exit;
  BytesListBox.Items.Delete(BytesListBox.ItemIndex);
end;

procedure TFileSplitForm.ClearByteItemButtonClick(Sender: TObject);
begin
  BytesListBox.Items.Clear;
end;

procedure TFileSplitForm.Inp144ButtonClick(Sender: TObject);
begin
  SameBytesComboBox.Text := IntToStr((Sender as TSpeedButton).Tag);
end;

procedure TFileSplitForm.AddByteEditKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key = #13 then
  begin
    Key := #0;
    AddByteItemButtonClick(nil);
  end;
end;

end.

⌨️ 快捷键说明

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