How I handel long operation?

Dani_S 3,686 Reputation points
2023-03-21T12:08:02.4266667+00:00

Hi,

I unzip big files(20G) => ExtractToDirectory ,the action not finish with big files, how is can be done properly?

private static void UnZipFile(string source, string directory)
{
var guid = Guid.NewGuid().ToString();
var tempFolder = Path.Combine(directory, guid);
Task task = Task.Run(async () =>
{
System.IO.Compression.ZipFile.ExtractToDirectory(source, tempFolder); // the action not finish with big files
await Task.Delay(15000);// itried to put delay

}).ContinueWith(t =>
{
foreach (var filePath in Directory.GetFiles(tempFolder, ".", SearchOption.AllDirectories))
{
var routeDirectory = Path.Combine(directory, new FileInfo(filePath).Name);
if (File.Exists(routeDirectory))
{
var newPathForRootfolder = GetFileRenamed(new FileInfo(routeDirectory));
File.Move(filePath, Path.Combine(directory, newPathForRootfolder.Name));
}
else
{
File.Move(filePath, Path.Combine(directory, new FileInfo(filePath).Name));
}
}
Directory.Delete(tempFolder);
File.Delete(source);

});
task.Wait();

}

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,819 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,501 Reputation points Microsoft Vendor
    2023-03-28T09:26:22.7566667+00:00

    @דניאל שטרית, thanks for the feedback, based on my test, I find use ZipArchiveEntry.ExtractToFile method will be faster than the ZipFile.ExtractToDirectory method.

    Here is a code example you could refer to.

     static void Main(string[] args)
            {
                string zipapth = @"B:\Test.zip";
                string destination1 = @"B:\a";
                string destination2 = @"B:\b";
                Stopwatch stopwatch1=new Stopwatch();
                Stopwatch stopwatch2= new Stopwatch();
                stopwatch1.Start();
                FirstMethod(zipapth, destination1);
                stopwatch1.Stop();
                stopwatch2.Start();
                SecondMethod(zipapth, destination2);
                stopwatch2.Stop();
                Console.WriteLine( stopwatch1.ElapsedMilliseconds);
                Console.WriteLine(  stopwatch2.ElapsedMilliseconds);
                Console.ReadKey();
    
    
            }
            static void FirstMethod(string zippath,string destination)
            {
             
              ZipFile.ExtractToDirectory(zippath,destination);
            }
            public static void SecondMethod(string sourceArchive, string destinationDirectoryName)
            {
                if (!Directory.Exists(destinationDirectoryName))
                {
                    Directory.CreateDirectory(destinationDirectoryName);
                }
                var entries = ZipFile.Open(sourceArchive, ZipArchiveMode.Read).Entries;
                foreach (ZipArchiveEntry entry in entries)
                {
                    string path = Path.Combine(destinationDirectoryName, entry.Name);
                    entry.ExtractToFile(path);
                }
            }
    

    Result:

    User's image

    As the result is shown, the second method is faster than the first method.

    Hope my solution could help you.

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.