我正在尝试从文件夹中的所有文件创建一个zip文件,但无法在线找到任何相关的代码段.我正在尝试做这样的事情:
DirectoryInfo dir = new DirectoryInfo("somedir path");
ZipFile zip = new ZipFile();
zip.AddFiles(dir.getfiles());
zip.SaveTo("some other path");
Run Code Online (Sandbox Code Playgroud)
很感谢任何形式的帮助.
编辑:我只想压缩文件夹中的文件,而不是它的子文件夹.
Sha*_*ger 19
在项目中引用System.IO.Compression和System.IO.Compression.FileSystem
using System.IO.Compression;
string startPath = @"c:\example\start";//folder to add
string zipPath = @"c:\example\result.zip";//URL for your ZIP file
ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest, true);
string extractPath = @"c:\example\extract";//path to extract
ZipFile.ExtractToDirectory(zipPath, extractPath);
Run Code Online (Sandbox Code Playgroud)
要仅使用文件,请使用:
//Creates a new, blank zip file to work with - the file will be
//finalized when the using statement completes
using (ZipArchive newFile = ZipFile.Open(zipName, ZipArchiveMode.Create))
{
foreach (string file in Directory.GetFiles(myPath))
{
newFile.CreateEntryFromFile(file, System.IO.Path.GetFileName(file));
}
}
Run Code Online (Sandbox Code Playgroud)