C#FileStream复制大文件

<p>即每次复制文件的一小段,以节省总内存开销。当然,本机复制也可以采用.NET内部的System.IO.File.Copy方法。</p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: gray;">///</span><span style="color: green;"> </span></span><span style="">复制文件<span lang="EN-US"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;param name="fromFile"&gt;</span></span><span style="">要复制的文件</span><span style="" lang="EN-US">&lt;/param&gt;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;param name="toFile"&gt;</span></span><span style="">要保存的位置</span><span style="" lang="EN-US">&lt;/param&gt;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;param name="lengthEachTime"&gt;</span></span><span style="">每次复制的长度</span><span style="" lang="EN-US">&lt;/param&gt;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">private</span> <span style="color: blue;">void</span> <span style="color: #010001;">CopyFile</span>(<span style="color: blue;">string</span> <span style="color: #010001;">fromFile</span>, <span style="color: blue;">string</span> <span style="color: #010001;">toFile</span>, <span style="color: blue;">int</span> <span style="color: #010001;">lengthEachTime</span>)</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #2b91af;">FileStream</span> <span style="color: #010001;">fileToCopy</span> = <span style="color: blue;">new</span> <span style="color: #2b91af;">FileStream</span>(<span style="color: #010001;">fromFile</span>, <span style="color: #2b91af;">FileMode</span>.<span style="color: #010001;">Open</span>, <span style="color: #2b91af;">FileAccess</span>.<span style="color: #010001;">Read</span>);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #2b91af;">FileStream</span> <span style="color: #010001;">copyToFile</span> = <span style="color: blue;">new</span> <span style="color: #2b91af;">FileStream</span>(<span style="color: #010001;">toFile</span>, <span style="color: #2b91af;">FileMode</span>.<span style="color: #010001;">Append</span>, <span style="color: #2b91af;">FileAccess</span>.<span style="color: #010001;">Write</span>);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">int</span> <span style="color: #010001;">lengthToCopy</span>;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">if</span> (<span style="color: #010001;">lengthEachTime</span> &lt; <span style="color: #010001;">fileToCopy</span>.<span style="color: #010001;">Length</span>)<span style="color: green;">//</span></span><span style="">如果分段拷贝,即每次拷贝内容小于文件总长度<span lang="EN-US"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">byte</span>[] <span style="color: #010001;">buffer</span> = <span style="color: blue;">new</span> <span style="color: blue;">byte</span>[<span style="color: #010001;">lengthEachTime</span>];</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">int</span> <span style="color: #010001;">copied</span> = 0;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">while</span> (<span style="color: #010001;">copied</span> &lt;= ((<span style="color: blue;">int</span>)<span style="color: #010001;">fileToCopy</span>.<span style="color: #010001;">Length</span> - <span style="color: #010001;">lengthEachTime</span>))<span style="color: green;">//</span></span><span style="">拷贝主体部分<span lang="EN-US"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">lengthToCopy</span> = <span style="color: #010001;">fileToCopy</span>.<span style="color: #010001;">Read</span>(<span style="color: #010001;">buffer</span>, 0, <span style="color: #010001;">lengthEachTime</span>);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">fileToCopy</span>.<span style="color: #010001;">Flush</span>();</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">copyToFile</span>.<span style="color: #010001;">Write</span>(<span style="color: #010001;">buffer</span>, 0, <span style="color: #010001;">lengthEachTime</span>);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">copyToFile</span>.<span style="color: #010001;">Flush</span>();</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">copyToFile</span>.<span style="color: #010001;">Position</span> = <span style="color: #010001;">fileToCopy</span>.<span style="color: #010001;">Position</span>;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">copied</span> += <span style="color: #010001;">lengthToCopy</span>;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">int</span> <span style="color: #010001;">left</span> = (<span style="color: blue;">int</span>)<span style="color: #010001;">fileToCopy</span>.<span style="color: #010001;">Length</span> - <span style="color: #010001;">copied</span>;<span style="color: green;">//</span></span><span style="">拷贝剩余部分<span lang="EN-US"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">lengthToCopy</span> = <span style="color: #010001;">fileToCopy</span>.<span style="color: #010001;">Read</span>(<span style="color: #010001;">buffer</span>, 0, <span style="color: #010001;">left</span>);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">fileToCopy</span>.<span style="color: #010001;">Flush</span>();</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">copyToFile</span>.<span style="color: #010001;">Write</span>(<span style="color: #010001;">buffer</span>, 0, <span style="color: #010001;">left</span>);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">copyToFile</span>.<span style="color: #010001;">Flush</span>();</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">else</span><span style="color: green;">//</span></span><span style="">如果整体拷贝,即每次拷贝内容大于文件总长度<span lang="EN-US"></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span>{</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: blue;">byte</span>[] <span style="color: #010001;">buffer</span> = <span style="color: blue;">new</span> <span style="color: blue;">byte</span>[<span style="color: #010001;">fileToCopy</span>.<span style="color: #010001;">Length</span>];</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">fileToCopy</span>.<span style="color: #010001;">Read</span>(<span style="color: #010001;">buffer</span>,0,(<span style="color: blue;">int</span>)<span style="color: #010001;">fileToCopy</span>.<span style="color: #010001;">Length</span>);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">fileToCopy</span>.<span style="color: #010001;">Flush</span>();</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">copyToFile</span>.<span style="color: #010001;">Write</span>(<span style="color: #010001;">buffer</span>, 0, (<span style="color: blue;">int</span>)<span style="color: #010001;">fileToCopy</span>.<span style="color: #010001;">Length</span>);</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">copyToFile</span>.<span style="color: #010001;">Flush</span>();</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span>}</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">fileToCopy</span>.<span style="color: #010001;">Close</span>();</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="" lang="EN-US"><span style=""> </span><span style="color: #010001;">copyToFile</span>.<span style="color: #010001;">Close</span>();</span></p>
<p><span style="" lang="EN-US"><span style=""> </span>}</span></p>

你可能感兴趣的:(Stream)