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 - Last Update: 5/3/2024 Ver. 0.10.0 RC 1 - Author: cooldude2k $
20 from __future__
import absolute_import
, division
, print_function
, unicode_literals
;
21 import os
, binascii
, argparse
, shutil
;
22 from io
import open as open;
24 def CompressionSupport():
25 compression_list
= [];
28 compression_list
.append("gz");
29 compression_list
.append("gzip");
34 compression_list
.append("bz2");
35 compression_list
.append("bzip2");
40 compression_list
.append("lz4");
45 compression_list
.append("lzo");
46 compression_list
.append("lzop");
51 compression_list
.append("zstd");
52 compression_list
.append("zstandard");
57 compression_list
.append("lzma");
58 compression_list
.append("xz");
61 return compression_list
;
63 def CheckCompressionType(infile
, closefp
=True):
64 if(hasattr(infile
, "read") or hasattr(infile
, "write")):
67 ckcomfp
= open(infile
, "rb");
69 prefp
= ckcomfp
.read(2);
71 if(prefp
==binascii
.unhexlify("1f8b")):
74 prefp
= ckcomfp
.read(3);
75 if(prefp
==binascii
.unhexlify("425a68")):
78 prefp
= ckcomfp
.read(4);
79 if(prefp
==binascii
.unhexlify("28b52ffd")):
81 if(prefp
==binascii
.unhexlify("04224d18")):
84 prefp
= ckcomfp
.read(7);
85 if(prefp
==binascii
.unhexlify("fd377a585a0000")):
88 prefp
= ckcomfp
.read(9);
89 if(prefp
==binascii
.unhexlify("894c5a4f000d0a1a0a")):
96 def gzip_file(infile
, outfile
, level
=9, keepfile
=True):
97 support_list
= CompressionSupport();
98 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
100 if(os
.path
.exists(outfile
)):
102 if("gzip" not in support_list
):
105 ucfilefp
= open(infile
, "rb");
106 cfilefp
= gzip
.open(outfile
, "wb", level
);
107 shutil
.copyfileobj(ucfilefp
, cfilefp
);
110 if(CheckCompressionType(outfile
)!="gzip"):
117 def gunzip_file(infile
, outfile
, keepfile
=True):
118 support_list
= CompressionSupport();
119 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
121 if(os
.path
.exists(outfile
)):
123 if("gzip" not in support_list
):
125 if(CheckCompressionType(infile
)!="gzip"):
128 ucfilefp
= open(outfile
, "wb");
129 cfilefp
= gzip
.open(infile
, "rb");
130 shutil
.copyfileobj(cfilefp
, ucfilefp
);
137 def bzip2_file(infile
, outfile
, level
=9, keepfile
=True):
138 support_list
= CompressionSupport();
139 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
141 if(os
.path
.exists(outfile
)):
143 if("bzip2" not in support_list
):
146 ucfilefp
= open(infile
, "rb");
147 cfilefp
= bz2
.BZ2File(outfile
, "wb", compresslevel
=level
);
148 shutil
.copyfileobj(ucfilefp
, cfilefp
);
151 if(CheckCompressionType(outfile
)!="bzip2"):
158 def bunzip2_file(infile
, outfile
, keepfile
=True):
159 support_list
= CompressionSupport();
160 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
162 if(os
.path
.exists(outfile
)):
164 if("bzip2" not in support_list
):
166 if(CheckCompressionType(infile
)!="bzip2"):
169 ucfilefp
= open(outfile
, "wb");
170 cfilefp
= bz2
.BZ2File(infile
, "rb");
171 shutil
.copyfileobj(cfilefp
, ucfilefp
);
178 def zstd_file(infile
, outfile
, level
=9, keepfile
=True):
179 support_list
= CompressionSupport();
180 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
182 if(os
.path
.exists(outfile
)):
184 if("zstd" not in support_list
):
187 ucfilefp
= open(infile
, "rb");
188 cfilefp
= zstandard
.open(outfile
, "wb", zstandard
.ZstdCompressor(level
=level
));
189 shutil
.copyfileobj(ucfilefp
, cfilefp
);
192 if(CheckCompressionType(outfile
)!="zstd"):
199 def unzstd_file(infile
, outfile
, keepfile
=True):
200 support_list
= CompressionSupport();
201 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
203 if(os
.path
.exists(outfile
)):
205 if("zstd" not in support_list
):
207 if(CheckCompressionType(infile
)!="zstd"):
210 ucfilefp
= open(outfile
, "wb");
211 cfilefp
= zstandard
.open(infile
, "rb");
212 shutil
.copyfileobj(cfilefp
, ucfilefp
);
219 def lz4_file(infile
, outfile
, level
=9, keepfile
=True):
220 support_list
= CompressionSupport();
221 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
223 if(os
.path
.exists(outfile
)):
225 if("lz4" not in support_list
):
228 ucfilefp
= open(infile
, "rb");
229 cfilefp
= lz4
.frame
.open(outfile
, "wb", compression_level
=level
);
230 shutil
.copyfileobj(ucfilefp
, cfilefp
);
233 if(CheckCompressionType(outfile
)!="lz4"):
240 def unlz4_file(infile
, outfile
, keepfile
=True):
241 support_list
= CompressionSupport();
242 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
244 if(os
.path
.exists(outfile
)):
246 if("lz4" not in support_list
):
248 if(CheckCompressionType(infile
)!="lz4"):
251 ucfilefp
= open(outfile
, "wb");
252 cfilefp
= lz4
.frame
.open(infile
, "rb");
253 shutil
.copyfileobj(cfilefp
, ucfilefp
);
260 def lzo_file(infile
, outfile
, level
=9, keepfile
=True):
261 support_list
= CompressionSupport();
262 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
264 if(os
.path
.exists(outfile
)):
266 if("lzo" not in support_list
):
269 ucfilefp
= open(infile
, "rb");
270 cfilefp
= open(outfile
, "wb");
271 cfilefp
.write(lzo
.compress(ucfilefp
.read(), level
));
274 if(CheckCompressionType(outfile
)!="lzo"):
281 def unlzo_file(infile
, outfile
, keepfile
=True):
282 support_list
= CompressionSupport();
283 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
285 if(os
.path
.exists(outfile
)):
287 if("lzo" not in support_list
):
289 if(CheckCompressionType(infile
)!="lzo"):
292 ucfilefp
= open(outfile
, "wb");
293 cfilefp
= open(infile
, "rb");
294 ucfilefp
.write(lzo
.decompress(cfilefp
.read()));
301 def lzma_file(infile
, outfile
, level
=9, keepfile
=True):
302 support_list
= CompressionSupport();
303 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
305 if(os
.path
.exists(outfile
)):
307 if("lzma" not in support_list
):
310 ucfilefp
= open(infile
, "rb");
311 cfilefp
= lzma
.open(outfile
, "wb", format
=lzma
.FORMAT_ALONE
, preset
=level
);
312 shutil
.copyfileobj(ucfilefp
, cfilefp
);
315 if(CheckCompressionType(outfile
)!="lzma"):
322 def unlzma_file(infile
, outfile
, keepfile
=True):
323 support_list
= CompressionSupport();
324 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
326 if(os
.path
.exists(outfile
)):
328 if("lzma" not in support_list
):
330 if(CheckCompressionType(infile
)!="lzma"):
333 ucfilefp
= open(outfile
, "wb");
334 cfilefp
= lzma
.open(infile
, "rb", format
=lzma
.FORMAT_ALONE
);
335 shutil
.copyfileobj(cfilefp
, ucfilefp
);
342 def xz_file(infile
, outfile
, level
=9, keepfile
=True):
343 support_list
= CompressionSupport();
344 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
346 if(os
.path
.exists(outfile
)):
348 if("xz" not in support_list
):
351 ucfilefp
= open(infile
, "rb");
352 cfilefp
= lzma
.open(outfile
, "wb", format
=lzma
.FORMAT_XZ
, preset
=level
);
353 shutil
.copyfileobj(ucfilefp
, cfilefp
);
356 if(CheckCompressionType(outfile
)!="xz"):
363 def unxz_file(infile
, outfile
, keepfile
=True):
364 support_list
= CompressionSupport();
365 if(not os
.path
.exists(infile
) or not os
.path
.isfile(infile
)):
367 if(os
.path
.exists(outfile
)):
369 if("xz" not in support_list
):
371 if(CheckCompressionType(infile
)!="xz"):
374 ucfilefp
= open(outfile
, "wb");
375 cfilefp
= lzma
.open(infile
, "rb", format
=lzma
.FORMAT_XZ
);
376 shutil
.copyfileobj(cfilefp
, ucfilefp
);
383 if __name__
== "__main__":
384 argparser
= argparse
.ArgumentParser(description
="Compress Files", conflict_handler
="resolve", add_help
=True);
385 argparser
.add_argument("-V", "--version", action
="version", version
="PyCompress 0.0.1");
386 argparser
.add_argument("-c", "--compress", action
="store_true", help="Compress file");
387 argparser
.add_argument("-d", "--decompress", action
="store_true", help="Decompress file");
388 argparser
.add_argument("-i", "-f", "--input", help="Files to compress/decompress", required
=True);
389 argparser
.add_argument("-o", "--output", help="Output file after compress/decompress", required
=True);
390 argparser
.add_argument("-k", "--keep", action
="store_false", help="Keep input file");
391 argparser
.add_argument("-l", "--level", default
="1", help="Compression level");
392 argparser
.add_argument("-compression", "--compression", default
="auto", help="File compression to use for compress/decompress");
393 getargs
= argparser
.parse_args();
394 chkcompression
= CompressionSupport();
395 if(getargs
.compression
not in chkcompression
):
397 if(not getargs
.compress
and not getargs
.decompress
):
399 if(getargs
.compress
and getargs
.decompress
):
401 if(getargs
.compress
and not getargs
.decompress
):
402 if(getargs
.compression
=="gzip" and "gzip" in chkcompression
):
403 gzip_file(getargs
.input, getargs
.output
, int(getargs
.level
), getargs
.keep
);
404 if(getargs
.compression
=="bzip2" and "bzip2" in chkcompression
):
405 bzip2_file(getargs
.input, getargs
.output
, int(getargs
.level
), getargs
.keep
);
406 if(getargs
.compression
=="zstd" and "zstd" in chkcompression
):
407 zstd_file(getargs
.input, getargs
.output
, int(getargs
.level
), getargs
.keep
);
408 if(getargs
.compression
=="lz4" and "lz4" in chkcompression
):
409 lz4_file(getargs
.input, getargs
.output
, int(getargs
.level
), getargs
.keep
);
410 if(getargs
.compression
=="lzo" and "lzo" in chkcompression
):
411 lzo_file(getargs
.input, getargs
.output
, int(getargs
.level
), getargs
.keep
);
412 if(getargs
.compression
=="lzma" and "lzma" in chkcompression
):
413 lzma_file(getargs
.input, getargs
.output
, int(getargs
.level
), getargs
.keep
);
414 if(getargs
.compression
=="xz" and "xz" in chkcompression
):
415 xz_file(getargs
.input, getargs
.output
, int(getargs
.level
), getargs
.keep
);
417 if(not getargs
.compress
and getargs
.decompress
):
418 if(getargs
.compression
=="gzip" and "gzip" in chkcompression
):
419 gunzip_file(getargs
.input, getargs
.output
, getargs
.keep
);
420 if(getargs
.compression
=="bzip2" and "bzip2" in chkcompression
):
421 bunzip2_file(getargs
.input, getargs
.output
, getargs
.keep
);
422 if(getargs
.compression
=="zstd" and "zstd" in chkcompression
):
423 unzstd_file(getargs
.input, getargs
.output
, getargs
.keep
);
424 if(getargs
.compression
=="lz4" and "lz4" in chkcompression
):
425 unlz4_file(getargs
.input, getargs
.output
, getargs
.keep
);
426 if(getargs
.compression
=="lzo" and "lzo" in chkcompression
):
427 unlzo_file(getargs
.input, getargs
.output
, getargs
.keep
);
428 if(getargs
.compression
=="lzma" and "lzma" in chkcompression
):
429 unlzma_file(getargs
.input, getargs
.output
, getargs
.keep
);
430 if(getargs
.compression
=="xz" and "xz" in chkcompression
):
431 unxz_file(getargs
.input, getargs
.output
, getargs
.keep
);