如果我在emacs中以dired(或x-dired)模式按'Z',光标下的文件将被压缩或解压缩。
我想将此行为扩展到目录。也就是说,如果光标位于目录“stuff”,我希望'Z'运行
tar -zcf stuff.tgz stuff
(假设操作系统提供'tar')。
旁注: 相反(将stuff.tgz扩展到完整目录树中)已经可以通过'!'来完成,这表明了扩展的猜测。不对称(压缩'Z'和解压缩'!'不会打扰我。
答案 0 :(得分:1)
dired-compress-file
中的几行。我已使用BEGIN EDIT
和END 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"))))))))
答案 1 :(得分:0)
以下是库dired-tar
的链接 - 它会对目录和文件进行分析和压缩。我已经验证它适用于OSX,最新版本的Emacs Trunk建于2014年3月19日。
答案 2 :(得分:0)
此功能现在安装在master中:
tar -czf dirname.tar.gz dirname
tar -zxvf dirname
如果您需要使用dired-compress-file-suffixes
以外的其他内容,则可以通过-zxvf
自定义后者。