Meshbeyn / Projects

namespace NTFSExtensions

public static class Compression


Compress or decompress any data stream on NTFS volume.



this FileSystemInfo FileOrFolder
A FileSystemInfo object as a reference to file or folder. You may call this method directly from this object, because this is an extension method.
this FileStream File
A FileStream object as a reference to opened stream. You may call this method directly from this object, because this is an extension method.
SafeFileHandle FileStream
Handle to opened stream. You can become this handle from file stream object or using WinAPI function CreateFile.
bool Compressed
Set or unset the Compressed attribute


Using the SetCompressedAttribute method you can compress or decompress any unlocked file on NTFS volume. Unlike most other attributes, the "compressed" attribute is not just a flag in MFT record. During changing the value of this attribute, file system driver shall rewrite the whole stream as compressed or uncompressed data. This operation takes a lot of time, so it is not so lightweight as change the "Read Only" attribute. You read the "Compressed" attribute like any other NTFS attribute, but to change this attribute, a function call is needed.

Attribute changing will fail if you have not sufficient access rights, if file is in use or if the file system driver cannot find enough free clusters. Very big files (30GB) cannot be compressed.

If you change this attribute for a folder, nothing is happened instantly. But the new files become this attribute from the folder by default. If you want to change this attribute for existing files in folder, you should recursively set the attribute value for each file and subfolder.

All this methods call WinAPI function, that needs a file handle. So, the most fast and safe overload is the one with SafeFileHandle.


Example: working with compressed streams

See also