# # Creating a simple compression software for windows. # The purpose is to compress all of the files in one dir # and to compress them with various types of algorithm. # # Copyright (C) 2007 Freeman Lo # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # # # FUTURE Plans: create a recursive compression, error handling, WX GUI, md5sum check, # save it to a directory. # # Instructions: # When using zip algorithm: # 1) python backup.py type inputFileName outputFileName.zip # 2) python backup.py type dirpath outputFileName.zip # Examples: # python backup.py zip readme.txt readme.zip # python backup.py zip c:\\doc docDir.zip # # When using gzip algorithm: # 1) python backup.py type mode compressionLevel inputFileName outputFileName.gzip # 2) python backup.py type mode compressionLevel dirpath outputFileName.gzip # # When using bz2 algorithm: # 1) python backup.py type mode compressionLevel inputFileName outputFileName.bz2 # 2) python backup.py type mode compressionLevel dirpath outputFileName.bz2 # ####################################################################################### import os, sys def gnuLicense(): mess="backup.py version 0.0.1, Copyright (C) 2007 Freeman Lo \n backup.py comes with ABSOLUTELY NO WARRANTY;\n This is free software, and you are welcome to redistribute it under certain conditions.\n" print mess def createDefaultDir(type): if type == "zip": os.mkdir("c:\\zipfile") elif type == "bz2": os.mkdir("c:\\bz2file") elif type == "gzip": os.mkdir("c:\\gzipfile") def chkFile(list): tmp=[] for a in list: if os.path.isfile(a): tmp.append(a) return tmp def mkDDir(): if sys.platform == "win32": p = os.getcwd() + "\\CompressFileDir" os.mkdir(p) else: p = os.getcwd() + "\\CompressFileDir" os.mkdir(p) import zipfile, glob def mkZip(ifDir, ofName): if os.path.isfile(ifDir): if not os.path.isabs(ifDir): fn = os.getcwd() + "\\" + ifDir zip = zipfile.ZipFile(ofName, "w") zip.write(fn, os.path.basename(fn), zipfile.ZIP_DEFLATED) zip.close() else: zip = zipfile.ZipFile(ofName, "w") zip.write(ifDir, os.path.basename(ifDir), zipfile.ZIP_DEFLATED) zip.close() elif os.path.isdir(ifDir): tDir = [] tPath = ifDir + "\\*" tDir = glob.glob(tPath) file = chkFile(tDir) zip = zipfile.ZipFile(ofName, "w") for a in file: zip.write(a, os.path.basename(a), zipfile.ZIP_DEFLATED) zip.close() else: print "This is the recursive portion, where i have not implemented" import gzip def mkGzip(fip, fop, mode='wb', cl=6): if os.path.isfile(fip): if not os.path.isabs(fip): fn = os.getcwd() + "\\" + fip inf = file(fn, 'rb') st = inf.read() inf.close() gz = gzip.GzipFile(os.path.basename(fop), mode, cl) gz.write(st) gz.close() else: inf = file(fip, 'rb') st = inf.read() inf.close() gz = gzip.GzipFile(os.path.basename(fop), mode, cl) gz.write(st) gz.close() elif os.path.isdir(fip): tDir = [] tPath = fip + "\\*" tDir = glob.glob(tPath) file = chkFile(tDir) gz = gzip.GzipFile(os.path.basename(fop), mode, cl) for a in file: inf = file(a, 'ab') st = inf.read() gz.write(st) inf.close() gz.close() else: print "This is the recursive portion, where i have not implemented" import bz2 def mkBzip(fip, fop, mode='w', buf=0, cl=6): if os.path.isfile(fip): if not os.path.isabs(fip): fn = os.getcwd() + "\\" + fip inf = file(fn, 'r') st = inf.read() inf.close() bz = bz2.BZ2File(os.path.basename(fop), mode, buf, cl) bz.write(st) bz.close() else: inf = file(fip, 'r') st = inf.read() inf.close() bz = bz2.BZ2File(os.path.basename(fop), mode, buf, cl) bz.write(st) bz.close() elif os.path.isdir(fip): tDir = [] tPath = fip + "\\*" tDir = glob.glob(tPath) file = chkFile(tDir) bz = bz2.BZ2File(os.path.basename(fop), mode, buf, cl) for a in file: inf = file(a, 'ab') st = inf.read() bz.write(st) inf.close() bz.close() else: print "This is the recursive portion, where i have not implemented" def main(arg=sys.argv): arg = arg[1:] if len(arg) == 3: ztype = arg[1].lower() ipFile = arg[2].lower() opFile = arg[3].lower() elif len(arg) == 5: ztype = arg[1].lower() mode = arg[2].lower() clevel = arg[3].lower() ipFile = arg[4].lower() opFile = arg[5].lower() else: print "Are you sure you have typed in the number of correct parameters?" if ztype == "gzip": mkGzip(ipFile, opFile, mode, clevel) elif ztype == "zip": mkZip(ipFile, opFile ) elif ztype == "bz2": mkBzip(ipFile, opFile, mode, 0, clevel) else: print "Check your parameters, there are no other options dude!" main()