• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#文件流合并(未测试是否可用)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
private void button1_Click(object sender, EventArgs e)
        {
            Packer.Pack(
@"c:\temp\all.dat", @"c:\temp\a.bat", @"c:\temp\a.jpg");
           
string[] resultFiles = Packer.Unpack( @"c:\temp\all.dat" );

            MessageBox.Show(
string.Join("\n", resultFiles), "files generated under the current directory");
        }
   
   
   
public class Packer
    {
       
class Header
        {
           
public long length;
           
public string filename;

           
public void WriteTo( Stream fs )
            {
               
byte[] len = BitConverter.GetBytes(length);
               
byte[] buf = new byte[256];

               
byte[] str = Encoding.Unicode.GetBytes(filename);
                str.CopyTo(buf,
0);

                fs.Write(len,
0, len.Length);
                fs.Write(buf,
0, buf.Length);
            }
           
public bool ReadFrom( Stream fs)
            {
               
byte[] len = BitConverter.GetBytes(length);
               
byte[] buf = new byte[256];

               
if (len.Length != fs.Read(len, 0, len.Length)) return false;
               
if (buf.Length != fs.Read(buf, 0, buf.Length)) return false;

                length
= BitConverter.ToInt64(len, 0);
                filename
= Encoding.Unicode.GetString(buf).Trim(new char[] { '\0' });
               
return true;
            }
        }

       
public static void Pack(string resultFilename, params string[] filenames)
        {
           
using (FileStream fout = new FileStream(resultFilename, FileMode.Create, FileAccess.Write))
            {
               
for (int i = 0; i < filenames.Length; i++)
                {
                   
using (FileStream fin = new FileStream(filenames[i], FileMode.Open))
                    {
                        Header header
= new Header();
                        header.length
= fin.Length;
                        header.filename
= Path.GetFileName(filenames[i]);
                        header.WriteTo(fout);

                       
byte[] buf = new byte[header.length];
                        fin.Read(buf,
0, buf.Length);
                        fout.Write(buf,
0, buf.Length);
                    }
                }
            }
        }

       
public static string[] Unpack(string filename)
        {
            List
<string> unpackedFiles = new List<string>();

           
using (FileStream fin = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                Header header
= new Header();
               
while (header.ReadFrom(fin))
                {
                    unpackedFiles.Add(header.filename);
                   
byte[] buf = new byte[header.length];
                    fin.Read(buf,
0, buf.Length);

                   
using (FileStream fout = new FileStream(header.filename, FileMode.Create, FileAccess.Write))
                    {
                        fout.Write(buf,
0, buf.Length);
                    }
                }
            }
           
return unpackedFiles.ToArray();
        }
    }

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#TCPSocket通讯基础发布时间:2022-07-13
下一篇:
C#中pictureBox笔记发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap