This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add-Type @"
using System;
using System.IO;
using System.Runtime.InteropServices;
public class NTFSFileCompression
{
#pragma warning disable
[DllImport("kernel32.dll")]
public static extern bool DeviceIoControl(
IntPtr hDevice, uint dwIoControlCode, ref short lpInBuffer, uint nInBufferSize,
IntPtr lpOutBuffer, uint nOutBufferSize, ref uint lpBytesReturned,
IntPtr lpOverlapped);
private const uint FSCTL_SET_COMPRESSION = 0x0009C040;
private const short COMPRESSION_FORMAT_NONE = 0;
private const short COMPRESSION_FORMAT_DEFAULT = 1;
private static bool SetCompression(IntPtr handle, short compression)
{
uint lpBytesReturned = 0;
return DeviceIoControl(
handle, FSCTL_SET_COMPRESSION, ref compression, 2, IntPtr.Zero,
0, ref lpBytesReturned, IntPtr.Zero);
}
public static bool SetFileCompressed(string path)
{
using (FileStream fs = OpenFileStream(path))
{
return SetCompression(fs.Handle, COMPRESSION_FORMAT_DEFAULT);
}
}
public static bool SetFileUncompressed(string path)
{
using (FileStream fs = OpenFileStream(path))
{
return SetCompression(fs.Handle, COMPRESSION_FORMAT_NONE);
}
}
private static FileStream OpenFileStream(string path)
{
return File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
}
"@
[NTFSFileCompression]::SetFileCompressed([string]"aFile.txt")
C# Source: http://www.blackwasp.co.uk/FileCompression.aspx