Exemple #1
0
        /// <inheritdoc />
        protected override void CompressInternal(Stream source, Stream target)
        {
            if (source.CanSeek)
            {
                source.Position = 0;
            }

            string     entryName   = source.GetFileName() ?? "_";
            ZipArchive archive     = null;
            Stream     entryStream = null;

            try
            {
                archive = new ZipArchive(target, CompressionMode.ToSystemCompressionMode(), true);
                ZipArchiveEntry entry = archive.CreateEntry(entryName, CompressionLevel.ToSystemCompressionLevel());
                entry.LastWriteTime = DateTimeOffset.Now;
                entryStream         = entry.Open();
                source.CopyTo(entryStream);
                entryStream.Flush();
                target.Flush();
            }
            finally
            {
                ObjectHelper.Dispose(ref entryStream);
                ObjectHelper.Dispose(ref archive);
            }
        }
Exemple #2
0
        /// <inheritdoc />
        protected override async Task CompressInternalAsync(Stream source, Stream target, CancellationToken token = default(CancellationToken))
        {
            token.ThrowIfCancellationRequested();
            if (source.CanSeek)
            {
                source.Position = 0;
            }

            string     entryName   = source.GetFileName() ?? "_";
            ZipArchive archive     = null;
            Stream     entryStream = null;

            try
            {
                archive = new ZipArchive(target, CompressionMode.ToSystemCompressionMode(), true);
                ZipArchiveEntry entry = archive.CreateEntry(entryName, CompressionLevel.ToSystemCompressionLevel());
                entry.LastWriteTime = DateTimeOffset.Now;
                entryStream         = entry.Open();
                await source.CopyToAsync(entryStream, Constants.BUFFER_16_KB, token);

                await entryStream.FlushAsync(token);

                await target.FlushAsync(token);
            }
            finally
            {
                ObjectHelper.Dispose(ref entryStream);
                ObjectHelper.Dispose(ref archive);
            }
        }
Exemple #3
0
        protected override Task CompressInternalAsync(string sourcePath, string destinationPath, CancellationToken token = default(CancellationToken))
        {
            token.ThrowIfCancellationRequested();
            sourcePath      = PathHelper.Trim(sourcePath) ?? throw new ArgumentNullException(nameof(sourcePath));
            destinationPath = PathHelper.Trim(destinationPath) ?? throw new ArgumentNullException(nameof(destinationPath));
            if (!DirectoryHelper.Ensure(destinationPath))
            {
                throw new DirectoryNotFoundException();
            }
            token.ThrowIfCancellationRequested();

            string fileName;
            string newPath;

            if (File.Exists(sourcePath))
            {
                // compressing one file
                fileName = Path.GetFileNameWithoutExtension(sourcePath);
                newPath  = Path.Combine(destinationPath, fileName + ".zip");
                ZipArchive zip = null;

                try
                {
                    zip = ZipFile.Open(newPath, CompressionMode.ToSystemCompressionMode(), Encoding.UTF8);
                    token.ThrowIfCancellationRequested();
                    zip.CreateEntryFromFile(sourcePath, fileName, CompressionLevel.ToSystemCompressionLevel());
                    token.ThrowIfCancellationRequested();
                }
                finally
                {
                    ObjectHelper.Dispose(ref zip);
                }

                return(Task.CompletedTask);
            }

            if (!Directory.Exists(sourcePath))
            {
                throw new NotFoundException("File or directory not found.");
            }
            fileName = Path.GetDirectoryName(sourcePath);
            newPath  = Path.Combine(destinationPath, fileName + ".zip");
            token.ThrowIfCancellationRequested();
            ZipFile.CreateFromDirectory(sourcePath, newPath, CompressionLevel.ToSystemCompressionLevel(), PreserveDirectoryRoot, Encoding.UTF8);
            token.ThrowIfCancellationRequested();
            return(Task.CompletedTask);
        }
Exemple #4
0
        /// <inheritdoc />
        protected override void CompressInternal(Stream source, Stream target)
        {
            if (source.CanSeek)
            {
                source.Position = 0;
            }

            GZipStream gZipStream = null;

            try
            {
                gZipStream = new GZipStream(target, CompressionLevel.ToSystemCompressionLevel(), true);
                source.CopyTo(gZipStream);
                gZipStream.Flush();
                target.Flush();
            }
            finally
            {
                ObjectHelper.Dispose(ref gZipStream);
            }
        }
Exemple #5
0
        /// <inheritdoc />
        protected override async Task CompressInternalAsync(Stream source, Stream target, CancellationToken token = default(CancellationToken))
        {
            token.ThrowIfCancellationRequested();
            if (source.CanSeek)
            {
                source.Position = 0;
            }

            GZipStream gZipStream = null;

            try
            {
                gZipStream = new GZipStream(target, CompressionLevel.ToSystemCompressionLevel(), true);
                await source.CopyToAsync(gZipStream, Constants.BUFFER_16_KB, token);

                await gZipStream.FlushAsync(token);

                await target.FlushAsync(token);
            }
            finally
            {
                ObjectHelper.Dispose(ref gZipStream);
            }
        }