如何压缩多个文件/文件夹(GZipStream and C#)
在 .Net Framework 2.0 中 添 加 了 System.IO.Compression 类 来 实 现 对 文 件 的 压 缩 / 解 压
(GZipStream 方法),下面我们来看一个简单的例子.
Code1:
public static void Compress(string filePath, string zipPath)
{
FileStream sourceFile = File.OpenRead(filePath);
FileStream destinationFile = File.Create(zipPath);
byte[] buffer = new byte[sourceFile.Length];
GZipStream zip = null;
try
{
sourceFile.Read(buffer, 0, buffer.Length);
zip = new GZipStream(destinationFile,
CompressionMode.Compress);
zip.Write(buffer, 0, buffer.Length);
}
catch
{
throw;
}
finally
{
zip.Close();
sourceFile.Close();
destinationFile.Close();
}
}
public static void Decompress(string zipPath,string filePath)
{
FileStream sourceFile = File.OpenRead(zipPath);
string path = filePath.Replace(Path.GetFileName(filePath), "");
if(!Directory.Exists(path))
Directory.CreateDirectory(path);
FileStream destinationFile = File.Create(filePath);
GZipStream unzip = null;
- 1
- 2
- 3
- 4
- 5
- 6
前往页