private List <IPackageFile> GetFilesNoCache()
        {
            using (Stream stream = _streamFactory())
            {
                Package package   = Package.Open(stream);
                var     packageId = ZipPackage.GetPackageIdentifier(package);

                return((from part in package.GetParts()
                        where IsPackageFile(part, packageId)
                        select(IPackageFile) new ZipPackageFile(part)).ToList());
            }
        }
        private static void CreatePart(Package package, string path, Stream sourceStream)
        {
            if (PackageHelper.IsPackageManifest(path, ZipPackage.GetPackageIdentifier(package)))
            {
                return;
            }

            Uri uri = UriUtility.CreatePartUri(path);

            // Create the part
            PackagePart packagePart = package.CreatePart(uri, DefaultContentType, CompressionOption.Maximum);

            using (Stream stream = packagePart.GetStream())
            {
                sourceStream.CopyTo(stream);
            }
        }
        public override void ExtractContents(IFileSystem fileSystem, string extractPath)
        {
            using (Stream stream = _streamFactory())
            {
                var package   = Package.Open(stream);
                var packageId = ZipPackage.GetPackageIdentifier(package);

                foreach (var part in package.GetParts()
                         .Where(p => IsPackageFile(p, packageId)))
                {
                    var relativePath = UriUtility.GetPath(part.Uri);

                    var targetPath = Path.Combine(extractPath, relativePath);
                    using (var partStream = part.GetStream())
                    {
                        fileSystem.AddFile(targetPath, partStream);
                    }
                }
            }
        }
        private void EnsurePackageFiles()
        {
            if (_files != null &&
                _expandedFolderPath != null &&
                _expandedFileSystem.DirectoryExists(_expandedFolderPath))
            {
                return;
            }

            _files = new Dictionary <string, PhysicalPackageFile>();
            _supportedFrameworks = null;

            var packageName = new PackageName(Id, Version);

            // Only use the cache for expanded folders under %temp%, or set from unit tests
            if (_expandedFileSystem == _tempFileSystem || _forceUseCache)
            {
                Tuple <string, DateTimeOffset> cacheValue;
                DateTimeOffset lastModifiedTime = _fileSystem.GetLastModified(_packagePath);

                // if the cache doesn't exist, or it exists but points to a stale package,
                // then we invalidate the cache and store the new entry.
                if (!_cachedExpandedFolder.TryGetValue(packageName, out cacheValue) ||
                    cacheValue.Item2 < lastModifiedTime)
                {
                    cacheValue = Tuple.Create(GetExpandedFolderPath(), lastModifiedTime);
                    _cachedExpandedFolder[packageName] = cacheValue;
                }

                _expandedFolderPath = cacheValue.Item1;
            }
            else
            {
                _expandedFolderPath = GetExpandedFolderPath();
            }

            using (Stream stream = GetStream())
            {
                Package package   = Package.Open(stream);
                var     packageId = ZipPackage.GetPackageIdentifier(package);
                // unzip files inside package
                var files = from part in package.GetParts()
                            where ZipPackage.IsPackageFile(part, packageId)
                            select part;

                // now copy all package's files to disk
                foreach (PackagePart file in files)
                {
                    string path     = UriUtility.GetPath(file.Uri);
                    string filePath = Path.Combine(_expandedFolderPath, path);

                    bool copyFile = true;
                    if (_expandedFileSystem.FileExists(filePath))
                    {
                        using (Stream partStream = file.GetStream(),
                               targetStream = _expandedFileSystem.OpenFile(filePath))
                        {
                            // if the target file already exists,
                            // don't copy file if the lengths are equal.
                            copyFile = partStream.Length != targetStream.Length;
                        }
                    }

                    if (copyFile)
                    {
                        using (Stream partStream = file.GetStream())
                        {
                            try
                            {
                                using (Stream targetStream = _expandedFileSystem.CreateFile(filePath))
                                {
                                    partStream.CopyTo(targetStream);
                                }
                            }
                            catch (Exception)
                            {
                                // if the file is read-only or has an access denied issue, we just ignore it
                            }
                        }
                    }

                    var packageFile = new PhysicalPackageFile
                    {
                        SourcePath = _expandedFileSystem.GetFullPath(filePath),
                        TargetPath = path
                    };

                    _files[path] = packageFile;
                }
            }
        }