public override async Task <Stream> Decompress(StorageFile archive, StorageFolder location, ReaderOptions options = null) { if (archive == null || location == null) { return(Stream.Null); } var compressorOptions = new CompressorOptions { IsCompression = false }; Stream archiveStream = null, progressStream = null, compressorStream = Stream.Null; try { archiveStream = await archive.OpenStreamForReadAsync(); progressStream = new ProgressObservableStream(this, archiveStream); compressorStream = GetCompressorStream(progressStream, compressorOptions); var outputFileName = archive.Name.Substring(0, archive.Name.Length - archive.FileType.Length); var file = await location.CreateFileAsync(outputFileName, CreationCollisionOption.GenerateUniqueName); if (file == null) { return(Stream.Null); // file was not created } using (var outputStream = await file.OpenStreamForWriteAsync()) { var bytes = new byte[DefaultBufferSize]; int readBytes; while ((readBytes = compressorStream.Read(bytes, 0, bytes.Length)) > 0) { await outputStream.WriteAsync(bytes, 0, readBytes, Token); } } await GZipOutputFileNameWorkaround(file, compressorStream); } finally { if (options != null && !options.LeaveStreamOpen) { archiveStream?.Dispose(); progressStream?.Dispose(); compressorStream.Dispose(); } } return(compressorStream); }
protected override Stream GetCompressorStream(Stream stream, CompressorOptions options) { var compressorStream = options.IsCompression ? new GZipStream(stream, CompressionMode.Compress, CompressionLevel.BestSpeed) : new GZipStream(stream, CompressionMode.Decompress); // set file name to stream var fileName = options.FileName; if (!string.IsNullOrEmpty(fileName)) { compressorStream.FileName = fileName; } return(compressorStream); }
public override async Task <Stream> Compress(IReadOnlyList <StorageFile> files, StorageFile archive, StorageFolder location, WriterOptions options = null) { if (files.IsNullOrEmpty() || archive == null || location == null) { return(Stream.Null); } var file = files[0]; // since multiple files are not supported var compressorOptions = new CompressorOptions { FileName = file.Name, IsCompression = true }; Stream archiveStream = null, progressStream = null, compressorStream = Stream.Null; try { archiveStream = await archive.OpenStreamForWriteAsync(); progressStream = new ProgressObservableStream(this, archiveStream); compressorStream = GetCompressorStream(progressStream, compressorOptions); using (var inputStream = await file.OpenStreamForReadAsync()) { var bytes = new byte[DefaultBufferSize]; int readBytes; while ((readBytes = inputStream.Read(bytes, 0, bytes.Length)) > 0) { await compressorStream.WriteAsync(bytes, 0, readBytes, Token); } } } finally { if (options != null && !options.LeaveStreamOpen) { archiveStream?.Dispose(); progressStream?.Dispose(); compressorStream.Dispose(); } } return(compressorStream); }
/// <summary> /// Gets the concrete compressor stream from the derived class. /// </summary> /// <param name="stream">The stream to be decorated.</param> /// <param name="options">Options to be applied.</param> /// <returns>Compressor stream which can be used for compression.</returns> protected abstract Stream GetCompressorStream(Stream stream, CompressorOptions options);
protected override Stream GetCompressorStream(Stream stream, CompressorOptions options) { return(options.IsCompression ? new BZip2Stream(stream, CompressionMode.Compress) : new BZip2Stream(stream, CompressionMode.Decompress)); }