How can I programatically (C#) ZIP a file (in Windows) without using
any third party libraries?
If using the 4.5+ Framework, there is now the ZipArchive and ZipFile classes.
using (ZipArchive zip = ZipFile.Open("test.zip", ZipArchiveMode.Create))
{
zip.CreateEntryFromFile(@"c:something.txt", "data/path/something.txt");
}
You need to add references to:
- System.IO.Compression
- System.IO.Compression.FileSystem
For .NET Core targeting net46, you need to add dependencies for
- System.IO.Compression
- System.IO.Compression.ZipFile
Example project.json:
"dependencies": {
"System.IO.Compression": "4.1.0",
"System.IO.Compression.ZipFile": "4.0.1"
},
"frameworks": {
"net46": {}
}
For .NET Core 2.0, just adding a simple using statement is all that is needed:
- using System.IO.Compression;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…