//------------------------------------------------------ // // Private Methods // //------------------------------------------------------ private void EnsurePieceStream(bool isLastPiece) { if (_pieceStream != null) { // Normally, the pieces are actually closed automatically for us by the // underlying ZipIO logic, but in the case of the last piece (when we // are called by our own Dispose(bool) method) we must close it explicitly. if (isLastPiece) { _pieceStream.Close(); } // We detect that the stream has been closed by inspecting the CanWrite property // since this is guaranteed not to throw even when the stream is disposed. if (!_pieceStream.CanWrite) { // increment our piece number so we can generate the correct // one below checked { ++_currentPieceNumber; } // release it to trigger the new piece creation below _pieceStream = null; } } if (_pieceStream == null) { string pieceName = PieceNameHelper.CreatePieceName( _partName, _currentPieceNumber, isLastPiece); string pieceZipName = ZipPackage.GetZipItemNameFromOpcName(pieceName); ZipFileInfo zipFileInfo = _archive.AddFile(pieceZipName, _compressionMethod, _deflateOption); // We've just created the file, so the mode can only be Create, not CreateNew. // (At least, this is part of ZipFileInfo's belief system.) _pieceStream = zipFileInfo.GetStream(FileMode.Create, _access); } }