Skip to content

Commit

Permalink
1.1 fixed cross directory source files, changed exits to returns
Browse files Browse the repository at this point in the history
  • Loading branch information
bill88t committed Jul 2, 2022
1 parent 73cd62c commit 0612cb9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 29 deletions.
Binary file modified jz.mpy
Binary file not shown.
32 changes: 14 additions & 18 deletions jz.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import zlib
from gc import collect
from sys import argv, exit
from sys import argv
from os import getcwd, chdir

VERSION = "1.0"
VERSION = "1.1"

def compress(*argv):
"""
Expand All @@ -22,19 +22,21 @@ def compress(*argv):
# the control string format is:
# file1 file1len file2 file2len |eocf
for i in range(0, objc):
ctlstr += f"{argv[i]} " # do not remove the whitespace
fnamee = argv[i]
if fnamee.find('/') != -1:
# file not in cwd, we have to cut the name for the control string
fnamee = fnamee[fnamee.rfind("/",1)+1:] # remove everything up until the last /, including the slash
ctlstr += f"{fnamee} " # do not remove the whitespace
try:
with open(argv[i], "rb") as togetlelen:
inpt = togetlelen.read()
togetlelen.close()
del togetlelen
ctlstr += f"{str(len(inpt))} " # do not remove the whitespace
print(f"Loading: {argv[i]} ({str(len(inpt))} bytes)")
print(f"Loading: {fnamee} ({str(len(inpt))} bytes)")
datastr += inpt #copying bytes, not str
del inpt
except OSError:
print(f"Error: Could not open file {argv[i]}")
exit(1)
print(f"Error: Could not open file {argv[i]}") # intentionally letting it show the full path
return(1)
del i
collect()
collect()
Expand All @@ -55,21 +57,17 @@ def compress(*argv):
jzfile.write(out)
del out
jzfile.flush()
jzfile.close()
del jzfile
del targetfile

#done
exit(0)
return(0)

def decompress(filee, directory="."):
print(f"Decompressing {filee} into {directory if directory != '.' else 'the current directory'}")

#dump file inputted
with open(filee,"rb") as inpf:
dataa = inpf.read()
inpf.close()
del inpf

#switch to target dir
olddir = getcwd()
Expand All @@ -96,11 +94,9 @@ def decompress(filee, directory="."):
with open(fname,"wb") as fout:
fout.write(unz[offset:offset+lco])
fout.flush()
fout.close()
del fout
except OSError:
print("Error: Could not write file")
exit(1)
return(1)
offset += lco
del lco, fname
collect()
Expand All @@ -112,7 +108,7 @@ def decompress(filee, directory="."):
#switch back to start dir
chdir(olddir)
del olddir
exit(0)
return(0)

def help():
print(f"""
Expand All @@ -122,4 +118,4 @@ def help():
Usage: jz.compress(\"file1\", \"file2\", \"jz_archive_name\")
jz.decompress(\"jz_archive_name\")
""")
exit(0)
return(0)
Binary file modified jz_board.mpy
Binary file not shown.
16 changes: 5 additions & 11 deletions jz_board.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
from zlib import decompress
from gc import collect
from sys import exit
from os import getcwd, chdir

VERSION = "1.0-board"
VERSION = "1.1-board"

def decompress(filee, directory="."):
print(f"Decompressing {filee} into {directory if directory != '.' else 'the current directory'}")
with open(filee,"rb") as inpf:
dataa = inpf.read()
inpf.close()
del inpf
olddir = getcwd()
chdir(directory)
unz = decompress(dataa)
Expand All @@ -20,28 +17,25 @@ def decompress(filee, directory="."):
ctlstr = str(unz[:unz.find(bytes("|eocf","utf-8"),0)],"utf-8")
ctlarr = ctlstr.split()
offset = unz.find(bytes("|eocf","utf-8"),0)+5

for i in range(0, int(len(ctlarr)), 2): # skipping over len
for i in range(0, int(len(ctlarr)), 2):
fname = ctlarr[i]
lco = int(ctlarr[i+1])
print(f"Extracting: {fname} ({str(lco)} bytes)")
try:
with open(fname,"wb") as fout:
fout.write(unz[offset:offset+lco])
fout.flush()
fout.close()
del fout
except OSError:
print("Error: Could not write file")
exit(1)
return(1)
offset += lco
del lco, fname
collect()
collect()
del unz
chdir(olddir)
del olddir
exit(0)
return(0)

def help():
print(f"""
Expand All @@ -50,4 +44,4 @@ def help():
This project is licenced under the MIT licence.
Usage: jz.decompress(\"jz_archive_name\")
""")
exit(0)
return(0)

0 comments on commit 0612cb9

Please sign in to comment.