System.IO.Compression.ZipArchiveFile.CopyStream C# (CSharp) Méthode

CopyStream() static private méthode

Copies from one stream to another.
static private CopyStream ( Stream fromStream, Stream toStream ) : int
fromStream Stream The input stream.
toStream Stream The output stream.
Résultat int
        internal static int CopyStream(Stream fromStream, Stream toStream)
        {
            byte[] buffer = new byte[8192];
            int totalBytes = 0;
            for (;;)
            {
                int count = fromStream.Read(buffer, 0, buffer.Length);
                if (count == 0)
                {
                    break;
                }
                toStream.Write(buffer, 0, count);
                totalBytes += count;
            }
            return totalBytes;
        }

Usage Example

        /// <summary>
        /// Copies 'sourceFilePath from the textStream system to the archive as
        /// 'targetArchivePath'. Will overwrite any existing textStream.
        /// </summary>
        /// <param name="sourceFilePath">The source filename.</param>
        /// <param name="targetArchivePath">The target path.</param>
        public void CopyFromFile(string sourceFilePath, string targetArchivePath)
        {
            using (Stream inFile = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite))
            {
                using (Stream outFile = Create(targetArchivePath))
                {
                    ZipArchiveFile.CopyStream(inFile, outFile);
                }
            }

            this[targetArchivePath].LastWriteTime = File.GetLastWriteTime(sourceFilePath);
        }