csharp 压缩和下载Zip
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 压缩和下载Zip相关的知识,希望对你有一定的参考价值。
public static void CompressAndDownload(HttpResponseBase _response, string zipFilename, List<FileCompressInfo> files)
{
MemoryStream outputMemStream = new MemoryStream();
ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);
zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
byte[] bytes = null;
foreach (var file in files)
{
var newEntry = new ZipEntry(file.Filename);
newEntry.DateTime = DateTime.Now;
zipStream.PutNextEntry(newEntry);
bytes = file.Bytes;
MemoryStream inStream = new MemoryStream(bytes);
StreamUtils.Copy(inStream, zipStream, new byte[4096]);
inStream.Close();
zipStream.CloseEntry();
}
zipStream.IsStreamOwner = false; // False stops the Close also Closing the underlying stream.
zipStream.Close(); // Must finish the ZipOutputStream before using outputMemStream.
outputMemStream.Position = 0;
MemoryStream ms = new MemoryStream(outputMemStream.ToArray());
var downloadFilename = string.Format("{0}_{1}", System.DateTime.Now.ToString("yyyy-MM-dd"), zipFilename);
_response.Clear();
_response.AddHeader("content-disposition", "attachment; filename=" + downloadFilename);
_response.ContentType = "application/octet-stream";
_response.Buffer = true;
ms.WriteTo(_response.OutputStream);
_response.End();
}
}
public class FileCompressInfo
{
public string Filename { get; set; }
public byte[] Bytes { get; set; }
}
以上是关于csharp 压缩和下载Zip的主要内容,如果未能解决你的问题,请参考以下文章
7-Zip下载|7-Zip下载
liunx下把网站文件压缩为zip文件备份提供给ftp下载
PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 && Linux下的ZipArchive配置开启压缩
压缩文件tar.gz和zip之间的区别
iPhone:在运行时在主包子目录中下载 zip 并解压缩
PHP扩展类ZipArchive实现压缩Zip文件和文件打包下载