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);
        }
Exemple #2
0
        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);
            }

            if (options == null)
            {
                options = GetWriterOptions();
                options.LeaveStreamOpen = false;
            }

            Stream archiveStream = null, progressStream = Stream.Null;

            try
            {
                archiveStream = await archive.OpenStreamForWriteAsync();

                progressStream = new ProgressObservableStream(this, archiveStream);

                using (var writer = WriterFactory.Open(progressStream, _type, options))
                {
                    foreach (var file in files)
                    {
                        Token.ThrowIfCancellationRequested();
                        using (var inputStream = await file.OpenStreamForReadAsync())
                        {
                            await writer.WriteAsync(file.Name, inputStream, Token);
                        }
                    }
                }
            }
            finally
            {
                if (!options.LeaveStreamOpen)
                {
                    archiveStream?.Dispose();
                    progressStream.Dispose();
                }
            }

            return(progressStream);
        }
        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);
        }
Exemple #4
0
        public override async Task <Stream> Decompress(StorageFile archive, StorageFolder location,
                                                       IReadOnlyList <FileEntry> entries, ReaderOptions options = null)
        {
            if (archive == null || entries.IsNullOrEmpty() || location == null)
            {
                return(Stream.Null);
            }

            options = options ?? new ReaderOptions {
                LeaveStreamOpen = false
            };
            Stream archiveStream = null, progressStream = Stream.Null;

            try
            {
                archiveStream = await archive.OpenStreamForReadAsync();

                progressStream = new ProgressObservableStream(this, archiveStream);

                using (var reader = ReaderFactory.Open(progressStream, options))
                {
                    while (reader.MoveToNextEntry())
                    {
                        Token.ThrowIfCancellationRequested();
                        if (entries.Any(entry => reader.Entry.Key.Equals(entry.Key)))
                        {
                            await WriteEntry(reader, location);
                        }
                    }
                }
            }
            finally
            {
                if (!options.LeaveStreamOpen)
                {
                    archiveStream?.Dispose();
                    progressStream.Dispose();
                }
            }

            return(progressStream);
        }