Zip file compression and decompression (MFC file operation 4)

Source: Internet
Author: User
Tags uncompress

1. compressed files

 

To use zip compression for our program, we need to add several zip files to the project.

 

Zlib. h zconf. h zlib. lib these can download http://d.download.csdn.net/down/2344459/mryeze

 

Add two. H files to project in the program. Then declare to introduce lib

# Include "zlib. h" // File compression <br/> # include "zconf. h" <br/> # pragma comment (lib, "zlib. lib ")

 

This is only to enable compress to use key functions in the program.

Then we wrote our code. The source code was found on the Internet. When we were doing it, we made some modifications to it so that it could run directly.

 

HANDLE hFile, hFileToWrite; </p> <p> CString strFilePath = "C:/aaa.txt "; // path of the file to be compressed <br/> // The suffix of the downloaded source code is rar, failed to open </p> <p> // open the file to be compressed <br/> hFile = CreateFile (strFilePath, // file name <br/> GENERIC_READ, // open for reading <br/> file_assist_read, // share for reading <br/> NULL, // no security <br/> OPEN_EXISTING, // existing file only <br/> FILE_ATTRIBUTE_NORMAL, // normal file <br/> NULL <br/>); // no Ttr. template <br/> if (hFile = INVALID_HANDLE_VALUE) <br/>{< br/> AfxMessageBox ("cocould not open file to read "); // process error <br/> return; <br/>}</p> <p> HANDLE hMapFile, hMapFileToWrite; <br/> // create a file ing <br/> hMapFile = CreateFileMapping (hFile, // Current file handle. <br/> NULL, // Default security. <br/> PAGE_READONLY, // Read/write permission. <br/> 0, // Max. object size. <br/> 0, // Size of hFil E. <br/> "zipteappsappingobjectforread" <br/>); // Name of mapping object. <br/> if (hMapFile = NULL) <br/>{< br/> AfxMessageBox ("cocould not create file mapping object"); <br/> return; <br/>}</p> <p> LPVOID lpMapAddress, lpMapAddressToWrite; <br/> // create a file ing view as source <br/> lpMapAddress = MapViewOfFile (hMapFile, // Handle to mapping object. <br/> FILE_MAP_READ, // Read/write permission <br/> 0, // Max. ob Ject size. <br/> 0, // Size of hFile. <br/> 0); // Map entire file. <br/> if (lpMapAddress = NULL) <br/>{< br/> AfxMessageBox ("cocould not map view of file"); <br/> return; <br/>}</p> <p> DWORD dwFileLength, dwFileLengthToWrite; <br/> dwFileLength = GetFileSize (hFile, NULL ); // obtain the file size </p> <p> // m_dwSourceFileLength = dwFileLength; </p> <p> // because the output buffer of the compression function must be 0.1% + 12 larger than the input, a DWORD is used to save the size before compression, <br/> // used for decompression. Of course, it can be saved More information is stored. <br/> dwFileLengthToWrite = (double) dwFileLength * 1.001 + 12 + sizeof (DWORD) is not used here ); </p> <p> // create a file to save the compressed file <br/> hFileToWrite = CreateFile ("C:/demoFile.txt ", // demoFile.rar <br/> GENERIC_WRITE | GENERIC_READ, // open for writing <br/> 0, // do not share <br/> NULL, // no security <br/> CREATE_ALWAYS, // overwrite existing <br/> FILE_ATTRIBUTE_NORMAL, // normal file <br/> NULL); // no attr. temp Late <br/> if (hFileToWrite = INVALID_HANDLE_VALUE) <br/> {<br/> AfxMessageBox ("cocould not open file to write "); // process error <br/> return; <br/>}</p> <p> // create a file ing for the output file <br/> hMapFileToWrite = CreateFileMapping (hFileToWrite, // Current file handle. <br/> NULL, // Default security. <br/> PAGE_READWRITE, // Read/write permission. <br/> 0, // Max. object size. <br/> dwFileLengthToWrite, // Size of hFile. <Br/> "zipteappsappingobjectforwrite"); // Name of mapping object. <br/> if (hMapFileToWrite = NULL) <br/> {<br/> AfxMessageBox ("cocould not create file mapping object for write"); <br/> return; <br/>}</p> <p> // create a file ing view for the output file <br/> lpMapAddressToWrite = MapViewOfFile (hMapFileToWrite, // Handle to mapping <br/> FILE_MAP_WRITE, // Read/write permission 0, // Max. object size. <br/> 0, // Size of hFile. <br /> 0, <br/> 0 <br/>); // Map entire file. <br/> if (lpMapAddressToWrite = NULL) <br/> {<br/> AfxMessageBox ("cocould not map view of file"); <br/> return; <br/>}</p> <p> // Save the size before compression in the first DWORD of the file. <br/> LPVOID pBuf = lpMapAddressToWrite; <br/> (* (DWORD *) pBuf) = dwFileLength; <br/> pBuf = (DWORD *) pBuf + 1; </p> <p> // This is the most important thing. zlib provides a method to compress the data cached by the source to the destination cache. <br/> // The prototype is as follows: <br/> // int compress (Bytef * dest, ulogfn * DestLen, const Bytef * source, uLong sourceLen); <br/> // The destLen parameter returns the size of the compressed file. <Br/> compress (Bytef *) pBuf, & dwFileLengthToWrite, (Bytef *) lpMapAddress, dwFileLength); </p> <p> UnmapViewOfFile (lpMapAddress ); <br/> CloseHandle (hMapFile); <br/> CloseHandle (hFile); <br/> UnmapViewOfFile (lpMapAddressToWrite); <br/> CloseHandle (hMapFileToWrite ); </p> <p> // reset the file size here <br/> SetFilePointer (hFileToWrite, dwFileLengthToWrite + sizeof (DWORD), NULL, FILE_BEGIN ); <br/> SetEndOfFile (hFileToWrite); <br/> CloseHandle (hFileToWrite );

 

After running the program, the aaa.txt file is compressed successfully.

 

 

 

Ii. decompress the file

 

The idea of extracting files is similar to that of compressing files. The difference is

1. Set the buffer size of the output file

Zip:

// Because the output buffer of the compression function must be 0.1% + 12 larger than the input, and then a DWORD is used to save the size before compression. <br/> // used for decompression, of course, more information can be saved. <br/> dwFileLengthToWrite = (double) dwFileLength * 1.001 + 12 + sizeof (DWORD) is not used here );

 

Upzip is:

DwFileLengthToWrite = (* (DWORD *) lpMapAddress );

 

2. compress and uncompress Functions

This is required!

Compress (Bytef *) pBuf, & dwFileLengthToWrite, (Bytef *) lpMapAddress, dwFileLength );

 

Uncompress (Bytef *) pBuf, & dwFileLengthToWrite, (Bytef *) pSourceBuf, dwFileLength );

 

Provide source code

HANDLE hFile, hFileToWrite; <br/> CString strFilePath = "C:/demoFile.txt "; <br/> // open the file to be decompressed <br/> hFile = CreateFile (strFilePath, // file name <br/> GENERIC_READ, // open for reading <br/> file_assist_read, // share for reading <br/> NULL, // no security <br/> OPEN_EXISTING, // existing file only <br/> FILE_ATTRIBUTE_NORMAL, // normal file <br/> NULL <br/>); // no attr. template <br/> if (hFile = INVALID_HA NDLE_VALUE) <br/>{< br/> AfxMessageBox ("cocould not open file to read"); // process error <br/> return; <br/>}< br/> HANDLE hMapFile, hMapFileToWrite; <br/> // create a file ing <br/> hMapFile = CreateFileMapping (hFile, // Current file handle. <br/> NULL, // Default security. <br/> PAGE_READONLY, // Read/write permission. <br/> 0, // Max. object size. <br/> 0, // Size of hFile. <br/> "zipteappsappingobjectforread ");// Name of mapping object. <br/> if (hMapFile = NULL) <br/>{< br/> AfxMessageBox ("cocould not create file mapping object"); <br/> return; <br/>}< br/> LPVOID lpMapAddress, lpMapAddressToWrite; <br/> // create a file ing view as source <br/> lpMapAddress = MapViewOfFile (hMapFile, // Handle to mapping <br/> FILE_MAP_READ, // Read/write permission <br/> 0, // Max. object size. <br/> 0, // Size of hFile. <br/> 0); // Map entir E file. <br/> if (lpMapAddress = NULL) <br/>{< br/> AfxMessageBox ("cocould not map view of file"); <br/> return; <br/>}< br/> DWORD dwFileLength, dwFileLengthToWrite; <br/> dwFileLength = GetFileSize (hFile, NULL)-sizeof (DWORD ); <br/> // because the output buffer of the compression function must be 0.1% + 12 larger than the input, a DWORD is used to save the size before compression. <br/> // used for decompression, of course, more information can be saved. <br/> // dwFileLengthToWrite = (double) dwFileLength * 1.001 + 12 + sizeof (DWORD) is not used here. <br/> IleLengthToWrite = (* (DWORD *) lpMapAddress); <br/> LPVOID pSourceBuf = lpMapAddress; <br/> pSourceBuf = (DWORD *) pSourceBuf + 1; <br/> // create a file to save the compressed file <br/> hFileToWrite = CreateFile ("C:/UnZipFile.txt ", // create demo.gz <br/> GENERIC_WRITE | GENERIC_READ, // open for writing <br/> 0, // do not share <br/> NULL, // no security <br/> CREATE_ALWAYS, // overwrite existing <br/> FILE_ATTRIBUTE_NORMAL, // n Ormal file <br/> NULL <br/>); // no attr. template <br/> if (hFileToWrite = INVALID_HANDLE_VALUE) <br/>{< br/> AfxMessageBox ("cocould not open file to write "); // process error <br/> return; <br/>}< br/> hMapFileToWrite = CreateFileMapping (hFileToWrite, // Currentfile handle. <br/> NULL, // Default security. <br/> PAGE_READWRITE, // Read/write permission. <br/> 0, // Max. object size. <br/> dwFileLengthToWri Te, // Size of hFile. <br/> "zipteappsappingobjectforwrite"); // Name of mapping object. <br/> if (hMapFileToWrite = NULL) <br/> {<br/> AfxMessageBox ("cocould not create file mapping object for write"); <br/> return; <br/>}< br/> lpMapAddressToWrite = MapViewOfFile (hMapFileToWrite, // Handle to mapping object. <br/> FILE_MAP_WRITE, // Read/write permission <br/> 0, // Max. object size. <br/> 0, // Size of hF Ile. <br/> 0 <br/>); // Map entire file. <br/> if (lpMapAddressToWrite = NULL) <br/> {<br/> AfxMessageBox ("cocould not map view of file"); <br/> return; <br/>}< br/> // Save the size before compression in the first DWORD of the file. <br/> LPVOID pBuf = lpMapAddressToWrite; <br/> // here is the most important. zlib provides a method to compress the data cached by the source to the destination cache. <br/> // The original form is as follows: <br/> // int compress (Bytef * dest, ulogfn * destLen, const Bytef * source, uLong sourceLen); <br/> // The destLen parameter returns the actual The size of the compressed file. <Br/> uncompress (Bytef *) pBuf, & dwFileLengthToWrite, (Bytef *) pSourceBuf, dwFileLength); <br/> UnmapViewOfFile (lpMapAddress ); <br/> CloseHandle (hMapFile); <br/> CloseHandle (hFile); <br/> UnmapViewOfFile (lpMapAddressToWrite); <br/> CloseHandle (hMapFileToWrite ); <br/> // reset the file size here <br/> SetFilePointer (hFileToWrite, dwFileLengthToWrite, NULL, FILE_BEGIN); <br/> SetEndOfFile (hFileToWrite ); <br/> CloseHandle (hFileToWrite );

 

The running effect is as follows:

 

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.