Using Sparse Files sample
Sparse files
are only supported under Windows 2000 and later and the file must be on a volume that is NTFS 5.0 or later.
MSDN links: The code below sets the SPARSE attribute and zeros all the data in the file. $SAMPLECODE$
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 CLEAR #DEFINE OPEN_EXISTING 3 #DEFINE GENERIC_WRITE 1073741824 && 0x40000000 #DEFINE INVALID_HANDLE_VALUE -1 #define FILE_ANY_ACCESS 0 #define FILE_WRITE_ACCESS 0x00000002 #define FILE_DEVICE_FILE_SYSTEM 0x00000009 #define FSCTL_SET_SPARSE BITLSHIFT(FILE_DEVICE_FILE_SYSTEM, 16) + ; BITLSHIFT(FILE_ANY_ACCESS,14) + BITLSHIFT(49, 2) #define FSCTL_SET_ZERO_DATA BITLSHIFT(FILE_DEVICE_FILE_SYSTEM, 16) + ; BITLSHIFT(FILE_WRITE_ACCESS,14) + BITLSHIFT(50, 2) = DeclareAPI() lcFileName = "H:\tmp\Copy1.dbf" + CHR(0) hFile = CreateFile(lcFileName , GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0) IF hFile == INVALID_HANDLE_VALUE ? "Error Opening file", Apierror(GetLastError()) RETURN .F. ENDIF ? "File handle returned: ", hFile lnSize = GetFileSize(hFile, 0) lnCompSize = GetCompressedFileSize(lcFileName , 0) ? "Size before", lnSize, lnCompSize lnBytesReturned = 0 lnResult = DeviceIoControl(hFile, FSCTL_SET_SPARSE, Null,0,0,0, @lnBytesReturned,0) IF lnResult = 0 ? "Set Sparse Error", Apierror(GetLastError()) RETURN .F. ENDIF lcFILE_ZERO_DATA_INFORMATION = Num2Long(0) + Num2Long(0) + Num2Long(lnSize) + Num2Long(0) lnBytesReturned = 0 lnResult = DeviceIoControl(hFile, FSCTL_SET_ZERO_DATA, ; @lcFILE_ZERO_DATA_INFORMATION, 16,0,0, @lnBytesReturned,0) IF lnResult = 0 ? "Zero Data Error", Apierror(GetLastError()) RETURN .F. ENDIF lnSize = GetFileSize(hFile, 0) lnCompSize = GetCompressedFileSize(lcFileName , 0) ? "Size after", lnSize, lnCompSize = CloseHandle (hFile) RETURN *---------------------------------------------------------------- FUNCTION Num2LOng(tnNum) DECLARE RtlMoveMemory IN WIN32API AS RtlCopyLong ; STRING @Dest, Long @Source, Long Length LOCAL lcString lcString = SPACE(4) =RtlCopyLong(@lcString, BITOR(tnNum,0), 4) RETURN lcString FUNCTION Apierror(tnErrorCode) Local lcErrBuffer, lcErrorMess, liNewErr, lnErrorCode lcErrBuffer = REPL(CHR(0),1000) lnErrorCode = tnErrorCode liNewErr = FormatMessage(0x1000; ,.NULL., lnErrorCode, 0, @lcErrBuffer,500,0) lcErrorMess = Transform(lnErrorCode) + " " + LEFT(lcErrBuffer, AT(CHR(0),lcErrBuffer)- 1 ) RETURN lcErrorMess FUNCTION DeclareAPI DECLARE Long DeviceIoControl IN kernel32 ; Long hDevice, Long dwIoControlCode, ; String @ lpInBuffer, Long nInBufferSize, ; Long lpOutBuffer, Long nOutBufferSize, ; Long @ lpBytesReturned, Long lpOverlapped DECLARE Long GetLastError IN kernel32 Declare Long FormatMessage In kernel32.dll as FormatMessage ; Long dwFlags, String @lpSource, Long dwMessageId, ; Long dwLanguageId, String @lpBuffer, Long nSize, ; Long Arguments DECLARE Long CreateFile IN kernel32 ; STRING lpFileName, Long dwDesiredAccess, Long dwShareMode, ; Long lpSecurityAttr, Long dwCreationDisp, Long dwFlagsAndAttrs, ; Long hTemplateFile DECLARE Long CloseHandle IN kernel32 Long hObject DECLARE Long GetFileSize IN kernel32 ; Long hFile , Long @ lpFileSizeHigh DECLARE Long GetCompressedFileSize IN kernel32 ; String FileName , Long @ lpFileSizeHigh RETURN
Comments