all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* how to generate a zip file (not a directory) within dired
@ 2022-02-20  9:22 Uwe Brauer
  2022-02-20 12:59 ` Jean Louis
  0 siblings, 1 reply; 12+ messages in thread
From: Uwe Brauer @ 2022-02-20  9:22 UTC (permalink / raw)
  To: help-gnu-emacs


Hi

sometime I need to generate a zip file of a single file

I checked dired-compress-file-suffixes
and it unzip zip files, but it seems not to generate them.

In https://stackoverflow.com/questions/27567019/how-to-zip-files-recursively-in-emacs

There is a solution posted, but again it does not allow me to generate a
zip file on a single file only on a whole directory (as the title suggests), and that I don't
need.

Any ideas? Yes I could call  dired-do-shell-command, but I want a simpler
solution.

Regards

Uwe Brauer 




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: how to generate a zip file (not a directory) within dired
  2022-02-20  9:22 how to generate a zip file (not a directory) within dired Uwe Brauer
@ 2022-02-20 12:59 ` Jean Louis
  2022-02-20 13:16   ` Uwe Brauer
  2022-02-20 13:32   ` [a directory is also created] (was: how to generate a zip file (not a directory) within dired) Uwe Brauer
  0 siblings, 2 replies; 12+ messages in thread
From: Jean Louis @ 2022-02-20 12:59 UTC (permalink / raw)
  To: help-gnu-emacs

* Uwe Brauer <oub@mat.ucm.es> [2022-02-20 15:01]:
> 
> Hi
> 
> sometime I need to generate a zip file of a single file
> 
> I checked dired-compress-file-suffixes
> and it unzip zip files, but it seems not to generate them.
> 
> In https://stackoverflow.com/questions/27567019/how-to-zip-files-recursively-in-emacs
> 
> There is a solution posted, but again it does not allow me to generate a
> zip file on a single file only on a whole directory (as the title suggests), and that I don't
> need.
> 
> Any ideas? Yes I could call  dired-do-shell-command, but I want a simpler
> solution.

Solution is Z.

1) Adapt `dired-compress-file-alist' for ZIP files;
   example:

   (setf (alist-get "\\.zip\\'" dired-compress-file-alist) "zip -9 %o %i")

   which may be placed in your init file.

2) Adapt `dired-compress-file-default-suffix' to be .ZIP

   or alternatively make a function that will adapt it on the fly.

Then press Z on the file. I don't know if it will work on multiple files.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: how to generate a zip file (not a directory) within dired
  2022-02-20 12:59 ` Jean Louis
@ 2022-02-20 13:16   ` Uwe Brauer
  2022-02-20 13:32   ` [a directory is also created] (was: how to generate a zip file (not a directory) within dired) Uwe Brauer
  1 sibling, 0 replies; 12+ messages in thread
From: Uwe Brauer @ 2022-02-20 13:16 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 496 bytes --]


> * Uwe Brauer <oub@mat.ucm.es> [2022-02-20 15:01]:

> Solution is Z.

> 1) Adapt `dired-compress-file-alist' for ZIP files;
>    example:

>    (setf (alist-get "\\.zip\\'" dired-compress-file-alist) "zip -9 %o %i")

>    which may be placed in your init file.

> 2) Adapt `dired-compress-file-default-suffix' to be .ZIP

>    or alternatively make a function that will adapt it on the fly.

> Then press Z on the file. I don't know if it will work on multiple files.

Thanks! Works like charm

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5673 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [a directory is also created] (was: how to generate a zip file (not a directory) within dired)
  2022-02-20 12:59 ` Jean Louis
  2022-02-20 13:16   ` Uwe Brauer
@ 2022-02-20 13:32   ` Uwe Brauer
  2022-02-20 13:42     ` [works on the comman line] (was: [a directory is also created]) Uwe Brauer
  1 sibling, 1 reply; 12+ messages in thread
From: Uwe Brauer @ 2022-02-20 13:32 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 763 bytes --]


> * Uwe Brauer <oub@mat.ucm.es> [2022-02-20 15:01]:

> Solution is Z.

> 1) Adapt `dired-compress-file-alist' for ZIP files;
>    example:

>    (setf (alist-get "\\.zip\\'" dired-compress-file-alist) "zip -9 %o %i")

It is a more complicated that I thought, but this is because of the
behavior of zip. If I use this setting zip creates a zip archive that
contains the file, but the whole absolute path, and that is crazy.


(setf (alist-get "\\.zip\\'" dired-compress-file-alist) "zip -9 --junk-paths %o %i")

Gives a bit better result, but 

Starting with test.tex
it generates test.tex.zip

And that is then uncompressed to 

 test.tex/test.tex

So an additional directory is created. 

I try to search for a complete solution.


[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5673 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [works on the comman line] (was: [a directory is also created])
  2022-02-20 13:32   ` [a directory is also created] (was: how to generate a zip file (not a directory) within dired) Uwe Brauer
@ 2022-02-20 13:42     ` Uwe Brauer
  2022-02-20 14:08       ` Jean Louis
  0 siblings, 1 reply; 12+ messages in thread
From: Uwe Brauer @ 2022-02-20 13:42 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 724 bytes --]

>>> "UB" == Uwe Brauer <oub@mat.ucm.es> writes:

>> * Uwe Brauer <oub@mat.ucm.es> [2022-02-20 15:01]:

>> Solution is Z.

>> 1) Adapt `dired-compress-file-alist' for ZIP files;
>> example:

>> (setf (alist-get "\\.zip\\'" dired-compress-file-alist) "zip -9 %o %i")

> It is a more complicated that I thought, but this is because of the
> behavior of zip. If I use this setting zip creates a zip archive that
> contains the file, but the whole absolute path, and that is crazy.


> (setf (alist-get "\\.zip\\'" dired-compress-file-alist) "zip -9 --junk-paths %o %i")

Very odd: 

On the command line 
zip -9 -j test.zip test.tex 

Works but emacs or dired adds a directory, who is here the culprit?


[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5673 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [works on the comman line] (was: [a directory is also created])
  2022-02-20 13:42     ` [works on the comman line] (was: [a directory is also created]) Uwe Brauer
@ 2022-02-20 14:08       ` Jean Louis
  2022-02-20 15:41         ` [works on the comman line] Uwe Brauer
  2022-02-21  4:18         ` [works on the comman line] (was: [a directory is also created]) Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 2 replies; 12+ messages in thread
From: Jean Louis @ 2022-02-20 14:08 UTC (permalink / raw)
  To: help-gnu-emacs

* Uwe Brauer <oub@mat.ucm.es> [2022-02-20 16:44]:
> >>> "UB" == Uwe Brauer <oub@mat.ucm.es> writes:
> 
> >> * Uwe Brauer <oub@mat.ucm.es> [2022-02-20 15:01]:
> 
> >> Solution is Z.
> 
> >> 1) Adapt `dired-compress-file-alist' for ZIP files;
> >> example:
> 
> >> (setf (alist-get "\\.zip\\'" dired-compress-file-alist) "zip -9 %o %i")
> 
> > It is a more complicated that I thought, but this is because of the
> > behavior of zip. If I use this setting zip creates a zip archive that
> > contains the file, but the whole absolute path, and that is crazy.
> 
> 
> > (setf (alist-get "\\.zip\\'" dired-compress-file-alist) "zip -9 --junk-paths %o %i")
> 
> Very odd: 
> 
> On the command line 
> zip -9 -j test.zip test.tex 
> 
> Works but emacs or dired adds a directory, who is here the culprit?

dired-compress-file-alist is a variable defined in ‘dired-aux.el’.

Where it says:

Within CMD, %i denotes the input file(s), and %o denotes the
output file.  %i path(s) are relative, while %o is absolute.

Maybe that is limitation.

You can do it in single function here:

(defun rcd-zip-file ()
  "ZIP single file within Dired."
  (interactive)
  (let* ((file (car (dired-get-marked-files t)))
	 (zip-file (concat file ".zip")))
    (when file
      (start-process "ZIP" "ZIP" "zip" "-9" zip-file file)
      (revert-buffer))))

Then just bind to a key.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [works on the comman line]
  2022-02-20 14:08       ` Jean Louis
@ 2022-02-20 15:41         ` Uwe Brauer
  2022-02-20 15:46           ` Uwe Brauer
  2022-02-21  4:28           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2022-02-21  4:18         ` [works on the comman line] (was: [a directory is also created]) Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 2 replies; 12+ messages in thread
From: Uwe Brauer @ 2022-02-20 15:41 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 1393 bytes --]


> * Uwe Brauer <oub@mat.ucm.es> [2022-02-20 16:44]:

> dired-compress-file-alist is a variable defined in ‘dired-aux.el’.

> Where it says:

> Within CMD, %i denotes the input file(s), and %o denotes the
> output file.  %i path(s) are relative, while %o is absolute.

Ok, I was looking for the %i and %o explanation.

> Maybe that is limitation.

> You can do it in single function here:

> (defun rcd-zip-file ()
>   "ZIP single file within Dired."
>   (interactive)
>   (let* ((file (car (dired-get-marked-files t)))
> 	 (zip-file (concat file ".zip")))
>     (when file
>       (start-process "ZIP" "ZIP" "zip" "-9" zip-file file)
>       (revert-buffer))))

Thanks but his does not work in the sense that it also add one
additional directory.


I tried to use 

(defun rcd-zip-file ()
  "ZIP single file within Dired."
  (interactive)
  (let* ((file (car (dired-get-marked-files t)))
	 (zip-file (concat file ".zip")))
    (when file
      (start-process "ZIP" "ZIP" "zip" "-9j" zip-file file)
      (revert-buffer))))
 

Or


(defun rcd-zip-file ()
  "ZIP single file within Dired."
  (interactive)
  (let* ((file (car (dired-get-marked-files t)))
	 (zip-file (concat file ".zip")))
    (when file
      (start-process "ZIP" "ZIP" "zip" "-9" "-j" zip-file file)
      (revert-buffer))))

But not worked. This seems to be cursed!

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5673 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [works on the comman line]
  2022-02-20 15:41         ` [works on the comman line] Uwe Brauer
@ 2022-02-20 15:46           ` Uwe Brauer
  2022-02-21  4:28           ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 12+ messages in thread
From: Uwe Brauer @ 2022-02-20 15:46 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 817 bytes --]


> I tried to use 

> (defun rcd-zip-file ()
>   "ZIP single file within Dired."
>   (interactive)
>   (let* ((file (car (dired-get-marked-files t)))
> 	 (zip-file (concat file ".zip")))
>     (when file
>       (start-process "ZIP" "ZIP" "zip" "-9j" zip-file file)
>       (revert-buffer))))
 

> Or


> (defun rcd-zip-file ()
>   "ZIP single file within Dired."
>   (interactive)
>   (let* ((file (car (dired-get-marked-files t)))
> 	 (zip-file (concat file ".zip")))
>     (when file
>       (start-process "ZIP" "ZIP" "zip" "-9" "-j" zip-file file)
>       (revert-buffer))))

> But not worked. This seems to be cursed!

I have to correct me, sorry.

It works, what happens is the 
dired-do-compress 
does create a new directory.

When I run unzip from the command line it is ok

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5673 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [works on the comman line] (was: [a directory is also created])
  2022-02-20 14:08       ` Jean Louis
  2022-02-20 15:41         ` [works on the comman line] Uwe Brauer
@ 2022-02-21  4:18         ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2022-02-21  4:18 UTC (permalink / raw)
  To: help-gnu-emacs

Uwe Brauer and then Jean Louis wrote:

> On the command line 
> zip -9 -j test.zip test.tex 
> 
> Works but emacs or dired adds a directory, who is here
> the culprit?

Actually, both are correct :)

It is because of the relative path where you are when you run
the command in the shell, OTOH in the Elisp code you give zip
the path.

See the man file for zip(1):

  By default, zip will store the full path (relative to the
  current directory).

The option you are looking for is '--junk-path'.

> You can do it in single function here:
>
> (defun rcd-zip-file ()
>   "ZIP single file within Dired."
>   (interactive)
>   (let* ((file (car (dired-get-marked-files t)))
> 	 (zip-file (concat file ".zip")))
>     (when file
>       (start-process "ZIP" "ZIP" "zip" "-9" zip-file file)
>       (revert-buffer))))

Yeah, but why have a specific function for a single file, if
it is based on files first being marked anyway? If that's the
method it is better to have a generic function and if one
wants to zip a zingle file, just mark that and nothing else,
if you want more, then instead you do that?

Try this:

(defun dired-zip-files ()
  (interactive)
    (dolist (f (dired-get-marked-files))
      (start-process "zip" nil "zip" "-9" "--junk-path" (concat f ".zip") f)
      (revert-buffer) ))

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [works on the comman line]
  2022-02-20 15:41         ` [works on the comman line] Uwe Brauer
  2022-02-20 15:46           ` Uwe Brauer
@ 2022-02-21  4:28           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2022-02-21 19:38             ` Uwe Brauer
  1 sibling, 1 reply; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2022-02-21  4:28 UTC (permalink / raw)
  To: help-gnu-emacs

Uwe Brauer wrote:

> (defun rcd-zip-file ()
>   "ZIP single file within Dired."
>   (interactive)
>   (let* ((file (car (dired-get-marked-files t)))
> 	 (zip-file (concat file ".zip")))
>     (when file
>       (start-process "ZIP" "ZIP" "zip" "-9" "-j" zip-file file)
>       (revert-buffer))))

But here -j (the short --junk-paths) doesn't don't add
anything (?) since the `t' in (dired-get-marked-files t) has
already removed the path ... so then why doesn't _that_ work?
Hm ...

But this works for me:

(defun dired-zip-files ()
  (interactive)
    (dolist (f (dired-get-marked-files))
      (start-process "zip" nil "zip" "-9" "--junk-path" (concat f ".zip") f)
      (revert-buffer) ))

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [works on the comman line]
  2022-02-21  4:28           ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2022-02-21 19:38             ` Uwe Brauer
  2022-02-26  0:47               ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 12+ messages in thread
From: Uwe Brauer @ 2022-02-21 19:38 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 772 bytes --]


> Uwe Brauer wrote:

> But here -j (the short --junk-paths) doesn't don't add
> anything (?) since the `t' in (dired-get-marked-files t) has
> already removed the path ... so then why doesn't _that_ work?
> Hm ...

> But this works for me:

> (defun dired-zip-files ()
>   (interactive)
>     (dolist (f (dired-get-marked-files))
>       (start-process "zip" nil "zip" "-9" "--junk-path" (concat f ".zip") f)
>       (revert-buffer) ))

Thanks, the point however is, as I said. 

    1. If I open the zip file generated by your method via unzip on the
       command like, it just returns that file without any additional
       directory.

    2. If I use however dired-do-compress then an additional directory
       is created.

Regards

 

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5673 bytes --]

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [works on the comman line]
  2022-02-21 19:38             ` Uwe Brauer
@ 2022-02-26  0:47               ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2022-02-26  0:47 UTC (permalink / raw)
  To: help-gnu-emacs

Uwe Brauer wrote:

>> (defun dired-zip-files ()
>>   (interactive)
>>     (dolist (f (dired-get-marked-files))
>>       (start-process "zip" nil "zip" "-9" "--junk-path" (concat f ".zip") f)
>>       (revert-buffer) ))
>
> 1. If I open the zip file generated by your method via unzip
>    on the command like, it just returns that file without
>    any additional directory.

What additional directory are you talking about?

> 2. If I use however dired-do-compress then an additional
>    directory is created.

But `dired-do-compress' creates .gz files (or a .tar.gz for
a directory) so that's another command from the get-go and to
unzip it one cannot even use unzip(1).

Still ... I use 'gunzip -fk' for .gz (and 'tar xfz' .tar.gz)
and I just tried it, it works as expected and there is no
"additional directory" ...

https://dataswamp.org/~incal/conf/.zsh/zip

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2022-02-26  0:47 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-02-20  9:22 how to generate a zip file (not a directory) within dired Uwe Brauer
2022-02-20 12:59 ` Jean Louis
2022-02-20 13:16   ` Uwe Brauer
2022-02-20 13:32   ` [a directory is also created] (was: how to generate a zip file (not a directory) within dired) Uwe Brauer
2022-02-20 13:42     ` [works on the comman line] (was: [a directory is also created]) Uwe Brauer
2022-02-20 14:08       ` Jean Louis
2022-02-20 15:41         ` [works on the comman line] Uwe Brauer
2022-02-20 15:46           ` Uwe Brauer
2022-02-21  4:28           ` Emanuel Berg via Users list for the GNU Emacs text editor
2022-02-21 19:38             ` Uwe Brauer
2022-02-26  0:47               ` Emanuel Berg via Users list for the GNU Emacs text editor
2022-02-21  4:18         ` [works on the comman line] (was: [a directory is also created]) Emanuel Berg via Users list for the GNU Emacs text editor

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.