python如何得到gz文件中被压缩的文件的大小

zhgflx 2010-10-17 09:14:31
例如test.xls.gz文件解压后得到test.xls文件
该如何得到test.xls文件的大小

速度要快
...全文
375 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
blueice12 2012-07-13
  • 打赏
  • 举报
回复
fseek(fp, -4, SEEK_END)
angel_su 2010-10-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zhgflx 的回复:]
读最后4个字节的话,seek要花比较长的时间,是否有比较短的时间内可以解决的呢?
[/Quote]
貌似seek不会因为位置在尾端所以得耗更多时间。看过c标准fseek简单测试百万次也就1秒,python估计也是调用c库然后加点料,可能就慢个30%应该够快的啦...
angel_su 2010-10-18
  • 打赏
  • 举报
回复
不需要解开的话,试试读取文件最后4byte...
zhgflx 2010-10-18
  • 打赏
  • 举报
回复
返回的只是压缩包的大小,而不是原始文件的大小
[Quote=引用 1 楼 insisted_search 的回复:]
os.path.getsize('d:/a.html')
[/Quote]
zhgflx 2010-10-18
  • 打赏
  • 举报
回复
读最后4个字节的话,seek要花比较长的时间,是否有比较短的时间内可以解决的呢?
[Quote=引用 3 楼 angel_su 的回复:]
不需要解开的话,试试读取文件最后4byte...
[/Quote]
aa515200aa 2010-10-18
  • 打赏
  • 举报
回复
这是我上次写的代码,不只包括这个功能,还有其他的,你可以参考下
#coding:utf-8


import urllib2
import StringIO,gzip,re
from time import ctime

MANZZBD_PPURLGF = 'http://.+?\..+?\..+'
URLXFPE_RAWORDV = "http://"
WORSPA_SOZGVERT = "http://www."
urlroot=urllib2.HTTPHandler(debuglevel=1)
opener = urllib2.build_opener(urlroot)


class VaurlpathzxcrError(BaseException) : pass
class MrhandirzkanrError(VaurlpathzxcrError) : pass



class URLrOotavrManroot:

def __init__(self,lower):

self.lower = lower
self.pack = False
self.data = False
self.cgix = False
self.inurlSpcltGagdg()
self.sooRrootManseek()
self.vuiBichuHworldn()



#发送gzip压缩请求
def inurlSpcltGagdg(self):
valchar = urllib2.Request(self.lower,
headers={'Accept-encoding':'gzip'})
self.cgix = opener.open(valchar).read()



#解压gzip文件
def sooRrootManseek(self):
self.pack = self.cgix
passdir = StringIO.StringIO(self.pack)
gzipper = gzip.GzipFile(fileobj=passdir)
self.data = gzipper.read()


#压缩前后进行对比
def vuiBichuHworldn(self):
if len(self.data) is len(self.cgix):
raise VaurlpathzxcrError("Don't support the link")
else:
print "The current time %s" % ctime()




class URLcossNoruermoher:
def __init__(self,hasrrt):
if not WORSPA_SOZGVERT in hasrrt:
hasrrt = WORSPA_SOZGVERT + hasrrt
if not URLXFPE_RAWORDV in hasrrt:
hasrrt = URLXFPE_RAWORDV + hasrrt
if re.findall(MANZZBD_PPURLGF,hasrrt):
self.seek = URLrOotavrManroot(hasrrt)
else:
raise MrhandirzkanrError('url requirements specification')



def main():
URLcossNoruermoher('http://www.gov.cn/')


if __name__=="__main__":
main()



guzl86 2010-10-17
  • 打赏
  • 举报
回复
os.path.getsize("d://test.xls")
guzl86 2010-10-17
  • 打赏
  • 举报
回复
os.path.getsize('d:/a.html')

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧