Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的zip文件#_C#_.net - Fatal编程技术网

C# C语言中的zip文件#

C# C语言中的zip文件#,c#,.net,C#,.net,我有下面的代码。基本上,使用下面的代码,我试图从C:\中的文件夹中获取文件,然后将它们移动到D:\并通过添加日期来压缩它们。“string outputfilename=Path.Combine…”出现错误,我还想从文件夹中删除较旧的存档文件。。请有人给我指路 using System; using System.IO; using System.Net; using System.Text; namespace test1 { class Program { stri

我有下面的代码。基本上,使用下面的代码,我试图从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)
      }
   }
}
提前感谢

试着这样做:

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

摘自

我以前使用过SharpZipLib,有很多样品可以让您开始使用

谢谢

试试看

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();

请修复粘贴的代码(从SO代码格式化程序中可以看到缺少引号)。请提供您得到的错误,并指定它是编译时错误还是运行时错误。我得到的错误是“System.FormatException未经处理,它表示message=Index(基于零)必须大于或等于零,并且小于参数列表的大小”您看到问题中的任何内容了吗?那里的代码没有编译的机会-无法解释为什么会出现运行时错误。。。从例外情况来看,它和压缩文件根本没有关系…我知道我得到了那个…我缺少了一个括号。。使在.net中处理zip文件变得轻松。但是使用Filestream,我可以读取单个文件..但我想获取多个文件..@radhikarao如果您不想使用第三方工具而不使用任何第三方工具,您可以使用此工具。我们可以这样做吗..我已粘贴了上面的代码..如果可能,您能告诉我哪里出了问题。。???