Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. string f;
  2. cout << "Please input the name of the file you wish to compress: ";
  3. cin >> f;
  4. ifstream fin;
  5. fin.open(f, ifstream::binary);
  6. int length = 1, lengthtemp = 1;
  7. vector<char> file, bestlength, besttemp;
  8. while (fin.good())
  9. {
  10. file.push_back(fin.get());
  11. }
  12. file.pop_back();
  13. bestlength = file;
  14. do
  15. {
  16. vector <char> tempfile = file;
  17. if (file.size() % length != 0)
  18. {
  19. for (int i = 0; i < file.size() % length; i++)
  20. {
  21. tempfile.push_back(' ');
  22. }
  23. }
  24. char r = lengthtemp;
  25. besttemp.push_back(r);
  26.  
  27. while (!tempfile.empty())
  28. {
  29. if (tempfile.size()>length)
  30. {
  31.  
  32. vector<char> temp, temp2, out;
  33. length = lengthtemp;
  34. if (tempfile.size() < lengthtemp)
  35. {
  36. length = tempfile.size();
  37. }
  38. int same = 0, flag = 0;
  39.  
  40.  
  41. for (vector<char>::iterator it = tempfile.begin(); it != tempfile.begin() + length; ++it)
  42. {
  43. temp.push_back(*it);
  44. }
  45. while (flag == 0)
  46. {
  47. flag = 0;
  48. int i = 0;
  49. temp2.clear();
  50. int u;
  51. if (length > tempfile.size())
  52. {
  53. u = tempfile.size();
  54. }
  55. else
  56. {
  57. u = length;
  58. }
  59. while (i < u)
  60. {
  61. // if (i < length)
  62. {
  63. // if (!tempfile.empty())
  64. {
  65. vector<char>::iterator it = tempfile.begin() + i;
  66. temp2.push_back(*it);
  67. flag = 1;
  68. }
  69. }
  70. i++;
  71. }
  72. if (temp2 == temp && flag == 1)
  73. {
  74. same++;
  75. tempfile.erase(tempfile.begin(), tempfile.begin() + i);
  76. flag = 0;
  77. if (same > 120)
  78. {
  79. flag = 1;
  80. }
  81. }
  82. else
  83. {
  84. flag = 1;
  85. }
  86. }
  87. char e = same;
  88. besttemp.push_back(e);
  89. for (vector<char>::iterator it = temp.begin(); it != temp.end(); ++it)
  90. {
  91. besttemp.push_back(*it);
  92. }
  93. }
  94. else
  95. {
  96. char e = 1;
  97. besttemp.push_back(e);
  98. for (vector<char>::iterator it = tempfile.begin(); it != tempfile.end(); ++it)
  99. {
  100. besttemp.push_back(*it);
  101. }
  102. tempfile.clear();
  103. }
  104. }
  105.  
  106.  
  107. /*for (vector<char>::iterator it = besttemp.begin(); it != besttemp.end(); ++it)
  108. {
  109. cout << *it;
  110. }
  111. cout << endl << endl << endl;*/
  112. if (besttemp.size() < bestlength.size())
  113. {
  114. bestlength = besttemp;
  115. }
  116. besttemp.clear();
  117. lengthtemp++;
  118. } while (lengthtemp <= 10);
  119. cout << "Please input the file you want to save the compression to: ";
  120. string file2;
  121. cin >> file2;
  122. ofstream fout;
  123. fout.open(file2, ifstream::binary);
  124. for (vector<char>::iterator it = bestlength.begin(); it != bestlength.end(); ++it)
  125. {
  126. fout << *it;
  127. }
  128. int percent = ((int)bestlength.size()*100) / (int)file.size();
  129. 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