简体   繁体   English

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

[英]How to get size of zip file before save to disk by using Ionic.Zip.ZipFile?

I am usig Ionic.Zip.ZipFile.I want to create 10 mb zip file.I need get size of ZipFile before save to disk.Is it possible ? 我是usig 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")));
      }

     }
   }

You can save your zip into a MemoryStream : 您可以将zip保存到MemoryStream

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

then read the MemoryStream.Length Property and get the size. 然后读取MemoryStream.Length属性并获取大小。

If you then still want to save it to disk just use the memory stream you already have: 如果您仍然想将其保存到磁盘上,请使用已有的内存流:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM