国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

python使用內(nèi)存zipfile對(duì)象在內(nèi)存中打包文件示例

2020-02-23 05:23:27
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

代碼如下:
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")

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 郧西县| 邻水| 米泉市| 泰和县| 新乐市| 石首市| 藁城市| 宝山区| 深泽县| 佛教| 南昌县| 景德镇市| 江都市| 南昌市| 抚宁县| 乐业县| 祁门县| 龙江县| 华池县| 万载县| 中西区| 绥化市| 凌云县| 康定县| 井冈山市| 大竹县| 措美县| 旬阳县| 乌兰浩特市| 巴林左旗| 福建省| 兴义市| 茶陵县| 苍南县| 东光县| 得荣县| 巩留县| 潞西市| 宜黄县| 老河口市| 仲巴县|