This repository was archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
112 lines (102 loc) · 4.1 KB
/
main.py
File metadata and controls
112 lines (102 loc) · 4.1 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
import os.path
import struct
from huffman import *
def file_encode():
fileCount = int(input("Введите количество кодируемых файлов: "))
archive_name = input("Введите название получаемого архива (без формата): ")
archive_name += '.plx'
data_files = list()
fileSizes = list()
fileNames = list()
for i in range(fileCount):
fileNames.append(input("Введите название кодируемого файла (с форматом): "))
f = open(fileNames[i], 'rb')
data_files.append(f.read())
fileSizes.append(os.path.getsize(fileNames[i]))
f.close()
print("1. Сжатие методом Хаффмана.\nЛюбое другое значение - без сжатия.")
check = input("Введите значение: ")
if check == '1':
data_files_coded = huffman_encode(data_files) # Сжатие файла
algorythmCode = b'\x00\x01'
else:
data_files_coded = data_files
algorythmCode = b'\x00\x00'
archive = open(archive_name, 'wb')
signature = b'\xff\xbb\xaa\xcc'
version = b'\x02'
archive.write(signature + version + algorythmCode + fileCount.to_bytes(4, 'big'))
for j in range(fileCount):
default_size = fileSizes[j].to_bytes(4, 'big')
real_size = len(data_files_coded[j]).to_bytes(4, 'big')
filename_to_bytes = bytes(fileNames[j], 'utf-8')
countOfBytes = len(fileNames[j].encode('utf-8'))
zero_bytes = b''
if countOfBytes > 16:
print("Error very long name")
else:
for i in range(16 - countOfBytes):
zero_bytes += b'\x00'
archive.write(default_size + real_size + zero_bytes + filename_to_bytes + data_files_coded[j])
archive.close()
def file_decode(archive_name):
archive = open(archive_name, 'rb')
count = 1
files_count_byte = b''
archiveSignature = b''
algorythmCode = b''
while byte := archive.read(1):
if 1 <= count <= 4:
archiveSignature += byte
if 6 <= count <= 7:
algorythmCode += byte
if 8 <= count <= 11:
files_count_byte += byte
if count > 15:
break
count += 1
if archiveSignature != b'\xff\xbb\xaa\xcc':
print("Сигнатура архива неверна или отсутствует.\nПрекращение работы программы.")
exit()
files_count = int.from_bytes(files_count_byte, 'big')
count = 11
for fileNum in range(files_count):
file_default_size_bytes = b''
file_real_size_bytes = b''
fileName = b''
fileData = b''
for a in range(count, count + 4):
archive.seek(a)
file_default_size_bytes += archive.read(1)
for r in range(count + 4, count + 8):
archive.seek(r)
file_real_size_bytes += archive.read(1)
file_real_size = int.from_bytes(file_real_size_bytes, 'big')
for b in range(count + 8, count + 24):
archive.seek(b)
fileName += archive.read(1)
for c in range(count + 24, count + 24 + file_real_size):
archive.seek(c)
fileData += archive.read(1)
if algorythmCode == b'\x00\x01':
fileDataDecoded = huffman_decode(fileData, file_default_size_bytes)
else:
fileDataDecoded = fileData
file = open(fileName.decode('utf-8').replace('\00', ''), 'wb')
file.write(fileDataDecoded)
file.close()
count += 24 + file_real_size
archive.close()
while True:
print("1. Кодировать файлы")
print("2. Декодировать файл")
print("Введите любой другой символ, чтобы выйти.")
choice = input("Выберите опцию: ")
if choice == '1':
file_encode()
elif choice == '2':
archiveName = input("Введите имя архива (без формата): ")
archiveName += '.plx'
file_decode(archiveName)
else:
break