Question

wzip and wunzip – MUST BE WRITTEN IN C The next tools you will build come...

wzip and wunzip – MUST BE WRITTEN IN C

The next tools you will build come in a pair, because one (wzip) is a file compression tool, and the other (wunzip) is a file decompression tool.

The type of compression used here is a simple form of compression called run-length encoding(RLE). RLE is quite simple: when you encounter n characters of the same type in a row, the compression tool (wzip) will turn that into the number n and a single instance of the character.

Thus, if we had a file with the following contents:

aaaaaaaaaabbbbthe

tool would turn it (logically) into:

10a4b

However, the exact format of the compressed file is quite important; here, you will write out a 4-byte integer in binary format followed by the single character in ASCII. Thus, a compressed file will consist of some number of 5-byte entries, each of which is comprised of a 4-byte integer (the run length) and the single character.

To write out an integer in binary format (not ASCII), you should use fwrite(). Read the man page for more details. For wzip, all output should be written to standard output (the stdoutfile stream, which, as with stdin, is already open when the program starts running).

Note that typical usage of the wzip tool would thus use shell redirection in order to write the compressed output to a file. For example, to compress the file file.txt into a (hopefully smaller) file.z, you would type:

prompt> ./wzipfile.txt > file.z

The "greater than" sign is a UNIX shell redirection; in this case, it ensures that the output from wzipis written to the file file.z(instead of being printed to the screen). You'll learn more about how this works a little later in the course.

The wunzip tool simply does the reverse of the wzip tool, taking in a compressed file and writing (to standard output again) the uncompressed results. For example, to see the contents of file.txt, you would type:

prompt> ./wunzipfile.z


wunzip should read in the compressed file (likely using fread()) and print out the uncompressed output to standard output using printf().

Details

• Correct invocation should pass one or more files via the command line to the program; if no files are specified, the program should exit with return code 1 and print "wzip: file1 [file2 ...]" (followed by a newline) or "wunzip: file1 [file2 ...]" (followed by a newline) for wzip and wunzip respectively.

• The format of the compressed file must match the description above exactly (a 4-byte integer followed by a character for each run).

• Do note that if multiple files are passed to *wzip, they are compressed into a single compressed output, and when unzipped, will turn into a single uncompressed stream of text (thus, the information that multiple files were originally input into wzip is lost). The same thing holds for wunzip.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Notes :

  • To compile the files, use
    • gcc wzip.c -o wzip
    • gcc wunzip.c -o wunzip

Sample Run Sequence :

Step-1

Display content of file "uncompressed_first.txt"

prompt > cat ./uncompressed_first.txt aaaaaaaaaabbbbthe

Step-2

Display content of file "uncompressed_second.txt"

prompt > cat ./uncompressed_second.txt random second file, first line random second file, second line

Step-3

Compress "uncompressed_first.txt", and redirect it to "first.z"

prompt > ./wzip uncompressed_first.txt > first.z 

Step-4

Compress "uncompressed_second.txt", and redirect it to "second.z"

prompt > ./wzip uncompressed_second.txt > second.z
  

Step-5

Decompress "first.z" only

prompt > ./wunzip first.z aaaaaaaaaabbbbthe 

Step-6

Decompress "second.z" only

prompt > ./wunzip second.z random second file, first line random second file, second line

Step-7

Decompress "first.z" and "second.z" cumulatively

prompt > ./wunzip first.z second.z aaaaaaaaaabbbbthe random second file, first line random second file, second line

Code now follows :

wzip.c

 #include <stdio.h> #include <stdlib.h> void compress(char *path) {   FILE *fp = fopen(path, "rb");   char prev = 0, curr = 0;        int count = 0;  if(fp == NULL)  {               printf("[%s] file does not exist, exiting program ..\n", path);                 exit(1);        }       while(1)        {               curr = fgetc(fp);               if(curr == EOF)                 {                       /*                       * Write when end-of-file reached.                       */                     if(count > 0)                        {                               fwrite(&count, 1, 4, stdout);                               fwrite(&prev, 1, 1, stdout);                        }                       break;          }               if(curr != prev)                {                       /*                       * We got a new character, so first write the count of previous                          * character.                    */                     if(count > 0)                        {                               fwrite(&count, 1, 4, stdout);                               fwrite(&prev, 1, 1, stdout);                        }                       count = 0;              }               count++;                prev = curr;    }       fclose(fp); } int main(int argc, char **argv) {         int i = 0;      if(argc < 2)         {               printf("wzip: file1 [file2 ...]\n");            exit(1);        }       for(i = 1; i < argc; i++)    {               compress(argv[i]);      }               return 0; }

wunzip.c

 #include <stdio.h> #include <stdlib.h> void decompress(char *path) {     char curr = 0;  int count = 0;  FILE *fp = fopen(path, "r");            if(fp == NULL)  {               printf("[%s] file does not exist, exiting program ..\n", path);                 exit(1);        }       while(1)        {               int bytes = fread(&count, 1, 4, fp);                if(bytes != 4)          {                       break;          }               fread(&curr, 1, 1, fp);             {                       int i = 0;                      for(i = 0; i < count; i++)                   {                               fwrite(&curr, 1, 1, stdout);                        }               }       }       fclose(fp); } int main(int argc, char **argv) {         int i = 0;      if(argc < 2)         {               printf("wunzip: file1 [file2 ...]\n");          exit(1);        }       for(i = 1; i < argc; i++)    {               decompress(argv[i]);    }               return 0; }

uncompressed_first.txt

 aaaaaaaaaabbbbthe

uncompressed_second.txt

Add a comment
Know the answer?
Add Answer to:
wzip and wunzip – MUST BE WRITTEN IN C The next tools you will build come...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need only one  C++ function . It's C++ don't write any other language. Hello I need...

    I need only one  C++ function . It's C++ don't write any other language. Hello I need unzip function here is my zip function below. So I need the opposite function unzip. Instructions: The next tools you will build come in a pair, because one (zip) is a file compression tool, and the other (unzip) is a file decompression tool. The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when...

  • I need only one  C++ function . It's C++ don't write any other language. Hello I need...

    I need only one  C++ function . It's C++ don't write any other language. Hello I need unzip function here is my zip function below. So I need the opposite function unzip. Instructions: The next tools you will build come in a pair, because one (zip) is a file compression tool, and the other (unzip) is a file decompression tool. The type of compression used here is a simple form of compression called run-length encoding (RLE). RLE is quite simple: when...

  • For this problem, you have to use following hash function: key modulo the number of buckets....

    For this problem, you have to use following hash function: key modulo the number of buckets. Input format: This program takes a file name as argument from the command line. The file is either blank or contains successive lines of input. Each line contains a character, either ‘i’ or ‘s’, followed by a tab and then an integer, the same format as in the Second Part. For each of the lines that starts with ‘i’, your program should insert that...

  • C language huffman This exercise will familiarize you with linked lists, which you will need for...

    C language huffman This exercise will familiarize you with linked lists, which you will need for a subsequent programming Getting Started assignment Overview Requirements Getting Started Submit Start by getting the files. Type 264get hw13 and then cd hw13 from bash. Pre-tester You will get the following files: Q&A Updates 1. huffman.h: An empty header file, you have to define your own functions in this homework. 2. huffman.c: An empty c file, you have to define your own functions in...

  • C++ program: The aim of this homework assignment is to practice writing hierarchy of classes. Design...

    C++ program: The aim of this homework assignment is to practice writing hierarchy of classes. Design a hierarchy of Files. You have two different kinds of Files, Text File and an Image File. The files are identified by their name and type, which is identified by either txt or gif extension. An Image file has dimensions of pixel rows and pixel columns. Each pixel has a color depth that can be represented by number of bits. (8 bits is one...

  • It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #inclu...

    It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #include "File.h" // Constructor of File that takes File name and type as arguments File::File(string type, string name) {    this->type = type;    this->name = name; } //returns the type. string File::getType() {    return type; } //returns the name of file. string File::getName() {    return name; } File.h #ifndef __FILE_H__ #define...

  • Hi everyone, I have a C programming problem, answers are better with explanations. Background Materials: The...

    Hi everyone, I have a C programming problem, answers are better with explanations. Background Materials: The Task: The Answer: The completed C code. Below is the file charIO4C.c: #include <stdio.h> #include <ctype.h> #define QUIT_LETTER 'q' int main(void) { int c, line_length; // Each pass through the outer loop reads a line of input. while (1) { printf("\nEnter a line of text. (To quit, start the line with %c.)\n", QUIT_LETTER); c = fgetc(stdin); if (c == EOF || c == QUIT_LETTER)...

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • Can you comment notes along with the code so I can follow along and understand what is being written?? Thanks in advance...

    Can you comment notes along with the code so I can follow along and understand what is being written?? Thanks in advance! Let's say we're in a directory and we want to rename every file ending in·c, to end in .C (i.e., capitalize the c'). Here's one way to do it: $ for i in *. c; b-basename "$1" .c'; mv "8b.c" do "8b.C". done Things get more complicated if you want more complex renaming. Say for example you just...

  • OUTPUT MUST MATCH THE IMAGE TO THE TEE. I have the majority of the program written,...

    OUTPUT MUST MATCH THE IMAGE TO THE TEE. I have the majority of the program written, just having issues with the output. Please make sure to have the right alignment, variable data types and number padding/decimals as shown below! ​ MUST BE IN C NOT C++/C#! Assume the Item Num is an int that must be padded with zeros (ex: 1234 ---> 00001234) Write a C program for a shopping list to run on ocelot. Use the listed items and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT