C ++ file packaging tool implementation

Source: Internet
Author: User
Tags fread

Let's write a packaging tool. Many of them are found on the network, but I just merged the changes.

// Packetfile. cpp: defines the entry point of the console application.

# Include "stdafx. H"
# Include "serchallfile. H"
# Include "mycab. H"

Int _ tmain (INT argc, _ tchar * argv [])
{
Vector <string> vec_file;
Vector <string >:: iterator itemfilename;
Char filepacketpath [max_path] = "E: \ huangyushi \ Test Written instance ";
Char fileputpath [max_path] = "D: \ fasdfasdf \ My. Cab ";
Cserchallfile: getinstance ()-> get_filelist (filepacketpath, vec_file );

// -------------------- Packaging process --------------------
// Declare the object
Cmycab MC;
// Set the output file
MC. setoutputfile (fileputpath );
// Add the file to be packaged
For (itemfilename = vec_file.begin (); itemfilename <vec_file.end (); itemfilename ++)
{
MC. AddFile (* itemfilename). c_str ());
}
// Perform Packaging
MC. domakecab ();

// -------------------- Solution Process --------------------
// Declare the object
Cmycab UMC;
// Unpackage
UMC. douncab (fileputpath );

Cin. Clear ();
Cin. Get ();

Return 0;

}

Bytes -----------------------------------------------------------------------------------------------------------------------

To package the files under a file, all I need to do is traverse the files in the file and record all the files (through get_filelist () we can get the corresponding file path and save it to a vector)

# Pragma once

# Include <string>
# Include <vector>
# Include <fstream>
# Include <windows. h>
# Include <iostream>
# Include <conio. h>

Using namespace STD;

Class cserchallfile
{
Public:
Cserchallfile (void );
~ Cserchallfile (void );
Static cserchallfile * getinstance ();

Public:
/* Void find (char * lppath );
Void _ Find (string path );*/

Void get_filelist (char * foldname, vector <string> & filelist );
Void readfile (const char * filename );
Void writefile (string filename );
};

# Include "stdafx. H"
# Include "serchallfile. H"

Static cserchallfile * instance;

Cserchallfile: cserchallfile (void)
{
}

Cserchallfile ::~ Cserchallfile (void)
{
}

Cserchallfile * cserchallfile: getinstance ()
{
If (instance = NULL)
{
Instance = new cserchallfile ();
}

Return instance;
}

// Traverse the list of file names under a folder (including nested folders)
Void cserchallfile: get_filelist (char * foldname, vector <string> & filelist)
{
Handle hfind;
Win32_find_data filedata;
String line;
Char FN [max_path];
Char tmpfn [max_path];
Strcpy (FN, foldname );

// Process the string of the folder name
If (FN [strlen (FN)-1]! = '\\')
{
Strcat (FN ,"\\");
}

// Pay attention to the order. At this time, FN has been added "\\"
Strcpy (tmpfn, FN );
// If this parameter is not added, an error occurs!
Strcat (FN ,"*");

Hfind = findfirstfile (FN, & filedata );
Findnextfile (hfind, & filedata );
While (findnextfile (hfind, & filedata ))
{
// If the scan result is a folder
If (filedata. dwfileattributes & file_attribute_directory)
{
If (filedata. cfilename [0]! = '.')
{
Char szfile [max_path];
Strcpy (szfile, tmpfn );
Strcat (szfile, filedata. cfilename );
Get_filelist (szfile, filelist );
}
}
// Scan the file
Else
{
Line = (string) tmpfn;
Line + = filedata. cfilename;
/* If (line. Find (". H", 0 )! = String: NPOs)
{
Filelist. push_back (line );
}
Else
{
Continue;
}*/
Filelist. push_back (line );
}
// Cout <line <Endl;
}
}

Bytes ---------------------------------------------------------------------------------------------------------------------

# Pragma once

# Include <iostream>
# Include <stdio. h>
# Include <stdlib. h>
# Include <memory. h>
# Include <string. h>
# Include <error. h>
# Include <direct. h>
Using namespace STD;

// Maximum number of packaged files
# Define max_file_count 1024
// Maximum path character Length
# Define max_path 260

// File Header
Struct filehead
{
Unsigned int filecount; // number of files
Unsigned int filelen [max_file_count]; // File Size
Char filename [max_file_count] [max_path]; // file name
};

Class cmycab
{
PRIVATE:
Filehead FH; // File Header
Char objectfilepathname [max_path]; // generate the location and name of the package file

Public:
Cmycab (void );
~ Cmycab (void );

// Add a file to the package
Void AddFile (const char * filepathname );
// Set the packaging output file
Void setoutputfile (char * OUTFILE );
// Get the file size (Pass in the file pointer opened in binary mode)
Long getfilesize (File * PF );
// Create a packaging File
Void domakecab ();
// Unpackage (to save time and avoid writing error handling, you can add it as needed)
Void douncab (char * cabfilepathname );

PRIVATE:
// Display package file information
Void printcab ();
// Create a folder
Void checktargetpath (string targetpath );
};

# Include "stdafx. H"
# Include "mycab. H"

Cmycab: cmycab (void)
{
Memset (& FH, 0x0, sizeof (FH ));
Memset (objectfilepathname, 0x0, sizeof (objectfilepathname ));
}

Cmycab ::~ Cmycab (void)
{
}

// Add a file to the package
Void cmycab: AddFile (const char * filepathname)
{
If (FH. filecount> = max_file_count-1)
{
Cout <"supports up to" <max_file_count <"Files" <Endl;
Return;
}
Strcpy (FH. filename [FH. filecount], filepathname );
FH. filecount ++;
}

// Set the packaging output file
Void cmycab: setoutputfile (char * OUTFILE)
{
Memset (objectfilepathname, 0x0, sizeof (objectfilepathname ));
Strcpy (objectfilepathname, OUTFILE );
}

// Get the file size (Pass in the file pointer opened in binary mode)
Long cmycab: getfilesize (File * PF)
{
// Move the pointer to the end of the file
Fseek (PF, 0,/* seek_end */2 );
Return ftell (PF );
}

// Create a packaging File
Void cmycab: domakecab ()
{
If (FH. filecount <1)
{
Cout <"no file added to packaging" <Endl;
Return;
}
If (strlen (objectfilepathname) <1)
{
Cout <"the output location of the package file is not specified" <Endl;
Return;
}

File * poutfile = NULL;
File * pworkfile = NULL;

// Obtain the size of all objects
For (INT I = 0; I <FH. filecount; I ++)
{
Pworkfile = fopen (FH. filename [I], "rb ");
If (null = pworkfile)
{
Cout <"file:" <FH. filename [I] <"cannot read [" <strerror (errno) <"]" <Endl;
Return;
}
FH. filelen [I] = getfilesize (pworkfile );
Fclose (pworkfile );
}

// Check whether a folder exists
Checktargetpath (objectfilepathname );
// Start to merge and write files
Poutfile = fopen (objectfilepathname, "WB ");
If (null = poutfile)
{
Cout <"failed to create output file [" <strerror (errno) <"]" <Endl;
Return;
}

// Write the file header
Fwrite (& FH, sizeof (FH), 1, poutfile );
// Write each file
For (INT I = 0; I <FH. filecount; I ++)
{
Unsigned char * ptmpdata = NULL;
Pworkfile = fopen (FH. filename [I], "rb ");
If (null = pworkfile)
{
Cout <"file:" <FH. filename [I] <"cannot read [" <strerror (errno) <"]" <Endl;
Fclose (pworkfile );
Fclose (poutfile );
Return;
}
Ptmpdata = new unsigned char [FH. filelen [I];
Fread (ptmpdata, FH. filelen [I], 1, pworkfile );
If (ferror (pworkfile ))
{
Cout <"file:" <FH. filename [I] <"cannot read [" <strerror (errno) <"]" <Endl;
Fclose (pworkfile );
Fclose (poutfile );
Return;
}
Fwrite (ptmpdata, FH. filelen [I], 1, poutfile );
If (ferror (poutfile ))
{
Cout <"file:" <objectfilepathname <"cannot be written to [" <strerror (errno) <"]" <Endl;
Fclose (pworkfile );
Fclose (poutfile );
Return;
}
Delete [] ptmpdata;
Fclose (pworkfile );
}

Fclose (poutfile );
Cout <"package completed" <Endl;
}

// Unpackage (to save time and avoid writing error handling, you can add it as needed)
Void cmycab: douncab (char * cabfilepathname)
{
File * pcab = NULL;
File * pwork = NULL;

Pcab = fopen (cabfilepathname, "rb ");

// Read the file header
Memset (& FH, 0x0, sizeof (FH ));
Fread (& FH, sizeof (FH), 1, pcab );

Printcab ();

// Put all files to the current directory.
For (INT I = 0; I <FH. filecount; I ++)
{
Unsigned char * ptmpdata = NULL;
Ptmpdata = new unsigned char [FH. filelen [I];
Fread (ptmpdata, FH. filelen [I], 1, pcab );
// Only get the file name. Do not generate the file path name.
Char tmpfilename [max_path];
String STR = "e :\\ huangyushi \ Test Written instance \\";
String AAA;
AAA. Assign (FH. filename [I], strlen (FH. filename [I]);
Const char * chaaaaa = aaa. Replace (0, str. Length (), "\"). c_str ();
Char ptmpc [max_path];
Strcpy (ptmpc, chaaaaa );

Memset (tmpfilename, 0x0, sizeof (tmpfilename ));
Strcpy (tmpfilename, ptmpc + 1 );
// Obtain the path of the cab file
Char tmppathname [max_path];
Memset (tmppathname, 0x0, sizeof (tmppathname ));
Strcpy (tmppathname, cabfilepathname );
Char * tnmpc = tmppathname + strlen (tmppathname );
While ('\\'! = * Tmpc)
{
Tmpc --;
}
Tmpc ++;
* Tmpc = '\ 0 ';
Strcat (tmppathname, tmpfilename );

Pwork = fopen (tmppathname, "WB ");
If (pwork = NULL)
{
Checktargetpath (tmppathname );
Pwork = fopen (tmppathname, "WB ");
}
Fwrite (ptmpdata, FH. filelen [I], 1, pwork );
Fclose (pwork );
Delete [] ptmpdata;
}

Fclose (pcab );
}

// Display package file information
Void cmycab: printcab ()
{
Cout <"File Information:" <Endl;
Cout <"Total number of files:" <FH. filecount <Endl;
For (INT I = 0; I <FH. filecount; I ++)
{
Cout <FH. filename [I] <"\ t" <FH. filelen [I] <"Byte" <Endl;
}
}

// Create a folder
Void cmycab: checktargetpath (string targetpath)
{
// Log & log = Log: getlog ("Main", "checktargetpath ");
Int e_pos = targetpath. Length ();
Int f_pos = targetpath. Find ("\", 0 );
String subdir;
Do
{
E_pos = targetpath. Find ("\", f_pos + 2 );
If (e_pos! =-1)
{
Subdir = targetpath. substr (0, e_pos );
If (_ mkdir (subdir. c_str () = 0)
Printf ("creat success % s", subdir. c_str ());
Else
Printf ("creat fail % s", subdir. c_str ());
}
F_pos = e_pos;
} While (f_pos! =-1 );
}

Related Article

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.