Function: archive-copy-file

archive-copy-file is an interactive and byte-compiled function defined in arc-mode.el.gz.

Signature

(archive-copy-file FILES NEW-NAME)

Documentation

Copy FILES to a location specified by NEW-NAME.

FILES can be a single file or a list of files.

Interactively, FILES is the list of marked files, or the file at point if nothing is marked, and the function prompts for NEW-NAME.

Probably introduced at or before Emacs version 28.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/arc-mode.el.gz
(defun archive-copy-file (files new-name)
  "Copy FILES to a location specified by NEW-NAME.
FILES can be a single file or a list of files.

Interactively, FILES is the list of marked files, or the file at
point if nothing is marked, and the function prompts for
NEW-NAME."
  (interactive
   (let ((names
          (mapcar
           #'archive--file-desc-ext-file-name
           (or (archive-get-marked ?*) (list (archive-get-descr))))))
     (list names
           (read-file-name (format "Copy %s to: " (string-join names ", "))
                           nil default-directory))))
  (unless (consp files)
    (setq files (list files)))
  (when (and (> (length files) 1)
             (not (file-directory-p new-name)))
    (user-error "Can't copy a list of files to a single file"))
  (save-excursion
    (dolist (file files)
      (let* ((write-to (if (file-directory-p new-name)
                           (expand-file-name file new-name)
                         new-name))
             (write-to-dir (file-name-directory write-to)))
        (when (and (file-exists-p write-to)
                   (not (yes-or-no-p (format "%s already exists; overwrite? "
                                             write-to))))
          (user-error "Not overwriting %s" write-to))
        (unless (file-directory-p write-to-dir)
          (make-directory write-to-dir t))
        (archive-goto-file file)
        (let* ((descr (archive-get-descr))
               (archive (buffer-file-name))
               (extractor (archive-name "extract"))
               (ename (archive--file-desc-ext-file-name descr))
               ;; If the archive is remote, we have to copy it to a
               ;; local file first to make extraction work.
               (copy (archive-maybe-copy archive)))
          (unwind-protect
              (with-temp-buffer
                (set-buffer-multibyte nil)
                (archive--extract-file extractor copy ename)
                (let ((coding-system-for-write
                       (or coding-system-for-write 'no-conversion)))
                  (write-region (point-min) (point-max) write-to)))
            (unless (equal copy archive)
              (delete-file copy))))))))