is it possible to get the size of a zip file while it is being created? CreateEntryFromFile

1.6k views Asked by At

I would like to know if it is possible to check the size of the zip file that is being created dynamically, because I am reading a directory and generate a 19 MB zip and I would like two zips instead to be created, one 10MB and the other 9MB. However, when I give a .Length in the zip file inside my loop it says the size is 0. When I finish adding the files it says that is 19MB. Would anyone know how to do this?

I am using only System.IO.Compression to this task.

here is some example to show how I am trying

    String FilePath = "D:\Invoices";
    string[] oFiles = Directory.GetFiles(FilePath,"*.pdf");
    string name = DateTime.Now.Ticks.ToString()+".zip";
    using(FileStream archive1 = File.Open(name,FileMode.Create))
    {
       using(var arch = new ZipArchive(archive1,ZipArchiveMode.Update))
           {
            for(int i =0; i<oFiles.Length;i++)
               {
                var fileinf = new FileInfo(oFiles[i]);
                arch.CreateEntryFromFile(fileinf.FullName,fileinf.Name);
                //here the zip file is always 0 
                Console.WriteLine(archive1.Length);
                }
    
           }
    }
//here the zip file is updated 
1

There are 1 answers

2
Peter Duniho On

From the documentation:

When you set the mode to Update … The content of the entire archive is held in memory, and no data is written to the underlying file or stream until the archive is disposed.

If you want to be able to read the size of the file as you're adding things, you need to use ZipArchiveMode.Create.

Otherwise, you should use the ZipArchiveEntry.CompressedSize property to monitor how much you've added to the archive.