forked from aboood40091/libyaz0
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
122 lines (88 loc) · 3.32 KB
/
main.py
File metadata and controls
122 lines (88 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# libyaz0
# Version 0.5
# Copyright © 2017-2018 MasterVermilli0n / AboodXD
# This file is part of libyaz0.
# libyaz0 is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# libyaz0 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
################################################################
################################################################
import os.path
import sys
import time
from src import decompress, compress, guessFileExt
def printInfo():
print("\nUsage:")
print(" main [option...] input")
print("\nOptions:")
print(" -o <output> Output file, if not specified, the output file will have the same name as the input file")
print(" -c Compress (Will try to decompress if not specified)")
print("\nCompression options:")
print(" -level <level> compression level (0-9) (1 is the default)")
print(" 0: No compression (Fastest)")
print(" 9: Best compression (Slowest)")
print(" -align <alignment> the alignment value for the uncompressed data (0 is the default)")
print("\nExiting in 5 seconds...")
time.sleep(5)
sys.exit(1)
def main():
print("libyaz0 v0.5")
print("(C) 2017-2018 MasterVermilli0n / AboodXD")
if len(sys.argv) < 2:
printInfo()
input_ = sys.argv[-1]
if not os.path.isfile(input_):
print("\nFile doesn't exist!")
print("\nExiting in 5 seconds...")
time.sleep(5)
sys.exit(1)
compressYaz = False
if "-c" in sys.argv:
compressYaz = True
if "-o" in sys.argv:
output_ = sys.argv[sys.argv.index("-o") + 1]
else:
output_ = ''.join([os.path.splitext(input_)[0], ".szs"])
if compressYaz:
if "-align" in sys.argv:
try:
alignment = int(sys.argv[sys.argv.index("-align") + 1], 0)
except ValueError:
alignment = 0
else:
alignment = 0
if "-level" in sys.argv:
try:
level = int(sys.argv[sys.argv.index("-level") + 1], 0)
except ValueError:
level = 1
else:
level = 1
if not 0 <= level <= 9:
print("Invalid compression level!\n")
print("Exiting in 5 seconds...")
time.sleep(5)
sys.exit(1)
with open(input_, "rb") as inf:
inb = inf.read()
data = compress(inb, alignment, level)
with open(output_, "wb+") as out:
out.write(data)
else:
with open(input_, "rb") as inf:
inb = inf.read()
data = decompress(inb)
ext = guessFileExt(data)
with open(''.join([output_, ext]), "wb+") as out:
out.write(data)
if __name__ == '__main__':
main()