strange results with compression

Gentlefolk

I am seeing some strange (to me) results with file compression in ntfs.

If I create a new file, set the end of file to one megabyte, then set the
compression format to default, then close the file - I get a file “size” 1MB
and “size on disk” 4KB. If I create a new file, set the compression format
to defauly, then set the end of file to 1MB, then close the file - I get a
file “size” 1MB and “size on disk” 1MB. (There is some daft user mode code
at the end of this post.) I sort of expected the same result in either case.

I was wondering if anyone can tell me if this is expected or even correct,
and if so why?

Thanks in advance
Lyndon

HANDLE hFile1, hFile2;
USHORT Format = COMPRESSION_FORMAT_DEFAULT;
DWORD dummy;

hFile1 = CreateFile(“C:\Flibble1”,
GENERIC_ALL,
SHARE_ALL,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);

SetFilePointer(hFile1, M, NULL, FILE_BEGIN);
SetEndOfFile(hFile1);
DeviceIoControl(hFile1, FSCTL_SET_COMPRESSION, &Format, sizeof(USHORT),
NULL, 0, &dummy, NULL);
(void) CloseHandle(hFile1);

hFile2 = CreateFile(“C:\Flibble2”,
GENERIC_ALL,
SHARE_ALL,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);

DeviceIoControl(hFile2, FSCTL_SET_COMPRESSION, &Format, sizeof(USHORT),
NULL, 0, &dummy, NULL);
SetFilePointer(hFile2, M, NULL, FILE_BEGIN);
SetEndOfFile(hFile2);
(void) CloseHandle(hFile2);