0

我正在尝试打开一个javascript文件,读取它,gzip它然后将它写回另一个文件..能够做到所有这些..但是如何在写入压缩内容之前设置“Content-Encoding:gzip”.. . 这里是代码:

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
4

1 回答 1

0

这通常由提供文件的任何内容决定。.gz当您创建文件并正确配置服务器时,您的工作通常仅限于设置正确的文件扩展名(在这种情况下)。

于 2011-09-29T16:10:42.343 回答