Do not need p7zip
or info-zip
external programs, Emacs deps
ship with a zip program: minizip
. (for more information, see Easiest way to install Emacs Windows support libraries)
The some situations we need to be known:
minizip
does not support recursive zip
zip
had been hard-coded in ox-odt.el
minizip
's options is not compatible with `zip'
So I generated a zip.bat
batch in elisp
then add the path of zip.bat
to %PATH%
(for org export to odt) and exec-path
(for zip files, dired-do-compress-to
)
zip.bat
:
@echo off
REM zip.bat for minizip on Windows
REM generated by More Reasonable Emacs https://github.com/junjiemars/.emacs.d
setlocal EnableDelayedExpansion
set _OPT=%*
set _ZIP=
set _ARGV=
REM parsing command line arguments
:getopt
if "%1"=="-mX0" set _OPT=%_OPT:-mX0=-0% & shift & goto :getopt
REM ignore options
if "%1"=="-r" set _OPT=%_OPT:-r=% & shift & goto :getopt
if "%1"=="--filesync" set _OPT=%_OPT:--filesync=% & shift & goto :getopt
if "%1"=="-rmTq" set _OPT=%_OPT:-rmTq=% & shift & goto :getopt
REM extract zip and argv
if not "%1"=="" (
if "%_ZIP%"=="" (
if "%_ARGV%"=="" (
set _ZIP=%1
)
) else (
set _ARGV=%_ARGV% %1
)
set _OPT=!_OPT:%1=!
shift
goto :getopt
)
REM minizip recursive call
call :loop %_ARGV%
goto :end
:zip
set _file=%1
set _file=%_file:./=%
if not "%_file%"=="%_ZIP%" (
if exist %_ZIP% (
minizip %_OPT% -a %_ZIP% %_file%
) else (
minizip %_OPT% %_ZIP% %_file%
)
)
goto :end
:loop
for %%i in (%*) do (
if exist "%%i/*" (
for %%f in (%%i/*) do (
call :loop %%i/%%f
)
for /d %%d in (%%i/*) do (
call :loop %%i/%%d
)
) else (
call :zip %%i
)
)
:end
The function make-zip-bat
that generate zip.bat
:
(defun make-zip-bat (zip &rest ignore)
"Make ZIP.bat in `exec-path' for minizip or 7za."
(declare (indent 1))
(when (stringp zip)
(save-str-to-file
(concat "@echo off\n"
(format "REM zip.bat for %s on Windows\n" zip)
"REM generated by More Reasonable Emacs https://github.com/junjiemars/.emacs.d\n\n"
(concat "REM local variable declaration\n\n"
"setlocal EnableDelayedExpansion\n"
"\n"
"set _OPT=%*\n"
"set _ZIP=\n"
"set _ARGV=\n"
"\n"
"REM parsing command line arguments\n\n"
":getopt\n"
(cond ((string= "minizip" zip)
"if \"%1\"==\"-mX0\" set _OPT=%_OPT:-mX0=-0% & shift & goto :getopt\n")
((string= "7za" zip)
(concat
"if \"%1\"==\"-mX0\" set _OPT=%_OPT:-mX0=-mx0% & shift & goto :getopt\n"
"if \"%1\"==\"-0\" set _OPT=%_OPT:-0=-mx0% & shift & goto :getopt\n"
"if \"%1\"==\"-9\" set _OPT=%_OPT:-9=-mx9% & shift & goto :getopt\n")))
"\n"
"REM ignore options\n"
(let ((options nil))
(dolist (x (cond ((string= "minizip" zip)
(append '("-r" "--filesync" "-rmTq") ignore))
((string= "7za" zip)
(append '("-r" "--filesync" "-rmTq"))))
options)
(setq options
(concat options
(format "if \"%%1\"==\"%s\" set _OPT=%%_OPT:%s=%% & shift & goto :getopt\n" x x)))))
"\n"
"REM extract zip and argv\n"
"if not \"%1\"==\"\" (\n"
" if \"%_ZIP%\"==\"\" (\n"
" if \"%_ARGV%\"==\"\" (\n"
" set _ZIP=%1\n"
" )\n"
" ) else (\n"
" set _ARGV=%_ARGV% %1\n"
" )\n"
" set _OPT=!_OPT:%1=!\n"
" shift\n"
" goto :getopt\n"
")\n\n")
(cond ((string= "7za" zip)
(concat "REM 7za call\n"
"7za a %_OPT% -tzip -- %_ZIP% %_ARGV%\n"
"if exist %_ZIP% (\n"
" 7za d %_OPT% -tzip -- %_ZIP% %_ZIP%\n"
")\n"))
((string= "minizip" zip)
(concat "REM minizip recursive call\n\n"
"call :loop %_ARGV%\n"
"goto :end\n"
"\n:zip\n"
"set _file=%1\n"
"set _file=%_file:./=%\n"
"if not \"%_file%\"==\"%_ZIP%\" (\n"
" if exist %_ZIP% (\n"
" minizip %_OPT% -a %_ZIP% %_file%\n"
" ) else (\n"
" minizip %_OPT% %_ZIP% %_file%\n"
" )\n"
")\n"
"goto :end\n"
"\n:loop\n"
"for %%i in (%*) do (\n"
" if exist \"%%i/*\" (\n"
" for %%f in (%%i/*) do (\n"
" call :loop %%i/%%f\n"
" )\n"
" for /d %%d in (%%i/*) do (\n"
" call :loop %%i/%%d\n"
" )\n"
" ) else (\n"
" call :zip %%i\n"
" )\n"
")\n"
"\n:end\n"))))
(v-home% ".exec/zip.bat"))))
The simple way to let zip
works is to copy the content of zip.bat
into a file named zip.bat
and put that file under exec-path
. To see more More Reasonable Emacs: zip program, make-zip-bat
support 7-Zip
too.
zip
from the command line (dos-prompt if you're in Windows as you seem to be) does it recognize it as a command or program? – Prolongeapt-get install zip unzip
will get you those utilities. – Dunbarton