/// <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); } }
internal static ZipIOCentralDirectoryFileHeader ParseRecord(BinaryReader reader, Encoding encoding) { ZipIOCentralDirectoryFileHeader header = new ZipIOCentralDirectoryFileHeader(encoding); header._signature = reader.ReadUInt32(); header._versionMadeBy = reader.ReadUInt16(); header._versionNeededToExtract = reader.ReadUInt16(); header._generalPurposeBitFlag = reader.ReadUInt16(); header._compressionMethod = reader.ReadUInt16(); header._lastModFileDateTime = reader.ReadUInt32(); header._crc32 = reader.ReadUInt32(); header._compressedSize = reader.ReadUInt32(); header._uncompressedSize = reader.ReadUInt32(); header._fileNameLength = reader.ReadUInt16(); header._extraFieldLength = reader.ReadUInt16(); header._fileCommentLength = reader.ReadUInt16(); header._diskNumberStart = reader.ReadUInt16(); header._internalFileAttributes = reader.ReadUInt16(); header._externalFileAttributes = reader.ReadUInt32(); header._relativeOffsetOfLocalHeader = reader.ReadUInt32(); header._fileName = reader.ReadBytes(header._fileNameLength); // check for the ZIP 64 version and escaped values ZipIOZip64ExtraFieldUsage zip64extraFieldUsage = ZipIOZip64ExtraFieldUsage.None; if (header._versionNeededToExtract >= (ushort)ZipIOVersionNeededToExtract.Zip64FileFormat) { if (header._compressedSize == UInt32.MaxValue) { zip64extraFieldUsage |= ZipIOZip64ExtraFieldUsage.CompressedSize; } if (header._uncompressedSize == UInt32.MaxValue) { zip64extraFieldUsage |= ZipIOZip64ExtraFieldUsage.UncompressedSize; } if (header._relativeOffsetOfLocalHeader == UInt32.MaxValue) { zip64extraFieldUsage |= ZipIOZip64ExtraFieldUsage.OffsetOfLocalHeader; } if (header._diskNumberStart == UInt16.MaxValue) { zip64extraFieldUsage |= ZipIOZip64ExtraFieldUsage.DiskNumber; } } // if the ZIP 64 record is missing the zip64extraFieldUsage value will be ignored header._extraField = ZipIOExtraField.ParseRecord(reader, zip64extraFieldUsage, header._extraFieldLength); header._fileComment = reader.ReadBytes(header._fileCommentLength); //populate frequently used field with user friendly data representations header._stringFileName = ZipIOBlockManager.ValidateNormalizeFileName(encoding.GetString(header._fileName)); header.Validate(); return(header); }
/// <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. /// It will not attempt the parsing of the local file headers / descriptors. /// </summary> internal bool FileExists(string zipFileName) { CheckDisposed(); // Validate parameteres zipFileName = ZipIOBlockManager.ValidateNormalizeFileName(zipFileName); return(_blockManager.CentralDirectoryBlock.FileExists(zipFileName)); }
/// <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); }
/// <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); } }