Compressed File (Attributes) Program Example
The following program example demonstrates some of the compressed file 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.
#include <windows.h>
#include <stdio.h>
int wmain(int argc, WCHAR *argv[])
{
BOOL bRetVal;
// Make sure the file is exist
LPCTSTR lpFileName = L\\\\?\\C:\\WINDOWS\\ie7\\iexplore.chm;
// LPCTSTR lpFileName = L\\\\?\\C:\\amad\\AddUserEncryptFile\\Debug\\testfilencrypt.doc;
WIN32_FILE_ATTRIBUTE_DATA data;
GET_FILEEX_INFO_LEVELS fInfoLevelId = GetFileExInfoStandard;
SYSTEMTIME times, stLocal;
DWORD dwFileSize = 0;
// File less than 4GB does not need this
LPDWORD lpFileSizeHigh = NULL;
bRetVal = GetFileAttributesEx(lpFileName, fInfoLevelId, &data);
if(bRetVal == 0)
wprintf(LGetFileAttributesEx() pretty damn failed, error %u\n, GetLastError());
else
{
wprintf(LGetFileAttributesEx() is working!\n);
// dwFileSize is the low-order DWORD of the actual number
// of bytes of disk storage used to store the specified file
dwFileSize = GetCompressedFileSize(lpFileName, lpFileSizeHigh);
wprintf(LFile size is %u bytes\n, dwFileSize);
FileTimeToSystemTime(&data.ftLastAccessTime, ×);
SystemTimeToTzSpecificLocalTime(NULL, ×, &stLocal);
wprintf(LLast accessed: %02d/%02d/%04d %02d:%02d:%02d\n, stLocal.wDay , stLocal.wMonth,
stLocal.wYear, stLocal.wHour, stLocal.wMinute, stLocal.wSecond);
// The value is bitwise AND lol
wprintf(Ldata.dwFileAttributes \'sum\' value is %u\n, data.dwFileAttributes);
// Let play around with some of the attributes
if(data.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)
wprintf(LThe file is compressed!\n);
else
wprintf(LThe file is not compressed!\n);
if(data.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)
wprintf(LThe file is an archive!\n);
else
wprintf(LThe file is not an archive!\n);
if(data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
wprintf(LThe file is hidden!\n);
else
wprintf(LThe file is not hidden!\n);
if(data.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
wprintf(LThe file is read only!\n);
else
wprintf(LThe file is not read only!\n);
if(data.dwFileAttributes & FILE_ATTRIBUTE_ENCRYPTED)
wprintf(LThe file is an encrypted!\n);
else
wprintf(LThe file is not encrypted!\n);
}
// You may want to try other attributes:
// data.ftCreationTime;
// data.ftLastWriteTime;
// data.nFileSizeHigh;
// data.nFileSizeLow;
// Close handle
return 0;
}
Build and run the project. The following screenshot is a sample output.
From the file property's page, we can see that the size on disk. Click the Advanced button for other information.
Well, the file is a compressed file (blue in colour). Both file size and compressed attribute should confirm our program output.