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

After searching the web, ASPN and not finding a simple example of using gzip to zip a file. I created this little gem. it is a little rough at the edges, ie it does not check to see if the file it is zipping to is there (it will over write it, you have been warned).

Python, 72 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
#!
import string
import gzip
from optparse import OptionParser

#
# version 1.01
#created by T.Newell 06/11/2007
# after searching the web ASPN and not finding a simple example of using gzip to zip a file i created this little gem.
#

def readCommandLine():
	parser = OptionParser()
	#read the options in
	parser.add_option("-f","--Full_file_location",
					dest="File_to_be_run",
					default=r"c:\tn.txt",
					help="This is the fully qualified path name to the file location")

	parser.add_option("-m","--Mode",
					dest="modeTn",
					default="r",
					help="The mode of zip unzip")
	
	parser.add_option("-c","--Compression",
					dest="compress",
					default=9,
					help="The level of compression")
	options, args = parser.parse_args()
	#print options
	return options

def zipit(filename, mode,compress):
	#Saves/Zipps a compressed file to disk
	#
	r_file = open(filename, 'r')
	# this is the zipping bit
	w_file = gzip.GzipFile(filename + '.gz', mode, compress)
	w_file.write(r_file.read())
	w_file.flush()
	w_file.close()
	r_file.close()

def un_zipit(filename,mode):
	#Unzips a compressed file from disk
	#
	#this is the unzipping bit	
	r_file = gzip.GzipFile(filename, mode)
	write_file = string.rstrip(filename, '.gz')
	w_file = open(write_file, 'w')
	w_file.write(r_file.read())
	w_file.close()
	r_file.close()

if __name__ == "__main__":
	#first thing to do is read the options in
	options = readCommandLine()
	if options.modeTn == "r":
		#unzippit mode
		if options.File_to_be_run[-3:] != '.gz':
			# check to see if it has the extension .gz
			print "This " + options.File_to_be_run + " is not a .gz file"
		else:
			#This should now unzipit
			un_zipit(options.File_to_be_run,options.modeTn)
	elif options.modeTn== "wb":
			#this should zipit
			zipit(options.File_to_be_run,options.modeTn,options.compress)
	else:
		# basically the wrong option was passed
		print "ABORT something went wrong"
		sys.exit()

I know this is not perfect but should work simply on most versions.