目录中的所有内容/home/username/main/books/
都会被写入并返回,但/home/username/main/index.html
不会被写入,因为“index.html”不是目录,因此无法遍历。我将如何修改脚本以说明它是否是一个目录,遍历它然后编写它找到的所有内容,如果它是直接文件,则编写它。
Python
def zipit (request):
file_paths = ['/home/username/main/books/', '/home/username/main/index.html']
buffer= StringIO.StringIO()
z= zipfile.ZipFile( buffer, "w" )
for p in file_paths:
for dir, subdirs, files in os.walk(p):
for f in files:
filename = os.path.join(dir, f)
z.write(filename, arcname = filename[15:])
z.close()
buffer.seek(0)
final = HttpResponse(buffer.read())
final['Content-Disposition'] = 'attachment; filename=dbs_custom_library.zip'
final['Content-Type'] = 'application/x-zip'
return final