New Tip - ...set a file\'s compression state ?

  • From: "SwissDelphiCenter.ch" <newsletter@xxxxxxxxxxxxxxxxxxxx>
  • To: sdcnewtip@xxxxxxxxxxxxx
  • Date: Mon, 4 Mar 2002 15:08:41 +0100

* SwissDelphiCenter new Tip newsletter
* www.swissdelphicenter.ch
*
* To unsubscribe send a mail to sdcnewtip-request@xxxxxxxxxxxxx with the
* subject unsubscribe


Hallo,

Folgender neuer Tip ist neu auf SwissDelphiCenter verfügbar:

Besuchen Sie die Programmier Tips unter 
http://www.swissdelphicenter.ch/de/tipsindex.php


Bewerten Sie diesen Tip nach Schwierigkeitsgrad, Nützlichkeit und Gesamthaft auf
http://www.swissdelphicenter.ch/de/showcode.php?id=1053



Autor:
Primoz Gabrijelcic <>



-------------
...das Attribut \'komprimiert\' einer Datei setzen ?
-------------
Kategorie: Dateien



{
  To set a file's compression state, use the DeviceIoControl function with the
  FSCTL_SET_COMPRESSION operation.

  Call the following function with the name of the file to compress and
  boolean parameter 'forceCompress'. If that one is true, file will be 
compressed.
  If it is false, the file will be compressed only if its parent folder is
  compressed (reason for that parameter: if you MoveFile uncompressed file from
  uncompressed folder to compressed folder, file will not become automatically
  compressed - at least under some NT 4 service packs).

  Ein "compressed" Attribut kann man nicht mit der FileSetAttr Funktion setzen
  sondern muss DeviceIoControl Funktion mit dem flag FSCTL_SET_COMPRESSION 
verwenden:
}


const
  COMPRESSION_FORMAT_DEFAULT = 1;
  FILE_DEVICE_FILE_SYSTEM = 9;
  METHOD_BUFFERED = 0;
  FILE_READ_DATA = 1;
  FILE_WRITE_DATA = 2;
  FSCTL_SET_COMPRESSION = (FILE_DEVICE_FILE_SYSTEM shl 16) or
    ((FILE_READ_DATA or FILE_WRITE_DATA) shl 14) or (16 shl 2) or 
METHOD_BUFFERED;

function SetCompressedAttribut(FileName: PChar; forceCompress: Boolean): 
Boolean;
var
  hnd: Integer;
  Comp: SHORT;
  res: DWORD;
begin
  if forceCompress or ((GetFileAttributes(PChar(ExtractFilePath(FileName))) and
    FILE_ATTRIBUTE_COMPRESSED) <> 0) then
  begin
    Result := False;
    if (GetFileAttributes(FileName) and FILE_ATTRIBUTE_COMPRESSED) = 0 then
    begin
      hnd := CreateFile(FileName, GENERIC_READ, 0, nil, OPEN_EXISTING, 0, 0);
      try
        Comp := COMPRESSION_FORMAT_DEFAULT;
        if not DeviceIoControl(hnd, FSCTL_SET_COMPRESSION, @Comp,
          SizeOf(SHORT), nil, 0, res, nil) then Exit;
      finally
        CloseHandle(hnd);
      end;
    end;
    Result := True;
  end
  else
    Result := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
  begin
    SetCompressedAttribut(PChar(OpenDialog1.FileName), True);
  end;
end;





Best Regards
SwissDelphiCenter Team
www.swissdelphicenter.ch
[automatisch generierte EMail]


----------------------------------------------------
ENGLISH NEWSLETTER
----------------------------------------------------

Hi,

This new tip is new available on SwissDelphiCenter.ch:

Visit the programming tips at http://www.swissdelphicenter.ch/en/tipsindex.php


Rate this tip after skill, useful and overall at
http://www.swissdelphicenter.ch/en/showcode.php?id=1053



Author:
Primoz Gabrijelcic <>



-------------
...set a file\'s compression state ?
-------------
Category: Files



{
  To set a file's compression state, use the DeviceIoControl function with the
  FSCTL_SET_COMPRESSION operation.

  Call the following function with the name of the file to compress and
  boolean parameter 'forceCompress'. If that one is true, file will be 
compressed.
  If it is false, the file will be compressed only if its parent folder is
  compressed (reason for that parameter: if you MoveFile uncompressed file from
  uncompressed folder to compressed folder, file will not become automatically
  compressed - at least under some NT 4 service packs).

  Ein "compressed" Attribut kann man nicht mit der FileSetAttr Funktion setzen
  sondern muss DeviceIoControl Funktion mit dem flag FSCTL_SET_COMPRESSION 
verwenden:
}


const
  COMPRESSION_FORMAT_DEFAULT = 1;
  FILE_DEVICE_FILE_SYSTEM = 9;
  METHOD_BUFFERED = 0;
  FILE_READ_DATA = 1;
  FILE_WRITE_DATA = 2;
  FSCTL_SET_COMPRESSION = (FILE_DEVICE_FILE_SYSTEM shl 16) or
    ((FILE_READ_DATA or FILE_WRITE_DATA) shl 14) or (16 shl 2) or 
METHOD_BUFFERED;

function SetCompressedAttribut(FileName: PChar; forceCompress: Boolean): 
Boolean;
var
  hnd: Integer;
  Comp: SHORT;
  res: DWORD;
begin
  if forceCompress or ((GetFileAttributes(PChar(ExtractFilePath(FileName))) and
    FILE_ATTRIBUTE_COMPRESSED) <> 0) then
  begin
    Result := False;
    if (GetFileAttributes(FileName) and FILE_ATTRIBUTE_COMPRESSED) = 0 then
    begin
      hnd := CreateFile(FileName, GENERIC_READ, 0, nil, OPEN_EXISTING, 0, 0);
      try
        Comp := COMPRESSION_FORMAT_DEFAULT;
        if not DeviceIoControl(hnd, FSCTL_SET_COMPRESSION, @Comp,
          SizeOf(SHORT), nil, 0, res, nil) then Exit;
      finally
        CloseHandle(hnd);
      end;
    end;
    Result := True;
  end
  else
    Result := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
  begin
    SetCompressedAttribut(PChar(OpenDialog1.FileName), True);
  end;
end;




Best Regards
SwissDelphiCenter Team
www.swissdelphicenter.ch
[automatic generated EMail]

Other related posts:

  • » New Tip - ...set a file\'s compression state ?