Laman

Minggu, 24 Juni 2012

Compress and Decompress file using agorithm Run-Length Encoding (C++)

Buat teman-teman yang lagi nyari program Compress and Decompress file ini ada contoh yang sudah bisa jalan..
tapi run-nya pake cmd
pada tau gak cara run C++ lewat cmd?? kalo belum tanya tuh ama om google.


programnaya sebagai berikut. tapi ini masih acak-acakan gitu. trus belum ada komentnya. mungkin nanti punya waktu luang akan dibenerin. oke silakan dilihat..


file: compress.cpp

  1.  #include <stdio.h>  
  2. #include <string.h>  
  3. #include <stdlib.h>  
  4.   
  5. void compress( char* data, int count, FILE* outfile );  
  6. char* readFileData( char* filename, int* count_ptr );  
  7.   
  8. int main( int num_args, char* arg_values[] )  
  9. {  
  10.   if (num_args != 2)  
  11.   {  
  12.     printf( "Usage: rle filename (produces filename.rle)\n" );  
  13.     printf( "       rle filename.rle (produces filename.plain)\n" );  
  14.     exit(1);  
  15.   }  
  16.   
  17.   char* input_filename = arg_values[1];  
  18.   
  19.   // read the file data into an array  
  20.   int count;  
  21.   char* data = readFileData(input_filename,&count);  
  22.   
  23.   // Call compress() or decompress().  
  24.   FILE* outfile;  
  25.     int len = strlen(input_filename);  
  26.   if (len < 4 || (strcmp(input_filename+(len-4),".rle") != 0 && strcmp(input_filename+(len-4),".txt")==0))  
  27.   {  
  28.     char output_filename[80];  
  29.     strcpy( output_filename, input_filename );  
  30.     strcat( output_filename, ".rle" );  
  31.     printf( "Compressing %s to %s\n", input_filename, output_filename );  
  32.   
  33.     outfile = fopen( output_filename, "wb" );  
  34.     compress( data, count, outfile );  
  35.   }  
  36.   else {}  
  37.   
  38.   // Close the output file to ensure data is saved.  
  39.   fclose(outfile);  
  40.   
  41.   // Free the array we allocated  
  42.   delete data;  
  43.   
  44.   return 0;  
  45. }  
  46.   
  47. void compress( char* data, int loop, FILE* outfile )  
  48. {  
  49.     char character,x;  
  50.    char next_character;  
  51.    int count=1;  
  52.   // TODO: compress the data instead of just writing it out to the file  
  53.   for (int i=0; i<loop; ++i)  
  54.   {  
  55.   
  56.         if(!character){  
  57.         character=data[i];  
  58.       }  
  59.       else{  
  60.         next_character=data[i];  
  61.         if(character==next_character){  
  62.                 count++;  
  63.          }  
  64.          else if(count==1){  
  65.             putc( character, outfile );  
  66.             character=next_character;  
  67.          }  
  68.          else if(count<=3){  
  69.             for(int j=i-count; j<i; j++){  
  70.                     putc( data[j], outfile );  
  71.             }  
  72.             count=1;  
  73.             character=next_character;  
  74.          }else{  
  75.                 putc('*', outfile);  
  76.             putc(character, outfile);  
  77.                 x=count+'0';  
  78.             putc(x, outfile);  
  79.             count=1;  
  80.             character=next_character;  
  81.          }  
  82.          if(i+1==loop){  
  83.             if(count==1){  
  84.                 putc( character, outfile );  
  85.                putc( data[i], outfile );  
  86.             }  
  87.            else if(count<=3){  
  88.             for(int j=i-count+1; j<=i; j++){  
  89.                         putc( data[j], outfile );  
  90.                 }  
  91.           }  
  92.             else{  
  93.                     putc('*', outfile);  
  94.              putc(character, outfile);  
  95.                x=count+'0';  
  96.                 putc(x, outfile);  
  97.              }  
  98.          }  
  99.       }  
  100.     //putc( data[i], outfile );  // write out a single byte of data  
  101.   }  
  102. }  
  103.   
  104.   
  105. char* readFileData( char* filename, int* count_ptr )  
  106. {  
  107.   // Returns a pointer to an array storing the file data.  
  108.   // Sets the variable pointed to by 'count' to contain the file size.  
  109.   // Exits the program if the filename doesn't exist.  
  110.   FILE* infile = fopen(filename,"rb");  
  111.   if ( !infile )  
  112.   {  
  113.     printf( "No such file \"%s\"!\n", filename );  
  114.     exit(1);  
  115.   }  
  116.   
  117.   // Get file size by going to the end of the file, getting the   
  118.   // position, and then going back to the start of the file.  
  119.   fseek( infile, 0, SEEK_END );  
  120.   int count = ftell(infile);  
  121.   fseek( infile, 0, SEEK_SET );  
  122.   
  123.   // read the data from the file  
  124.   char* data = new char[count];  
  125.   
  126.   fread( data, 1, count, infile );  
  127.   
  128.   fclose(infile);  
  129.   
  130.   *count_ptr = count;  
  131.   return data;  
  132. }  


file: decompress.cpp
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <stdlib.h>  
  4.   
  5. void decompress( char* data, int count, FILE* outfile );  
  6. char* readFileData( char* filename, int* count_ptr );  
  7.   
  8. int main( int num_args, char* arg_values[] )  
  9. {  
  10.   if (num_args != 2)  
  11.   {  
  12.     printf( "Usage: rle filename (produces filename.rle)\n" );  
  13.     printf( "       rle filename.rle (produces filename.plain)\n" );  
  14.     exit(1);  
  15.   }  
  16.   
  17.   char* input_filename = arg_values[1];  
  18.   
  19.   // read the file data into an array  
  20.   int count;  
  21.   char* data = readFileData(input_filename,&count);  
  22.   
  23.   // Call compress() or decompress().  
  24.   FILE* outfile;  
  25.     int len = strlen(input_filename);  
  26.     if(len < 4 || strcmp(input_filename+(len-4),".rle") == 0)  
  27.     {  
  28.         char output_filename[80];  
  29.         strncpy( output_filename, input_filename, len-4 );  
  30.         output_filename[len-4] = 0;  
  31.         //strcat( output_filename, ".txt" );  
  32.         printf( "Decompressing %s to %s\n", input_filename, output_filename );  
  33.   
  34.         outfile = fopen( output_filename, "wb" );  
  35.         decompress( data, count, outfile );  
  36.     }  
  37.     else{}  
  38.   
  39.   // Close the output file to ensure data is saved.  
  40.   fclose(outfile);  
  41.   
  42.   // Free the array we allocated  
  43.   delete data;  
  44.   
  45.   return 0;  
  46. }  
  47.       
  48. void decompress( char* data, int loop, FILE* outfile )  
  49. {  
  50.     char character;  
  51.     int count;  
  52.   // TODO: decompress the data instead of just writing it out to the file  
  53.   for (int i=0; i<loop; ++i)  
  54.   {  
  55.       
  56.     if(data[i]=='*'){  
  57.         character = data[i+1];  
  58.         count = atoi (&data[i+2]);  
  59.         for(int j=0; j<count; j++){  
  60.             putc(character, outfile);  
  61.         }  
  62.         i+=2;  
  63.     }  
  64.     else{  
  65.         putc( data[i], outfile );  // write out a single byte of data  
  66.     }  
  67.   }  
  68. }     
  69.       
  70. char* readFileData( char* filename, int* count_ptr )  
  71. {  
  72.   // Returns a pointer to an array storing the file data.  
  73.   // Sets the variable pointed to by 'count' to contain the file size.  
  74.   // Exits the program if the filename doesn't exist.  
  75.   FILE* infile = fopen(filename,"rb");  
  76.   if ( !infile )  
  77.   {  
  78.     printf( "No such file \"%s\"!\n", filename );  
  79.     exit(1);  
  80.   }  
  81.   
  82.   // Get file size by going to the end of the file, getting the   
  83.   // position, and then going back to the start of the file.  
  84.   fseek( infile, 0, SEEK_END );  
  85.   int count = ftell(infile);  
  86.   fseek( infile, 0, SEEK_SET );  
  87.   
  88.   // read the data from the file  
  89.   char* data = new char[count];  
  90.   
  91.   fread( data, 1, count, infile );  
  92.   
  93.   fclose(infile);  
  94.   
  95.   *count_ptr = count;  
  96.   return data;  
  97. }  
nah kalo merasa susah untuk copy ada nih filenya yang udah jadi download disini.. ada juga contoh file .txt yang akan di compress..
jangan lupa ninggalin komentarnya yah.. ditunggu loh..
oke sekian semoga bermanfaat.

2 komentar:

  1. gan,, gimana tuh pas di klik kebuka tapi langsung kok malah cepet ilang .. gmn tuh ga ? mohon pencerahannya

    BalasHapus
    Balasan
    1. run nya lewat apaan?? kalo pake kayak borland harus di tambah
      #include
      dan tambahin
      getch();
      diantara delete data; ama return 0; di main function
      biar ketahan gitu.
      tapi kalo runnya pake cmd pasti ketahan.

      Hapus