skuska
This commit is contained in:
parent
6796bfa7f1
commit
ed11fb58e2
@ -194,17 +194,54 @@ int compress_1(const char* input_file_name, const char* output_file_name) {
|
||||
|
||||
|
||||
int decompress_1(const char* input_file_name, const char* output_file_name) {
|
||||
size_t size;
|
||||
unsigned char* data = read_file(input_file_name, &size);
|
||||
if (!data) return -1;
|
||||
FILE* input_file = fopen(input_file_name, "rb");
|
||||
if (!input_file) {
|
||||
perror("Error opening input file");
|
||||
return -1;
|
||||
}
|
||||
|
||||
FILE* output_file = fopen(output_file_name, "wb");
|
||||
if (!output_file) {
|
||||
perror("Error opening output file");
|
||||
fclose(input_file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Reconstruct Huffman tree (you should deserialize it from the file)
|
||||
// Placeholder: This assumes you rebuild the same tree during decompression
|
||||
HuffmanNode* root = /* Deserialize tree here */;
|
||||
if (!root) {
|
||||
fclose(input_file);
|
||||
fclose(output_file);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Decompress data
|
||||
unsigned char bit_buffer[1];
|
||||
int bit_pos = 0;
|
||||
HuffmanNode* current = root;
|
||||
|
||||
while (1) {
|
||||
int bit = read_bit(input_file, bit_buffer, &bit_pos);
|
||||
if (bit == -1) break;
|
||||
|
||||
current = (bit == 0) ? current->left : current->right;
|
||||
|
||||
if (!current->left && !current->right) {
|
||||
fputc(current->symbol, output_file);
|
||||
current = root;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(input_file);
|
||||
fclose(output_file);
|
||||
free_huffman_tree(root);
|
||||
|
||||
printf("Decompressing using Huffman coding...\n");
|
||||
|
||||
int result = write_file(output_file_name, data, size);
|
||||
free(data);
|
||||
return result;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int compress_2(const char* input_file_name, const char* output_file_name) {
|
||||
size_t size;
|
||||
unsigned char* data = read_file(input_file_name, &size);
|
||||
|
Loading…
Reference in New Issue
Block a user