On Mar 6, 6:32 am, Tom Link <micat...@gmail.com> wrote:
> >         Item     Tool/Suite   Free   Website
> >         ----     ----------   ----   -------
> >         Winzip      tool       n    http://www.winzip.com/downwz.htm
>
> Built-in support for zlib would be better of course. Anyway, I'd
> suggest to restrict the above list to OSS.
>

Since Vim supports Python on multiple platforms, it could be used
to add support for multiple archive types. The functions below can
pack/unpack gz, bz2 and zip (files can only be added "incrementally").
The code is just a proof of concept and may be the starting point
for a real vim-packer.

Marko

# file: vimpack.py
import os, sys
import shutil
import optparse
from string import Template
import zlib, gzip, bz2, zipfile

def copyfile(fin, fout, chunksize=4096):
    while True:
        buf = fin.read(chunksize)
        fout.write(buf)
        if len(buf) < chunksize: break

def gzip_compress(fname, fnameOut, level=6):
    fin = open(fname, 'rb')
    fout = gzip.GzipFile(fnameOut, 'wb', level)
    copyfile(fin, fout)
    fin.close()
    fout.close()

def gzip_expand(fname, fnameOut):
    fin = gzip.GzipFile(fname, 'rb')
    fout = open(fnameOut, 'wb')
    copyfile(fin, fout)
    fin.close()
    fout.close()

def bzip2_compress(fname, fnameOut, level=9):
    fin = open(fname, 'rb')
    fout = bz2.BZ2File(fnameOut, 'w', compresslevel=level)
    copyfile(fin, fout)
    fin.close()
    fout.close()

def bzip2_expand(fname, fnameOut):
    fin = bz2.BZ2File(fname, 'r')
    fout = open(fnameOut, 'wb')
    copyfile(fin, fout)
    fin.close()
    fout.close()

# TODO: Find latest versioned file
def zip_expand(archive, fnameArchive, fnameFs, level=9):
    arch = zipfile.ZipFile(archive, 'r')
    fin = arch.open(fnameArchive, 'r')
    fout = open(fnameFs, 'wb')
    copyfile(fin, fout)
    fin.close()
    fout.close()
    arch.close()

# If a file fnameArchive is already in zip, a suffix ;N will be added
# Otherwise we'd have to rewrite the whole zip file.
def zip_compress(archive, fnameFs, fnameArchive, level=9):
    arch = zipfile.ZipFile(archive, 'a', zipfile.ZIP_DEFLATED)
    namelist = arch.namelist()
    if fnameArchive in namelist:
        n=1
        while "%s;%d" % (fnameArchive, n) in namelist: n+=1
        fnameArchive = "%s;%d" % (fnameArchive, n)
    arch.write(fnameFs, fnameArchive)
    arch.close()

# TODO: Copy to a new zip keeping only latest versioned files
def zip_pack(archive):
    arch = zipfile.ZipFile(archive, 'r')
    namelist = arch.namelist()
    for name in namelist:
        if name.find(";") >= 0: print name
    arch.close()

def test():
    gzip_expand("example/example1c.txt.gz", "xdata/example1c.txt")
    gzip_compress("example/example1.txt", "xdata/example1.txt.gz")
    gzip_expand("xdata/example1.txt.gz", "xdata/example1.txt")

    bzip2_expand("example/example2c.txt.bz2", "xdata/example2c.txt")
    bzip2_compress("example/example2.txt", "xdata/example2.txt.bz2")
    bzip2_expand("xdata/example2.txt.bz2", "xdata/example2.txt")

    # example3c.zip contains exampleX.txt (X=1..3)
    zip_expand("example/example3c.zip", "example3.txt", "xdata/
example3c.txt")
    zip_compress("xdata/example3.zip", "example/example3.txt",
"example3.txt")
    zip_expand("xdata/example3.zip", "example3.txt", "xdata/
example3.txt")
    zip_pack("xdata/example3.zip")

if __name__ == "__main__":  test()

-- 
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Reply via email to