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

NTFS file compression

I need to compress a file on an NTFS volume using NT's compression attributes. It can't be done by changing the compressed attribute (2048). I've found API calls CreateFile and DeviceIoControl can do the trick, but I cant get them to work correctly.
0
pokrakam
Asked:
pokrakam
  • 3
  • 3
  • 2
1 Solution
 
MikeP090797Commented:
Which one doesn't work?
0
 
pokrakamAuthor Commented:
I got this line of C code which apparently does the trick:
bRet = DeviceIoControl(hFile, FSCTL_SET_COMPRESSION, &uMode, sizeof(uMode), NULL, 0, &dwAttributes, NULL)
Can't getit to work with VB as:
nRet = DeviceIoControl(nHandle, &HD0003, nMode, 4&, 0&, 4&, 0&, 0&)
Tried various combinations of parameters.
0
 
MikeP090797Commented:
Maybe just write a dll and call it from vb
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
alamoCommented:
That's not a very good answer - with a few rare exceptions if it can be done from a DLL it can be done from VB.

pokrakam, I just figured out how to do it - I'll post the code as soon as I can post it as an answer (i.e. as soon as you reject the current one). What you had is close, who knows, maybe by the time you read this you might have figured it out. (If so, please give me a chance to post the answer anyway and get the points, since I spent a lot of time figuring this one out). Thanks!
0
 
pokrakamAuthor Commented:
alamo's comment was about the most useful answer thus far, so I'll reopen the question.
0
 
alamoCommented:
Thanks pokrakam. For completeness I'll include the declares and definitions I am using, even though you probably have most of them already.

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

Dim nHandle As Long
Dim nBytesReturned As Long
Dim nMode As Long

nHandle = CreateFile("d:\path\file", 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 GoTo ShowErr ' error handler

nMode = COMPRESSION_FORMAT_DEFAULT
nRet = DeviceIoControl(nHandle, FSCTL_SET_COMPRESSION, nMode, Len(nMode), ByVal 0&, 0&, nBytesReturned, ByVal 0&)
If nRet = 0 Then GoTo ShowErr ' error handler
CloseHandle (nHandle)

Leave a comment if you have any problems with this, but I suspect  it'll work fine for you. Good luck!
0
 
pokrakamAuthor Commented:
Sorry about delay - 'puter got trashed, back online now.
All workes fine - I'd love to know where you got
FSCTL_SET_COMPRESSION As Long = &H9C040
from???

0
 
alamoCommented:
I computed FSCTL_SET_COMPRESSION from the header file definitions. I did however, compute it the first time incorrectly (as &HD0003, same as you). Having seen &HD0003 in your post I didn't question my initial computation and  wasted 2-3 hours trying things. Then I went back and double-checked and found that in:

#define CTL_CODE( DeviceType, Function, Method, Access )
(((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | Method))

the order of the arguments and the order they are used are different, the middle two are swapped. If you don't notice they are swapped you get &HD003. When you do eventually notice you get
the right answer... oh well.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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