Solved

NTFS file compression

Posted on 1997-11-11
8
491 Views
Last Modified: 2008-03-04
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
Comment
Question by:pokrakam
  • 3
  • 3
  • 2
8 Comments
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1440461
Which one doesn't work?
0
 

Author Comment

by:pokrakam
ID: 1440462
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
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1440463
Maybe just write a dll and call it from vb
0
 
LVL 6

Expert Comment

by:alamo
ID: 1440464
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:pokrakam
ID: 1440465
alamo's comment was about the most useful answer thus far, so I'll reopen the question.
0
 
LVL 6

Accepted Solution

by:
alamo earned 150 total points
ID: 1440466
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
 

Author Comment

by:pokrakam
ID: 1440467
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
 
LVL 6

Expert Comment

by:alamo
ID: 1440468
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now