C# 为多个文件创建zip,然后将其上载到文件夹中
我只想将多个文件的zip文件保存到服务器。 首先,它应该创建zip文件,然后将其上载到文件夹C# 为多个文件创建zip,然后将其上载到文件夹中,c#,asp.net,C#,Asp.net,我只想将多个文件的zip文件保存到服务器。 首先,它应该创建zip文件,然后将其上载到文件夹 if (FileUpload1.HasFile) { string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName); string fileLocation = Server.MapPath("~/uploads/" + fileName); FileUpload
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string fileLocation = Server.MapPath("~/uploads/" + fileName);
FileUpload1.SaveAs(fileLocation);
ZipFile createZipFile = new ZipFile();
createZipFile.AddFile(fileLocation, string.Empty);
createZipFile.Save(Server.MapPath("~/uploads/CsharpAspNetArticles.zip"));
}
如果我理解你的“无问题”问题:)你应该像这样使用IHttpHandler:
using System;
using System.IO;
using System.Web;
using ICSharpCode.SharpZipLib.Zip;
public class ZipHandler : IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext ctx)
{
var path = HttpUtility.UrlDecode(ctx.Request.QueryString["folder"]);
if (path == null)
{
return;
}
var folderName = Path.GetFileName(path);
if (folderName == String.Empty)
{
folderName = "root";
}
int folderOffset = ctx.Server.MapPath("~").Length;
using (var zipStream = new ZipOutputStream(ctx.Response.OutputStream))
{
ctx.Response.Clear();
ctx.Response.BufferOutput = false;
ctx.Response.AddHeader("Content-Disposition", "attachment; filename=" + folderName + ".zip");
ctx.Response.AddHeader("Content-Type", "application/zip");
zipStream.SetLevel(3);
CompressFolder(path, zipStream, folderOffset);
ctx.Response.Flush();
}
}
private static void CompressFolder(string path, ZipOutputStream zipStream, int folderOffset)
{
string[] files = Directory.GetFiles(path);
foreach (string filename in files)
{
try
{
using (var streamReader = File.OpenRead(filename))
{
var fi = new FileInfo(filename);
string entryName = filename.Substring(folderOffset);
entryName = ZipEntry.CleanName(entryName);
var newEntry = new ZipEntry(entryName)
{
DateTime = fi.LastWriteTime,
Size = fi.Length
};
zipStream.PutNextEntry(newEntry);
streamReader.CopyTo(zipStream, 4096);
zipStream.CloseEntry();
}
}
catch (IOException) { }
}
var folders = Directory.GetDirectories(path);
foreach (string folder in folders)
{
CompressFolder(folder, zipStream, folderOffset);
}
}
}
你的问题是?嗨…我的问题是如果有多个文件,那么首先创建zip,然后在上载按钮上将该zip文件上载到文件夹。单击这仍然不是问题,尝试使用诸如“如何”、“为什么”、“什么”或“谁”之类的词,并在句子结尾处使用?。如何通过将多个文件添加到zip并将其保存到文件夹中来上载这些文件??