Пример #1
0
    def extract(self, member, path=".", fileobj=None):
        """Extract and decompress `member` into `path` which defaults to the
        current directory."""
        self.fileobj.seek(member._offset)
        decomp = bz2.BZ2Decompressor()
        if fileobj:
            data = self.fileobj.read(member.size)
            fileobj.write(decomp.decompress(data))
            return

        dstpath = safe_join(path, member.name)
        dirname = os.path.dirname(dstpath)
        if not os.path.exists(dirname):
            os.makedirs(dirname)

        output = open(dstpath, "wb")
        toread = member.size
        while True:
            thisblock = min(128 * 1024, toread)
            block = self.fileobj.read(thisblock)
            if not block:
                break
            toread -= len(block)
            output.write(decomp.decompress(block))
        output.close()
        os.chmod(dstpath, member.flags)

        return dstpath
Пример #2
0
    def extract(self, member, path="."):
        """Extract `member` into `path` which defaults to the current
        directory. Absolute paths are converted to be relative to `path`

        Returns the path the member was extracted to."""
        dstpath = safe_join(path, member.name)
        dirname = os.path.dirname(dstpath)
        if not os.path.exists(dirname):
            os.makedirs(dirname)

        self.fileobj.seek(member._offset)
        # TODO: Should this be done all in memory?
        open(dstpath, "wb").write(self.fileobj.read(member.size))
        os.chmod(dstpath, member.flags)

        return dstpath
Пример #3
0
    def extract(self, member, path=".", fileobj=None):
        """Extract `member` into `path` which defaults to the current
        directory. Absolute paths are converted to be relative to `path`

        Returns the path the member was extracted to."""
        self.fileobj.seek(member._offset)
        if fileobj:
            fileobj.write(self.fileobj.read(member.size))
            return

        dstpath = safe_join(path, member.name)
        dirname = os.path.dirname(dstpath)
        if not os.path.exists(dirname):
            os.makedirs(dirname)

        # TODO: Should this be done all in memory?
        open(dstpath, "wb").write(self.fileobj.read(member.size))
        os.chmod(dstpath, member.flags)

        return dstpath
Пример #4
0
 def test_absolute(self):
     self.assertEquals(safe_join("/a/b/c", "/foo/bar"), "/a/b/c/foo/bar")
Пример #5
0
 def test_normal(self):
     self.assertEquals(safe_join("/a/b/c", "foo/bar"), "/a/b/c/foo/bar")
Пример #6
0
def test_safe_join(a, b):
    result = safe_join(a, b)
    assert result.startswith(a)
Пример #7
0
 def test_absolute(self):
     self.assertEquals(safe_join("/a/b/c", "/foo/bar"), "/a/b/c/foo/bar")
Пример #8
0
 def test_normal(self):
     self.assertEquals(safe_join("/a/b/c", "foo/bar"), "/a/b/c/foo/bar")