
| CODE |
#include <iostream> #include <fstream> #include <string> #include <zlib.h> #define EXIT_C2 1 #define C2_COMPRESS 2 #define C2_DECOMPRESS 3 bool decompress_one_file(const char *infilename, const char *outfilename); bool compress_one_file(const char *infilename, const char *outfilename); void Player_Control_run(); int main(int argc, char *argv[]) { Player_Control_run(); } inline void Run_Command(std::string command) { std::string Str; std::string Str2; int Command; if ( command == "C2_EXIT" ) Command = EXIT_C2; else if ( command == "C2_COMPRESS" ) Command = C2_COMPRESS; else if ( command == "C2_DECOMPRESS" ) Command = C2_DECOMPRESS; switch( Command ) { case EXIT_C2: exit(0); break; case C2_COMPRESS: std::cout << "Enter the name of the File you want to compress: "; getline(std::cin, Str); std::cout << "Enter the destination/name of the Compressed File: "; getline(std::cin, Str2); if ( compress_one_file( Str.c_str(), Str2.c_str() ) ) std::cout << "\n The File Has Been Decompressed Successfully" << std::endl; else std::cout << "\n ERROR: The File was not successfully decompressed."; break; case C2_DECOMPRESS: std::cout << "Enter the name of the File you want to decompress: "; getline(std::cin, Str); std::cout << "Enter the destination/name of the decompressed File: "; getline(std::cin, Str2); if ( decompress_one_file( Str.c_str() , Str2.c_str() ) ) std::cout << "\n The File Has Been Decompressed Successfully" << std::endl; else std::cout << "\n ERROR: The File was not successfully decompressed."; break; default: std::system( command.c_str() ); break; } } inline void Player_Control_run() { std::string Command = ""; while ( true ) { getline ( std::cin, Command ); Run_Command( Command.c_str() ); Command = ""; } exit(0); } inline bool decompress_one_file(const char *infilename, const char *outfilename) { gzFile infile = gzopen(infilename, "rb"); FILE *outfile = fopen(outfilename, "wb"); if (!infile || !outfile) return false; char buffer[128]; int num_read = 0; while ( ( num_read = gzread(infile, buffer, sizeof(buffer) ) ) > 0) { fwrite(buffer, 1, num_read, outfile); } gzclose(infile); fclose(outfile); return true; } inline bool compress_one_file(const char *infilename, const char *outfilename) { FILE *infile = fopen(infilename, "rb"); gzFile outfile = gzopen(outfilename, "wb"); if (!infile || !outfile) return false; char inbuffer[128]; int num_read = 0; unsigned long total_read = 0, total_wrote = 0; while ((num_read = fread(inbuffer, 1, sizeof(inbuffer), infile)) > 0) { total_read += num_read; gzwrite(outfile, inbuffer, num_read); } fclose(infile); gzclose(outfile); return true; } |