将 tar.gz 转换为 zip
我的 Ubuntu 网络服务器上有大量 gzip 压缩档案,我需要将它们转换为 zip。我认为这可以通过脚本来完成,但是我应该使用什么语言,以及如何解压缩和重新压缩文件?
I've got a large collection of gzipped archives on my Ubuntu webserver, and I need them converted to zips. I figure this would be done with a script, but what language should I use, and how would I go about unzipping and rezipping files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我会用
bash(1)
一句台词来完成它:这不是很漂亮,因为我不太擅长
bash(1)
。请注意,这会破坏很多目录,因此在执行此操作之前请确保您知道它的作用。请参阅
bash(1)
参考卡有关${foo%bar}
语法的更多详细信息。I'd do it with a
bash(1)
one-liner:It isn't very pretty because I'm not great at
bash(1)
. Note that this destroys a lot of directories so be sure you know what this does before doing it.See the
bash(1)
reference card for more details on the${foo%bar}
syntax.一个简单的 bash 脚本肯定是最简单的吧?这样您就可以调用
tar
和zip
命令。A simple bash script would be easiest, surely? That way you can just invoke the
tar
andzip
commands.Unix 平台上最简单的解决方案很可能是使用 fusion 和类似 archivemount (libarchive) 的东西, http://en .wikipedia.org/wiki/Archivemount。
/iaw
the easiest solution on unix platforms may well be to use fuse and something like archivemount (libarchive), http://en.wikipedia.org/wiki/Archivemount .
/iaw
您可以使用 node.js 和 tar-to-zip 用于此目的。您需要做的就是:
使用 nvm 安装node.js(如果您没有)。
然后使用以下命令安装
tar-to-zip
并使用它:
您还可以通过编程方式将
.tar.gz
文件转换为.zip
。您应该在本地安装
async
和tar-to-zip
:然后使用以下内容创建
converter.js
:You can use node.js and tar-to-zip for this purpose. All you need to do is:
Install node.js with nvm if you do not have it.
And then install
tar-to-zip
with:And use it with:
Also you can convert
.tar.gz
files to.zip
programmatically.You should install
async
andtar-to-zip
locally:And then create
converter.js
with contents:Zip 文件很方便,因为它们提供对文件的随机访问。 Tar 文件仅是连续的。
我对此转换的解决方案是这个 shell 脚本,它通过 tar(1)“--to-command”选项调用自身。 (我更喜欢这样而不是有两个脚本)。但我承认“untar and zip -r”比这更快,因为不幸的是 zipnote(1) 无法就地工作。
Zipfiles are handy because they offer random access to files. Tar files only sequential.
My solution for this conversion is this shell script, which calls itself via tar(1) "--to-command" option. (I prefer that rather than having 2 scripts). But I admit "untar and zip -r" is faster than this, because zipnote(1) cannot work in-place, unfortunately.
这是一个基于此处答案的Python解决方案:
Here is a python solution based on this answer here:
这是基于 @Brad Campbell 的答案的脚本,它适用于作为命令参数传递的文件,适用于其他 tar 文件类型(未压缩或 tarfile 支持的其他压缩类型),并处理源 tar 文件中的目录。如果源文件包含符号链接或硬链接,它还会打印警告,并将其转换为常规文件。对于符号链接,链接在转换期间解析。如果链接目标不在 tar 中,这可能会导致错误;从安全角度来看,这也存在潜在危险,因此用户要小心。
Here is script based on @Brad Campbell's answer that works on files passed as command arguments, works with other tar file types (uncompressed or the other compression types supported by tarfile), and handles directories in the source tar file. It will also print warnings if the source file contains a symlink or hardlink, which are converted to regular files. For symlinks, the link is resolved during conversion. This can lead to an error if the link target is not in the tar; this is also potentially dangerous from a security standpoint, so user beware.