Last time we talked about the packing of the file, let's talk about the packing of the folder.
There may be a lot of folders to package, and subdirectories for individual folders are unknown, so you need to walk through all the subdirectories of the folder! This interface everyone should already know (still do not know, go to pick my last blog O (∩_∩) o~) programmer must: Clear thinking, then I pretended to be a qualified program, so painstakingly drew a picture, more directly and clearly expressed my views and practices:
Folders to package: Folder1, Folder2, Folder3, the absolute paths are: C:/users/folder1, D:tmp/folder2, D:folder3; requirements: Packaged under E:/test/pack
What kind of packaging is used to preserve the hierarchical correspondence of different sub-files under different folders and individual folders? After all, we have to implement the solution package, the total to achieve the solution in which specific path!!! After a long reflection, finally tiandaochouqin, let me think of the solution, the last blog post also mentioned, but still want to solve (to) the next: for packaging, the whole path to package is placed in the parameter vector, namely: known. What about this full-path information that is not stored in the final packaged. dat? In fact, it is not necessary, because we want its full path useless. Li: Folder1 folder, its path c:/users/folder1, packaged under E:/test/pack, if it retains its true full path, as a file prerequisite information in the. dat, the package will be the path: e:/test/pack/c :/users/folder1, how does this path create a new folder? and a small partner may ask questions, do not keep the path information is OK, then I ask, how to unpack the time and how to know the level of the corresponding relationship.
(focus) If you want to package the folder, such as: Folder1, Folder2, Folder3, the path = its folder name; if SubFolder1, SubFolder2, SubFolder3 .... This seed catalogue is also 1.txt, 2.avi, 3.dll ... In this seed subdirectory, we set its path to the parent directory for its packaging level: that is: the first tier save path is: Folder1, Folder2, Folder3; the second layer saves the path: Folder1/subfolder1, Folder1/subfolder2 The third layer is: Folder1/subfolder1/1.txt, folder1/subfolder2/foler1; the fourth layer is: folder1/subfolder2/foler1/2.avi;folder1/ Subfolder2/foler1/3.dll
Come on, let's go. Code: Folder part of the dedication ~
1 intFoldercount = Directories.size ();//How many folders did the vector keep?2 intsum =0, Subnum =0;3 for(size_t i =0; i < Foldercount; i++)4 {5vector<string>Allsubfiles;6 Getallsubfiles (Directories[i], allsubfiles);7Subnum =allsubfiles.size ();8Sum + =Subnum;9 }TenSum + =Foldercount; OneFwrite (&sum,sizeof(sum),1, WFP);//using the path method to solve: different layers of the solution package, namely: Record folder + Its subdirectories of all the number, write to the packaging file A - intj =0; - for(inti =0; i < Foldercount; i++) the { - structFolderInfo folder; - - stringDirName =Getfilenamefrompath (Directories[i]); +strcpy (folder. Filepath, Dirname.c_str ());//Path -strcpy (folder. FolderName, Dirname.c_str ());//name +Folder. Foldernamelen = strlen (Dirname.c_str ());//name Length AFolder. FileSize =0;//Folder Size atFolder.type =1; - -Fwrite (&folder,sizeof(folder),1, WFP); Folder information is written to a packaged file -cout <<"FolderName:"<< folder. FolderName <<"; Namelength:"<< folder. Foldernamelen <<"; Path:"<< folder. Filepath <<", FileSize:"<< folder. FileSize <<"; Type:"<< Folder.type <<Endl; - -vector<string>Allsubfile; invector<string>:: iterator Newit; -Getallsubfiles (Directories[i], allsubfile);//traversing sub-file directories to intCount =allsubfile.size (); + - for(Newit = Allsubfile.begin (); Newit! = Allsubfile.end (); newit++) the { * structFolderInfo Fileindir; $ Panax Notoginseng //Custom relative path functions: - stringFolderPath = Directories[i];//Folder path the stringSubpath = *newit;//Sub directory path + A intN1 =strlen (Folderpath.c_str ()); the intN2 = strlen (*newit). C_STR ()); + - string_str; $ for(size_t i = n1 +1; I < N2; i++) $ { -_str + =Subpath[i]; - } the stringstr = DirName +'\\'+_str; - Wuyistrcpy (Fileindir.filepath, Str.c_str ());//relative Path the stringSubfilename = Getfilenamefrompath (*newit); -strcpy (Fileindir.foldername, Subfilename.c_str ());//name WuFileindir.foldernamelen = strlen (Subfilename.c_str ());//name Length - About ifstream dirfile; $ Dirfile.open (subpath, ios::binary); - -DIRFILE.SEEKG (0, ios::end); -Streampos fsize =Dirfile.tellg (); AFileindir.filesize =fsize; + the Win32_find_dataa Findfiledata; -Findfirstfilea (*newit). C_str (), &findfiledata); $ if(Findfiledata.dwfileattributes &file_attribute_directory) the { the theFileindir.type =1;//folder Type the } - Else in { theFileindir.type =0;//File Type the } About theFwrite (&fileindir,sizeof(Fileindir),1, WFP);//to write subdirectory information to a packaged file thecout <<"SubName:"<< Fileindir.foldername <<", Namelen:"<< Fileindir.foldernamelen <<", Path:"<< Fileindir.filepath <<", Size:"<< fileindir.filesize <<", type:"<< Fileindir.type <<Endl; the +FILE *files = fopen ((*newit). C_STR (),"RB"); - the if(Fileindir.type = =0)Bayi { theEachfile =NewUnsignedChar[fileindir.filesize]; theFolderbuffer.push_back (Eachfile);//Define global variables: unsigned char *eachfile; and folder buffer vector<unsigned char *>folderbuffer; - - theFread (Folderbuffer[j], fileindir.filesize,1, Files); the theFwrite (Folderbuffer[j], fileindir.filesize,1, WFP); theJ + +; - } the Else the { the Continue;94 } the } thecout <<Endl; the}
Don't forget our packaging function interface design is: BOOL Packfileanddirectory (const vector<string>& files, const vector<string>& directories, const string& outputfilename)
Ah yo, blog not only let me back to warm up my own ideas, the code, but also let me think a good programmer is not only the code can also write articles, haha, little woman Caishuxueqian, willing to the great God, the next article we solve the lump!!