如何将多个文件合并成一个文件?

huheng_0_0 2008-05-28 03:27:01
比如我的程序一共产生了3个文件,a.txt, b.xml, c.jpg, 如何将这个三个文件放在一个自定义文件中,我下次读取的时候,又应该怎么解析,分别得到这个三个文件?
...全文
567 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
huheng_0_0 2008-05-29
  • 打赏
  • 举报
回复
谢谢楼上,其实我不需要将两个文件合并,只是希望将他们捆绑,然后用一个“壳”包装他们,暴露给用户的就是这个壳而已。
如果你愿意,我也非常希望学习一下你的方法。 :)
Tll_W 2008-05-29
  • 打赏
  • 举报
回复
我随便选择了几个文件a.jpg和b.txt,然后我把他们两个分别读取并存到了一个文件中:c.tll,然后我用一个按钮,可以把c.tll分解到一个目录下,然后该目录下就是a.jpg和b.txt两个文件了.如果你是要这个效果,我可以给你些代码参考,如果不是,就当我帮你顶了,^_^
家鸣 2008-05-29
  • 打赏
  • 举报
回复
先生成一个Exe文件作为母版,然后把要捆绑的文件全都放到这个Exe的后面。具体过程就是:读入一个文件到Exe的未部,写入这个文件的大小,如此循环,直到所有文件绑定完毕。

运行时,这个Exe先检查自身文件的大小,如果这个Size不等于母版Exe文件自身,说明自身捆绑了文件,然后从未部根据文件的大小来释放文件,如此循环。
这过程在C++里验证通过。我想C#应该可以实现这样的功能。

另把其它文件写入到Exe的未部,并不改变Exe的文件结构,所以这个Exe文件还可以正常运行。
Tll_W 2008-05-29
  • 打赏
  • 举报
回复

struct CFileInfor
{
public string fileName;
public long fileLength;
}
long GetFileSize(string fileName)
{
FileInfo fi = new FileInfo(fileName);
if (fi != null && fi.Exists == true)
{
return fi.Length;
}
return -1;
}

byte[] GetFileBytes(string fileName)
{
byte[] bytes = null;
try
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
bytes = new byte[fs.Length];
br.Read(bytes, 0, (int)fs.Length);
br.Close();
fs.Close();
}
catch
{ }
return bytes;
}

void CombineOneFile(string[] inFiles, string outFile)
{
FileStream fs = new FileStream(outFile, FileMode.CreateNew, FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(inFiles.Length);

foreach (string inFile in inFiles)
{
string fileName = Path.GetFileName(inFile);
long fileSize = GetFileSize(inFile);
bw.Write(inFile);
bw.Write(fileSize);
}
foreach (string inFile in inFiles)
{
byte[] bytes = GetFileBytes(inFile);
bw.Write(bytes);
}
bw.Close();
fs.Close();
}

void SplitFromOneFile(string oneFile, string outPath)
{
if (Directory.Exists(outPath) == false)
{
Directory.CreateDirectory(outPath);
}
FileStream fs = new FileStream(oneFile, FileMode.Open, FileAccess.Read);
int count = 0;
BinaryReader br = new BinaryReader(fs);
count = br.ReadInt32();
CFileInfor[] files = new CFileInfor[count];
for (int i = 1; i <= count; i++)
{
files[i - 1] = new CFileInfor();
files[i - 1].fileName = br.ReadString();
files[i - 1].fileLength = br.ReadInt64();
}
foreach (CFileInfor file in files)
{
byte[] bytes = null;
bytes = br.ReadBytes((int)file.fileLength);
FileStream fsTemp = new FileStream(outPath + "\\" + file.fileName, FileMode.Create, FileAccess.Write);
fsTemp.Write(bytes, 0, bytes.Length);
fsTemp.Close();
}
br.Close();
fs.Close();
}
huheng_0_0 2008-05-28
  • 打赏
  • 举报
回复
当然可以采取把这三个文件放入一个文件夹,然后调用ziplib压缩这个文件夹,然后更改后缀名。但是有没有别的办法呢?
请问ChrisAK,你说的.net自带的,能稍微详细一点吗,或者链接。
huheng_0_0 2008-05-28
  • 打赏
  • 举报
回复
不知道我是不是没有理解大家的意思,还是我没说清楚,我并不需要对这三个文件进行压缩或者更改,我只是希望,这3个文件不要分散放置,而是像放在一个文件夹里一样,但是暴露给用户的,仅到这个所谓的文件夹级别。
ChrisAK 2008-05-28
  • 打赏
  • 举报
回复
要压缩的话就找找#ziplib之类的开源格式.
懒的话.net自己带的资源文件格式也不错,而且读写都是库直接支持的.

不嫌麻烦的话自己写也不错.
gomoku 2008-05-28
  • 打赏
  • 举报
回复

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();
}
}

kingstou 2008-05-28
  • 打赏
  • 举报
回复
用ZIP庫
marvelstack 2008-05-28
  • 打赏
  • 举报
回复
最简洁的做法,如微软的新Office文档格式,使用压缩包。
huheng_0_0 2008-05-28
  • 打赏
  • 举报
回复
可能我没有表达清楚,其实我的意思是不要改变这三个文件,只是相当于做了一个zip文件一样,把这三个文件放在里面,用winRar压缩文件管理器查看的时候,可以看见这个三个独立的文件,只是外面看,这是一个叫做combination.cbn(自定义)的文件而已。:)
baihe_591 2008-05-28
  • 打赏
  • 举报
回复


private void hebin()
{
int data=0;
FileStream targefile = new FileStream("a.txt", FileMode.Append , FileAccess.Write);
FileStream newfile = new FileStream("b.txt", FileMode.OpenOrCreate, FileAccess.Read);
byte [] buffer=new byte [newfile .Length ];
while ((data= newfile .Read (buffer ,0,(int)newfile .Length ))>0)
{
targefile .Write (buffer ,0,(int )newfile .Length );
}
newfile .Close ();
targefile .Close ();
}
Tll_W 2008-05-28
  • 打赏
  • 举报
回复
我说下我的思路啊。。。
文件个数
文件名,文件长度
文件名,文件长度
......
文件名,文件长度
文件二进制流
例如:
3
a.txt,1435
b.xml,12334
c,jpg,234920
234920+1435+12334个字节
读取的时候方法:
先读取3
之后读取3行:文件名和长度
最后把后面的二进制流读到byte[] bytes中,然后截断分别写入文件就ok
baihe_591 2008-05-28
  • 打赏
  • 举报
回复
将文件内容转换成二进制,追加到目标文件里面.

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧