Home > Mobile >  Is it possible to extract files from TZipFile using multiple threads?
Is it possible to extract files from TZipFile using multiple threads?

Time:08-25

I was wondering if it was possible to extract multiple files simultaneously from a TZipFile. I'm using Delphi 11.

I've had a bit of a play around with no luck. I was thinking something along the lines of, which doesn't work.

  TParallel.For(1, 0, z.FileCount-1,
    procedure(i : integer)
    begin
      TTask.Run(
        procedure
        begin
          z.Extract(i, 'c:\temp\Unzip');

          TThread.Synchronize(nil,
            procedure
            begin
              Memo1.Lines.Add(z.FileNames[i]);
            end
          );
        end
      );
    end
  );

CodePudding user response:

I was wondering if it was possible to extract multiple files simultaneously from a TZipFile.

No. TZipFile does not support parallel extraction of the compressed files.

Most notable issue that prevents parallel extraction comes from FStream and FFileStream fields. Delphi streams don't support parallel access and even if they would support it, you wouldn't get any speed up in extracting operations, because any kind of operation in progress on stream would need to block other operations until the current one is complete.

Streams are state-full instances and every operation on a stream changes internal stream state (current stream position). Even read operations change the state and that is what makes streams thread-unsafe and that unsafety impacts TZipFile class as well.

Of course, there may be other sources of thread unsafety in the TZipFile class, but once you have one thing that is not safe and cannot be easily changed (fixed), there is no point in searching for other potential issues.

  • Related