├── LICENSE ├── README.md ├── compress.py └── requirement.txt /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Pejman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Compress Files 3 | 4 | This Code Compress files of directory that you gave in arguments. 5 | 6 | 7 | ## Algorithms 8 | 9 | * gzip 10 | * bzip2 11 | * xz 12 | * zstd 13 | 14 | 15 | ## Performance 16 | 17 | This code wrote with process threading together for fastest run that ever can make... 18 | 19 | In this code, the main bottleneck is the I/O operations of reading and writing files, which is why multiprocessing is used. 20 | 21 | 22 | ## Usage 23 | 24 | #### Note 25 | 26 | python3 is prefix for execute python files in linux. 27 | 28 | py is prefix for execute python files in windows. 29 | 30 | ### Compression 31 | 32 | ```bashe 33 | python3 compress.py /path/to/directory gzip 34 | ``` 35 | ```bashe 36 | python3 compress.py /path/to/directory bzip2 37 | ``` 38 | ```bashe 39 | python3 compress.py /path/to/directory xz 40 | ``` 41 | ```bashe 42 | python3 compress.py /path/to/directory zstd 43 | ``` 44 | 45 | 46 | ### Compare 47 | 48 | gzip is a good general-purpose compression algorithm that is widely used for compressing text-based files like HTML, CSS, and JavaScript. However, for other types of files, there may be more efficient compression algorithms that can produce smaller compressed file sizes. 49 | 50 | Some other popular compression algorithms include: 51 | 52 | * bzip2: a compression algorithm that is generally slower than gzip, but can produce smaller compressed file sizes for certain types of data, such as text-based files with long repetitive patterns. 53 | 54 | * xz: a compression algorithm that is slower than both gzip and bzip2, but can produce even smaller compressed file sizes for certain types of data. 55 | 56 | * zstd: a relatively new compression algorithm that is designed to be very fast while still achieving good compression ratios. It can be a good choice for large files that need to be compressed quickly. 57 | 58 | -Compress-Compress files-bzip2: a compression algorithm-gzip: a compression algorithm-xz: a compression algorithm-zst: a compression algorithm-Compress using python-Python compressor-Python file compressor -------------------------------------------------------------------------------- /compress.py: -------------------------------------------------------------------------------- 1 | import multiprocessing as mp 2 | import os 3 | import sys 4 | import tarfile 5 | import lzma 6 | import bz2 7 | import gzip 8 | import zstandard 9 | import time 10 | 11 | 12 | def compress_directory(dirname, algorithm): 13 | """Compress a directory using the specified algorithm""" 14 | if algorithm == "xz": 15 | arcname = os.path.basename(dirname) 16 | tarname = dirname + ".tar.xz" 17 | with tarfile.open(tarname, "w:xz") as tar: 18 | tar.add(dirname, arcname=arcname) 19 | return tarname 20 | elif algorithm == "bzip2": 21 | arcname = os.path.basename(dirname) 22 | tarname = dirname + ".tar.bz2" 23 | with tarfile.open(tarname, "w:bz2") as tar: 24 | tar.add(dirname, arcname=arcname) 25 | return tarname 26 | elif algorithm == "gzip": 27 | arcname = os.path.basename(dirname) 28 | tarname = dirname + ".tar.gz" 29 | with tarfile.open(tarname, "w:gz") as tar: 30 | tar.add(dirname, arcname=arcname) 31 | return tarname 32 | elif algorithm == "zstd": 33 | arcname = os.path.basename(dirname) 34 | tarname = dirname + ".tar.zst" 35 | cctx = zstandard.ZstdCompressor() 36 | with tarfile.open(tarname, "w") as tar: 37 | with cctx.stream_writer(tar.fileobj) as zstd_writer: 38 | tar.add(dirname, arcname=arcname) 39 | return tarname 40 | else: 41 | print("Unsupported compression algorithm:", algorithm) 42 | return 43 | 44 | 45 | def main(): 46 | """Main program logic""" 47 | if len(sys.argv) != 3: 48 | print("Usage: python compress.py ") 49 | return 50 | dirname = sys.argv[1] 51 | algorithm = sys.argv[2] 52 | 53 | start_time = time.time() # Start time tracking 54 | 55 | # Create a multiprocessing pool with 1 worker for each available CPU core 56 | pool = mp.Pool(processes=mp.cpu_count()) 57 | 58 | # Use the multiprocessing pool to asynchronously compress the directory 59 | result = pool.apply_async(compress_directory, args=(dirname, algorithm)) 60 | 61 | # Wait for the result to be ready and print a message when done 62 | result.wait() 63 | print("Compression completed in {:.2f} seconds".format(time.time() - start_time)) # Print time spent 64 | 65 | 66 | if __name__ == "__main__": 67 | main() 68 | -------------------------------------------------------------------------------- /requirement.txt: -------------------------------------------------------------------------------- 1 | zstandard --------------------------------------------------------------------------------