使用c#在一个文件夹中压缩文件并返回响应
我想使用c#压缩文件夹中的文件和文件夹,而且我查看了以前提出的问题,但它们不适用于我......我目前正在尝试与DotNetZip合作。以下是我的代码:使用c#在一个文件夹中压缩文件并返回响应
using (ZipFile zip = new ZipFile())
{
string[] files = Directory.GetFiles(@"C:\Users\T1132\Desktop\logs");
// add all those files to the logs folder in the zip file
zip.AddFiles(files, "logs");
zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");
var a = System.DateTime.Now.ToString();
zip.Save(@"C:\Users\T1132\Desktop\logs\Archiver.zip");
foreach (var f in files)
{
System.IO.File.Delete(f);
System.Diagnostics.Debug.WriteLine(f + "will be deleted");
}
}
上面的代码工作,但它只能压缩文件并保留文件夹。请帮助我,谢谢。
有很多方法可以使用DotNetZip压缩文件。
using (ZipFile zip = new ZipFile())
{
zip.Encoding = System.Text.Encoding.GetEncoding("big5"); // chinese
zip.AddDirectory(@"MyDocuments\ProjectX");
zip.Save(ZipFileToCreate);
}
上面拉动一个目录。对于更多的用途,你可以按照链接
https://dotnetzip.codeplex.com/wikipage?title=CS-Examples&referringTitle=Examples
或
http://grapevine.dyndns-ip.com/download/authentec/download/dotnetzip/examples.htm
感谢
感谢这到目前为止不错,但我想要在控制台应用程序屏幕上弹出一个响应,有什么想法? –
你想在控制台上做什么? –
我想要一个回复,指定它是否成功 –
我使用这一点,它是伟大的工作。确保目录具有足够的权限。
System.IO.Compression.ZipFile.CreateFromDirectory(zipPath, path + strFileName + ".zip");
您可以使用下面的代码。
string zipFileName= "zip full path with extension";
var files = Directory.GetFiles(" directory path");
Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile();
foreach(var file in files) {
zip.AddFile(file);
}
zip.Save(zipFileName);
检查[这个答案](https://stackoverflow.com/a/10643602/4685428) –
请参阅[问]和[MCVE]如何提出好的问题。 DotNetZip遇到的具体问题是什么?你有什么尝试?你卡在哪里? –