0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C言語組み込みzipライブラリminizの最近の使い方メモ

Posted at

古い記事とか読んでそのままやっても駄目だったんでその結果をメモ。
例ではアーカイブ中のファイルをソートしてファイル名とファイルインデックスを返し、
オンメモリ上に解凍するかんじ。

下記からライブラリをダウンロード。
https://github.com/richgel999/miniz/releases

#define MINIZ_HEADER_FILE_ONLY
#include "miniz.h"
#include "miniz_zip.h"

int zip_files(char* path, mz_zip_archive* zip_archive, char*** files, int** indexes) {
  if (access(path, F_OK) == -1) {
    fprintf(stderr,"error file not found\n");
    exit(EXIT_FAILURE);
  }

  memset(zip_archive, 0, sizeof(mz_zip_archive));
  mz_zip_reader_init_file(zip_archive, path, 0);

  int numfiles = mz_zip_reader_get_num_files(zip_archive);

  *files = (char**) malloc((numfiles + 1) * sizeof(char *));
  memset(*files, 0, numfiles + 1);

  *indexes = (int*) malloc((numfiles + 1) * sizeof(int));
  memset(*indexes, 0, (numfiles + 1) * sizeof(int));

  for (int u = 0; u < numfiles; u++) {
    char getfile[516];
    int rr = mz_zip_reader_is_file_a_directory(zip_archive, u);

    if (rr == 1) { // is directory
      continue;
    }

    int r = mz_zip_reader_get_filename(zip_archive, u, getfile, sizeof(getfile));
    int i = get_number(getfile);
    char* file = (char*)malloc((strlen(getfile) + 1) * sizeof(char));
    memset(file, 0, (strlen(getfile) + 1) * sizeof(char));

    strcpy(file, getfile);
    (*files)[i] = file;
    (*indexes)[i] = u;
  }

  return numfiles;
}


int main(int argc, char* argv[]) {
  char* path = argv[1];
  mz_zip_archive zip_archive;
  char** files = 0;
  int* indexes = 0;

  int count = zip_files(path, &zip_archive, &files, &indexes);
  for (int i = 0; i <= count; i++) {
    printf("%03d %03d %s\n", i, indexes[i], (files[i] == 0) ? "" : files[i]);
    size_t size;
    void* file_contents;
    file_contents = mz_zip_reader_extract_to_heap(&zip_archive, indexes[i], &size, 0); 
  }
}

ビルド例。

cc -Wformat -o ziplist main.c -I miniz-3.0.2/ miniz-3.0.2/*.c
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?