python使用內(nèi)存zipfile對象在內(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按鈕未按卻主動執(zhí)行command函數(shù)的問題
這篇文章主要介紹了解決Tkinter中button按鈕未按卻主動執(zhí)行command函數(shù)的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05pytorch-神經(jīng)網(wǎng)絡(luò)擬合曲線實例
今天小編就為大家分享一篇pytorch-神經(jīng)網(wǎng)絡(luò)擬合曲線實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01python使用正則表達式分析網(wǎng)頁中的圖片并進行替換的方法
這篇文章主要介紹了python使用正則表達式分析網(wǎng)頁中的圖片并進行替換的方法,涉及Python使用正則表達式的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03用ldap作為django后端用戶登錄驗證的實現(xiàn)
這篇文章主要介紹了用ldap作為django后端用戶登錄驗證的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12深入理解Python虛擬機中字典(dict)的實現(xiàn)原理及源碼剖析
這篇文章主要介紹了在?cpython?當(dāng)中字典的實現(xiàn)原理,在本篇文章當(dāng)中主要介紹在早期?python3?當(dāng)中的版本字典的實現(xiàn),現(xiàn)在的字典做了部分優(yōu)化,希望對大家有所幫助2023-03-03