File zip package, stream download Python code

Source: Internet
Author: User

Usage:

Input address: for example, http: // 0.0.0.0: 8000? ABC, def

#! Usr/bin/Python

# Coding = UTF-8
Import UUID
Import OS
Import zipfile
Import re
Import sys
Import officetoimage
Import rarfile
Import tarfile
Import subprocess
From cherrypy import wsgiserver
Import time
Import threading

######## Set code to deal Chinese ############
Default_encoding = 'utf-8'
If SYS. getdefaultencoding ()! = Default_encoding:
Reload (sys)
SYS. setdefaultencoding (default_encoding)

Def app (Environ, start_response ):
Out = 'tmp/'{str(uid.uuid1({{}'.zip'
Status = '100 OK'
Filename = 'test.zip'
Response_header = [('content-type', 'application/'+ out), ('content-disposition', 'attachment; filename =' + filename)]
Start_response (status, response_header)
# Dict_e = get_environ ()
Filename_list = environ ['query _ string']. Split (',')
If OS. Path. exists ('tmp ') is not true:
OS. makedirs ('tmp ', 0755)


Position = 0
Flag = true

Archive = zipfile. zipfile (Out, 'A', zipfile. zip_deflated)
STRs = open (Out, 'rb ')
For filename in filename_list:
If OS. Path. isfile (filename ):
Archive. Write (filename)
STRs. Seek (position)
Result = STRs. Read ()
Position = STRs. Tell ()
Yield result
Else:
For root, dirs, files in OS. Walk (filename ):
For file in files:
Archive. Write (OS. Path. Join (root, file ))
STRs. Seek (position)
Result = STRs. Read ()
Position = STRs. Tell ()
Yield result
Archive. Close ()
STRs. Seek (position)
Result = STRs. Read ()
Position = STRs. Tell ()
Yield result
Return

If _ name _ = '_ main __':
Server = wsgiserver. cherrypywsgiserver ('0. 0.0.0 ', 8000), APP)
Server. Start ()


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.