Decompress the GZ format in UNIX in C ++ in Windows

Source: Internet
Author: User

In the project, you have encountered a problem. In the project, you need to develop a PC tool (Windows Required). In this case, you need to package some files into a gz file, the gz file is a compressed file in the UNIX system. Later, I found the online resources and only decompressed the C ++ source code. I didn't tell you how to compress the file in the GZ format. Of course, you can also use the 7Z software to decompress the file in GZ. In this article, we will use another idea to decompress the GZ format.

 

First, create a C ++ project. Here we use the MFC form project.

 

The function is very simple. Let's take a look at the design of the entire form:

 

In the above row, select "select file" to display the file path in the following list, and then click "compress" to save it to the specified directory.

 

 

Select a file in the gz format in the following row, and click "decompress" to save the package to the specified directory. ,

 

 

This is the interface function. Next I will introduce some of the main core functions.

 

First, let's look at a processing class of GZ:

GZHelper. h header file

// Gzuncompress the class (executable files of mongotar.exe)
Class GZHelper
{
Public:
GZHelper ();
Virtual ~ GZHelper ();
Static void Compress (char * gzFilePath, int fileNum, char * file,...); // compression, (compressed package path, number of files, variable parameter file list)
Static void Compress (char * gzFilePath, int fileNum, char ** files); // compression (compressed package path, number of files, file list)
Static void Decompress (char * folderPath, char * gzFilePath); // Decompress (Decompress directory, compressed package path)

Private:
Static CString ConvertToUnix (CString winFile); // convert the path format on the Window to the UNIX path format
Static void FindFile (CString path, CString outPath); // traverses the Directory and moves all the files in the directory to the outPath
}; GZHelper. cpp

Void GZHelper: Compress (char * gzFilePath, int fileNum, char * file ,...)
{
Va_list argptr;

Va_start (argptr, gzFilePath );
Va_arg (argptr, int );
 
Char ** files;
Files = new char * [fileNum];
For (int index = 0; index <fileNum; index ++)
{
Char * file = va_arg (argptr, char *);
CString str_file;
Str_file = file;

Files [index] = new char [str_file.GetLength ()];
Memcpy (files [index], file, str_file.GetLength ());
Files [index] [str_file.GetLength ()] = 0;
}
Va_end (argptr );

Compress (gzFilePath, fileNum, files );
}

Void GZHelper: Compress (char * gzFilePath, int fileNum, char ** files)
{
CString str_gzFilePath (gzFilePath );
CString folderPath = str_gzFilePath.Left (str_gzFilePath.ReverseFind (\) + 1 );
CString command = "cd ";
Command = command + Path: StartupPath () + "tar &" + Path: GetDrive () + "& tar.exe zcPf ";
CString unix_str_gzfile = ConvertToUnix (str_gzFilePath );
Command = command + "" + unix_str_gzfile + """;
For (int index = 0; index <fileNum; index ++)
{
Char * file = files [index];
CString str_file;
Str_file = file;
CString unix_str_file = ConvertToUnix (str_file );
Command = command + "" + unix_str_file + """;
}
 
// Execute the command
System (command );
}

Void GZHelper: Decompress (char * folderPath, char * gzFilePath)
{
CString str_folderPath (folderPath );
CString str_gzFilePath (gzFilePath );
CString command = "cd ";
Command = command + Path: StartupPath () + "tar &" + Path: GetDrive () + "& tar.exe zxvf ";
CString unix_str_gzfile = ConvertToUnix (str_gzFilePath );
Command = command + "" + unix_str_gzfile + """;

System (command );

CString outPath = str_folderPath + "\ demo ";
CreateDirectory (outPath, NULL );

CString inPath = Path: StartupPath () + "tar \ cygdrive ";
GZHelper: FindFile (inPath, outPath );

RemoveDirectory (inPath );
}

// Convert the path in Windows to a UNIX path
CString GZHelper: ConvertToUnix (CString winFile)
{
CString unixFile;
UnixFile = winFile;
UnixFile. Replace ("\","/");
UnixFile = "/cygdrive/" + unixFile. Mid (0, 1) + unixFile. Mid (2, unixFile. GetLength ()-2 );
Return unixFile;
}

Void GZHelper: FindFile (CString path, CString outPath)
{
CString szDir = path + "\*.*";

CFileFind fileFind;
BOOL result = fileFind. FindFile (szDir );

While (result)
{
Result = fileFind. FindNextFile ();

If (fileFind. IsDots ())
Continue;

If (fileFind. IsDirectory ())
{
GZHelper: FindFile (fileFind. GetFilePath (), outPath );
}
Else
{
// Move the file
MoveFile (fileFind. GetFilePath (), outPath + "\" + fileFind. GetFileName ());
}
}

FileFind. Close ();
} In the code, we can see two methods: Compress and Decompress. Here is the core function.

In fact, the principle is to call a tar command package compiled in windows through the command prompt cmd on Windows. The contents of this package are as follows:

In fact, it uses the cygwin1.dll component to convert the tar command on UNIX to run on Windows.

This package will be provided at the end of the article together with the tool and source code.

We can see that we use the "cd" command in Compress. here we need to set the current cmd path to the path of a tar package in the application.

The "&" symbol can be executed in combination in a single command.

Note that the command here should be separated by "" in the string path to prevent the path in the string from including space characters.

The system function executes the cmd command.

In addition, we can see that the ConverToUnix function is used to convert the path in Windows to the virtual UNIX path in cygwin:

What does this mean? Now I open a cygwin.exe tool and execute the df command:

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.