I'm working on a HTML WYSIWYG editor, and I am currently working on a 'Download' feature where the user can press a Download button to download a zip file of their theme. I am using a Python CGI script to achieve this feature. Currently, my script makes a zip file and prompts the user to Download it, but when I try to decompress the zip file, it only creates another zip file with the extension '.cpgz'. I believe that my script did not create the zip properly.
I am using the 'zipfile' module to create a zipfile object in memory instead of on Disk, the 'StringIO' module to create a filelike object in memory, and the 'cgi' module to accept POST data from an Ajax request.
My problem is in my for-loop. The 'zf' zip file isn't adding the files and sub-directories from the 'layoutDir' parameter I passed into os.walk(). The script will prompt the browser to download the zip file, but I am unable to decompress it.
#!/usr/bin/python
import sys
import os
import zipfile
import StringIO
import cgitb
cgitb.enable()
layoutDir = 'http://localhost:8888/funWYSIWYG/public/views/layouts/Marketing'
tmpZip = StringIO.StringIO()
zf = zipfile.ZipFile(tmpZip, 'w', zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk(layoutDir):
for name in files:
absfn = os.path.join(root, name)
relfn = absfn[len(layoutDir) + len(os.sep):]
zf.write(absfn, relfn)
zf.close()
sys.stdout.write("Content-Type: application/octet-stream\r\n")
sys.stdout.write("Content-Disposition: attachment; filename=\"funWYSIWYG-Marketing.zip\"\r\n\r\n")
sys.stdout.write(tmpZip.getvalue())
# Close opened file
tmpZip.close()
UPDATE 1: I got rid of some of the irrelevant stuff that was in my code. I also correct the typo with 'absfn' and 'adsfn'. The code above now represents exactly what I have in my local code editor. I am still having the same problem with being unable to decompress the zip file that is made.
UPDATE 2: Here is what the 'Marketing' directory looks like on my computer.
|---- Marketing
|---- css
| |---- default.css
|
|---- img
|
|---- index.html