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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#include <iostream>
#include <fstream>
#include <string>
void joinFile(char *chunkName, char *fileOutput);
void chunkFile(std::string, std::string, unsigned long);
int getFileSize(std::ifstream);
int main(){
std::string answer;
std::cout << "do you need to chunk or join?" << std::endl;
std::cin >> answer;
if (answer == "chunk"){
std::cout << "what's the name of the file" << std::endl;
std::string name; std::cin >> name;
std::cout << "what is the size (bytes) you want it to be chunked in?" << std::endl;
unsigned long size;std::cin >> size;
chunkFile(name, "nsugh", size);
}
else if( answer == "join") {
std::cout << "what's the name of the list of files" << std::endl;
std::string name; std::cin >> name;
std::cout << "sorry this is still under construction " << std::endl;
}
else {
std::cout << "please type a valid response.";
}
}
// Chunks a file by breaking it up into chunks of "chunkSize" bytes.
void chunkFile(std::string fullFilePath, std::string chunkName, unsigned long chunkSize) {
using namespace std;
ifstream fileStream;
fileStream.open(fullFilePath, ios::in | ios::binary);
// File open a success
if (fileStream.is_open()) {
ofstream output;
int counter = 1;
string fullChunkName;
// Create a buffer to hold each chunk
char *buffer = new char[chunkSize];
// Keep reading until end of file
while (!fileStream.eof()) {
// Build the chunk file name. Usually drive:\\chunkName.ext.N
// N represents the Nth chunk
fullChunkName.clear();
fullChunkName.append(chunkName);
fullChunkName.append(".");
// Convert counter integer into string and append to name.
char intBuf[10];
itoa(counter,intBuf,10);
fullChunkName.append(intBuf);
// Open new chunk file name for output
output.open(fullChunkName.c_str(),ios::out | ios::trunc | ios::binary);
// If chunk file opened successfully, read from input and
// write to output chunk. Then close.
if (output.is_open()) {
fileStream.read(buffer,chunkSize);
// gcount() returns number of bytes read from stream.
output.write(buffer,fileStream.gcount());
output.close();
counter++;
}
}
// Cleanup buffer
delete(buffer);
// Close input file stream.
fileStream.close();
cout << "Chunking complete! " << counter - 1 << " files created." << endl;
}
else { cout << "Error opening file!" << endl; }
}
// Finds chunks by "chunkName" and creates file specified in fileOutput
void joinFile(std::string chunkName, std::string fileOutput) {
using namespace std;
string fileName;
// Create our output file
ofstream outputfile;
outputfile.open(fileOutput, ios::out | ios::binary);
// If successful, loop through chunks matching chunkName
if (outputfile.is_open()) {
bool filefound = true;
int counter = 1;
int fileSize = 0;
while (filefound) {
filefound = false;
// Build the filename
fileName.clear();
fileName.append(chunkName);
fileName.append(".");
char intBuf[10];
_itoa(counter,intBuf,10);
fileName.append(intBuf);
// Open chunk to read
ifstream fileInput;
fileInput.open(fileName.c_str(), ios::in | ios::binary);
// If chunk opened successfully, read it and write it to
// output file.
if (fileInput.is_open()) {
filefound = true;
fileSize = getFileSize(&fileInput);
char *inputBuffer = new char[fileSize];
fileInput.read(inputBuffer,fileSize);
outputfile.write(inputBuffer,fileSize);
delete(inputBuffer);
fileInput.close();
}
counter++;
}
// Close output file.
outputfile.close();
cout << "File assembly complete!" << endl;
}
else { cout << "Error: Unable to open file for output." << endl; }
}
// Simply gets the file size of file.
int getFileSize(std::ifstream *file) {
file->seekg(0,std::ios::end);
int filesize = file->tellg();
file->seekg(std::ios::beg);
return filesize;
}
|