miniz C不能用绝对路径压缩文件

问题描述 投票:0回答:1

我正在使用miniz在Windows上用C创建.zip文件。

我使用doc生成我的代码并且它有效。我可以用我想要的文件创建一个存档,只要我给zip函数提供相对路径。

我不明白为什么“file_name”变量必须是“../test/file.txt”而不是“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 !!"));

在此功能之前,我打开我的文件,获取内部数据并用它调用zip_function。

    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);

所以我将zip函数称为归档名称,文件名(带绝对路径)和其中的数据(以char *格式)。

这是“完整”代码:函数init_zip是我的程序调用的第一个函数。 arg参数是我想要创建的存档名称(它可以是绝对路径并且可以工作),args参数是我想要添加到存档文件的不同文件的名称(相对路径有效但不是绝对的)。

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);
}

这是add_path()中使用的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);
}

有人知道吗?

c windows filepath miniz
1个回答
1
投票

如果没有任何帮助,那么你应该查找源。按照miniz on Github中的代码,文件miniz_zip.c第4297行我看到:

mz_bool mz_zip_add_mem_to_archive_file_in_place(...

调用函数mz_zip_writer_validate_archive_name检查第二个文件名,前提是它不能以驱动器号开头(第3069行),如果是,则返回FALSE,错误设置为MZ_ZIP_INVALID_FILENAME

至于为什么第二个文件名可能不是绝对路径,我不知道。如果它对您很重要,您可以从Github获取代码并进行调整。

© www.soinside.com 2019 - 2024. All rights reserved.