LANCommander/LANCommander.Server.Services/Extensions/ZipArchiveExtensions.cs

37 lines
1.1 KiB
C#
Raw Permalink Normal View History

using System.IO.Compression;
namespace LANCommander.Server.Services.Extensions
{
public static class ZipArchiveExtensions
{
public static ZipArchiveEntry CreateEntry(this ZipArchive archive, string fileName, byte[] data)
{
var entry = archive.CreateEntry(fileName, CompressionLevel.Fastest);
using (var stream = entry.Open())
{
stream.Write(data, 0, data.Length);
}
return entry;
}
public static async Task<string> ReadAllTextAsync(this ZipArchive zip, string entryName)
{
var entry = zip.GetEntry(entryName);
using (var reader = new StreamReader(entry.Open()))
{
return await reader.ReadToEndAsync();
}
}
public static void ExtractEntry(this ZipArchive zip, string entryName, string destinationFileName, bool overwrite = false)
{
var entry = zip.GetEntry(entryName);
entry.ExtractToFile(destinationFileName, overwrite);
}
}
}