1313using System . Windows . Forms ;
1414using Vanara . PInvoke ;
1515using Windows . System ;
16+ using Windows . Win32 ;
17+ using Windows . Win32 . Foundation ;
18+ using Windows . Win32 . Storage . FileSystem ;
19+ using static Vanara . PInvoke . Kernel32 ;
20+ using COMPRESSION_FORMAT = Windows . Win32 . Storage . FileSystem . COMPRESSION_FORMAT ;
21+ using HRESULT = Vanara . PInvoke . HRESULT ;
22+ using HWND = Vanara . PInvoke . HWND ;
1623
1724namespace Files . App . Helpers
1825{
@@ -324,7 +331,7 @@ public static string ExtractStringFromDLL(string file, int number)
324331 }
325332
326333 if ( iconData is not null || iconOptions . HasFlag ( IconOptions . ReturnThumbnailOnly ) )
327- return iconData ;
334+ return iconData ;
328335 else
329336 {
330337 var shfi = new Shell32 . SHFILEINFO ( ) ;
@@ -333,7 +340,7 @@ public static string ExtractStringFromDLL(string file, int number)
333340 // Cannot access file, use file attributes
334341 var useFileAttibutes = iconData is null ;
335342
336- var ret = Shell32 . SHGetFileInfo ( path , isFolder ? FileAttributes . Directory : 0 , ref shfi , Shell32 . SHFILEINFO . Size , flags ) ;
343+ var ret = Shell32 . SHGetFileInfo ( path , isFolder ? FileAttributes . Directory : 0 , ref shfi , Shell32 . SHFILEINFO . Size , flags ) ;
337344 if ( ret == IntPtr . Zero )
338345 return iconData ;
339346
@@ -884,24 +891,24 @@ private static Process CreatePowershellProcess(string command, PowerShellExecuti
884891 public static SafeFileHandle CreateFileForWrite ( string filePath , bool overwrite = true )
885892 {
886893 return new SafeFileHandle ( Win32PInvoke . CreateFileFromApp ( filePath ,
887- Win32PInvoke . GENERIC_WRITE , 0 , IntPtr . Zero , overwrite ? Win32PInvoke . CREATE_ALWAYS : Win32PInvoke . OPEN_ALWAYS , ( uint ) Win32PInvoke . File_Attributes . BackupSemantics , IntPtr . Zero ) , true ) ;
894+ ( uint ) FILE_ACCESS_RIGHTS . FILE_GENERIC_WRITE , 0 , IntPtr . Zero , overwrite ? Win32PInvoke . CREATE_ALWAYS : Win32PInvoke . OPEN_ALWAYS , ( uint ) Win32PInvoke . File_Attributes . BackupSemantics , IntPtr . Zero ) , true ) ;
888895 }
889896
890897 public static SafeFileHandle OpenFileForRead ( string filePath , bool readWrite = false , uint flags = 0 )
891898 {
892899 return new SafeFileHandle ( Win32PInvoke . CreateFileFromApp ( filePath ,
893- Win32PInvoke . GENERIC_READ | ( readWrite ? Win32PInvoke . GENERIC_WRITE : 0 ) , ( uint ) ( Win32PInvoke . FILE_SHARE_READ | ( readWrite ? 0 : Win32PInvoke . FILE_SHARE_WRITE ) ) , IntPtr . Zero , Win32PInvoke . OPEN_EXISTING , ( uint ) Win32PInvoke . File_Attributes . BackupSemantics | flags , IntPtr . Zero ) , true ) ;
900+ ( uint ) FILE_ACCESS_RIGHTS . FILE_GENERIC_READ | ( uint ) ( readWrite ? FILE_ACCESS_RIGHTS . FILE_GENERIC_WRITE : 0u ) , ( uint ) ( Win32PInvoke . FILE_SHARE_READ | ( readWrite ? 0 : Win32PInvoke . FILE_SHARE_WRITE ) ) , IntPtr . Zero , Win32PInvoke . OPEN_EXISTING , ( uint ) Win32PInvoke . File_Attributes . BackupSemantics | flags , IntPtr . Zero ) , true ) ;
894901 }
895902
896903 public static bool GetFileDateModified ( string filePath , out FILETIME dateModified )
897904 {
898- using var hFile = new SafeFileHandle ( Win32PInvoke . CreateFileFromApp ( filePath , Win32PInvoke . GENERIC_READ , Win32PInvoke . FILE_SHARE_READ , IntPtr . Zero , Win32PInvoke . OPEN_EXISTING , ( uint ) Win32PInvoke . File_Attributes . BackupSemantics , IntPtr . Zero ) , true ) ;
905+ using var hFile = new SafeFileHandle ( Win32PInvoke . CreateFileFromApp ( filePath , ( uint ) FILE_ACCESS_RIGHTS . FILE_GENERIC_READ , Win32PInvoke . FILE_SHARE_READ , IntPtr . Zero , Win32PInvoke . OPEN_EXISTING , ( uint ) Win32PInvoke . File_Attributes . BackupSemantics , IntPtr . Zero ) , true ) ;
899906 return Win32PInvoke . GetFileTime ( hFile . DangerousGetHandle ( ) , out _ , out _ , out dateModified ) ;
900907 }
901908
902909 public static bool SetFileDateModified ( string filePath , FILETIME dateModified )
903910 {
904- using var hFile = new SafeFileHandle ( Win32PInvoke . CreateFileFromApp ( filePath , Win32PInvoke . FILE_WRITE_ATTRIBUTES , 0 , IntPtr . Zero , Win32PInvoke . OPEN_EXISTING , ( uint ) Win32PInvoke . File_Attributes . BackupSemantics , IntPtr . Zero ) , true ) ;
911+ using var hFile = new SafeFileHandle ( Win32PInvoke . CreateFileFromApp ( filePath , ( uint ) FILE_ACCESS_RIGHTS . FILE_WRITE_ATTRIBUTES , 0 , IntPtr . Zero , Win32PInvoke . OPEN_EXISTING , ( uint ) Win32PInvoke . File_Attributes . BackupSemantics , IntPtr . Zero ) , true ) ;
905912 return Win32PInvoke . SetFileTime ( hFile . DangerousGetHandle ( ) , new ( ) , new ( ) , dateModified ) ;
906913 }
907914
@@ -935,10 +942,64 @@ public static bool UnsetFileAttribute(string lpFileName, FileAttributes dwAttrs)
935942 return Win32PInvoke . SetFileAttributesFromApp ( lpFileName , lpFileInfo . dwFileAttributes & ~ dwAttrs ) ;
936943 }
937944
945+ public static unsafe bool CanCompressContent ( string path )
946+ {
947+ path = Path . GetPathRoot ( path ) ?? string . Empty ;
948+ uint dwFileSystemFlags = 0 ;
949+
950+ var success = PInvoke . GetVolumeInformation (
951+ path ,
952+ null ,
953+ 0u ,
954+ null ,
955+ null ,
956+ & dwFileSystemFlags ,
957+ null ,
958+ 0u ) ;
959+
960+ if ( ! success )
961+ return false ;
962+
963+ return ( dwFileSystemFlags & PInvoke . FILE_FILE_COMPRESSION ) != 0 ;
964+ }
965+
966+ public static unsafe bool SetCompressionAttributeIoctl ( string lpFileName , bool isCompressed )
967+ {
968+ // GENERIC_READ | GENERIC_WRITE flags are needed here
969+ // FILE_FLAG_BACKUP_SEMANTICS is used to open directories
970+ using var hFile = PInvoke . CreateFile (
971+ lpFileName ,
972+ ( uint ) ( FILE_ACCESS_RIGHTS . FILE_GENERIC_READ | FILE_ACCESS_RIGHTS . FILE_GENERIC_WRITE | FILE_ACCESS_RIGHTS . FILE_WRITE_ATTRIBUTES ) ,
973+ FILE_SHARE_MODE . FILE_SHARE_READ | FILE_SHARE_MODE . FILE_SHARE_WRITE ,
974+ lpSecurityAttributes : null ,
975+ FILE_CREATION_DISPOSITION . OPEN_EXISTING ,
976+ FILE_FLAGS_AND_ATTRIBUTES . FILE_ATTRIBUTE_NORMAL | FILE_FLAGS_AND_ATTRIBUTES . FILE_FLAG_BACKUP_SEMANTICS ,
977+ hTemplateFile : null ) ;
978+
979+ if ( hFile . IsInvalid )
980+ return false ;
981+
982+ var bytesReturned = 0u ;
983+ var compressionFormat = isCompressed
984+ ? COMPRESSION_FORMAT . COMPRESSION_FORMAT_DEFAULT
985+ : COMPRESSION_FORMAT . COMPRESSION_FORMAT_NONE ;
986+
987+ var result = PInvoke . DeviceIoControl (
988+ new ( hFile . DangerousGetHandle ( ) ) ,
989+ PInvoke . FSCTL_SET_COMPRESSION ,
990+ & compressionFormat ,
991+ sizeof ( ushort ) ,
992+ null ,
993+ 0u ,
994+ & bytesReturned ) ;
995+
996+ return result ;
997+ }
998+
938999 public static string ReadStringFromFile ( string filePath )
9391000 {
9401001 IntPtr hFile = Win32PInvoke . CreateFileFromApp ( filePath ,
941- Win32PInvoke . GENERIC_READ ,
1002+ ( uint ) FILE_ACCESS_RIGHTS . FILE_GENERIC_READ ,
9421003 Win32PInvoke . FILE_SHARE_READ ,
9431004 IntPtr . Zero ,
9441005 Win32PInvoke . OPEN_EXISTING ,
@@ -987,7 +1048,7 @@ public static string ReadStringFromFile(string filePath)
9871048 public static bool WriteStringToFile ( string filePath , string str , Win32PInvoke . File_Attributes flags = 0 )
9881049 {
9891050 IntPtr hStream = Win32PInvoke . CreateFileFromApp ( filePath ,
990- Win32PInvoke . GENERIC_WRITE , 0 , IntPtr . Zero , Win32PInvoke . CREATE_ALWAYS , ( uint ) ( Win32PInvoke . File_Attributes . BackupSemantics | flags ) , IntPtr . Zero ) ;
1051+ ( uint ) FILE_ACCESS_RIGHTS . FILE_GENERIC_WRITE , 0 , IntPtr . Zero , Win32PInvoke . CREATE_ALWAYS , ( uint ) ( Win32PInvoke . File_Attributes . BackupSemantics | flags ) , IntPtr . Zero ) ;
9911052 if ( hStream . ToInt64 ( ) == - 1 )
9921053 {
9931054 return false ;
0 commit comments