解压缩 ZIP 文件并提取未知文件夹名称的内容

发布于 2024-10-05 02:45:38 字数 726 浏览 6 评论 0原文

我的用户将压缩如下所示的文件:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不离久伴 2024-10-12 02:45:38

我认为你的方法是一个很好的方法。步骤 2 可以改进我提取到新创建的目录(稍后删除),以便“检测”变得微不足道。

# Bash (minimally tested)
tempdest=$(mktemp -d)
unzip -d "$tempdest" TEMPLATE1.ZIP
dir=("$tempdest"/*)
if (( ${#dir[@]} == 1 )) && [[ -d $dir ]]
    # in Bash, etc., scalar $var is the same as ${var[0]}
    mv "$dir"/* /mysite/user_uploaded_templates/myrandomname
else
    echo "rejected"
fi
rm -rf "$tempdest"

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.

# Bash (minimally tested)
tempdest=$(mktemp -d)
unzip -d "$tempdest" TEMPLATE1.ZIP
dir=("$tempdest"/*)
if (( ${#dir[@]} == 1 )) && [[ -d $dir ]]
    # in Bash, etc., scalar $var is the same as ${var[0]}
    mv "$dir"/* /mysite/user_uploaded_templates/myrandomname
else
    echo "rejected"
fi
rm -rf "$tempdest"
时间海 2024-10-12 02:45:38

除了您建议的选项之外,我看到的另一个选项是使用 unzip -j 标志,它将转储所有路径并将所有文件放入当前目录。如果您确定每个 TEMPLATE1.ZIP 文件都包含 index.html*.jpg 文件,那么您可以做一些事情就像:

destdir=/mysite/user_uploaded_templates/myrandomname
unzip -j -d "$destdir"
mkdir "${destdir}/images"
mv "${destdir}/*.jpg" "${destdir}/images"

这并不是最干净的解决方案,但至少您不必像在示例中那样进行任何解析。我似乎找不到任何类似于 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 your TEMPLATE1.ZIP files includes an index.html and *.jpg files then you can just do something like:

destdir=/mysite/user_uploaded_templates/myrandomname
unzip -j -d "$destdir"
mkdir "${destdir}/images"
mv "${destdir}/*.jpg" "${destdir}/images"

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.

不羁少年 2024-10-12 02:45:38

每个 zipunzip 命令都不同,但通常有一种方法可以列出文件内容。从那里,您可以解析输出以确定未知的目录名称。

在 Windows 上,1996 Wales/Gaily/van der Linden/Rommel 版本是 unzip -l

当然,您可以简单地允许解压缩将文件解压缩到所需的任何目录,然后使用 mv 将目录重命名为您想要的名称。

$tempDir = temp.$
mv $zipFile temp.$
cd $tempDir
unzip $zipFile
$unknownDir = *   #Should be the only directory here
mv $unknownDir $whereItShouldBe
cd ..
rm -rf $tempDir

为这些类型的操作创建一个临时目录始终是一个好主意,以防您最终运行此命令的两个实例。

Each zip and unzip 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.

$tempDir = temp.$
mv $zipFile temp.$
cd $tempDir
unzip $zipFile
$unknownDir = *   #Should be the only directory here
mv $unknownDir $whereItShouldBe
cd ..
rm -rf $tempDir

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文