The Windows File Management 9

 

 

 

 

 

File Compression and Decompression Libraries

 

The file compression and decompression libraries take an existing file or files and produce a file or files that are compressed versions of the originals. The compression is also lossless, but the compression is not transparent to applications. An application can only operate on such files with the assistance of a file compression library. In addition, the only operations you can perform on such files are creating a compressed file from an original and recovering the original data from the decompressed version. Editing is typically not supported, and seeking is limited if supported at all. Typically, an application calls functions in LzExpand.dll to decompress data that was compressed using Compress.exe. The functions can also process files without attempting to decompress them. You can use the functions in LzExpand.dll to decompress single or multiple files. You can also use them to decompress compressed files a portion at a time.

 

Decompressing a Single File

 

An application can decompress a single compressed file by performing the following tasks:

  1. Open the source file by calling the LZOpenFile() function.
  2. Open the destination file by calling LZOpenFile().
  3. Copy the source file to the destination file by calling the LZCopy() function and passing the handles returned by LZOpenFile().
  4. Close the files by calling the LZClose() function.

 

Decompressing Multiple Files

 

An application can decompress multiple files by performing the following tasks:

  1. Open the source files by calling the LZOpenFile() function.
  2. Open the destination files by calling LZOpenFile().
  3. Copy the source files to the destination files by calling the LZCopy() function.
  4. Close the files by calling the LZClose() function.

 

Reading from Compressed Files

 

In addition to decompressing a complete file in a single operation, an application can decompress a compressed file a portion at a time by using the LZSeek() and LZRead() functions. These functions are particularly useful when it is necessary to extract parts of large files. For example, a font manufacturer may have compressed files containing font metrics in addition to character data. To use the information in these files, an application would need to decompress the file; however, most applications would use only part of the file at any particular time. To get information about font metrics, the application would extract data from the header. To get information from the text, the application would reposition the file pointer by calling LZSeek() and extract character data by calling LZRead().

 

Using Windows Compressed Functions Program Example

 

The following program example demonstrates some of Windows compressed functions.

Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.

Then, add the source file and give it a suitable name.

Next, add the following source code.

 

/*

How to use the LZOpenFile(), LZCopy(), and LZClose() functions from

the LZ Expand/Compress library (LZ32.dll). You can use these

functions to make a copy of an existing file.

The LZCopy function creates a decompressed destination file

if the source file is compressed with the

Microsoft File Compression Utility (Compress.exe).

*/

#include <windows.h>

#include <stdio.h>

 

// #pragma comment(lib, "LzExpand")

#pragma comment(lib, "LZ32")

 

int CopyFile(LPWSTR Source, LPWSTR Destination)

{

    INT hsource;

    INT hdest;

    DWORD iret;

    LPOFSTRUCT OpenStruct = {0};

 

      // Allocate buffer

      OpenStruct = (LPOFSTRUCT)malloc(sizeof(LPOFSTRUCT));

 

      if(OpenStruct != NULL)

            wprintf(L"malloc() - memory allocated!\n");

      else

            wprintf(L"malloc() - memory allocation failed! error %u\n", GetLastError());

 

    // Open the source and destination files.

    // Opens the file for reading only.

    hsource = LZOpenFile(Source, OpenStruct, OF_READ);

      if(hsource)

            wprintf(L"LZOpenFile() for reading compressed file looks OK!\n");

      else

            // Can't use GetLastError() lol

            wprintf(L"LZOpenFile() for reading compressed file failed miserably!\n");

 

    // Directs LZOpenFile to create a new file. If the file already exists,

    // it is truncated to zero length.

    hdest = LZOpenFile(Destination, OpenStruct, OF_CREATE);

     

      if(hdest)

            wprintf(L"LZOpenFile() for creating file looks OK!\n");

      else

            // Can't use GetLastError() lol

            wprintf(L"LZOpenFile() for creating file failed miserably!\n");

 

    // Copy the source file to the destination location, and

    // decompress the Source file if it was compressed.

    // Fail - returns less than 1

    // Success - returns the size, in bytes, of the destination file

    iret = LZCopy(hsource, hdest);

    // Close those files. This function does not return a value.

    LZClose(hdest);

    LZClose(hsource);

 

    if(iret == -1)

        wprintf(L"File transfer failed\n");

    else

        wprintf(L"Copy is successful: %u bytes were transferred.\n");

 

      // Failed to free the allocated buffer lol!

      // free(OpenStruct);

 

      // Let see what is returned

      return iret;

}

 

int wmain(int argc, WCHAR *argv[])

{

      LPWSTR pSourceFileName = NULL;

      LPWSTR pDestFileName = NULL;

      DWORD dwRetVal = 0;

 

      // Just a test. iexplore.chm is a compressed file

      pSourceFileName = L"\\\\?\\C:\\WINDOWS\\ie7\\iexplore.chm";

      // We uncompress and copy to C:

      pDestFileName = L"\\\\?\\C:\\iexplore.chm";

 

      // Call CopyFile()

      dwRetVal = CopyFile(pSourceFileName,pDestFileName);

      wprintf(L"CopyFile() returns %u\n", dwRetVal);

 

      return 0;

}

 

Build and run the project. The following screenshot is a sample output with an error.

 

Using Windows Compressed Functions Program Example: A sample console program output in action

 

Compress and uncompressed error output

 

Find the uncompressed file and open its property page. The size is matched with the program output.

 

Confirming the compressed file size

 

 

 

 

< Windows Files 8 | Win32 Programming | Win32 File Index | Windows Files 10 >