解压缩 ZIP 文件并提取未知文件夹名称的内容
我的用户将压缩如下所示的文件:
TEMPLATE1.ZIP
|--------- UnknownName
|------- index.html
|------- images
|------- image1.jpg
我想按如下方式提取此 zip 文件:
/mysite/user_uploaded_templates/myrandomname/index.html
/mysite/user_uploaded_templates/myrandomname/images/image1.jpg
我的麻烦在于 UnknownName - 我事先不知道它是什么,将所有内容提取到“基本”级别会破坏所有相关index.html 中的路径
如何从此 ZIP 文件中提取 UnknownName 的内容?
有没有比以下更好的事情:
1. Extract everything
2. Detect which "new subdidrectory" got created
3. mv newsubdir/* .
4. rmdir newsubdir/
如果 UnknownName 级别有多个子目录,我可以拒绝该用户的 zip 文件。
My users will be zipping up files which will look like this:
TEMPLATE1.ZIP
|--------- UnknownName
|------- index.html
|------- images
|------- image1.jpg
I want to extract this zip file as follows:
/mysite/user_uploaded_templates/myrandomname/index.html
/mysite/user_uploaded_templates/myrandomname/images/image1.jpg
My trouble is with UnknownName - I do not know what it is beforehand and extracting everything to the "base" level breaks all the relative paths in index.html
How can I extract from this ZIP file the contents of UnknownName?
Is there anything better than:
1. Extract everything
2. Detect which "new subdidrectory" got created
3. mv newsubdir/* .
4. rmdir newsubdir/
If there is more than one subdirectory at UnknownName level, I can reject that user's zip file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你的方法是一个很好的方法。步骤 2 可以改进我提取到新创建的目录(稍后删除),以便“检测”变得微不足道。
I think your approach is a good one. Step 2 could be improved my extracting to a newly created directory (later deleted) so that "detection" is trivial.
除了您建议的选项之外,我看到的另一个选项是使用
unzip -j
标志,它将转储所有路径并将所有文件放入当前目录。如果您确定每个TEMPLATE1.ZIP
文件都包含index.html
和*.jpg
文件,那么您可以做一些事情就像:这并不是最干净的解决方案,但至少您不必像在示例中那样进行任何解析。我似乎找不到任何类似于
patch -p#
的选项,可以让您指定路径级别。The other option I can see other than the one you suggested is to use the
unzip -j
flag which will dump all paths and put all files into the current directory. If you know for certain that each of yourTEMPLATE1.ZIP
files includes anindex.html
and*.jpg
files then you can just do something like:It's not exactly the cleanest solution but at least you don't have to do any parsing like you do in your example. I can't seem to find any option similar to
patch -p#
that lets you specify the path level.每个
zip
和unzip
命令都不同,但通常有一种方法可以列出文件内容。从那里,您可以解析输出以确定未知的目录名称。在 Windows 上,1996 Wales/Gaily/van der Linden/Rommel 版本是
unzip -l
。当然,您可以简单地允许解压缩将文件解压缩到所需的任何目录,然后使用 mv 将目录重命名为您想要的名称。
为这些类型的操作创建一个临时目录始终是一个好主意,以防您最终运行此命令的两个实例。
Each
zip
andunzip
command differs, but there's usually a way to list the file contents. From there, you can parse the output to determine the unknown directory name.On Windows, the 1996 Wales/Gaily/van der Linden/Rommel version it is
unzip -l
.Of course, you could just simply allow the unzip to unzip the files to whatever directory it wants, then use
mv
to rename the directory to what you want it as.It's always a good idea to create a temporary directory for these types of operations in case you end up running two instances of this command.