cl-cookbook icon indicating copy to clipboard operation
cl-cookbook copied to clipboard

Copying a file

Open renzo-orsini opened this issue 7 years ago • 0 comments

This is an efficient way of copying any kind of file, either character or binary.

(defun copy-file (from-file to-file)
  "copy the file from-file into to-file"
  (with-open-file (input-stream from-file
				:direction :input
				:element-type '(unsigned-byte 8))
    (with-open-file (output-stream to-file
				   :direction :output
				   :if-exists :supersede
				   :if-does-not-exist :create
				   :element-type '(unsigned-byte 8))
      (let ((buf (make-array 4096 :element-type (stream-element-type input-stream))))
	(loop for pos = (read-sequence buf input-stream)
	      while (plusp pos)
	      do (write-sequence buf output-stream :end pos))))))

renzo-orsini avatar Aug 30 '18 14:08 renzo-orsini