c# – SharpZipLib创建一个带有内存字符串的存档,并作为附件下载
发布时间:2020-12-15 18:13:15 所属栏目:百科 来源:网络整理
导读:我使用DotNetZip创建一个带有内存字符串的zip存档,并使用以下代码将其下载为附件. byte[] formXml = UTF8Encoding.Default.GetBytes("formpkgTest1/pkg/form");byte[] formHtml = UTF8Encoding.Default.GetBytes("htmlbodyTest2/body/html");ZipFile zipFile
我使用DotNetZip创建一个带有内存字符串的zip存档,并使用以下代码将其下载为附件.
byte[] formXml = UTF8Encoding.Default.GetBytes("<form><pkg>Test1</pkg></form>"); byte[] formHtml = UTF8Encoding.Default.GetBytes("<html><body>Test2</body></html>"); ZipFile zipFile = new ZipFile(); zipFile.AddEntry("Form.xml",formXml); zipFile.AddEntry("Form.html",formHtml); Response.ClearContent(); Response.ClearHeaders(); Response.AppendHeader("content-disposition","attachment; filename=FormsPackage.zip"); zipFile.Save(Response.OutputStream); zipFile.Dispose(); 现在我需要对SharpZipLib做同样的事情.我该怎么做 ? SharpZipLib是否支持将文件添加为字节数组? 解决方法
试试以下
MemoryStream msFormXml = new MemoryStream(UTF8Encoding.Default.GetBytes("<form><pkg>Test1</pkg></form>")); MemoryStream msFormHTML = new MemoryStream(UTF8Encoding.Default.GetBytes("<html><body>Test2</body></html>")); MemoryStream outputMemStream = new MemoryStream(); ZipOutputStream zipStream = new ZipOutputStream(outputMemStream); zipStream.SetLevel(3); //0-9,9 being the highest level of compression ZipEntry xmlEntry = new ZipEntry("Form.xml"); xmlEntry.DateTime = DateTime.Now; zipStream.PutNextEntry(xmlEntry); StreamUtils.Copy(msFormXml,zipStream,new byte[4096]); zipStream.CloseEntry(); ZipEntry htmlEntry = new ZipEntry("Form.html"); htmlEntry.DateTime = DateTime.Now; zipStream.PutNextEntry(htmlEntry); StreamUtils.Copy(msFormHTML,new byte[4096]); zipStream.CloseEntry(); zipStream.IsStreamOwner = false; zipStream.Close(); outputMemStream.Position = 0; byte[] byteArray = outputMemStream.ToArray(); Response.Clear(); Response.AppendHeader("Content-Disposition","attachment; filename=FormsPackage.zip"); Response.AppendHeader("Content-Length",byteArray.Length.ToString()); Response.ContentType = "application/octet-stream"; Response.BinaryWrite(byteArray); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读