2009-02-24 15 views
19

मेरे पास एक छोटी परियोजना है जो Google App Engine के लिए बिल्कुल सही होगी। कार्यान्वित करने से यह एक ज़िप फ़ाइल उत्पन्न करने और इसे वापस करने की क्षमता पर निर्भर करता है।क्या ऐप इंजन के साथ एक ज़िप फ़ाइल उत्पन्न करना और वापस करना संभव है?

ऐप इंजन की वितरित प्रकृति के कारण, जो मैं कह सकता हूं, से ज़िप फ़ाइल को पारंपरिक अर्थ में "इन-मेमोरी" नहीं बनाया जा सका। इसे मूल रूप से उत्पन्न किया जाना चाहिए और एक अनुरोध/प्रतिक्रिया चक्र में भेजा जाना चाहिए।

क्या पाइथन ज़िप मॉड्यूल भी ऐप इंजन वातावरण में मौजूद है?

उत्तर

32

zipfile ऐप्लिकेशन इंजन पर उपलब्ध है और पर फिर से काम example इस प्रकार है:

from contextlib import closing 
from zipfile import ZipFile, ZIP_DEFLATED 

from google.appengine.ext import webapp 
from google.appengine.api import urlfetch 

def addResource(zfile, url, fname): 
    # get the contents  
    contents = urlfetch.fetch(url).content 
    # write the contents to the zip file 
    zfile.writestr(fname, contents) 

class OutZipfile(webapp.RequestHandler): 
    def get(self): 
     # Set up headers for browser to correctly recognize ZIP file 
     self.response.headers['Content-Type'] ='application/zip' 
     self.response.headers['Content-Disposition'] = \ 
      'attachment; filename="outfile.zip"'  

     # compress files and emit them directly to HTTP response stream 
     with closing(ZipFile(self.response.out, "w", ZIP_DEFLATED)) as outfile: 
      # repeat this for every URL that should be added to the zipfile 
      addResource(outfile, 
       'https://www.google.com/intl/en/policies/privacy/', 
       'privacy.html') 
      addResource(outfile, 
       'https://www.google.com/intl/en/policies/terms/', 
       'terms.html') 
+1

रखें ध्यान में रखते हुए ऐप इंजन पर प्रतिक्रिया आकार सीमा 10 एमबी है, इसलिए आप उससे ज़िप्पी फ़ाइल वापस नहीं कर सकते हैं। शायद नई फाइल एपीआई (एसडीके 1.4.3) का उपयोग करके आप ज़िप फ़ाइल बना सकते हैं, इसे ब्लॉबस्टोर में स्टोर कर सकते हैं, फिर वापस लौट सकते हैं ब्लॉब –

+0

यह उत्तर 'buf = zipf.read (2048)' में विफल रहता है; पहले कोई रेफरी नहीं है 'zipf' के लिए erence; – Justin

+0

@ जस्टिन के नीचे दिए गए उत्तर का उपयोग करें: अद्यतन नमूना कोड बेहतर काम करना चाहिए। – myroslav

2

What is Google App Engine से:

You can upload other third-party libraries with your application, as long as they are implemented in pure Python and do not require any unsupported standard library modules.

तो, भले ही यह डिफ़ॉल्ट रूप से मौजूद नहीं है आप (संभावित) यह अपने आप को शामिल कर सकते हैं। । (मैं कहता हूँ अगर अजगर ज़िप पुस्तकालय किसी भी "असमर्थित मानक पुस्तकालय मॉड्यूल" की आवश्यकता है संभावित क्योंकि मैं नहीं जानता कि

9
import zipfile 
import StringIO 

text = u"ABCDEFGHIJKLMNOPQRSTUVWXYVabcdefghijklmnopqqstuvweyxáéöüï东 廣 広 广 國 国 国 界" 

zipstream=StringIO.StringIO() 
file = zipfile.ZipFile(file=zipstream,compression=zipfile.ZIP_DEFLATED,mode="w") 
file.writestr("data.txt.zip",text.encode("utf-8")) 
file.close() 
zipstream.seek(0) 
self.response.headers['Content-Type'] ='application/zip' 
self.response.headers['Content-Disposition'] = 'attachment; filename="data.txt.zip"' 
self.response.out.write(zipstream.getvalue()) 
संबंधित मुद्दे

 संबंधित मुद्दे