Jump to content
Sign in to follow this  
Der schöne Günther

How to wait for a file compression to finish?

Recommended Posts

NTFS allows files to be compressed. When a directory is compressed, that just means its files automatically inherit the "compressed" attribute.

 

You can use WinApi.Windows.GetCompressedFileSize(..) to find about how many bytes a file actually uses on disk. When compressed, it is less than the actual file content.#

 

My "problem": When a file is written into a directory with the "compressed" attribute set, it will first get written without compression, then Windows will take care of compression in the background. While I query the "true" file size with GetCompressedFileSize(..), I can see it shrinking. I would like to find out when the compression has finished. Is it possible?

 

In case someone wants to test a running Demo, here is the source for a console application:

https://gist.github.com/JensMertelmeyer/eb238ce57f8bb6cbb1ef3514c3d58ae8

Share this post


Link to post

Well, if you call GetCompressedFilesize repeatedly and the returned size does not change for a certain time anymore the compression is finished. :classic_biggrin:.

 

Since the compression runs in a different process (service) completely separate from yours I see no way to get informed directly. There is ReadDirectoryChangesW, which allows you to monitor the folder for changes in a file size, but that would probably have the same problem you are having now: you don't know if the change notification is the last one you will get for the file.

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×