Exemple #1
0
        /// <summary>
        ///  This method will result in a complete parsing of the EndOfCentralDirectory
        /// and CentralDirectory records (if it hasn't been done yet).
        /// After that it will check whether central directory contains the file.
        /// If it is present it will parse local fileheader, and remove their in memory
        /// representation
        /// </summary>
        internal void DeleteFile(string zipFileName)
        {
            CheckDisposed();

            if (_openAccess == FileAccess.Read)
            {
                throw new InvalidOperationException(SR.Get(SRID.CanNotWriteInReadOnlyMode));
            }

            // Validate parameteres
            zipFileName = ZipIOBlockManager.ValidateNormalizeFileName(zipFileName);

            if (FileExists(zipFileName)) // is it in central Directory ?
            {
                ZipFileInfo fileInfoToBeDeleted = GetFile(zipFileName);

                //this should invalidate any outstanding collections
                // and update central directory status as appropriate
                ZipFileInfoDictionary.Remove(zipFileName);

                //this should remove the local file block
                // from the blockManager's collection
                _blockManager.RemoveLocalFileBlock(fileInfoToBeDeleted.LocalFileBlock);
            }
        }
Exemple #2
0
        /// <summary>
        ///  This method will result in a complete parsing of the EndOfCentralDirectory
        /// and CentralDirectory records (if it hasn't been done yet).
        /// After that (assuming no duplicates were found). It will create in appropriate
        /// in memory Local FileHeaders and Central Directory Headers.
        /// </summary>
        internal ZipFileInfo AddFile(string zipFileName, CompressionMethodEnum compressionMethod, DeflateOptionEnum deflateOption)
        {
            CheckDisposed();

            if (_openAccess == FileAccess.Read)
            {
                throw new InvalidOperationException(SR.Get(SRID.CanNotWriteInReadOnlyMode));
            }

            // Validate parameteres
            zipFileName = ZipIOBlockManager.ValidateNormalizeFileName(zipFileName);

            if ((compressionMethod != CompressionMethodEnum.Stored) &&
                (compressionMethod != CompressionMethodEnum.Deflated))
            {
                throw new ArgumentOutOfRangeException("compressionMethod");
            }

            // non-contiguous range requires more complex test
            if (deflateOption < DeflateOptionEnum.Normal || (
                    deflateOption > DeflateOptionEnum.SuperFast && deflateOption != DeflateOptionEnum.None))
            {
                throw new ArgumentOutOfRangeException("deflateOption");
            }

            // Check for duplicates ,
            if (FileExists(zipFileName))
            {
                throw new System.InvalidOperationException(SR.Get(SRID.AttemptedToCreateDuplicateFileName));
            }

            // Create Local File Block through Block Manager
            ZipIOLocalFileBlock fileBlock = _blockManager.CreateLocalFileBlock(zipFileName, compressionMethod, deflateOption);

            //build new ZipFileInfo and add reference to the collection, so we can keep track of the instances of the ZipFileInfo,
            // that were given out  and invalidate any collection that was returned on GetFiles calls
            ZipFileInfo zipFileInfo = new ZipFileInfo(this, fileBlock);

            ZipFileInfoDictionary.Add(zipFileInfo.Name, zipFileInfo);

            return(zipFileInfo);
        }
Exemple #3
0
        /// <summary>
        ///  This method will result in a complete parsing of the EndOfCentralDirectory
        /// and CentralDirectory records (if it hasn't been done yet).
        /// After that (assuming the file was found). It will parse the apropriate local file block
        /// header and data descriptor (if present).
        /// </summary>
        internal ZipFileInfo GetFile(string zipFileName)
        {
            CheckDisposed();

            if (_openAccess == FileAccess.Write)
            {
                throw new InvalidOperationException(SR.Get(SRID.CanNotReadInWriteOnlyMode));
            }

            // Validate parameteres
            zipFileName = ZipIOBlockManager.ValidateNormalizeFileName(zipFileName);

            // try to get it from the ZipFileInfo dictionary
            if (ZipFileInfoDictionary.Contains(zipFileName))
            {
                // this ZipFileInfo was already built through AddFile or GetFile(s)
                // we have this cached
                return((ZipFileInfo)(ZipFileInfoDictionary[zipFileName]));
            }
            else
            {
                // we need to check whether it is present in the central directory
                if (!FileExists(zipFileName))
                {
                    throw new InvalidOperationException(SR.Get(SRID.FileDoesNotExists));
                }

                // Load Local File Block through Block Manager
                ZipIOLocalFileBlock fileBlock = _blockManager.LoadLocalFileBlock(zipFileName);

                // build new ZipFileInfo and add reference to the collection, so we can keep track of the instances of the ZipFileInfo,
                // that were given out  and invalidate any collection that was returned on GetFiles calls
                ZipFileInfo zipFileInfo = new ZipFileInfo(this, fileBlock);

                //this should invalidate any outstanding collections
                ZipFileInfoDictionary.Add(zipFileInfo.Name, zipFileInfo);

                return(zipFileInfo);
            }
        }
        /// <summary>
        ///  This method will result in a complete parsing of the EndOfCentralDirectory 
        /// and CentralDirectory records (if it hasn't been done yet). 
        /// After that (assuming the file was found). It will parse the apropriate local file block 
        /// header and data descriptor (if present).
        /// </summary>
        internal ZipFileInfo GetFile(string zipFileName)
        {
            CheckDisposed();        

            if (_openAccess == FileAccess.Write)
            {
                throw new InvalidOperationException(SR.Get(SRID.CanNotReadInWriteOnlyMode));
            }

            // Validate parameteres 
            zipFileName = ZipIOBlockManager.ValidateNormalizeFileName(zipFileName);

            // try to get it from the ZipFileInfo dictionary 
            if (ZipFileInfoDictionary.Contains(zipFileName))
            {
                // this ZipFileInfo was already built through AddFile or GetFile(s)
                // we have this cached 
                return (ZipFileInfo)(ZipFileInfoDictionary[zipFileName]);
            }
            else
            {
                // we need to check whether it is present in the central directory 
                if (!FileExists(zipFileName))
                {
                    throw new InvalidOperationException(SR.Get(SRID.FileDoesNotExists));                
                }

                // Load Local File Block through Block Manager 
                ZipIOLocalFileBlock fileBlock = _blockManager.LoadLocalFileBlock(zipFileName);

                // build new ZipFileInfo and add reference to the collection, so we can keep track of the instances of the ZipFileInfo, 
                // that were given out  and invalidate any collection that was returned on GetFiles calls
                ZipFileInfo zipFileInfo = new ZipFileInfo(this, fileBlock);

                //this should invalidate any outstanding collections                 
                ZipFileInfoDictionary.Add(zipFileInfo.Name, zipFileInfo);

                return zipFileInfo;
            }
        }
        /// <summary>
        ///  This method will result in a complete parsing of the EndOfCentralDirectory 
        /// and CentralDirectory records (if it hasn't been done yet). 
        /// After that (assuming no duplicates were found). It will create in appropriate 
        /// in memory Local FileHeaders and Central Directory Headers.  
        /// </summary>
        internal ZipFileInfo AddFile(string zipFileName, CompressionMethodEnum compressionMethod, DeflateOptionEnum deflateOption)
        {
            CheckDisposed();

            if (_openAccess == FileAccess.Read)
            {
                throw new InvalidOperationException(SR.Get(SRID.CanNotWriteInReadOnlyMode));
            }

            // Validate parameteres 
            zipFileName = ZipIOBlockManager.ValidateNormalizeFileName(zipFileName);

            if ((compressionMethod != CompressionMethodEnum.Stored) && 
                (compressionMethod != CompressionMethodEnum.Deflated))
            {
                throw new ArgumentOutOfRangeException("compressionMethod");            
            }

            // non-contiguous range requires more complex test
            if (deflateOption < DeflateOptionEnum.Normal || (
                deflateOption > DeflateOptionEnum.SuperFast && deflateOption != DeflateOptionEnum.None))
            {
                throw new ArgumentOutOfRangeException("deflateOption");            
            }

            // Check for duplicates , 
            if (FileExists(zipFileName))
            {
                throw new System.InvalidOperationException(SR.Get(SRID.AttemptedToCreateDuplicateFileName));            
            }
                
            // Create Local File Block through Block Manager 
            ZipIOLocalFileBlock fileBlock = _blockManager.CreateLocalFileBlock(zipFileName, compressionMethod, deflateOption);

            //build new ZipFileInfo and add reference to the collection, so we can keep track of the instances of the ZipFileInfo, 
            // that were given out  and invalidate any collection that was returned on GetFiles calls
            ZipFileInfo zipFileInfo = new ZipFileInfo(this, fileBlock);
            ZipFileInfoDictionary.Add(zipFileInfo.Name, zipFileInfo);
            
            return zipFileInfo; 
        }
Exemple #6
0
     //------------------------------------------------------
     //
     //  Public Events
     //
     //------------------------------------------------------
     // None
     //------------------------------------------------------
     //
     //  Internal Constructors
     //
     //------------------------------------------------------
 
     #region Internal Constructors
     
     /// <summary>
     /// Constructs a ZipPackagePart for an atomic (i.e. non-interleaved) part.
     /// This is called from the ZipPackage class as a result of GetPartCore,
     /// GetPartsCore or CreatePartCore methods     
     /// </summary>
     /// <param name="container"></param>
     /// <param name="zipFileInfo"></param>
     /// <param name="partUri"></param>
     /// <param name="compressionOption"></param>
     /// <param name="contentType"></param>
     internal ZipPackagePart(ZipPackage container, 
         ZipFileInfo zipFileInfo,
         PackUriHelper.ValidatedPartUri partUri, 
         string contentType,
         CompressionOption compressionOption)
         :base(container, partUri, contentType, compressionOption)
     {
         _zipArchive = zipFileInfo.ZipArchive;
         _zipFileInfo = zipFileInfo;
     }