Universal compression library for UWP projects
Source |
Original Project
SharpCompress is a fully managed C# library to deal with many compression types and formats.
The project originally developed by Adam Hathcock
Based on the Original SharpCompress 0.30.1
In progress, currently only two main functions ported
-
WriteEntryToDirectory
-
AddAllFromDirectory
-
More functions in progress
It will support any UWP project (build 14393+)
but if you want to compile your project with .NETNativeToolChain use 16299+ as min target
You will be able for now to compress or decompress with specific functions
var zipFile = await folder.CreateFileAsync("testFile.zip", CreationCollisionOption.ReplaceExisting);
using (var stream = await zipFile.OpenStreamForWriteAsync())
using (var archive = ZipArchive.Create())
{
//To avoid UI block run this code into Task
await archive.AddAllFromDirectory(targetFolder);
//AddAllFromDirectory can extended to:
//AddAllFromDirectory(storageFolder, string[] searchPattern, SearchOption.AllDirectories, IProgress<int> progress, bool IncludeRootFolder, CancellationTokenSource cancellationTokenSource)
//IProgress<int> will report how many file queued
archive.SaveTo(stream);
//SaveTo can extended to:
//SaveTo(Stream stream, IProgress<Dictionary<string, long>> progress, CancellationTokenSource cancellationTokenSource)
//IProgress<Dictionary<string, long>> will provide file name / size like below:
//string fileName = value.FirstOrDefault().Key;
//string size = value.FirstOrDefault().Value.ToFileSize();
}
Stream zipStream = await zipFile.OpenStreamForReadAsync();
using (var zipArchive = ArchiveFactory.Open(zipStream)){
//It should support 7z, zip, rar, gz, tar
var reader = zipArchive.ExtractAllEntries();
//Bind progress event
reader.EntryExtractionProgress += (sender, e) =>
{
var entryProgress = e.ReaderProgress.PercentageReadExact;
var sizeProgress = e.ReaderProgress.BytesTransferred.ToFileSize();
};
//Extract files
while (reader.MoveToNextEntry()){
if (!reader.Entry.IsDirectory){
await reader.WriteEntryToDirectory(destinationFolder, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true });
//WriteEntryToDirectory can extended to:
//WriteEntryToDirectory(folder, options, cancellationTokenSource)
// ^^^^^^^^^^^^^^^^^^^^^^^
//it will help to terminate the current entry directly if cancellation requested
}
}
}
Developed by Adam Hathcock
UWP Port Bashar Astifan