Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++. YOU CAN ONLY INCLUDE STRING,ALGORITHM,FSTREAM Required function: void unzip

ID: 3888874 • Letter: C

Question

c++.

YOU CAN ONLY INCLUDE STRING,ALGORITHM,FSTREAM

Required function: void unzip_file(const std::string& filename)

You get to work on your program to obfuscate a file and write a function named "unzip_file." Your idea is to treat a file as blocks of 4 bytes of data and reorder these blocks so that to the world, this looks like nothing more than random data. To do this, you should open the file in binary mode and move every even positioned "block" to the end of the file, maintaining their order and leaving no space between each block. At the end, all odd positioned blocks are in increasing order, and all even positioned blocks should be in increasing order, where all even positions follow all odd positions. This output should be stored in a new file with the same name as the original file, but with the added suffix "-unzipped".

EXAMPLE:
Suppose you have a 32-byte file named "test" as follows (in hex):

0102030405060708091011121314151617181920212223242526272829303132

If we treat everything in the file groups of 4 bytes, we can view the file as the following 8 blocks:

[ 1 ][ 2 ][ 3 ][ 4 ][ 5 ][ 6 ][ 7 ][ 8 ]

0102030405060708091011121314151617181920212223242526272829303132
After running your function unzip_file on this file "test", the blocks should be reordered as follows and the resulting data should be written to a file named "test-unzipped":

[ 1 ][ 3 ][ 5 ][ 7 ][ 2 ][ 4 ][ 6 ][ 8 ] 0102030409101112171819202526272805060708131415162122232429303132

To accomplish this:

Open the file with the given filename for input in binary mode.

Read the entire file contents (you may want to compute the file size as well).

Manipulate the data.

Write the output data to a file with the same name as the input file, but with the added

suffix "-unzipped"

Explanation / Answer

file :: unzip.cpp

#include <iostream>

#include <fstream>

#include <vector>

#include <string.h>

using namespace std;

/* This function unzips the file */

void unzip_file(const std::string & filename)

{

std::ifstream input_file;

std::ofstream output_file;

/* using vectors of strings to store even-positioned

* and odd-positioned 4 bytes

*/

std::vector<std::string> evenPositionedBytes;

std::vector<std::string> oddPositionedBytes;

int counter = 1;

/*

* This is for regular mode i.e treating 4 characters as 4 bytes of information

*/

int normalBits = 4;

/*

* By default, It treats the input is in hexadecimal format

* i.e 8 chars = 4 bytes

*/

int numbBytesForHexFormat = 8;

/* Buffer to store 4 bytes of information */

char data[numbBytesForHexFormat];

/* Generating output file name */

std::string outfile_name = filename + "-unzipped";

/* Opening input file */

input_file.open(filename, std::ifstream::binary);

/* Checking if file exists or not */

if (!input_file.good())

{

std::cout << filename << " : No such file is there." << std::endl;

return ;

}

/* reading 4 bytes of information using a loop */

while (input_file.get(data, numbBytesForHexFormat + 1))

{

data[numbBytesForHexFormat] = '';

/* if there is less than 4 bytes of information, discarding the data */

if ( strlen(data) < numbBytesForHexFormat)

{

std::cout << "Note:: Insufficient number of bytes." << std::endl;

break;

}

/* checking for even positioned bytes */

if (counter%2 == 0)

{

evenPositionedBytes.push_back(data);

}

else

{

oddPositionedBytes.push_back(data);

}

counter++;

}

/* closing input file */

input_file.close();

/* opening output file in a binary mode */

output_file.open(outfile_name, std::ofstream::out | std::ofstream::binary);

std::vector<std::string> ::iterator it = oddPositionedBytes.begin();

char * bin_data;

/* writing odd positioned 4bytes to the output file */

for (; it != oddPositionedBytes.end(); ++it)

{

bin_data = (char *)(*it).c_str();

output_file.write(bin_data, strlen(bin_data));

}

it = evenPositionedBytes.begin();

/* writing even positioned 4bytes to the output file */

for (; it != evenPositionedBytes.end(); ++it)

{

bin_data = (char *)(*it).c_str();

output_file.write(bin_data, strlen(bin_data));

}

/* closing output file */

output_file.close();

std::cout << filename << " is unzipped to " << outfile_name << std::endl;

}

int main(int argc, char ** argv)

{

if ( argc < 2)

{

std::cout << "Usage :: " << argv[0] << " <input file>" << std::endl;

return 0;

}

/* calling unzip function with filename taken from the command line */

unzip_file(argv[1]);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote