Frage

If I press 'Z' in dired (or x-dired) mode in emacs, the file under the cursor is compressed or uncompressed.

I'd like to extend this behavior to directories. That is, if the cursor is at a directory "stuff", I'd like 'Z' to run

tar -zcf stuff.tgz stuff

(on the assumption that 'tar' is provided by the OS).

Side note: The converse (expanding stuff.tgz into the full directory tree) can already be done by '!', which suggests guesses for expansion. The asymmetry ('Z' to compress and '!' to uncompress) does not bother me.

War es hilfreich?

Lösung 2

The following is a link to the library dired-tar -- it tars and compresses directories and files. I have verified it works on OSX with a recent version of Emacs Trunk built on March 19, 2014.

http://www.emacswiki.org/emacs/DiredTar

Andere Tipps

Cool idea. Here's a start that works for me: just need to change a few lines in dired-compress-file. I've highlighted the changes with BEGIN EDIT and END EDIT comments (sorry if there's a better way to highlight a diff). I'm sure this isn't robust, but maybe it can point you in the right direction.

EDIT: I should say that the below works for me in GNU Emacs 24.3.1.

(defun dired-compress-file (file)
  ;; Compress or uncompress FILE.
  ;; Return the name of the compressed or uncompressed file.
  ;; Return nil if no change in files.
  (let ((handler (find-file-name-handler file 'dired-compress-file))
        suffix newname
        (suffixes dired-compress-file-suffixes))
    ;; See if any suffix rule matches this file name.
    (while suffixes
      (let (case-fold-search)
        (if (string-match (car (car suffixes)) file)
            (setq suffix (car suffixes) suffixes nil))
        (setq suffixes (cdr suffixes))))
    ;; If so, compute desired new name.
    (if suffix
        (setq newname (concat (substring file 0 (match-beginning 0))
                              (nth 1 suffix))))
    (cond (handler
           (funcall handler 'dired-compress-file file))
          ((file-symlink-p file)
           nil)
          ((and suffix (nth 2 suffix))
           ;; We found an uncompression rule.
           (if (not (dired-check-process (concat "Uncompressing " file)
                                         (nth 2 suffix) file))
               newname))
          (t
       ;;; We don't recognize the file as compressed, so compress it.
       ;;; Try gzip; if we don't have that, use compress.
           (condition-case nil
               ;; BEGIN EDIT - choose the correct name if looking at a directory
               (let ((out-name (if (file-directory-p file) (concat file ".tar.gz") (concat file ".gz"))))
                 ;; END EDIT
                     (and (or (not (file-exists-p out-name))

                              (y-or-n-p
                               (format "File %s already exists.  Really compress? "
                                       out-name)))
                          ;; BEGIN EDIT: create a tarball if we're looking at a directory
                          (not (if (file-directory-p file)
                                   (dired-check-process (concat "Compressing " file)
                                                        "tar" "-zcf" out-name file)
                                 (dired-check-process (concat "Compressing " file)
                                                      "gzip" "-f" file)))
                          ;; END EDIT
                          (or (file-exists-p out-name)
                              (setq out-name (concat file ".z")))
                          ;; Rename the compressed file to NEWNAME
                          ;; if it hasn't got that name already.
                          (if (and newname (not (equal newname out-name)))
                              (progn
                                (rename-file out-name newname t)
                                newname)
                            out-name)))
                 (file-error
                  (if (not (dired-check-process (concat "Compressing " file)
                                                "compress" "-f" file))
                      ;; Don't use NEWNAME with `compress'.
                      (concat file ".Z"))))))))

This functionality is now installed in master:

  • press Z on a directory to run tar -czf dirname.tar.gz dirname
  • press Z on a *.tar.gz file to run tar -zxvf dirname

The latter can be customized via dired-compress-file-suffixes, if you ever need to use something other than -zxvf.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top