python使用內(nèi)存zipfile對(duì)象在內(nèi)存中打包文件示例
import zipfile
import StringIO
class InMemoryZip(object):
def __init__(self):
# Create the in-memory file-like object
self.in_memory_zip = StringIO.StringIO()
def append(self, filename_in_zip, file_contents):
'''Appends a file with name filename_in_zip and contents of
file_contents to the in-memory zip.'''
# Get a handle to the in-memory zip in append mode
zf = zipfile.ZipFile(self.in_memory_zip, "a", zipfile.ZIP_DEFLATED, False)
# Write the file to the in-memory zip
zf.writestr(filename_in_zip, file_contents)
# Mark the files as having been created on Windows so that
# Unix permissions are not inferred as 0000
for zfile in zf.filelist:
zfile.create_system = 0
return self
def read(self):
'''Returns a string with the contents of the in-memory zip.'''
self.in_memory_zip.seek(0)
return self.in_memory_zip.read()
def writetofile(self, filename):
'''Writes the in-memory zip to a file.'''
f = file(filename, "w")
f.write(self.read())
f.close()
if __name__ == "__main__":
# Run a test
imz = InMemoryZip()
imz.append("test.txt", "Another test").append("test2.txt", "Still another")
imz.writetofile("test.zip")
相關(guān)文章
解決Tkinter中button按鈕未按卻主動(dòng)執(zhí)行command函數(shù)的問(wèn)題
這篇文章主要介紹了解決Tkinter中button按鈕未按卻主動(dòng)執(zhí)行command函數(shù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05Python實(shí)現(xiàn)分?jǐn)?shù)序列求和
今天小編就為大家分享一篇Python實(shí)現(xiàn)分?jǐn)?shù)序列求和,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02pytorch-神經(jīng)網(wǎng)絡(luò)擬合曲線實(shí)例
今天小編就為大家分享一篇pytorch-神經(jīng)網(wǎng)絡(luò)擬合曲線實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01Python通過(guò)kerberos安全認(rèn)證操作kafka方式
這篇文章主要介紹了Python通過(guò)kerberos安全認(rèn)證操作kafka方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06python使用正則表達(dá)式分析網(wǎng)頁(yè)中的圖片并進(jìn)行替換的方法
這篇文章主要介紹了python使用正則表達(dá)式分析網(wǎng)頁(yè)中的圖片并進(jìn)行替換的方法,涉及Python使用正則表達(dá)式的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03用ldap作為django后端用戶登錄驗(yàn)證的實(shí)現(xiàn)
這篇文章主要介紹了用ldap作為django后端用戶登錄驗(yàn)證的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Python unittest基本使用方法代碼實(shí)例
這篇文章主要介紹了Python unittest基本使用方法代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06pygame庫(kù)實(shí)現(xiàn)移動(dòng)底座彈球小游戲
這篇文章主要為大家詳細(xì)介紹了pygame庫(kù)實(shí)現(xiàn)移動(dòng)底座彈球小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11深入理解Python虛擬機(jī)中字典(dict)的實(shí)現(xiàn)原理及源碼剖析
這篇文章主要介紹了在?cpython?當(dāng)中字典的實(shí)現(xiàn)原理,在本篇文章當(dāng)中主要介紹在早期?python3?當(dāng)中的版本字典的實(shí)現(xiàn),現(xiàn)在的字典做了部分優(yōu)化,希望對(duì)大家有所幫助2023-03-03