Advertisement
dan-masek

Enable/Disable NTFS file compression programmatically

Oct 8th, 2019
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. import win32file
  2. import winioctlcon
  3. import win32con
  4. import struct
  5.  
  6. def set_file_compression(filename, state):
  7.     handle = win32file.CreateFile(filename
  8.         , win32file.GENERIC_READ | win32file.GENERIC_WRITE
  9.         , 0, None
  10.         , win32file.OPEN_EXISTING
  11.         , 0, None)
  12.     if state:
  13.         buffer = struct.pack('H', win32con.COMPRESSION_FORMAT_DEFAULT)
  14.     else:
  15.         buffer = struct.pack('H', win32con.COMPRESSION_FORMAT_NONE)
  16.  
  17.     win32file.DeviceIoControl(Device = handle
  18.         , IoControlCode = winioctlcon.FSCTL_SET_COMPRESSION
  19.         , InBuffer = buffer
  20.         , OutBuffer = None)
  21.  
  22.     win32file.CloseHandle(handle)
  23.  
  24. set_file_compression('test.dat', False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement