Skip to content

Instantly share code, notes, and snippets.

@JensMertelmeyer
Created June 27, 2019 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JensMertelmeyer/eb238ce57f8bb6cbb1ef3514c3d58ae8 to your computer and use it in GitHub Desktop.
Save JensMertelmeyer/eb238ce57f8bb6cbb1ef3514c3d58ae8 to your computer and use it in GitHub Desktop.
// JCL_DEBUG_EXPERT_GENERATEJDBG OFF
// JCL_DEBUG_EXPERT_INSERTJDBG OFF
program Project6;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.IoUtils,
WinApi.Windows;
procedure createCompressedDirectory(out path: String);
const
COMPRESSION_FORMAT_DEFAULT: DWORD = 1;
var
directoryHandle: THandle;
bytesReturned: DWORD;
begin
path := TPath.Combine( TPath.GetTempPath(), '{9F2D14A1-DE08-488F-BE40-69D51605D2EE}' );
if not TDirectory.Exists(path) then
TDirectory.CreateDirectory(path);
directoryHandle := CreateFile(
PChar(path),
GENERIC_WRITE or GENERIC_READ,
0, //dwShareMode
nil, // security attributes
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
0
);
Win32Check( directoryHandle <> INVALID_HANDLE_VALUE );
try
if not DeviceIoControl(
directoryHandle,
FSCTL_SET_COMPRESSION,
Addr(COMPRESSION_FORMAT_DEFAULT),
SizeOf(COMPRESSION_FORMAT_DEFAULT),
nil,
0,
bytesReturned,
nil
) then
RaiseLastOSError();
finally
CloseHandle(directoryHandle);
end;
end;
procedure createNewFile(
const directory: String;
out filePath: String
);
const
_4MB = 1024 * 1024 * 4;
var
bytes: TBytes;
begin
SetLength(bytes, _4MB);
FillChar(bytes[0], _4MB, 0);
filePath := TPath.Combine(directory, TPath.GetRandomFileName());
TFile.WriteAllBytes(filePath, bytes);
end;
function getCompressedFileSize(const filePath: String): Int64;
var
lowDword, highDword: DWORD;
errorCode: DWORD;
begin
lowDword := WinApi.Windows.GetCompressedFileSize(
PChar(filePath),
Addr(highDword)
);
if (lowDword = INVALID_FILE_SIZE) then
begin
errorCode := GetLastError();
if (errorCode <> NO_ERROR) then
RaiseLastOSError();
end;
Result := Int64(lowDword) or Int64(highDword shl 32);
end;
var
directoryPath: String;
filePath: String;
begin
createCompressedDirectory(directoryPath);
try
createNewFile(directoryPath, filePath);
WriteLn( 'Compressed Size: ', getCompressedFileSize(filePath) );
WriteLn('Wait for a few seconds, then press Enter...'); readln;
WriteLn( 'Compressed Size: ', getCompressedFileSize(filePath) );
finally
if TDirectory.Exists(directoryPath) then
TDirectory.Delete(directoryPath, True);
end;
Readln;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment