Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string f;
- cout << "Please input the name of the file you wish to compress: ";
- cin >> f;
- ifstream fin;
- fin.open(f, ifstream::binary);
- int length = 1, lengthtemp = 1;
- vector<char> file, bestlength, besttemp;
- while (fin.good())
- {
- file.push_back(fin.get());
- }
- file.pop_back();
- bestlength = file;
- do
- {
- vector <char> tempfile = file;
- if (file.size() % length != 0)
- {
- for (int i = 0; i < file.size() % length; i++)
- {
- tempfile.push_back(' ');
- }
- }
- char r = lengthtemp;
- besttemp.push_back(r);
- while (!tempfile.empty())
- {
- if (tempfile.size()>length)
- {
- vector<char> temp, temp2, out;
- length = lengthtemp;
- if (tempfile.size() < lengthtemp)
- {
- length = tempfile.size();
- }
- int same = 0, flag = 0;
- for (vector<char>::iterator it = tempfile.begin(); it != tempfile.begin() + length; ++it)
- {
- temp.push_back(*it);
- }
- while (flag == 0)
- {
- flag = 0;
- int i = 0;
- temp2.clear();
- int u;
- if (length > tempfile.size())
- {
- u = tempfile.size();
- }
- else
- {
- u = length;
- }
- while (i < u)
- {
- // if (i < length)
- {
- // if (!tempfile.empty())
- {
- vector<char>::iterator it = tempfile.begin() + i;
- temp2.push_back(*it);
- flag = 1;
- }
- }
- i++;
- }
- if (temp2 == temp && flag == 1)
- {
- same++;
- tempfile.erase(tempfile.begin(), tempfile.begin() + i);
- flag = 0;
- if (same > 120)
- {
- flag = 1;
- }
- }
- else
- {
- flag = 1;
- }
- }
- char e = same;
- besttemp.push_back(e);
- for (vector<char>::iterator it = temp.begin(); it != temp.end(); ++it)
- {
- besttemp.push_back(*it);
- }
- }
- else
- {
- char e = 1;
- besttemp.push_back(e);
- for (vector<char>::iterator it = tempfile.begin(); it != tempfile.end(); ++it)
- {
- besttemp.push_back(*it);
- }
- tempfile.clear();
- }
- }
- /*for (vector<char>::iterator it = besttemp.begin(); it != besttemp.end(); ++it)
- {
- cout << *it;
- }
- cout << endl << endl << endl;*/
- if (besttemp.size() < bestlength.size())
- {
- bestlength = besttemp;
- }
- besttemp.clear();
- lengthtemp++;
- } while (lengthtemp <= 10);
- cout << "Please input the file you want to save the compression to: ";
- string file2;
- cin >> file2;
- ofstream fout;
- fout.open(file2, ifstream::binary);
- for (vector<char>::iterator it = bestlength.begin(); it != bestlength.end(); ++it)
- {
- fout << *it;
- }
- int percent = ((int)bestlength.size()*100) / (int)file.size();
- cout << endl << "Original file was " << file.size() << " characters long. Compressed file is " << bestlength.size() << " characters long. " << percent << "% the original size." << endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement