You are not logged in.
Hello,
I have a strange probleme. I use this piece of python2 code to generate PDF and TEX files (thats my "creation(parametres)" fonction) in a tmp file (in /tmp).
This PDF and TEX files are compress in one ZIP call exercice.zip and send to the client.
# Creation d'un dossier temporaire dans tmp et creation des exercices
parametres['chemin_fichier'] = mkdtemp(dir=dossier_creation)
# Au cas ou la creation des exercices echoue
try:
creation(parametres)
# Compression de tous les fichiers crees en zip
buffer_zip = BytesIO()
archive = ZipFile(buffer_zip, 'w', ZIP_DEFLATED)
for fichier in listdir(parametres['chemin_fichier']):
archive.write(fichier)
archive.close()
# Envoie du zip
buffer_zip.seek(0)
print "Content-type: application/zip\r\nContent-Disposition: attachment; filename=exercice.zip\r\nContent-Title: exercice.zip\r\n\r\n"
print buffer_zip.read()
buffer_zip.close()
return u"Terminé"
except:
return u"Erreur interne"
finally:
# Supression du dossier temporaire
rmtree(parametres['chemin_fichier'])
This code work, but i have a problem with the ZIP file. This file work with fileroller, 7-zip, but windows XP built in tools won't (invalid file). On android, Astro File manager and androzip file manager show me an empty file.
I search the whole internet, but....
Last edited by jbreizh (2014-02-07 05:14:26)
Offline
If you to see my app in full action, Actimaths, but i don't think it work well as i live in New Caledonia (french pacific island) and our internet is sssslllllllloooooowwwwwwww.
Here also an exemple of ZIP file: exercice.zip
Last edited by jbreizh (2014-01-30 01:19:03)
Offline
You didn't try regular unzip?
% unzip exercice.zip
Archive: exercice.zip
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of exercice.zip or
exercice.zip.zip, and cannot find exercice.zip.ZIP, period.
Try to do it without seek()ing, as that is almost certainly the cause of your trouble.
Offline
Ok,
no i didn't try with regular unzip. So their is a problem with my zip. So i try with this code:
# Creation d'un dossier temporaire dans tmp et creation des exercices
parametres['chemin_fichier'] = mkdtemp(dir=dossier_creation)
# Au cas ou la creation des exercices echoue
try:
creation(parametres)
# Compression de tous les fichiers crees en zip
# buffer_zip = BytesIO()
# archive = ZipFile(buffer_zip, 'w', ZIP_DEFLATED)
archive = ZipFile("/tmp/test.zip", 'w', ZIP_DEFLATED)
for fichier in listdir(parametres['chemin_fichier']):
archive.write(fichier)
archive.close()
# Envoie du zip
#buffer_zip.seek(0)
print "Content-type: application/zip\r\nContent-Disposition: attachment; filename=exercice.zip\r\nContent-Title: exercice.zip\r\n\r\n"
print open("/tmp/test.zip", 'rb').read()
#buffer_zip.close()
return u"Terminé"
except:
return u"Erreur interne"
finally:
# Supression du dossier temporaire
rmtree(parametres['chemin_fichier'])
I take back the bytesIO for the moment, so i can see my original zip and the send one. The original zip is ok and the send one is not. I also see take an 92k original zip file become an 200+k zip file. So their is a problem in my send part:
print "Content-type: application/zip\r\nContent-Disposition: attachment; filename=exercice.zip\r\nContent-Title: exercice.zip\r\n\r\n"
print open("/tmp/test.zip", 'rb').read()
An idea ?
Offline
So i find the problem !!!!
In fact their was 2 problems , one that I introduce later witch was adding a web page to the zip (pfffffffffff)
BUT the real problem was :
print "Content-type: application/zip\r\nContent-Disposition: attachment; filename=exercice.zip\r\nContent-Title: exercice.zip\r\n\r\n"
It introduce a blank line at the beginning of the ZIP file so it become:
print "Content-type: application/zip\r\nContent-Disposition: attachment; filename=exercice.zip\r\nContent-Title: exercice.zip\r\n"
I originaly add \r\n\r\n because of advise found on internet. Maybe someone has clear explanation about this?
Thanks for your support
Offline
Python2's print statement adds a newline to whatever it prints, so you actually printed "\r\n\r\n\n" at the end of that line. You can suppress this by adding a comma to the end of the line. The print function has a better syntax for this (which you can use in Python2 by adding "from __future__ import print_function" at the top of the file).
print("content-type: application/zip\r\nblahblahblah\r\n\r\n", end='')
Offline
thanks Trent for this explaination. So i read the doc about print and you re rigth it's clearly mark that print had a \n at the end of the line.
Offline