Coping of the files

 

I have diffculties with coping the files. Delphi don't want to compile LZCopy command.

this way it work very slow

 


pbBuf := PChar( LocalAlloc(LMEM_FIXED, 1) );

 

FileSeek(source,0,0);

FileSeek(dest,0,0);

repeat

    cbRead := Fileread(source, pbBuf, 1);

    FileWrite(dest, pbBuf, cbRead);

until (cbRead = 0);

 


Solution 1

[Niel Calitz, omremcon@iafrica.com]

 


{  You must add LZExpand to your uses clause  ea. USES LZExpand; }

function CopyFile(SrcF,DestF : string) : boolean;

var

  SFile,

  DFile : integer;

  Res   : longint;

  Msg   : string;

 

begin

  SFile := FileOpen(SrcF,0);        { Open ReadOnly = 0, Write=1, Readwrite=2}

  DFile := FileCreate(DestF);

  Res := LZCopy(SFile,DFile);

  FileClose(SFile);

  FileClose(DFile);

  if Res < 0 then

  begin

    Msg := 'Unknown error';

    case Res of

      LZERROR_BADINHANDLE   : Msg := 'Invalid Source file handle';

      LZERROR_BADOUTHANDLE  : Msg := 'Invalid Destination file handle';

      LZERROR_BADVALUE      : Msg := 'Input parameter is out of range';

      LZERROR_GLOBALLOC     : Msg := 'Insufficient memory for the required buffers';

      LZERROR_GLOBLOCK      : Msg := 'Internal data structure handle invalid';

      LZERROR_READ          : Msg := 'Source file format is not valid';

      LZERROR_UNKNOWNALG    : Msg := 'The Source file was compressed with an unrecognized compression algorithm';

      LZERROR_WRITE         : Msg := 'There is insufficient space for the output file';

    end;

    MessageDlg(Msg,mtERROR,[mbOK],0);

    result := FALSE

  end else

    result := TRUE;

end;

 


Solution 2

[Tadas Vizbaras, tavizb@rc.lrs.lt]

I'll bet it's slow! It's reading the file one character at a time... Try allocating 8192 bytes and reading 8192 bytes at a time. That should speed it up a bit...

Solution 3

[Sid Gudes, cougar@roadrunner.com]

The simplest way to copy files is this:

 


                VAR

                         sI,dI:Longint;

                        sD,sS:TFilename;

 

                USES LZExpand;

                        ............

                  sI := FileOpen(sS,fmShareDenyWrite);

                 dI := FileCreate(sD);

                  { Copy file }

                   CopyLZFile(sI,dI);

                  {close files}

                 FileClose(sI);

                 FileClose(dI);

                        ............