const
COMPRESSION_FORMAT_NONE = 0;
COMPRESSION_FORMAT_DEFAULT = 1;
COMPRESSION_FORMAT_LZNT1 = 2;
procedure SetCompressionAttribute(
const FileName:
string;
const CompressionFormat: USHORT);
const
FSCTL_SET_COMPRESSION = $9C040;
var
Handle: THandle;
Flags: DWORD;
BytesReturned: DWORD;
begin
if DirectoryExists(FileName)
then
Flags := FILE_FLAG_BACKUP_SEMANTICS
else if FileExists(FileName)
then
Flags := 0
else
raise Exception.CreateFmt('
%s does not exist', [FileName]);
Handle := CreateFile(PChar(FileName), GENERIC_READ
or GENERIC_WRITE, 0,
nil, OPEN_EXISTING, Flags, 0);
if Handle=0
then
RaiseLastOSError;
if not DeviceIoControl(
Handle, FSCTL_SET_COMPRESSION, @CompressionFormat, SizeOf(Comp),
nil, 0, BytesReturned,
nil)
then
begin
CloseHandle(
Handle);
RaiseLastOSError;
end;
CloseHandle(
Handle);
end;