2012-08-16 59 views
0

我有下面的code.Basically與下面的代碼我試圖從C:\中的文件夾抓住文件,然後將它們移動到D:\並通過追加日期壓縮它們。我得到錯誤在「字符串outputfilename = Path.Combine ...」,我也想從文件夾中刪除舊的存檔文件..請任何人指導我。zip文件在C#

using System; 
using System.IO; 
using System.Net; 
using System.Text; 

namespace test1 
{ 
    class Program 
    { 
     string folder = @"C:\folder1"; 
     string folder2 = @"D:\Test1"; 
     string outputFilename = Path.Combine(output, string.Format(Archive{0}.zip"),DateTime.Now.ToString("MMddyyyy"));  
     using (ZipFile zip = new ZipFile()) 
     { 
     foreach (var file Directory.EnumerateFiles(folder)) 
     zip.Save(outputFilename) 
     } 
    } 
} 

在此先感謝!

+2

請修復您粘貼代碼(缺少報價,你可以從SO代碼格式化看到)。請提供錯誤信息,並指定它是編譯還是運行時錯誤。我得到的錯誤是 – 2012-08-16 01:26:29

+0

是「System.FormatException未處理,它說message = Index(從零開始)必須大於或等於零並且小於參數列表的大小」 – 2012-08-16 01:41:09

+0

你有沒有看到你在問題中有什麼?那裏的代碼沒有機會編譯 - 這是不可能的原因你得到運行時錯誤...從例外它是根本無關Zip文件... – 2012-08-16 01:44:40

回答

1

試試這個方法:

using System; 
using System.IO; 
using System.IO.Compression; 

namespace ConsoleApplication 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      using (FileStream zipToOpen = new FileStream(@"c:\users\exampleuser\release.zip", FileMode.Open)) 
      { 
       using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update)) 
       { 
        ZipArchiveEntry readmeEntry = archive.CreateEntry("Readme.txt"); 
        using (StreamWriter writer = new StreamWriter(readmeEntry.Open())) 
        { 
          writer.WriteLine("Information about this package."); 
          writer.WriteLine("========================"); 
        } 
       } 
      } 
     } 
    } 
} 

here

+0

但Filestream我可以讀取一個文件..但我想抓住多個文件.. – 2012-08-16 01:38:03

+0

@ radhikarao - 如果你不想使用第三方工具,你可以使用http://msdn.microsoft.com /en-us/library/system.io.compression.gzipstream%28v=vs.100%29 – coder 2012-08-16 01:44:43

0

接過我之前已經使用SharpZipLib並有大量樣品here的讓你開始。

感謝

+0

沒有使用任何第三方工具,我們可以做到這一點..我已經粘貼上面的代碼..你可以告訴我,如果可能的話我錯了...... – 2012-08-16 01:42:36

0

嘗試

Response.Clear(); 

     var downloadFileName = string.Format("" + FileName + ".zip");//string.Format("YourDownload-{0}.zip", DateTime.Now.ToString("yyyy-MM-dd-HH_mm_ss")); 
     Response.ContentType = "application/zip"; 
     Response.AddHeader("Content-Disposition", "filename=" + downloadFileName); 

     // Zip the contents of the selected files 
     using (var zip = new ZipFile()) 
     { 
      // Add the password protection, if specified 
      if (!string.IsNullOrEmpty(txtZIPPassword.Text)) 
      { 
       zip.Password = txtZIPPassword.Text; 

       // 'This encryption is weak! Please see 
       // zip.Encryption = EncryptionAlgorithm.WinZipAes128; 
      } 

      // Construct the contents of the README.txt file that will be included in this ZIP 
      var readMeMessage = string.Format("Your ZIP file {0} contains the following files:{1}{1}", downloadFileName, Environment.NewLine); 

      // Add the checked files to the ZIP 
      foreach (ListItem li in cblFiles.Items) 
       if (li.Selected) 
       { 
        // Record the file that was included in readMeMessage 
        readMeMessage += string.Concat("\t* ", li.Text, Environment.NewLine); 

        // Now add the file to the ZIP (use a value of "" as the second parameter to put the files in the "root" folder) 
        zip.AddFile(li.Value, "Your Files"); 
       } 

      if (!String.IsNullOrEmpty(chkDocument.Value) && chkDocument.Checked == true) 
      { 
       zip.AddFile(chkDocument.Value, "Cover Sheet"); 
       readMeMessage += string.Concat("\t* ", lbldocName.Text.Trim(), Environment.NewLine); 
      } 
      // Add the README.txt file to the ZIP 
      zip.AddEntry("README.txt", readMeMessage, Encoding.ASCII); 


      // Send the contents of the ZIP back to the output stream 
      // zip.MaxOutputSegmentSize = 65536; 
      zip.Save(Response.OutputStream); 
      // int a=zip.NumberOfSegmentsForMostRecentSave; 
      // txtZIPPassword.Text=a.ToString(); 
     } 

     Response.Flush(); 
     HttpContext.Current.Response.End();