2012-02-24 161 views
-1

我正在使用.Net框架2.0。我需要从文件夹路径“D:\ Nagaraj \ New Project Read Document \ TCBILPOS \ TCBILPOS \ TCBILPOS \ FileBuild \ HOST”创建zip文件...在该主机文件夹中有7个txt文件。所以,现在我想在这个相同的文件夹路径“D:\ Nagaraj \ New Project Read Document \ TCBILPOS \ TCBILPOS \ TCBILPOS \ FileBuild”中创建zip文件“HOST.zip”........提前致谢如何在asp.net中生成zip文件

+0

http://stackoverflow.com/questions/9426992/how-to-create-zip-file-in-asp-net – arunes 2012-02-24 09:15:05

+0

的重复这可以帮助你。 http://forums.asp.net/t/1086292.aspx – Junaid 2012-02-24 09:14:19

回答

0

看看这个link

您可以选择使用第三方库,也可以使用J#(这已经荏苒包括保持与Java库的兼容性)。该链接中的完整源代码。

0

没有使用第三方..我们可以通过使用J#库与C#做到这一点。 只需添加对vjslib.dll的引用并添加这些命名空间。

using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using java.io; 
using java.util.zip; 
using System.IO; 
using System.Text 

现在混合使用j#和C#进入图片。类别FileOutputStreamZipOutputStreamFileInputStreamZipEntry是在zip压缩中扮演重要角色的j#类。

StringBuilder sb = new StringBuilder(); 
    string ZipFileName = String.Format(@"C:\ZippedFolders\({0}).MyZip.zip",DateTime.Now.ToString("yyyyMMdd")); 
    string theDirectory = @"C:\Sarat"; 

    try 
    { 
     sb.Append(String.Format("Directory To Zip: {0}.<br/>", theDirectory)); 
     sb.Append(String.Format("Zip file: {0}.<br/>", ZipFileName)); 

     string[] allFiles = Directory.GetFiles(theDirectory, "*.*", SearchOption.AllDirectories); 

     if (System.IO.File.Exists(ZipFileName)) 
     { 
      System.IO.File.Delete(ZipFileName); 
      sb.Append(String.Format("Deleted old Zip file: {0}.<br/>", ZipFileName)); 
     } 


     FileOutputStream fos = new FileOutputStream(ZipFileName); 
     ZipOutputStream zos = new ZipOutputStream(fos); 
     zos.setLevel(9); 

     for (int i = 0; i < allFiles.Length; i++) 
     { 
      string sourceFile = allFiles[i]; 

      FileInputStream fis = new FileInputStream(sourceFile); 

      ZipEntry ze = new ZipEntry(sourceFile.Replace(theDirectory + @"\", "")); 
      zos.putNextEntry(ze); 

      sbyte[] buffer = new sbyte[1024]; 
      int len; 

      while ((len = fis.read(buffer)) >= 0) 
      { 
       zos.write(buffer, 0, len); 
      } 

      fis.close(); 
     } 

     zos.closeEntry(); 
     zos.close(); 
     fos.close(); 

     sb.Append(String.Format("Folder {0} Zipped successfuly to File {1}.<br/>", theDirectory, ZipFileName)); 


    } 
    catch (Exception eX) 
    { 
     sb.Append(String.Format("Error zipping folder {0}. Details: {1}. Stack Trace: {2}.<br/>", theDirectory, eX.Message, eX.StackTrace)); 
    }