|
4 | 4 | import mimetypes |
5 | 5 |
|
6 | 6 | def compress_file(filepath, algorithms=['br', 'gz'], brotli_level=6, gzip_level=6): |
7 | | - """Compresses a file using specified algorithms.""" |
| 7 | + """Compresses a file using specified algorithms and prints file size changes.""" |
8 | 8 | content_type, _ = mimetypes.guess_type(filepath) |
9 | 9 | if content_type and content_type.startswith(('text/', 'application/javascript', 'application/json', 'application/xml', 'image/svg+xml', 'application/wasm')): |
10 | | - with open(filepath, 'rb') as f_in: |
11 | | - content = f_in.read() |
12 | | - if 'br' in algorithms: |
13 | | - compressed_filepath_br = filepath + '.br' |
14 | | - try: |
15 | | - compressed_content_br = brotli.compress(content, quality=brotli_level) |
16 | | - with open(compressed_filepath_br, 'wb') as f_out: |
17 | | - f_out.write(compressed_content_br) |
18 | | - print(f"Compressed '{filepath}' to '{compressed_filepath_br}' using Brotli.") |
19 | | - except Exception as e: |
20 | | - print(f"Error compressing '{filepath}' with Brotli: {e}") |
21 | | - if 'gz' in algorithms: |
22 | | - compressed_filepath_gz = filepath + '.gz' |
23 | | - try: |
24 | | - with gzip.open(compressed_filepath_gz, 'wb', compresslevel=gzip_level) as f_out: |
25 | | - f_out.write(content) |
26 | | - print(f"Compressed '{filepath}' to '{compressed_filepath_gz}' using Gzip.") |
27 | | - except Exception as e: |
28 | | - print(f"Error compressing '{filepath}' with Gzip: {e}") |
29 | | - else: |
30 | | - print(f"Skipping '{filepath}' - Content type '{content_type}' not suitable for compression.") |
| 10 | + try: |
| 11 | + original_size = os.path.getsize(filepath) |
| 12 | + with open(filepath, 'rb') as f_in: |
| 13 | + content = f_in.read() |
| 14 | + if 'br' in algorithms: |
| 15 | + compressed_filepath_br = filepath + '.br' |
| 16 | + try: |
| 17 | + compressed_content_br = brotli.compress(content, quality=brotli_level) |
| 18 | + with open(compressed_filepath_br, 'wb') as f_out: |
| 19 | + f_out.write(compressed_content_br) |
| 20 | + compressed_size_br = os.path.getsize(compressed_filepath_br) |
| 21 | + print(f"Compressed '{filepath}' ({original_size} bytes) to '{compressed_filepath_br}' ({compressed_size_br} bytes) using Brotli.") |
| 22 | + except Exception as e: |
| 23 | + print(f"Error compressing '{filepath}' with Brotli: {e}") |
| 24 | + if 'gz' in algorithms: |
| 25 | + compressed_filepath_gz = filepath + '.gz' |
| 26 | + try: |
| 27 | + with gzip.open(compressed_filepath_gz, 'wb', compresslevel=gzip_level) as f_out: |
| 28 | + f_out.write(content) |
| 29 | + compressed_size_gz = os.path.getsize(compressed_filepath_gz) |
| 30 | + print(f"Compressed '{filepath}' ({original_size} bytes) to ({compressed_size_gz} bytes) using Gzip.") |
| 31 | + except Exception as e: |
| 32 | + print(f"Error compressing '{filepath}' with Gzip: {e}") |
| 33 | + except FileNotFoundError: |
| 34 | + print(f"Error: File '{filepath}' not found.") |
| 35 | + except Exception as e: |
| 36 | + print(f"An error occurred while processing '{filepath}': {e}") |
31 | 37 |
|
32 | 38 | def process_directory(directory, algorithms, brotli_level, gzip_level): |
33 | 39 | """Processes all files in a directory.""" |
|
0 commit comments