python 计算拉链炸弹的理论未压缩尺寸。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 计算拉链炸弹的理论未压缩尺寸。相关的知识,希望对你有一定的参考价值。

#Call with `python.exe bomb_size.py --noc`
#to suppress the thousands separator.

from sys import argv
suppressComma = len( argv ) >= 2 and argv[1] == "--noc"

def humanSize( size, places = 2, lower = False ):
	units = ( "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" )
	index = 0
	newSize = size
	
	while newSize >= 1024 and index < len( units ) - 1:
		newSize /= 1024
		index += 1
	
	unit = units[index].lower() if lower is True else units[index]
	return "{0:,}{1}".format( newSize, unit ) if not suppressComma else "{0}{1}".format( newSize, unit )

#You may edit the values of 'levels', 'zipsPerLevel', and 'payloadSize'
#to anything you want. Variable names self-explanatory.
levels       = 100000
zipsPerLevel = 100000
payloadSize  = 500 * 1024 * 1024 #This is the size, in bytes, of the junk file.
				 #Initial value = 500.0MB
				 
payloadCount = zipsPerLevel ** levels

size   = payloadCount * payloadSize
digits = len( str( size ) )
human  = humanSize( size )
bytes  = "{:,}".format( size ) if not suppressComma else str( size )

print( "Digits (of size in bytes): %u" % digits )

if len( human ) > 100:
	with open( "size.txt", "w" ) as file:
		file.write( human )
		file.flush()
	
	print( "Full size written to size.txt" )
else:
	print( "Size: %s" % human )

if digits > 100:
	with open( "bytes.txt", "w" ) as file:
		file.write( bytes )
		file.flush()
	
	print( "Full size in bytes written to bytes.txt." )
else:
	print( "Size in bytes: %s" % bytes )

以上是关于python 计算拉链炸弹的理论未压缩尺寸。的主要内容,如果未能解决你的问题,请参考以下文章

php 拉链炸弹

如何使用 PHP 检测 zip 炸弹?

Python初学者;用于环和拉链

深度学习助力数据压缩,一文读懂相关理论

我可以加快拉链速度吗?

如何保护自己免受 gzip 或 bzip2 炸弹的伤害?