3

如果我在 emacs 中的 dired(或 x-dired)模式下按“Z”,则光标下的文件将被压缩或未压缩。

我想将此行为扩展到目录。也就是说,如果光标位于目录“stuff”上,我希望运行“Z”

tar -zcf stuff.tgz stuff

(假设'tar'由操作系统提供)。

旁注: 反过来(将 stuff.tgz 扩展到完整的目录树)已经可以通过 '!' 完成,这表明对扩展的猜测。不对称(“Z”压缩和“!”解压缩)不会打扰我。

4

3 回答 3

1

很酷的想法。这是一个适合我的开始:只需要在dired-compress-file. 我已经用BEGIN EDITEND EDIT注释突出显示了更改(抱歉,如果有更好的方法来突出显示差异)。我敢肯定这并不可靠,但也许它可以为您指明正确的方向。

编辑:我应该说以下在 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"))))))))
于 2013-03-21T21:35:37.710 回答
0

以下是该库的链接dired-tar——它对目录和文件进行 tar 和压缩。我已经使用 2014 年 3 月 19 日构建的最新版本的 Emacs Trunk 验证了它可以在 OSX 上运行。

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

于 2014-04-12T23:19:56.937 回答
0

此功能现在已安装在 master 中:

  • Z目录运行tar -czf dirname.tar.gz dirname
  • Z*.tar.gz 文件运行tar -zxvf dirname

dired-compress-file-suffixes如果您需要使用除-zxvf.

于 2015-10-13T13:59:41.357 回答