在 Django 中对生成的 zip 文件设置正确的权限

发布于 2024-11-04 20:52:47 字数 1366 浏览 1 评论 0原文

我使用 Django 和 Python2.6 生成自定义呈现的 Django 模板的 zip 文件,以便每个用户下载自定义的 zip 文件。目前,views.py 中的代码如下所示:

def download(request):
    response = HttpResponse(mimetype='application/x-zip-compressed')
    response['Content-Disposition'] = 'attachment; filename=download.zip'

    myzip = zipfile.ZipFile(response, 'w')

    now = datetime.datetime.now()
    zipInfo = zipfile.ZipInfo('thefile.txt', (now.year, now.month, now.day, now.hour, now.minute, now.second))
    myzip.writestr(zipInfo, render_to_string('template.txt', locals(), context_instance=RequestContext(request)))
    myzip.close()

    return response

大多数情况下,这工作正常:zip 文件(在本例中包含单个 txt 文件)已正确下载,我可以提取内容。然而,唯一的问题是,生成的文件的权限对于我的默认用户来说既不是读取也不是写入,对于我的网站用户也不是。

如何在下载前更改自动生成文件的权限?

更新

我已经按照迈克的建议尝试使用os.chmodos.fchmod,但是这个要么需要路径名(我没有),要么给出错误(对于 fchmod):

ZipFile 实例没有属性 '__trunc__'

我猜有一个选项,首先保存 zip 文件,设置权限,然后允许下载,但这似乎有点矫枉过正 - 必须有更好的方法来克服这个简单的问题。有人有什么建议或想法吗?

Update2

这个问题似乎仅限于 Unix 系统,因为它在 Windows 和(显然)OS X 中运行良好。我发现了一个类似的线程 此处。据我所知,它一定与 writestr 方法有关。如何设置使用 writestr 添加到 zip 文件的文件的权限?

I'm using Django and Python2.6 to generate a zip file of custom-rendered Django templates for each user to download a custom-made zip file. At the moment, the code in views.py looks like this:

def download(request):
    response = HttpResponse(mimetype='application/x-zip-compressed')
    response['Content-Disposition'] = 'attachment; filename=download.zip'

    myzip = zipfile.ZipFile(response, 'w')

    now = datetime.datetime.now()
    zipInfo = zipfile.ZipInfo('thefile.txt', (now.year, now.month, now.day, now.hour, now.minute, now.second))
    myzip.writestr(zipInfo, render_to_string('template.txt', locals(), context_instance=RequestContext(request)))
    myzip.close()

    return response

Mostly, this works fine: the zip file (containing a single txt file in this example) is downloaded correctly, and I can extract the contents. The only problem is, however, that the permissions on the generated file are neither read nor write for my default user, and neither will it be for my website users.

How do I change the permissions of the auto-generated file before download?

Update:

I've tried using os.chmod and os.fchmod, as suggested by Mike, but this either requires a path name (which I don't have) or gives an error (for fchmod):

ZipFile instance has no attribute '__trunc__'

One option, I guess, would be to save the zip file first, setting the permissions, and then allowing download, but that seems like overkill - there must be a better way to overcome this simple problem. Anyone have any suggestions or ideas?

Update2:

It seems this issue is limited to Unix systems, as it works fine in Windows and (apparently) OS X. There's a similar thread I found here. As far as I can tell, it must be related to the writestr method. How do I set the permissions on a file added to a zip file with writestr?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

唐婉 2024-11-11 20:52:47

我认为这是您用来提取 zip 的任何内容的问题。这些权限对我来说似乎很好:

zk@fool:~/Downloads% ls -l | grep thefile
-rwxr-xr-x@  1 zk  staff           9 May  3 06:37 thefile.txt*

对于我来说,在 osx 和 windows 的内置 zip 资源管理器和 7-zip 上使用 Archive Utility 效果很好。检查生成的 zip 显示文件根本没有属性。所以我怀疑您用来解压缩文件的任何内容只是错误地设置了权限。

您可以尝试设置 ZipInfo.external_attr:

zipInfo.external_attr = 0777 << 16L # set permissions on file

似乎可以修复 Linux 系统上的权限:

zk@arch:~% ls -l | grep thefile
-rwxrwxrwx  1 zk     9 May  3 07:06 thefile.txt*

I think this is an issue with whatever you are using to extract the zip. The permissions seem fine to me:

zk@fool:~/Downloads% ls -l | grep thefile
-rwxr-xr-x@  1 zk  staff           9 May  3 06:37 thefile.txt*

Works fine for me with Archive Utility on osx and window's built-in zip explorer and 7-zip. Inspecting the generated zip shows files have no attributes at all. So I suspect whatever you are using to unzip the file is just setting permissions incorrectly.

You could try setting ZipInfo.external_attr:

zipInfo.external_attr = 0777 << 16L # set permissions on file

seems to fix the permissions on a linux system:

zk@arch:~% ls -l | grep thefile
-rwxrwxrwx  1 zk     9 May  3 07:06 thefile.txt*
才能让你更想念 2024-11-11 20:52:47

在 unix 中,每个进程都有默认的文件权限掩码..请继续阅读:
掩码
尝试将其设置为 django 进程。

In unix every process have default file permissions mask.. read on:
umask
try setting it for django process.

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