0

I am trying to open a javascript file, read it, gzip it and then write it back to another file.. able to do all that.. but how can set the "Content-Encoding : gzip" before writing the compressed content... here is the code:

import os, sys, mimetypes, zipfile, gzip, cStringIO
from optparse import OptionParser
def main():
    parser = OptionParser(usage='usage: %prog [options] src_file destination_file')
    parser.add_option('-x', '--expires', action='store_true', help='set far future expiry for all files')
    (options, args) = parser.parse_args()
    if len(args) != 2:
        parser.error("incorrect number of arguments")
    name = os.path.normpath(args[0])
    des_file = os.path.normpath(args[1])

    try:
        s_file = open(name, 'r')
        content = s_file.read()

        compressed = cStringIO.StringIO()
        gz = gzip.GzipFile(filename=name,  mode='w', fileobj=compressed)
        gz.write(content)
        gz.close()
        s_file.close()

        o_file = open(des_file, 'w')
        ##
        ## BEFORE WRITING THE CONTENT INTO A FILE HOW WE ADD THE Content-Encoding
        ##
        o_file.write(compressed.getvalue())
        o_file.close()

    except (IOError, os.error), why:
        print 'Failed to read the file', filename, '\n Exception:', why
Vicky
  • 12,934
  • 4
  • 46
  • 54
Santosh S Kumar
  • 469
  • 1
  • 6
  • 30

1 Answers1

0

That is usually determined by whatever is serving the file. Your job is usually limited to setting the correct file name extension (.gz in this case) when you create the file and configuring the server properly.

the wolf
  • 34,510
  • 13
  • 53
  • 71