如何在asp.net中创建zip文件

时间:2012-02-24 07:31:13

标签: asp.net

我需要从文件夹路径:

创建zip文件
D:\Nagaraj\New Project Read Document\TCBILPOS\TCBILPOS\TCBILPOS\FileBuild\HOST

在该主机文件夹中有7个txt文件。

我想在上面的文件夹中创建zip文件HOST.zip

D:\Nagaraj\New Project Read Document\TCBILPOS\TCBILPOS\TCBILPOS\FileBuild

提前致谢。

4 个答案:

答案 0 :(得分:1)

现在可以使用带有c#,

的asp.net轻松完成

这是使用System.IO.Packaging库的示例:

http://weblogs.asp.net/jgalloway/archive/2007/10/25/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib.aspx

答案 1 :(得分:1)

我在自己的项目中使用了Ionic ZIP

using (ZipFile zip = new ZipFile())
 {
     // add this map file into the "images" directory in the zip archive
     zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images");
     // add the report into a different directory in the archive
     zip.AddFile("c:\\Reports\\2008-Regional-Sales-Report.pdf", "files");
     zip.AddFile("ReadMe.txt");
     zip.Save("MyZipFile.zip");
 }

答案 2 :(得分:1)

最受欢迎的第三方库之一(可能是因为它是免费的)是SharpZipLib:http://www.icsharpcode.net/opensource/sharpziplib/

答案 3 :(得分:0)

public class Ziper
{
    public static string MapPathReverse(string fullServerPath)
    {
        return @"~\" + fullServerPath.Replace(HttpContext.Current.Request.PhysicalApplicationPath, String.Empty);
    }
    public static void Zip(HttpResponse Response, HttpServerUtility Server, string[] pathes)
    {
        Response.Clear();
        Response.BufferOutput = false; // false = stream immediately
        System.Web.HttpContext c = System.Web.HttpContext.Current;
        //String ReadmeText = String.Format("README.TXT\n\nHello!\n\n" +
        //                                 "This is text for a readme.");
        string archiveName = String.Format("archive-{0}.zip",
                                          DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
        Response.ContentType = "application/zip";
        Response.AddHeader("content-disposition", "filename=" + archiveName);

        var path = Server.MapPath(@"../Images/TempFile/TempFile" + DateTime.Now.Ticks);
        if (Directory.Exists(path) == false)
            Directory.CreateDirectory(path);
        var pathzipfile = Server.MapPath(@"../Images/TempFile/zip_" + DateTime.Now.Ticks + ".zip");
        for (int i = 0; i < pathes.Length; i++)
        {
            if (File.Exists(pathes[i]))
            {
                string dst = Path.Combine(path, Path.GetFileName(pathes[i]));
                File.Copy(pathes[i], dst);
            }

        }
        if (File.Exists(pathzipfile))
            File.Delete(pathzipfile);
        ZipFile.CreateFromDirectory(path, pathzipfile);
        {
            byte[] bytes = File.ReadAllBytes(pathzipfile);
            Response.OutputStream.Write(bytes, 0, bytes.Length);
        }
        Response.Close();
        File.Delete(pathzipfile);
        Directory.Delete(path, true);
    }
    public Ziper()
    {

    }
}