引用using System.IO.Packaging;
第一步生成zip类
public class Zip
{
public string SourcePath { get; set; }
public Zip(string sourcePath)
{
this.SourcePath = sourcePath;
}
public void zipFolder(string zipfilepath)
{
using (Package package = Package.Open(zipfilepath, System.IO.FileMode.Create))
{
DirectoryInfo directoryInfo = new DirectoryInfo(SourcePath);
ZipDirectory(directoryInfo, package);
}
}
private void ZipDirectory(DirectoryInfo di, Package pa)
{
foreach (FileInfo fi in di.GetFiles())
{
string relativepath = fi.FullName.Replace(SourcePath, String.Empty);
relativepath = relativepath.Replace("\\", "/");
PackagePart part = pa.CreatePart(new Uri(relativepath, UriKind.Relative), System.Net.Mime.MediaTypeNames.Application.Zip);
using (FileStream fs = fi.OpenRead())
{
CopyStream(fs, part.GetStream());
}
}
foreach (DirectoryInfo sbdi in di.GetDirectories())
{
ZipDirectory(sbdi, pa);
}
}
private void CopyStream(Stream source, Stream target)
{
const int bufSize = 0x1000;
byte[] buf = new byte[bufSize];
int byteread = 0;
while ((byteread = source.Read(buf, 0, bufSize)) > 0)
{
target.Write(buf, 0, byteread);
}
}
}
第二步生成目录文件,添加引用using System.IO;
string path = "";//文件存放的文件夹路径
byte[] img1 = (byte[])ds.Tables[0].Rows[0]["img1"];
File.WriteAllBytes(HttpContext.Current.Server.MapPath(path + "/img1.jpg"), img1);//生成图片
byte[] video1 = (byte[])ds.Tables[0].Rows[0]["video1"];
File.WriteAllBytes(HttpContext.Current.Server.MapPath(path + "/video1.mp4"), video1);//生成视频
File.WriteAllText(HttpContext.Current.Server.MapPath(path + "/shuoming.doc"), info, System.Text.Encoding.UTF8);//生成word文档
Zip zip = new Zip(HttpContext.Current.Server.MapPath(path));
zip.zipFolder(HttpContext.Current.Server.MapPath(path + ".zip"));//文件夹生成zip