示例#1
0
        private async Task ExtractContents(ICollection <ZipArchiveEntry> entries, string destination,
                                           IEntry baseEntry = null, IOperationProgress progressObserver = null)
        {
            var total  = entries.Count;
            var copied = 0;

            foreach (var entry in entries)
            {
                var filePath = entry.Key.Substring(baseEntry?.Key.Length ?? 0);

                var destFile = Path.Combine(destination, filePath.Replace("/", "\\"));
                var dir      = Path.GetDirectoryName(destFile);
                if (!fileSystemOperations.DirectoryExists(dir))
                {
                    fileSystemOperations.CreateDirectory(dir);
                }

                using (var destStream = File.Open(destFile, FileMode.OpenOrCreate))
                    using (var stream = entry.OpenEntryStream())
                    {
                        await stream.CopyToAsync(destStream);

                        copied++;
                        progressObserver?.Percentage.OnNext(copied / (double)total);
                    }
            }

            progressObserver?.Percentage.OnNext(double.NaN);
        }
        /// <summary>
        /// Returns an empty stream which can be used to write a new package to the store.
        /// </summary>
        /// <param name="packageId">The ID of the package to create.</param>
        /// <param name="packageVersion">The version of the package to create.</param>
        /// <returns>Empty stream backed by the new package.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="packageId"/> is null or contains only whitespace or <paramref name="packageVersion"/> is null.</exception>
        public override Stream CreatePackage(string packageId, SemanticVersion packageVersion)
        {
            if (string.IsNullOrWhiteSpace(packageId))
            {
                throw new ArgumentNullException("packageId");
            }
            if (packageVersion == null)
            {
                throw new ArgumentNullException("packageVersion");
            }

            InitPackageStore();

            _logger.Debug("CreatePackage('" + packageId + "', '" + packageVersion + "') called");

            try
            {
                var packagePath = Path.Combine(RootPath, packageId);
                _logger.DebugFormat("Creating package '{0}', version '{1}' in directory '{2}'", packageId, packageVersion, packagePath);
                _fileSystemOperations.CreateDirectory(packagePath);

                var versionPath = Path.Combine(packagePath, packageId + "." + packageVersion + ".nupkg");
                _logger.DebugFormat("Creating package '{0}', version '{1}' at '{2}'", packageId, packageVersion, versionPath);

                return(_fileSystemOperations.GetFileStream(versionPath, FileMode.Create, FileAccess.Write, FileShare.Delete));
            }
            catch (Exception ex)
            {
                _logger.Error(String.Format("Error creating package '{0}', version '{1}'.", packageId, packageVersion), ex);
                throw;
            }
        }
示例#3
0
        private async Task ExtractContents(ZipArchiveEntry baseEntry,
                                           IEnumerable <ZipArchiveEntry> entries)
        {
            foreach (var entry in entries)
            {
                var filePath = entry.FullName.Substring(baseEntry.FullName.Length);

                var destFile = Path.Combine(destination, filePath.Replace("/", "\\"));
                var dir      = Path.GetDirectoryName(destFile);
                if (!fileSystemOperations.DirectoryExists(dir))
                {
                    fileSystemOperations.CreateDirectory(dir);
                }

                using (var destStream = File.Open(destFile, FileMode.OpenOrCreate))
                    using (var stream = entry.Open())
                    {
                        await stream.CopyToAsync(destStream);
                    }
            }
        }