3

emacsでdired(またはx-dired)モードで「Z」を押すと、カーソルの下のファイルが圧縮または非圧縮になります。

この動作をディレクトリに拡張したいと思います。つまり、カーソルが「stuff」ディレクトリにある場合は、「Z」を実行します。

tar -zcf stuff.tgz stuff

(「tar」がOSによって提供されることを前提としています)。

補足: 逆(stuff.tgzを完全なディレクトリツリーに展開する)は、すでに'!'で実行できます。これは、展開の推測を示唆しています。非対称性(圧縮する場合は「Z」、解凍する場合は「!」)は気になりません。

4

3 に答える 3

1

かっこいいアイデア。これが私にとってうまくいくスタートです:で数行を変更する必要がありdired-compress-fileます。変更点BEGIN EDITEND EDITコメントを強調表示しました(差分を強調表示するためのより良い方法がある場合は申し訳ありません)。これは堅牢ではないと確信していますが、おそらく正しい方向に向けることができます。

編集:GNUEmacs24.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日にビルドされた最新バージョンのEmacsTrunkを使用してOSXで動作することを確認しました。

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

于 2014-04-12T23:19:56.937 に答える
0

この機能はマスターにインストールされました:

  • Zディレクトリを押して実行しますtar -czf dirname.tar.gz dirname
  • Z*.tar.gzファイルを押して実行しますtar -zxvf dirname

後者はdired-compress-file-suffixes、以外のものを使用する必要がある場合は、を介してカスタマイズできます-zxvf

于 2015-10-13T13:59:41.357 に答える