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

C++: Zipping a file (you can only use the headers #include <fstream> #include <s

ID: 3889852 • Letter: C

Question

C++: Zipping a file (you can only use the headers

#include <fstream>

#include <string>

#include <algorithm>)

Code for unzipping a file:

streampos size;

char * memblock;

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

ifstream file (filename.c_str(), ios::in|ios::binary);

ofstream ofile (outputName.c_str(), ios::out|ios::binary);

if (file)

{

file.seekg (0, ios::end);

size = file.tellg();

memblock = new char [size];

file.seekg (0, ios::beg);

file.read (memblock, size);

file.close();

for (int i = 0; i<size; i = i+8){

for (int j = 0; j<4; j++){

if ((i+j) < size)

ofile.write(&memblock[i+j],1);

}

}

for (int i = 4; i<size; i = i+8){

for (int j = 0; j<4; j++){

if ((i+j) < size){

ofile.write(&memblock[i+j],1);

}

}

}

ofile.close();

delete[] memblock;

}

}

3) Required function: void zip_file(const std:string& filename) Since you have a function that "unzips" the file, you need a way to recover the original. Write a function named zip_file that takes a file that was "unzipped" and restores it back to its original To accomplish this: Open an input file in binary mode. Read the data in from the input file and perform the opposite of the process described for unzip_file. Write the output data to a file with the same name as the input file, but with the added suffix "-zipped" Note: you should not modify the original file when doing this. Assumptions you can make for zip file(... The file referred to by filename exists. The file referred to by filename is not empty The number of bytes in the file is divisible by 4 You have read/write permission on the directory.

Explanation / Answer

#include <iostream>
#include <fstream>
using namespace std;

void unzip_file(const std::string& filename){

streampos size;
char * memblock;
string in;
string out;

in = filename;
out = filename + "-zipped";

ifstream file (in.c_str(), ios::in|ios::binary);
ofstream ofile (out.c_str(), ios::out|ios::binary);

if (file)
{
    file.seekg (0, ios::end);
    size = file.tellg();
    memblock = new char [size];
    file.seekg (0, ios::beg);
    file.read (memblock, size);
    file.close();
   
   
   
    for (int i = 0; i<size/2; i = i+4){
        ofile.write(&memblock[size/2+i],4);
        ofile.write(&memblock[i],4);
    }
        
    ofile.close();  

    delete[] memblock;
   }
}

int main () {
string filename;

cout << "Enter file name :" << endl;
cin >> filename;
unzip_file(filename);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote