Welcome, guest | Sign In | My Account | Store | Cart

Why implementing this?

  • transfering a file or a folder (including sub folders) to another machine
  • therefore zipping content to one compressed buffer
  • keeping the relating ZIP in memory only but ...
  • being able to save or load too
  • and being able to unzip again at target destination
Python, 100 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
"""
   @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

   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()

The basic usage can be seen in the test function in the code. Anyway:

imz = InMemoryZip()
# does add the the whole .vim folder to the ZIP archive in memory
imz.append("~/.vim")
# does save the archive to /tmp
imz.saveAs("/tmp/.vim.zip")

For sending the data via communication:

data = imz.getData()
connection.send(data)

For receiving data:

data = connection.receive()
imz  = InMemoryZip(data)