c# zip file and folder programmatically

In .net 4.5 Framework, we can zip a file by this way:

        private static string CompressFile(string sourceFileName)
        {
            using (ZipArchive archive = ZipFile.Open(Path.ChangeExtension(sourceFileName, ".zip"), ZipArchiveMode.Create))
            {
                archive.CreateEntryFromFile(sourceFileName, Path.GetFileName(sourceFileName));
            }
            return Path.ChangeExtension(sourceFileName, ".zip");
        }

  

zip a folder and unzip in a easy way:

        private static void CompressFloder(string startPath, string zipPath)
        {
            ZipFile.CreateFromDirectory(startPath, zipPath);
        }

        private static void UncompressToDirectory(string zipPath, string extractPath)
        {
            ZipFile.ExtractToDirectory(zipPath, extractPath);
        }

  

原文地址:https://www.cnblogs.com/wushuaiyi/p/4980820.html