NTFS Compression


Jump to: navigation, search



The standard and recommended file system of Windows NT and newer is NTFS. One of its many features is its support for transparent compression. See File Compression and Decompression at MSDN and NTFS on Wikipedia for more information.

Windows Update, for instance, compresses the backup folders and files for each update if the system drive is formatted with NTFS.

Inno Setup installers can do something similar. They can compress backup folders or log files, or even data files which are not used very often.

Before you decide to compress files or folders on the target system, you may however want to read the Microsoft article Best practices for NTFS compression in Windows.


Since NTFS compression is a standard Windows functionality it can be used by Inno Setup scripts.

Beginning with version 5.2.0, Inno Setup provides the flag setntfscompression for entries in the [Files] and [Dirs] sections. The following example shows a possible [Dirs] section that tells Inno Setup 5.2.0 and newer to use Windows' built-in NTFS compression. The flag is ignored on file systems that don't support transparent compression, like FAT and FAT32.

Name: "{app}\Data"; Flags: setntfscompression

For Inno Setup versions lower than 5.2.0 the game is still not lost.

Considerations for versions prior to 5.2.0

There are several ways of implementing Windows internal compression in Inno Setup. One method could be to convert the following C code to Pascal script:

BOOL DeviceIoControl(
  (HANDLE) hDevice,            // handle to file or directory
  FSCTL_SET_COMPRESSION,       // dwIoControlCode
  (LPVOID) lpInBuffer,         // input buffer
  (DWORD) nInBufferSize,       // size of input buffer
  NULL,                        // lpOutBuffer
  0,                           // nOutBufferSize
  (LPDWORD) lpBytesReturned,   // number of bytes returned
  (LPOVERLAPPED) lpOverlapped  // OVERLAPPED structure

The above code has been taken from FSCTL_SET_COMPRESSION on MSDN.

Another method is to put the painful C stuff in an extension DLL and simply invoke the required function from an Inno Setup [Code] section. This is what Andrew McLean has already done for us (http://news.jrsoftware.org/news/innosetup/msg35585.html).

DLL Implementation

1. The DLL can be downloaded from here: NTFScompress.zip

2. Extract the file NTFScompress.dll into the same directory where your .iss (Inno Setup script) file is.

3. Make the following line the first line in your [Files] section:

Source: "NTFScompress.dll"; Flags: dontcopy

That means your [Files] section should start like this:

Source: "NTFScompress.dll"; Flags: dontcopy

4. Add the compression function's declaration to your [Code] section.

function NTFSsetCompression (const FileName: string; const State: ShortInt): Boolean;
 external 'NTFSsetCompression@files:NTFScompress.dll stdcall';

It can be called like

NTFSsetCompression (ExpandConstant ('{app}\mytext.txt', 1));

from an AfterInstall () function in any [Files] entry.

The values for 'State' are:

  • 0 = No compression
  • 1 = Default NTFS compression
  • 2 = LZNT1 compression

NTFS currently only supports LZNT1, so the values 1 and 2 are actually identical. Or, as Microsoft say it (excerpt from FSCTL_SET_COMPRESSION at MSDN):

The LZNT1 compression algorithm is the only compression algorithm implemented. As a result, the LZNT1 compression algorithm is used as the DEFAULT compression method. Future releases may have additional compression methods which may be used as the DEFAULT.

As mentioned earlier, the DLL is not required for Inno Setup version 5.2.0 and newer. However, NTFScompress.dll can be used from other applications as well.


Although the documentation for the FSCTL_SET_COMPRESSION control code states that the minimum operating system is Windows 2000 Professional it works on Windows NT as well - at least with the latest service pack.

See also

External Links

Personal tools