• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

自定义python的类InMemoryZip,可以在内存压缩文件为zip

python 水墨上仙 2704次浏览

自定义python的类InMemoryZip,可以在内存压缩文件为zip,也可以对文件进行解压缩,同时还可以通过网络对文件进行压缩后进行高速传输

"""
   @author  Thomas Lehmann
   @file    InMemoryZip.py
   @brief   inspired by http://www.kompato.com/post/43805938842/in-memory-zip-in-python
            providing an in memory zip with some more features
   @note    tested with Python 2.7.5 and with Python 3.3
   @down   http://www.75271.com

   Copyright (c) 2013 Thomas Lehmann

   Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
   documentation files (the "Software"), to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
   and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in all copies
   or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
   INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
   DAMAGES OR OTHER LIABILITY,
   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import io
import os
import zipfile

class InMemoryZip:
    """ Basic idea behind was to be able to transfer single files or
        or a folder (with sub folders) as zipped memory via communication
        to another machine. """

    def __init__(self, data=None):
        """ creating an in memory file (zip archive) """
        self.inMemory = io.BytesIO()
        if data:
            self.setData(data)

    def getData(self):
        """ retrieving the whole file as buffer """
        self.inMemory.seek(0)
        return self.inMemory.read()

    def setData(self, data):
        """ creating new in memory file wit new content """
        self.inMemory = io.BytesIO()
        self.inMemory.write(data)

    def append(self, pathAndFileName):
        """ adding a file or a path (recursive) to the zip archive in memory """
        zf = zipfile.ZipFile(self.inMemory, "a", zipfile.ZIP_DEFLATED, False)

        if os.path.isfile(pathAndFileName):
            zf.write(pathAndFileName)
        else:
            path = pathAndFileName
            for root, folders, files in os.walk(path):
                for file in files:
                    fullName = os.path.join(root, file)
                    zf.write(fullName)

    def saveAs(self, pathAndFileName):
        """ does save the in memory zip archive to a file """
        file = open(pathAndFileName, "wb")
        file.write(self.getData())
        file.close()

    def readFrom(self, pathAndFileName):
        """ reading a file expected to be a ZIP file """
        self.setData(open(pathAndFileName, "rb").read())

    def extractAllAt(self, path):
        """ does extract all files in memory below given path;
            the underlying zipfile module is clever enough to
            create the necessary path if not existing """
        zf = zipfile.ZipFile(self.inMemory)
        zf.extractall(path)

    def listContent(self):
        """ getting list of zip files entries (ZipInfo instances) """
        zf = zipfile.ZipFile(self.inMemory)
        return zf.infolist()

def test():
    zm = InMemoryZip()
    zm.append(".")
    zm.saveAs("InMemoryZip_test.zip")

    zm2 = InMemoryZip(zm.getData())
    zm2.extractAllAt("./tmp")

    zm3 = InMemoryZip()
    zm3.readFrom("InMemoryZip_test.zip")
    for entry in zm3.listContent():
        print(entry.filename)

if __name__ == "__main__":
    test()


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明自定义python的类InMemoryZip,可以在内存压缩文件为zip
喜欢 (0)
加载中……