Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h> // For exit()
- #include <string.h>
- #include <ctype.h>
- #include <assert.h>
- void compress_file(FILE *fp_in, FILE *fp_out);
- void uncompress_file(FILE *fp_in, FILE *fp_out);
- int main(int argc, char **argv) {
- assert(argc >= 3 && "my-zip: [input files] 'out put file'\n");
- FILE *fptr_w;
- char file_r[100], file_w[100];
- // Last argv is target file
- strcpy(file_w, argv[argc - 1]);
- fptr_w = fopen(file_w, "w");
- if (fptr_w == NULL) {
- printf("Cannot open file %s \n", file_w);
- exit(1);
- }
- for (int i = 1; i < argc - 1; i++) {
- // Open file for reading
- FILE *fptr_r;
- strcpy(file_r, argv[i]);
- fptr_r = fopen(file_r, "r");
- if (fptr_r == NULL) {
- printf("Cannot open file %s \n", file_r);
- exit(1);
- } else {
- printf("compressing file %s\n", file_r);
- compress_file(fptr_r, fptr_w);
- fclose(fptr_r);
- }
- }
- /*
- size_t bytes;
- while (0 < (bytes = fread(buffer, 1, sizeof(buffer), fptr1))) {
- strcpy(buffer, compress(buffer));
- fwrite(buffer, 1, bytes, fptr2);
- }
- c = fgetc(fptr1);
- while (c != EOF)
- {
- fputc(c, fptr2);
- c = fgetc(fptr1);
- }
- */
- fclose(fptr_w);
- return 0;
- }
- void compress_file(FILE *fp_in, FILE *fp_out) {
- int count, ch, ch2, chk;
- ch = getc(fp_in);
- ch2 = ch;
- while (ch2 != EOF) {
- // if next byte is the same increase count and test
- for (count = 0; ch2 == ch && count < 255; count++) {
- ch2 = getc(fp_in); // set next variable for comparison
- }
- // write bytes into new file
- putc(count, fp_out);
- putc(ch, fp_out);
- ch = ch2;
- }
- }
- void uncompress_file(FILE *fp_in, FILE *fp_out) {
- int count, ch, ch2;
- for (count = 0; ch2 != EOF; count = 0) {
- ch = getc(fp_in); // grab first byte
- ch2 = getc(fp_in); // grab second byte
- // write the bytes
- do {
- putc(ch2, fp_out);
- count++;
- } while (count < ch);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement