0

I have below section of code in my application. I would like to store Zipped file size in DB but when I store buffer value it is different from compressed zip file size.

How to identify exact Zip file size in C#?

using (ZipFile zip = new ZipFile())
{
    zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;

    foreach (Docs Docs in FileByteCollection)
    {
        doctype = "Insured";
        country = "US";

        if (doctype == "Local")
            Docs.DocumentName = "LocalDoc.pdf";

        else if (doctype != "Local")
            Docs.DocumentName = "Interantional.pdf"

        zip.AddEntry(Docs.DocName, Docs.DocumentContent);
    }

    zip.Save(outputMemStream);
}

Current logic to get FileSize:

byte[] byteArr = ((MemoryStream)outputMemStream).ToArray();

int totalSize = 0;

foreach (byte b in byteArr)
    totalSize = totalSize + b;
Tech Learner
  • 1,227
  • 6
  • 24
  • 59
  • Uh, how are you currently getting the file size? – ProgrammingLlama Mar 13 '18 at 06:03
  • @john - Sorry, I have included the logic now. – Tech Learner Mar 13 '18 at 06:09
  • @Alexei Levenkov - I want file size not count. – Tech Learner Mar 13 '18 at 06:22
  • @Ask_SO I mean... `byteArr.Length` is the size of your memory stream which presumably contains the Zip file... Since your sample code computes sum of all bytes it very hard to reason what you are actually trying to compute. I'm happy to re-open the question when you [edit] it to clarify what you need (as code shown in the post does not convey it) and why it is not the length of the array. – Alexei Levenkov Mar 13 '18 at 06:26
  • @AlexeiLevenkov - Thanks. I would like to identify total ZipFileSize before storing it to my local folder. – Tech Learner Mar 13 '18 at 06:28
  • I still don't get it, sorry stupid me. Please provide an example of data you want to get at the end. I.e. "size of first file 10000, second 20000, size of zip file on disk 1234 bytes (same as `byteArr.Length`). I'm looking for ?????" (my only other guess is you are looking for https://stackoverflow.com/questions/9458724/how-do-i-convert-from-long-value-to-kb-string-format) – Alexei Levenkov Mar 13 '18 at 06:32
  • `byteArr.Length` _is_ size of your zip file. – Evk Mar 13 '18 at 06:42
  • @Evk - Thanks. byteArr.Length resolves the issue. – Tech Learner Mar 13 '18 at 14:15

0 Answers0