因此,基本上我需要列出所有文件和子文件夹以及文件等等,直到检查完所有路径,读取每个对象的数据并打包它全部放入单个文件中。获取给定路径中的所有文件和子文件夹 - C/C++中的语言兼容性
我不想使用boost或minizip,因为它太容易了,所以我只是通过dirent自己写的 - 一切正常,我只是在目录和非ANSI字符的标题文件(我来自波兰) 我可以打开它们没有问题,并从它们中读取所有数据,但是当我将它们的文件名打包到std :: string时 - 它会被破坏并发生例如:
TEST〜 1而不是TESTąę
如何将文件和目录打包到单个文件中:
我通过dirent读取了主目录的内容,如果我找到了文件 - 我只是读了它的路径和数据给strucutre,如果我找到另一个目录 - 我只是读它的结构路径并在该目录上递归调用搜索函数。
要读写文件我使用fopen,fwrite,fread。
只需注意文件内部的数据正在写入时没有问题 - 所有特殊字符都可以毫无问题地存储。
任何帮助,将非常感激。
代码:
#include <list>
#include <string>
#include <dirent.h>
#include <cstdio>
#include <windows.h>
typedef struct {
std::string path;
unsigned lenght_of_path;
char type;
unsigned size;
char *data;
} single_element;
int pack(std::string root_directory, std::list<single_element> *all_elements)
//packs all files and subfolder and files and subfolder inside them etc into a std::list of structures
//please don't mind 0 error checks but I just want to get that language thing first
{
single_element temp;
temp.data = NULL;
FILE *processed;
DIR *directory;
struct dirent *ent;
if ((directory = opendir(root_directory.c_str())) != NULL)
{
while ((ent = readdir(directory)) != NULL)
{
std::string actual_path = root_directory;
std::string packed_file = ent->d_name;
if (packed_file != "." && packed_file != "..") //ignore .. and . in listing
{
if (ent->d_type == 16384) //if we found a directory
{
actual_path += "\\";
actual_path += packed_file;
temp.data = NULL;
temp.type = 'D'; // D is for directory, used later when extracting
temp.path = actual_path;
temp.lenght_of_path = actual_path.length();
temp.size = 0;
all_elements->push_back(temp);
pack(actual_path,all_elements); //recursively scan subfolder
}
else //we got file - I know there also can be other things as symbolic link but its only for test and I'm going to fix it once it works
{
actual_path += "\\";
actual_path += packed_file;
processed = fopen(actual_path.c_str(),"rb");
if (processed != NULL)
{
temp.path = actual_path;
temp.lenght_of_path = actual_path.length();
temp.type = 'F'; //F is for FILE;
fseek (processed,0,SEEK_END);
temp.size = ftell(processed);
all_elements->push_back(temp);
all_elements->back().data = new char[all_elements->back().size];
rewind(processed);
fread(all_elements->back().data,all_elements->back().size,1,processed);
fclose(processed);
}
}
}
}
}
closedir (directory);
return 0;
}
int save(std::string file_name, std::list<single_element> all_elements)
//write all_elements to single file
{
file_name += ".pak";
FILE *packed = fopen(file_name.c_str(),"wb");
int ammount = all_elements.size();
fwrite((const void*)&ammount,sizeof(int),1,packed); //fist write how many elements there are to be packed
for (std::list<single_element>::iterator i = all_elements.begin(); i != all_elements.end(); i++) //write each element
{
fwrite((const void*)&i->lenght_of_path,sizeof(unsigned),1,packed);
fwrite(i->path.c_str(),i->lenght_of_path,1,packed);
fwrite((const void*)&i->type,sizeof(char),1,packed);
if (i->type == 'F') //if its file write also data and it's size
{
fwrite((const void*)&i->size,sizeof(unsigned),1,packed);
fwrite(i->data,i->size,1,packed);
}
}
fclose(packed);
return 0;
}
int load(std::string file, std::list<single_element> *all_elements)
//load elements from file to list of structures
{
char *buf = NULL;
file += ".pak";
FILE *packed = fopen(file.c_str(),"rb");
single_element temp;
int ammount;
fread((void*)&ammount,sizeof(int),1,packed); //first read how many elements there are
for (int i = 0; i < ammount; i++)
{
fread((void*)&temp.lenght_of_path,sizeof(unsigned),1,packed);
buf = new char[temp.lenght_of_path+1];
fread(buf,temp.lenght_of_path,1,packed);
buf[temp.lenght_of_path] = '\0';
temp.path = buf;
delete buf;
buf = NULL;
fread((void*)&temp.type,sizeof(char),1,packed);
all_elements->push_back(temp);
if (temp.type == 'F') //if its file, also read its data and size
{
fread((void*)&all_elements->back().size,sizeof(unsigned),1,packed);
all_elements->back().data = new char[all_elements->back().size];
fread(all_elements->back().data,all_elements->back().size,1,packed);
}
else //else we got directory so just write 0 to size and NULL to data
{
all_elements->back().size = 0;
all_elements->back().data = NULL;
}
}
fclose(packed);
return 0;
}
int extract(std::string root_directory, std::list<single_element> all_elements)
{
CreateDirectory(root_directory.c_str(),NULL); //create root directory
std::list<single_element>::iterator i = all_elements.begin();
while (i != all_elements.end()) //first create all directories
{
if (i->type == 'D') //if we got a directory create it and erase from list
{
CreateDirectory(i->path.c_str(),NULL);
i = all_elements.erase(i);
}
else ++i;
}
i = all_elements.begin();
while (i != all_elements.end()) //now extract all files
{
FILE *extracted;
extracted = fopen(i->path.c_str(),"wb");
fwrite(i->data,i->size,1,extracted);
fclose(extracted);
++i;
}
return 0;
}
int main()
{
std::list<single_element> all;
std::string root = "Test";
pack(root,&all);
save(root,all);
load(root,&all);
extract(root,all);
return 0;
}
为什么要给这么多不相关的信息,而不是提供代码示例显示你的问题? – Slava 2014-08-27 20:46:42
'我不想使用boost或minizip,因为它太容易了,如果这很容易,为什么你会遇到麻烦? – PaulMcKenzie 2014-08-27 20:49:24
也许他认为助推器过多,他认为应该是一项微不足道的任务? – 2014-08-27 21:20:30