Skip to content

Commit

Permalink
No more dependancy to SharpZipLib
Browse files Browse the repository at this point in the history
Increment version to 0.0.8
  • Loading branch information
Nonobis committed May 12, 2018
1 parent 6892491 commit 89053b6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 64 deletions.
125 changes: 63 additions & 62 deletions SimpleHelper.Core/CompressHelper.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Gs the zip.
/// Compress the string with GZIP.
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
Expand Down Expand Up @@ -48,7 +49,7 @@ public static string GZip(string value)
}

/// <summary>
/// Uns the G zip.
/// Uncompress the string with GZIP.
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
Expand Down Expand Up @@ -95,80 +96,80 @@ public static string UnGZip(string value)
/// <param name="filepath">The filepath.</param>
/// <param name="outputDirectory">The output directory.</param>
/// <returns></returns>
public static bool UnzipToFolder(string filepath, string outputDirectory)
{
return DecompressToFolder(filepath, outputDirectory);
}

/// <summary>
/// Unzips to folder.
/// </summary>
/// <param name="filepath">The filepath.</param>
/// <param name="outputDirectory">The output directory.</param>
/// <param name="excludeFiles">The exclude files.</param>
/// <returns></returns>
public static bool UnzipToFolder(string filepath, string outputDirectory, List<string> excludeFiles)
{
return UnzipToFolder(filepath, outputDirectory);
}

/// <summary>
/// Decompresses to folder.
/// </summary>
/// <param name="filepath">The filepath.</param>
/// <param name="outputDirectory">The output directory.</param>
/// <param name="excludeFiles">The exclude files.</param>
/// <returns></returns>
/// <exception cref="Exception">
/// </exception>
public static bool UnzipToFolder(string filepath, string outputDirectory)
private static bool DecompressToFolder(string filepath, string outputDirectory, List<string> 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;
}

/// <summary>
/// Compresses the memory byte array.
Expand Down
3 changes: 1 addition & 2 deletions SimpleHelper.Core/SimpleHelper.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageReleaseNotes></PackageReleaseNotes>
<Version>0.0.7</Version>
<Version>0.0.8</Version>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageLicenseUrl>https://github.com/Nonobis/SimpleHelper/blob/master/LICENSE</PackageLicenseUrl>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NuGet.CommandLine" Version="4.6.2" />
<PackageReference Include="SharpZipLib.NETStandard" Version="1.0.7" />
<PackageReference Include="System.Drawing.Primitives" Version="4.3.0" />
<PackageReference Include="System.IO" Version="4.3.0" />
<PackageReference Include="System.Net.NetworkInformation" Version="4.3.0" />
Expand Down

0 comments on commit 89053b6

Please sign in to comment.