internal ZipFileInfo AddFile(string zipFileName, CompressionMethodEnum compressionMethod, DeflateOptionEnum deflateOption) { this.CheckDisposed(); if (this._openAccess == FileAccess.Read) { throw new InvalidOperationException(SR.Get("CanNotWriteInReadOnlyMode")); } zipFileName = ZipIOBlockManager.ValidateNormalizeFileName(zipFileName); if ((compressionMethod != CompressionMethodEnum.Stored) && (compressionMethod != CompressionMethodEnum.Deflated)) { throw new ArgumentOutOfRangeException("compressionMethod"); } if ((deflateOption < DeflateOptionEnum.Normal) || ((deflateOption > DeflateOptionEnum.SuperFast) && (deflateOption != DeflateOptionEnum.None))) { throw new ArgumentOutOfRangeException("deflateOption"); } if (this.FileExists(zipFileName)) { throw new InvalidOperationException(SR.Get("AttemptedToCreateDuplicateFileName")); } ZipIOLocalFileBlock fileBlock = this._blockManager.CreateLocalFileBlock(zipFileName, compressionMethod, deflateOption); ZipFileInfo info = new ZipFileInfo(this, fileBlock); this.ZipFileInfoDictionary.Add(info.Name, info); return(info); }
public ZipFileInfo AddFile(string path, CompressionMethodEnum compmeth = CompressionMethodEnum.Deflated, DeflateOptionEnum option = DeflateOptionEnum.Normal) { var comp = GetExternalType("MS.Internal.IO.Zip.CompressionMethodEnum").GetStaticField(compmeth.ToString()); var opti = GetExternalType("MS.Internal.IO.Zip.DeflateOptionEnum").GetStaticField(option.ToString()); return(new ZipFileInfo { external = external.Call("AddFile", path, comp, opti) }); }
public ZipFileInfo AddFile(string path, CompressionMethodEnum compmeth, DeflateOptionEnum option) { var type = external.GetType(); var meth = type.GetMethod("AddFile", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); var comp = type.Assembly.GetType("MS.Internal.IO.Zip.CompressionMethodEnum").GetField(compmeth.ToString()).GetValue(null); var opti = type.Assembly.GetType("MS.Internal.IO.Zip.DeflateOptionEnum").GetField(option.ToString()).GetValue(null); return(new ZipFileInfo(meth.Invoke(external, new object[] { path, comp, opti }))); }
public NET35ZipFileEntry AddFile(string path, CompressionMethodEnum compmeth = CompressionMethodEnum.Deflated, DeflateOptionEnum option = DeflateOptionEnum.Normal) { var comp = msCompressionMethodEnumType.GetField(compmeth.ToString()).GetValue(null); var opti = msDeflateOptionEnumType.GetField(option.ToString()).GetValue(null); return(new NET35ZipFileEntry(zipArchiveWrapper.Invoke(this.external, "AddFile", path, comp, opti))); }
internal ZipIOLocalFileBlock CreateLocalFileBlock(string zipFileName, CompressionMethodEnum compressionMethod, DeflateOptionEnum deflateOption) { this.CheckDisposed(); ZipIOLocalFileBlock block = ZipIOLocalFileBlock.CreateNew(this, zipFileName, compressionMethod, deflateOption); this.InsertBlock(this.CentralDirectoryBlockIndex, block); this.CentralDirectoryBlock.AddFileBlock(block); this.DirtyFlag = true; return(block); }
public int Load(byte[] buffer, long offset) { long value; BufferTools.ReadNumberFromBuffer(buffer, offset, num_bytes, out value); if (Enum.IsDefined(typeof(CompressionMethodEnum), value)) { this.value = (CompressionMethodEnum)value; } return(num_bytes); }
/// <summary> /// Create a new LocalFileHeader /// </summary> /// <param name="fileName"></param> /// <param name="encoding"></param> /// <param name="compressionMethod"></param> /// <param name="deflateOption"></param> /// <param name="streaming">true if in streaming mode</param> /// <returns></returns> internal static ZipIOLocalFileHeader CreateNew(string fileName, Encoding encoding, CompressionMethodEnum compressionMethod, DeflateOptionEnum deflateOption, bool streaming) { //this should be ensured by the higher levels Debug.Assert(Enum.IsDefined(typeof(CompressionMethodEnum), compressionMethod)); Debug.Assert(Enum.IsDefined(typeof(DeflateOptionEnum), deflateOption)); byte[] asciiName = encoding.GetBytes(fileName); if (asciiName.Length > ZipIOBlockManager.MaxFileNameSize) { throw new ArgumentOutOfRangeException("fileName"); } ZipIOLocalFileHeader header = new ZipIOLocalFileHeader(); header._signature = ZipIOLocalFileHeader._signatureConstant; header._compressionMethod = (ushort)compressionMethod; if (streaming) { header._versionNeededToExtract = (UInt16)ZipIOVersionNeededToExtract.Zip64FileFormat; } else { header._versionNeededToExtract = (UInt16)ZipIOBlockManager.CalcVersionNeededToExtractFromCompression( compressionMethod); } if (compressionMethod != CompressionMethodEnum.Stored) { Debug.Assert(deflateOption != DeflateOptionEnum.None); //this should be ensured by the higher levels header.DeflateOption = deflateOption; } if (streaming) { // set bit 3 header.StreamingCreationFlag = true; } header._lastModFileDateTime = ZipIOBlockManager.ToMsDosDateTime(DateTime.Now); header._fileNameLength = (UInt16)asciiName.Length; header._fileName = asciiName; header._extraField = ZipIOExtraField.CreateNew(!streaming /* creating padding if it is not in streaming creation mode */); header._extraFieldLength = header._extraField.Size; //populate frequently used field with user friendly data representations header._stringFileName = fileName; return(header); }
internal static ZipIOVersionNeededToExtract CalcVersionNeededToExtractFromCompression(CompressionMethodEnum compression) { CompressionMethodEnum enum2 = compression; if (enum2 != CompressionMethodEnum.Stored) { if (enum2 != CompressionMethodEnum.Deflated) { throw new NotSupportedException(); } return(ZipIOVersionNeededToExtract.DeflatedData); } return(ZipIOVersionNeededToExtract.StoredData); }
/// <summary> /// Create a new LocalFileHeader /// </summary> /// <param name="fileName"></param> /// <param name="encoding"></param> /// <param name="compressionMethod"></param> /// <param name="deflateOption"></param> /// <param name="streaming">true if in streaming mode</param> /// <returns></returns> internal static ZipIOLocalFileHeader CreateNew(string fileName, Encoding encoding, CompressionMethodEnum compressionMethod, DeflateOptionEnum deflateOption, bool streaming) { //this should be ensured by the higher levels Debug.Assert(Enum.IsDefined(typeof(CompressionMethodEnum), compressionMethod)); Debug.Assert(Enum.IsDefined(typeof(DeflateOptionEnum), deflateOption)); byte[] asciiName = encoding.GetBytes(fileName); if (asciiName.Length > ZipIOBlockManager.MaxFileNameSize) { throw new ArgumentOutOfRangeException("fileName"); } ZipIOLocalFileHeader header = new ZipIOLocalFileHeader(); header._signature = ZipIOLocalFileHeader._signatureConstant; header._compressionMethod = (ushort)compressionMethod; if (streaming) header._versionNeededToExtract = (UInt16)ZipIOVersionNeededToExtract.Zip64FileFormat; else { header._versionNeededToExtract = (UInt16)ZipIOBlockManager.CalcVersionNeededToExtractFromCompression( compressionMethod); } if (compressionMethod != CompressionMethodEnum.Stored) { Debug.Assert(deflateOption != DeflateOptionEnum.None); //this should be ensured by the higher levels header.DeflateOption = deflateOption; } if (streaming) { // set bit 3 header.StreamingCreationFlag = true; } header._lastModFileDateTime = ZipIOBlockManager.ToMsDosDateTime(DateTime.Now); header._fileNameLength = (UInt16)asciiName.Length; header._fileName = asciiName; header._extraField = ZipIOExtraField.CreateNew(!streaming /* creating padding if it is not in streaming creation mode */); header._extraFieldLength = header._extraField.Size; //populate frequently used field with user friendly data representations header._stringFileName = fileName; return header; }
internal static ZipIOLocalFileBlock CreateNew(ZipIOBlockManager blockManager, string fileName, CompressionMethodEnum compressionMethod, DeflateOptionEnum deflateOption) { //this should be ensured by the higher levels Debug.Assert(Enum.IsDefined(typeof(CompressionMethodEnum), compressionMethod)); Debug.Assert(Enum.IsDefined(typeof(DeflateOptionEnum), deflateOption)); ZipIOLocalFileBlock block = new ZipIOLocalFileBlock(blockManager, false, false); block._localFileHeader = ZipIOLocalFileHeader.CreateNew (fileName, blockManager.Encoding, compressionMethod, deflateOption, blockManager.Streaming); // if in streaming mode - force to Zip64 mode in case the streams get large if (blockManager.Streaming) { block._localFileDataDescriptor = ZipIOLocalFileDataDescriptor.CreateNew(); } block._offset = 0; // intial value, that is not too important for the brand new File item block._dirtyFlag = true; block._fileItemStream = new ZipIOFileItemStream(blockManager, block, block._offset + block._localFileHeader.Size, 0); // create deflate wrapper if necessary if (compressionMethod == CompressionMethodEnum.Deflated) { Debug.Assert(block._fileItemStream.Position == 0, "CompressStream assumes base stream is at position zero"); // Pass bool to indicate that this stream is "new" and must be dirty so that // the valid empty deflate stream is emitted (2-byte sequence - see CompressStream for details). block._deflateStream = new CompressStream(block._fileItemStream, 0, true); block._crcCalculatingStream = new ProgressiveCrcCalculatingStream(blockManager, block._deflateStream); } else { block._crcCalculatingStream = new ProgressiveCrcCalculatingStream(blockManager, block._fileItemStream); } return(block); }
/// <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); }
//------------------------------------------------------ // // Constructors // //------------------------------------------------------ /// <summary> /// Build a System.IO.Stream to create a multi-piece (i.e. interleaved) part. /// Does not require a part, but a proper part name (not a piece name), and a ZipArchive. /// </summary> internal StreamingZipPartStream( string partName, ZipArchive zipArchive, CompressionMethodEnum compressionMethod, DeflateOptionEnum deflateOption, FileMode mode, FileAccess access) { // Right now, only production is supported in streaming mode. if (!( (mode == FileMode.Create || mode == FileMode.CreateNew) && access == FileAccess.Write) ) { throw new NotSupportedException(SR.Get(SRID.OnlyStreamingProductionIsSupported)); } _partName = partName; _archive = zipArchive; _compressionMethod = compressionMethod; _deflateOption = deflateOption; _mode = mode; _access = access; }
//------------------------------------------------------ // // Constructors // //------------------------------------------------------ /// <summary> /// Build a System.IO.Stream to create a multi-piece (i.e. interleaved) part. /// Does not require a part, but a proper part name (not a piece name), and a ZipArchive. /// </summary> internal StreamingZipPartStream( string partName, ZipArchive zipArchive, CompressionMethodEnum compressionMethod, DeflateOptionEnum deflateOption, FileMode mode, FileAccess access) { // Right now, only production is supported in streaming mode. if (!((mode == FileMode.Create || mode == FileMode.CreateNew) && access == FileAccess.Write)) { throw new NotSupportedException(SR.Get(SRID.OnlyStreamingProductionIsSupported)); } _partName = partName; _archive = zipArchive; _compressionMethod = compressionMethod; _deflateOption = deflateOption; _mode = mode; _access = access; }
internal static ZipIOLocalFileHeader CreateNew(string fileName, Encoding encoding, CompressionMethodEnum compressionMethod, DeflateOptionEnum deflateOption, bool streaming) { byte[] bytes = encoding.GetBytes(fileName); if (bytes.Length > ZipIOBlockManager.MaxFileNameSize) { throw new ArgumentOutOfRangeException("fileName"); } ZipIOLocalFileHeader header = new ZipIOLocalFileHeader(); header._signature = 0x4034b50; header._compressionMethod = (ushort)compressionMethod; if (streaming) { header._versionNeededToExtract = 0x2d; } else { header._versionNeededToExtract = (ushort)ZipIOBlockManager.CalcVersionNeededToExtractFromCompression(compressionMethod); } if (compressionMethod != CompressionMethodEnum.Stored) { header.DeflateOption = deflateOption; } if (streaming) { header.StreamingCreationFlag = true; } header._lastModFileDateTime = ZipIOBlockManager.ToMsDosDateTime(DateTime.Now); header._fileNameLength = (ushort)bytes.Length; header._fileName = bytes; header._extraField = ZipIOExtraField.CreateNew(!streaming); header._extraFieldLength = header._extraField.Size; header._stringFileName = fileName; return(header); }
public MyZipFileInfo AddFile(string path, CompressionMethodEnum compressionMethod = CompressionMethodEnum.Deflated, DeflateOptionEnum deflateOption = DeflateOptionEnum.Normal) { return(new MyZipFileInfo(MyZipArchiveReflection.AddFile(m_zip, path, (ushort)compressionMethod, (byte)deflateOption))); }
internal static ZipIOLocalFileBlock CreateNew(ZipIOBlockManager blockManager, string fileName, CompressionMethodEnum compressionMethod, DeflateOptionEnum deflateOption) { //this should be ensured by the higher levels Debug.Assert(Enum.IsDefined(typeof(CompressionMethodEnum), compressionMethod)); Debug.Assert(Enum.IsDefined(typeof(DeflateOptionEnum), deflateOption)); ZipIOLocalFileBlock block = new ZipIOLocalFileBlock(blockManager, false, false); block._localFileHeader = ZipIOLocalFileHeader.CreateNew (fileName, blockManager.Encoding, compressionMethod, deflateOption, blockManager.Streaming); // if in streaming mode - force to Zip64 mode in case the streams get large if (blockManager.Streaming) { block._localFileDataDescriptor = ZipIOLocalFileDataDescriptor.CreateNew(); } block._offset = 0; // intial value, that is not too important for the brand new File item block._dirtyFlag = true; block._fileItemStream = new ZipIOFileItemStream(blockManager, block, block._offset + block._localFileHeader.Size, 0); // create deflate wrapper if necessary if (compressionMethod == CompressionMethodEnum.Deflated) { Debug.Assert(block._fileItemStream.Position == 0, "CompressStream assumes base stream is at position zero"); // Pass bool to indicate that this stream is "new" and must be dirty so that // the valid empty deflate stream is emitted (2-byte sequence - see CompressStream for details). block._deflateStream = new CompressStream(block._fileItemStream, 0, true); block._crcCalculatingStream = new ProgressiveCrcCalculatingStream(blockManager, block._deflateStream); } else { block._crcCalculatingStream = new ProgressiveCrcCalculatingStream(blockManager, block._fileItemStream); } return block; }
/// <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; }
internal ZipIOLocalFileBlock CreateLocalFileBlock(string zipFileName, CompressionMethodEnum compressionMethod, DeflateOptionEnum deflateOption) { CheckDisposed(); // we are guaranteed uniqueness at this point , so let's just add a // block at the end of the file, just before the central directory // construct Block find it and parse it // STREAMING Mode: // NOTE: _blockList is NOT in offset order except the last four blocks // (CD, Zip64 EOCD, Zip64 EOCD Locator, and EOCD) ZipIOLocalFileBlock localFileBlock = ZipIOLocalFileBlock.CreateNew(this, zipFileName, compressionMethod, deflateOption); InsertBlock(CentralDirectoryBlockIndex, localFileBlock); CentralDirectoryBlock.AddFileBlock(localFileBlock); DirtyFlag = true; return localFileBlock; }
internal static ZipIOVersionNeededToExtract CalcVersionNeededToExtractFromCompression (CompressionMethodEnum compression) { switch (compression) { case CompressionMethodEnum.Stored: return ZipIOVersionNeededToExtract.StoredData; case CompressionMethodEnum.Deflated: return ZipIOVersionNeededToExtract.DeflatedData; default: throw new NotSupportedException(); // Deflated64 this is OFF } }
/// <summary> /// If the logical end precedes the physical end, delete invalidated pieces /// and rename the logical end to a name containing ".last". /// </summary> private void UpdatePhysicalEndIfNecessary() { if (!_logicalEndPrecedesPhysicalEnd) { return; } // Delete invalidated pieces. int pieceNumber = _lastPieceIndex + 1; while (pieceNumber < _sortedPieceInfoList.Count) { _zipArchive.DeleteFile(_sortedPieceInfoList[pieceNumber].ZipFileInfo.Name); pieceNumber++; } _sortedPieceInfoList.RemoveRange(_lastPieceIndex + 1, _sortedPieceInfoList.Count - (_lastPieceIndex + 1)); // Since there is no rename in Zip I/O, getting the last piece to have .last // in its name necessarily involves creating a new piece. The simplest and most // effective solution consists in adding an empty terminal piece. // Number of the new physical last piece. int lastPiece = _lastPieceIndex + 1; // Record the compression parameters of the first piece to apply them to the new piece. // (Though this part will be created as empty, it may grow later.) ZipFileInfo firstPieceInfo = _sortedPieceInfoList[0].ZipFileInfo; CompressionMethodEnum compressionMethod = firstPieceInfo.CompressionMethod; DeflateOptionEnum deflateOption = firstPieceInfo.DeflateOption; // We have to special-case SetLength(0), because in that case, there is no nonempty // piece at all; and only the last piece is allowed to be empty. if (_lastPieceIndex == 0 && _pieceStreamInfoList[0].Stream.Length == 0) { _zipArchive.DeleteFile(firstPieceInfo.Name); // The list of piece descriptors now becomes totally empty. // This temporarily violates an invariant that should obtain again // on exiting this function. _indexOfLastPieceStreamInfoAccessed = -1; //Remove all the items in the list _pieceStreamInfoList.Clear(); lastPiece = 0; // Create "[0].last.piece" } string newLastPieceFileName = PieceNameHelper.CreatePieceName( _sortedPieceInfoList[0].PrefixName, lastPiece, true /* last piece */); ZipFileInfo newLastPieceInfo = _zipArchive.AddFile(newLastPieceFileName, compressionMethod, deflateOption); _lastPieceIndex = lastPiece; //We need to update the _sortedPieceInfoList with this new last piece information _sortedPieceInfoList.Add( new PieceInfo( newLastPieceInfo, _sortedPieceInfoList[0].PartUri, _sortedPieceInfoList[0].PrefixName, _lastPieceIndex, true /* last piece */)); // If we have been creating [0].last.piece, create a stream descriptor for it. // (In other cases, create on demand, as usual.) if (lastPiece == 0) { Stream pieceStream = newLastPieceInfo.GetStream(_fileMode, _fileAccess); _indexOfLastPieceStreamInfoAccessed = 0; //The list should be empty at this point Invariant.Assert(_pieceStreamInfoList.Count == 0); _pieceStreamInfoList.Add(new PieceStreamInfo(pieceStream, 0 /*startOffset*/)); } // Mark update complete. _logicalEndPrecedesPhysicalEnd = false; }
internal static ZipIOLocalFileBlock CreateNew(ZipIOBlockManager blockManager, string fileName, CompressionMethodEnum compressionMethod, DeflateOptionEnum deflateOption) { ZipIOLocalFileBlock block = new ZipIOLocalFileBlock(blockManager, false, false); block._localFileHeader = ZipIOLocalFileHeader.CreateNew(fileName, blockManager.Encoding, compressionMethod, deflateOption, blockManager.Streaming); if (blockManager.Streaming) { block._localFileDataDescriptor = ZipIOLocalFileDataDescriptor.CreateNew(); } block._offset = 0L; block._dirtyFlag = true; block._fileItemStream = new ZipIOFileItemStream(blockManager, block, block._offset + block._localFileHeader.Size, 0L); if (compressionMethod == CompressionMethodEnum.Deflated) { block._deflateStream = new CompressStream(block._fileItemStream, 0L, true); block._crcCalculatingStream = new ProgressiveCrcCalculatingStream(blockManager, block._deflateStream); return(block); } block._crcCalculatingStream = new ProgressiveCrcCalculatingStream(blockManager, block._fileItemStream); return(block); }