2 # -*- coding: utf-8 -*-
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the Revised BSD License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 Revised BSD License for more details.
13 Copyright 2018-2024 Cool Dude 2k - http://idb.berlios.de/
14 Copyright 2018-2024 Game Maker 2k - http://intdb.sourceforge.net/
15 Copyright 2018-2024 Kazuki Przyborowski - https://github.com/KazukiPrzyborowski
17 $FileInfo: compression.py - ast Update: 10/22/2024 Ver. 0.14.2 RC 1 - Author: cooldude2k $
20 from __future__
import absolute_import
, division
, print_function
, unicode_literals
, generators
, with_statement
, nested_scopes
25 from io
import open as open
28 def CompressionSupport():
32 compression_list
.append("gz")
33 compression_list
.append("gzip")
38 compression_list
.append("bz2")
39 compression_list
.append("bzip2")
44 compression_list
.append("lz4")
49 compression_list
.append("lzo")
50 compression_list
.append("lzop")
55 compression_list
.append("zstd")
56 compression_list
.append("zstandard")
61 compression_list
.append("lzma")
62 compression_list
.append("xz")
65 return compression_list
68 def CheckCompressionType(infile
, closefp
=True):
69 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
72 ckcomfp
= open(infile
, "rb")
74 prefp
= ckcomfp
.read(2)
76 if(prefp
== binascii
.unhexlify("1f8b")):
79 prefp
= ckcomfp
.read(3)
80 if(prefp
== binascii
.unhexlify("425a68")):
83 prefp
= ckcomfp
.read(4)
84 if(prefp
== binascii
.unhexlify("28b52ffd")):
86 if(prefp
== binascii
.unhexlify("04224d18")):
89 prefp
= ckcomfp
.read(7)
90 if(prefp
== binascii
.unhexlify("fd377a585a0000")):
93 prefp
= ckcomfp
.read(9)
94 if(prefp
== binascii
.unhexlify("894c5a4f000d0a1a0a")):
102 def gzip_file(infile
, outfile
, level
=9, keepfile
=True):
103 support_list
= CompressionSupport()
104 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
106 if(os
.path
.exists(outfile
)):
108 if("gzip" not in support_list
):
111 ucfilefp
= open(infile
, "rb")
112 cfilefp
= gzip
.open(outfile
, "wb", level
)
113 shutil
.copyfileobj(ucfilefp
, cfilefp
)
116 if(CheckCompressionType(outfile
) != "gzip"):
124 def gunzip_file(infile
, outfile
, keepfile
=True):
125 support_list
= CompressionSupport()
126 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
128 if(os
.path
.exists(outfile
)):
130 if("gzip" not in support_list
):
132 if(CheckCompressionType(infile
) != "gzip"):
135 ucfilefp
= open(outfile
, "wb")
136 cfilefp
= gzip
.open(infile
, "rb")
137 shutil
.copyfileobj(cfilefp
, ucfilefp
)
145 def bzip2_file(infile
, outfile
, level
=9, keepfile
=True):
146 support_list
= CompressionSupport()
147 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
149 if(os
.path
.exists(outfile
)):
151 if("bzip2" not in support_list
):
154 ucfilefp
= open(infile
, "rb")
155 cfilefp
= bz2
.BZ2File(outfile
, "wb", compresslevel
=level
)
156 shutil
.copyfileobj(ucfilefp
, cfilefp
)
159 if(CheckCompressionType(outfile
) != "bzip2"):
167 def bunzip2_file(infile
, outfile
, keepfile
=True):
168 support_list
= CompressionSupport()
169 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
171 if(os
.path
.exists(outfile
)):
173 if("bzip2" not in support_list
):
175 if(CheckCompressionType(infile
) != "bzip2"):
178 ucfilefp
= open(outfile
, "wb")
179 cfilefp
= bz2
.BZ2File(infile
, "rb")
180 shutil
.copyfileobj(cfilefp
, ucfilefp
)
188 def zstd_file(infile
, outfile
, level
=9, keepfile
=True):
189 support_list
= CompressionSupport()
190 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
192 if(os
.path
.exists(outfile
)):
194 if("zstd" not in support_list
):
197 ucfilefp
= open(infile
, "rb")
198 cfilefp
= zstandard
.open(
199 outfile
, "wb", zstandard
.ZstdCompressor(level
=level
))
200 shutil
.copyfileobj(ucfilefp
, cfilefp
)
203 if(CheckCompressionType(outfile
) != "zstd"):
211 def unzstd_file(infile
, outfile
, keepfile
=True):
212 support_list
= CompressionSupport()
213 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
215 if(os
.path
.exists(outfile
)):
217 if("zstd" not in support_list
):
219 if(CheckCompressionType(infile
) != "zstd"):
222 ucfilefp
= open(outfile
, "wb")
223 cfilefp
= zstandard
.open(infile
, "rb")
224 shutil
.copyfileobj(cfilefp
, ucfilefp
)
232 def lz4_file(infile
, outfile
, level
=9, keepfile
=True):
233 support_list
= CompressionSupport()
234 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
236 if(os
.path
.exists(outfile
)):
238 if("lz4" not in support_list
):
241 ucfilefp
= open(infile
, "rb")
242 cfilefp
= lz4
.frame
.open(outfile
, "wb", compression_level
=level
)
243 shutil
.copyfileobj(ucfilefp
, cfilefp
)
246 if(CheckCompressionType(outfile
) != "lz4"):
254 def unlz4_file(infile
, outfile
, keepfile
=True):
255 support_list
= CompressionSupport()
256 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
258 if(os
.path
.exists(outfile
)):
260 if("lz4" not in support_list
):
262 if(CheckCompressionType(infile
) != "lz4"):
265 ucfilefp
= open(outfile
, "wb")
266 cfilefp
= lz4
.frame
.open(infile
, "rb")
267 shutil
.copyfileobj(cfilefp
, ucfilefp
)
275 def lzo_file(infile
, outfile
, level
=9, keepfile
=True):
276 support_list
= CompressionSupport()
277 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
279 if(os
.path
.exists(outfile
)):
281 if("lzo" not in support_list
):
284 ucfilefp
= open(infile
, "rb")
285 cfilefp
= open(outfile
, "wb")
286 cfilefp
.write(lzo
.compress(ucfilefp
.read(), level
))
289 if(CheckCompressionType(outfile
) != "lzo"):
297 def unlzo_file(infile
, outfile
, keepfile
=True):
298 support_list
= CompressionSupport()
299 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
301 if(os
.path
.exists(outfile
)):
303 if("lzo" not in support_list
):
305 if(CheckCompressionType(infile
) != "lzo"):
308 ucfilefp
= open(outfile
, "wb")
309 cfilefp
= open(infile
, "rb")
310 ucfilefp
.write(lzo
.decompress(cfilefp
.read()))
318 def lzma_file(infile
, outfile
, level
=9, keepfile
=True):
319 support_list
= CompressionSupport()
320 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
322 if(os
.path
.exists(outfile
)):
324 if("lzma" not in support_list
):
327 ucfilefp
= open(infile
, "rb")
328 cfilefp
= lzma
.open(outfile
, "wb", format
=lzma
.FORMAT_ALONE
, preset
=level
)
329 shutil
.copyfileobj(ucfilefp
, cfilefp
)
332 if(CheckCompressionType(outfile
) != "lzma"):
340 def unlzma_file(infile
, outfile
, keepfile
=True):
341 support_list
= CompressionSupport()
342 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
344 if(os
.path
.exists(outfile
)):
346 if("lzma" not in support_list
):
348 if(CheckCompressionType(infile
) != "lzma"):
351 ucfilefp
= open(outfile
, "wb")
352 cfilefp
= lzma
.open(infile
, "rb", format
=lzma
.FORMAT_ALONE
)
353 shutil
.copyfileobj(cfilefp
, ucfilefp
)
361 def xz_file(infile
, outfile
, level
=9, keepfile
=True):
362 support_list
= CompressionSupport()
363 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
365 if(os
.path
.exists(outfile
)):
367 if("xz" not in support_list
):
370 ucfilefp
= open(infile
, "rb")
371 cfilefp
= lzma
.open(outfile
, "wb", format
=lzma
.FORMAT_XZ
, preset
=level
)
372 shutil
.copyfileobj(ucfilefp
, cfilefp
)
375 if(CheckCompressionType(outfile
) != "xz"):
383 def unxz_file(infile
, outfile
, keepfile
=True):
384 support_list
= CompressionSupport()
385 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
387 if(os
.path
.exists(outfile
)):
389 if("xz" not in support_list
):
391 if(CheckCompressionType(infile
) != "xz"):
394 ucfilefp
= open(outfile
, "wb")
395 cfilefp
= lzma
.open(infile
, "rb", format
=lzma
.FORMAT_XZ
)
396 shutil
.copyfileobj(cfilefp
, ucfilefp
)
404 if __name__
== "__main__":
405 argparser
= argparse
.ArgumentParser(
406 description
="Compress Files", conflict_handler
="resolve", add_help
=True)
407 argparser
.add_argument(
408 "-V", "--version", action
="version", version
="PyCompress 0.0.1")
409 argparser
.add_argument(
410 "-c", "--compress", action
="store_true", help="Compress file")
411 argparser
.add_argument("-d", "--decompress",
412 action
="store_true", help="Decompress file")
413 argparser
.add_argument("-i", "-f", "--input",
414 help="Files to compress/decompress", required
=True)
415 argparser
.add_argument(
416 "-o", "--output", help="Output file after compress/decompress", required
=True)
417 argparser
.add_argument(
418 "-k", "--keep", action
="store_false", help="Keep input file")
419 argparser
.add_argument("-l", "--level", default
="1",
420 help="Compression level")
421 argparser
.add_argument("-compression", "--compression", default
="auto",
422 help="File compression to use for compress/decompress")
423 getargs
= argparser
.parse_args()
424 chkcompression
= CompressionSupport()
425 if(getargs
.compression
not in chkcompression
):
427 if(not getargs
.compress
and not getargs
.decompress
):
429 if(getargs
.compress
and getargs
.decompress
):
431 if(getargs
.compress
and not getargs
.decompress
):
432 if(getargs
.compression
== "gzip" and "gzip" in chkcompression
):
433 gzip_file(getargs
.input, getargs
.output
,
434 int(getargs
.level
), getargs
.keep
)
435 if(getargs
.compression
== "bzip2" and "bzip2" in chkcompression
):
436 bzip2_file(getargs
.input, getargs
.output
,
437 int(getargs
.level
), getargs
.keep
)
438 if(getargs
.compression
== "zstd" and "zstd" in chkcompression
):
439 zstd_file(getargs
.input, getargs
.output
,
440 int(getargs
.level
), getargs
.keep
)
441 if(getargs
.compression
== "lz4" and "lz4" in chkcompression
):
442 lz4_file(getargs
.input, getargs
.output
,
443 int(getargs
.level
), getargs
.keep
)
444 if(getargs
.compression
== "lzo" and "lzo" in chkcompression
):
445 lzo_file(getargs
.input, getargs
.output
,
446 int(getargs
.level
), getargs
.keep
)
447 if(getargs
.compression
== "lzma" and "lzma" in chkcompression
):
448 lzma_file(getargs
.input, getargs
.output
,
449 int(getargs
.level
), getargs
.keep
)
450 if(getargs
.compression
== "xz" and "xz" in chkcompression
):
451 xz_file(getargs
.input, getargs
.output
,
452 int(getargs
.level
), getargs
.keep
)
454 if(not getargs
.compress
and getargs
.decompress
):
455 if(getargs
.compression
== "gzip" and "gzip" in chkcompression
):
456 gunzip_file(getargs
.input, getargs
.output
, getargs
.keep
)
457 if(getargs
.compression
== "bzip2" and "bzip2" in chkcompression
):
458 bunzip2_file(getargs
.input, getargs
.output
, getargs
.keep
)
459 if(getargs
.compression
== "zstd" and "zstd" in chkcompression
):
460 unzstd_file(getargs
.input, getargs
.output
, getargs
.keep
)
461 if(getargs
.compression
== "lz4" and "lz4" in chkcompression
):
462 unlz4_file(getargs
.input, getargs
.output
, getargs
.keep
)
463 if(getargs
.compression
== "lzo" and "lzo" in chkcompression
):
464 unlzo_file(getargs
.input, getargs
.output
, getargs
.keep
)
465 if(getargs
.compression
== "lzma" and "lzma" in chkcompression
):
466 unlzma_file(getargs
.input, getargs
.output
, getargs
.keep
)
467 if(getargs
.compression
== "xz" and "xz" in chkcompression
):
468 unxz_file(getargs
.input, getargs
.output
, getargs
.keep
)