0

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 
  • `CreateEntryFromFile` returns the entry. You could keep a running total of the compressed sizes of those as you add them. – Retired Ninja Sep 26 '20 at 01:10
  • Yes, I think about it. However, the file size after zipping gets smaller right? – Airton Pereira Sep 26 '20 at 01:25
  • I doubt the size of the final file is somehow less than the total of the compressed sizes of each entry. I'd expect it to be slightly larger to allow for bookkeeping and such. – Retired Ninja Sep 26 '20 at 01:27
  • The total size of my "Invoice" folder is 18MB and when it is zipped, the total is like 16MB. I am wondering if I can use a counter to sum all the normal size to do what I need. – Airton Pereira Sep 26 '20 at 01:44

1 Answers1

1

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.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • But is not possible to use this property because the zip is already in use. The process cannot access the file 'file.ext' because it is being used by another process. – Airton Pereira Sep 26 '20 at 02:22
  • You said you are trying to create two (or more) .zip files from the original. Given that, you might as just create all the new ones from scratch, reading from the original. If you don't want to do that, then just skip the first N entries (where N is however many you want to keep, based on the sum of the `CompressedSize` values), and then delete the remaining ones from the original as you write them to a new archive. For that matter, even if you are creating all new archives, you can just use `CompressedSize` to determine total size, just like I wrote in this answer. – Peter Duniho Sep 26 '20 at 02:26