Skip to content

Commit

Permalink
Entries support in CompressorAlgorithm (file name update)
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolocust committed Mar 2, 2019
1 parent 3730719 commit 5e0ed71
Showing 1 changed file with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;

Expand All @@ -38,9 +39,8 @@ namespace SimpleZIP_UI.Application.Compression.Algorithm
/// </summary>
public abstract class CompressorAlgorithm : AbstractAlgorithm
{
/// <inheritdoc />
public override async Task<Stream> Decompress(StorageFile archive, StorageFolder location,
ReaderOptions options = null)
private async Task<Stream> DecompressArchive(IStorageFile archive, IStorageFolder location,
IReadOnlyCollection<FileEntry> entries, bool collectFileNames, ReaderOptions options = null)
{
if (archive == null || location == null) return Stream.Null;

Expand Down Expand Up @@ -68,7 +68,15 @@ public override async Task<Stream> Decompress(StorageFile archive, StorageFolder
await outputStream.WriteAsync(bytes, 0, readBytes, Token);
}
}

await GZipOutputFileNameWorkaround(file, compressorStream);

// update file name of corresponding entry
if (collectFileNames && !entries.IsNullOrEmpty())
{
var entry = entries.ElementAt(0);
entry.FileName = file.Name;
}
}
finally
{
Expand All @@ -77,21 +85,33 @@ public override async Task<Stream> Decompress(StorageFile archive, StorageFolder
compressorStream.Dispose();
}
}

return compressorStream;
}

/// <inheritdoc />
public override async Task<Stream> Decompress(StorageFile archive, StorageFolder location,
ReaderOptions options = null)
{
return await DecompressArchive(archive, location, null, false, options);
}

/// <inheritdoc />
/// <summary>Entries are not supported in non-archive formats,
/// but will be used if file names are to be collected.</summary>
public override async Task<Stream> Decompress(StorageFile archive, StorageFolder location,
IReadOnlyList<FileEntry> entries, bool collectFileNames, ReaderOptions options = null)
{
return await Decompress(archive, location, options); // ignore entries as they are not supported
return await DecompressArchive(archive, location, entries, collectFileNames, options);
}

/// <inheritdoc />
/// <summary>Entries are not supported in non-archive formats,
/// and hence will be ignored.</summary>
public sealed override async Task<Stream> Decompress(StorageFile archive, StorageFolder location,
IReadOnlyList<FileEntry> entries, ReaderOptions options = null)
{
return await Decompress(archive, location, options); // ignore entries as they are not supported
return await DecompressArchive(archive, location, entries, false, options);
}

/// <inheritdoc />
Expand Down Expand Up @@ -128,6 +148,7 @@ public override async Task<Stream> Compress(IReadOnlyList<StorageFile> files, St
compressorStream.Dispose();
}
}

return compressorStream;
}

Expand All @@ -149,6 +170,7 @@ await file.RenameAsync(gzipStream.FileName,
NameCollisionOption.GenerateUniqueName);
return true;
}

return false;
}

Expand Down

0 comments on commit 5e0ed71

Please sign in to comment.