From 89053b6362cc20ae55a0b22c47ec76fa5d889092 Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Sat, 12 May 2018 02:15:34 +0200 Subject: [PATCH] No more dependancy to SharpZipLib Increment version to 0.0.8 --- SimpleHelper.Core/CompressHelper.cs | 125 +++++++++++---------- SimpleHelper.Core/SimpleHelper.Core.csproj | 3 +- 2 files changed, 64 insertions(+), 64 deletions(-) diff --git a/SimpleHelper.Core/CompressHelper.cs b/SimpleHelper.Core/CompressHelper.cs index 54005a9..67dfbb7 100644 --- a/SimpleHelper.Core/CompressHelper.cs +++ b/SimpleHelper.Core/CompressHelper.cs @@ -1,16 +1,17 @@ -using ICSharpCode.SharpZipLib.Zip; -using System.IO; +using System.IO; using System.IO.Compression; using System.Text; using System.Xml.Serialization; using System; +using System.Collections.Generic; +using System.Linq; namespace SimpleHelper.Core { public static class CompressHelper { /// - /// Gs the zip. + /// Compress the string with GZIP. /// /// The value. /// @@ -48,7 +49,7 @@ public static string GZip(string value) } /// - /// Uns the G zip. + /// Uncompress the string with GZIP. /// /// The value. /// @@ -95,80 +96,80 @@ public static string UnGZip(string value) /// The filepath. /// The output directory. /// + public static bool UnzipToFolder(string filepath, string outputDirectory) + { + return DecompressToFolder(filepath, outputDirectory); + } + + /// + /// Unzips to folder. + /// + /// The filepath. + /// The output directory. + /// The exclude files. + /// + public static bool UnzipToFolder(string filepath, string outputDirectory, List excludeFiles) + { + return UnzipToFolder(filepath, outputDirectory); + } + + /// + /// Decompresses to folder. + /// + /// The filepath. + /// The output directory. + /// The exclude files. + /// /// /// - public static bool UnzipToFolder(string filepath, string outputDirectory) + private static bool DecompressToFolder(string filepath, string outputDirectory, List excludeFiles = null) { - try + if (!string.IsNullOrEmpty(filepath)) { - if (!string.IsNullOrEmpty(outputDirectory)) - { - throw new Exception($"Zipped file not specified"); - } + throw new Exception($"Zipped file not specified"); + } - if (!string.IsNullOrEmpty(outputDirectory)) - { - throw new Exception($"Output Folder not specified"); - } + if (!string.IsNullOrEmpty(outputDirectory)) + { + throw new Exception($"Output Folder not specified"); + } - if (File.Exists(filepath)) - throw new Exception($"File {filepath} not found"); + if (File.Exists(filepath)) + throw new Exception($"File {filepath} not found"); - if (!Directory.Exists(outputDirectory)) - Directory.CreateDirectory(outputDirectory); + if (!Directory.Exists(outputDirectory)) + Directory.CreateDirectory(outputDirectory); - // Unzip all content to target folder - using (ZipInputStream s = new ZipInputStream(File.OpenRead(filepath))) + using (ZipArchive za = System.IO.Compression.ZipFile.OpenRead(filepath)) + { + foreach (var zipArchiveEntry in za.Entries) { + if (zipArchiveEntry == null) + continue; - ZipEntry theEntry; - while ((theEntry = s.GetNextEntry()) != null) - { - string directoryPath = Path.Combine(outputDirectory, Path.GetDirectoryName(theEntry.Name)); - string fileName = Path.GetFileName(theEntry.Name); + if (excludeFiles != null && excludeFiles.Any(p => p.ToLower() == zipArchiveEntry.Name.ToLower())) + continue; - // create directory - if (!string.IsNullOrEmpty(directoryPath)) - { - if (!Directory.Exists(directoryPath)) - Directory.CreateDirectory(directoryPath); - } + var directoryPath = Path.Combine(outputDirectory, Path.GetDirectoryName(zipArchiveEntry.Name)); - if (!string.IsNullOrEmpty(fileName)) - { - string filePath = Path.Combine(directoryPath, theEntry.Name); - - if (File.Exists(filePath)) - File.Delete(filePath); - - using (FileStream streamWriter = File.Create(filePath)) - { - int size = 2048; - byte[] data = new byte[2048]; - while (true) - { - size = s.Read(data, 0, data.Length); - if (size > 0) - { - streamWriter.Write(data, 0, size); - } - else - { - break; - } - } - } - } + // create directory + if (!string.IsNullOrEmpty(directoryPath)) + { + if (!Directory.Exists(directoryPath)) + Directory.CreateDirectory(directoryPath); } + + var filePath = Path.Combine(directoryPath, zipArchiveEntry.Name); + + if (File.Exists(filePath)) + File.Delete(filePath); + + zipArchiveEntry.ExtractToFile(filePath); } - return true; - } - catch (Exception) - { - throw; } - } + return true; + } /// /// Compresses the memory byte array. diff --git a/SimpleHelper.Core/SimpleHelper.Core.csproj b/SimpleHelper.Core/SimpleHelper.Core.csproj index 979b954..f3e6407 100644 --- a/SimpleHelper.Core/SimpleHelper.Core.csproj +++ b/SimpleHelper.Core/SimpleHelper.Core.csproj @@ -22,14 +22,13 @@ false true - 0.0.7 + 0.0.8 false https://github.com/Nonobis/SimpleHelper/blob/master/LICENSE -