Sam Price commented: 
https://gitlab.rtems.org/rtems/rtos/rtems/-/work_items/5772#note_159161


If it helps I am using zlib from RTEMS, with small wrappers around them.
Might be a starting point?
```c
#include <stdio.h>
#include <stdlib.h>
#include <zlib.h>
#include <rtems/shell.h>

#define CHUNK 16384

static int compress_file(const char *inpath) {
  char outpath[256];
  sprintf(outpath, "%s.gz", inpath);

  FILE *fin = fopen(inpath, "rb");
  gzFile fout = gzopen(outpath, "wb");
  if (!fin || !fout) {
    fprintf(stderr, "Error opening files\n");
    if (fin) fclose(fin);
    if (fout) gzclose(fout);
    return -1;
  }

  char buf[CHUNK];
  int len;
  while ((len = fread(buf, 1, CHUNK, fin)) > 0) {
    if (gzwrite(fout, buf, len) != len) {
      fprintf(stderr, "Write error\n");
      fclose(fin);
      gzclose(fout);
      return -1;
    }
  }

  fclose(fin);
  gzclose(fout);
  printf("Compressed to %s\n", outpath);
  return 0;
}

static int decompress_file(const char *inpath) {
  if (!strstr(inpath, ".gz")) {
    fprintf(stderr, "Input must end in .gz\n");
    return -1;
  }

  char outpath[256];
  strcpy(outpath, inpath);
  outpath[strlen(outpath) - 3] = '\0';  // remove ".gz"

  gzFile fin = gzopen(inpath, "rb");
  FILE *fout = fopen(outpath, "wb");
  if (!fin || !fout) {
    fprintf(stderr, "Error opening files\n");
    if (fin) gzclose(fin);
    if (fout) fclose(fout);
    return -1;
  }

  char buf[CHUNK];
  int len;
  while ((len = gzread(fin, buf, CHUNK)) > 0) {
    if (fwrite(buf, 1, len, fout) != len) {
      fprintf(stderr, "Write error\n");
      gzclose(fin);
      fclose(fout);
      return -1;
    }
  }

  gzclose(fin);
  fclose(fout);
  printf("Decompressed to %s\n", outpath);
  return 0;
}

static int rtems_shell_compress(int argc, char **argv) {
  if (argc != 2) {
    printf("Usage: compress <file>\n");
    return 1;
  }
  return compress_file(argv[1]);
}

static int rtems_shell_decompress(int argc, char **argv) {
  if (argc != 2) {
    printf("Usage: decompress <file.gz>\n");
    return 1;
  }
  return decompress_file(argv[1]);
}


void rki_add_compress_commands(){
    rtems_shell_add_cmd("compress", "file", "compress <file> \n Outputs 
file.gz", rtems_shell_compress);
    rtems_shell_add_cmd("decompress", "file", "decompress <file.gz>", 
rtems_shell_decompress);

}
```

-- 
View it on GitLab: 
https://gitlab.rtems.org/rtems/rtos/rtems/-/work_items/5772#note_159161
You're receiving this email because of your account on gitlab.rtems.org. 
Unsubscribe from this thread: 
https://gitlab.rtems.org/-/namespace/49/sent_notifications/5-034w04llt2fcs6gqufzn0u0cy-1d/unsubscribe
 | Manage all notifications: https://gitlab.rtems.org/-/profile/notifications | 
Help: https://gitlab.rtems.org/help


_______________________________________________
bugs mailing list
[email protected]
http://lists.rtems.org/mailman/listinfo/bugs

Reply via email to