我正在尝试打开一个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
答案 0 :(得分:0)
这通常由whatever is serving the file.决定。当您创建文件并正确配置服务器时,您的工作通常仅限于设置正确的文件扩展名(在本例中为.gz
)。