Exemplo n.º 1
0
        // Retrieve part name. The position points to the slash past the part name.
        // So simply return the prefix up to that slash.
        private static bool FindPartName(string path, ref int position, ref ScanStepDelegate nextStep,
                                         ref PieceNameInfo parseResults)
        {
            parseResults.PrefixName = path.Substring(0, position);

            // Subtract the length of the part name from position.
            position = 0;

            if (parseResults.PrefixName.Length == 0)
            {
                return(false);
            }

            Uri partUri = new Uri(ZipPackage.GetOpcNameFromZipItemName(parseResults.PrefixName), UriKind.Relative);

            PackUriHelper.TryValidatePartUri(partUri, out parseResults.PartUri);
            return(true);
        }
Exemplo n.º 2
0
        //------------------------------------------------------
        //
        //   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);
            }
        }