国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 平谷区| 蒲江县| 甘洛县| 胶州市| 墨玉县| 乳山市| 临澧县| 麻栗坡县| 离岛区| 肃宁县| 赤峰市| 如东县| 凤阳县| 屏东县| 江都市| 浦江县| 巩留县| 雷山县| 新郑市| 油尖旺区| 揭西县| 广东省| 道真| 亳州市| 尼勒克县| 湖州市| 湘潭县| 平凉市| 昌乐县| 贞丰县| 湖州市| 同德县| 武山县| 柏乡县| 托里县| 河间市| 和龙市| 静宁县| 永昌县| 新龙县| 平昌县|