0

我有下面的代码。基本上使用下面的代码,我试图从 C:\ 中的文件夹中抓取文件,然后将它们移动到 D:\ 并通过附加日期来压缩它们。我在“string outputfilename = Path.Combine ...”出现错误,我还想从文件夹中删除较旧的存档文件..请任何人指导我。

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)
      }
   }
}

提前致谢!!!

4

3 回答 3

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

这里取

于 2012-08-16T01:27:02.363 回答
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();
于 2012-08-16T02:43:50.407 回答
0

我以前使用过 SharpZipLib,这里有很多示例可以帮助您入门。

谢谢

于 2012-08-16T01:40:25.297 回答