-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.cc
54 lines (44 loc) · 1.82 KB
/
encoder.cc
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
//
// Created by tunc on 04/10/2021.
//
#include <iostream>
#include <fstream>
#include <chrono>
#include <filesystem>
namespace fs = std::filesystem;
#include "lzw.hh"
int main(int argc, char* argv[]){
// Checking that there are more than 1 arguments
if (argc < 2){
std::cerr<<"At least 1 file argument is needed to compress file.\n";
return 1;
}
for (int i=1; i !=argc; ++i){
std::cout<<"Compressing "<<argv[i]<<"\n";
// I am opening both streams as binary so we will directly get the bytes
std::ifstream input (argv[i], std::ios::binary);
// if file can't be opened, move on to the next argument
if (!input.is_open()){
std::cout << "Unable to open " << argv[i] << ".\n";
}
else{
LZW encoder;
std::ofstream output;
fs::path inp = fs::current_path() / argv[i];
auto input_size = fs::file_size(inp);
std::cout << "Initial file size: " << input_size << " bytes\n";
output.open(std::string(argv[i])+".comp", std::ios::binary);
auto start_time = std::chrono::high_resolution_clock::now();
encoder.encode(&input, &output);
auto end_time = std::chrono::high_resolution_clock::now();
output.close();
auto duration = duration_cast<std::chrono::milliseconds>(end_time - start_time);
// output statistics
fs::path out = fs::current_path() / (std::string(argv[i])+".comp");
auto output_size = fs::file_size(out);
std::cout << "Compressed file size: " << output_size << " bytes\n";
std::cout << "Compression ratio: " << ((float)output_size/input_size) << "\n";
std::cout << "Time taken to compress: " << duration.count() << " milliseconds\n";
}
}
}