当前位置: 移动技术网 > IT编程>开发语言>.net > .Net实现合并文件的具体方法

.Net实现合并文件的具体方法

2017年12月12日  | 移动技术网IT编程  | 我要评论
以上列表中的文件并不是来自于某个文件夹中的所有jpg文件,而是来自于 这个文件。将多个文件合并为一个文件在许多应用领域都十分有用。亲自实现这样一个程序一定不但过瘾

以上列表中的文件并不是来自于某个文件夹中的所有jpg文件,而是来自于

这个文件。
将多个文件合并为一个文件在许多应用领域都十分有用。亲自实现这样一个程序一定不但过瘾且在许多时候可以帮助我们构建更高效的程序。这里我做了一个方案例分享给大家。
由于合并后的文件就像一个包裹,所以下文中都把这样的文件称为“包文件”
主构思:
要把多个文件合并成一个包文件,还要可以区分其中的某个文件并提取出来。我们需要知道文件的名称和这个文件在包文件中的位置及长度,也就是所谓的地址偏移。
由于包文件常常会比较大,所以不应该让它的内容常驻于内存,只应该需要某部分的时候再从包文件中提取。
我是这样做的:

一个管理器类,提供一些外围的方法
_pathlist用于存放要添加到包文件的文件路径,通过调用addsourcefile()方法添加
_pf 是具体的包文件,通过loadpackfile() 生成实例,通过currentpackfile属性返回
build方法用于生成包文件

packfile类作为packfilemanager的嵌套类,它提供包文件的属性和施工细节。
好了,我们先来看看packfilemanager.build()方法

复制代码 代码如下:

           public void build(string path)
        {
            using (filestream fs = new filestream(path, filemode.create, fileaccess.write))
            {
                binarywriter bw = new binarywriter(fs);
                bw.write("packfile");
                bw.write(this._pathlist.count);
                foreach (string f in this._pathlist)
                {
                    fileinfo fi = new fileinfo(f);
                    bw.write(fi.length);
                    fi = null;
                }
                foreach (string f in this._pathlist)
                {
                    bw.write(path.getfilename(f));
                }
                foreach (string f in this._pathlist)
                {
                    bw.write(file.readallbytes(f));
                    bw.flush();
                }
            }
        }

1. 先写个“packfile”字符串到文件头
2. 把以int32为类型的,要输出到包文件中的文件数量写入
3. 把以long为类型的,要输出到包文件中的每个文件的长度写入。
4. 再把每个文件名写入
5. 最后写入每个文件的实体内容。
由于在写或读时不频繁在write方法或readxxx方法的不同版本间频繁切换,所以我想这样组织文件结构可以更高效一些。

疑问来了。在写入文件名的时候,我们使用bw.write(path.getfilename(f));
调用了binarywriter.write(string value),传入的是字符串,那么在读取的时候要调用binaryreader.readstring()。这时它是如何区分两个字符串边界的。还好,write方法会先将字符串长度作为一个四字节无符号整数写入,于是在用binaryreader.readstring()的时候它会根据这个值来读取特定长度的值,并理解为字符串。
这里列出几个重要方法:

复制代码 代码如下:

packfilemanager的loadpackfile方法
       public void loadpackfile(string path)
        {
            if (!file.exists(path))
            {
                throw new filenotfoundexception(path);
            }
            if (_pf != null)
            {
                _pf.close();
                _pf = null;
            }
            filestream fs = new filestream(path, filemode.open, fileaccess.read);
            binaryreader br = new binaryreader(fs);
            if (br.readstring() != "packfile")
            {
                throw new invalidcoalescentfileexception("该文件不是有效的包文件");
            }
            this._pf = new packfile(fs,br);
        }

此时,我们在生成时写入的字符串"packfile" 就有了明确的功能
packfile的构造函数
复制代码 代码如下:

        internal packfile(filestream srcfile,binaryreader br)
            {
                this._sourcefile = srcfile;
                _br = br;
                this._filecount = _br.readint32();//取文件数
                for (int i = 1; i <= _filecount; i++)
                {
                    this._filelengthlist.add(_br.readint64());
                }
                for (int i = 1; i <= _filecount; i++)
                {
                    this._shortnamelist.add(_br.readstring());
                }
                this._contentstartpos = _sourcefile.position;//设置实体文件总起始位置
            }
 

packfile.getbytes()
复制代码 代码如下:

            public byte[] getbytes(int index)
            {
                long startpos = this._contentstartpos;
                for (int i = 0; i < index; i++)
                {
                    startpos += this._filelengthlist[i];
                }
                _sourcefile.position = startpos; //设置某文件内容的起始位置
                return _br.readbytes((int)_filelengthlist[index]);
            }

这只是一个草案,我们还可以加入压缩、或是像zip文件那样的嵌套文件夹功能,改进后的代码别忘与我分享哦。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网