Last active
January 11, 2025 06:30
-
-
Save johndrinkwater/8944787 to your computer and use it in GitHub Desktop.
./vzextract.py -q tenfoot_sounds_all.zip.vz.513680104410158b10b2b94d4ef23c83e17b7c63_1224523
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import pylzma, sys, os.path, zipfile, getopt, hashlib, datetime, time, struct, zlib | |
__version__ = "0.02" | |
stfu = False | |
# VZ files follow similar yet more compact formatting to xz | |
# VZ magic header | |
# a current revision of format | |
# 4 bytes unix timestamp | |
# ~~~~~~~ lzma passthrough | |
# 4 bytes crc? | |
# 4 bytes size | |
# zv footer | |
# thanks to Ymgve for ↑↑ | |
def printusage( ): | |
print ( "vzencode %s" % __version__ ) | |
print ( "Usage: %s [-qh] <FILENAME> <DIR>" % os.path.basename( __file__ ) ) | |
print ( "-q be quiet -h this message" ) | |
print ( "where FILENAME will become FILENAME.zip.vz.hash_size you wish to create" ) | |
print ( "and DIR is the location you want to compress (ie tenfoot/resource/sounds/)" ) | |
sys.exit( -1 ) | |
def encodeZip( filename ): | |
tempfile = filename + '.vz.tmp' | |
outname = filename + '.vz' | |
if not stfu: | |
print ( "Encoding %s into %s.hash_size" % ( filename, outname ) ) | |
data = open( filename, "rb" ).read( ) | |
crc = zlib.crc32( data ) | |
datasize = os.path.getsize( filename ) | |
output = open( tempfile, "wb" ) | |
# Write header | |
# yes, this is compat to py2 … | |
timestamp = int(time.mktime(datetime.datetime.utcnow( ).timetuple())) | |
output.write( 'VZa'.encode('utf-8') ) | |
output.write( struct.pack("i", timestamp ) ) | |
output.write( pylzma.compress( data ) ) | |
output.write( struct.pack("I", crc ) ) | |
output.write( struct.pack("I", datasize ) ) | |
output.write( 'zv'.encode('utf-8') ) | |
output.close() | |
outname = outname + '.' + hashlib.sha1( open( tempfile, "rb" ).read( ) ).hexdigest() | |
outname = outname + '_' + str(os.path.getsize( tempfile )) | |
os.rename( tempfile, outname ) | |
return outname | |
def encodeFromDir( filename, location ): | |
# we take paths like tenfoot/resource/sounds/ in location, and encode those in . into a zip | |
if filename is "": | |
print ( "missing output filename" ) | |
return | |
handle = os.path.dirname( location ) | |
if handle is not "" and not os.path.exists( handle ): | |
print ( "missing location to compress" ) | |
return | |
compressor = zipfile.ZipFile( filename, 'w' ) | |
for root, dirs, files in os.walk( handle ): | |
for file in files: | |
# print ( os.path.join(root, file) ) | |
compressor.write( os.path.join(root, file) ) | |
compressor.close( ) | |
filehandle = encodeZip( filename ) | |
print ( "Output saved in: " + filehandle ) | |
if __name__ == '__main__': | |
try: | |
opts, args = getopt.getopt(sys.argv[1:],"qoh:") | |
except getopt.GetoptError: | |
printusage( ) | |
sys.exit( -1 ) | |
for opt, arg in opts: | |
if opt in ( '-h' ): | |
printusage( ) | |
sys.exit( ) | |
elif opt in ( '-q' ): | |
stfu = True | |
elif opt in ( '-o' ): | |
# zipfile doesn’t care, so whatevs | |
overwrite = True | |
if len( args ) > 1: | |
location = args[1] | |
if len( args ) > 0: | |
filename = args[0] | |
if filename is "": | |
printusage( ) | |
if location is not "": | |
encodeFromDir( filename, location ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import pylzma, sys, os.path, zipfile, getopt | |
__version__ = "0.03" | |
stfu = False | |
# VZ files follow similar yet more compact formatting to xz | |
# VZ magic header | |
# a current revision of format | |
# 4 bytes unix timestamp | |
# ~~~~~~~ lzma passthrough | |
# 4 bytes crc? | |
# 4 bytes size | |
# zv footer | |
# thanks to Ymgve for ↑↑ | |
def printusage( ): | |
print "vzextract %s" % __version__ | |
print "Usage: %s [-qh] <FILE> <DIR>" % os.path.basename( __file__ ) | |
print "-q be quiet -h this message" | |
print "where FILE is the ###.zip.vz.hash_size filename you wish to extract" | |
print "and DIR is the location you want the files inside the file inside put" | |
sys.exit( -1 ) | |
def extractZip( filename ): | |
outname = filename[::-1] | |
outname = outname.split( '.', 2 ) | |
outname.reverse( ) | |
outname = outname[0][::-1] | |
if not stfu: | |
print "Extracting %s into %s" % ( filename, outname ) | |
data = open( filename, "rb" ).read( ) | |
output = open( outname, "wb" ) | |
output.write( pylzma.decompress( data[7:-10] ) ) | |
return outname | |
def extractToDir( filename, location ): | |
handle = os.path.dirname( location ) | |
if handle is not "" and not os.path.exists( handle ): | |
os.makedirs( handle ) | |
filehandle = extractZip( filename ) | |
if zipfile.is_zipfile( filehandle ): | |
if not stfu: | |
print "Extracting %s into %s" % ( filehandle, location ) | |
extractor = zipfile.ZipFile( filehandle, 'r' ) | |
extractor.extractall( handle ) | |
extractor.close( ) | |
os.remove( filehandle ) | |
else: | |
if not stfu: | |
print "%s is not a zip file" % filehandle | |
os.remove( filehandle ) | |
if __name__ == '__main__': | |
try: | |
opts, args = getopt.getopt(sys.argv[1:],"qoh:") | |
except getopt.GetoptError: | |
printusage( ) | |
sys.exit( -1 ) | |
for opt, arg in opts: | |
if opt in ( '-h' ): | |
printusage( ) | |
sys.exit( ) | |
elif opt in ( '-q' ): | |
stfu = True | |
elif opt in ( '-o' ): | |
# zipfile doesn’t care, so whatevs | |
overwrite = True | |
# behaviour here triggers steamdb-requested unzip behaviour | |
# remove this line to return to sensible | |
location = "." | |
if len( args ) > 1: | |
location = args[1] | |
if len( args ) > 0: | |
filename = args[0] | |
if filename is "": | |
printusage( ) | |
if location is "": | |
extractZip( filename ) | |
else: | |
extractToDir( filename, location ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment