2012-02-24 65 views
0

zip文件,我需要創建該文件夾的壓縮文件,路徑爲:如何建立在asp.net

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

主機文件夾中有7 txt文件。

我想上面的文件夾中創建壓縮文件HOST.zip:提前

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

感謝。

+0

你問過9個問題,並沒有接受一個答案。這不是StackOverflow社區的工作方式。請在信用到期時給予信用。 – 2012-02-24 22:46:54

回答

1

這可以很容易使用,現在用asp.net C#來完成,

這裏是使用System.IO.Packaging程序庫的例子:

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

+0

我使用的是.Net框架2.o – user1225988 2012-02-24 07:54:38

+0

這應該更有幫助:http://forums.asp.net/t/1398549.aspx/1 – 2012-02-24 07:56:21

+0

我使用.Net框架2.0。因此,使用System.IO。包裝不來 – user1225988 2012-02-24 07:56:58

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"); 
} 
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() 
    { 

    } 
} 
+0

現在可以使用asp.net與c#輕鬆完成此操作, – 2016-09-10 09:36:15