想要增加MemoryStream的大小 [英] Want to increase MemoryStream size

查看:69
本文介绍了想要增加MemoryStream的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一个zip文件中下载两个或多个文件.但是zip文件仅包含第一个文件,而其他文件则不存在.下面是代码:


 字符串 [] fileLists = Directory.GetFiles("  C:\\ Downloadable");
        字符串 strNow = 字符串 .Format("  {0:MMM-dd-yyyy_hh-mm-ss}",System.DateTime.Now);
        使用(MemoryStream mstr =  MemoryStream())
        {
            使用(ZipOutputStream ZOS =  ZipOutputStream(mstr))
            {
                 for ( int  i =  0 ; i <  fileLists.Length; i ++)
                {
                    字节 [] content = File.ReadAllBytes(fileLists [i]);
                    字符串 fileName = fileLists [i] .Substring( 16 );
                    ZOS.CompressionLevel = CompressionLevel.BestCompression;
                    ZOS.PutNextEntry(fileName);
                    ZOS.Write(content, 0 ,content.Length- 1 );
                }
                字节 [] zipFile =  字节 [ ( int )mstr.Length];
                Response.Clear();
                Array.Copy(mstr.ToArray(),zipFile,( int )mstr.Length);
                Response.AppendHeader(" " 附件;文件名=" + strNow);
                Response.ContentType = " ;
                Response.BinaryWrite(zipFile);
                Response.Expires =  0 ;
                Response.End();
            }
        } 


}
}

如果有任何想法,请更新.

解决方案

snehashis ghosh 2写道:

字符串fileName = fileLists [i ] .Substring(16);



这看起来像是一个广泛假设的代码.您确定假设始终是正确的吗?

所有这些对我来说似乎是您正在使用的第三方库不起作用. IMO最好的选择是与该库的作者交谈.您的MemoryStream并没有被魔术地限制为第一个文件的大小,增加内存流的大小绝对不是您的问题所在.成为.如果要获取文件名,可以使用System.IO.Path.GetFileName.


I am trying to download two or more files in one zip file. But zip file contains only first file and other files are not there. Below is the code :


string[] fileLists = Directory.GetFiles("C:\\Downloadable");
        string strNow = String.Format("{0:MMM-dd-yyyy_hh-mm-ss}", System.DateTime.Now);
        using (MemoryStream mstr = new MemoryStream())
        {
            using (ZipOutputStream ZOS = new ZipOutputStream(mstr))
            {
                for (int i = 0; i < fileLists.Length; i++)
                {
                    byte[] content = File.ReadAllBytes(fileLists[i]);
                    string fileName = fileLists[i].Substring(16);
                    ZOS.CompressionLevel = CompressionLevel.BestCompression;
                    ZOS.PutNextEntry(fileName);
                    ZOS.Write(content, 0, content.Length - 1);
                }
                byte[] zipFile = new byte[(int)mstr.Length];
                Response.Clear();
                Array.Copy(mstr.ToArray(), zipFile, (int)mstr.Length);
                Response.AppendHeader("content-disposition", "attachment;filename=" + strNow);
                Response.ContentType = "application/x-zip-compressed";
                Response.BinaryWrite(zipFile);
                Response.Expires = 0;
                Response.End();
            }
        }


}
}

If any idea please update. Thanks.

解决方案

snehashis ghosh 2 wrote:

string fileName = fileLists[i].Substring(16);



This looks like code that''s making a broad assumption. Are you sure that assumption is always true ?

All of this reads to me as if the third party library you''re using is not working. Your best bet IMO is to talk to the authors of that library. Your MemoryStream is not magically constrained to EXACTLY the size of the first file, increasing the size of a memory stream is most certainly not where your issue lies.


Christian has succinctly stated what the problem seems to be. If you want to get the filename, you can do this using System.IO.Path.GetFileName.


这篇关于想要增加MemoryStream的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆