[英]miniz C can't zip file with absolute path
I'm using miniz to create a .zip file in C, on Windows. 我正在使用miniz在Windows上的C中创建.zip文件。
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.
仅当我提供zip函数的相对路径时,才能使用所需的文件创建档案。
I don't get why the "file_name" variable must be something like "../test/file.txt" and not "C:/../test/file.txt". 我不明白为什么“ 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 !!"));
Before this function, I open my file, get the data inside and call the zip_function with it. 在使用此功能之前,我先打开文件,将数据放入其中,然后调用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);
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). 因此,我用存档名称,文件名(具有绝对路径)和其中的数据(char *格式)来调用zip函数。
This is the "full" code : the function init_zip is the first function called by my program. 这是“完整”的代码:函数init_zip是我的程序调用的第一个函数。 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). 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);
}
This is the add_path()
function used in get_data()
: 这是
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);
}
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: 在Github上miniz中的代码之后,文件
miniz_zip.c
第4297行我看到:
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
. 调用函数
mz_zip_writer_validate_archive_name
来检查第二个文件名,条件是该文件名不能以驱动器号开头(第3069行),如果是,则返回FALSE,错误设置为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.
如果这对您很重要,则可以从Github获取代码并对其进行修改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.