1

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?

2
  • possible duplicate of How to [tar and] compress marked files in Emacs
    – sds
    Dec 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. Dec 19, 2014 at 13:55

2 Answers 2

2

Mark the files with m, then press ! and type zip -r yourfile.zip *.

0

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)
2
  • 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 m moves the file(s), 9 is best compression, v is for verbose, and r is 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.
    – lawlist
    Dec 19, 2014 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.