首页 新闻 会员 周边 捐助

c#文件分割合并的问题

0
悬赏园豆:20 [已关闭问题] 关闭于 2010-03-25 11:44

文件分割代

           FileInfo fInfo = new FileInfo("c:\\xx.rar");
           int splitSize = 1024 * 400;
           int splitCount = (int)fInfo.Length % splitSize==0 ? (int)fInfo.Length / splitSize : (int)fInfo.Length / splitSize + 1;
            FileStream fileStream = new FileStream(fInfo.FullName,FileMode.Open,FileAccess.Read);
            BinaryReader reader = new BinaryReader(fileStream);
            for (int i = 0; i < splitCount; i++)
            {
                string tempName = "temp_" + i.ToString();
                FileStream fs = new FileStream("c:\\tempfile" + tempName,FileMode.CreateNew,FileAccess.Write);
                BinaryWriter tempWriter = new BinaryWriter(fs);
                byte []tempData= reader.ReadBytes(splitSize);
                if (tempData.Length == 0)
                {
                    break;
                }
                tempWriter.Write(tempData, 0, tempData.Length);
                tempWriter.Flush();
                tempWriter.Close();
                fs.Close();
            }
            reader.Close();
            fileStream.Close();

 

文件合并代码:

            DirectoryInfo dir = new DirectoryInfo("D:\\tempfile");
            FileInfo[] fs = dir.GetFiles();
            var sorts = from singlefile in fs
                        orderby singlefile.Name ascending
                        select singlefile;

            string fullname = "D:\\xx.rar"
            FileStream fileStream = new FileStream(fullname,FileMode.CreateNew,FileAccess.Write);
            BinaryWriter writer = new BinaryWriter(fileStream);

            foreach (FileInfo f in sorts)
            {
                FileStream tempStream = new FileStream(f.FullName, FileMode.Open, FileAccess.Read);
                BinaryReader tempReader = new BinaryReader(tempStream);
                byte[] data = tempReader.ReadBytes(1024 * 400);
                writer.Write(data, 0, data.Length);
                writer.Flush();
                tempReader.Close();
                tempStream.Close();
            }
            writer.Flush();
            writer.Close();
            fileStream.Close();

 

对exe文件和其他类型文件都没问题 对压缩文件(rar)分割后合并 出现 不可预料的压缩文件末端

不知道是什么原因?

孤城浪子的主页 孤城浪子 | 初学一级 | 园豆:197
提问于:2010-03-24 15:42
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册