简体   繁体   English

阅读上传的zip文件

[英]Reading uploaded zip file

How can i read the fileitems in zip file that uploaded using form in html and using Cgi, i tried as this: 我如何阅读zip文件中的文件项目,该文件在html中使用表格上传并使用Cgi,我试过这样:

form = cgi.FieldStorage() 
file_upload = form['file']
if zipfile.is_zipfile(file_upload.filename):
    print "%s is a valid pkzip file" % file_upload.filename
else:
    print "%s is not a valid pkzip file" % file_upload.filename
zfile=zipfile.ZipFile(file_upload.filename,"r")
files_zip=zfile.namelist()

For example when i upload (test.zip)the error is No such file or directory: 'test.zip',and if i run the code without this zfile=zipfile.ZipFile(file_upload.filename,"r") , i get that test.zip is not a valid pkzip file. 例如,当我上传(test.zip)错误是没有这样的文件或目录:'test.zip',如果我运行没有这个zfile=zipfile.ZipFile(file_upload.filename,"r") ,我得到test.zip不是有效的pkzip文件。 Thanks in advance. 提前致谢。

You could try to pass file_upload.file to ZipFile instead of file_upload.filename . 您可以尝试将file_upload.file传递给ZipFile而不是file_upload.filename

Here's a script that prints the list of files in the zip file: 这是一个打印zip文件中文件列表的脚本:

import sys
sys.stderr = sys.stdout
print "Content-Type: text/plain"
print

import cgi
import zipfile

form = cgi.FieldStorage()

filefield = form['somefile']
print "Filename:", filefield.filename

if filefield.file is not None and zipfile.is_zipfile(filefield.file):
    zfile = zipfile.ZipFile(filefield.file)
    print "Name list:\n\t",
    print "\n\t".join(zfile.namelist())

And the corresponding html form: 和相应的html表单:

<!DOCTYPE html>
<form enctype="multipart/form-data" action="file-upload" method=post>
<p><label for=somefile>File: <input type=file name=somefile>
<p><input type=submit>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM