如何在Emacs中对标记的文件进行[tar和]压缩

11
在Debian Squeeze变体上的Emacs 23.2.1中的中,我使用<*>选择了四个文件,然后按下将它们压缩。我回答< y >以确认,并在小型缓冲区中看到一些状态更新。我应该在哪里找到压缩后的文件?我对<一个>文件()进行了测试,Emacs在这个文件上运行了gzip,并将其制作成了一个.gz文件。如何在Emacs中[打包和]压缩标记的文件?
(为了预防任何关于tar、gzip、其他格式和档案的哲学或方法论讨论,我想要的只是将这四个文件作为压缩数据存储在一个文件中。如果可以通过tar和gzip或将每个文件直接压缩到一个归档文件中来实现这一点并不重要。)
4个回答

22
如果dired+dired相似,您可以使用m标记文件,然后按!(在标记的文件上运行shell命令)并将命令指定为tar -czf foo.tar.gz **是一个特殊标记,将被标记的文件名替换)。

谢谢,这在dired+中有效。我之前看过类似的示例,但没有意识到*会被替换为/标记/文件的名称。我本以为像这样常用的命令会被“内置”到dired[+]中。 - SabreWolfy
dired+只是对dired的一组扩展。 - Ivan Andrus

6
您可以通过标记并将文件复制到归档文件来归档文件。
例如,在dired中标记几个文件,然后选择。
在提示输入目的地时,键入。 文件将自动添加到zip存档中。
您还可以通过在dired中选择它们并运行命令来解压缩文件。
要设置此项,请查看以下变量: dired-to-archive-copy-alist dired-extract-alist
这是我的设置,已经为我服务多年...
;; dired-a provides support functions, including archiving, for dired
(load "dired-a")

;; Alist with information how to add files to an archive (from dired-a)
;; Each element has the form (REGEXP ADD-CMD NEW-CMD). If REGEXP matches
;; the file name of a target, that target is an archive and ADD-CMD is a command
;; that adds to an existing archive and NEW-CMD is a command that makes a new
;; archive (overwriting an old one if it exists). ADD-CMD and NEW-CMD are:
;; 1. Nil (meaning we cannot do this for this type of archive) (one of
;;    ADD-CMD and NEW-CMD must be non-nil).
;; 2. A symbol that must be a function e.g. dired-do-archive-op.
;; 3. A format string with two arguments, the source files concatenated into
;;    a space separated string and the target archive.
;; 4. A list of strings, the command and its flags, to which the target and
;;    the source-files are concatenated."
(setq dired-to-archive-copy-alist
      '(("\\.sh\\(ar\\|[0-9]\\)*$" nil "shar %s > %s")
    ("\\.jar$" ("jar" "uvf") ("jar" "cvf"))
    ("\\.tar$" ("tar" "-uf") ("tar" "-cf"))
    ("\\.tgz$\\|\\.tar\\.g?[zZ]$" ("tar" "-uf %s" "|" "gzip > %s") ("tar" "-czvf"))
    ("\\.ear$" ("zip" "-qr") ("zip" "-qr"))
;   ("\\.rar$" ("rar" "a")   ("rar" "a"))
    ("\\.war$" ("zip" "-qr") ("zip" "-qr"))
    ("\\.zip$" ("zip" "-qr") ("zip" "-qr"))
    ("\\.wmz$" ("zip" "-qr") ("zip" "-qr")) ;; for media player skins
    ("\\.arc$" ("arc" "a") nil)
    ("\\.zoo$" ("zoo" "aP") nil)
    ))

;; use pkzip with manipulating zip files (t) from within dired (use zip
;; and unzip otherwise)
(setq archive-zip-use-pkzip nil)

;; add these file types to archive mode to allow viewing and changing
;; their contents
(add-to-list 'auto-mode-alist '("\\.[ejrw]ar$\\'" . archive-mode))

;; modify the dired-extract switches to use the directory
;; ~/download/tryout as the default extract directory for zip files
(defconst MY_TRYOUT_DIR "~/downloads/tryout"
  "Directory for extracting files")

(setq dired-extract-alist
      `(
    ("\\.u\\(ue\\|aa\\)$" . dired-uud)
    ("\\.jar$" . "jar -xvf %s")
    ("\\.tar$" . ,(concat "tar -xf %s -C " MY_TRYOUT_DIR))
    ("\\.tgz$\\|\\.tar\\.g?[zZ]$" . ,(concat "tar -xzf %s -C " MY_TRYOUT_DIR))
    ("\\.arc$" . "arc x %s ")
    ("\\.bz2$" . ,(concat "bunzip2 -q %s"))
    ("\\.rar$" . ,(concat "unrar x %s " MY_TRYOUT_DIR "\\"))
    ("\\.zip$" . ,(concat "unzip -qq -Ux %s -d " MY_TRYOUT_DIR))
    ("\\.ear$" . ,(concat "unzip -qq -Ux %s -d " MY_TRYOUT_DIR))
    ("\\.war$" . ,(concat "unzip -qq -Ux %s -d " MY_TRYOUT_DIR))
    ("\\.zoo$" . "zoo x. %s ")
    ("\\.lzh$" . "lha x %s ")
    ("\\.7z$"  . "7z e %s ")
    ("\\.g?[zZ]$" . "gzip -d %s")   ; There is only one file
    ))

加载dired-a失败,似乎找不到任何包。 - Joseph Garvin
1
同时,执行dired复制似乎并不像你所描述的那样有效,我认为你必须有一个扩展程序来启用此功能,否则我只会收到一个错误,指出目标必须是一个目录。 - Joseph Garvin
我在这里找到了 dired-a:https://github.com/NikolausDemmel/emacs-pkgs/blob/master/misc/dired-a.el,还有这里:http://groups.google.com/groups?selm=yzig226gpoz.fsf%40inge.nada.kth.se。 - legoscia

1

平台:Ubuntu

需求

系统:
sudo apt-get install atool

Emacs:
M-x package-list-packages
C-s dired-atool RET
i
x

解决方法

1) 在dired缓冲区中标记文件。
2) M-x dired-atool-do-pack 注意:确保shell-file-name设置为"/bin/bash"。


0
回答后一个问题,另一个可用于从dired生成压缩tar文件的工具是pack。标记所需的文件并执行pack-dired-do-pack,指定whatever.tar.gz作为输出文件名。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接