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)文章
pandas讀取文件夾下所有excel文件的實(shí)現(xiàn)
最近需要做一個(gè)需求,要求匯總一個(gè)文件夾所有的excel文件,所以本文就來介紹一下pandas讀取文件夾下所有excel文件的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
一文掌握6種Python中常用數(shù)據(jù)庫操作及代碼
在數(shù)據(jù)處理和管理領(lǐng)域,Python作為一種高效、易用的編程語言,擁有豐富的數(shù)據(jù)庫操作模塊,可以輕松實(shí)現(xiàn)對(duì)關(guān)系型數(shù)據(jù)庫的數(shù)據(jù)操作,本文將介紹六種常見的Python數(shù)據(jù)庫操作模塊,需要的可以參考下2023-12-12
python爬蟲_微信公眾號(hào)推送信息爬取的實(shí)例
下面小編就為大家?guī)硪黄猵ython爬蟲_微信公眾號(hào)推送信息爬取的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
OpenCV學(xué)習(xí)之圖像加噪與濾波的實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了OpenCV中圖像的加噪與濾波操作的相關(guān)資料,文中的示例代碼簡(jiǎn)潔易懂,具有一定的借鑒價(jià)值,需要的可以參考一下2023-02-02

