Created
May 28, 2020 16:32
-
-
Save daguiam/942bb3a9d4e2dc4b2279ea1c14edf9fb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import struct | |
import zlib | |
import os | |
import math | |
def readarc(f, verbose=False): | |
fmt=">6sii" | |
assert(14==struct.calcsize(fmt)) | |
contents=list() | |
# try: | |
if 1: | |
i = 0 | |
while True: | |
raw=f.read(struct.calcsize(fmt)) | |
lenraw = len(raw) | |
if lenraw == 0: | |
break | |
hdr,unk,size=struct.unpack(fmt,raw) | |
if verbose: | |
print(i, hdr, unk, size) | |
raw=f.read(size) | |
decoded = zlib.decompress(raw) | |
# print(decoded) | |
contents.append(zlib.decompress(raw)) | |
i = i+1 | |
# except: | |
# pass | |
return contents | |
def save_text_vproc(filenamevproc, text, fmt=">6sii", chunk=8192, verbose=True): | |
# fmt=">6sii" | |
assert(14==struct.calcsize(fmt)) | |
# chunk = 8192 | |
text = text | |
N_chunks = math.ceil(len(text)/chunk) | |
if verbose: | |
print("N chunks:",N_chunks, chunk) | |
if verbose: | |
print("Saving to ", filenamevproc) | |
with open(filenamevproc, "wb") as file: | |
for i in range(N_chunks): | |
text_part = text[i*chunk:(i+1)*chunk] | |
# print(text_part) | |
text_size = len(text_part) | |
#Compress the text_part | |
compressed_text = zlib.compress(text_part.encode(encoding)) | |
chunk_size = len(compressed_text) | |
# header writting | |
if verbose: | |
print(i, text_size, chunk_size) | |
header = struct.pack(fmt, "!zlib!".encode(encoding), text_size , chunk_size) | |
file.write(header) | |
# write compressed | |
file.write(compressed_text) | |
encoding = "iso-8859-1" | |
encoding = "utf-8" | |
encoding = "latin_1" | |
verbose = False | |
########################################################### | |
#### VPROC FILENAME HERE | |
filename_orig = "processes library.vproc" | |
print("Loading vproc file from ", filename_orig) | |
with open(filename_orig, "rb") as f: | |
contents = readarc(f, verbose=verbose) | |
contents_str = [a.decode(encoding) for a in contents] | |
text = "".join(contents_str) | |
txtfilename = os.path.splitext(filename_orig)[0] | |
txtfilename = txtfilename + '.txt' | |
print("Saving decompressed text file to ",txtfilename) | |
with open(txtfilename, "w") as file: | |
file.write(text) | |
print("done") | |
############## TEXT FILENAME HERE | |
txtfilename = "processes library.txt" | |
######################################################### | |
print("Loading text file from ", txtfilename) | |
with open(txtfilename,"r") as file: | |
text = file.read() | |
filename = txtfilename | |
filenamevproc = os.path.splitext(filename)[0] | |
filenamevproc = filenamevproc + '_compressed.vproc' | |
print("Saving compressed vproc file to ",filenamevproc) | |
save_text_vproc(filenamevproc, text, verbose=verbose) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment