Packaging and unpacking of documents (including multi-level subdirectories) (top)

Source: Internet
Author: User
Tags strcmp unpack

  In the company to do the project, there is the need to package and reconcile files, and files for any type (eg:txt, EXE, DLL, JPG, OSG, BMP, Avi ...) ), just met this problem, full of all the files are so many formats only with packaging and unpacking two interface how to achieve it, it is simply hard to die, but also to achieve this function, drank two bags of coffee, cool half an hour, decided to study a good.

First from the concept of the document, I believe that the software development personnel are not unfamiliar, the computer is stored in the form of files, the operation of the file is divided into three steps: Open file-read-write-close file (and put the elephant in the refrigerator is a process, haha), looks simple three steps, but it took me two weeks time.

First of all, packaging, is to pack all the files in a. dat file (binary, as for what name, you happy, no matter), to package the file, but actually divided into two parts: Files and folders, so the packaging of the interface I wrote a bit of trouble, to separate files and folders:

BOOL Packfileanddirectory (const std::vector<std::string>& files, const std::vector<std::string> & Directories, const std::string& outputfilename);

Parameter description: Files: File (full path), Directories: folder (full path), OutputFileName: File name (full path), return value: Successful return True, failure return false

unpack the interface design, that is: There is a packaged file, you unpack it into multiple files, and the file level information can not be wrong, the content can not be pigtailed (similar to the one-click Unpack with Windows)   :

BOOL Unpackfileanddirectory (const std::string& inputzipfile, const std::string& outputpath), unpacking files and folders into a directory

Parameter description: Inputzipfile: The file to be unpacked (full path), OutputPath: The output to the target location, all the files and folders after unpacking to the folder (full path), return value: Successfully return True, failed to return false

First, for the packaging interface:

The first parameter: file, this is relatively simple, is to put all the files of the full path in the vector, can use the full path to obtain the basic information of the file: file name, the contents of files and so on.

The second parameter: folder, this will be troublesome, because there are more than one folder, each folder may contain a multilevel subdirectory, first take a folder, first we must get the information of this folder: the name of the folder, name length, path and so on, but also to design a traverse folder all subdirectories of the function, retention level of the corresponding relationship, in the path of this piece, I think for a long time, can not use the full path (why, if the full path, and so on when the solution package, others let you unpack in which disk on which disk, you can not take your original disk of the path bar (eg: the c:/test/ File1.txt packaged, unpacking requirements are solved in: d:/tmp/unpack/, you find your packaging files stored in the path information is: C:/test/file1.txt, after unpacking will appear d:/tmp/unpack/c:/test/ File1.txt, how can I generate a file? So I used a relative path method: Unpacking path + own relative path, perfect solution to the path problem

Files: Statistics file number, get file information, get file content; folder: From the idea of preserving multilevel directory relationship, to keep the path information (not the original path information, but the full path information after unpacking), the folder content is 0, the content of the file is retrieved, traverse all children + Parent file number, File information, etc.

Traverse all subdirectories of a folder:

1 voidGetallsubfiles (stringPath, vector<string>& files)//get all sub-files2 {3     Longhfile =0;4     struct_finddata_t FileInfo;5     stringp;6     if(hfile = _findfirst (p.assign (Path). Append ("\\*"). C_str (), &fileinfo))! =-1)7     {8          Do9         {Ten             if((Fileinfo.attrib &_a_subdir)) One             { A                 if(strcmp (Fileinfo.name,".") !=0&& strcmp (Fileinfo.name,"..") !=0) -                 { -Files.push_back (p.assign (Path). Append ("\\"). Append (Fileinfo.name)); theGetallsubfiles (p.assign (Path). Append ("\\"). Append (Fileinfo.name), files); -                 } -             } -             Else +             { -Files.push_back (p.assign (Path). Append ("\\"). Append (Fileinfo.name)); +             } A  at} while(_findnext (hfile, &fileinfo) = =0); -  - _findclose (hfile); -     } -}

Package Files section:

1 BOOLPackfileanddirectory (Constvector<string>& files,Constvector<string>& directories,Const string& OutputFileName)//Packaging: The process of writing a file2 {3FILE *WFP =NULL; 4WFP = fopen (Outputfilename.c_str (),"WB");5     if(WFP = =NULL)6     {7cout <<"Package: File open failed! "<<Endl;8     }9 Ten     /************************ First parameter: file vector**********************/ One  A     intFileCount =files.size (); -Fwrite (&filecount,sizeof(FileCount),1, WFP); -  the      for(size_t i =0; i < FileCount; i++) -     { -         structFileInfo file; -  +         stringPath = Files[i];//double slash path name -         stringfilename = getfilenamefrompath (path);//get file names based on path + strcpy (file. FileName, Filename.c_str ()); AFile.filenamelen =strlen (Filename.c_str ()); at  - ifstream fin; - fin.open (path, ios::binary); -  -FIN.SEEKG (0, ios::end); -Streampos SP =Fin.tellg (); inFile.filesize = SP;//size of File -  toFwrite (&file,sizeof(file),1, WFP); +cout <<"FileName:"<< file. FileName <<"; Filenamelength:"<< File.filenamelen <<"; FileSize:"<< file.filesize <<Endl; -  theFILE *files = fopen (Path.c_str (),"RB"); *Eachfile =NewUnsignedChar[file.filesize]; $ Filebuffer.push_back (eachfile);Panax NotoginsengFread (Filebuffer[i], file.filesize,1, files); -  theFwrite (Filebuffer[i], file.filesize,1, WFP); +     } A}

Today first said so much, packed inside there are a lot of small knowledge points, really to do will find, so must consolidate their foundation, as a small white + Cheng in the workplace, if there is a need to improve the views and views, but also hope that the IT community of the great God Enlighten, O (∩_∩) o~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.