Typecho合集站.  留言 关于 记事本 
首页 > TYPECHO > typecho教程 > typecho插件 > typecho主题 > 网站开发 > PHP > Linux > web > 编程 > 个人收藏 > 互联网旧文 > 互联网趣文

C# 创建压缩文件


(FileStream fsOut = File.Create( {

(ZipOutputStream zipStream = {

fileName = FileInfo fi =
entryName = ZipEntry newEntry = newEntry.DateTime = newEntry.Size = zipStream.PutNextEntry(newEntry);
[] buffer = [

    <span style="color: #0000ff"&gt;using</span> (FileStream streamReader =<span style="color: #000000"&gt; File.OpenRead(fileName))
    {
        StreamUtils.Copy(streamReader,zipStream,buffer);
    }
    zipStream.CloseEntry();
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;<a href="https://www.jb51.cc/tag/tianjia/" target="_blank" class="keywords">添加</a>多个<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;如果要压缩一个<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>夹,就是通过遍历<a href="https://www.jb51.cc/tag/tianjia/" target="_blank" class="keywords">添加</a><a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>夹下所有的<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a></span>
    <span style="color: #0000ff"&gt;string</span> fileName2 =  <span style="color: #800000"&gt;@"</span><span style="color: #800000"&gt;G:\share\web.dll</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;;
    FileInfo fi2 </span>= <span style="color: #0000ff"&gt;new</span><span style="color: #000000"&gt; FileInfo(fileName2);

    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>在压缩包中的路径</span>
    <span style="color: #0000ff"&gt;string</span> entryName2 = <span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;share\\web.dll</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;;
    ZipEntry newEntry2 </span>= <span style="color: #0000ff"&gt;new</span><span style="color: #000000"&gt; ZipEntry(entryName2);
    newEntry2.DateTime </span>=<span style="color: #000000"&gt; fi2.LastWriteTime;
    newEntry2.Size </span>=<span style="color: #000000"&gt; fi2.Length;
    zipStream.PutNextEntry(newEntry2);
    </span><span style="color: #0000ff"&gt;byte</span>[] buffer2 = <span style="color: #0000ff"&gt;new</span> <span style="color: #0000ff"&gt;byte</span>[<span style="color: #800080"&gt;4096</span><span style="color: #000000"&gt;];
    </span><span style="color: #0000ff"&gt;using</span> (FileStream streamReader =<span style="color: #000000"&gt; File.OpenRead(fileName2))
    {
        StreamUtils.Copy(streamReader,buffer2);
    }
    zipStream.CloseEntry();
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;使用流操作时一定要设置IsStreamOwner为false。否则很容易发生在<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>流<a href="https://www.jb51.cc/tag/guanbi/" target="_blank" class="keywords">关闭</a>后的异常。</span><span style="color: #000000"&gt;
    zipStream.IsStreamOwner </span>= <span style="color: #0000ff"&gt;false</span><span style="color: #000000"&gt;;
    zipStream.Finish();
    zipStream.Close();
}

}


[] string1 = Encoding.UTF8.GetBytes( (FileStream fsOut = File.Create( {
(ZipOutputStream zipStream = {
ZipEntry entry
= ZipEntry( entry.DateTime = zipStream.PutNextEntry(entry);
zipStream.Write(string1, zipStream.CloseEntry();
zipStream.IsStreamOwner
= zipStream.Finish();
zipStream.Close();
}
}

[] string1 = Encoding.UTF8.GetBytes( [] result = (MemoryStream ms = {
(ZipOutputStream zipStream = {
ZipEntry entry
= ZipEntry( entry.DateTime = zipStream.PutNextEntry(entry);
zipStream.Write(string1,string1.Length);
zipStream.CloseEntry();
zipStream.IsStreamOwner
= zipStream.Finish();
zipStream.Close();
ms.Position
= </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;压缩后的数据被保存到了byte[]数组中。</span><span style="color: #000000"&gt; result </span>=<span style="color: #000000"&gt; ms.ToArray(); }}

[] string1 = Encoding.UTF8.GetBytes( (FileStream fsOut = File.Create( {
(ZipOutputStream zipStream = {
ZipEntry entry = ZipEntry( entry.DateTime = ...
}
}

entry.IsUnicodeText = ;

总结

以上是Typecho合集站为你收集整理的C# 创建压缩文件全部内容,希望文章能够帮你解决C# 创建压缩文件所遇到的程序开发问题。

如果觉得Typecho合集站网站内容还不错,欢迎将Typecho合集站网站推荐给程序员好友。


发表新评论


本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除
Theme by
夏目贵志, Powered by Typecho | RSS | sitemap