如何在使用Ionic.Zip.ZipFile保存到磁盘之前获取zip文件的大小?

时间:2015-10-11 10:28:22

标签: c# zip zipfile

我是Iig Ionic.Zip.ZipFile。我想创建10 MB的zip文件。我需要在保存到磁盘之前获取ZipFile的大小。这可能吗?

    private static string tempPath = "@Temp Folder";
    List<string> fileNames = new List<string>();


    using (Ionic.Zip.ZipFile zf = new Ionic.Zip.ZipFile())
    {
      for (int i = 0; i < fileNames.Count; i++)
      {
        zf.AddFile(tempPath + fileNames[i], string.Empty);

        //How can I get size of zf before save here ?

      if(zf size==10mb)
      {

       zf.Save(tempPath + string.Format("{0}-{1}-{2}.zip","XXX", "XXX", 
          DateTime.Now.ToString("yyyyMMdd")));
      }

     }
   }

1 个答案:

答案 0 :(得分:0)

您可以将zip保存到MemoryStream

var ms = new MemoryStream();
zip.Save(ms);

然后阅读MemoryStream.Length Property并获取尺寸。

如果您仍想将其保存到磁盘,只需使用您已有的内存流:

FileStream file = new FileStream("file.zip", FileMode.Create, FileAccess.Write);
// I believe you'll need to rewind the stream before saving
ms.Seek(0, SeekOrigin.Begin); 
ms.WriteTo(file);
file.Close();
ms.Close();