-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from VahidFarahmandian/compression
compression and decompression added
- Loading branch information
Showing
14 changed files
with
230 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
01-Core/Jinget.Core/Utilities/Compression/ZipUtility.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using Ionic.Zip; | ||
using Ionic.Zlib; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Jinget.Core.Utilities.Compression | ||
{ | ||
public class ZipUtility | ||
{ | ||
/// <param name="files">each file will be compressed idependently</param> | ||
/// <param name="maxDOP">max degree of parallelism</param> | ||
/// <param name="eachFileMaxSize">each file will be compressed and then chunked based on this parameter. eachFileMaxSize is in MB</param> | ||
/// <param name="path">location to store compressed files</param> | ||
/// <param name="password">compressed files can also become password protected</param> | ||
/// <param name="compressionLevel">It is up to you to choose between best speed to high compression</param> | ||
/// <param name="encryption">compressed files can also become encrypted too!</param> | ||
public async Task CompressAsync( | ||
FileInfo[] files, | ||
string path, | ||
int maxDOP = 1, | ||
int eachFileMaxSize = 5, | ||
string password = "", | ||
CompressionLevel compressionLevel = CompressionLevel.BestSpeed, | ||
EncryptionAlgorithm encryption = EncryptionAlgorithm.None | ||
) | ||
{ | ||
if (eachFileMaxSize < 1) | ||
eachFileMaxSize = 5; | ||
|
||
var allTasks = new List<Task>(); | ||
|
||
var threadCount = files.Length < maxDOP ? files.Length : maxDOP; | ||
var throttler = new SemaphoreSlim(threadCount); | ||
|
||
foreach (var file in files) | ||
{ | ||
await throttler.WaitAsync(); | ||
allTasks.Add(Task.Factory.StartNew(() => | ||
{ | ||
var fileName = Path.GetFileNameWithoutExtension(file.Name); | ||
using ZipFile zip = new ZipFile(); | ||
if (!string.IsNullOrWhiteSpace(password)) | ||
zip.Password = password; | ||
zip.BufferSize = eachFileMaxSize * 1024 * 1024; | ||
zip.MaxOutputSegmentSize64 = eachFileMaxSize * 1024 * 1024; | ||
zip.UseZip64WhenSaving = Zip64Option.AsNecessary; | ||
zip.Encryption = encryption; | ||
zip.CompressionLevel = compressionLevel; | ||
zip.AddFile(file.FullName); | ||
zip.Save(Path.Combine(path, $"{file.Name}.zip")); | ||
throttler.Release(); | ||
})); | ||
} | ||
|
||
Task.WaitAll(allTasks.ToArray()); | ||
} | ||
|
||
/// <param name="files">list of files to extract</param> | ||
/// <param name="maxDOP">max degree of parallelism</param> | ||
/// <param name="path">location to extract the files</param> | ||
/// <param name="password">password used to extract the compressed files</param> | ||
public async Task DecompressAsync( | ||
FileInfo[] files, | ||
string path, | ||
int maxDOP = 1, | ||
string password = "") | ||
{ | ||
var allTasks = new List<Task>(); | ||
var threadCount = files.Length < maxDOP ? files.Length : maxDOP; | ||
var throttler = new SemaphoreSlim(threadCount); | ||
|
||
foreach (var file in files) | ||
{ | ||
await throttler.WaitAsync(); | ||
|
||
allTasks.Add(Task.Factory.StartNew(() => | ||
{ | ||
using ZipFile zip1 = ZipFile.Read(file.FullName); | ||
zip1.FlattenFoldersOnExtract = true; | ||
foreach (ZipEntry e in zip1) | ||
{ | ||
if (string.IsNullOrWhiteSpace(password)) | ||
{ | ||
e.Extract(path, ExtractExistingFileAction.OverwriteSilently); | ||
} | ||
else | ||
e.ExtractWithPassword(path, ExtractExistingFileAction.OverwriteSilently, password); | ||
} | ||
throttler.Release(); | ||
})); | ||
} | ||
Task.WaitAll(allTasks.ToArray()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
Tests/Jinget.Core.Tests/ExtensionMethods/Reflection/PropertiesExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
Tests/Jinget.Core.Tests/Utilities/Compression/ZipUtilityTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using System.IO; | ||
using System; | ||
|
||
namespace Jinget.Core.Utilities.Compression.Tests | ||
{ | ||
[TestClass()] | ||
public class ZipUtilityTests | ||
{ | ||
ZipUtility zip; | ||
[TestInitialize] | ||
public void Initialize() => zip = new ZipUtility(); | ||
|
||
[TestMethod()] | ||
public async Task should_compress_and_chunkAsync() | ||
{ | ||
FileInfo file = new("sample.txt"); | ||
int maxDOP = 5; | ||
int eachFileMaxSize = 4; | ||
|
||
List<FileInfo> files = new() | ||
{ | ||
file | ||
}; | ||
await zip.CompressAsync(files.ToArray(), files[0].DirectoryName, maxDOP, eachFileMaxSize); | ||
|
||
Assert.IsTrue(file.Directory.GetFiles("sample.txt.z??").Any()); | ||
} | ||
|
||
[TestMethod()] | ||
public async Task should_compress_using_password_and_chunkAsync() | ||
{ | ||
FileInfo file = new("sample.txt"); | ||
string password = "123"; | ||
List<FileInfo> files = new() | ||
{ | ||
file | ||
}; | ||
await zip.CompressAsync(files.ToArray(), files[0].DirectoryName, password: password); | ||
|
||
Assert.IsTrue(file.Directory.GetFiles("sample.txt.z??").Any()); | ||
} | ||
|
||
[TestMethod()] | ||
public async Task should_decompressed_file() | ||
{ | ||
string fileName = Guid.NewGuid() + ".txt"; | ||
using (var tw = new StreamWriter(fileName, true)) | ||
{ | ||
tw.WriteLine("sample text"); | ||
} | ||
string password = "123"; | ||
var tobeCompressed = new FileInfo[] { new FileInfo(fileName) }; | ||
await zip.CompressAsync(tobeCompressed, tobeCompressed[0].DirectoryName, password: password); | ||
File.Delete(fileName); | ||
|
||
FileInfo file = new($"{fileName}.zip"); | ||
int maxDOP = 5; | ||
|
||
List<FileInfo> files = new() | ||
{ | ||
file | ||
}; | ||
await zip.DecompressAsync(files.ToArray(), files[0].DirectoryName, maxDOP); | ||
|
||
Assert.IsTrue(file.Directory.GetFiles(fileName).Any()); | ||
} | ||
|
||
[TestMethod()] | ||
public async Task should_decompressed_using_password() | ||
{ | ||
string fileName = Guid.NewGuid() + ".txt"; | ||
using (var tw = new StreamWriter(fileName, true)) | ||
{ | ||
tw.WriteLine("sample text"); | ||
} | ||
string password = "123"; | ||
var tobeCompressed = new FileInfo[] { new FileInfo(fileName) }; | ||
await zip.CompressAsync(tobeCompressed, tobeCompressed[0].DirectoryName, password: password); | ||
File.Delete(fileName); | ||
|
||
FileInfo file = new($"{fileName}.zip"); | ||
List<FileInfo> files = new() | ||
{ | ||
file | ||
}; | ||
await zip.DecompressAsync(files.ToArray(), files[0].DirectoryName, password: password); | ||
|
||
Assert.IsTrue(file.Directory.GetFiles(fileName).Any()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
2 changes: 0 additions & 2 deletions
2
...Handlers.ExternalServiceHandlers.Tests/DefaultServiceHandler/JingetServiceHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters