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

Using the code in the link below it is possible to observe that during compression or decompression, the file is locked by one of the following processes: 'explorer' (if compression or decompression is started automatically), 'compact' (if compression or decompression is started using the compact.exe command).

When the compression or decompression is finished, the process lock ('explorer' or 'compact') is removed.

Note: the code is in c#.

 

http://csharphelper.com/blog/2017/01/see-processes-file-locked-c/

Edited by f.m

Share this post


Link to post

Thanks, I will try that. I didn't notice the file was supposed to be locked, I think it could still be moved around in explorer while compression was taking place but I might be wrong.

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  

×