因此,基本上我需要列出所有文件和子文件夾以及文件等等,直到檢查完所有路徑,讀取每個對象的數據並打包它全部放入單個文件中。獲取給定路徑中的所有文件和子文件夾 - 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