SharpZipLib 是一个广泛使用的开源库,用于在 .NET 应用程序中处理压缩和解压缩文件。它支持多种压缩格式,包括 Zip、GZip、BZip2 和 TAR。本文将介绍如何在 C# 中使用 SharpZipLib 进行文件压缩和解压缩操作。
首先,你需要在项目中安装 SharpZipLib 库。你可以使用 NuGet 包管理器来安装:
C#Install-Package SharpZipLib
或者在 PackageManager
控制台中运行:
PowerShellPM> Install-Package SharpZipLib
下面是一个简单的示例,展示如何将一个文件压缩成 ZIP 格式:
C#using ICSharpCode.SharpZipLib.Zip;
namespace AppSharpZip
{
internal class Program
{
static void Main(string[] args)
{
string sourceFile = @"d:\example.txt";
string zipFile = @"d:\example.zip";
using (FileStream fsOut = File.Create(zipFile))
using (ZipOutputStream zipStream = new ZipOutputStream(fsOut))
{
zipStream.SetLevel(3); // 压缩级别 0-9
byte[] buffer = new byte[4096];
ZipEntry entry = new ZipEntry(Path.GetFileName(sourceFile));
entry.DateTime = DateTime.Now;
zipStream.PutNextEntry(entry);
using (FileStream fsIn = File.OpenRead(sourceFile))
{
int sourceBytes;
do
{
sourceBytes = fsIn.Read(buffer, 0, buffer.Length);
zipStream.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
zipStream.CloseEntry();
}
Console.WriteLine("压缩成功!");
}
}
}
下面是一个递归压缩整个文件夹的示例:
C#using ICSharpCode.SharpZipLib.Zip;
namespace AppSharpZip
{
internal class Program
{
public static void Main(string[] args)
{
string folderToZip = "D:\\sqlmodel";
string zipFile = "D:\\sqlmodelfolder.zip";
using (FileStream fsOut = File.Create(zipFile))
using (ZipOutputStream zipStream = new ZipOutputStream(fsOut))
{
zipStream.SetLevel(3); // 压缩级别 0-9
ZipDirectory(folderToZip, zipStream, folderToZip.Length + 1);
}
Console.WriteLine("文件夹压缩成功!");
}
private static void ZipDirectory(string folder, ZipOutputStream zipStream, int folderOffset)
{
string[] files = Directory.GetFiles(folder);
foreach (string fileName in files)
{
FileInfo fi = new FileInfo(fileName);
string entryName = fileName.Substring(folderOffset); // 截断绝对路径
entryName = ZipEntry.CleanName(entryName); // Windows 和 Unix 路径处理
ZipEntry newEntry = new ZipEntry(entryName);
newEntry.DateTime = fi.LastWriteTime;
newEntry.Size = fi.Length;
zipStream.PutNextEntry(newEntry);
byte[] buffer = new byte[4096];
using (FileStream fsInput = fi.OpenRead())
{
int sourceBytes;
do
{
sourceBytes = fsInput.Read(buffer, 0, buffer.Length);
zipStream.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
zipStream.CloseEntry();
}
string[] folders = Directory.GetDirectories(folder);
foreach (string folderName in folders)
{
ZipDirectory(folderName, zipStream, folderOffset);
}
}
}
}
下面是一个解压 ZIP 文件的示例:
C#using ICSharpCode.SharpZipLib.Zip;
namespace AppSharpZip
{
internal class Program
{
public static void Main(string[] args)
{
string zipFile = "d:\\example.zip";
string extractPath = "d:\\";
using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(zipFile)))
{
ZipEntry entry;
while ((entry = zipStream.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(entry.Name);
string fileName = Path.GetFileName(entry.Name);
if (!string.IsNullOrEmpty(directoryName))
{
Directory.CreateDirectory(Path.Combine(extractPath, directoryName));
}
if (!string.IsNullOrEmpty(fileName))
{
string filePath = Path.Combine(extractPath, entry.Name);
using (FileStream streamWriter = File.Create(filePath))
{
int size = 2048;
byte[] data = new byte[size];
while (true)
{
size = zipStream.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
}
}
}
}
Console.WriteLine("解压成功!");
}
}
}
除了 ZIP 之外,SharpZipLib 还支持 GZip 格式。下面是一个将文件压缩为 GZip 的示例:
C#using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Zip;
namespace AppSharpZip
{
internal class Program
{
public static void Main(string[] args)
{
string sourceFile = "d:\\example.txt";
string gzipFile = "d:\\example.gz";
using (FileStream fsOut = File.Create(gzipFile))
using (GZipOutputStream gzipStream = new GZipOutputStream(fsOut))
{
byte[] buffer = new byte[4096];
using (FileStream fsIn = File.OpenRead(sourceFile))
{
int sourceBytes;
do
{
sourceBytes = fsIn.Read(buffer, 0, buffer.Length);
gzipStream.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
Console.WriteLine("GZip 压缩成功!");
}
}
}
C#using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Zip;
namespace AppSharpZip
{
internal class Program
{
public static void Main(string[] args)
{
string gzipFile = "d:\\example.gz";
string decompressedFile = "d:\\example1.txt";
using (FileStream fsIn = File.OpenRead(gzipFile))
using (GZipInputStream gzipStream = new GZipInputStream(fsIn))
using (FileStream fsOut = File.Create(decompressedFile))
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = gzipStream.Read(buffer, 0, buffer.Length)) > 0)
{
fsOut.Write(buffer, 0, bytesRead);
}
}
Console.WriteLine("GZip 解压成功!");
}
}
}
SharpZipLib 是一个功能强大的库,可以轻松地集成到您的 .NET 项目中处理压缩和解压缩任务。本文介绍了如何在 C# 项目中使用 SharpZipLib 进行文件和文件夹的压缩和解压缩操作,示例涵盖了 ZIP 和 GZip 两种常见的压缩格式。希望本文能帮助您更好地理解和应用 SharpZipLib。
本文作者:技术老小子
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!