如何直接通过 Emacs 命令行从 zip 文件中打开文件?
How to open a file from within a zip directly through the Emacs command-line?
我知道可以通过 Dired 在 Emacs 上打开 Zip 文件,然后访问单个文件。但是,我想从命令行做同样的事情。 Emacs 支持这个吗?
我正在寻找的命令类型示例:
emacs --open-zip foo.zip --open-file README.md
编辑: 虽然当前接受的解决方案是规范的正确解决方案,但它不适用于我的用例,因为我使用的是 OSX不兼容,无论如何都无法轻松安装 Emacs 27。我现在正在寻找一个不太规范但更实用的解决方案(可能通过在 Emacs 的启动时调用 lisp 脚本)- 可以接受围绕 Emacs 的包装器。
在 Emacs 27 中,有 tramp-archive.el
。它通过特殊的语法结构添加对存档文件的访问,将 zip 文件视为目录。你的例子是
emacs path/to/foo.zip/README.md
有关详细信息,请参阅 https://www.gnu.org/software/tramp/#Archive-file-names。除了安装 Emacs 27,您还可以安装最新的 Tramp 2.4.1,据说它可以与旧版 Emacsen 配合使用。
注意:这仅适用于 GNU/Linux 系统。
我想我可能有一种快速而肮脏的方法来打开 OSX 附带的旧 (V22) emacs 存档中的特定文件。
它未经测试或优化,只是一个概念证明。文件 jea_open_specific_file_in_zip.el
的内容是:
(defun jea-open-specific-file-in-zip (outer-archive-file-name inner-file-name)
(progn
(find-file outer-archive-file-name)
(while (not (eobp))
(let ((bounds (bounds-of-thing-at-point 'filename)))
(if bounds
(let ((zipped-file-name (buffer-substring-no-properties (car bounds) (cdr bounds))))
(if (string= zipped-file-name inner-file-name)
(archive-extract)))))
(archive-next-line 1))))
这是一个粗略的 shell 捕获:
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$ rm ./data/test1.zip
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$ /usr/bin/emacs --version # old emacs that comes with OSX
GNU Emacs 22.1.1
Copyright (C) 2007 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$ ll ./data/*.md # some test data
-rw-r--r-- 1 jamesanderson staff 17B 30 Jul 19:31 ./data/README1.md
-rw-r--r-- 1 jamesanderson staff 17B 30 Jul 19:32 ./data/README2.md
-rw-r--r-- 1 jamesanderson staff 19B 30 Jul 19:32 ./data/README3.md
-rw-r--r-- 1 jamesanderson staff 18B 30 Jul 19:32 ./data/README4.md
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$ cat ./data/*.md
Hello World One!
Hello World Two!
Hello World Three!
Hello World Four!
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$ zip ./data/test1.zip -9 ./data/README1.md ./data/README2.md ./data/README3.md ./data/README4.md
adding: data/README1.md (stored 0%)
adding: data/README2.md (stored 0%)
adding: data/README3.md (stored 0%)
adding: data/README4.md (stored 0%)
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$ ll ./data/
total 40
drwxr-xr-x 7 jamesanderson staff 224B 30 Jul 22:09 .
drwxr-xr-x 5 jamesanderson staff 160B 30 Jul 21:52 ..
-rw-r--r-- 1 jamesanderson staff 17B 30 Jul 19:31 README1.md
-rw-r--r-- 1 jamesanderson staff 17B 30 Jul 19:32 README2.md
-rw-r--r-- 1 jamesanderson staff 19B 30 Jul 19:32 README3.md
-rw-r--r-- 1 jamesanderson staff 18B 30 Jul 19:32 README4.md
-rw-r--r-- 1 jamesanderson staff 725B 30 Jul 22:09 test1.zip
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$ /usr/bin/emacs -Q --load ./jea_open_specific_file_in_zip.el --eval "(jea-open-specific-file-in-zip \"./data/test1.zip\" \"data/README3.md\")"
打开第三个文件。它只是从文件 jea_open_specific_file_in_zip.el
加载上面粘贴的 elisp 代码,然后将外部 zip 文件名和特定的内部压缩文件名传递给函数:(jea-open-specific-file-in-zip "./data/test1.zip" "data/README3.md")
以及所需的转义符。 -Q
arg 只是为了让它不加载初始化脚本(这样我就不会不小心使用我机器本地的库)
我知道可以通过 Dired 在 Emacs 上打开 Zip 文件,然后访问单个文件。但是,我想从命令行做同样的事情。 Emacs 支持这个吗?
我正在寻找的命令类型示例:
emacs --open-zip foo.zip --open-file README.md
编辑: 虽然当前接受的解决方案是规范的正确解决方案,但它不适用于我的用例,因为我使用的是 OSX不兼容,无论如何都无法轻松安装 Emacs 27。我现在正在寻找一个不太规范但更实用的解决方案(可能通过在 Emacs 的启动时调用 lisp 脚本)- 可以接受围绕 Emacs 的包装器。
在 Emacs 27 中,有 tramp-archive.el
。它通过特殊的语法结构添加对存档文件的访问,将 zip 文件视为目录。你的例子是
emacs path/to/foo.zip/README.md
有关详细信息,请参阅 https://www.gnu.org/software/tramp/#Archive-file-names。除了安装 Emacs 27,您还可以安装最新的 Tramp 2.4.1,据说它可以与旧版 Emacsen 配合使用。
注意:这仅适用于 GNU/Linux 系统。
我想我可能有一种快速而肮脏的方法来打开 OSX 附带的旧 (V22) emacs 存档中的特定文件。
它未经测试或优化,只是一个概念证明。文件 jea_open_specific_file_in_zip.el
的内容是:
(defun jea-open-specific-file-in-zip (outer-archive-file-name inner-file-name)
(progn
(find-file outer-archive-file-name)
(while (not (eobp))
(let ((bounds (bounds-of-thing-at-point 'filename)))
(if bounds
(let ((zipped-file-name (buffer-substring-no-properties (car bounds) (cdr bounds))))
(if (string= zipped-file-name inner-file-name)
(archive-extract)))))
(archive-next-line 1))))
这是一个粗略的 shell 捕获:
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$ rm ./data/test1.zip
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$ /usr/bin/emacs --version # old emacs that comes with OSX
GNU Emacs 22.1.1
Copyright (C) 2007 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$ ll ./data/*.md # some test data
-rw-r--r-- 1 jamesanderson staff 17B 30 Jul 19:31 ./data/README1.md
-rw-r--r-- 1 jamesanderson staff 17B 30 Jul 19:32 ./data/README2.md
-rw-r--r-- 1 jamesanderson staff 19B 30 Jul 19:32 ./data/README3.md
-rw-r--r-- 1 jamesanderson staff 18B 30 Jul 19:32 ./data/README4.md
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$ cat ./data/*.md
Hello World One!
Hello World Two!
Hello World Three!
Hello World Four!
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$ zip ./data/test1.zip -9 ./data/README1.md ./data/README2.md ./data/README3.md ./data/README4.md
adding: data/README1.md (stored 0%)
adding: data/README2.md (stored 0%)
adding: data/README3.md (stored 0%)
adding: data/README4.md (stored 0%)
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$ ll ./data/
total 40
drwxr-xr-x 7 jamesanderson staff 224B 30 Jul 22:09 .
drwxr-xr-x 5 jamesanderson staff 160B 30 Jul 21:52 ..
-rw-r--r-- 1 jamesanderson staff 17B 30 Jul 19:31 README1.md
-rw-r--r-- 1 jamesanderson staff 17B 30 Jul 19:32 README2.md
-rw-r--r-- 1 jamesanderson staff 19B 30 Jul 19:32 README3.md
-rw-r--r-- 1 jamesanderson staff 18B 30 Jul 19:32 README4.md
-rw-r--r-- 1 jamesanderson staff 725B 30 Jul 22:09 test1.zip
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$
Jamess-MacBook-Pro-3:open_zip_file jamesanderson$ /usr/bin/emacs -Q --load ./jea_open_specific_file_in_zip.el --eval "(jea-open-specific-file-in-zip \"./data/test1.zip\" \"data/README3.md\")"
打开第三个文件。它只是从文件 jea_open_specific_file_in_zip.el
加载上面粘贴的 elisp 代码,然后将外部 zip 文件名和特定的内部压缩文件名传递给函数:(jea-open-specific-file-in-zip "./data/test1.zip" "data/README3.md")
以及所需的转义符。 -Q
arg 只是为了让它不加载初始化脚本(这样我就不会不小心使用我机器本地的库)