您的位置:首页 > 编程语言 > C#

what is new - .net 4.5: Simple zip examples

2013-01-24 00:00 423 查看
As in
what is new in .net 4.5 framework, one is mentioned that the System.IO.Compression names has improved so that the zip reduce the size of generated files. In .NET 4.5, the namespace that takes care of compression is System.IO.Compression, and you may need to use the System.IO.Compression.FileSystem.dll;

For some very simple use case, you can use the following method to zip files. here is the
ZipFile class where you can perform the very basic compression and extraction with .

the source code for the example is as follow.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// you will need to use  System.IO.Compression.FileSystem.dll
using System.IO.Compression;

using System.IO;

namespace ZipCompression
{
public class SimpleZip
{
public static void  SimpleZipFile(string[] args)
{
string startPath = @"U:\docs\blogs";
string zipPath = @"U:\temp\result.zip";
string extractPath = @"U:\temp\extract";

try
{
if (!Directory.Exists(startPath))
{
Console.Error.WriteLine("Directory {0} does not exist!", startPath);
}

if (File.Exists(zipPath))
{
//
Console.WriteLine("Removing existing zip file - {0}", zipPath);
File.Delete(zipPath);
}

if (Directory.Exists(extractPath))
{
Console.WriteLine("Removing existing extract directory - {0}", extractPath);
Directory.Delete(extractPath, true);
}

ZipFile.CreateFromDirectory(startPath, zipPath);
Console.WriteLine("zip contents completed");

ZipFile.ExtractToDirectory(zipPath, extractPath);

Console.WriteLine("Extracat completed");

}
catch
{
Console.WriteLine("Failed to run the zip examples");
throw;
}
}
}
}
Extended reading:

DeflateStream
GzipStream
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# .net