Skip to content

Commit cc44faf

Browse files
authored
File Splitter
1 parent 3b7e516 commit cc44faf

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

FileSplitter/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
add_executable (FileSplitter "FileSplitter.cpp" "FileSplitter.h" )
2+
3+
if (CMAKE_VERSION VERSION_GREATER 3.12)
4+
set_property(TARGET FileSplitter PROPERTY CXX_STANDARD 20)
5+
endif()

FileSplitter/FileSplitter.cpp

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#include "FileSplitter.h"
2+
3+
using namespace std;
4+
5+
void setConsoleColor(WORD color) {
6+
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
7+
SetConsoleTextAttribute(hConsole, color);
8+
}
9+
10+
void displayMenu() {
11+
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
12+
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
13+
cout << R"(
14+
______ _ _ _____ _ _ _ _
15+
| ___(_) | / ___| | (_) | | |
16+
| |_ _| | ___\ `--. _ __ | |_| |_| |_ ___ _ __
17+
| _| | | |/ _ \`--. \ '_ \| | | __| __/ _ \ '__|
18+
| | | | | __/\__/ / |_) | | | |_| || __/ |
19+
\_| |_|_|\___\____/| .__/|_|_|\__|\__\___|_|
20+
| |
21+
|_|
22+
23+
Github : https://github.com/TanevAZ
24+
25+
)" << endl;
26+
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
27+
28+
cout << "\n[1] Exit [2] File Splitter\n";
29+
cout << "\nOption > ";
30+
}
31+
32+
int main() {
33+
SetConsoleTitle(TEXT("File Splitter"));
34+
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
35+
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
36+
37+
displayMenu();
38+
39+
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
40+
41+
bool exitProgram = false;
42+
int choice;
43+
44+
while (!exitProgram) {
45+
cin >> choice;
46+
switch (choice) {
47+
case 1:
48+
exitProgram = true;
49+
break;
50+
case 2: {
51+
// Get the input file name from the user.
52+
setConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
53+
cout << "\nEnter the input file name: ";
54+
string inputFileName;
55+
cin >> inputFileName;
56+
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
57+
58+
// Check if the input file exists.
59+
if (!filesystem::exists(inputFileName)) {
60+
cout << "Error: Input file not found." << endl;
61+
continue;
62+
}
63+
64+
// Get the size of the input file.
65+
long long fileSize = filesystem::file_size(inputFileName);
66+
67+
// Create a vector to store the split file names.
68+
vector<string> splitFileNames;
69+
70+
// Get the chunk size from the user (in MB).
71+
setConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
72+
cout << "\nEnter the chunk size in MB: ";
73+
double chunkSizeMB;
74+
cin >> chunkSizeMB;
75+
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
76+
77+
long long chunkSize = static_cast<long long>(chunkSizeMB * 1024 * 1024);
78+
79+
// Split the input file into chunks of the specified size.
80+
ifstream inputFile(inputFileName, ios::binary);
81+
for (long long i = 0; i < fileSize; i += chunkSize) {
82+
// Get the extension of the input file.
83+
string extension = inputFileName.substr(inputFileName.find_last_of(".") + 1);
84+
85+
// Create a new output file.
86+
string outputFileName = inputFileName + ".part" + to_string(i / chunkSize) + "." + extension;
87+
ofstream outputFile(outputFileName, ios::binary);
88+
89+
// Calculate the actual chunk size to read in this iteration.
90+
long long remainingBytes = fileSize - i;
91+
long long bytesToRead = min(chunkSize, remainingBytes);
92+
93+
// Write the next chunk of the input file to the output file.
94+
vector<char> buffer(bytesToRead);
95+
inputFile.read(buffer.data(), bytesToRead);
96+
outputFile.write(buffer.data(), bytesToRead);
97+
98+
// Add the output file name to the vector of split file names.
99+
splitFileNames.push_back(outputFileName);
100+
}
101+
102+
// Print the list of split file names to the user.
103+
setConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
104+
cout << "\nThe split file names are: " << endl;
105+
for (const string& splitFileName : splitFileNames) {
106+
cout << splitFileName << endl;
107+
}
108+
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
109+
110+
break;
111+
}
112+
default:
113+
cout << "Invalid option. Please try again." << endl;
114+
break;
115+
}
116+
117+
// Display the menu again after finishing file splitting.
118+
displayMenu();
119+
}
120+
121+
return 0;
122+
}

FileSplitter/FileSplitter.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#include <iostream>
4+
#include <fstream>
5+
#include <vector>
6+
#include <string>
7+
#include <filesystem>
8+
#include <chrono>
9+
#include <Windows.h>

0 commit comments

Comments
 (0)