我有以下代码。基本上使用下面的代码我试图从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)
}
}
}
提前致谢!!!
答案 0 :(得分: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
开始答案 1 :(得分:0)
之前我使用过SharpZipLib,有很多样本here可以让你入门。
由于
答案 2 :(得分: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();