简体   繁体   中英

miniz C can't zip file with absolute path

I'm using miniz to create a .zip file in C, on Windows.

I used the doc to produce my code and it works. I can create an archive with the files I want, ONLY if I give relative path to the zip function.

I don't get why the "file_name" variable must be something like "../test/file.txt" and not "C:/../test/file.txt".

if (!(status = mz_zip_add_mem_to_archive_file_in_place(archive, file_name, data, strlen(data) + 1, s_pComment,
                                                           (uint16) strlen(s_pComment), MZ_BEST_COMPRESSION)))
        return (merror("add file to archive failed !!"));

Before this function, I open my file, get the data inside and call the zip_function with it.

    if (!(src = fopen(file_name, "r")))
        return (merror("can't open this file"));
    char *line = NULL;
    char *data= NULL;
    size_t n = 0;
    getline(&line, &n, src);
    data= strdup(line);
    while (getline(&line, &n, src) != -1){
        data = realloc(save, sizeof(char) * (strlen(data) + strlen(line)) + 1);
        data = strcat(data, line);
    }
    fopen(src);

So I call the zip function with the archive name, the file name (with the absolute path) and the datas inside it (in char * format).

This is the "full" code : the function init_zip is the first function called by my program. The arg parameter is the archive name I want to be create(it can be an absolute path and works) and the args parameter are the names of the differents files I want to add to the archive file (relative path works but not absolute).

typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint;

static const char *s_pComment = "";

static int isDirectory(const char *path) {
    struct stat statbuf;
    if (stat(path, &statbuf) != 0)
        return 0;
    return S_ISDIR(statbuf.st_mode);
}

int get_data(const char *archive, const char *file)
{
    FILE               *src;

    if (!isDirectory(file)) {
        if (!(src = fopen(file, "r")))
            return (merror("can't open this file"));
        char *line = NULL;
        char *save = NULL;
        size_t n = 0;
        getline(&line, &n, src);
        save = strdup(line);
        while (getline(&line, &n, src) != -1) {
            save = realloc(save, sizeof(char) * (strlen(save) + strlen(line)) + 1);
            save = strcat(save, line);
        }
        printf("compressing %s ..\n", file);
        if (m_compress(archive, file, save))
            return (merror("compress function failed"));
        printf(("\tOK.\n"));
        fclose(src);
    }
    else
    {
        DIR                *dir;
        struct dirent      *entry;
        char                *new_file;

        if (!(dir = opendir(file)))
            return (merror("opendir failed: ", "wrong directory path in init_zip.get_data command : ", file, NULL));
        while ((entry = readdir(dir)) != NULL)
        {
            if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
                new_file = add_path(file, entry->d_name);
                get_data(archive, new_file);
            }
        }
        if (new_file)
            free(new_file);
        closedir(dir);
    }
}

int init_zip(const char *arg, const char **args)
{
    printf("\nZIP cmd:\n >");
    remove(arg);
    for (int counter = 0; args[counter]; ++counter)
    {
       get_data(arg, args[counter]);
    }
    printf("All the files are added to %s archive file.\n", arg);
    return (0);
}

int m_compress(const char *archive, const char *file_name, const char *data)
{
    mz_bool status;

    if (data)
        if (!(status = mz_zip_add_mem_to_archive_file_in_place(archive, file_name, data, strlen(data) + 1, s_pComment,
                                                               (uint16) strlen(s_pComment), MZ_BEST_COMPRESSION)))
            return (merror("add file to archive failed !!"));
    else
        if (!(status = mz_zip_add_mem_to_archive_file_in_place(archive, file_name, NULL, 0, "no comment", (uint16)strlen("no comment"), MZ_BEST_COMPRESSION)))
            return (merror("add directory to archive failed !!"));

    return (0);
}

This is the add_path() function used in get_data() :

char          *add_path(const char *str1, const char *str2)
{
  char *path;

  path = malloc(sizeof(char) * (strlen(str1) + 1 + strlen(str2) + 1));
  path = strcpy(path, str1);
  path = strcat(path, "/");
  path = strcat(path, str2);
  return (path);
}

Anyone knows something about it?

If nothing helps, then you should lookup the sources. Following the code in miniz on Github , file miniz_zip.c line 4297 I see:

mz_bool mz_zip_add_mem_to_archive_file_in_place(...

which calls function mz_zip_writer_validate_archive_name to check the second filename provided that it cannot start with a drive letter (line 3069) and if so returns FALSE with error set to MZ_ZIP_INVALID_FILENAME .

As to why this second filename may not be an absolute path, I don't know. If it is important to you, you could get the code from Github and adapt it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM