protected virtual IPackage OpenPackage(string path)
        {
            var nuspecPath = Path.ChangeExtension(path, Constants.ManifestExtension);

            if (FileSystem.FileExists(nuspecPath))
            {
                return(new UnzippedPackage(FileSystem, Path.GetFileNameWithoutExtension(nuspecPath)));
            }

            ZipPackage package;

            try
            {
                package = new ZipPackage(() => FileSystem.OpenFile(path), _enableCaching);
            }
            catch (FileFormatException ex)
            {
                throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, NuGetResources.ErrorReadingPackage, path), ex);
            }
            // Set the last modified date on the package
            package.Published = FileSystem.GetLastModified(path);

            // Clear the cache whenever we open a new package file
            ZipPackage.ClearCache(package);
            return(package);
        }
        internal IPackage DownloadAndVerifyPackage(IPackageRepository repository)
        {
            if (String.IsNullOrEmpty(PackageHash))
            {
                throw new InvalidOperationException(NuGetResources.PackageContentsVerifyError);
            }

            IPackage package = null;

            // If OldHash is null, we're looking at a new instance of the data service package.
            // The package might be stored in the cache so we're going to try the looking there before attempting a download.
            if (OldHash == null)
            {
                package = GetPackage(repository);
            }

            if (package == null)
            {
                byte[] hashBytes = Convert.FromBase64String(PackageHash);

                package = Downloader.DownloadPackage(DownloadUrl, hashBytes, this);

                // Add the package to the cache
                repository.AddPackage(package);

                // Clear the cache for this package
                ZipPackage.ClearCache(package);
            }

            // Update the hash
            OldHash = PackageHash;

            return(package);
        }
        protected virtual IPackage OpenPackage(string path)
        {
            var package = new ZipPackage(() => FileSystem.OpenFile(path), _enableCaching);

            // Set the last modified date on the package
            package.Published = FileSystem.GetLastModified(path);

            // Clear the cache whenever we open a new package file
            ZipPackage.ClearCache(package);
            return package;
        }
        internal void EnsurePackage(IPackageRepository cacheRepository)
        {
            // OData caches instances of DataServicePackage while updating their property values. As a result,
            // the ZipPackage that we downloaded may no longer be valid (as indicated by a newer hash).
            // When using MachineCache, once we've verified that the hashes match (which happens the first time around),
            // we'll simply verify the file exists between successive calls.
            IPackageMetadata packageMetadata = this;
            bool             refreshPackage  = _package == null ||
                                               !String.Equals(OldHash, PackageHash, StringComparison.OrdinalIgnoreCase) ||
                                               (_usingMachineCache && !cacheRepository.Exists(Id, packageMetadata.Version));

            if (refreshPackage &&
                TryGetPackage(cacheRepository, packageMetadata, out _package) &&
                _package.GetHash(HashProvider).Equals(PackageHash, StringComparison.OrdinalIgnoreCase))
            {
                OldHash = PackageHash;

                // Reset the flag so that we no longer need to download the package since it exists and is valid.
                refreshPackage = false;

                // Make a note that we the backing store for the ZipPackage is the machine cache.
                _usingMachineCache = true;
            }

            if (refreshPackage)
            {
                // We either do not have a package available locally or they are invalid. Download the package from the server.
                _package = Downloader.DownloadPackage(DownloadUrl, this);

                // Make a note that we are using an in-memory instance of the package.
                _usingMachineCache = false;

                // Add the package to the cache
                cacheRepository.AddPackage(_package);

                OldHash = PackageHash;

                // Clear any cached items for this package
                ZipPackage.ClearCache(_package);
            }
        }