繁体   English   中英

miniz C无法使用绝对路径压缩文件

[英]miniz C can't zip file with absolute path

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

我使用该文档生成了我的代码,并且可以正常工作。 仅当我提供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);

因此,我用存档名称,文件名(具有绝对路径)和其中的数据(char *格式)来调用zip函数。

这是“完整”的代码:函数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);
}

这是get_data()使用的add_path()函数:

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

有人知道吗?

如果没有帮助,则应查找源。 Githubminiz中的代码之后,文件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获取代码并对其进行修改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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