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.