This is a migrated thread and some comments may be shown as answers.

How to tell how big zip file will be

1 Answer 281 Views
ZipLibrary
This is a migrated thread and some comments may be shown as answers.
Randy Hompesch
Top achievements
Rank 1
Randy Hompesch asked on 17 May 2017, 10:50 AM

Hi,

I have a situation where I need to send a fairly large amount of data via zip files by email.

The company server limits emails to 4 mg in size, so I can't send one big zip file that, for example,  might be 10mg.

I need to:

1. create a zip file

2. add a file to it

3. test how big the zip file is.

4. if it's larger than 4 mg, back out the file I just added use the< 4mg file to attach to the email.

5. Lather, rinse, repeat until all files have been sent.

How do I tell what the current size of the zip file will be after adding a file to it but before I close the archive?

 

01.MailMessage mail;
02.long currentZipSize = 0;
03.int attIdx = 0;
04.// Get the selected attachments
05.var q = from a in db.Attachments
06.    where rptParms.AttachmentIds.Contains(a.AttachmentId)
07.    select a;
08.List<Models.Attachment> lstSel = q.ToList();
09.int i;
10.mail = new MailMessage();
11.using (MemoryStream ms = new MemoryStream())
12.{
13.using (ZipArchive zArchive = new ZipArchive(ms, ZipArchiveMode.Create, true))
14.{
15.    var lrEntry = zArchive.CreateEntry("LabReport.pdf", CompressionLevel.Fastest);
16.    using (BinaryWriter bw = new BinaryWriter(lrEntry.Open()))
17.    {
18.        bw.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
19.        bw.Flush();
20.    }
21.    currentZipSize = zArchive.GetSize; // ************* how do I get this size?
22.    Models.Attachment att1;
23. 
24.    // Add them to teh zip
25.    for(i = attIdx; i < lstSel.Count; i++)
26.    {
27.        att1 = lstSel[i];
28.        ZipArchiveEntry zEntry = zArchive.CreateEntry(att1.FileName, CompressionLevel.Fastest);
29.        using (var f = File.Open(att1.FullPathDecorated, FileMode.Open))
30.        {
31.            using (Stream entryStream = zEntry.Open())
32.            {
33.                f.CopyTo(entryStream);
34.// ************* how do I get this size? when I run this I get an error
35.// *************
36.                currentZipSize += zEntry.CompressedLength;
37.            }
38.            // if the size is too big for the mail server back out the last file
39.            if (currentZipSize > 4000000)
40.            {
41.                currentZipSize -= zEntry.CompressedLength;
42.                zArchive.GetEntry(att1.FileName).Delete();
43. 
44.                ms.Seek(currentZipSize, SeekOrigin.Begin);
45.                SendMail(mail, rptParms, ms, zArchive, af.CustomKey, emailCnt++);
46.                currentZipSize = 0;
47.                attIdx--;
48.            }
49.            else
50.                attIdx++;
51.        }
52.    }
53. 
54. 
55.}
56.//byte[] buf = ms.GetBuffer();
57.//Buffer.BlockCopy(buf, numberOfBytesToRemove, buf, 0, (int)ms.Length - numberOfBytesToRemove);
58.//ms.SetLength(ms.Length - numberOfBytesToRemove);
59.//ms.Seek(0, SeekOrigin.Begin);
60.//System.Net.Mail.Attachment oAttach = new System.Net.Mail.Attachment(ms, "LabReport.zip");
61.//mail.Attachments.Add(oAttach);
62.//SmtpClient client = new SmtpClient();
63.//client.Host = "intermail.basf-corp.com";
64.//client.Send(mail);
65.//client.Dispose();
66.//mail.Dispose();
67.Utilities.SetStatusBarText(EventAggregator, "Report Sent.");
68.}

 

 

1 Answer, 1 is accepted

Sort by
0
Boby
Telerik team
answered on 22 May 2017, 10:40 AM
Hello Randy,

You can get the size of the entry only after the disposal of the entry, so the following should work:
using (Stream stream = File.Open(@"d:\temp\test.zip", FileMode.Create))
{
    using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create, false, null))
    {
        ZipArchiveEntry entry = archive.CreateEntry("text.txt");
        try
        {
            StreamWriter writer = new StreamWriter(entry.Open());
            writer.WriteLine("Hello world!");
            writer.Flush();
        }
        finally
        {
            entry.Dispose();
 
            // Here, after the disposal, you can get the compressed size.
            var length = entry.CompressedLength;
        }
    }
}


Regards,
Boby
Telerik by Progress

Tags
ZipLibrary
Asked by
Randy Hompesch
Top achievements
Rank 1
Answers by
Boby
Telerik team
Share this question
or