2006. január 29., vasárnap

Divide a file into 1.44 mb volumes


Problem/Question/Abstract:

How can i divide a file into 1.44 mb volumes if file size is longer than floppy capacity?

Answer:

const
  MaxSize: Longint = 1440000; //byte

function ExtractFileNames(FileNames: string): string;
var
  S: string;
begin
  S := '';
  while Pos('.', FileNames) > 0 do
  begin
    S := S + Copy(FileNames, 1, Pos('.', FileNames) - 1);
    Delete(FileNames, 1, Pos('.', FileNames));
  end;
  result := S;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  InFile, OutFile: file;
  CopyBuffer: POINTER; { buffer for copying }
  iRecsOK, iRecsWr, index: Integer;
  sFileName, sFileExt, sFileFullName: string;
  fFileSize: file of Byte;
  Size: LongInt;
begin
  sFileFullName := 'C:\1\1.mp3';
  sFileName := ExtractFileName(sFileFullName);
  sFileExt := ExtractFileExt(sFileName);
  sFileName := ExtractFileNames(sFileName);

  ShowMessage(sFileFullName + #13 + sFilename + #13 + sFileExt);

  if FileExists(sFileFullName) then
  begin
    AssignFile(fFileSize, sFileFullName);
    FileMode := 0; {Set file access to read only }
    Reset(fFileSize);
    Size := FileSize(fFileSize); {Get File Size}
    CloseFile(fFileSize);
    ShowMessage(IntToStr(Size));

    if Size > MaxSize then
    begin {Divide}
      Getmem(CopyBuffer, MaxSize); { allocate the buffer }

      Assignfile(inFile, sFileFullName); //+ '.ZIP');
      Reset(inFile, 1);

      index := 1;
      repeat
        AssignFile(outFile, sFileName + '-' + IntToStr(index) + sFileExt);
        Rewrite(OutFile, 1);
        inc(index);
        BlockRead(InFile, CopyBuffer^, MaxSize, iRecsOK);
        BlockWrite(OutFile, CopyBuffer^, iRecsOK, iRecsWr);
        CloseFile(OutFile);
      until (iRecsOK < MaxSize);
      CloseFile(InFile);
      FreeMem(CopyBuffer, MaxSize); { free the buffer }
      ShowMessage('Done..!');

    end
    else
    begin
      ShowMessage('Do nothing..!');
    end;
  end
  else
    ShowMessage('File: ' + sFileFullName + ' not found');
end;

If you can put it back, you can use DOS copy function;

copy /b file-1.xxx+file-2.xxx+file-3.xxx file.mp3

Nincsenek megjegyzések:

Megjegyzés küldése