| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 629 人关注过本帖
标题:关于”文件合并“代码的问题
只看楼主 加入收藏
poo09876
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-5-18
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
关于”文件合并“代码的问题
大一新生一枚,照着课本做一个文件分割与文件合并工具软件,文件分割没什么问题,但文件合并总是出问题。举个例子,分割出来三个文件,照课本上说的只需要选择分割文件中编号“0001”的小文件,通过Directory类的GetFiles找到源文件所在目录中文件名只编号不同的所有文件,计算出该类型文件的树木,通过循环按此目录文件名称的顺序读取文件,并使用BinaryWriter不断追加,循环技术及合并文件完成。
但是我做出来后选择编号“0001”文件后合并虽然名字变了但内容还是“0001”的内容,照着课本打的代码,不知道哪错了??求助!!!!
文件合并的代码如下:

        private void btnMerge_Click(object sender, EventArgs e)
        {
            string filename = "";
            string[] arrFileNames;
            int iFileCount = 0;
            FileStream desMergeStream = null;
            BinaryWriter desMergeBinaryWriter = null;
            FileStream srcStream = null;
            BinaryReader srcReader = null;
            try
            {
                filename = fileName + "*." + fileExtra;
                arrFileNames = Directory.GetFiles(srcMerFilename, filename);
                iFileCount = arrFileNames.Length;
                filename = fileName.Substring(0, fileName.Length - 4);
                progBarMerge.Maximum = iFileCount;

                desMergeStream = new FileStream(txtMergDes.Text + "\\" + filename + "." + fileExtra, FileMode.OpenOrCreate);
                desMergeBinaryWriter = new BinaryWriter(desMergeStream);

                for (int i = 0; i < iFileCount; i++)
                {
                    srcStream = new FileStream(arrFileNames[i], FileMode.Open);
                    srcReader = new BinaryReader(srcStream);
                    desMergeBinaryWriter.Write(srcReader.ReadBytes((int)srcStream.Length));
                    srcReader.Close();
                    srcStream.Close();
                    progBarMerge.Value = i + 1;
                }
                desMergeBinaryWriter.Close();
                desMergeStream.Close();
                MessageBox.Show("成功合并!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (desMergeStream != null) desMergeStream.Close();
                if (desMergeBinaryWriter != null) desMergeBinaryWriter.Close();
                if (srcStream != null) srcStream.Close();
                if (srcReader != null) srcReader.Close();
                txtMerSrc.Clear();
                txtMergDes.Clear();
                srcMerFilename = null;
                desMerFilename = null;
                fileName = "";
                fileExtra = "";
                srcMerFilename = "";
                progBarMerge.Value = 0;
            }
        }



整个软件的代码如下:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using

namespace WindowsFormsApplication1
{
    public partial class MainFrm : Form
    {
        string srcSplFilename = null;
        string desSplFilename = null;
        string srcMerFilename = null;
        string desMerFilename = null;
        string fileName = "";
        string fileExtra = "";

        public MainFrm()
        {
            InitializeComponent();
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {

        }

        private void btnSource_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDlg = new OpenFileDialog();
            if (openDlg.ShowDialog() == DialogResult.OK && openDlg.FileName != string.Empty)
            {
                srcSplFilename = openDlg.FileName;
                txtSourceDir.Text = srcSplFilename;
            }
        }

        private void btnDes_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDlg = new FolderBrowserDialog();
            folderDlg.Description = "请选择目的文件存放的文件夹";

            if (folderDlg.ShowDialog() == DialogResult.OK)
            {
                desSplFilename = folderDlg.SelectedPath;
                txtDesDir.Text = desSplFilename;
            }
            else MessageBox.Show("您没有选择目录");
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            int desFileSize = 0;
            int desFileCount = 0;
            byte[] buffer;
            string[] desExtra;
            string filename;
            FileStream splitFileStream = null;
            BinaryReader splitFileReader = null;
            FileStream desFileStream = null;
            BinaryWriter desBinaryWriter = null;
            try
            {
                splitFileStream = new FileStream(srcSplFilename, FileMode.Open);
                splitFileReader = new BinaryReader(splitFileStream);

                desFileSize = int.Parse(cboSize.SelectedItem.ToString()) * 1024 * 1024;
                desFileCount = (int)(splitFileStream.Length / desFileSize);
                if (splitFileStream.Length % desFileSize != 0)
                    desFileCount++;
                desExtra = srcSplFilename.Split('.');
                filename = srcSplFilename.Substring(srcSplFilename.LastIndexOf("\\") + 1,
                    srcSplFilename.LastIndexOf(".") - (srcSplFilename.LastIndexOf("\\") + 1));

                progBarSplit.Maximum = desFileCount - 1;

                for (int i = 1; i <= desFileCount; i++)
                {
                    string desFileName = @desSplFilename + @"\" + filename + i.ToString().PadLeft(4, '0') + "." + desExtra[desExtra.Length - 1];

                    desFileStream = new FileStream(desFileName, FileMode.OpenOrCreate);
                    desBinaryWriter = new BinaryWriter(desFileStream);

                    buffer = splitFileReader.ReadBytes(desFileSize);
                    desBinaryWriter.Write(buffer);

                    desBinaryWriter.Close();
                    desFileStream.Close();

                    progBarSplit.Value = i - 1;
                }
                splitFileReader.Close();
                splitFileStream.Close();

                MessageBox.Show("分割成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (desBinaryWriter != null) desBinaryWriter.Close();
                if (desFileStream != null) desFileStream.Close();
                if (splitFileReader != null) splitFileReader.Close();
                if (splitFileStream != null) splitFileStream.Close();
                txtSourceDir.Clear();
                txtDesDir.Clear();
                txtSourceDir.Focus();
                desSplFilename = null;
                srcSplFilename = null;
                cboSize.SelectedIndex = 0;
                progBarSplit.Value = 0;
            }
        }

        private void btnMergSrc_Click(object sender, EventArgs e)
        {
            string[] srcPath;
            string[] filename;
            string strTemp = "";
            int i = 0;

            OpenFileDialog openDlg = new OpenFileDialog();
            openDlg.Title = "请选择要合并的第一个文件";
            if (openDlg.ShowDialog() == DialogResult.OK && openDlg.FileName != "")
            {
                txtMerSrc.Text = openDlg.FileName;
            }
            srcPath = openDlg.FileName.Split(@"\".ToCharArray());
            for (i = 0; i < srcPath.Length - 1; i++)
            {
                strTemp = strTemp + srcPath[i] + @"\";
            }
            srcMerFilename = strTemp;

            filename = srcPath[srcPath.Length - 1].Split(@".".ToCharArray());
            for (i = 0; i < filename.Length - 1; i++)
            {
                fileName = fileName + filename[i] + @".";
            }
            fileName = fileName.Substring(0, fileName.Length - 1);
            fileExtra = filename[filename.Length - 1];
        }

        private void btnMergDes_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDlg = new FolderBrowserDialog();
            folderDlg.Description = "请选择目的文件存放的文件夹";

            if (folderDlg.ShowDialog() == DialogResult.OK)
            {
                desMerFilename = folderDlg.SelectedPath;
                txtMergDes.Text = desMerFilename;
            }
            else MessageBox.Show("你没有选择目录");
        }

        private void btnMerge_Click(object sender, EventArgs e)
        {
            string filename = "";
            string[] arrFileNames;
            int iFileCount = 0;
            FileStream desMergeStream = null;
            BinaryWriter desMergeBinaryWriter = null;
            FileStream srcStream = null;
            BinaryReader srcReader = null;
            try
            {
                filename = fileName + "*." + fileExtra;
                arrFileNames = Directory.GetFiles(srcMerFilename, filename);
                iFileCount = arrFileNames.Length;
                filename = fileName.Substring(0, fileName.Length - 4);
                progBarMerge.Maximum = iFileCount;

                desMergeStream = new FileStream(txtMergDes.Text + "\\" + filename + "." + fileExtra, FileMode.OpenOrCreate);
                desMergeBinaryWriter = new BinaryWriter(desMergeStream);

                for (int i = 0; i < iFileCount; i++)
                {
                    srcStream = new FileStream(arrFileNames[i], FileMode.Open);
                    srcReader = new BinaryReader(srcStream);
                    desMergeBinaryWriter.Write(srcReader.ReadBytes((int)srcStream.Length));
                    srcReader.Close();
                    srcStream.Close();
                    progBarMerge.Value = i + 1;
                }
                desMergeBinaryWriter.Close();
                desMergeStream.Close();
                MessageBox.Show("成功合并!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (desMergeStream != null) desMergeStream.Close();
                if (desMergeBinaryWriter != null) desMergeBinaryWriter.Close();
                if (srcStream != null) srcStream.Close();
                if (srcReader != null) srcReader.Close();
                txtMerSrc.Clear();
                txtMergDes.Clear();
                srcMerFilename = null;
                desMerFilename = null;
                fileName = "";
                fileExtra = "";
                srcMerFilename = "";
                progBarMerge.Value = 0;
            }
        }


        
    }
}
搜索更多相关主题的帖子: 大一新生 源文件 技术 课本 软件 
2014-05-18 09:13
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:0 
因为文件名没有取正确
例如分割的文件为test0001.dll  test0002.dll
你搜索该目录下文件的时候,使用的文件名是 test0001*.dll,这样是肯定搜不到test0002.dll的,所以合并以后,只会有你选择的第一个文件的内容
也就是filename = fileName + "*." + fileExtra;这句中fileName的值是不正确的
btnMergSrc_Click事件里面处理文件名的时候要注意一下
2014-05-22 23:05
poo09876
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-5-18
收藏
得分:0 
回复 2 楼 yhlvht
是要在btnMergSrc_Click中给fileName中的"."中加入*么?还是有点晕,能不能解释的再详细一点,多谢啦!!!
2014-05-24 08:41
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:20 
这个问题1是在你分割文件的时候就要注意了
就像之前所说,假如一个test.dll文件,被分割成test0001.dll,test0002.dll,合并的时候,你无法取到test*.dll,就不能找到所有的分割文件
如果你分割的文件名为test.dll.0001,test.dll.0002,合并的时候就可以用test.dll.* 找到所有的分割文件
2是如果你加的编号字符数是固定的,例如就是4位数字,像test0001.dll,test0002.dll,你也可以取到文件名test0001后,截取长度减4位,就能取到test,再使用test*.dll找所有的分割文件
2014-05-24 10:02
poo09876
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-5-18
收藏
得分:0 
回复 4 楼 yhlvht
具体在哪里改动,代码是什么,我直接把filename=fileName+"*."+fileExtra;这一句删了还是不行,或者我又加了一句fileName=fileName.Substring(0,fileName.Length-4);也不行,直接告诉我怎么改吧,麻烦你了。
2014-05-24 13:00
快速回复:关于”文件合并“代码的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037730 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved