How do I uncompress/unzip within Emacs

Damien Cassou picture Damien Cassou · Sep 16, 2009 · Viewed 13.4k times · Source

I would like to run unzip (or even zip) within dired or a dired-like buffer. Is there anything like this? I would like something similar as in the Nautilus file manager: i.e., selecting files and then pressing a keystroke to get these files into a new archive file.

Thank you

Answer

Trey Jackson picture Trey Jackson · Sep 16, 2009

You've got options...

To uncompress a .zip file, you just need to add to the variable 'dired-compress-file-suffixes

(eval-after-load "dired-aux"
   '(add-to-list 'dired-compress-file-suffixes 
                 '("\\.zip\\'" ".zip" "unzip")))

Now the Z key in dired will recognize the .zip extension and uncompress a .zip archive. Already supported are gunzip, bunzip2, uncompress and dictunzip.

If you want to mark files and add them to a .zip archive you can use the following to make z bound to zip the set of marked files:

(eval-after-load "dired"
  '(define-key dired-mode-map "z" 'dired-zip-files))
(defun dired-zip-files (zip-file)
  "Create an archive containing the marked files."
  (interactive "sEnter name of zip file: ")

  ;; create the zip file
  (let ((zip-file (if (string-match ".zip$" zip-file) zip-file (concat zip-file ".zip"))))
    (shell-command 
     (concat "zip " 
             zip-file
             " "
             (concat-string-list 
              (mapcar
               '(lambda (filename)
                  (file-name-nondirectory filename))
               (dired-get-marked-files))))))

  (revert-buffer)

  ;; remove the mark on all the files  "*" to " "
  ;; (dired-change-marks 42 ?\040)
  ;; mark zip file
  ;; (dired-mark-files-regexp (filename-to-regexp zip-file))
  )

(defun concat-string-list (list) 
   "Return a string which is a concatenation of all elements of the list separated by spaces" 
    (mapconcat '(lambda (obj) (format "%s" obj)) list " "))