0

I have uploaded a zip file using django in a local directory . How do I unzip it , and store it in 3 different files ?

Ok . Let me explain my problem a bit more in detail .

def upload(request):
    if request.method == 'POST':
        form = UploadForm(request.POST, request.FILES)
        if form.is_valid():
            form.handle(request.FILES['file_obj'])
            #form.save() # if a modelform
            #form.cleaned_data['user'] = request.user
            z = zipfile.ZipFile('file_obj')
        for files in z.namelist():
            file(files,'wb').write(z.read(files))

        z.close()

            return render_to_response('uploaded.html', RequestContext(request,{}))
    else:
        form = UploadForm()
    return render_to_response('upload.html', RequestContext(request,{'form': form})

)

This is my upload form , which is supposed to work . file_obj contains the uploaded zip file . But it doesnt give any output .

4
  • Did you look in the Python documentation for anything to do with zip files? Jun 6, 2011 at 18:03
  • I did . extract and extractall are not working properly .
    – Hick
    Jun 6, 2011 at 18:09
  • Those look like GIS files, have you tried GIS specific packages?
    – Stedy
    Jun 6, 2011 at 18:34
  • I have edited my question to explain my problem .
    – Hick
    Jun 6, 2011 at 18:41

1 Answer 1

0

It doesnt look like you're actually opening the upload, but a file in the current directory called 'file_obj'. You want something more like

z = zipfile.ZipFile(request.FILES['file_obj'])

also I might be wrong, but i don't think you need the form.handle() call at all, at least I've never used it, but i might stand corrected.

Also, you want to be very careful here, as you're writing out to the filenames contained in the zipfile, which for all you know could be absolute paths somewhere. You need to be very careful with that. In case I didn't say it, be careful with user created content, it might be malicious.

1
  • Tried that already but doesn't work . The zip file still remains without being extracted .
    – Hick
    Jun 7, 2011 at 4:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.