• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 503
  • Last Modified:

how to set the compression attribute of a folder

I'm need to create a directory and set it as compressed.

I found this code but is working only on files (not folders)  - the CreateFile throw an error if the sFile is a folder...

thanks in advance
Andrea




Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize As Long, lpOutBuffer As Any, ByVal nOutBufferSize As Long, lpBytesReturned As Long, lpOverlapped As Any) As Long ' note lpOverlapped as Any so can use null
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Const FILE_READ_DATA = &H1
Const FILE_WRITE_DATA = &H2
Const OPEN_EXISTING = 3
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const FSCTL_SET_COMPRESSION As Long = &H9C040 ' This was the tricky one to figure out
Const COMPRESSION_FORMAT_NONE = 0
Const COMPRESSION_FORMAT_DEFAULT = 1

Private Sub Command1_Click()
Dim nHandle As Long
Dim nBytesReturned As Long
Dim nMode As Long
Dim nret As Long
Dim sfile As String

sfile = "C:\Documents and Settings\Andrea\Desktop\TEST\"
nHandle = CreateFile(sfile, FILE_READ_DATA Or FILE_WRITE_DATA, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, ByVal 0&)
If Err.LastDllError <> 0 Then MsgBox "error"

nMode = COMPRESSION_FORMAT_DEFAULT
nret = DeviceIoControl(nHandle, FSCTL_SET_COMPRESSION, nMode, Len(nMode), ByVal 0&, 0&, nBytesReturned, ByVal 0&)
If nret = 0 Then MsgBox "error"
CloseHandle (nHandle)

End Sub
0
RandomResearchLtd
Asked:
RandomResearchLtd
  • 4
  • 2
1 Solution
 
aacoolCommented:
You don't really need all that code.

In C#, you can do this:

public static void Main(string[] args)
{
            Console.WriteLine("Enter directory name:");
            string dirStr = Console.ReadLine();
            System.IO.DirectoryInfo dir = System.IO.Directory.CreateDirectory(dirStr);
            System.IO.FileAttributes fAttr = FileAttributes.Compressed | FileAttributes.Directory;
            dir.Attributes = fAttr;
            Console.ReadLine();
}

That could be done in VB.NET if you needed to.

Hope this helps
0
 
RandomResearchLtdAuthor Commented:
Thank You for the answer, but it seems to don't work ..

It works for standard attributes like hidden, but it seems to ignore the compressioe attribute.

0
 
RandomResearchLtdAuthor Commented:
Looking at the source code with Anacrino I found that the Attributes property of the DirectoryInfo type calls the win32 api SetFileAttributes that ignore the FILE_ATTRIBUTE_COMPRESSED ...
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
RandomResearchLtdAuthor Commented:
This is working on files and dirs:

Public Class NtfsCompressClass
    Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Integer, ByVal dwIoControlCode As Integer, ByRef lpInBuffer As Short, ByVal nInBufferSize As Integer, ByVal lpOutBuffer As IntPtr, ByVal nOutBufferSize As Integer, ByRef lpBytesReturned As Integer, ByVal lpOverlapped As IntPtr) As Integer
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As Integer, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Integer
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer


    Private Const FILE_FLAG_BACKUP_SEMANTICS As Integer = &H2000000
    Private Const FILE_ATTRIBUTE_COMPRESSED As Integer = &H800S
    Private Const FILE_READ_DATA As Integer = &H1
    Private Const FILE_WRITE_DATA As Integer = &H2
    Private Const FILE_DEVICE_FILE_SYSTEM As Integer = &H9
    Private Const METHOD_BUFFERED As Integer = 0
    Private Const FILE_ANY_ACCESS As Integer = 0
    Private Const COMPRESSION_FORMAT_NONE As Integer = 0
    Private Const COMPRESSION_FORMAT_DEFAULT As Integer = 1
    Private Const FSCTL_SET_COMPRESSION As Integer = 639040
    Private Const GENERIC_READ As Integer = &H80000000
    Private Const GENERIC_WRITE As Integer = &H40000000
    Private Const GENERIC_EXECUTE As Integer = &H20000000
    Private Const GENERIC_ALL As Integer = &H10000000
    Private Const FILE_SHARE_READ As Integer = &H1
    Private Const FILE_SHARE_WRITE As Integer = &H2
    Private Const FILE_SHARE_DELETE As Integer = &H4
    Private Const CREATE_NEW As Integer = 1
    Private Const CREATE_ALWAYS As Integer = 2
    Private Const OPEN_EXISTING As Integer = 3
    Private Const OPEN_ALWAYS As Integer = 4
    Private Const TRUNCATE_EXISTING As Integer = 5
    Private Const INVALID_HANDLE_VALUE As Integer = -1



    Public Shared Sub Compress(ByVal sPath As String)
        Dim lpBytesReturned As Integer = 0
        Dim nRet As Integer
        Dim nHandle As Integer


        nHandle = CreateFile(sPath, FILE_READ_DATA Or FILE_WRITE_DATA, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)
        If nHandle = INVALID_HANDLE_VALUE Then Throw New Exception(ErrorToString(Err.LastDllError))

        Try
            nRet = DeviceIoControl(nHandle, FSCTL_SET_COMPRESSION, COMPRESSION_FORMAT_DEFAULT, 2, IntPtr.Zero, 0, lpBytesReturned, IntPtr.Zero)
            If nRet = 0 Then Throw New Exception(ErrorToString(Err.LastDllError))
        Finally
            nRet = CloseHandle(nHandle)
        End Try

    End Sub
   
End Class
0
 
armoghanCommented:
Instead of using CreateFileA, use CreateDirectoryA

soemthing liek
http://custom.programming-in.net/articles/art9-1.asp?f=CreateDirectory
and pass the security arguments to it
0
 
RandomResearchLtdAuthor Commented:
How can I close the question ?

I don't know how to set it at solved..
0
 
armoghanCommented:
You can let the moderator close it , or you can press the accept button present in front of any comment
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now