tsolomko / SWCompression

A Swift framework for working with compression, archives and containers.
MIT License
238 stars 41 forks source link

Progress when decompression is needed #27

Closed YevhenHerasymenko closed 3 years ago

YevhenHerasymenko commented 3 years ago

When decompress big files it takes a long time. Can you add progress callback to show that everything is okay?

tsolomko commented 3 years ago

Hmm, I am not sure about this.

As far as I understand you would like to have the following function added (as an example, I use Deflate here, but this also applies to other components):

extension Deflate {

    public static func decompress<Result>(data: Data, _ callback: ((???) -> Result)? = nil) throws -> Data {
        /* ... */ 
    }

}

The key question here is what should be the input type of the callback? What value should decompression function pass to the callback function to indicate the progress? Off the top of my head, I can think of two options, both vaguely defined:

  1. Some numerical value indicating how many "internal structures" (blocks, frames, streams, etc.) from the input data has been processed so far. However, in many cases it is impossible to tell ahead of time without processing the entire input, how many of these structures you have in total, and without that this progress value is even more meaningless.
  2. How many bytes of the input have been processed so far. While this is better defined, there is another problem: how often/at what points during the decompression should the callback function be called. Depending on the answer to this question, you may end up with callback function being called too often, which in turn will slow down the decompression even more.

Finally, I should point out that the phrase "show that everything is okay" is also hard to define, because in practice everything is okay by default, and if something is not, an error is thrown. In view of this, I personally think that the callback function is a bit redundant.