How can I zip files in Dired, where trying to add a folder to the zip, it will also add the files in that folder recursively?
-
possible duplicate of How to [tar and] compress marked files in Emacs– sdsDec 19, 2014 at 13:43
-
@sds I am aware of this Z function in dired, however, it does not work for subfolders and is thus a unique question. In fact, I looked at that post and realised it does not allow subfolders.– PascalVKootenDec 19, 2014 at 13:55
Add a comment
|
2 Answers
You can (un)compress individual files using Z which runs the command dired-do-compress.
You can also do something like this:
(defun sds-dired-zip-file ()
"ZIP the current file and all subdir's; or unZIP if already a ZIP."
(interactive)
(let* ((fn (dired-get-filename)) (nd (file-name-nondirectory fn)) cmd msg)
(cond ((and (string-match ".zip$" fn)
(y-or-n-p (format "unzip %s? " fn)))
(setq msg "unZIPing file %s..." cmd (concat "unzip " nd)))
((and (or (string-match ".tgz$" fn) (string-match ".tar.gz$" fn))
(y-or-n-p (format "tar xfz `%s'? " fn)))
(setq msg "unTAR/GZIPing file %s..." cmd (concat "tar xfz " nd)))
((and (or (string-match ".tbz2$" fn) (string-match ".tar.bz2$" fn))
(y-or-n-p (format "tar xfj `%s'? " fn)))
(setq msg "unTAR/BZIPing file %s..." cmd (concat "tar xfj " nd)))
((and (string-match ".tar$" fn)
(y-or-n-p (format "tar xf `%s'? " fn)))
(setq msg "unTARing file %s..." cmd (concat "tar xf " nd)))
((and (string-match ".rar$" fn)
(y-or-n-p (format "unrar x `%s'? " fn)))
(setq msg "unRARing file %s..." cmd (concat "unrar x " nd)))
((and (file-directory-p fn)
(y-or-n-p (format "zip -rmv9 `%s'? " fn)))
(setq msg "ZIPing directory %s..."
cmd (concat "zip -rmv9 " nd ".zip " nd)))
((y-or-n-p "(un?)compress? ") (dired-do-compress)))
(when cmd
(message msg fn)
(shell-command (concat "cd " (file-name-directory fn) " && " cmd))
(revert-buffer))))
(define-key dired-mode-map "I" 'sds-dired-zip-file)
-
Would this also allow to mark certain files (and perhaps some folders) and then zip it up with this command, or does it only work for a single file? Dec 19, 2014 at 13:54
-
Perhaps add a little notation in your doc-string that zip arguments such as
mmoves the file(s),9is best compression,vis for verbose, andris recursive -- or something like that, so the user is not surprised when the file(s) that is/are being zipped disappear(s) from its previous location.– lawlistDec 19, 2014 at 15:57